[sanboot] Prevent leaking a stack reference for "keep-san" AoE
[gpxe.git] / contrib / 3c90xutil / cromutil.c
blobb86807801d9a4f261a4c56ce4ae304b36e0acf6a
1 /*
2 * JLdL 21Jun04.
4 * cromutil.c
6 * Perform various control operations on the flash EEPROM of
7 * _ the 3COM models 3C905C or 3C905CX network cards, in order
8 * _ to write a boot program such as Etherboot into it.
10 * This program is meant for the Linux operating system only,
11 * _ and only for the i386 architecture.
13 * The flash EEPROM usually used in these cards is the AT49BV512
14 * _ chip, which has 512 Kbit (64 KByte). Another possible chip,
15 * _ which is equivalent to this one, is the SST39VF512.
17 * Added alternative read128 and prog128 commands for cards with
18 * _ the SST29EE020 fast page-write (super-)flash EEPROM, which
19 * _ has 2 Mbit (256 KByte), and which has to be programmed in
20 * _ a 128-byte page mode. NOTE: it seems that the card can
21 * _ address only the first half of the memory in this chip,
22 * _ so only 128 Kbytes are actually available for use.
24 * Added a few informative messages and a detailed help message.
28 #ifndef __i386__
29 # error "This program can't compile or run on non-Intel computers"
30 #else
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <unistd.h>
35 #include <sys/io.h>
36 #include <string.h>
38 int main(int argc, char **argv)
40 /* Counters. */
41 unsigned int i, j, n;
42 /* For ROM chips larger than 64 KB, a long integer
43 _ is needed for the global byte counter. */
44 unsigned long k;
45 /* The I/O address of the card. */
46 unsigned int ioaddr;
47 /* Storage for a byte. */
48 unsigned char b;
49 /* Storage for a page. */
50 unsigned char buf[128];
52 /* Initialize a few things to avoid compiler warnings. */
53 i=0; j=0; n=0; k=0;
55 /* Verify the command-line parameters; write
56 _ out an usage message if needed. */
57 if (argc != 3) {
58 /* Exactly 2 command line parameters are needed. */
59 printf("Usage: ./cromutil ioaddr command [(>|<) file]\n");
60 printf(" (try './cromutil 0x0000 help' for details)\n");
61 exit(-1);
64 /* Set the UID to root if possible. */
65 setuid(0);
67 /* Get port-access permissions for in{blw}/out{blw}. */
68 if (iopl(3)) {
69 perror("iopl()");
70 exit(1);
73 /* Pass the I/O address of the card to a variable. */
74 sscanf(argv[1],"%x",&ioaddr);
76 /* Set the register window to 0. */
77 outw(0x800, ioaddr+0xe);
80 * Execute the requested command.
82 * "id": get and write out the ID numbers.
84 if (strcmp(argv[2], "id") == 0) {
85 /* Software ID entry command sequence. */
86 outl(0x5555, ioaddr+0x4); outb(0xaa, ioaddr+0x8);
87 outl(0x2aaa, ioaddr+0x4); outb(0x55, ioaddr+0x8);
88 outl(0x5555, ioaddr+0x4); outb(0x90, ioaddr+0x8);
89 /* A 10 ms delay is needed. */
90 usleep(10000);
91 /* Get the manufacturer id. */
92 outl(0x0000, ioaddr+0x4);
93 printf("Manufacturer ID - %02x\n", inb(ioaddr+0x8));
94 /* Get the device id. */
95 outl(0x0001, ioaddr+0x4);
96 printf("Device ID - %02x\n", inb(ioaddr+0x8));
97 /* Software ID exit command sequence. */
98 outl(0x5555, ioaddr+0x4); outb(0xaa, ioaddr+0x8);
99 outl(0x2aaa, ioaddr+0x4); outb(0x55, ioaddr+0x8);
100 outl(0x5555, ioaddr+0x4); outb(0xf0, ioaddr+0x8);
103 * "read": read data from the 512 Kbit ROM.
105 else if (strcmp(argv[2], "read") == 0) {
106 /* Loop over the whole ROM. */
107 for (k = 0; k < 65536; k++) {
108 outl(k, ioaddr+0x4);
109 b = inb(ioaddr+0x8);
110 write(1, &b, 1);
112 /* Write out an informative message. */
113 perror("Read 65536 bytes from ROM");
116 * "read128": this alternative is for the 2 Mbit ROM.
118 else if (strcmp(argv[2], "read128") == 0) {
119 /* Loop over the accessible part of the ROM. */
120 for (k = 0; k < 131072; k++) {
121 outl(k, ioaddr+0x4);
122 b = inb(ioaddr+0x8);
123 write(1, &b, 1);
125 /* Write out an informative message. */
126 perror("Read 131072 bytes from ROM");
129 * "erase": erase the ROM contents.
131 else if (strcmp(argv[2], "erase") == 0) {
132 /* Software chip-erase command sequence. */
133 outl(0x5555, ioaddr+0x4); outb(0xaa, ioaddr+0x8);
134 outl(0x2aaa, ioaddr+0x4); outb(0x55, ioaddr+0x8);
135 outl(0x5555, ioaddr+0x4); outb(0x80, ioaddr+0x8);
136 outl(0x5555, ioaddr+0x4); outb(0xaa, ioaddr+0x8);
137 outl(0x2aaa, ioaddr+0x4); outb(0x55, ioaddr+0x8);
138 outl(0x5555, ioaddr+0x4); outb(0x10, ioaddr+0x8);
139 /* Wait a bit. */
140 sleep(1);
141 /* Write out an informative message. */
142 printf("Bios ROM at %04x has been erased: Success\n", ioaddr);
145 * "prog": program the 512 Kbit ROM.
147 else if (strcmp(argv[2], "prog") == 0) {
148 /* Loop over the bytes in pages, to
149 _ allow for a progress report. */
150 for (j = 0; j < 512; j++) {
151 for (i = 0; i < 128; i++) {
152 /* If this program is to run on a diskless node,
153 _ must read in the byte _before_ changing the
154 _ mode of the chip, or NFS may block. */
155 n = read(0, &b, 1);
156 /* At EOF exit the inner loop. */
157 if (n == 0)
158 break;
159 if (n < 0) {
160 perror("Input File Error");
161 exit(-3);
163 /* Disable SDP temporarily for programming a byte. */
164 outl(0x5555, ioaddr+0x4); outb(0xaa, ioaddr+0x8);
165 outl(0x2aaa, ioaddr+0x4); outb(0x55, ioaddr+0x8);
166 outl(0x5555, ioaddr+0x4); outb(0xA0, ioaddr+0x8);
167 /* Calculate the address of the byte. */
168 k=i+128*j;
169 /* Program this byte. */
170 outl(k, ioaddr+0x4); outb(b, ioaddr+0x8);
171 /* Wait for the programming of this byte to complete. */
172 while (inb(ioaddr+0x8) != b)
175 /* At EOF exit the outer loop. */
176 if (n == 0)
177 break;
178 /* Write out a progress report. */
179 printf("."); fflush(NULL);
181 /* Write out an informative message. */
182 printf("\nWrote %ld bytes to ROM: Success\n", k);
185 * "prog128": this alternative is for the 2 Mbit ROM.
187 else if (strcmp(argv[2], "prog128") == 0) {
188 /* Loop over the accessible pages; the card can
189 _ access only the first half of the chip. */
190 for (j = 0; j < 1024; j++) {
191 /* If this program is to run on a diskless node,
192 _ must read in the page _before_ changing the
193 _ mode of the chip, or NFS may block. */
194 n = read(0, buf, 128);
195 /* At EOF exit the loop. */
196 if (n == 0)
197 break;
198 if (n < 0) {
199 perror("Input File Error");
200 exit(-3);
202 /* Disable SDP temporarily for programming a page. */
203 outl(0x5555, ioaddr+0x4); outb(0xaa, ioaddr+0x8);
204 outl(0x2aaa, ioaddr+0x4); outb(0x55, ioaddr+0x8);
205 outl(0x5555, ioaddr+0x4); outb(0xA0, ioaddr+0x8);
206 /* Loop over the bytes in a page. */
207 for (i = 0; i < n; i++) {
208 /* Calculate the address of the byte. */
209 k=i+128*j;
210 /* Program this byte. */
211 outl(k, ioaddr+0x4); outb(buf[i], ioaddr+0x8);
213 /* Wait for the programming of this page to complete. */
214 while (inb(ioaddr+0x8) != buf[i-1])
216 /* Write out a progress report. */
217 printf("."); fflush(NULL);
219 /* Write out an informative message. */
220 printf("\nWrote %d pages to ROM: Success\n", j);
223 * "help": write out a detailed help message.
225 else if (strcmp(argv[2], "help") == 0) {
226 printf("This utility can be used to write data, usually boot loaders\n");
227 printf(" such as Etherboot, to the flash EEPROM of the 3COM models\n");
228 printf(" 3C905C and 3C905CX network cards. You use it like this:\n");
229 printf(" ./cromutil ioaddr command [(>|<) file]\n");
230 printf("Here ioaddr is the hexadecimal I/O address of the card, such\n");
231 printf(" as 0xA123, in some cases you need input/output redirection\n");
232 printf(" from/to a file, and the command can be one of these:\n");
233 printf(" id get the ID numbers of the card;\n");
234 printf(" read > file read the contents of the ROM into a file;\n");
235 printf(" read128 > file read the contents of the ROM into a file;\n");
236 printf(" erase erase the whole ROM to the 1 state;\n");
237 printf(" prog < file write the contents of a file into the ROM;\n");
238 printf(" prog128 < file write the contents of a file into the ROM.\n");
239 printf("You can get the I/O address of the card using the commands\n");
240 printf(" 'lspci -v', 'cat /proc/pci', or 'dmesg | grep -i 3C905C'.\n");
241 printf("The read and prog commands are to be used if the card has a\n");
242 printf(" traditional 512 Kb (64 KB) flash EEPROM chip, such as:\n");
243 printf(" | AT49BV512 | SST39VF512 |\n");
244 printf("The read128 and prog128 versions are for cards with a 2 Mb\n");
245 printf(" (128 KB usable) page-write flash EEPROM chip, such as:\n");
246 printf(" | SST29EE020 |\n");
249 * Write out the usage message if an unknown command is used.
251 else {
252 printf("Usage: ./cromutil ioaddr command [(>|<) file]\n");
253 printf("(try './cromutil 0x0000 help' for details)\n");
254 exit(-1);
256 return 0;
259 #endif /* __i386__ */