2 DP83820 flash utility written by Dave Ashley for NXTV, Inc.
3 Copyright (C) 2004 by NXTV, Inc.
4 Written 20040219 by Dave Ashley.
6 Currently only supports the AT29C512
8 This code is released under the terms of the GPL. No warranty.
12 This code uses the /proc/dp83820 file which is created by the
13 dp83820flash.o module. That file allows single byte reads + writes
23 #include <sys/types.h>
30 // SMC9462TX card has D5 + D6 on the bootrom socket reversed
33 return (val&~0x60) | ((val&0x20)<<1) | ((val&0x40)>>1);
38 fd=open("/proc/dp83820",O_RDWR);
41 printf("Failed to open the /proc/dp83820 file to access the flashrom.\n");
42 printf("Make sure you've done:\n");
43 printf(" modprobe dp83820flash\n");
48 void set(int addr, unsigned char val)
73 int getromsize(unsigned char *id)
75 if(id[0]==0xbf && id[1]==0xb6) return 0x40000;
76 if(id[0]==0xc2 && id[1]==0xb0) return 0x40000;
77 if(id[0]==0x1f && id[1]==0x3d) return 0x10000;
81 #define MAXROMSIZE 0x200000
82 unsigned char *buffer;
84 int loadfile(char *name)
88 filefd=open(name,O_RDONLY);
91 printf("Couldn't open file %s\n",name);
94 filesize=read(filefd,buffer,MAXROMSIZE);
98 printf("Error trying to read from file %s\n",name);
103 void readbios(char *name,int len)
107 unsigned char block[256];
110 filefd=open(name,O_WRONLY|O_TRUNC|O_CREAT,0644);
113 printf("Couldn't create file %s for writing\n",name);
119 if(j<sizeof(block)) continue;
120 filesize+=write(filefd,block,j);
126 printf("Error during write of %s file\n",name);
129 printf("BIOS contents saved to %s, $%x bytes\n",name,len);
132 int verifybios(char *name,int len, int print)
138 filelen=loadfile(name);
139 for(i=0;i<filelen;++i)
140 if(get(i)!=buffer[i]) break;
144 printf("BIOS contents does not match file %s, from byte $%x\n",
149 printf("BIOS contents match file %s for all of its $%x bytes\n",
156 void writebios(char *name,int len,unsigned char *id)
162 if(len!=loadfile(name))
164 printf("File size does not match expected ROM size\n");
167 if(0 && (id[0]!=0xbf || id[1]!=0xb6))
169 printf("Don't know how to write this kind of flash device\n");
173 printf("Erasing device\n");
174 set(0x5555,fixb(0xaa));
175 set(0x2aaa,fixb(0x55));
176 set(0x5555,fixb(0x80));
177 set(0x5555,fixb(0xaa));
178 set(0x2aaa,fixb(0x55));
179 set(0x5555,fixb(0x10));
183 printf(".");fflush(stdout);
185 if(get(0)==get(0) && get(0)==get(0))
188 printf("BIOS erased\n");
190 printf("Writing to BIOS\n");
197 printf("\r%d%%",p1=p2);
202 set(0x5555,fixb(0xaa));
203 set(0x2aaa,fixb(0x55));
204 set(0x5555,fixb(0xa0));
207 if(i%sectorsize==sectorsize-1)
208 while(get(0)!=get(0) || get(0)!=get(0));
213 void helptext(char *name)
215 printf("USE: %s <options>\n",name);
216 printf(" -v <filename> = verify bios rom contents with file\n");
217 printf(" -w <filename> = write to bios rom contents from file\n");
218 printf(" -r <filename> = read from bios rom contents to file\n");
219 printf(" -f = force erase/write even if contents already match\n");
223 int main(int argc,char **argv)
236 if(argc<2) helptext(argv[0]);
252 else helptext(argv[0]);
260 buffer=malloc(MAXROMSIZE);
263 printf("No memory available!\n");
267 set(0x5555,fixb(0xaa)); // get into flash ID mode
268 set(0x2aaa,fixb(0x55));
269 set(0x5555,fixb(0x90));
271 for(i=0;i<4;++i) id[i]=get(i);
273 set(0x5555,fixb(0xaa)); // get out of flash ID mode
274 set(0x2aaa,fixb(0x55));
275 set(0x5555,fixb(0xf0));
279 if(id[i]!=get(i)) break;
282 printf("Could not read BIOS flashrom ID.\n");
285 printf("ID %02x %02x\n",id[0],id[1]);
286 romsize=getromsize(id);
289 printf("Unknown rom type\n");
292 printf("romsize=$%x bytes\n",romsize);
294 readbios(filename,romsize);
298 same=verifybios(filename,romsize,0);
302 writebios(filename,romsize,id);
304 if(action=='v' || action=='w')
305 verifybios(filename,romsize,1);