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