summaryrefslogtreecommitdiffstats
path: root/contrib/3c90xutil/cromutil.c
blob: b86807801d9a4f261a4c56ce4ae304b36e0acf6a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
 * JLdL 21Jun04.
 *
 * cromutil.c
 *
 * Perform various control operations on the flash EEPROM of
 * _ the 3COM models 3C905C or 3C905CX network cards, in order
 * _ to write a boot program such as Etherboot into it.
 *
 * This program is meant for the Linux operating system only,
 * _ and only for the i386 architecture.
 *
 * The flash EEPROM usually used in these cards is the AT49BV512
 * _ chip, which has 512 Kbit (64 KByte). Another possible chip,
 * _ which is equivalent to this one, is the SST39VF512.
 *
 * Added alternative read128 and prog128 commands for cards with
 * _ the SST29EE020 fast page-write (super-)flash EEPROM, which
 * _ has 2 Mbit (256 KByte), and which has to be programmed in
 * _ a 128-byte page mode. NOTE: it seems that the card can
 * _ address only the first half of the memory in this chip,
 * _ so only 128 Kbytes are actually available for use.
 *
 * Added a few informative messages and a detailed help message.
 *
 */

#ifndef __i386__
#  error "This program can't compile or run on non-Intel computers"
#else

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/io.h>
#include <string.h>

int main(int argc, char **argv)
{
  /* Counters. */
  unsigned int i, j, n;
  /* For ROM chips larger than 64 KB, a long integer
     _ is needed for the global byte counter. */
  unsigned long k;
  /* The I/O address of the card. */
  unsigned int ioaddr;
  /* Storage for a byte. */
  unsigned char b;
  /* Storage for a page. */
  unsigned char buf[128];

  /* Initialize a few things to avoid compiler warnings. */
  i=0; j=0; n=0; k=0;

  /* Verify the command-line parameters; write
     _ out an usage message if needed. */
  if (argc != 3) {
    /* Exactly 2 command line parameters are needed. */
    printf("Usage: ./cromutil ioaddr command [(>|<) file]\n");
    printf(" (try './cromutil 0x0000 help' for details)\n");
    exit(-1);
  }

  /* Set the UID to root if possible. */
  setuid(0);

  /* Get port-access permissions for in{blw}/out{blw}. */
  if (iopl(3)) {
    perror("iopl()");
    exit(1);
  }

  /* Pass the I/O address of the card to a variable. */
  sscanf(argv[1],"%x",&ioaddr);

  /* Set the register window to 0. */
  outw(0x800, ioaddr+0xe);

  /*
   * Execute the requested command.
   *
   * "id": get and write out the ID numbers.
   */
  if (strcmp(argv[2], "id") == 0) {
    /* Software ID entry command sequence. */
    outl(0x5555, ioaddr+0x4); outb(0xaa, ioaddr+0x8);
    outl(0x2aaa, ioaddr+0x4); outb(0x55, ioaddr+0x8);
    outl(0x5555, ioaddr+0x4); outb(0x90, ioaddr+0x8);
    /* A 10 ms delay is needed. */
    usleep(10000);
    /* Get the manufacturer id. */
    outl(0x0000, ioaddr+0x4);
    printf("Manufacturer ID - %02x\n", inb(ioaddr+0x8));
    /* Get the device id. */
    outl(0x0001, ioaddr+0x4);
    printf("Device ID - %02x\n", inb(ioaddr+0x8));
    /* Software ID exit command sequence. */
    outl(0x5555, ioaddr+0x4); outb(0xaa, ioaddr+0x8);
    outl(0x2aaa, ioaddr+0x4); outb(0x55, ioaddr+0x8);
    outl(0x5555, ioaddr+0x4); outb(0xf0, ioaddr+0x8);
  }
  /*
   * "read": read data from the 512 Kbit ROM.
   */
  else if (strcmp(argv[2], "read") == 0) {
    /* Loop over the whole ROM. */
    for (k = 0; k < 65536; k++) {
      outl(k, ioaddr+0x4);
      b = inb(ioaddr+0x8);
      write(1, &b, 1);
    }
    /* Write out an informative message. */
    perror("Read 65536 bytes from ROM");
  }
  /*
   * "read128": this alternative is for the 2 Mbit ROM.
   */
  else if (strcmp(argv[2], "read128") == 0) {
    /* Loop over the accessible part of the ROM. */
    for (k = 0; k < 131072; k++) {
      outl(k, ioaddr+0x4);
      b = inb(ioaddr+0x8);
      write(1, &b, 1);
    }
    /* Write out an informative message. */
    perror("Read 131072 bytes from ROM");
  }
  /*
   * "erase": erase the ROM contents.
   */
  else if (strcmp(argv[2], "erase") == 0) {
    /* Software chip-erase command sequence. */
    outl(0x5555, ioaddr+0x4); outb(0xaa, ioaddr+0x8);
    outl(0x2aaa, ioaddr+0x4); outb(0x55, ioaddr+0x8);
    outl(0x5555, ioaddr+0x4); outb(0x80, ioaddr+0x8);
    outl(0x5555, ioaddr+0x4); outb(0xaa, ioaddr+0x8);
    outl(0x2aaa, ioaddr+0x4); outb(0x55, ioaddr+0x8);
    outl(0x5555, ioaddr+0x4); outb(0x10, ioaddr+0x8);
    /* Wait a bit. */
    sleep(1);
    /* Write out an informative message. */
    printf("Bios ROM at %04x has been erased: Success\n", ioaddr);
  }
  /*
   * "prog": program the 512 Kbit ROM.
   */
  else if (strcmp(argv[2], "prog") == 0) {
    /* Loop over the bytes in pages, to
       _ allow for a progress report. */
    for (j = 0; j < 512; j++) {
      for (i = 0; i < 128; i++) {
	/* If this program is to run on a diskless node,
	   _ must read in the byte _before_ changing the
	   _ mode of the chip, or NFS may block. */
	n = read(0, &b, 1);
	/* At EOF exit the inner loop. */
	if (n == 0)
	  break;
	if (n < 0) {
	  perror("Input File Error");
	  exit(-3);
	}
	/* Disable SDP temporarily for programming a byte. */
	outl(0x5555, ioaddr+0x4); outb(0xaa, ioaddr+0x8);
	outl(0x2aaa, ioaddr+0x4); outb(0x55, ioaddr+0x8);
	outl(0x5555, ioaddr+0x4); outb(0xA0, ioaddr+0x8);
	/* Calculate the address of the byte. */
	k=i+128*j;
	/* Program this byte. */
	outl(k, ioaddr+0x4); outb(b, ioaddr+0x8);
	/* Wait for the programming of this byte to complete. */
	while (inb(ioaddr+0x8) != b)
	  ;
      }
      /* At EOF exit the outer loop. */
      if (n == 0)
	break;
      /* Write out a progress report. */
      printf("."); fflush(NULL);
    }
    /* Write out an informative message. */
    printf("\nWrote %ld bytes to ROM: Success\n", k);
  }
  /*
   * "prog128": this alternative is for the 2 Mbit ROM.
   */
  else if (strcmp(argv[2], "prog128") == 0) {
    /* Loop over the accessible pages; the card can
       _ access only the first half of the chip. */
    for (j = 0; j < 1024; j++) {
      /* If this program is to run on a diskless node,
	 _ must read in the page _before_ changing the
	 _ mode of the chip, or NFS may block. */
      n = read(0, buf, 128);
      /* At EOF exit the loop. */
      if (n == 0)
	break;
      if (n < 0) {
	perror("Input File Error");
	exit(-3);
      }
      /* Disable SDP temporarily for programming a page. */
      outl(0x5555, ioaddr+0x4); outb(0xaa, ioaddr+0x8);
      outl(0x2aaa, ioaddr+0x4); outb(0x55, ioaddr+0x8);
      outl(0x5555, ioaddr+0x4); outb(0xA0, ioaddr+0x8);
      /* Loop over the bytes in a page. */
      for (i = 0; i < n; i++) {
	/* Calculate the address of the byte. */
	k=i+128*j;
	/* Program this byte. */
	outl(k, ioaddr+0x4); outb(buf[i], ioaddr+0x8);
      }
      /* Wait for the programming of this page to complete. */
      while (inb(ioaddr+0x8) != buf[i-1])
	;
      /* Write out a progress report. */
      printf("."); fflush(NULL);
    }
    /* Write out an informative message. */
    printf("\nWrote %d pages to ROM: Success\n", j);
  }
  /*
   * "help": write out a detailed help message.
   */
  else if (strcmp(argv[2], "help") == 0) {
    printf("This utility can be used to write data, usually boot loaders\n");
    printf("  such as Etherboot, to the flash EEPROM of the 3COM models\n");
    printf("  3C905C and 3C905CX network cards. You use it like this:\n");
    printf("        ./cromutil ioaddr command [(>|<) file]\n");
    printf("Here ioaddr is the hexadecimal I/O address of the card, such\n");
    printf("  as 0xA123, in some cases you need input/output redirection\n");
    printf("  from/to a file, and the command can be one of these:\n");
    printf("  id               get the ID numbers of the card;\n");
    printf("  read > file      read the contents of the ROM into a file;\n");
    printf("  read128 > file   read the contents of the ROM into a file;\n");
    printf("  erase            erase the whole ROM to the 1 state;\n");
    printf("  prog < file      write the contents of a file into the ROM;\n");
    printf("  prog128 < file   write the contents of a file into the ROM.\n");
    printf("You can get the I/O address of the card using the commands\n");
    printf("  'lspci -v', 'cat /proc/pci', or 'dmesg | grep -i 3C905C'.\n");
    printf("The read and prog commands are to be used if the card has a\n");
    printf("  traditional 512 Kb (64 KB) flash EEPROM chip, such as:\n");
    printf("  | AT49BV512 | SST39VF512 |\n");
    printf("The read128 and prog128 versions are for cards with a 2 Mb\n");
    printf("  (128 KB usable) page-write flash EEPROM chip, such as:\n");
    printf("  | SST29EE020 |\n");
  }
  /*
   * Write out the usage message if an unknown command is used.
   */
  else {
    printf("Usage: ./cromutil ioaddr command [(>|<) file]\n");
    printf("(try './cromutil 0x0000 help' for details)\n");
    exit(-1);
  }
  return 0;
}

#endif /* __i386__ */