aboutsummaryrefslogtreecommitdiffstats
path: root/com32/hdt/hdt-common.c
blob: 68dca51032e6f540181d37383abcb9cb120fdde9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
/* ----------------------------------------------------------------------- *
 *
 *   Copyright 2009 Erwan Velu - All Rights Reserved
 *
 *   Permission is hereby granted, free of charge, to any person
 *   obtaining a copy of this software and associated documentation
 *   files (the "Software"), to deal in the Software without
 *   restriction, including without limitation the rights to use,
 *   copy, modify, merge, publish, distribute, sublicense, and/or
 *   sell copies of the Software, and to permit persons to whom
 *   the Software is furnished to do so, subject to the following
 *   conditions:
 *
 *   The above copyright notice and this permission notice shall
 *   be included in all copies or substantial portions of the Software.
 *
 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 *   OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 *   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 *   HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 *   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 *   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 *   OTHER DEALINGS IN THE SOFTWARE.
 *
 * -----------------------------------------------------------------------
*/

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <getkey.h>
#include "syslinux/config.h"
#include "../lib/sys/vesa/vesa.h"
#include "hdt-common.h"
#include "lib-ansi.h"

/* ISOlinux requires a 8.3 format */
void convert_isolinux_filename(char *filename, struct s_hardware *hardware) {
  /* Exit if we are not running ISOLINUX */
  if (hardware->sv->filesystem != SYSLINUX_FS_ISOLINUX) return;
  /* Searching the dot */
  char *dot=strchr(filename,'.');
  /* Exiting if not dot exists in that string */
  if (dot==NULL) return;
  /* Exiting if the extension is 3 char or less */
  if (strlen(dot)<=4) return;

  /* We have an extension bigger than .blah
   * so we have to shorten it to 3*/
  dot[4]='\0';
}

void detect_parameters(const int argc, const char *argv[],
                       struct s_hardware *hardware)
{
  for (int i = 1; i < argc; i++) {
    if (!strncmp(argv[i], "modules=", 8)) {
      strncpy(hardware->modules_pcimap_path, argv[i] + 8,
        sizeof(hardware->modules_pcimap_path));
      convert_isolinux_filename(hardware->modules_pcimap_path,hardware);
    } else if (!strncmp(argv[i], "pciids=", 7)) {
      strncpy(hardware->pciids_path, argv[i] + 7,
        sizeof(hardware->pciids_path));
      convert_isolinux_filename(hardware->pciids_path,hardware);
    } else if (!strncmp(argv[i], "memtest=", 8)) {
      strncpy(hardware->memtest_label, argv[i] + 8,
        sizeof(hardware->memtest_label));
      convert_isolinux_filename(hardware->memtest_label,hardware);
    }
  }
}

void detect_syslinux(struct s_hardware *hardware)
{
  hardware->sv = syslinux_version();
  switch (hardware->sv->filesystem) {
  case SYSLINUX_FS_SYSLINUX:
    strlcpy(hardware->syslinux_fs, "SYSlinux", 9);
    break;
  case SYSLINUX_FS_PXELINUX:
    strlcpy(hardware->syslinux_fs, "PXElinux", 9);
    break;
  case SYSLINUX_FS_ISOLINUX:
    strlcpy(hardware->syslinux_fs, "ISOlinux", 9);
    break;
  case SYSLINUX_FS_EXTLINUX:
    strlcpy(hardware->syslinux_fs, "EXTlinux", 9);
    break;
  case SYSLINUX_FS_UNKNOWN:
  default:
    strlcpy(hardware->syslinux_fs, "Unknown Bootloader",
      sizeof hardware->syslinux_fs);
    break;
  }
}

void init_hardware(struct s_hardware *hardware)
{
  hardware->pci_ids_return_code = 0;
  hardware->modules_pcimap_return_code = 0;
  hardware->cpu_detection = false;
  hardware->pci_detection = false;
  hardware->disk_detection = false;
  hardware->disks_count=0;
  hardware->dmi_detection = false;
  hardware->pxe_detection = false;
  hardware->vesa_detection = false;
  hardware->vpd_detection = false;
  hardware->nb_pci_devices = 0;
  hardware->is_dmi_valid = false;
  hardware->is_pxe_valid = false;
  hardware->is_vpd_valid = false;
  hardware->pci_domain = NULL;

  /* Cleaning structures */
  memset(hardware->disk_info, 0, sizeof(hardware->disk_info));
  memset(&hardware->dmi, 0, sizeof(s_dmi));
  memset(&hardware->cpu, 0, sizeof(s_cpu));
  memset(&hardware->pxe, 0, sizeof(struct s_pxe));
  memset(&hardware->vesa, 0, sizeof(struct s_vesa));
  memset(&hardware->vpd, 0, sizeof(s_vpd));
  memset(hardware->syslinux_fs, 0, sizeof hardware->syslinux_fs);
  memset(hardware->pciids_path, 0, sizeof hardware->pciids_path);
  memset(hardware->modules_pcimap_path, 0,
         sizeof hardware->modules_pcimap_path);
  memset(hardware->memtest_label, 0, sizeof hardware->memtest_label);
  strcat(hardware->pciids_path, "pci.ids");
  strcat(hardware->modules_pcimap_path, "modules.pcimap");
  strcat(hardware->memtest_label, "memtest");
}

/*
 * Detecting if a DMI table exist
 * if yes, let's parse it
 */
int detect_dmi(struct s_hardware *hardware)
{
  if (hardware->dmi_detection == true)
    return -1;
  hardware->dmi_detection = true;
  if (dmi_iterate(&hardware->dmi) == -ENODMITABLE) {
    hardware->is_dmi_valid = false;
    return -ENODMITABLE;
  }

  parse_dmitable(&hardware->dmi);
  hardware->is_dmi_valid = true;
  return 0;
}

/**
 * vpd_detection - populate the VPD structure
 *
 * VPD is a structure available on IBM machines.
 * It is documented at:
 *    http://www.pc.ibm.com/qtechinfo/MIGR-45120.html
 * (XXX the page seems to be gone)
 **/
int detect_vpd(struct s_hardware *hardware)
{
	if (hardware->vpd_detection)
		return -1;
	else
		hardware->vpd_detection = true;

	if (vpd_decode(&hardware->vpd) == -ENOVPDTABLE) {
		hardware->is_vpd_valid = false;
		return -ENOVPDTABLE;
	} else {
		hardware->is_vpd_valid = true;
		return 0;
	}
}

/* Detection vesa stuff*/
int detect_vesa(struct s_hardware *hardware) {
  static com32sys_t rm;
  struct vesa_general_info *gi;
  struct vesa_mode_info *mi;
  uint16_t mode, *mode_ptr;
  char *oem_ptr;

  if (hardware->vesa_detection == true) return -1;

  hardware->vesa_detection=true;
  hardware->is_vesa_valid=false;

  /* Allocate space in the bounce buffer for these structures */
  gi = &((struct vesa_info *)__com32.cs_bounce)->gi;
  mi = &((struct vesa_info *)__com32.cs_bounce)->mi;

  gi->signature = VBE2_MAGIC;   /* Get VBE2 extended data */
  rm.eax.w[0] = 0x4F00;         /* Get SVGA general information */
  rm.edi.w[0] = OFFS(gi);
  rm.es      = SEG(gi);
  __intcall(0x10, &rm, &rm);

  if ( rm.eax.w[0] != 0x004F ) {
    return -1;
  };

  mode_ptr = GET_PTR(gi->video_mode_ptr);
  oem_ptr = GET_PTR(gi->oem_vendor_name_ptr);
  strncpy(hardware->vesa.vendor,oem_ptr,sizeof(hardware->vesa.vendor));
  oem_ptr = GET_PTR(gi->oem_product_name_ptr);
  strncpy(hardware->vesa.product,oem_ptr,sizeof(hardware->vesa.product));
  oem_ptr = GET_PTR(gi->oem_product_rev_ptr);
  strncpy(hardware->vesa.product_revision,oem_ptr,sizeof(hardware->vesa.product_revision));

  hardware->vesa.major_version=(gi->version >> 8) & 0xff;
  hardware->vesa.minor_version=gi->version & 0xff;
  hardware->vesa.total_memory=gi->total_memory;
  hardware->vesa.software_rev=gi->oem_software_rev;

  hardware->vesa.vmi_count=0;

  while ((mode = *mode_ptr++) != 0xFFFF) {

    rm.eax.w[0] = 0x4F01;       /* Get SVGA mode information */
    rm.ecx.w[0] = mode;
    rm.edi.w[0] = OFFS(mi);
    rm.es  = SEG(mi);
    __intcall(0x10, &rm, &rm);

    /* Must be a supported mode */
    if ( rm.eax.w[0] != 0x004f )
      continue;

    /* Saving detected values*/
    memcpy(&hardware->vesa.vmi[hardware->vesa.vmi_count].mi, mi,
           sizeof(struct vesa_mode_info));
    hardware->vesa.vmi[hardware->vesa.vmi_count].mode = mode;

    hardware->vesa.vmi_count++;
  }
  hardware->is_vesa_valid = true;
 return 0;
}

/* Try to detect disks from port 0x80 to 0xff */
void detect_disks(struct s_hardware *hardware)
{
  hardware->disk_detection = true;
  for (int drive = 0x80; drive < 0xff; drive++) {
    if (get_disk_params(drive, hardware->disk_info) != 0)
      continue;
    struct diskinfo *d = &hardware->disk_info[drive];
    hardware->disks_count++;
    more_printf
        ("  DISK 0x%X: %s : %s %s: sectors=%d, s/t=%d head=%d : EDD=%s\n",
         drive, d->aid.model, d->host_bus_type, d->interface_type,
         d->sectors, d->sectors_per_track, d->heads,
         d->edd_version);
  }
}

int detect_pxe(struct s_hardware *hardware)
{
  void *dhcpdata;

  size_t dhcplen;
  t_PXENV_UNDI_GET_NIC_TYPE gnt;

  if (hardware->pxe_detection == true)
    return -1;
  hardware->pxe_detection = true;
  hardware->is_pxe_valid = false;
  memset(&gnt, 0, sizeof(t_PXENV_UNDI_GET_NIC_TYPE));
  memset(&hardware->pxe, 0, sizeof(struct s_pxe));

  /* This code can only work if pxelinux is loaded */
  if (hardware->sv->filesystem != SYSLINUX_FS_PXELINUX) {
    return -1;
  }
// printf("PXE: PXElinux detected\n");
  if (!pxe_get_cached_info
      (PXENV_PACKET_TYPE_DHCP_ACK, &dhcpdata, &dhcplen)) {
    pxe_bootp_t *dhcp = &hardware->pxe.dhcpdata;
    memcpy(&hardware->pxe.dhcpdata, dhcpdata,
           sizeof(hardware->pxe.dhcpdata));
    snprintf(hardware->pxe.mac_addr, sizeof(hardware->pxe.mac_addr),
       "%02x:%02x:%02x:%02x:%02x:%02x", dhcp->CAddr[0],
       dhcp->CAddr[1], dhcp->CAddr[2], dhcp->CAddr[3],
       dhcp->CAddr[4], dhcp->CAddr[5]);

    /* Saving our IP address in a easy format */
    hardware->pxe.ip_addr[0] = hardware->pxe.dhcpdata.yip & 0xff;
    hardware->pxe.ip_addr[1] =
        hardware->pxe.dhcpdata.yip >> 8 & 0xff;
    hardware->pxe.ip_addr[2] =
        hardware->pxe.dhcpdata.yip >> 16 & 0xff;
    hardware->pxe.ip_addr[3] =
        hardware->pxe.dhcpdata.yip >> 24 & 0xff;

    if (!pxe_get_nic_type(&gnt)) {
      switch (gnt.NicType) {
      case PCI_NIC:
        hardware->is_pxe_valid = true;
        hardware->pxe.vendor_id =
            gnt.info.pci.Vendor_ID;
        hardware->pxe.product_id = gnt.info.pci.Dev_ID;
        hardware->pxe.subvendor_id =
            gnt.info.pci.SubVendor_ID;
        hardware->pxe.subproduct_id =
            gnt.info.pci.SubDevice_ID,
            hardware->pxe.rev = gnt.info.pci.Rev;
        hardware->pxe.pci_bus =
            (gnt.info.pci.BusDevFunc >> 8) & 0xff;
        hardware->pxe.pci_dev =
            (gnt.info.pci.BusDevFunc >> 3) & 0x7;
        hardware->pxe.pci_func =
            gnt.info.pci.BusDevFunc & 0x03;
        hardware->pxe.base_class =
            gnt.info.pci.Base_Class;
        hardware->pxe.sub_class =
            gnt.info.pci.Sub_Class;
        hardware->pxe.prog_intf =
            gnt.info.pci.Prog_Intf;
        hardware->pxe.nictype = gnt.NicType;
        break;
      case CardBus_NIC:
        hardware->is_pxe_valid = true;
        hardware->pxe.vendor_id =
            gnt.info.cardbus.Vendor_ID;
        hardware->pxe.product_id =
            gnt.info.cardbus.Dev_ID;
        hardware->pxe.subvendor_id =
            gnt.info.cardbus.SubVendor_ID;
        hardware->pxe.subproduct_id =
            gnt.info.cardbus.SubDevice_ID,
            hardware->pxe.rev = gnt.info.cardbus.Rev;
        hardware->pxe.pci_bus =
            (gnt.info.cardbus.BusDevFunc >> 8) & 0xff;
        hardware->pxe.pci_dev =
            (gnt.info.cardbus.BusDevFunc >> 3) & 0x7;
        hardware->pxe.pci_func =
            gnt.info.cardbus.BusDevFunc & 0x03;
        hardware->pxe.base_class =
            gnt.info.cardbus.Base_Class;
        hardware->pxe.sub_class =
            gnt.info.cardbus.Sub_Class;
        hardware->pxe.prog_intf =
            gnt.info.cardbus.Prog_Intf;
        hardware->pxe.nictype = gnt.NicType;
        break;
      case PnP_NIC:
      default:
        return -1;
        break;
      }
      /* Let's try to find the associated pci device */
      detect_pci(hardware);

      /* The firt pass try to find the exact pci device */
      hardware->pxe.pci_device = NULL;
      hardware->pxe.pci_device_pos = 0;
      struct pci_device *pci_device;
      int pci_number = 0;
      for_each_pci_func(pci_device, hardware->pci_domain) {
        pci_number++;
        if ((__pci_bus == hardware->pxe.pci_bus) &&
            (__pci_slot == hardware->pxe.pci_dev) &&
            (__pci_func == hardware->pxe.pci_func) &&
            (pci_device->vendor ==
             hardware->pxe.vendor_id)
            && (pci_device->product ==
          hardware->pxe.product_id)) {
          hardware->pxe.pci_device = pci_device;
          hardware->pxe.pci_device_pos =
              pci_number;
	      return 0;
        }
      }

      /* If we reach that part, it means the pci device pointed by
       * the pxe rom wasn't found in our list.
       * Let's try to find the device only by its pci ids.
       * The pci device we'll match is maybe not exactly the good one
       * as we can have the same pci id several times.
       * At least, the pci id, the vendor/product will be right.
       * That's clearly a workaround for some weird cases.
       * This should happend very unlikely */
      hardware->pxe.pci_device = NULL;
      hardware->pxe.pci_device_pos = 0;
      pci_number = 0;
      for_each_pci_func(pci_device, hardware->pci_domain) {
        pci_number++;
        if ((pci_device->vendor ==
             hardware->pxe.vendor_id)
            && (pci_device->product ==
          hardware->pxe.product_id)) {
          hardware->pxe.pci_device = pci_device;
          hardware->pxe.pci_device_pos =
              pci_number;
	      return 0;
        }
      }

    }
  }
  return 0;
}

void detect_pci(struct s_hardware *hardware)
{
  if (hardware->pci_detection == true)
    return;
  hardware->pci_detection = true;

  hardware->nb_pci_devices = 0;

  /* Scanning to detect pci buses and devices */
  hardware->pci_domain = pci_scan();

  if (!hardware->pci_domain)
    return;

  /* Gathering addtional information*/
  gather_additional_pci_config(hardware->pci_domain);

  struct pci_device *pci_device;
  for_each_pci_func(pci_device, hardware->pci_domain) {
    hardware->nb_pci_devices++;
  }

  more_printf("PCI: %d devices detected\n", hardware->nb_pci_devices);
  more_printf("PCI: Resolving names\n");
  /* Assigning product & vendor name for each device */
  hardware->pci_ids_return_code =
      get_name_from_pci_ids(hardware->pci_domain, hardware->pciids_path);

  more_printf("PCI: Resolving class names\n");
  /* Assigning class name for each device */
  hardware->pci_ids_return_code =
      get_class_name_from_pci_ids(hardware->pci_domain,
          hardware->pciids_path);

  more_printf("PCI: Resolving module names\n");
  /* Detecting which kernel module should match each device */
  hardware->modules_pcimap_return_code =
      get_module_name_from_pcimap(hardware->pci_domain,
           hardware->modules_pcimap_path);

  /* We try to detect the pxe stuff to populate the PXE: field of pci devices */
  detect_pxe(hardware);
}

void cpu_detect(struct s_hardware *hardware)
{
  if (hardware->cpu_detection == true)
    return;
  detect_cpu(&hardware->cpu);
  hardware->cpu_detection = true;
}

/*
 * Find the last instance of a particular command line argument
 * (which should include the final =; do not use for boolean arguments)
 */
const char *find_argument(const char **argv, const char *argument)
{
  int la = strlen(argument);
  const char **arg;
  const char *ptr = NULL;

  for (arg = argv; *arg; arg++) {
    if (!memcmp(*arg, argument, la))
      ptr = *arg + la;
  }

  return ptr;
}

void clear_screen(void)
{
  move_cursor_to_next_line();
  disable_utf8();
  set_g1_special_char();
  set_us_g0_charset();
  display_cursor(false);
  clear_entire_screen();
  reset_more_printf();
}

/* remove begining spaces */
char *skip_spaces(char *p)
{
  while (*p && *p <= ' ') {
    p++;
  }

  return p;
}

/* remove trailing & begining spaces */
char *remove_spaces(char *p)
{
  char *save=p;
  p+=strlen(p)-1;
  while (*p && *p <= ' ') {
   *p='\0';
   p--;
  }
  p=save;
  while (*p && *p <= ' ') {
    p++;
  }

  return p;
}

/* Reset the more_printf counter */
void reset_more_printf() {
  display_line_nb=0;
}