• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* ----------------------------------------------------------------------- *
2  *
3  *   Copyright 2009 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 <stdlib.h>
30 
31 #include "hdt-menu.h"
32 #include "hdt-util.h"
33 
34 static int dn;
35 
show_partition_information(struct driveinfo * drive_info,struct part_entry * ptab __unused,int partition_offset __unused,int nb_partitions_seen)36 static void show_partition_information(struct driveinfo *drive_info,
37 				       struct part_entry *ptab __unused,
38 				       int partition_offset __unused,
39 				       int nb_partitions_seen)
40 {
41     char menu_title[MENULEN + 1];
42     char menu_title_ref[MENULEN + 1];
43 
44     if (nb_partitions_seen == 1)
45 	add_sep();
46 
47     memset(menu_title, 0, sizeof menu_title);
48     memset(menu_title_ref, 0, sizeof menu_title_ref);
49     snprintf(menu_title_ref, sizeof menu_title_ref, "disk_%x_part_%d",
50 	     drive_info[dn].disk, nb_partitions_seen);
51     snprintf(menu_title, sizeof menu_title, "Partition %d", nb_partitions_seen);
52 
53     add_item(menu_title,
54 	     "Partition information (start, end, length, type, ...)",
55 	     OPT_SUBMENU, menu_title_ref, 0);
56 }
57 
58 /**
59  * compute_partition_information - print information about a partition
60  * @ptab:       part_entry describing the partition
61  * @i:          Partition number (UI purposes only)
62  * @ptab_root:  part_entry describing the root partition (extended only)
63  * @drive_info: driveinfo struct describing the drive on which the partition
64  *              is
65  *
66  * Note on offsets (from hpa, see chain.c32):
67  *
68  *  To make things extra confusing: data partition offsets are relative to where
69  *  the data partition record is stored, whereas extended partition offsets
70  *  are relative to the beginning of the extended partition all the way back
71  *  at the MBR... but still not absolute!
72  **/
compute_partition_information(struct driveinfo * drive_info,struct part_entry * ptab,int partition_offset,int nb_partitions_seen)73 static void compute_partition_information(struct driveinfo *drive_info,
74 					  struct part_entry *ptab,
75 					  int partition_offset,
76 					  int nb_partitions_seen)
77 {
78     char size[11];
79     char bootloader_name[9];
80     char *parttype;
81     unsigned int start, end;
82     char buffer[SUBMENULEN + 1];
83     char statbuffer[STATLEN + 1];
84     char menu_title[MENULEN + 1];
85     char menu_title_ref[MENULEN + 1];
86 
87     memset(buffer, 0, sizeof buffer);
88     memset(statbuffer, 0, sizeof statbuffer);
89     memset(menu_title, 0, sizeof menu_title);
90     memset(menu_title_ref, 0, sizeof menu_title_ref);
91     snprintf(menu_title_ref, sizeof menu_title_ref, "disk_%x_part_%d",
92 	     drive_info[dn].disk, nb_partitions_seen);
93     snprintf(menu_title, sizeof menu_title, "Partition %d", nb_partitions_seen);
94 
95     add_named_menu(menu_title_ref, menu_title, -1);
96     set_menu_pos(SUBMENU_Y, SUBMENU_X);
97 
98     start = partition_offset;
99     end = start + ptab->length - 1;
100 
101     if (ptab->length > 0)
102 	sectors_to_size(ptab->length, size);
103     else
104 	memset(size, 0, sizeof size);
105 
106     get_label(ptab->ostype, &parttype);
107 
108     snprintf(buffer, sizeof buffer, "Size        : %s", remove_spaces(size));
109     snprintf(statbuffer, sizeof statbuffer, "Size : %s", remove_spaces(size));
110     add_item(buffer, statbuffer, OPT_INACTIVE, NULL, 0);
111 
112     snprintf(buffer, sizeof buffer, "Type        : %s", parttype);
113     snprintf(statbuffer, sizeof statbuffer, "Type: %s", parttype);
114     add_item(buffer, statbuffer, OPT_INACTIVE, NULL, 0);
115 
116     if (get_bootloader_string(drive_info, ptab, bootloader_name, 9) == 0) {
117 	snprintf(buffer, sizeof buffer, "Bootloader  : %s", bootloader_name);
118 	snprintf(statbuffer, sizeof statbuffer, "Bootloader: %s",
119 		 bootloader_name);
120 	add_item(buffer, statbuffer, OPT_INACTIVE, NULL, 0);
121     }
122 
123     snprintf(buffer, sizeof buffer, "Boot Flag   : %s",
124 	     (ptab->active_flag == 0x80) ? "Yes" : "No");
125     snprintf(statbuffer, sizeof statbuffer, "Boot Flag: %s",
126 	     (ptab->active_flag == 0x80) ? "Yes" : "No");
127     add_item(buffer, statbuffer, OPT_INACTIVE, NULL, 0);
128 
129     snprintf(buffer, sizeof buffer, "Start       : %d", start);
130     snprintf(statbuffer, sizeof statbuffer, "Start: %d", start);
131     add_item(buffer, statbuffer, OPT_INACTIVE, NULL, 0);
132 
133     snprintf(buffer, sizeof buffer, "End         : %d", end);
134     snprintf(statbuffer, sizeof statbuffer, "End: %d", end);
135     add_item(buffer, statbuffer, OPT_INACTIVE, NULL, 0);
136 
137     snprintf(buffer, sizeof buffer, "Id          : %X", ptab->ostype);
138     snprintf(statbuffer, sizeof statbuffer, "Id: %X", ptab->ostype);
139     add_item(buffer, statbuffer, OPT_INACTIVE, NULL, 0);
140 
141     free(parttype);
142 
143     /* Extra info */
144     if (ptab->ostype == 0x82 && swsusp_check(drive_info, ptab) != -1) {
145 	snprintf(buffer, sizeof buffer, "%s", "Swsusp sig  : detected");
146 	snprintf(statbuffer, sizeof statbuffer, "%s", "Swsusp sig  : detected");
147 	add_item(buffer, statbuffer, OPT_INACTIVE, NULL, 0);
148     }
149 }
150 
151 /* Compute the disk submenu */
compute_disk_module(struct s_my_menu * menu,int nb_sub_disk_menu,const struct s_hardware * hardware,int disk_number)152 static int compute_disk_module(struct s_my_menu *menu, int nb_sub_disk_menu,
153 			       const struct s_hardware *hardware,
154 			       int disk_number)
155 {
156     char buffer[MENULEN + 1];
157     char statbuffer[STATLEN + 1];
158     char mbr_name[50];
159     struct driveinfo *d = (struct driveinfo *)hardware->disk_info;
160 
161     snprintf(buffer, sizeof buffer, " Disk <0x%X> (EDD %X)",
162 	     d[disk_number].disk, d[disk_number].edd_version);
163     menu[nb_sub_disk_menu].menu = add_menu(buffer, -1);
164     menu[nb_sub_disk_menu].items_count = 0;
165 
166     int previous_size, size;
167     char previous_unit[3], unit[3];	// GB
168     char size_iec[11];		// GiB
169     char size_dec[11];		// GB
170     sectors_to_size_dec(previous_unit, &previous_size, unit, &size,
171 			d[disk_number].edd_params.sectors);
172     sectors_to_size(d[disk_number].edd_params.sectors, size_iec);
173     sectors_to_size_dec2(d[disk_number].edd_params.sectors, size_dec);
174 
175     snprintf(buffer, sizeof buffer, "Size              : %s/%s (%d %s)",
176 	     remove_spaces(size_iec), remove_spaces(size_dec), previous_size,
177 	     previous_unit);
178     snprintf(statbuffer, sizeof statbuffer, "Size: %s/%s (%d %s)",
179 	     remove_spaces(size_iec), remove_spaces(size_dec), previous_size,
180 	     previous_unit);
181     add_item(buffer, statbuffer, OPT_INACTIVE, NULL, 0);
182     menu[nb_sub_disk_menu].items_count++;
183 
184     /* Do not print Host Bus & Interface if EDD isn't 3.0 or more */
185     if (d[disk_number].edd_version >= 0x30) {
186 	snprintf(buffer, sizeof buffer, "Host Bus/Interface: %s / %s",
187 		 remove_spaces((char *)d[disk_number].edd_params.host_bus_type),
188 		 d[disk_number].edd_params.interface_type);
189 	snprintf(statbuffer, sizeof statbuffer, "Host Bus / Interface: %s / %s",
190 		 remove_spaces((char *)d[disk_number].edd_params.host_bus_type),
191 		 d[disk_number].edd_params.interface_type);
192 	add_item(buffer, statbuffer, OPT_INACTIVE, NULL, 0);
193 	menu[nb_sub_disk_menu].items_count++;
194     }
195 
196     snprintf(buffer, sizeof buffer, "C / H / S         : %d / %d / %d",
197 	     d[disk_number].legacy_max_cylinder + 1,
198 	     d[disk_number].legacy_max_head + 1,
199 	     (int)d[disk_number].edd_params.sectors);
200     snprintf(statbuffer, sizeof statbuffer,
201 	     "Cylinders / Heads / Sectors: %d / %d / %d",
202 	     d[disk_number].legacy_max_cylinder + 1,
203 	     d[disk_number].legacy_max_head + 1,
204 	     (int)d[disk_number].edd_params.sectors);
205     add_item(buffer, statbuffer, OPT_INACTIVE, NULL, 0);
206     menu[nb_sub_disk_menu].items_count++;
207 
208     snprintf(buffer, sizeof buffer, "Sectors/Track     : %d",
209 	     d[disk_number].legacy_sectors_per_track);
210     snprintf(statbuffer, sizeof statbuffer, "Sectors per Track: %d",
211 	     d[disk_number].legacy_sectors_per_track);
212     add_item(buffer, statbuffer, OPT_INACTIVE, NULL, 0);
213     menu[nb_sub_disk_menu].items_count++;
214 
215     get_mbr_string(hardware->mbr_ids[disk_number], &mbr_name, 50);
216 
217     snprintf(buffer, sizeof buffer, "MBR               : %s (0x%X)",
218 	     remove_spaces(mbr_name), hardware->mbr_ids[disk_number]);
219     snprintf(statbuffer, sizeof statbuffer, "MBR: %s (id 0x%X)",
220 	     remove_spaces(mbr_name), hardware->mbr_ids[disk_number]);
221     add_item(buffer, statbuffer, OPT_INACTIVE, NULL, 0);
222     menu[nb_sub_disk_menu].items_count++;
223 
224     dn = disk_number;
225 
226     parse_partition_table(&d[disk_number], &show_partition_information);
227     if (!parse_partition_table(&d[disk_number], &compute_partition_information)) {
228 	get_error("parse_partition_table");
229 	menu[nb_sub_disk_menu].items_count++;
230     }
231 
232     return 0;
233 }
234 
235 /* Compute the Disks menu */
compute_disks(struct s_hdt_menu * menu,struct s_hardware * hardware)236 void compute_disks(struct s_hdt_menu *menu, struct s_hardware *hardware)
237 {
238     char buffer[MENULEN + 1];
239     int nb_sub_disk_menu = 0;
240 
241     /* No need to compute that menu if no disks were detected */
242     menu->disk_menu.items_count = 0;
243     if (hardware->disks_count == 0)
244 	return;
245 
246     for (int drive = 0x80; drive < 0xff; drive++) {
247 	if (!hardware->disk_info[drive - 0x80].cbios)
248 	    continue;		/* Invalid geometry */
249 	compute_disk_module
250 	    ((struct s_my_menu *)&(menu->disk_sub_menu), nb_sub_disk_menu,
251 	     hardware, drive - 0x80);
252 	nb_sub_disk_menu++;
253     }
254 
255     menu->disk_menu.menu = add_menu(" Disks ", -1);
256 
257     for (int i = 0; i < nb_sub_disk_menu; i++) {
258 	snprintf(buffer, sizeof buffer, " Disk <%d> ", i + 1);
259 	add_item(buffer, "Disk", OPT_SUBMENU, NULL,
260 		 menu->disk_sub_menu[i].menu);
261 	menu->disk_menu.items_count++;
262     }
263     printf("MENU: Disks menu done (%d items)\n", menu->disk_menu.items_count);
264 }
265