hdt: Don't search HDDs when no HDD got detected
[profile/ivi/syslinux.git] / com32 / hdt / hdt-dump-disks.c
1 /* ----------------------------------------------------------------------- *
2  *
3  *   Copyright 2011 Erwan Velu - All Rights Reserved
4  *
5  *   Permission is hereby granted, free of charge, to any person
6  *   obtaining a copy of this software and associated documentation
7  *   files (the "Software"), to deal in the Software without
8  *   restriction, including without limitation the rights to use,
9  *   copy, modify, merge, publish, distribute, sublicense, and/or
10  *   sell copies of the Software, and to permit persons to whom
11  *   the Software is furnished to do so, subject to the following
12  *   conditions:
13  *
14  *   The above copyright notice and this permission notice shall
15  *   be included in all copies or substantial portions of the Software.
16  *
17  *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
19  *   OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20  *   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21  *   HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22  *   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23  *   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24  *   OTHER DEALINGS IN THE SOFTWARE.
25  *
26  * -----------------------------------------------------------------------
27  */
28
29 #include "hdt-common.h"
30 #include "hdt-dump.h"
31 #include "hdt-util.h"
32
33 ZZJSON_CONFIG *config;
34 ZZJSON **item;
35
36 static void show_partition_information(struct driveinfo *drive_info,
37                                        struct part_entry *ptab,
38                                        int partition_offset,
39                                        int nb_partitions_seen) {
40     char size[11] = {0};
41     char bootloader_name[9] = {0};
42     char ostype[64]={0};
43     char *parttype;
44     unsigned int start, end;
45     char bootable[6] = {0};
46
47     int i = nb_partitions_seen;
48     start = partition_offset;
49     end = start + ptab->length - 1;
50
51     if (ptab->length > 0)
52         sectors_to_size(ptab->length, size);
53
54     get_label(ptab->ostype, &parttype);
55     get_bootloader_string(drive_info, ptab, bootloader_name, 9);
56     if (ptab->active_flag == 0x80)
57         snprintf(bootable,sizeof(bootable),"%s","true");
58     else
59         snprintf(bootable,sizeof(bootable),"%s","false");
60
61     snprintf(ostype,sizeof(ostype),"%02X",ptab->ostype);
62
63     APPEND_ARRAY
64             add_ai("partition->number",i)
65             add_ai("partition->sector_start",start)
66             add_ai("partition->sector_end",end)
67             add_as("partition->size",size)
68             add_as("partition->type",parttype)
69             add_as("partition->os_type",ostype)
70             add_as("partition->boot_flag",bootable)
71     END_OF_APPEND;
72     free(parttype);
73 }
74
75
76
77 void show_disk(struct s_hardware *hardware, ZZJSON_CONFIG *conf, ZZJSON **it, int drive) {
78         config=conf;
79         item=it;
80         int i = drive - 0x80;
81         struct driveinfo *d = &hardware->disk_info[i];
82         char mbr_name[50]={0};
83         char disk_size[11]={0};
84
85         get_mbr_string(hardware->mbr_ids[i], &mbr_name,sizeof(mbr_name));
86         if ((int)d->edd_params.sectors > 0)
87                 sectors_to_size((int)d->edd_params.sectors, disk_size);
88
89         char disk[5]={0};
90         char edd_version[5]={0};
91         snprintf(disk,sizeof(disk),"0x%X",d->disk);
92         snprintf(edd_version,sizeof(edd_version),"%X",d->edd_version);
93         zzjson_print(config, *item);
94         zzjson_free(config, *item);
95
96         CREATE_ARRAY
97                 add_as("disk->number",disk) 
98                 add_ai("disk->cylinders",d->legacy_max_cylinder +1) 
99                 add_ai("disk->heads",d->legacy_max_head +1)
100                 add_ai("disk->sectors_per_track",d->legacy_sectors_per_track)
101                 add_as("disk->edd_version",edd_version)
102                 add_as("disk->size",disk_size)
103                 add_ai("disk->bytes_per_sector",(int)d->edd_params.bytes_per_sector)
104                 add_ai("disk->sectors_per_track",(int)d->edd_params.sectors_per_track)
105                 add_as("disk->host_bus",remove_spaces((char *)d->edd_params.host_bus_type))
106                 add_as("disk->interface_type",remove_spaces((char *)d->edd_params.interface_type))
107                 add_as("disk->mbr_name",mbr_name)
108                 add_ai("disk->mbr_id",hardware->mbr_ids[i])
109         END_OF_ARRAY;
110
111         if (parse_partition_table(d, &show_partition_information)) {
112                 if (errno_disk) { 
113                         APPEND_ARRAY
114                                 add_as("disk->error", "IO Error")
115                         END_OF_APPEND;
116                 } else  {
117                         APPEND_ARRAY
118                                 add_as("disk->error", "Unrecognized Partition Layout")
119                         END_OF_APPEND;
120                 }
121         }
122 }
123
124 void dump_disks(struct s_hardware *hardware, ZZJSON_CONFIG *config, ZZJSON **item) {
125         bool found=false;
126
127         if (hardware->disks_count > 0)  
128             for (int drive = 0x80; drive < 0xff; drive++) {
129                 if (hardware->disk_info[drive - 0x80].cbios) {
130                         if (found==false) {
131                                 CREATE_NEW_OBJECT;
132                                 add_b("disks->is_valid",true);
133                                 found=true;
134                         }
135                         show_disk(hardware, config, item, drive);
136                 }
137         }
138
139         if (found==false) {
140                 CREATE_NEW_OBJECT;
141                 add_b("disks->is_valid",false);
142         }
143         FLUSH_OBJECT;
144         to_cpio("disks");
145 }