Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

dp83820_write.c 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. DP83820 flash utility written by Dave Ashley for NXTV, Inc.
  3. Copyright (C) 2004 by NXTV, Inc.
  4. Written 20040219 by Dave Ashley.
  5. Currently only supports the AT29C512
  6. This code is released under the terms of the GPL. No warranty.
  7. THEORY:
  8. This code uses the /proc/dp83820 file which is created by the
  9. dp83820flash.o module. That file allows single byte reads + writes
  10. to the bootrom.
  11. */
  12. #include <unistd.h>
  13. #include <sys/io.h>
  14. #include <stdio.h>
  15. #include <unistd.h>
  16. #include <sys/mman.h>
  17. #include <sys/types.h>
  18. #include <sys/stat.h>
  19. #include <fcntl.h>
  20. #include <stdlib.h>
  21. #include <signal.h>
  22. // SMC9462TX card has D5 + D6 on the bootrom socket reversed
  23. int fixb(int val)
  24. {
  25. return (val&~0x60) | ((val&0x20)<<1) | ((val&0x40)>>1);
  26. }
  27. int openit(void)
  28. {
  29. int fd;
  30. fd=open("/proc/dp83820",O_RDWR);
  31. if(fd<0)
  32. {
  33. printf("Failed to open the /proc/dp83820 file to access the flashrom.\n");
  34. printf("Make sure you've done:\n");
  35. printf(" modprobe dp83820flash\n");
  36. exit(-1);
  37. }
  38. return fd;
  39. }
  40. void set(int addr, unsigned char val)
  41. {
  42. unsigned char msg[3];
  43. int fd;
  44. fd=openit();
  45. msg[0]=addr;
  46. msg[1]=addr>>8;
  47. msg[2]=val;
  48. write(fd,msg,3);
  49. close(fd);
  50. }
  51. int get(int addr)
  52. {
  53. unsigned char msg[2];
  54. int fd;
  55. fd=openit();
  56. msg[0]=addr;
  57. msg[1]=addr>>8;
  58. write(fd,msg,2);
  59. read(fd,msg,1);
  60. close(fd);
  61. return msg[0];
  62. }
  63. int getromsize(unsigned char *id)
  64. {
  65. if(id[0]==0xbf && id[1]==0xb6) return 0x40000;
  66. if(id[0]==0xc2 && id[1]==0xb0) return 0x40000;
  67. if(id[0]==0x1f && id[1]==0x3d) return 0x10000;
  68. return -1;
  69. }
  70. #define MAXROMSIZE 0x200000
  71. unsigned char *buffer;
  72. int loadfile(char *name)
  73. {
  74. int filefd;
  75. int filesize;
  76. filefd=open(name,O_RDONLY);
  77. if(filefd<0)
  78. {
  79. printf("Couldn't open file %s\n",name);
  80. return -1;
  81. }
  82. filesize=read(filefd,buffer,MAXROMSIZE);
  83. close(filefd);
  84. if(filesize<0)
  85. {
  86. printf("Error trying to read from file %s\n",name);
  87. }
  88. return filesize;
  89. }
  90. void readbios(char *name,int len)
  91. {
  92. int filefd;
  93. int filesize=0;
  94. unsigned char block[256];
  95. int i,j;
  96. filefd=open(name,O_WRONLY|O_TRUNC|O_CREAT,0644);
  97. if(filefd<0)
  98. {
  99. printf("Couldn't create file %s for writing\n",name);
  100. return;
  101. }
  102. for(i=j=0;i<len;++i)
  103. {
  104. block[j++]=get(i);
  105. if(j<sizeof(block)) continue;
  106. filesize+=write(filefd,block,j);
  107. j=0;
  108. }
  109. close(filefd);
  110. if(filesize!=len)
  111. {
  112. printf("Error during write of %s file\n",name);
  113. return;
  114. }
  115. printf("BIOS contents saved to %s, $%x bytes\n",name,len);
  116. }
  117. int verifybios(char *name,int len, int print)
  118. {
  119. int filelen;
  120. int i;
  121. int same=0;
  122. filelen=loadfile(name);
  123. for(i=0;i<filelen;++i)
  124. if(get(i)!=buffer[i]) break;
  125. if(i<filelen)
  126. {
  127. if(print)
  128. printf("BIOS contents does not match file %s, from byte $%x\n",
  129. name,i);
  130. } else
  131. {
  132. if(print)
  133. printf("BIOS contents match file %s for all of its $%x bytes\n",
  134. name,i);
  135. same=1;
  136. }
  137. return same;
  138. }
  139. void writebios(char *name,int len,unsigned char *id)
  140. {
  141. int i;
  142. int p1,p2;
  143. int sectorsize=128;
  144. if(len!=loadfile(name))
  145. {
  146. printf("File size does not match expected ROM size\n");
  147. return;
  148. }
  149. if(0 && (id[0]!=0xbf || id[1]!=0xb6))
  150. {
  151. printf("Don't know how to write this kind of flash device\n");
  152. return;
  153. }
  154. printf("Erasing device\n");
  155. set(0x5555,fixb(0xaa));
  156. set(0x2aaa,fixb(0x55));
  157. set(0x5555,fixb(0x80));
  158. set(0x5555,fixb(0xaa));
  159. set(0x2aaa,fixb(0x55));
  160. set(0x5555,fixb(0x10));
  161. for(;;)
  162. {
  163. printf(".");fflush(stdout);
  164. usleep(250000);
  165. if(get(0)==get(0) && get(0)==get(0))
  166. break;
  167. }
  168. printf("BIOS erased\n");
  169. printf("Writing to BIOS\n");
  170. p1=-1;
  171. for(i=0;i<len;++i)
  172. {
  173. p2=100*i/(len-1);
  174. if(p2!=p1)
  175. {
  176. printf("\r%d%%",p1=p2);
  177. fflush(stdout);
  178. }
  179. if(i%sectorsize==0)
  180. {
  181. set(0x5555,fixb(0xaa));
  182. set(0x2aaa,fixb(0x55));
  183. set(0x5555,fixb(0xa0));
  184. }
  185. set(i,buffer[i]);
  186. if(i%sectorsize==sectorsize-1)
  187. while(get(0)!=get(0) || get(0)!=get(0));
  188. }
  189. printf("\n");
  190. }
  191. void helptext(char *name)
  192. {
  193. printf("USE: %s <options>\n",name);
  194. printf(" -v <filename> = verify bios rom contents with file\n");
  195. printf(" -w <filename> = write to bios rom contents from file\n");
  196. printf(" -r <filename> = read from bios rom contents to file\n");
  197. printf(" -f = force erase/write even if contents already match\n");
  198. exit(0);
  199. }
  200. int main(int argc,char **argv)
  201. {
  202. int i;
  203. int vals;
  204. unsigned char id[4];
  205. char *filename=0;
  206. char action=0;
  207. int romsize;
  208. int force=0;
  209. int same;
  210. vals=0;
  211. if(argc<2) helptext(argv[0]);
  212. for(i=1;i<argc;++i)
  213. {
  214. if(argv[i][0]!='-')
  215. helptext(argv[0]);
  216. switch(argv[i][1])
  217. {
  218. case 'f':
  219. force=1;
  220. break;
  221. case 'v':
  222. case 'w':
  223. case 'r':
  224. action=argv[i][1];
  225. if(i+1<argc)
  226. filename=argv[++i];
  227. else helptext(argv[0]);
  228. break;
  229. default:
  230. helptext(argv[0]);
  231. }
  232. }
  233. buffer=malloc(MAXROMSIZE);
  234. if(!buffer)
  235. {
  236. printf("No memory available!\n");
  237. exit(-1);
  238. }
  239. set(0x5555,fixb(0xaa)); // get into flash ID mode
  240. set(0x2aaa,fixb(0x55));
  241. set(0x5555,fixb(0x90));
  242. for(i=0;i<4;++i) id[i]=get(i);
  243. set(0x5555,fixb(0xaa)); // get out of flash ID mode
  244. set(0x2aaa,fixb(0x55));
  245. set(0x5555,fixb(0xf0));
  246. usleep(10000);
  247. for(i=0;i<4;++i)
  248. if(id[i]!=get(i)) break;
  249. if(i==4)
  250. {
  251. printf("Could not read BIOS flashrom ID.\n");
  252. goto biosdone;
  253. }
  254. printf("ID %02x %02x\n",id[0],id[1]);
  255. romsize=getromsize(id);
  256. if(romsize<0)
  257. {
  258. printf("Unknown rom type\n");
  259. goto biosdone;
  260. }
  261. printf("romsize=$%x bytes\n",romsize);
  262. if(action=='r')
  263. readbios(filename,romsize);
  264. if(action=='w')
  265. {
  266. if(!force)
  267. same=verifybios(filename,romsize,0);
  268. else
  269. same=0;
  270. if(!same)
  271. writebios(filename,romsize,id);
  272. }
  273. if(action=='v' || action=='w')
  274. verifybios(filename,romsize,1);
  275. biosdone:
  276. return 0;
  277. }