Update syslinux to 4.05 to support mic-0.15
[external/syslinux.git] / com32 / hdt / hdt-common.c
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 #include <string.h>
31 #include <stdio.h>
32 #include <getkey.h>
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>
38 #include <memory.h>
39
40 /* ISOlinux requires a 8.3 format */
41 void convert_isolinux_filename(char *filename, struct s_hardware *hardware)
42 {
43     /* Exit if we are not running ISOLINUX */
44     if (hardware->sv->filesystem != SYSLINUX_FS_ISOLINUX)
45         return;
46     /* Searching the dot */
47     char *dot = strchr(filename, '.');
48     /* Exiting if no dot exists in that string */
49     if (dot == NULL)
50         return;
51     /* Exiting if the extension is 3 char or less */
52     if (strlen(dot) <= 4)
53         return;
54
55     /* We have an extension bigger than .blah
56      * so we have to shorten it to 3*/
57     dot[4] = '\0';
58 }
59
60 void detect_parameters(const int argc, const char *argv[],
61                        struct s_hardware *hardware)
62 {
63     /* Quiet mode - make the output more quiet */
64     quiet = true;
65
66     /* Vesa mode isn't set until we explictly call it */
67     vesamode = false;
68
69     /* Automode isn't the default*/
70     automode = false;
71
72     /* Menu mode is the default*/
73     menumode = true;
74
75     for (int i = 1; i < argc; i++) {
76         if (!strncmp(argv[i], "quiet", 5)) {
77             quiet = true;
78         } else  if (!strncmp(argv[i], "verbose", 7)) {
79             quiet = false;
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)) {
97             vesamode = true;
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));
103             }
104         } else if (!strncmp(argv[i], "novesa", 6)) {
105             vesamode = false;
106             max_console_lines = MAX_CLI_LINES;
107         } else if (!strncmp(argv[i], "nomenu", 6)) {
108             menumode = false;
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
119              * of this parameter.
120              * i.e auto='show dmi; show pci'
121              */
122
123             automode=true;
124             char *argument = (char*)argv[i]+6;
125             /* Extracting the first parameter */
126             strcpy(hardware->auto_label, argument);
127
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)) {
130                 i++;
131                 argument = (char *)argv[i];
132                 strcat(hardware->auto_label, " ");
133                 strcat(hardware->auto_label, argument);
134             } 
135
136              hardware->auto_label[strlen(hardware->auto_label) - 1] = 0;
137         }
138     }
139 }
140
141 void detect_syslinux(struct s_hardware *hardware)
142 {
143     hardware->sv = syslinux_version();
144     switch (hardware->sv->filesystem) {
145     case SYSLINUX_FS_SYSLINUX:
146         strlcpy(hardware->syslinux_fs, "SYSlinux", 9);
147         break;
148     case SYSLINUX_FS_PXELINUX:
149         strlcpy(hardware->syslinux_fs, "PXElinux", 9);
150         break;
151     case SYSLINUX_FS_ISOLINUX:
152         strlcpy(hardware->syslinux_fs, "ISOlinux", 9);
153         break;
154     case SYSLINUX_FS_EXTLINUX:
155         strlcpy(hardware->syslinux_fs, "EXTlinux", 9);
156         break;
157     case SYSLINUX_FS_UNKNOWN:
158     default:
159         strlcpy(hardware->syslinux_fs, "Unknown Bootloader",
160                 sizeof hardware->syslinux_fs);
161         break;
162     }
163 }
164
165 void init_hardware(struct s_hardware *hardware)
166 {
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 */
188
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));
216 }
217
218 /*
219  * Detecting if a DMI table exist
220  * if yes, let's parse it
221  */
222 int detect_dmi(struct s_hardware *hardware)
223 {
224     if (hardware->dmi_detection == true)
225         return -1;
226     hardware->dmi_detection = true;
227     if (dmi_iterate(&hardware->dmi) == -ENODMITABLE) {
228         hardware->is_dmi_valid = false;
229         return -ENODMITABLE;
230     }
231
232     parse_dmitable(&hardware->dmi);
233     hardware->is_dmi_valid = true;
234     return 0;
235 }
236
237 /*
238  * Detecting ACPI
239  * if yes, let's parse it
240  */
241 int detect_acpi(struct s_hardware *hardware)
242 {
243     int retval;
244     if (hardware->acpi_detection == true)
245         return -1;
246     hardware->acpi_detection = true;
247     if ((retval=parse_acpi(&hardware->acpi)) != ACPI_FOUND) {
248         hardware->is_acpi_valid = false;
249         return retval;
250     }
251
252     hardware->is_acpi_valid = true;
253     return retval;
254 }
255
256 /**
257  * vpd_detection - populate the VPD structure
258  *
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)
263  **/
264 int detect_vpd(struct s_hardware *hardware)
265 {
266     if (hardware->vpd_detection)
267         return -1;
268     else
269         hardware->vpd_detection = true;
270
271     if (vpd_decode(&hardware->vpd) == -ENOVPDTABLE) {
272         hardware->is_vpd_valid = false;
273         return -ENOVPDTABLE;
274     } else {
275         hardware->is_vpd_valid = true;
276         return 0;
277     }
278 }
279
280 /* Detection vesa stuff*/
281 int detect_vesa(struct s_hardware *hardware)
282 {
283     static com32sys_t rm;
284     struct vesa_general_info *gi;
285     struct vesa_mode_info *mi;
286     uint16_t mode, *mode_ptr;
287     char *oem_ptr;
288
289     if (hardware->vesa_detection == true)
290         return -1;
291
292     hardware->vesa_detection = true;
293     hardware->is_vesa_valid = false;
294
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;
298
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);
302     rm.es = SEG(gi);
303     __intcall(0x10, &rm, &rm);
304
305     if (rm.eax.w[0] != 0x004F) {
306         return -1;
307     };
308
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));
317
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;
322
323     hardware->vesa.vmi_count = 0;
324
325     while ((mode = *mode_ptr++) != 0xFFFF) {
326
327         rm.eax.w[0] = 0x4F01;   /* Get SVGA mode information */
328         rm.ecx.w[0] = mode;
329         rm.edi.w[0] = OFFS(mi);
330         rm.es = SEG(mi);
331         __intcall(0x10, &rm, &rm);
332
333         /* Must be a supported mode */
334         if (rm.eax.w[0] != 0x004f)
335             continue;
336
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;
341
342         hardware->vesa.vmi_count++;
343     }
344     hardware->is_vesa_valid = true;
345     return 0;
346 }
347
348 /* Try to detect disks from port 0x80 to 0xff */
349 void detect_disks(struct s_hardware *hardware)
350 {
351     int i = -1;
352     int err;
353
354     if (hardware->disk_detection)
355         return;
356
357     hardware->disk_detection = true;
358     for (int drive = 0x80; drive < 0xff; drive++) {
359         i++;
360         hardware->disk_info[i].disk = drive;
361         err = get_drive_parameters(&hardware->disk_info[i]);
362
363         /*
364          * Do not print output when drive does not exist or
365          * doesn't support int13 (cdrom, ...)
366          */
367         if (err == -1 || !hardware->disk_info[i].cbios)
368             continue;
369
370         /* Detect MBR */
371         hardware->mbr_ids[i] = get_mbr_id(&hardware->disk_info[i]);
372
373         hardware->disks_count++;
374     }
375 }
376
377 int detect_pxe(struct s_hardware *hardware)
378 {
379     void *dhcpdata;
380
381     size_t dhcplen;
382     t_PXENV_UNDI_GET_NIC_TYPE gnt;
383
384     if (hardware->pxe_detection == true)
385         return -1;
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));
390
391     /* This code can only work if pxelinux is loaded */
392     if (hardware->sv->filesystem != SYSLINUX_FS_PXELINUX) {
393         return -1;
394     }
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]);
404
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;
410
411         if (!pxe_get_nic_type(&gnt)) {
412             switch (gnt.NicType) {
413             case PCI_NIC:
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;
428                 break;
429             case CardBus_NIC:
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;
446                 break;
447             case PnP_NIC:
448             default:
449                 return -1;
450                 break;
451             }
452
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;
457             int pci_number = 0;
458             for_each_pci_func(pci_device, hardware->pci_domain) {
459                 pci_number++;
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;
467                     return 0;
468                 }
469             }
470
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;
481             pci_number = 0;
482             for_each_pci_func(pci_device, hardware->pci_domain) {
483                 pci_number++;
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;
488                     return 0;
489                 }
490             }
491
492         }
493     }
494     return 0;
495 }
496
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();
501      }
502 }
503
504 void detect_pci(struct s_hardware *hardware)
505 {
506     if (hardware->pci_detection == true)
507         return;
508     hardware->pci_detection = true;
509
510     hardware->nb_pci_devices = 0;
511
512     /* Scanning to detect pci buses and devices */
513     hardware->pci_domain = pci_scan();
514
515     if (!hardware->pci_domain)
516         return;
517
518     /* Gathering addtional information */
519     gather_additional_pci_config(hardware->pci_domain);
520
521     struct pci_device *pci_device;
522     for_each_pci_func(pci_device, hardware->pci_domain) {
523         hardware->nb_pci_devices++;
524     }
525
526     if (!quiet) {
527         more_printf("PCI: %d devices detected\n", hardware->nb_pci_devices);
528         more_printf("PCI: Resolving names\n");
529     }
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);
533
534     if (!quiet)
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);
540
541     if (!quiet)
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);
547
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);
552
553 }
554
555 void cpu_detect(struct s_hardware *hardware)
556 {
557     if (hardware->cpu_detection == true)
558         return;
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));
565
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));
570
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;
573     }
574     hardware->cpu_detection = true;
575 }
576
577 /*
578  * Find the last instance of a particular command line argument
579  * (which should include the final =; do not use for boolean arguments)
580  */
581 const char *find_argument(const char **argv, const char *argument)
582 {
583     int la = strlen(argument);
584     const char **arg;
585     const char *ptr = NULL;
586
587     for (arg = argv; *arg; arg++) {
588         if (!memcmp(*arg, argument, la))
589             ptr = *arg + la;
590     }
591
592     return ptr;
593 }
594
595 void clear_screen(void)
596 {
597     move_cursor_to_next_line();
598     disable_utf8();
599     set_g1_special_char();
600     set_us_g0_charset();
601     display_cursor(false);
602     clear_entire_screen();
603     gotoxy(0,0);
604     reset_more_printf();
605 }
606
607 /* remove begining spaces */
608 char *skip_spaces(char *p)
609 {
610     while (*p && *p <= ' ') {
611         p++;
612     }
613
614     return p;
615 }
616
617 /* remove trailing & begining spaces */
618 char *remove_spaces(char *p)
619 {
620     char *save = p;
621     p += strlen(p) - 1;
622     while (*p && *p <= ' ') {
623         *p = '\0';
624         p--;
625     }
626     p = save;
627     while (*p && *p <= ' ') {
628         p++;
629     }
630
631     return p;
632 }
633
634 /* remove trailing LF */
635 char *remove_trailing_lf(char *p)
636 {
637     char *save = p;
638     p += strlen(p) - 1;
639     while (*p && *p == 10) {
640         *p = '\0';
641         p--;
642     }
643     p = save;
644
645     return p;
646 }
647
648 /* delete multiple spaces, one is enough */
649 char *del_multi_spaces(char *p)
650 {
651     /* Saving the original pointer */
652     char *save = p;
653
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)) {
659
660         /* If we have two consecutive spaces */
661         if ((*p == ' ') && (*(p + 1) == ' ')) {
662
663             /* Let's copy to the current position
664              * the content from the second space*/
665             strlcpy(p, p + 1, strlen(p + 1));
666
667             /* Don't increment the pointer as we
668              * changed the content of the current position*/
669             continue;
670         }
671
672         /* Nothing as been found, let's see on the next char */
673         p++;
674     }
675     /* Returning the original pointer */
676     return save;
677 }
678
679 /* Reset the more_printf counter */
680 void reset_more_printf(void)
681 {
682     display_line_nb = 0;
683 }
684
685 int draw_background(const char *what)
686 {
687     if (!what)
688         return vesacon_default_background();
689     else
690         return vesacon_load_background(what);
691 }
692
693 void init_console(struct s_hardware *hardware)
694 {
695     if (vesamode) {
696         openconsole(&dev_rawcon_r, &dev_vesaserial_w);
697         draw_background(hardware->vesa_background);
698     } else
699         console_ansi_raw();
700 }
701
702 void detect_hardware(struct s_hardware *hardware)
703 {
704     if (!quiet)
705         more_printf("ACPI: Detecting\n");
706     detect_acpi(hardware);
707
708     if (!quiet)
709         more_printf("MEMORY: Detecting\n");
710     detect_memory(hardware);
711
712     if (!quiet)
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");
717     } else {
718         if (!quiet)
719             more_printf("DMI: Table found ! (version %u.%u)\n",
720                         hardware->dmi.dmitable.major_version,
721                         hardware->dmi.dmitable.minor_version);
722     }
723
724     if (!quiet)
725         more_printf("CPU: Detecting\n");
726     cpu_detect(hardware);
727
728     if (!quiet)
729         more_printf("DISKS: Detecting\n");
730     detect_disks(hardware);
731
732     if (!quiet)
733         more_printf("VPD: Detecting\n");
734     detect_vpd(hardware);
735
736     detect_pci(hardware);
737     if (!quiet)
738         more_printf("PCI: %d Devices Found\n", hardware->nb_pci_devices);
739  
740    if (!quiet)
741         more_printf("PXE: Detecting\n");
742     detect_pxe(hardware);
743
744     if (!quiet)
745         more_printf("VESA: Detecting\n");
746     detect_vesa(hardware);
747 }
748