1 /* ----------------------------------------------------------------------- *
3 * Copyright 2009 Erwan Velu - All Rights Reserved
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
14 * The above copyright notice and this permission notice shall
15 * be included in all copies or substantial portions of the Software.
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.
26 * -----------------------------------------------------------------------
33 #include "syslinux/config.h"
34 #include "../lib/sys/vesa/vesa.h"
35 #include "hdt-common.h"
36 #include <disk/util.h>
37 #include <disk/mbrs.h>
40 /* ISOlinux requires a 8.3 format */
41 void convert_isolinux_filename(char *filename
, struct s_hardware
*hardware
)
43 /* Exit if we are not running ISOLINUX */
44 if (hardware
->sv
->filesystem
!= SYSLINUX_FS_ISOLINUX
)
46 /* Searching the dot */
47 char *dot
= strchr(filename
, '.');
48 /* Exiting if no dot exists in that string */
51 /* Exiting if the extension is 3 char or less */
55 /* We have an extension bigger than .blah
56 * so we have to shorten it to 3*/
60 void detect_parameters(const int argc
, const char *argv
[],
61 struct s_hardware
*hardware
)
63 /* Quiet mode - make the output more quiet */
66 /* Vesa mode isn't set until we explictly call it */
69 for (int i
= 1; i
< argc
; i
++) {
70 if (!strncmp(argv
[i
], "quiet", 5)) {
72 } else if (!strncmp(argv
[i
], "modules_pcimap=", 15)) {
73 strlcpy(hardware
->modules_pcimap_path
, argv
[i
] + 15,
74 sizeof(hardware
->modules_pcimap_path
));
75 convert_isolinux_filename(hardware
->modules_pcimap_path
, hardware
);
76 } else if (!strncmp(argv
[i
], "pciids=", 7)) {
77 strlcpy(hardware
->pciids_path
, argv
[i
] + 7,
78 sizeof(hardware
->pciids_path
));
79 convert_isolinux_filename(hardware
->pciids_path
, hardware
);
80 } else if (!strncmp(argv
[i
], "modules_alias=", 14)) {
81 strlcpy(hardware
->modules_alias_path
, argv
[i
] + 14,
82 sizeof(hardware
->modules_alias_path
));
83 convert_isolinux_filename(hardware
->modules_alias_path
, hardware
);
84 } else if (!strncmp(argv
[i
], "memtest=", 8)) {
85 strlcpy(hardware
->memtest_label
, argv
[i
] + 8,
86 sizeof(hardware
->memtest_label
));
87 convert_isolinux_filename(hardware
->memtest_label
, hardware
);
88 } else if (!strncmp(argv
[i
], "vesa", 4)) {
90 max_console_lines
= MAX_CLI_LINES
;
91 /* If the user defines a background image */
92 if (!strncmp(argv
[i
], "vesa=", 5)) {
93 strlcpy(hardware
->vesa_background
, argv
[i
] + 5,
94 sizeof(hardware
->vesa_background
));
96 } else if (!strncmp(argv
[i
], "novesa", 6)) {
98 max_console_lines
= MAX_VESA_CLI_LINES
;
99 } else if (!strncmp(argv
[i
], "auto=", 5)) {
100 /* The auto= parameter is separated in several argv[]
101 * as it can contains spaces.
102 * We use the AUTO_DELIMITER char to define the limits
104 * i.e auto='show dmi; show pci'
107 /* Extracting the first parameter */
108 strcpy(hardware
->auto_label
, argv
[i
] + 6);
109 strcat(hardware
->auto_label
, " ");
113 /* While we can't find the other AUTO_DELIMITER, let's process the argv[] */
114 while (((pos
= strstr(argv
[i
], AUTO_DELIMITER
)) == NULL
)
116 strcat(hardware
->auto_label
, argv
[i
]);
117 strcat(hardware
->auto_label
, " ");
121 /* If we didn't reach the end of the line, let's grab the last item */
123 strcat(hardware
->auto_label
, argv
[i
]);
124 hardware
->auto_label
[strlen(hardware
->auto_label
) - 1] = 0;
130 void detect_syslinux(struct s_hardware
*hardware
)
132 hardware
->sv
= syslinux_version();
133 switch (hardware
->sv
->filesystem
) {
134 case SYSLINUX_FS_SYSLINUX
:
135 strlcpy(hardware
->syslinux_fs
, "SYSlinux", 9);
137 case SYSLINUX_FS_PXELINUX
:
138 strlcpy(hardware
->syslinux_fs
, "PXElinux", 9);
140 case SYSLINUX_FS_ISOLINUX
:
141 strlcpy(hardware
->syslinux_fs
, "ISOlinux", 9);
143 case SYSLINUX_FS_EXTLINUX
:
144 strlcpy(hardware
->syslinux_fs
, "EXTlinux", 9);
146 case SYSLINUX_FS_UNKNOWN
:
148 strlcpy(hardware
->syslinux_fs
, "Unknown Bootloader",
149 sizeof hardware
->syslinux_fs
);
154 void init_hardware(struct s_hardware
*hardware
)
156 hardware
->pci_ids_return_code
= 0;
157 hardware
->modules_pcimap_return_code
= 0;
158 hardware
->modules_alias_return_code
= 0;
159 hardware
->cpu_detection
= false;
160 hardware
->pci_detection
= false;
161 hardware
->disk_detection
= false;
162 hardware
->disks_count
= 0;
163 hardware
->dmi_detection
= false;
164 hardware
->pxe_detection
= false;
165 hardware
->vesa_detection
= false;
166 hardware
->vpd_detection
= false;
167 hardware
->memory_detection
= false;
168 hardware
->nb_pci_devices
= 0;
169 hardware
->is_dmi_valid
= false;
170 hardware
->is_pxe_valid
= false;
171 hardware
->is_vpd_valid
= false;
172 hardware
->pci_domain
= NULL
;
173 hardware
->detected_memory_size
= 0;
175 /* Cleaning structures */
176 memset(hardware
->disk_info
, 0, sizeof(hardware
->disk_info
));
177 memset(hardware
->mbr_ids
, 0, sizeof(hardware
->mbr_ids
));
178 memset(&hardware
->dmi
, 0, sizeof(s_dmi
));
179 memset(&hardware
->cpu
, 0, sizeof(s_cpu
));
180 memset(&hardware
->pxe
, 0, sizeof(struct s_pxe
));
181 memset(&hardware
->vesa
, 0, sizeof(struct s_vesa
));
182 memset(&hardware
->vpd
, 0, sizeof(s_vpd
));
183 memset(hardware
->syslinux_fs
, 0, sizeof hardware
->syslinux_fs
);
184 memset(hardware
->pciids_path
, 0, sizeof hardware
->pciids_path
);
185 memset(hardware
->modules_pcimap_path
, 0,
186 sizeof hardware
->modules_pcimap_path
);
187 memset(hardware
->modules_alias_path
, 0,
188 sizeof hardware
->modules_alias_path
);
189 memset(hardware
->memtest_label
, 0, sizeof hardware
->memtest_label
);
190 memset(hardware
->auto_label
, 0, sizeof hardware
->auto_label
);
191 memset(hardware
->vesa_background
, 0, sizeof hardware
->vesa_background
);
192 strcat(hardware
->pciids_path
, "pci.ids");
193 strcat(hardware
->modules_pcimap_path
, "modules.pcimap");
194 strcat(hardware
->modules_alias_path
, "modules.alias");
195 strcat(hardware
->memtest_label
, "memtest");
196 strlcpy(hardware
->vesa_background
, CLI_DEFAULT_BACKGROUND
,
197 sizeof(hardware
->vesa_background
));
201 * Detecting if a DMI table exist
202 * if yes, let's parse it
204 int detect_dmi(struct s_hardware
*hardware
)
206 if (hardware
->dmi_detection
== true)
208 hardware
->dmi_detection
= true;
209 if (dmi_iterate(&hardware
->dmi
) == -ENODMITABLE
) {
210 hardware
->is_dmi_valid
= false;
214 parse_dmitable(&hardware
->dmi
);
215 hardware
->is_dmi_valid
= true;
220 * vpd_detection - populate the VPD structure
222 * VPD is a structure available on IBM machines.
223 * It is documented at:
224 * http://www.pc.ibm.com/qtechinfo/MIGR-45120.html
225 * (XXX the page seems to be gone)
227 int detect_vpd(struct s_hardware
*hardware
)
229 if (hardware
->vpd_detection
)
232 hardware
->vpd_detection
= true;
234 if (vpd_decode(&hardware
->vpd
) == -ENOVPDTABLE
) {
235 hardware
->is_vpd_valid
= false;
238 hardware
->is_vpd_valid
= true;
243 /* Detection vesa stuff*/
244 int detect_vesa(struct s_hardware
*hardware
)
246 static com32sys_t rm
;
247 struct vesa_general_info
*gi
;
248 struct vesa_mode_info
*mi
;
249 uint16_t mode
, *mode_ptr
;
252 if (hardware
->vesa_detection
== true)
255 hardware
->vesa_detection
= true;
256 hardware
->is_vesa_valid
= false;
258 /* Allocate space in the bounce buffer for these structures */
259 gi
= &((struct vesa_info
*)__com32
.cs_bounce
)->gi
;
260 mi
= &((struct vesa_info
*)__com32
.cs_bounce
)->mi
;
262 gi
->signature
= VBE2_MAGIC
; /* Get VBE2 extended data */
263 rm
.eax
.w
[0] = 0x4F00; /* Get SVGA general information */
264 rm
.edi
.w
[0] = OFFS(gi
);
266 __intcall(0x10, &rm
, &rm
);
268 if (rm
.eax
.w
[0] != 0x004F) {
272 mode_ptr
= GET_PTR(gi
->video_mode_ptr
);
273 oem_ptr
= GET_PTR(gi
->oem_vendor_name_ptr
);
274 strlcpy(hardware
->vesa
.vendor
, oem_ptr
, sizeof(hardware
->vesa
.vendor
));
275 oem_ptr
= GET_PTR(gi
->oem_product_name_ptr
);
276 strlcpy(hardware
->vesa
.product
, oem_ptr
, sizeof(hardware
->vesa
.product
));
277 oem_ptr
= GET_PTR(gi
->oem_product_rev_ptr
);
278 strlcpy(hardware
->vesa
.product_revision
, oem_ptr
,
279 sizeof(hardware
->vesa
.product_revision
));
281 hardware
->vesa
.major_version
= (gi
->version
>> 8) & 0xff;
282 hardware
->vesa
.minor_version
= gi
->version
& 0xff;
283 hardware
->vesa
.total_memory
= gi
->total_memory
;
284 hardware
->vesa
.software_rev
= gi
->oem_software_rev
;
286 hardware
->vesa
.vmi_count
= 0;
288 while ((mode
= *mode_ptr
++) != 0xFFFF) {
290 rm
.eax
.w
[0] = 0x4F01; /* Get SVGA mode information */
292 rm
.edi
.w
[0] = OFFS(mi
);
294 __intcall(0x10, &rm
, &rm
);
296 /* Must be a supported mode */
297 if (rm
.eax
.w
[0] != 0x004f)
300 /* Saving detected values */
301 memcpy(&hardware
->vesa
.vmi
[hardware
->vesa
.vmi_count
].mi
, mi
,
302 sizeof(struct vesa_mode_info
));
303 hardware
->vesa
.vmi
[hardware
->vesa
.vmi_count
].mode
= mode
;
305 hardware
->vesa
.vmi_count
++;
307 hardware
->is_vesa_valid
= true;
311 /* Try to detect disks from port 0x80 to 0xff */
312 void detect_disks(struct s_hardware
*hardware
)
317 if (hardware
->disk_detection
)
320 hardware
->disk_detection
= true;
321 for (int drive
= 0x80; drive
< 0xff; drive
++) {
323 hardware
->disk_info
[i
].disk
= drive
;
324 err
= get_drive_parameters(&hardware
->disk_info
[i
]);
327 * Do not print output when drive does not exist or
328 * doesn't support int13 (cdrom, ...)
330 if (err
== -1 || !hardware
->disk_info
[i
].cbios
)
334 hardware
->mbr_ids
[i
] = get_mbr_id(&hardware
->disk_info
[i
]);
336 hardware
->disks_count
++;
340 int detect_pxe(struct s_hardware
*hardware
)
345 t_PXENV_UNDI_GET_NIC_TYPE gnt
;
347 if (hardware
->pxe_detection
== true)
349 hardware
->pxe_detection
= true;
350 hardware
->is_pxe_valid
= false;
351 memset(&gnt
, 0, sizeof(t_PXENV_UNDI_GET_NIC_TYPE
));
352 memset(&hardware
->pxe
, 0, sizeof(struct s_pxe
));
354 /* This code can only work if pxelinux is loaded */
355 if (hardware
->sv
->filesystem
!= SYSLINUX_FS_PXELINUX
) {
358 // printf("PXE: PXElinux detected\n");
359 if (!pxe_get_cached_info(PXENV_PACKET_TYPE_DHCP_ACK
, &dhcpdata
, &dhcplen
)) {
360 pxe_bootp_t
*dhcp
= &hardware
->pxe
.dhcpdata
;
361 memcpy(&hardware
->pxe
.dhcpdata
, dhcpdata
,
362 sizeof(hardware
->pxe
.dhcpdata
));
363 snprintf(hardware
->pxe
.mac_addr
, sizeof(hardware
->pxe
.mac_addr
),
364 "%02x:%02x:%02x:%02x:%02x:%02x", dhcp
->CAddr
[0],
365 dhcp
->CAddr
[1], dhcp
->CAddr
[2], dhcp
->CAddr
[3],
366 dhcp
->CAddr
[4], dhcp
->CAddr
[5]);
368 /* Saving our IP address in a easy format */
369 hardware
->pxe
.ip_addr
[0] = hardware
->pxe
.dhcpdata
.yip
& 0xff;
370 hardware
->pxe
.ip_addr
[1] = hardware
->pxe
.dhcpdata
.yip
>> 8 & 0xff;
371 hardware
->pxe
.ip_addr
[2] = hardware
->pxe
.dhcpdata
.yip
>> 16 & 0xff;
372 hardware
->pxe
.ip_addr
[3] = hardware
->pxe
.dhcpdata
.yip
>> 24 & 0xff;
374 if (!pxe_get_nic_type(&gnt
)) {
375 switch (gnt
.NicType
) {
377 hardware
->is_pxe_valid
= true;
378 hardware
->pxe
.vendor_id
= gnt
.info
.pci
.Vendor_ID
;
379 hardware
->pxe
.product_id
= gnt
.info
.pci
.Dev_ID
;
380 hardware
->pxe
.subvendor_id
= gnt
.info
.pci
.SubVendor_ID
;
381 hardware
->pxe
.subproduct_id
=
382 gnt
.info
.pci
.SubDevice_ID
,
383 hardware
->pxe
.rev
= gnt
.info
.pci
.Rev
;
384 hardware
->pxe
.pci_bus
= (gnt
.info
.pci
.BusDevFunc
>> 8) & 0xff;
385 hardware
->pxe
.pci_dev
= (gnt
.info
.pci
.BusDevFunc
>> 3) & 0x7;
386 hardware
->pxe
.pci_func
= gnt
.info
.pci
.BusDevFunc
& 0x03;
387 hardware
->pxe
.base_class
= gnt
.info
.pci
.Base_Class
;
388 hardware
->pxe
.sub_class
= gnt
.info
.pci
.Sub_Class
;
389 hardware
->pxe
.prog_intf
= gnt
.info
.pci
.Prog_Intf
;
390 hardware
->pxe
.nictype
= gnt
.NicType
;
393 hardware
->is_pxe_valid
= true;
394 hardware
->pxe
.vendor_id
= gnt
.info
.cardbus
.Vendor_ID
;
395 hardware
->pxe
.product_id
= gnt
.info
.cardbus
.Dev_ID
;
396 hardware
->pxe
.subvendor_id
= gnt
.info
.cardbus
.SubVendor_ID
;
397 hardware
->pxe
.subproduct_id
=
398 gnt
.info
.cardbus
.SubDevice_ID
,
399 hardware
->pxe
.rev
= gnt
.info
.cardbus
.Rev
;
400 hardware
->pxe
.pci_bus
=
401 (gnt
.info
.cardbus
.BusDevFunc
>> 8) & 0xff;
402 hardware
->pxe
.pci_dev
=
403 (gnt
.info
.cardbus
.BusDevFunc
>> 3) & 0x7;
404 hardware
->pxe
.pci_func
= gnt
.info
.cardbus
.BusDevFunc
& 0x03;
405 hardware
->pxe
.base_class
= gnt
.info
.cardbus
.Base_Class
;
406 hardware
->pxe
.sub_class
= gnt
.info
.cardbus
.Sub_Class
;
407 hardware
->pxe
.prog_intf
= gnt
.info
.cardbus
.Prog_Intf
;
408 hardware
->pxe
.nictype
= gnt
.NicType
;
415 /* Let's try to find the associated pci device */
416 detect_pci(hardware
);
418 /* The firt pass try to find the exact pci device */
419 hardware
->pxe
.pci_device
= NULL
;
420 hardware
->pxe
.pci_device_pos
= 0;
421 struct pci_device
*pci_device
;
423 for_each_pci_func(pci_device
, hardware
->pci_domain
) {
425 if ((__pci_bus
== hardware
->pxe
.pci_bus
) &&
426 (__pci_slot
== hardware
->pxe
.pci_dev
) &&
427 (__pci_func
== hardware
->pxe
.pci_func
) &&
428 (pci_device
->vendor
== hardware
->pxe
.vendor_id
)
429 && (pci_device
->product
== hardware
->pxe
.product_id
)) {
430 hardware
->pxe
.pci_device
= pci_device
;
431 hardware
->pxe
.pci_device_pos
= pci_number
;
436 /* If we reach that part, it means the pci device pointed by
437 * the pxe rom wasn't found in our list.
438 * Let's try to find the device only by its pci ids.
439 * The pci device we'll match is maybe not exactly the good one
440 * as we can have the same pci id several times.
441 * At least, the pci id, the vendor/product will be right.
442 * That's clearly a workaround for some weird cases.
443 * This should happend very unlikely */
444 hardware
->pxe
.pci_device
= NULL
;
445 hardware
->pxe
.pci_device_pos
= 0;
447 for_each_pci_func(pci_device
, hardware
->pci_domain
) {
449 if ((pci_device
->vendor
== hardware
->pxe
.vendor_id
)
450 && (pci_device
->product
== hardware
->pxe
.product_id
)) {
451 hardware
->pxe
.pci_device
= pci_device
;
452 hardware
->pxe
.pci_device_pos
= pci_number
;
462 void detect_memory(struct s_hardware
*hardware
) {
463 if (hardware
->memory_detection
== false) {
464 hardware
->memory_detection
= true;
465 hardware
->detected_memory_size
= detect_memsize();
469 void detect_pci(struct s_hardware
*hardware
)
471 if (hardware
->pci_detection
== true)
473 hardware
->pci_detection
= true;
475 hardware
->nb_pci_devices
= 0;
477 /* Scanning to detect pci buses and devices */
478 hardware
->pci_domain
= pci_scan();
480 if (!hardware
->pci_domain
)
483 /* Gathering addtional information */
484 gather_additional_pci_config(hardware
->pci_domain
);
486 struct pci_device
*pci_device
;
487 for_each_pci_func(pci_device
, hardware
->pci_domain
) {
488 hardware
->nb_pci_devices
++;
492 more_printf("PCI: %d devices detected\n", hardware
->nb_pci_devices
);
493 more_printf("PCI: Resolving names\n");
495 /* Assigning product & vendor name for each device */
496 hardware
->pci_ids_return_code
=
497 get_name_from_pci_ids(hardware
->pci_domain
, hardware
->pciids_path
);
500 more_printf("PCI: Resolving class names\n");
501 /* Assigning class name for each device */
502 hardware
->pci_ids_return_code
=
503 get_class_name_from_pci_ids(hardware
->pci_domain
,
504 hardware
->pciids_path
);
507 more_printf("PCI: Resolving module names\n");
508 /* Detecting which kernel module should match each device using modules.pcimap */
509 hardware
->modules_pcimap_return_code
=
510 get_module_name_from_pcimap(hardware
->pci_domain
,
511 hardware
->modules_pcimap_path
);
513 /* Detecting which kernel module should match each device using modules.alias */
514 hardware
->modules_alias_return_code
=
515 get_module_name_from_alias(hardware
->pci_domain
,
516 hardware
->modules_alias_path
);
518 /* We try to detect the pxe stuff to populate the PXE: field of pci devices */
519 detect_pxe(hardware
);
522 void cpu_detect(struct s_hardware
*hardware
)
524 if (hardware
->cpu_detection
== true)
526 detect_cpu(&hardware
->cpu
);
527 /* Old processors doesn't manage the identify commands
528 * Let's use the dmi value in that case */
529 if (strlen(remove_spaces(hardware
->cpu
.model
)) == 0)
530 strlcpy(hardware
->cpu
.model
, hardware
->dmi
.processor
.version
,
531 sizeof(hardware
->cpu
.model
));
533 /* Some CPUs like to put many spaces in the model name
534 * That makes some weird display in console/menu
535 * Let's remove that mulitple spaces */
536 strlcpy(hardware
->cpu
.model
,del_multi_spaces(hardware
->cpu
.model
),sizeof(hardware
->cpu
.model
));
537 hardware
->cpu_detection
= true;
541 * Find the last instance of a particular command line argument
542 * (which should include the final =; do not use for boolean arguments)
544 const char *find_argument(const char **argv
, const char *argument
)
546 int la
= strlen(argument
);
548 const char *ptr
= NULL
;
550 for (arg
= argv
; *arg
; arg
++) {
551 if (!memcmp(*arg
, argument
, la
))
558 void clear_screen(void)
560 move_cursor_to_next_line();
562 set_g1_special_char();
564 display_cursor(false);
565 clear_entire_screen();
570 /* remove begining spaces */
571 char *skip_spaces(char *p
)
573 while (*p
&& *p
<= ' ') {
580 /* remove trailing & begining spaces */
581 char *remove_spaces(char *p
)
585 while (*p
&& *p
<= ' ') {
590 while (*p
&& *p
<= ' ') {
597 /* remove trailing LF */
598 char *remove_trailing_lf(char *p
)
602 while (*p
&& *p
== 10) {
611 /* delete multiple spaces, one is enough */
612 char *del_multi_spaces(char *p
)
614 /* Saving the original pointer */
617 /* Let's parse the complete string
618 * As we search for a double spacing
619 * we have to be sure then string is
620 * long enough to be processed */
621 while (*p
&& *p
+ 1) {
623 /* If we have two consecutive spaces */
624 if ((*p
== ' ') && (*(p
+ 1) == ' ')) {
626 /* Let's copy to the current position
627 * the content from the second space*/
628 strlcpy(p
, p
+ 1, strlen(p
+ 1));
630 /* The string is 1 char smaller */
631 *(p
+ strlen(p
) - 1) = '\0';
633 /* Don't increment the pointer as we
634 * changed the content of the current position*/
638 /* Nothing as been found, let's see on the next char */
641 /* Returning the original pointer */
645 /* Reset the more_printf counter */
646 void reset_more_printf(void)
651 int draw_background(const char *what
)
654 return vesacon_default_background();
656 return vesacon_load_background(what
);
659 void init_console(struct s_hardware
*hardware
)
662 openconsole(&dev_rawcon_r
, &dev_vesaserial_w
);
663 draw_background(hardware
->vesa_background
);