Adding debian version 3.70~pre8+dfsg-1.
[syslinux-debian/hramrach.git] / gpxe / contrib / 3c90xutil / bromutil.c
bloba736e5af07ab1403a7b464d292914d2c30b46a11
1 /*
2 * readutil.c - perform various control ops on the 3c509b bios rom
4 */
6 #ifndef __i386__
7 # error "This program can't compile or run on non-intel computers"
8 #else
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <unistd.h>
14 #ifdef __FreeBSD__
16 #include <fcntl.h>
17 #include <machine/cpufunc.h>
19 #define OUTB(data, port) outb(port, data)
20 #define OUTW(data, port) outw(port, data)
21 #define OUTL(data, port) outl(port, data)
23 #else
25 #include <sys/io.h>
27 #define OUTB(data, port) outb(data, port)
28 #define OUTW(data, port) outw(data, port)
29 #define OUTL(data, port) outl(data, port)
31 #endif
33 int main(int argc, char **argv)
35 unsigned int i, j, n;
36 unsigned int ioaddr;
37 unsigned long recvrstat;
38 unsigned char buf[128];
39 unsigned char b;
41 if (argc != 3) {
42 printf("Usage: romid ioaddr [erase|protect|unprotect|id|read >file|prog <file]\n");
43 exit(-1);
46 #ifdef __FreeBSD__
47 /* get permissions for in/out{blw} */
48 open("/dev/io",O_RDONLY,0);
49 #else
50 setuid(0); /* if we're setuid, do it really */
51 if (iopl(3)) {
52 perror("iopl()");
53 exit(1);
55 #endif
57 sscanf(argv[1],"%x",&ioaddr);
58 /* Set the register window to 3 for the 3c905b */
59 OUTW(0x803, ioaddr+0xe);
60 recvrstat = inl(ioaddr); /* save the receiver status */
61 /* set the receiver type to MII so the full bios rom address space
62 can be accessed */
63 OUTL((recvrstat & 0xf00fffff)|0x00600000, ioaddr);
65 /* Set the register window to 0 for the 3c905b */
66 OUTW(0x800, ioaddr+0xe);
68 if (strcmp(argv[2], "erase") == 0) {
69 /* do the funky chicken to erase the rom contents */
70 OUTL(0x5555, ioaddr+0x4);
71 OUTB(0xaa, ioaddr+0x8);
72 OUTL(0x2aaa, ioaddr+0x4);
73 OUTB(0x55, ioaddr+0x8);
74 OUTL(0x5555, ioaddr+0x4);
75 OUTB(0x80, ioaddr+0x8);
76 OUTL(0x5555, ioaddr+0x4);
77 OUTB(0xaa, ioaddr+0x8);
78 OUTL(0x2aaa, ioaddr+0x4);
79 OUTB(0x55, ioaddr+0x8);
80 OUTL(0x5555, ioaddr+0x4);
81 OUTB(0x10, ioaddr+0x8);
82 printf("Bios ROM at %04x has been erased\n", ioaddr);
83 } else if (strcmp(argv[2], "protect") == 0) {
84 OUTL(0x5555, ioaddr+0x4);
85 OUTB(0xaa, ioaddr+0x8);
86 OUTL(0x2aaa, ioaddr+0x4);
87 OUTB(0x55, ioaddr+0x8);
88 OUTL(0x5555, ioaddr+0x4);
89 OUTB(0xa0, ioaddr+0x8);
90 printf("Software Data Protection for Bios ROM at %04x has been enabled\n",
91 ioaddr);
92 } else if (strcmp(argv[2], "unprotect") == 0) {
93 OUTL(0x5555, ioaddr+0x4);
94 OUTB(0xaa, ioaddr+0x8);
95 OUTL(0x2aaa, ioaddr+0x4);
96 OUTB(0x55, ioaddr+0x8);
97 OUTL(0x5555, ioaddr+0x4);
98 OUTB(0x80, ioaddr+0x8);
99 OUTL(0x5555, ioaddr+0x4);
100 OUTB(0xaa, ioaddr+0x8);
101 OUTL(0x2aaa, ioaddr+0x4);
102 OUTB(0x55, ioaddr+0x8);
103 OUTL(0x5555, ioaddr+0x4);
104 OUTB(0x20, ioaddr+0x8);
105 printf("Software Data Protection for Bios ROM at %04x has been disabled\n",
106 ioaddr);
107 } else if (strcmp(argv[2], "id") == 0) {
108 OUTL(0x5555, ioaddr+0x4);
109 OUTB(0xaa, ioaddr+0x8);
110 OUTL(0x2aaa, ioaddr+0x4);
111 OUTB(0x55, ioaddr+0x8);
112 OUTL(0x5555, ioaddr+0x4);
113 OUTB(0x90, ioaddr+0x8);
114 /* 10ms delay needed */
115 printf("Manufacturer ID - ");
116 /* manuf. id */
117 OUTL(0x0000, ioaddr+0x4);
118 printf("%02x\n", inb(ioaddr+0x8));
119 /* device id */
120 OUTL(0x0001, ioaddr+0x4);
121 printf("Device ID - %02x\n", inb(ioaddr+0x8));
122 /* undo the funky chicken */
123 OUTL(0x5555, ioaddr+0x4);
124 OUTB(0xaa, ioaddr+0x8);
125 OUTL(0x2aaa, ioaddr+0x4);
126 OUTB(0x55, ioaddr+0x8);
127 OUTL(0x5555, ioaddr+0x4);
128 OUTB(0xf0, ioaddr+0x8);
129 } else if (strcmp(argv[2], "read") == 0) {
130 for (i = 0; i < 65536; i++) {
131 OUTL(i, ioaddr+0x4);
132 b = inb(ioaddr+0x8);
133 write(1, &b, 1);
135 } else if (strcmp(argv[2], "prog") == 0) {
136 /* program the rom in 128 bute chunks */
137 for (i = 0, n = 0; i < 65536; i += n) {
138 n = read(0, buf, 128);
139 if (n == 0)
140 break;
141 if (n < 0) {
142 perror("File Error");
143 exit(-3);
145 /* disable SDP temporarily for programming a sector */
146 OUTL(0x5555, ioaddr+0x4);
147 OUTB(0xaa, ioaddr+0x8);
148 OUTL(0x2aaa, ioaddr+0x4);
149 OUTB(0x55, ioaddr+0x8);
150 OUTL(0x5555, ioaddr+0x4);
151 OUTB(0xa0, ioaddr+0x8);
152 for (j = 0; j < n; j++) {
153 OUTL(i+j, ioaddr+0x4);
154 OUTB(buf[j], ioaddr+0x8);
156 /* wait for the programming of this sector to coomplete */
157 while (inb(ioaddr+0x8) != buf[j-1])
162 /* Set the register window to 3 for the 3c905b */
163 OUTW(0x803, ioaddr+0xe);
164 /* restore the receiver status */
165 OUTL(recvrstat, ioaddr);
166 return 0;
169 #endif /* __i386__ */