Initial code release
[external/syslinux.git] / com32 / hdt / hdt-cli-pci.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 <errno.h>
32
33 #include "hdt-cli.h"
34 #include "hdt-common.h"
35
36 void main_show_pci(int argc __unused, char **argv __unused,
37                    struct s_hardware *hardware)
38 {
39     reset_more_printf();
40     more_printf("PCI\n");
41     more_printf(" NB Devices   : %d\n", hardware->nb_pci_devices);
42 }
43
44 static void show_pci_device(int argc, char **argv, struct s_hardware *hardware)
45 {
46     int i = 0;
47     struct pci_device *pci_device = NULL, *temp_pci_device;
48     int pcidev = -1;
49     bool nopciids = false;
50     bool nomodulespcimap = false;
51     bool nomodulesalias = false;
52     bool nomodulesfiles = false;
53     char kernel_modules[LINUX_KERNEL_MODULE_SIZE *
54                         MAX_KERNEL_MODULES_PER_PCI_DEVICE];
55     int bus = 0, slot = 0, func = 0;
56
57     reset_more_printf();
58     /* Sanitize arguments */
59     if (argc <= 0) {
60         more_printf("show device <number>\n");
61         return;
62     } else
63         pcidev = strtol(argv[0], (char **)NULL, 10);
64
65     if (errno == ERANGE) {
66         more_printf("This PCI device number is incorrect\n");
67         return;
68     }
69     if ((pcidev > hardware->nb_pci_devices) || (pcidev <= 0)) {
70         more_printf("PCI device %d doesn't exist\n", pcidev);
71         return;
72     }
73     if (hardware->pci_ids_return_code == -ENOPCIIDS) {
74         nopciids = true;
75     }
76     if (hardware->modules_pcimap_return_code == -ENOMODULESPCIMAP) {
77         nomodulespcimap = true;
78     }
79     if (hardware->modules_alias_return_code == -ENOMODULESALIAS) {
80         nomodulesalias = true;
81     }
82     nomodulesfiles = nomodulespcimap && nomodulesalias;
83     for_each_pci_func(temp_pci_device, hardware->pci_domain) {
84         i++;
85         if (i == pcidev) {
86             bus = __pci_bus;
87             slot = __pci_slot;
88             func = __pci_func;
89             pci_device = temp_pci_device;
90         }
91     }
92
93     if (pci_device == NULL) {
94         more_printf("We were enabled to find PCI device %d\n", pcidev);
95         return;
96     }
97
98     memset(kernel_modules, 0, sizeof kernel_modules);
99     for (int kmod = 0;
100          kmod < pci_device->dev_info->linux_kernel_module_count; kmod++) {
101         if (kmod > 0) {
102             strncat(kernel_modules, " | ", 3);
103         }
104         strncat(kernel_modules,
105                 pci_device->dev_info->linux_kernel_module[kmod],
106                 LINUX_KERNEL_MODULE_SIZE - 1);
107     }
108     if (pci_device->dev_info->linux_kernel_module_count == 0)
109         strlcpy(kernel_modules, "unknown", 7);
110
111     more_printf("PCI Device %d\n", pcidev);
112
113     if (nopciids == false) {
114         more_printf("Vendor Name   : %s\n", pci_device->dev_info->vendor_name);
115         more_printf("Product Name  : %s\n", pci_device->dev_info->product_name);
116         more_printf("Class Name    : %s\n", pci_device->dev_info->class_name);
117     }
118
119     if (nomodulesfiles == false) {
120         more_printf("Kernel module : %s\n", kernel_modules);
121     }
122
123     more_printf("Vendor ID     : %04x\n", pci_device->vendor);
124     more_printf("Product ID    : %04x\n", pci_device->product);
125     more_printf("SubVendor ID  : %04x\n", pci_device->sub_vendor);
126     more_printf("SubProduct ID : %04x\n", pci_device->sub_product);
127     more_printf("Class ID      : %02x.%02x.%02x\n", pci_device->class[2],
128                 pci_device->class[1], pci_device->class[0]);
129     more_printf("Revision      : %02x\n", pci_device->revision);
130     if ((pci_device->dev_info->irq > 0)
131         && (pci_device->dev_info->irq < 255))
132         more_printf("IRQ           : %0d\n", pci_device->dev_info->irq);
133     more_printf("Latency       : %0d\n", pci_device->dev_info->latency);
134     more_printf("PCI Bus       : %02d\n", bus);
135     more_printf("PCI Slot      : %02d\n", slot);
136     more_printf("PCI Func      : %02d\n", func);
137
138     if (hardware->is_pxe_valid == true) {
139         if ((hardware->pxe.pci_device != NULL)
140             && (hardware->pxe.pci_device == pci_device)) {
141             more_printf("Mac Address   : %s\n", hardware->pxe.mac_addr);
142             more_printf("PXE           : Current boot device\n");
143         }
144     }
145 }
146
147 static void show_pci_devices(int argc __unused, char **argv __unused,
148                              struct s_hardware *hardware)
149 {
150     int i = 1;
151     struct pci_device *pci_device;
152     char kernel_modules[LINUX_KERNEL_MODULE_SIZE *
153                         MAX_KERNEL_MODULES_PER_PCI_DEVICE];
154     bool nopciids = false;
155     bool nomodulespcimap = false;
156     bool nomodulesalias = false;
157     bool nomodulesfile = false;
158     char first_line[81];
159     char second_line[81];
160
161     reset_more_printf();
162     more_printf("%d PCI devices detected\n", hardware->nb_pci_devices);
163
164     if (hardware->pci_ids_return_code == -ENOPCIIDS) {
165         nopciids = true;
166     }
167     if (hardware->modules_pcimap_return_code == -ENOMODULESPCIMAP) {
168         nomodulespcimap = true;
169     }
170     if (hardware->modules_pcimap_return_code == -ENOMODULESALIAS) {
171         nomodulesalias = true;
172     }
173
174     nomodulesfile = nomodulespcimap && nomodulesalias;
175
176     /* For every detected pci device, compute its submenu */
177     for_each_pci_func(pci_device, hardware->pci_domain) {
178         memset(kernel_modules, 0, sizeof kernel_modules);
179         for (int kmod = 0;
180              kmod < pci_device->dev_info->linux_kernel_module_count; kmod++) {
181             if (kmod > 0) {
182                 strncat(kernel_modules, " | ", 3);
183             }
184             strncat(kernel_modules,
185                     pci_device->dev_info->linux_kernel_module[kmod],
186                     LINUX_KERNEL_MODULE_SIZE - 1);
187         }
188         if (pci_device->dev_info->linux_kernel_module_count == 0)
189             strlcpy(kernel_modules, "unknown", 7);
190
191         if (nopciids == false) {
192             snprintf(first_line, sizeof(first_line),
193                      "%02d: %s %s \n", i,
194                      pci_device->dev_info->vendor_name,
195                      pci_device->dev_info->product_name);
196             if (nomodulesfile == false)
197                 snprintf(second_line, sizeof(second_line),
198                          "    # %-25s # Kmod: %s\n",
199                          pci_device->dev_info->class_name, kernel_modules);
200             else
201                 snprintf(second_line, sizeof(second_line),
202                          "    # %-25s # ID:%04x:%04x[%04x:%04x]\n",
203                          pci_device->dev_info->class_name,
204                          pci_device->vendor,
205                          pci_device->product,
206                          pci_device->sub_vendor, pci_device->sub_product);
207
208             more_printf("%s", first_line);
209             more_printf("%s", second_line);
210             more_printf("\n");
211         } else if (nopciids == true) {
212             if (nomodulesfile == true) {
213                 more_printf("%02d: %04x:%04x [%04x:%04x] \n",
214                             i, pci_device->vendor,
215                             pci_device->product,
216                             pci_device->sub_vendor, pci_device->sub_product);
217             } else {
218                 more_printf
219                     ("%02d: %04x:%04x [%04x:%04x] Kmod:%s\n", i,
220                      pci_device->vendor, pci_device->product,
221                      pci_device->sub_vendor,
222                      pci_device->sub_product, kernel_modules);
223             }
224         }
225         i++;
226     }
227 }
228
229 static void show_pci_irq(int argc __unused, char **argv __unused,
230                          struct s_hardware *hardware)
231 {
232     struct pci_device *pci_device;
233     bool nopciids = false;
234
235     reset_more_printf();
236     more_printf("%d PCI devices detected\n", hardware->nb_pci_devices);
237     more_printf("IRQ : product\n");
238     more_printf("-------------\n");
239
240     if (hardware->pci_ids_return_code == -ENOPCIIDS) {
241         nopciids = true;
242     }
243
244     /* For every detected pci device, compute its submenu */
245     for_each_pci_func(pci_device, hardware->pci_domain) {
246         /* Only display valid IRQs */
247         if ((pci_device->dev_info->irq > 0)
248             && (pci_device->dev_info->irq < 255)) {
249             if (nopciids == false) {
250                 more_printf("%02d  : %s %s \n",
251                             pci_device->dev_info->irq,
252                             pci_device->dev_info->vendor_name,
253                             pci_device->dev_info->product_name);
254             } else {
255                 more_printf("%02d  : %04x:%04x [%04x:%04x] \n",
256                             pci_device->dev_info->irq,
257                             pci_device->vendor,
258                             pci_device->product,
259                             pci_device->sub_vendor, pci_device->sub_product);
260             }
261         }
262     }
263 }
264
265 struct cli_callback_descr list_pci_show_modules[] = {
266     {
267      .name = CLI_IRQ,
268      .exec = show_pci_irq,
269      },
270     {
271      .name = CLI_PCI_DEVICE,
272      .exec = show_pci_device,
273      },
274     {
275      .name = NULL,
276      .exec = NULL,
277      },
278 };
279
280 struct cli_module_descr pci_show_modules = {
281     .modules = list_pci_show_modules,
282     .default_callback = show_pci_devices,
283 };
284
285 struct cli_mode_descr pci_mode = {
286     .mode = PCI_MODE,
287     .name = CLI_PCI,
288     .default_modules = NULL,
289     .show_modules = &pci_show_modules,
290     .set_modules = NULL,
291 };