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 <stdio.h>
30 #include <string.h>
31 #include <stdlib.h>
32 #include <errno.h>
33 
34 #include "hdt-cli.h"
35 #include "hdt-common.h"
36 
main_show_kernel(int argc __unused,char ** argv __unused,struct s_hardware * hardware)37 void main_show_kernel(int argc __unused, char **argv __unused,
38 		      struct s_hardware *hardware)
39 {
40     char buffer[1024] = {0};
41     struct pci_device *pci_device;
42     bool found = false;
43     char kernel_modules[LINUX_KERNEL_MODULE_SIZE *
44 			MAX_KERNEL_MODULES_PER_PCI_DEVICE];
45 
46     reset_more_printf();
47     more_printf("Kernel modules\n");
48 
49 // more_printf(" PCI device no: %d \n", p->pci_device_pos);
50 
51     if ((hardware->modules_pcimap_return_code == -ENOMODULESPCIMAP)
52 	&& (hardware->modules_alias_return_code == -ENOMODULESALIAS)) {
53 	more_printf(" modules.pcimap and modules.alias files are missing\n");
54 	return;
55     }
56 
57     /* For every detected pci device, compute its submenu */
58     for_each_pci_func(pci_device, hardware->pci_domain) {
59 	memset(kernel_modules, 0, sizeof kernel_modules);
60 
61 	for (int kmod = 0;
62 	     kmod < pci_device->dev_info->linux_kernel_module_count; kmod++) {
63 	    if (kmod > 0) {
64 		strncat(kernel_modules, " | ", 3);
65 	    }
66 	    strncat(kernel_modules,
67 		    pci_device->dev_info->linux_kernel_module[kmod],
68 		    LINUX_KERNEL_MODULE_SIZE - 1);
69 	}
70 
71 	if ((pci_device->dev_info->linux_kernel_module_count > 0)
72 	    && (!strstr(buffer, kernel_modules))) {
73 	    found = true;
74 	    if (pci_device->dev_info->linux_kernel_module_count > 1)
75 		strncat(buffer, "(", 1);
76 	    strncat(buffer, kernel_modules, sizeof(kernel_modules));
77 	    if (pci_device->dev_info->linux_kernel_module_count > 1)
78 		strncat(buffer, ")", 1);
79 	    strncat(buffer, " # ", 3);
80 	}
81 
82     }
83     if (found == true) {
84 	strncat(buffer, "\n", 1);
85 	more_printf("%s", buffer);
86     }
87 }
88 
show_kernel_modules(int argc __unused,char ** argv __unused,struct s_hardware * hardware)89 static void show_kernel_modules(int argc __unused, char **argv __unused,
90 				struct s_hardware *hardware)
91 {
92     struct pci_device *pci_device;
93     char kernel_modules[LINUX_KERNEL_MODULE_SIZE *
94 			MAX_KERNEL_MODULES_PER_PCI_DEVICE];
95     char modules[MAX_PCI_CLASSES][256] = {{0}};
96     char category_name[MAX_PCI_CLASSES][256] = {{0}};
97 
98     if (hardware->pci_ids_return_code == -ENOPCIIDS) {
99 	more_printf(" Missing pci.ids, we can't compute the list\n");
100 	return;
101     }
102 
103     if (hardware->modules_pcimap_return_code == -ENOMODULESPCIMAP) {
104 	more_printf(" Missing modules.pcimap, we can't compute the list\n");
105 	return;
106     }
107 
108     reset_more_printf();
109     for_each_pci_func(pci_device, hardware->pci_domain) {
110 	memset(kernel_modules, 0, sizeof kernel_modules);
111 
112 	for (int kmod = 0;
113 	     kmod < pci_device->dev_info->linux_kernel_module_count; kmod++) {
114 	    strncat(kernel_modules,
115 		    pci_device->dev_info->linux_kernel_module[kmod],
116 		    LINUX_KERNEL_MODULE_SIZE - 1);
117 	    strncat(kernel_modules, " ", 1);
118 	}
119 
120 	if ((pci_device->dev_info->linux_kernel_module_count > 0)
121 	    && (!strstr(modules[pci_device->class[2]], kernel_modules))) {
122 	    strncat(modules[pci_device->class[2]], kernel_modules,
123 		    sizeof(kernel_modules));
124 	    snprintf(category_name[pci_device->class[2]],
125 		     sizeof(category_name[pci_device->class[2]]),
126 		     "%s", pci_device->dev_info->category_name);
127 	}
128     }
129     /* Print the found items */
130     for (int i = 0; i < MAX_PCI_CLASSES; i++) {
131 	if (strlen(category_name[i]) > 1) {
132 	    more_printf("%s : %s\n", category_name[i], modules[i]);
133 	}
134     }
135 }
136 
137 struct cli_module_descr kernel_show_modules = {
138     .modules = NULL,
139     .default_callback = show_kernel_modules,
140 };
141 
142 struct cli_mode_descr kernel_mode = {
143     .mode = KERNEL_MODE,
144     .name = CLI_KERNEL,
145     .default_modules = NULL,
146     .show_modules = &kernel_show_modules,
147     .set_modules = NULL,
148 };
149