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 /* Automode isn't the default*/
72 /* Menu mode is the default*/
75 for (int i = 1; i < argc; i++) {
76 if (!strncmp(argv[i], "quiet", 5)) {
78 } else if (!strncmp(argv[i], "verbose", 7)) {
80 } else if (!strncmp(argv[i], "modules_pcimap=", 15)) {
81 strlcpy(hardware->modules_pcimap_path, argv[i] + 15,
82 sizeof(hardware->modules_pcimap_path));
83 convert_isolinux_filename(hardware->modules_pcimap_path, hardware);
84 } else if (!strncmp(argv[i], "pciids=", 7)) {
85 strlcpy(hardware->pciids_path, argv[i] + 7,
86 sizeof(hardware->pciids_path));
87 convert_isolinux_filename(hardware->pciids_path, hardware);
88 } else if (!strncmp(argv[i], "modules_alias=", 14)) {
89 strlcpy(hardware->modules_alias_path, argv[i] + 14,
90 sizeof(hardware->modules_alias_path));
91 convert_isolinux_filename(hardware->modules_alias_path, hardware);
92 } else if (!strncmp(argv[i], "memtest=", 8)) {
93 strlcpy(hardware->memtest_label, argv[i] + 8,
94 sizeof(hardware->memtest_label));
95 convert_isolinux_filename(hardware->memtest_label, hardware);
96 } else if (!strncmp(argv[i], "vesa", 4)) {
98 max_console_lines = MAX_VESA_CLI_LINES;
99 /* If the user defines a background image */
100 if (!strncmp(argv[i], "vesa=", 5)) {
101 strlcpy(hardware->vesa_background, argv[i] + 5,
102 sizeof(hardware->vesa_background));
104 } else if (!strncmp(argv[i], "novesa", 6)) {
106 max_console_lines = MAX_CLI_LINES;
107 } else if (!strncmp(argv[i], "nomenu", 6)) {
109 } else if (!strncmp(argv[i], "dump_path=", 10)) {
110 strlcpy(hardware->dump_path, argv[i] + 10,
111 sizeof(hardware->dump_path));
112 } else if (!strncmp(argv[i], "tftp_ip=", 8)) {
113 strlcpy(hardware->tftp_ip, argv[i] + 8,
114 sizeof(hardware->tftp_ip));
115 } else if (!strncmp(argv[i], "auto=", 5)) {
116 /* The auto= parameter is separated in several argv[]
117 * as it can contains spaces.
118 * We use the AUTO_DELIMITER char to define the limits
120 * i.e auto='show dmi; show pci'
124 char *argument = (char*)argv[i]+6;
125 /* Extracting the first parameter */
126 strcpy(hardware->auto_label, argument);
128 /* While we can't find the other AUTO_DELIMITER, let's process the argv[] */
129 while ((strchr(argument, AUTO_DELIMITER) == NULL) && (i+1<argc)) {
131 argument = (char *)argv[i];
132 strcat(hardware->auto_label, " ");
133 strcat(hardware->auto_label, argument);
136 hardware->auto_label[strlen(hardware->auto_label) - 1] = 0;
141 void detect_syslinux(struct s_hardware *hardware)
143 hardware->sv = syslinux_version();
144 switch (hardware->sv->filesystem) {
145 case SYSLINUX_FS_SYSLINUX:
146 strlcpy(hardware->syslinux_fs, "SYSlinux", 9);
148 case SYSLINUX_FS_PXELINUX:
149 strlcpy(hardware->syslinux_fs, "PXElinux", 9);
151 case SYSLINUX_FS_ISOLINUX:
152 strlcpy(hardware->syslinux_fs, "ISOlinux", 9);
154 case SYSLINUX_FS_EXTLINUX:
155 strlcpy(hardware->syslinux_fs, "EXTlinux", 9);
157 case SYSLINUX_FS_UNKNOWN:
159 strlcpy(hardware->syslinux_fs, "Unknown Bootloader",
160 sizeof hardware->syslinux_fs);
165 void init_hardware(struct s_hardware *hardware)
167 hardware->pci_ids_return_code = 0;
168 hardware->modules_pcimap_return_code = 0;
169 hardware->modules_alias_return_code = 0;
170 hardware->cpu_detection = false;
171 hardware->pci_detection = false;
172 hardware->disk_detection = false;
173 hardware->disks_count = 0;
174 hardware->dmi_detection = false;
175 hardware->pxe_detection = false;
176 hardware->vesa_detection = false;
177 hardware->vpd_detection = false;
178 hardware->memory_detection = false;
179 hardware->acpi_detection = false;
180 hardware->nb_pci_devices = 0;
181 hardware->is_dmi_valid = false;
182 hardware->is_pxe_valid = false;
183 hardware->is_vpd_valid = false;
184 hardware->is_acpi_valid = false;
185 hardware->pci_domain = NULL;
186 hardware->detected_memory_size = 0;
187 hardware->physical_cpu_count =1; /* we have at least one cpu */
189 /* Cleaning structures */
190 memset(hardware->disk_info, 0, sizeof(hardware->disk_info));
191 memset(hardware->mbr_ids, 0, sizeof(hardware->mbr_ids));
192 memset(&hardware->dmi, 0, sizeof(s_dmi));
193 memset(&hardware->cpu, 0, sizeof(s_cpu));
194 memset(&hardware->pxe, 0, sizeof(struct s_pxe));
195 memset(&hardware->vesa, 0, sizeof(struct s_vesa));
196 memset(&hardware->vpd, 0, sizeof(s_vpd));
197 memset(&hardware->acpi, 0, sizeof(s_acpi));
198 memset(hardware->syslinux_fs, 0, sizeof hardware->syslinux_fs);
199 memset(hardware->pciids_path, 0, sizeof hardware->pciids_path);
200 memset(hardware->modules_pcimap_path, 0,
201 sizeof hardware->modules_pcimap_path);
202 memset(hardware->modules_alias_path, 0,
203 sizeof hardware->modules_alias_path);
204 memset(hardware->memtest_label, 0, sizeof hardware->memtest_label);
205 memset(hardware->auto_label, 0, sizeof hardware->auto_label);
206 memset(hardware->dump_path, 0, sizeof hardware->dump_path);
207 memset(hardware->vesa_background, 0, sizeof hardware->vesa_background);
208 memset(hardware->tftp_ip, 0, sizeof hardware->tftp_ip);
209 strcat(hardware->dump_path, "hdt");
210 strcat(hardware->pciids_path, "pci.ids");
211 strcat(hardware->modules_pcimap_path, "modules.pcimap");
212 strcat(hardware->modules_alias_path, "modules.alias");
213 strcat(hardware->memtest_label, "memtest");
214 strlcpy(hardware->vesa_background, CLI_DEFAULT_BACKGROUND,
215 sizeof(hardware->vesa_background));
219 * Detecting if a DMI table exist
220 * if yes, let's parse it
222 int detect_dmi(struct s_hardware *hardware)
224 if (hardware->dmi_detection == true)
226 hardware->dmi_detection = true;
227 if (dmi_iterate(&hardware->dmi) == -ENODMITABLE) {
228 hardware->is_dmi_valid = false;
232 parse_dmitable(&hardware->dmi);
233 hardware->is_dmi_valid = true;
239 * if yes, let's parse it
241 int detect_acpi(struct s_hardware *hardware)
244 if (hardware->acpi_detection == true)
246 hardware->acpi_detection = true;
247 if ((retval=parse_acpi(&hardware->acpi)) != ACPI_FOUND) {
248 hardware->is_acpi_valid = false;
252 hardware->is_acpi_valid = true;
257 * vpd_detection - populate the VPD structure
259 * VPD is a structure available on IBM machines.
260 * It is documented at:
261 * http://www.pc.ibm.com/qtechinfo/MIGR-45120.html
262 * (XXX the page seems to be gone)
264 int detect_vpd(struct s_hardware *hardware)
266 if (hardware->vpd_detection)
269 hardware->vpd_detection = true;
271 if (vpd_decode(&hardware->vpd) == -ENOVPDTABLE) {
272 hardware->is_vpd_valid = false;
275 hardware->is_vpd_valid = true;
280 /* Detection vesa stuff*/
281 int detect_vesa(struct s_hardware *hardware)
283 static com32sys_t rm;
284 struct vesa_general_info *gi;
285 struct vesa_mode_info *mi;
286 uint16_t mode, *mode_ptr;
289 if (hardware->vesa_detection == true)
292 hardware->vesa_detection = true;
293 hardware->is_vesa_valid = false;
295 /* Allocate space in the bounce buffer for these structures */
296 gi = &((struct vesa_info *)__com32.cs_bounce)->gi;
297 mi = &((struct vesa_info *)__com32.cs_bounce)->mi;
299 gi->signature = VBE2_MAGIC; /* Get VBE2 extended data */
300 rm.eax.w[0] = 0x4F00; /* Get SVGA general information */
301 rm.edi.w[0] = OFFS(gi);
303 __intcall(0x10, &rm, &rm);
305 if (rm.eax.w[0] != 0x004F) {
309 mode_ptr = GET_PTR(gi->video_mode_ptr);
310 oem_ptr = GET_PTR(gi->oem_vendor_name_ptr);
311 strlcpy(hardware->vesa.vendor, oem_ptr, sizeof(hardware->vesa.vendor));
312 oem_ptr = GET_PTR(gi->oem_product_name_ptr);
313 strlcpy(hardware->vesa.product, oem_ptr, sizeof(hardware->vesa.product));
314 oem_ptr = GET_PTR(gi->oem_product_rev_ptr);
315 strlcpy(hardware->vesa.product_revision, oem_ptr,
316 sizeof(hardware->vesa.product_revision));
318 hardware->vesa.major_version = (gi->version >> 8) & 0xff;
319 hardware->vesa.minor_version = gi->version & 0xff;
320 hardware->vesa.total_memory = gi->total_memory;
321 hardware->vesa.software_rev = gi->oem_software_rev;
323 hardware->vesa.vmi_count = 0;
325 while ((mode = *mode_ptr++) != 0xFFFF) {
327 rm.eax.w[0] = 0x4F01; /* Get SVGA mode information */
329 rm.edi.w[0] = OFFS(mi);
331 __intcall(0x10, &rm, &rm);
333 /* Must be a supported mode */
334 if (rm.eax.w[0] != 0x004f)
337 /* Saving detected values */
338 memcpy(&hardware->vesa.vmi[hardware->vesa.vmi_count].mi, mi,
339 sizeof(struct vesa_mode_info));
340 hardware->vesa.vmi[hardware->vesa.vmi_count].mode = mode;
342 hardware->vesa.vmi_count++;
344 hardware->is_vesa_valid = true;
348 /* Try to detect disks from port 0x80 to 0xff */
349 void detect_disks(struct s_hardware *hardware)
354 if (hardware->disk_detection)
357 hardware->disk_detection = true;
358 for (int drive = 0x80; drive < 0xff; drive++) {
360 hardware->disk_info[i].disk = drive;
361 err = get_drive_parameters(&hardware->disk_info[i]);
364 * Do not print output when drive does not exist or
365 * doesn't support int13 (cdrom, ...)
367 if (err == -1 || !hardware->disk_info[i].cbios)
371 hardware->mbr_ids[i] = get_mbr_id(&hardware->disk_info[i]);
373 hardware->disks_count++;
377 int detect_pxe(struct s_hardware *hardware)
382 t_PXENV_UNDI_GET_NIC_TYPE gnt;
384 if (hardware->pxe_detection == true)
386 hardware->pxe_detection = true;
387 hardware->is_pxe_valid = false;
388 memset(&gnt, 0, sizeof(t_PXENV_UNDI_GET_NIC_TYPE));
389 memset(&hardware->pxe, 0, sizeof(struct s_pxe));
391 /* This code can only work if pxelinux is loaded */
392 if (hardware->sv->filesystem != SYSLINUX_FS_PXELINUX) {
395 // printf("PXE: PXElinux detected\n");
396 if (!pxe_get_cached_info(PXENV_PACKET_TYPE_DHCP_ACK, &dhcpdata, &dhcplen)) {
397 pxe_bootp_t *dhcp = &hardware->pxe.dhcpdata;
398 memcpy(&hardware->pxe.dhcpdata, dhcpdata,
399 sizeof(hardware->pxe.dhcpdata));
400 snprintf(hardware->pxe.mac_addr, sizeof(hardware->pxe.mac_addr),
401 "%02x:%02x:%02x:%02x:%02x:%02x", dhcp->CAddr[0],
402 dhcp->CAddr[1], dhcp->CAddr[2], dhcp->CAddr[3],
403 dhcp->CAddr[4], dhcp->CAddr[5]);
405 /* Saving our IP address in a easy format */
406 hardware->pxe.ip_addr[0] = hardware->pxe.dhcpdata.yip & 0xff;
407 hardware->pxe.ip_addr[1] = hardware->pxe.dhcpdata.yip >> 8 & 0xff;
408 hardware->pxe.ip_addr[2] = hardware->pxe.dhcpdata.yip >> 16 & 0xff;
409 hardware->pxe.ip_addr[3] = hardware->pxe.dhcpdata.yip >> 24 & 0xff;
411 if (!pxe_get_nic_type(&gnt)) {
412 switch (gnt.NicType) {
414 hardware->is_pxe_valid = true;
415 hardware->pxe.vendor_id = gnt.info.pci.Vendor_ID;
416 hardware->pxe.product_id = gnt.info.pci.Dev_ID;
417 hardware->pxe.subvendor_id = gnt.info.pci.SubVendor_ID;
418 hardware->pxe.subproduct_id =
419 gnt.info.pci.SubDevice_ID,
420 hardware->pxe.rev = gnt.info.pci.Rev;
421 hardware->pxe.pci_bus = (gnt.info.pci.BusDevFunc >> 8) & 0xff;
422 hardware->pxe.pci_dev = (gnt.info.pci.BusDevFunc >> 3) & 0x7;
423 hardware->pxe.pci_func = gnt.info.pci.BusDevFunc & 0x03;
424 hardware->pxe.base_class = gnt.info.pci.Base_Class;
425 hardware->pxe.sub_class = gnt.info.pci.Sub_Class;
426 hardware->pxe.prog_intf = gnt.info.pci.Prog_Intf;
427 hardware->pxe.nictype = gnt.NicType;
430 hardware->is_pxe_valid = true;
431 hardware->pxe.vendor_id = gnt.info.cardbus.Vendor_ID;
432 hardware->pxe.product_id = gnt.info.cardbus.Dev_ID;
433 hardware->pxe.subvendor_id = gnt.info.cardbus.SubVendor_ID;
434 hardware->pxe.subproduct_id =
435 gnt.info.cardbus.SubDevice_ID,
436 hardware->pxe.rev = gnt.info.cardbus.Rev;
437 hardware->pxe.pci_bus =
438 (gnt.info.cardbus.BusDevFunc >> 8) & 0xff;
439 hardware->pxe.pci_dev =
440 (gnt.info.cardbus.BusDevFunc >> 3) & 0x7;
441 hardware->pxe.pci_func = gnt.info.cardbus.BusDevFunc & 0x03;
442 hardware->pxe.base_class = gnt.info.cardbus.Base_Class;
443 hardware->pxe.sub_class = gnt.info.cardbus.Sub_Class;
444 hardware->pxe.prog_intf = gnt.info.cardbus.Prog_Intf;
445 hardware->pxe.nictype = gnt.NicType;
453 /* The firt pass try to find the exact pci device */
454 hardware->pxe.pci_device = NULL;
455 hardware->pxe.pci_device_pos = 0;
456 struct pci_device *pci_device;
458 for_each_pci_func(pci_device, hardware->pci_domain) {
460 if ((__pci_bus == hardware->pxe.pci_bus) &&
461 (__pci_slot == hardware->pxe.pci_dev) &&
462 (__pci_func == hardware->pxe.pci_func) &&
463 (pci_device->vendor == hardware->pxe.vendor_id)
464 && (pci_device->product == hardware->pxe.product_id)) {
465 hardware->pxe.pci_device = pci_device;
466 hardware->pxe.pci_device_pos = pci_number;
471 /* If we reach that part, it means the pci device pointed by
472 * the pxe rom wasn't found in our list.
473 * Let's try to find the device only by its pci ids.
474 * The pci device we'll match is maybe not exactly the good one
475 * as we can have the same pci id several times.
476 * At least, the pci id, the vendor/product will be right.
477 * That's clearly a workaround for some weird cases.
478 * This should happend very unlikely */
479 hardware->pxe.pci_device = NULL;
480 hardware->pxe.pci_device_pos = 0;
482 for_each_pci_func(pci_device, hardware->pci_domain) {
484 if ((pci_device->vendor == hardware->pxe.vendor_id)
485 && (pci_device->product == hardware->pxe.product_id)) {
486 hardware->pxe.pci_device = pci_device;
487 hardware->pxe.pci_device_pos = pci_number;
497 void detect_memory(struct s_hardware *hardware) {
498 if (hardware->memory_detection == false) {
499 hardware->memory_detection = true;
500 hardware->detected_memory_size = detect_memsize();
504 void detect_pci(struct s_hardware *hardware)
506 if (hardware->pci_detection == true)
508 hardware->pci_detection = true;
510 hardware->nb_pci_devices = 0;
512 /* Scanning to detect pci buses and devices */
513 hardware->pci_domain = pci_scan();
515 if (!hardware->pci_domain)
518 /* Gathering addtional information */
519 gather_additional_pci_config(hardware->pci_domain);
521 struct pci_device *pci_device;
522 for_each_pci_func(pci_device, hardware->pci_domain) {
523 hardware->nb_pci_devices++;
527 more_printf("PCI: %d devices detected\n", hardware->nb_pci_devices);
528 more_printf("PCI: Resolving names\n");
530 /* Assigning product & vendor name for each device */
531 hardware->pci_ids_return_code =
532 get_name_from_pci_ids(hardware->pci_domain, hardware->pciids_path);
535 more_printf("PCI: Resolving class names\n");
536 /* Assigning class name for each device */
537 hardware->pci_ids_return_code =
538 get_class_name_from_pci_ids(hardware->pci_domain,
539 hardware->pciids_path);
542 more_printf("PCI: Resolving module names\n");
543 /* Detecting which kernel module should match each device using modules.pcimap */
544 hardware->modules_pcimap_return_code =
545 get_module_name_from_pcimap(hardware->pci_domain,
546 hardware->modules_pcimap_path);
548 /* Detecting which kernel module should match each device using modules.alias */
549 hardware->modules_alias_return_code =
550 get_module_name_from_alias(hardware->pci_domain,
551 hardware->modules_alias_path);
555 void cpu_detect(struct s_hardware *hardware)
557 if (hardware->cpu_detection == true)
559 detect_cpu(&hardware->cpu);
560 /* Old processors doesn't manage the identify commands
561 * Let's use the dmi value in that case */
562 if (strlen(remove_spaces(hardware->cpu.model)) == 0)
563 strlcpy(hardware->cpu.model, hardware->dmi.processor.version,
564 sizeof(hardware->cpu.model));
566 /* Some CPUs like to put many spaces in the model name
567 * That makes some weird display in console/menu
568 * Let's remove that mulitple spaces */
569 strlcpy(hardware->cpu.model,del_multi_spaces(hardware->cpu.model),sizeof(hardware->cpu.model));
571 if ((hardware->is_acpi_valid) && (hardware->acpi.madt.valid)) {
572 hardware->physical_cpu_count=hardware->acpi.madt.processor_local_apic_count / hardware->cpu.num_cores;
574 hardware->cpu_detection = true;
578 * Find the last instance of a particular command line argument
579 * (which should include the final =; do not use for boolean arguments)
581 const char *find_argument(const char **argv, const char *argument)
583 int la = strlen(argument);
585 const char *ptr = NULL;
587 for (arg = argv; *arg; arg++) {
588 if (!memcmp(*arg, argument, la))
595 void clear_screen(void)
597 move_cursor_to_next_line();
599 set_g1_special_char();
601 display_cursor(false);
602 clear_entire_screen();
607 /* remove begining spaces */
608 char *skip_spaces(char *p)
610 while (*p && *p <= ' ') {
617 /* remove trailing & begining spaces */
618 char *remove_spaces(char *p)
622 while (*p && *p <= ' ') {
627 while (*p && *p <= ' ') {
634 /* remove trailing LF */
635 char *remove_trailing_lf(char *p)
639 while (*p && *p == 10) {
648 /* delete multiple spaces, one is enough */
649 char *del_multi_spaces(char *p)
651 /* Saving the original pointer */
654 /* Let's parse the complete string
655 * As we search for a double spacing
656 * we have to be sure then string is
657 * long enough to be processed */
658 while (*p && *(p + 1)) {
660 /* If we have two consecutive spaces */
661 if ((*p == ' ') && (*(p + 1) == ' ')) {
663 /* Let's copy to the current position
664 * the content from the second space*/
665 strlcpy(p, p + 1, strlen(p + 1));
667 /* Don't increment the pointer as we
668 * changed the content of the current position*/
672 /* Nothing as been found, let's see on the next char */
675 /* Returning the original pointer */
679 /* Reset the more_printf counter */
680 void reset_more_printf(void)
685 int draw_background(const char *what)
688 return vesacon_default_background();
690 return vesacon_load_background(what);
693 void init_console(struct s_hardware *hardware)
696 openconsole(&dev_rawcon_r, &dev_vesaserial_w);
697 draw_background(hardware->vesa_background);
702 void detect_hardware(struct s_hardware *hardware)
705 more_printf("ACPI: Detecting\n");
706 detect_acpi(hardware);
709 more_printf("MEMORY: Detecting\n");
710 detect_memory(hardware);
713 more_printf("DMI: Detecting Table\n");
714 if (detect_dmi(hardware) == -ENODMITABLE) {
715 printf("DMI: ERROR ! Table not found ! \n");
716 printf("DMI: Many hardware components will not be detected ! \n");
719 more_printf("DMI: Table found ! (version %u.%u)\n",
720 hardware->dmi.dmitable.major_version,
721 hardware->dmi.dmitable.minor_version);
725 more_printf("CPU: Detecting\n");
726 cpu_detect(hardware);
729 more_printf("DISKS: Detecting\n");
730 detect_disks(hardware);
733 more_printf("VPD: Detecting\n");
734 detect_vpd(hardware);
736 detect_pci(hardware);
738 more_printf("PCI: %d Devices Found\n", hardware->nb_pci_devices);
741 more_printf("PXE: Detecting\n");
742 detect_pxe(hardware);
745 more_printf("VESA: Detecting\n");
746 detect_vesa(hardware);