Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

bromutil.c 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * readutil.c - perform various control ops on the 3c509b bios rom
  3. *
  4. */
  5. #ifndef __i386__
  6. # error "This program can't compile or run on non-intel computers"
  7. #else
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <unistd.h>
  11. #include <string.h>
  12. #ifdef __FreeBSD__
  13. #include <fcntl.h>
  14. #include <machine/cpufunc.h>
  15. #define OUTB(data, port) outb(port, data)
  16. #define OUTW(data, port) outw(port, data)
  17. #define OUTL(data, port) outl(port, data)
  18. #else
  19. #include <sys/io.h>
  20. #define OUTB(data, port) outb(data, port)
  21. #define OUTW(data, port) outw(data, port)
  22. #define OUTL(data, port) outl(data, port)
  23. #endif
  24. /*
  25. * write_eeprom() and enum definitions are copied from vortex-diag.c,
  26. * Copyright 1997-2004 by Donald Becker.
  27. * This software may be used and distributed according to the terms of
  28. * the GNU General Public License (GPL), incorporated herein by reference.
  29. * Contact the author for use under other terms.
  30. */
  31. enum vortex_cmd {
  32. TotalReset = 0<<11, SelectWindow = 1<<11, StartCoax = 2<<11,
  33. RxDisable = 3<<11, RxEnable = 4<<11, RxReset = 5<<11,
  34. UpStall = 6<<11, UpUnstall = (6<<11)+1,
  35. DownStall = (6<<11)+2, DownUnstall = (6<<11)+3,
  36. RxDiscard = 8<<11, TxEnable = 9<<11, TxDisable = 10<<11, TxReset = 11<<11,
  37. FakeIntr = 12<<11, AckIntr = 13<<11, SetIntrEnb = 14<<11,
  38. SetStatusEnb = 15<<11, SetRxFilter = 16<<11, SetRxThreshold = 17<<11,
  39. SetTxThreshold = 18<<11, SetTxStart = 19<<11,
  40. StartDMAUp = 20<<11, StartDMADown = (20<<11)+1, StatsEnable = 21<<11,
  41. StatsDisable = 22<<11, StopCoax = 23<<11, SetFilterBit = 25<<11,
  42. };
  43. enum Window0 {
  44. Wn0EepromCmd = 10, /* Window 0: EEPROM command register. */
  45. Wn0EepromData = 12, /* Window 0: EEPROM results register. */
  46. IntrStatus=0x0E, /* Valid in all windows. */
  47. };
  48. enum Win0_EEPROM_cmds {
  49. EEPROM_Read = 2, EEPROM_WRITE = 1, EEPROM_ERASE = 3,
  50. EEPROM_EWENB = 0xC, /* Enable erasing/writing for 10 msec. */
  51. EEPROM_EWDIS = 0x0, /* Disable EWENB before 10 msec timeout. */
  52. };
  53. #define debug 1
  54. static void write_eeprom(long ioaddr, int addrlen, int index, int value)
  55. {
  56. int timer;
  57. /* Verify that the EEPROM is idle. */
  58. for (timer = 1620; inw(ioaddr + Wn0EepromCmd) & 0x8000;)
  59. if (--timer < 0)
  60. goto error_return;
  61. /* Enable writing: EEPROM_EWENB | 110000.... */
  62. OUTW(3 << (addrlen-2), ioaddr + Wn0EepromCmd);
  63. for (timer = 400; inw(ioaddr + Wn0EepromCmd) & 0x8000;) {
  64. if (--timer < 0)
  65. goto error_return;
  66. }
  67. if (debug)
  68. fprintf(stderr, "EEPROM write enable took %d ticks!\n", 400 - timer);
  69. OUTW((EEPROM_ERASE << addrlen) + index, ioaddr + Wn0EepromCmd);
  70. for (timer = 16000; inw(ioaddr + Wn0EepromCmd) & 0x8000;)
  71. if (--timer < 0) {
  72. fprintf(stderr, "EEPROM failed to erase index %d!\n", index);
  73. return;
  74. }
  75. if (debug)
  76. fprintf(stderr, "EEPROM erased index %d after %d ticks!\n",
  77. index, 16000-timer);
  78. OUTW(3 << (addrlen-2), ioaddr + Wn0EepromCmd);
  79. for (timer = 400; inw(ioaddr + Wn0EepromCmd) & 0x8000;) {
  80. if (--timer < 0)
  81. goto error_return;
  82. }
  83. if (debug)
  84. fprintf(stderr, "EEPROM write enable took %d ticks!\n", 400-timer);
  85. OUTW(value, ioaddr + Wn0EepromData);
  86. OUTW((EEPROM_WRITE << addrlen) + index, ioaddr + Wn0EepromCmd);
  87. for (timer = 16000; inw(ioaddr + Wn0EepromCmd) & 0x8000;)
  88. if (--timer < 0)
  89. goto error_return;
  90. if (debug)
  91. fprintf(stderr, "EEPROM wrote index %d with 0x%4.4x after %d ticks!\n",
  92. index, value, 16000-timer);
  93. return;
  94. error_return:
  95. fprintf(stderr, "Failed to write EEPROM location %d with 0x%4.4x!\n",
  96. index, value);
  97. }
  98. int main(int argc, char **argv)
  99. {
  100. unsigned int i, j, n;
  101. unsigned int ioaddr;
  102. unsigned long recvrstat;
  103. unsigned char buf[128];
  104. unsigned char b;
  105. if (argc != 3) {
  106. printf
  107. ("Usage: romid ioaddr [erase|protect|unprotect|id|bootrom|read >file|prog <file]\n");
  108. exit(-1);
  109. }
  110. #ifdef __FreeBSD__
  111. /* get permissions for in/out{blw} */
  112. open("/dev/io", O_RDONLY, 0);
  113. #else
  114. setuid(0); /* if we're setuid, do it really */
  115. if (iopl(3)) {
  116. perror("iopl()");
  117. exit(1);
  118. }
  119. #endif
  120. sscanf(argv[1], "%x", &ioaddr);
  121. /* Set the register window to 3 for the 3c905b */
  122. OUTW(0x803, ioaddr + 0xe);
  123. recvrstat = inl(ioaddr); /* save the receiver status */
  124. /* set the receiver type to MII so the full bios rom address space
  125. can be accessed */
  126. OUTL((recvrstat & 0xf00fffff) | 0x00600000, ioaddr);
  127. /* Set the register window to 0 for the 3c905b */
  128. OUTW(0x800, ioaddr + 0xe);
  129. if (strcmp(argv[2], "erase") == 0) {
  130. /* do the funky chicken to erase the rom contents */
  131. OUTL(0x5555, ioaddr + 0x4);
  132. OUTB(0xaa, ioaddr + 0x8);
  133. OUTL(0x2aaa, ioaddr + 0x4);
  134. OUTB(0x55, ioaddr + 0x8);
  135. OUTL(0x5555, ioaddr + 0x4);
  136. OUTB(0x80, ioaddr + 0x8);
  137. OUTL(0x5555, ioaddr + 0x4);
  138. OUTB(0xaa, ioaddr + 0x8);
  139. OUTL(0x2aaa, ioaddr + 0x4);
  140. OUTB(0x55, ioaddr + 0x8);
  141. OUTL(0x5555, ioaddr + 0x4);
  142. OUTB(0x10, ioaddr + 0x8);
  143. printf("Bios ROM at %04x has been erased\n", ioaddr);
  144. } else if (strcmp(argv[2], "protect") == 0) {
  145. OUTL(0x5555, ioaddr + 0x4);
  146. OUTB(0xaa, ioaddr + 0x8);
  147. OUTL(0x2aaa, ioaddr + 0x4);
  148. OUTB(0x55, ioaddr + 0x8);
  149. OUTL(0x5555, ioaddr + 0x4);
  150. OUTB(0xa0, ioaddr + 0x8);
  151. printf
  152. ("Software Data Protection for Bios ROM at %04x has been enabled\n",
  153. ioaddr);
  154. } else if (strcmp(argv[2], "unprotect") == 0) {
  155. OUTL(0x5555, ioaddr + 0x4);
  156. OUTB(0xaa, ioaddr + 0x8);
  157. OUTL(0x2aaa, ioaddr + 0x4);
  158. OUTB(0x55, ioaddr + 0x8);
  159. OUTL(0x5555, ioaddr + 0x4);
  160. OUTB(0x80, ioaddr + 0x8);
  161. OUTL(0x5555, ioaddr + 0x4);
  162. OUTB(0xaa, ioaddr + 0x8);
  163. OUTL(0x2aaa, ioaddr + 0x4);
  164. OUTB(0x55, ioaddr + 0x8);
  165. OUTL(0x5555, ioaddr + 0x4);
  166. OUTB(0x20, ioaddr + 0x8);
  167. printf
  168. ("Software Data Protection for Bios ROM at %04x has been disabled\n",
  169. ioaddr);
  170. } else if (strcmp(argv[2], "id") == 0) {
  171. OUTL(0x5555, ioaddr + 0x4);
  172. OUTB(0xaa, ioaddr + 0x8);
  173. OUTL(0x2aaa, ioaddr + 0x4);
  174. OUTB(0x55, ioaddr + 0x8);
  175. OUTL(0x5555, ioaddr + 0x4);
  176. OUTB(0x90, ioaddr + 0x8);
  177. /* 10ms delay needed */
  178. printf("Manufacturer ID - ");
  179. /* manuf. id */
  180. OUTL(0x0000, ioaddr + 0x4);
  181. printf("%02x\n", inb(ioaddr + 0x8));
  182. /* device id */
  183. OUTL(0x0001, ioaddr + 0x4);
  184. printf("Device ID - %02x\n", inb(ioaddr + 0x8));
  185. /* undo the funky chicken */
  186. OUTL(0x5555, ioaddr + 0x4);
  187. OUTB(0xaa, ioaddr + 0x8);
  188. OUTL(0x2aaa, ioaddr + 0x4);
  189. OUTB(0x55, ioaddr + 0x8);
  190. OUTL(0x5555, ioaddr + 0x4);
  191. OUTB(0xf0, ioaddr + 0x8);
  192. } else if(strcmp(argv[2], "bootrom") == 0) {
  193. printf("bootrom fix\n");
  194. write_eeprom(ioaddr, 6, 19, 0x160);
  195. } else if (strcmp(argv[2], "read") == 0) {
  196. for (i = 0; i < 65536; i++) {
  197. OUTL(i, ioaddr + 0x4);
  198. b = inb(ioaddr + 0x8);
  199. write(1, &b, 1);
  200. }
  201. } else if (strcmp(argv[2], "prog") == 0) {
  202. /* program the rom in 128 bute chunks */
  203. for (i = 0, n = 0; i < 65536; i += n) {
  204. n = read(0, buf, 128);
  205. if (n == 0)
  206. break;
  207. if (n < 0) {
  208. perror("File Error");
  209. exit(-3);
  210. }
  211. /* disable SDP temporarily for programming a sector */
  212. OUTL(0x5555, ioaddr + 0x4);
  213. OUTB(0xaa, ioaddr + 0x8);
  214. OUTL(0x2aaa, ioaddr + 0x4);
  215. OUTB(0x55, ioaddr + 0x8);
  216. OUTL(0x5555, ioaddr + 0x4);
  217. OUTB(0xa0, ioaddr + 0x8);
  218. for (j = 0; j < n; j++) {
  219. OUTL(i + j, ioaddr + 0x4);
  220. OUTB(buf[j], ioaddr + 0x8);
  221. }
  222. /* wait for the programming of this sector to coomplete */
  223. while (inb(ioaddr + 0x8) != buf[j - 1]);
  224. }
  225. }
  226. /* Set the register window to 3 for the 3c905b */
  227. OUTW(0x803, ioaddr + 0xe);
  228. /* restore the receiver status */
  229. OUTL(recvrstat, ioaddr);
  230. return 0;
  231. }
  232. #endif /* __i386__ */