flash.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * flash - stand-alone FLASH ROM upgrade program
  3. *
  4. * Copyright (c) 2009 Openmoko Inc.
  5. *
  6. * Authors Christopher Hall <hsw@openmoko.com>
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include <stdlib.h>
  22. #include <stdbool.h>
  23. #include <tff.h>
  24. #include <diskio.h>
  25. #include <delay.h>
  26. #include <misc.h>
  27. #include <lcd.h>
  28. #include <FLASH.h>
  29. #include "program.h"
  30. #include "ok.h"
  31. #include "fail.h"
  32. // size of the flash memory
  33. uint8_t ROMBuffer[FLASH_TotalBytes];
  34. enum {
  35. ProgramRetries = 5,
  36. };
  37. static bool process(const char *filename);
  38. int flash(int arg)
  39. {
  40. // set the initial stack and data pointers
  41. asm volatile (
  42. "\txld.w\t%r15, __MAIN_STACK\n"
  43. "\tld.w\t%sp, %r15\n"
  44. "\txld.w\t%r15, __dp\n"
  45. "\tld.w\t%r5, 0\n"
  46. "\tld.w\t%psr, %r5\n"
  47. );
  48. print("Flash Program: ");
  49. const char *filename = "flash.rom";
  50. FLASH_initialise();
  51. if (0 == arg) {
  52. print("Internal");
  53. FLASH_SelectInternal();
  54. } else {
  55. print("Test Jig");
  56. filename = "test-jig.rom";
  57. FLASH_SelectExternal();
  58. }
  59. print(" FLASH\n");
  60. int i = 0;
  61. bool flag = false;
  62. for (i = 0; i < ProgramRetries; ++i) {
  63. print("Begin Pass: ");
  64. print_dec32(i);
  65. print_char('\n');
  66. LCD_DisplayImage(LCD_PositionTop, true, &program_image);
  67. if (process(filename)) {
  68. print("Finished sucessfully\n");
  69. LCD_DisplayImage(LCD_PositionTop, true, &ok_image);
  70. flag = true;
  71. break;
  72. } else {
  73. print("Error occurred\n");
  74. LCD_DisplayImage(LCD_PositionTop, true, &fail_image);
  75. }
  76. }
  77. print(flag ? "PASS" : "FAIL");
  78. print(": FLASH MBR\n");
  79. for (;;) {
  80. }
  81. }
  82. static bool process(const char *filename)
  83. {
  84. bool result = true;
  85. uint8_t b = 0;
  86. // power cycle the SD Card
  87. disk_ioctl(0, CTRL_POWER, &b);
  88. disk_initialize(0);
  89. FATFS TheFileSystem;
  90. f_mount(0, &TheFileSystem); // only possible value is zero
  91. FIL file;
  92. FRESULT rc = FR_OK;
  93. print("Loading: ");
  94. print(filename);
  95. print(" ");
  96. rc = f_open(&file, filename, FA_READ);
  97. if (FR_OK != rc) {
  98. print("open error = ");
  99. print_dec32(rc);
  100. print_char('\n');
  101. return false;
  102. }
  103. unsigned int length;
  104. rc = f_read(&file, ROMBuffer, sizeof(ROMBuffer), &length);
  105. if (FR_OK != rc) {
  106. print("read error = ");
  107. print_dec32(rc);
  108. print_char('\n');
  109. f_close(&file);
  110. return false;
  111. }
  112. if (sizeof(ROMBuffer) != length) {
  113. print("only read: ");
  114. print_dec32(length);
  115. print(" required ");
  116. print_dec32(sizeof(ROMBuffer));
  117. print_char('\n');
  118. f_close(&file);
  119. return false;
  120. }
  121. f_close(&file);
  122. print("OK\n");
  123. print("Preserve Serial number: ");
  124. FLASH_read(&ROMBuffer[FLASH_SerialNumberAddress], FLASH_SerialNumberSize, FLASH_SerialNumberAddress);
  125. size_t i;
  126. for (i = 0; i < FLASH_SerialNumberSize; ++i) {
  127. char c = ROMBuffer[FLASH_SerialNumberAddress + i];
  128. if (' ' > c || '\xff' == c) {
  129. break;
  130. }
  131. print_char(c);
  132. }
  133. print("\nErase: ");
  134. if (FLASH_WriteEnable() && FLASH_ChipErase()) {
  135. print("OK");
  136. } else {
  137. print("FAIL\n");
  138. return false;
  139. }
  140. print("\nProgram: ");
  141. for (i = 0; i < sizeof(ROMBuffer); i += FLASH_PageSize) {
  142. if (!FLASH_WriteEnable() || !FLASH_write(&ROMBuffer[i], FLASH_PageSize, i)) {
  143. print_char('E');
  144. }
  145. if (0 == (i & (FLASH_SectorSize - 1))) {
  146. print_char('.');
  147. }
  148. }
  149. print_char('\n');
  150. print("Verify: ");
  151. for (i = 0; i < sizeof(ROMBuffer); i += FLASH_SectorSize) {
  152. if(FLASH_verify(&ROMBuffer[i], FLASH_SectorSize, i)) {
  153. print_char('.');
  154. } else {
  155. print_char('E');
  156. result = false;
  157. }
  158. }
  159. print_char('\n');
  160. return result;
  161. }