aboutsummaryrefslogtreecommitdiffstats
path: root/com32/hdt/hdt-common.c
blob: d71f215a4734834f1beb5bf14f35d47f82ada5c5 (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
/* ----------------------------------------------------------------------- *
 *
 *   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 "hdt-common.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "syslinux/config.h"

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->dmi_detection=false;
  hardware->pxe_detection=false;
  hardware->nb_pci_devices=0;
  hardware->is_dmi_valid=false;
  hardware->is_pxe_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->gnt,0,sizeof(t_PXENV_UNDI_GET_NIC_TYPE));
}

/* Detecting if a DMI table exist
 * if yes, let's parse it */
int detect_dmi(struct s_hardware *hardware) {
  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;
}

/* Try to detects disk 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];
    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;
 const struct syslinux_version *sv;

 hardware->pxe_detection=true;
 memset(&hardware->gnt,0, sizeof(t_PXENV_UNDI_GET_NIC_TYPE));

 sv = syslinux_version();
 /* This code can only work if pxelinux is loaded*/
 if (sv->filesystem != SYSLINUX_FS_PXELINUX) {
	 printf("No valid PXE Rom found\n");
	 return -1;
 }
 printf("PXE: PXElinux detected, Detecting parameters\n");

 if (!pxe_get_cached_info(PXENV_PACKET_TYPE_DHCP_ACK, &dhcpdata, &dhcplen)) {
        if (!pxe_get_nic_type(&hardware->gnt)) {
	 hardware->is_pxe_valid=true;
        }
 }
 return 0;
}

void detect_pci(struct s_hardware *hardware) {
  hardware->pci_detection=true;
  printf("PCI: Detecting Devices\n");

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

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

  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);

  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);


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

}

void cpu_detect(struct s_hardware *hardware) {
  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) */
char *find_argument(const char **argv, const char *argument)
{
  int la = strlen(argument);
  const char **arg;
  char *ptr = NULL;

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

  return ptr;
}

void clear_screen(void)
{
  fputs("\033e\033%@\033)0\033(B\1#0\033[?25l\033[2J", stdout);
  display_line_nb=0;
}