aboutsummaryrefslogtreecommitdiffstats
path: root/com32/hdt/hdt-common.c
blob: 19ddbadf45fb847f1077a4898a94ee776f748ca9 (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
/* ----------------------------------------------------------------------- *
 *
 *   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>

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->nb_pci_devices=0;
  hardware->is_dmi_valid=false;
  hardware->pci_domain=NULL;

  /* Cleaning structures */
  memset(hardware->disk_info,0,sizeof(hardware->disk_info));
  memset(&hardware->dmi,0,sizeof(hardware->dmi));
  memset(&hardware->cpu,0,sizeof(hardware->cpu));
}

/* 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);
 }
}

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