burner.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:6k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  *  arch/mips/galileo/compressed/burner.c
  3.  *
  4.  *  By RidgeRun Inc (Leveraged from Galileo's main.c, misc.c, etc).
  5.  *
  6.  *  Burn image from ram to flash
  7.  *  For use with Galileo EVB64120A MIPS eval board.
  8.  */
  9. #include <asm/types.h>
  10. #include <asm/byteorder.h>
  11. #include <asm/galileo-boards/evb64120A/eeprom_param.h>
  12. #include <asm/galileo-boards/evb64120A/flashdrv.h>
  13. #define IMAGEOFFSET 0x00300000
  14. static void burn_image_from_memory(void);
  15. static char *sprintf(char *buf, const char *fmt, ...);
  16. static void printf(const char *fmt, ...);
  17. unsigned int FlashSize;
  18. /******************************
  19.  Routine:
  20.  Description:
  21.  ******************************/
  22. int main(void)
  23. {
  24. printf("n");
  25. printf("n");
  26. printf("n");
  27. printf("n");
  28. printf("       +--------------------+n");
  29. printf("       |                    |n");
  30. printf("       | Flash Burn Utility |n");
  31. printf("       |                    |n");
  32. printf("       +--------------------+n");
  33. printf("n");
  34. printf("Please send your *.srec image to the parallel portn");
  35. printf("n");
  36. printf("Note: The *.srec image should be setup ton");
  37. printf("      load into address 0xa0300000 wheren");
  38. printf("      it will then be transferred to flashn");
  39. printf("n");
  40. SET_REG_BITS(0x468, BIT20); // Set Flash to be 16 bit wide
  41. FlashSize = flashInit(0xbf000000, 2, X16);
  42. galileo_dl(); // read in the users *.srec image.
  43. burn_image_from_memory(); // put it in flash.
  44. printf("n");
  45. printf("n");
  46. printf("+---------------+n");
  47. printf("|     Done      |n");
  48. printf("|(please reboot)|n");
  49. printf("+---------------+n");
  50. printf("n");
  51. while (1) {
  52. }
  53. return 0;
  54. }
  55. /******************************
  56.  Routine:
  57.  Description:
  58.  ******************************/
  59. static void burn_image_from_memory(void)
  60. {
  61. unsigned int count, delta, temp, temp1;
  62. unsigned int to_sector, last_sector;
  63. /* Find how many sectors needed to be erased */
  64. to_sector = flashInWhichSector(FlashSize - 4); // skranz, modified.
  65. if (to_sector == 0xffffffff) {
  66. printf
  67.     ("Flash Burning Error - Flash too small - Cannot burn imagen"); // skranz, modified.
  68. return;
  69. }
  70. /* Which is the last sector */
  71. last_sector = flashInWhichSector(FlashSize - 4);
  72. delta = 0;
  73. printf("nErasing first %d sectorsn", to_sector);
  74. for (count = 0; count < to_sector + 1; count++) {
  75. printf("Erasing sector %dn", count);
  76. flashEraseSector(count);
  77. }
  78. printf("flash region size = %dn", FlashSize); // skranz, added
  79. printf("Sdram IMAGEOFFSET = %dn", IMAGEOFFSET); // skranz, added
  80. printf("Burning from Sdram to %d mark; full burn.n", FlashSize);
  81. for (count = 0; count < (FlashSize - delta); count = count + 4) {
  82. flashWriteWord(count,
  83.        *(unsigned int *) ((count | NONE_CACHEABLE)
  84.   + IMAGEOFFSET)); // skranz, modified.
  85. temp = flashReadWord(count);
  86. temp1 = *(unsigned int *) ((count | NONE_CACHEABLE) + IMAGEOFFSET); // skranz, modified.
  87. if (((unsigned int) temp) != ((unsigned int) temp1)) {
  88. printf
  89.     ("Burning error at address %X : flash(%X) sdram(%X)n",
  90.      count, flashReadWord(count),
  91.      *(unsigned int *) ((count | NONE_CACHEABLE) + IMAGEOFFSET)); // skranz, modified.
  92. printf("Aborting Prematurally.n");
  93. break;
  94. }
  95. }
  96. printf("Finished burning Imagen");
  97. }
  98. /******************************
  99.  Routine:
  100.  Description:
  101. Formats:
  102. %X - 4 byte ASCII (8 hex digits)
  103. %x - 2 byte ASCII (4 hex digits)
  104. %b - 1 byte ASCII (2 hex digits)
  105. %d - decimal (also %i)
  106. %c - ASCII char
  107. %s - ASCII string
  108. %I - Internet address in x.x.x.x notation
  109.  ******************************/
  110. static char hex[] = "0123456789ABCDEF";
  111. static char *do_printf(char *buf, const char *fmt, const int *dp)
  112. {
  113. register char *p;
  114. char tmp[16];
  115. while (*fmt) {
  116. if (*fmt == '%') { /* switch() uses more space */
  117. fmt++;
  118. if (*fmt == 'X') {
  119. const long *lp = (const long *) dp;
  120. register long h = *lp++;
  121. dp = (const int *) lp;
  122. *(buf++) = hex[(h >> 28) & 0x0F];
  123. *(buf++) = hex[(h >> 24) & 0x0F];
  124. *(buf++) = hex[(h >> 20) & 0x0F];
  125. *(buf++) = hex[(h >> 16) & 0x0F];
  126. *(buf++) = hex[(h >> 12) & 0x0F];
  127. *(buf++) = hex[(h >> 8) & 0x0F];
  128. *(buf++) = hex[(h >> 4) & 0x0F];
  129. *(buf++) = hex[h & 0x0F];
  130. }
  131. if (*fmt == 'x') {
  132. register int h = *(dp++);
  133. *(buf++) = hex[(h >> 12) & 0x0F];
  134. *(buf++) = hex[(h >> 8) & 0x0F];
  135. *(buf++) = hex[(h >> 4) & 0x0F];
  136. *(buf++) = hex[h & 0x0F];
  137. }
  138. if (*fmt == 'b') {
  139. register int h = *(dp++);
  140. *(buf++) = hex[(h >> 4) & 0x0F];
  141. *(buf++) = hex[h & 0x0F];
  142. }
  143. if ((*fmt == 'd') || (*fmt == 'i')) {
  144. register int dec = *(dp++);
  145. p = tmp;
  146. if (dec < 0) {
  147. *(buf++) = '-';
  148. dec = -dec;
  149. }
  150. do {
  151. *(p++) = '0' + (dec % 10);
  152. dec = dec / 10;
  153. } while (dec);
  154. while ((--p) >= tmp)
  155. *(buf++) = *p;
  156. }
  157. if (*fmt == 'I') {
  158. union {
  159. long l;
  160. unsigned char c[4];
  161. } u;
  162. const long *lp = (const long *) dp;
  163. u.l = *lp++;
  164. dp = (const int *) lp;
  165. buf = sprintf(buf, "%d.%d.%d.%d",
  166.       u.c[0], u.c[1], u.c[2],
  167.       u.c[3]);
  168. }
  169. if (*fmt == 'c')
  170. *(buf++) = *(dp++);
  171. if (*fmt == 's') {
  172. p = (char *) *dp++;
  173. while (*p)
  174. *(buf++) = *p++;
  175. }
  176. } else
  177. *(buf++) = *fmt;
  178. fmt++;
  179. }
  180. *buf = 0;
  181. return (buf);
  182. }
  183. /******************************
  184.  Routine:
  185.  Description:
  186.  ******************************/
  187. static char *sprintf(char *buf, const char *fmt, ...)
  188. {
  189. return do_printf(buf, fmt, ((const int *) &fmt) + 1);
  190. }
  191. /******************************
  192.  Routine:
  193.  Description:
  194.  ******************************/
  195. void putchar(int c)
  196. {
  197. if (c == 'n') {
  198. serial_putc('r');
  199. }
  200. serial_putc(c);
  201. }
  202. /******************************
  203.  Routine:
  204.  Description:
  205.  ******************************/
  206. static void printf(const char *fmt, ...)
  207. {
  208. char buf[256], *p;
  209. p = buf;
  210. do_printf(buf, fmt, ((const int *) &fmt) + 1);
  211. while (*p)
  212. putchar(*p++);
  213. }