1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2
3 #include "superiotool.h"
4
5 #if defined(__FreeBSD__)
6 #include <fcntl.h>
7 #include <unistd.h>
8 #endif
9
10 /* Command line options. */
11 int dump = 0, verbose = 0, extra_dump = 0, alternate_dump = 0;
12
13 /* Global flag which indicates whether a chip was detected at all. */
14 int chip_found = 0;
15
set_bank(uint16_t port,uint8_t bank)16 static void set_bank(uint16_t port, uint8_t bank)
17 {
18 OUTB(0x4E, port);
19 OUTB(bank, port + 1);
20 }
21
datareg(uint16_t port,uint8_t reg)22 static uint8_t datareg(uint16_t port, uint8_t reg)
23 {
24 OUTB(reg, port);
25 return INB(port + 1);
26 }
27
regval(uint16_t port,uint8_t reg)28 uint8_t regval(uint16_t port, uint8_t reg)
29 {
30 OUTB(reg, port);
31 return INB(port + ((port == 0x3bd) ? 2 : 1)); /* 0x3bd is special. */
32 }
33
regwrite(uint16_t port,uint8_t reg,uint8_t val)34 void regwrite(uint16_t port, uint8_t reg, uint8_t val)
35 {
36 OUTB(reg, port);
37 OUTB(val, port + 1);
38 }
39
enter_conf_mode_winbond_fintek_ite_8787(uint16_t port)40 void enter_conf_mode_winbond_fintek_ite_8787(uint16_t port)
41 {
42 OUTB(0x87, port);
43 OUTB(0x87, port);
44 }
45
exit_conf_mode_winbond_fintek_ite_8787(uint16_t port)46 void exit_conf_mode_winbond_fintek_ite_8787(uint16_t port)
47 {
48 OUTB(0xaa, port); /* Fintek, Winbond */
49 regwrite(port, 0x02, 0x02); /* ITE */
50 }
51
enter_conf_mode_fintek_7777(uint16_t port)52 void enter_conf_mode_fintek_7777(uint16_t port)
53 {
54 OUTB(0x77, port);
55 OUTB(0x77, port);
56 }
57
exit_conf_mode_fintek_7777(uint16_t port)58 void exit_conf_mode_fintek_7777(uint16_t port)
59 {
60 OUTB(0xaa, port); /* Fintek */
61 }
62
superio_unknown(const struct superio_registers reg_table[],uint16_t id)63 int superio_unknown(const struct superio_registers reg_table[], uint16_t id)
64 {
65 return !strncmp(get_superio_name(reg_table, id), "<unknown>", 9);
66 }
67
68
get_superio_name(const struct superio_registers reg_table[],uint16_t id)69 const char *get_superio_name(const struct superio_registers reg_table[],
70 uint16_t id)
71 {
72 int i;
73
74 for (i = 0; /* Nothing */; i++) {
75 if (reg_table[i].superio_id == EOT)
76 break;
77
78 if ((uint16_t)reg_table[i].superio_id != id)
79 continue;
80
81 return reg_table[i].name;
82 }
83
84 return "<unknown>";
85 }
86
dump_regs(const struct superio_registers reg_table[],int i,int j,uint16_t port,uint8_t ldn_sel)87 static void dump_regs(const struct superio_registers reg_table[],
88 int i, int j, uint16_t port, uint8_t ldn_sel)
89 {
90 int k;
91 const int16_t *idx, *def;
92
93 if (reg_table[i].ldn[j].ldn != NOLDN) {
94 printf("LDN 0x%02x", reg_table[i].ldn[j].ldn);
95 if (reg_table[i].ldn[j].name != NULL)
96 printf(" (%s)", reg_table[i].ldn[j].name);
97 regwrite(port, ldn_sel, reg_table[i].ldn[j].ldn);
98 } else {
99 if (reg_table[i].ldn[j].name == NULL)
100 printf("Register dump:");
101 else
102 printf("(%s)", reg_table[i].ldn[j].name);
103 }
104
105 idx = reg_table[i].ldn[j].idx;
106 def = reg_table[i].ldn[j].def;
107
108 if (alternate_dump) {
109 int skip_def = 0;
110 int val;
111
112 printf("\nidx def val\n");
113
114 for (k = 0; idx[k] != EOT; k++) {
115 if (skip_def || def[k] == EOT) {
116 skip_def = 1;
117 printf("\n");
118 continue;
119 }
120
121 printf("0x%02x: ", idx[k]);
122 val = regval(port, idx[k]);
123
124 if (def[k] == NANA)
125 printf("(NA) 0x%02x\n", val);
126 else if (def[k] == RSVD)
127 printf("(RR) 0x%02x\n", val);
128 else if (def[k] == MISC)
129 printf("(MM) 0x%02x\n", val);
130 else {
131 if (def[k] == val)
132 printf("0x%02x 0x%02x\n", def[k], val);
133 else
134 printf("0x%02x [0x%02x]\n", def[k], val);
135 }
136 }
137 } else {
138 printf("\nidx");
139 for (k = 0; idx[k] != EOT; k++) {
140 if (k && !(k % 8))
141 putchar(' ');
142 printf(" %02x", idx[k]);
143 }
144
145 printf("\nval");
146 for (k = 0; idx[k] != EOT; k++) {
147 if (k && !(k % 8))
148 putchar(' ');
149 printf(" %02x", regval(port, idx[k]));
150 }
151
152 printf("\ndef");
153 for (k = 0; def[k] != EOT; k++) {
154 if (k && !(k % 8))
155 putchar(' ');
156 if (def[k] == NANA)
157 printf(" NA");
158 else if (def[k] == RSVD)
159 printf(" RR");
160 else if (def[k] == MISC)
161 printf(" MM");
162 else
163 printf(" %02x", def[k]);
164 }
165 }
166 printf("\n");
167 }
168
dump_superio(const char * vendor,const struct superio_registers reg_table[],uint16_t port,uint16_t id,uint8_t ldn_sel)169 void dump_superio(const char *vendor,
170 const struct superio_registers reg_table[],
171 uint16_t port, uint16_t id, uint8_t ldn_sel)
172 {
173 int i, j, no_dump_available = 1;
174
175 if (!dump)
176 return;
177
178 for (i = 0; /* Nothing */; i++) {
179 if (reg_table[i].superio_id == EOT)
180 break;
181
182 if ((uint16_t)reg_table[i].superio_id != id)
183 continue;
184
185 for (j = 0; /* Nothing */; j++) {
186 if (reg_table[i].ldn[j].ldn == EOT)
187 break;
188 no_dump_available = 0;
189 dump_regs(reg_table, i, j, port, ldn_sel);
190 }
191
192 if (no_dump_available)
193 printf("No dump available for this Super I/O\n");
194 }
195 }
196
dump_io(uint16_t iobase,uint16_t length)197 void dump_io(uint16_t iobase, uint16_t length)
198 {
199 uint16_t i;
200
201 printf("Dumping %d I/O mapped registers at base 0x%04x:\n",
202 length, iobase);
203 for (i = 0; i < length; i++)
204 printf("%02x ", i);
205 printf("\n");
206 for (i = 0; i < length; i++)
207 printf("%02x ", INB(iobase + i));
208 printf("\n");
209 }
210
dump_data(uint16_t iobase,int bank)211 void dump_data(uint16_t iobase, int bank)
212 {
213 uint16_t i;
214
215 printf("Bank %d:\n", bank);
216 printf(" ");
217 for (i = 0; i < 16; i++)
218 printf("%02x ", i);
219 set_bank(iobase, bank);
220 for (i = 0; i < 256; i++) {
221 if (i % 16 == 0)
222 printf("\n%02x: ", i / 16);
223 printf("%02x ", datareg(iobase, i));
224 }
225 printf("\n");
226 }
227
probing_for(const char * vendor,const char * info,uint16_t port)228 void probing_for(const char *vendor, const char *info, uint16_t port)
229 {
230 if (!verbose)
231 return;
232
233 /* Yes, there's no space between '%s' and 'at'! */
234 printf("Probing for %s Super I/O %sat 0x%x...\n", vendor, info, port);
235 }
236
237 /** Print a list of all supported chips from the given vendor. */
print_vendor_chips(const char * vendor,const struct superio_registers reg_table[])238 void print_vendor_chips(const char *vendor,
239 const struct superio_registers reg_table[])
240 {
241 int i;
242
243 for (i = 0; reg_table[i].superio_id != EOT; i++) {
244 printf("%s %s", vendor, reg_table[i].name);
245
246 /* Unless the ldn is empty, assume this chip has a dump. */
247 if (reg_table[i].ldn[0].ldn != EOT)
248 printf(" (dump available)");
249
250 printf("\n");
251 }
252
253 /* If we printed any chips for this vendor, put in a blank line. */
254 if (i != 0)
255 printf("\n");
256 }
257
258 /** Print a list of all chips supported by superiotool. */
print_list_of_supported_chips(void)259 void print_list_of_supported_chips(void)
260 {
261 int i;
262
263 printf("Supported Super I/O chips:\n\n");
264
265 for (i = 0; i < ARRAY_SIZE(vendor_print_functions); i++)
266 vendor_print_functions[i].print_list();
267
268 printf("See <https://coreboot.org/Superiotool#Supported_devices> "
269 "for more information.\n");
270 }
271
print_version(void)272 static void print_version(void)
273 {
274 printf("superiotool r%s\n", SUPERIOTOOL_VERSION);
275 }
276
main(int argc,char * argv[])277 int main(int argc, char *argv[])
278 {
279 int i, j, opt, option_index;
280 #if defined(__FreeBSD__)
281 int io_fd;
282 #endif
283
284 static const struct option long_options[] = {
285 {"dump", no_argument, NULL, 'd'},
286 {"extra-dump", no_argument, NULL, 'e'},
287 {"alternate-dump", no_argument, NULL, 'a'},
288 {"list-supported", no_argument, NULL, 'l'},
289 {"verbose", no_argument, NULL, 'V'},
290 {"version", no_argument, NULL, 'v'},
291 {"help", no_argument, NULL, 'h'},
292 {0, 0, 0, 0}
293 };
294
295 while ((opt = getopt_long(argc, argv, "dealVvh",
296 long_options, &option_index)) != EOF) {
297 switch (opt) {
298 case 'd':
299 dump = 1;
300 break;
301 case 'e':
302 extra_dump = 1;
303 break;
304 case 'a':
305 alternate_dump = 1;
306 break;
307 case 'l':
308 print_list_of_supported_chips();
309 exit(0);
310 break;
311 case 'V':
312 verbose = 1;
313 break;
314 case 'v':
315 print_version();
316 exit(0);
317 break;
318 case 'h':
319 printf(USAGE);
320 printf(USAGE_INFO);
321 exit(0);
322 break;
323 default:
324 /* Unknown option. */
325 exit(1);
326 break;
327 }
328 }
329
330 #if defined(__FreeBSD__)
331 if ((io_fd = open("/dev/io", O_RDWR)) < 0) {
332 perror("/dev/io");
333 #elif defined(__NetBSD__)
334 if (iopl(3) < 0) {
335 perror("iopl");
336 #else
337 if (ioperm(0, 6000, 1) < 0) {
338 perror("ioperm");
339 #endif
340 printf("Superiotool must be run as root.\n");
341 exit(1);
342 }
343
344 print_version();
345
346 #ifdef PCI_SUPPORT
347 /* Do some basic libpci init. */
348 pacc = pci_alloc();
349 pci_init(pacc);
350 pci_scan_bus(pacc);
351 #endif
352
353 for (i = 0; i < ARRAY_SIZE(superio_ports_table); i++) {
354 for (j = 0; superio_ports_table[i].ports[j] != EOT; j++)
355 superio_ports_table[i].probe_idregs(
356 superio_ports_table[i].ports[j]);
357 }
358
359 if (!chip_found)
360 printf("No Super I/O found\n");
361
362 #if defined(__FreeBSD__)
363 close(io_fd);
364 #endif
365 return 0;
366 }
367