cmd_flash.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /*
  2. * (C) Copyright 2000
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (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, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. /*
  24. * FLASH support
  25. */
  26. #include <common.h>
  27. #include <command.h>
  28. #include <flash.h>
  29. #if (CONFIG_COMMANDS & CFG_CMD_FLASH)
  30. extern flash_info_t flash_info[]; /* info for FLASH chips */
  31. /*
  32. * The user interface starts numbering for Flash banks with 1
  33. * for historical reasons.
  34. */
  35. /*
  36. * this routine looks for an abbreviated flash range specification.
  37. * the syntax is B:SF[-SL], where B is the bank number, SF is the first
  38. * sector to erase, and SL is the last sector to erase (defaults to SF).
  39. * bank numbers start at 1 to be consistent with other specs, sector numbers
  40. * start at zero.
  41. *
  42. * returns: 1 - correct spec; *pinfo, *psf and *psl are
  43. * set appropriately
  44. * 0 - doesn't look like an abbreviated spec
  45. * -1 - looks like an abbreviated spec, but got
  46. * a parsing error, a number out of range,
  47. * or an invalid flash bank.
  48. */
  49. static int
  50. abbrev_spec(char *str, flash_info_t **pinfo, int *psf, int *psl)
  51. {
  52. flash_info_t *fp;
  53. int bank, first, last;
  54. char *p, *ep;
  55. if ((p = strchr(str, ':')) == NULL)
  56. return 0;
  57. *p++ = '\0';
  58. bank = simple_strtoul(str, &ep, 10);
  59. if (ep == str || *ep != '\0' ||
  60. bank < 1 || bank > CFG_MAX_FLASH_BANKS ||
  61. (fp = &flash_info[bank - 1])->flash_id == FLASH_UNKNOWN)
  62. return -1;
  63. str = p;
  64. if ((p = strchr(str, '-')) != NULL)
  65. *p++ = '\0';
  66. first = simple_strtoul(str, &ep, 10);
  67. if (ep == str || *ep != '\0' || first >= fp->sector_count)
  68. return -1;
  69. if (p != NULL) {
  70. last = simple_strtoul(p, &ep, 10);
  71. if (ep == p || *ep != '\0' ||
  72. last < first || last >= fp->sector_count)
  73. return -1;
  74. }
  75. else
  76. last = first;
  77. *pinfo = fp;
  78. *psf = first;
  79. *psl = last;
  80. return 1;
  81. }
  82. int do_flinfo (cmd_tbl_t *cmdtp, bd_t *bd, int flag, int argc, char *argv[])
  83. {
  84. ulong bank;
  85. if (argc == 1) { /* print info for all FLASH banks */
  86. for (bank=0; bank <CFG_MAX_FLASH_BANKS; ++bank) {
  87. printf ("\nBank # %ld: ", bank+1);
  88. flash_print_info (&flash_info[bank]);
  89. }
  90. return 0;
  91. }
  92. bank = simple_strtoul(argv[1], NULL, 16);
  93. if ((bank < 1) || (bank > CFG_MAX_FLASH_BANKS)) {
  94. printf ("Only FLASH Banks # 1 ... # %d supported\n",
  95. CFG_MAX_FLASH_BANKS);
  96. return 1;
  97. }
  98. printf ("\nBank # %ld: ", bank);
  99. flash_print_info (&flash_info[bank-1]);
  100. return 0;
  101. }
  102. int do_flerase(cmd_tbl_t *cmdtp, bd_t *bd, int flag, int argc, char *argv[])
  103. {
  104. flash_info_t *info;
  105. ulong bank, addr_first, addr_last;
  106. int n, sect_first, sect_last;
  107. int rcode = 0;
  108. if (argc < 2) {
  109. printf ("Usage:\n%s\n", cmdtp->usage);
  110. return 1;
  111. }
  112. if (strcmp(argv[1], "all") == 0) {
  113. for (bank=1; bank<=CFG_MAX_FLASH_BANKS; ++bank) {
  114. printf ("Erase Flash Bank # %ld ", bank);
  115. info = &flash_info[bank-1];
  116. rcode = flash_erase (info, 0, info->sector_count-1);
  117. }
  118. return rcode;
  119. }
  120. if ((n = abbrev_spec(argv[1], &info, &sect_first, &sect_last)) != 0) {
  121. if (n < 0) {
  122. printf("Bad sector specification\n");
  123. return 1;
  124. }
  125. printf ("Erase Flash Sectors %d-%d in Bank # %d ",
  126. sect_first, sect_last, (info-flash_info)+1);
  127. rcode = flash_erase(info, sect_first, sect_last);
  128. return rcode;
  129. }
  130. if (argc != 3) {
  131. printf ("Usage:\n%s\n", cmdtp->usage);
  132. return 1;
  133. }
  134. if (strcmp(argv[1], "bank") == 0) {
  135. bank = simple_strtoul(argv[2], NULL, 16);
  136. if ((bank < 1) || (bank > CFG_MAX_FLASH_BANKS)) {
  137. printf ("Only FLASH Banks # 1 ... # %d supported\n",
  138. CFG_MAX_FLASH_BANKS);
  139. return 1;
  140. }
  141. printf ("Erase Flash Bank # %ld ", bank);
  142. info = &flash_info[bank-1];
  143. rcode = flash_erase (info, 0, info->sector_count-1);
  144. return rcode;
  145. }
  146. addr_first = simple_strtoul(argv[1], NULL, 16);
  147. addr_last = simple_strtoul(argv[2], NULL, 16);
  148. if (addr_first >= addr_last) {
  149. printf ("Usage:\n%s\n", cmdtp->usage);
  150. return 1;
  151. }
  152. printf ("Erase Flash from 0x%08lx to 0x%08lx ", addr_first, addr_last);
  153. rcode = flash_sect_erase(addr_first, addr_last);
  154. return rcode;
  155. }
  156. int flash_sect_erase (ulong addr_first, ulong addr_last)
  157. {
  158. flash_info_t *info;
  159. ulong bank;
  160. int s_first, s_last;
  161. int erased;
  162. int rcode = 0;
  163. erased = 0;
  164. for (bank=0,info=&flash_info[0]; bank < CFG_MAX_FLASH_BANKS; ++bank, ++info) {
  165. ulong b_end;
  166. int sect;
  167. if (info->flash_id == FLASH_UNKNOWN) {
  168. continue;
  169. }
  170. b_end = info->start[0] + info->size - 1; /* bank end addr */
  171. s_first = -1; /* first sector to erase */
  172. s_last = -1; /* last sector to erase */
  173. for (sect=0; sect < info->sector_count; ++sect) {
  174. ulong end; /* last address in current sect */
  175. short s_end;
  176. s_end = info->sector_count - 1;
  177. end = (sect == s_end) ? b_end : info->start[sect + 1] - 1;
  178. if (addr_first > end)
  179. continue;
  180. if (addr_last < info->start[sect])
  181. continue;
  182. if (addr_first == info->start[sect]) {
  183. s_first = sect;
  184. }
  185. if (addr_last == end) {
  186. s_last = sect;
  187. }
  188. }
  189. if (s_first>=0 && s_first<=s_last) {
  190. erased += s_last - s_first + 1;
  191. rcode = flash_erase (info, s_first, s_last);
  192. }
  193. }
  194. if (erased) {
  195. /* printf ("Erased %d sectors\n", erased); */
  196. } else {
  197. printf ("Error: start and/or end address"
  198. " not on sector boundary\n");
  199. rcode = 1;
  200. }
  201. return rcode;
  202. }
  203. int do_protect(cmd_tbl_t *cmdtp, bd_t *bd, int flag, int argc, char *argv[])
  204. {
  205. flash_info_t *info;
  206. ulong bank, addr_first, addr_last;
  207. int i, p, n, sect_first, sect_last;
  208. int rcode = 0;
  209. if (argc < 3) {
  210. printf ("Usage:\n%s\n", cmdtp->usage);
  211. return 1;
  212. }
  213. if (strcmp(argv[1], "off") == 0)
  214. p = 0;
  215. else if (strcmp(argv[1], "on") == 0)
  216. p = 1;
  217. else {
  218. printf ("Usage:\n%s\n", cmdtp->usage);
  219. return 1;
  220. }
  221. if (strcmp(argv[2], "all") == 0) {
  222. for (bank=1; bank<=CFG_MAX_FLASH_BANKS; ++bank) {
  223. info = &flash_info[bank-1];
  224. if (info->flash_id == FLASH_UNKNOWN) {
  225. continue;
  226. }
  227. /*printf ("%sProtect Flash Bank # %ld\n", */
  228. /* p ? "" : "Un-", bank); */
  229. for (i=0; i<info->sector_count; ++i) {
  230. #if defined(CFG_FLASH_PROTECTION)
  231. if (flash_real_protect(info, i, p))
  232. rcode = 1;
  233. putc ('.');
  234. #else
  235. info->protect[i] = p;
  236. #endif /* CFG_FLASH_PROTECTION */
  237. }
  238. }
  239. #if defined(CFG_FLASH_PROTECTION)
  240. if (!rcode) puts (" done\n");
  241. #endif /* CFG_FLASH_PROTECTION */
  242. return rcode;
  243. }
  244. if ((n = abbrev_spec(argv[2], &info, &sect_first, &sect_last)) != 0) {
  245. if (n < 0) {
  246. printf("Bad sector specification\n");
  247. return 1;
  248. }
  249. /*printf("%sProtect Flash Sectors %d-%d in Bank # %d\n", */
  250. /* p ? "" : "Un-", sect_first, sect_last, */
  251. /* (info-flash_info)+1); */
  252. for (i = sect_first; i <= sect_last; i++) {
  253. #if defined(CFG_FLASH_PROTECTION)
  254. if (flash_real_protect(info, i, p))
  255. rcode = 1;
  256. putc ('.');
  257. #else
  258. info->protect[i] = p;
  259. #endif /* CFG_FLASH_PROTECTION */
  260. }
  261. #if defined(CFG_FLASH_PROTECTION)
  262. if (!rcode) puts (" done\n");
  263. #endif /* CFG_FLASH_PROTECTION */
  264. return rcode;
  265. }
  266. if (argc != 4) {
  267. printf ("Usage:\n%s\n", cmdtp->usage);
  268. return 1;
  269. }
  270. if (strcmp(argv[2], "bank") == 0) {
  271. bank = simple_strtoul(argv[3], NULL, 16);
  272. if ((bank < 1) || (bank > CFG_MAX_FLASH_BANKS)) {
  273. printf ("Only FLASH Banks # 1 ... # %d supported\n",
  274. CFG_MAX_FLASH_BANKS);
  275. return 1;
  276. }
  277. printf ("%sProtect Flash Bank # %ld\n",
  278. p ? "" : "Un-", bank);
  279. info = &flash_info[bank-1];
  280. if (info->flash_id == FLASH_UNKNOWN) {
  281. printf ("missing or unknown FLASH type\n");
  282. return 1;
  283. }
  284. for (i=0; i<info->sector_count; ++i) {
  285. #if defined(CFG_FLASH_PROTECTION)
  286. if (flash_real_protect(info, i, p))
  287. rcode = 1;
  288. putc ('.');
  289. #else
  290. info->protect[i] = p;
  291. #endif /* CFG_FLASH_PROTECTION */
  292. }
  293. #if defined(CFG_FLASH_PROTECTION)
  294. if (!rcode) puts (" done\n");
  295. #endif /* CFG_FLASH_PROTECTION */
  296. return rcode;
  297. }
  298. addr_first = simple_strtoul(argv[2], NULL, 16);
  299. addr_last = simple_strtoul(argv[3], NULL, 16);
  300. if (addr_first >= addr_last) {
  301. printf ("Usage:\n%s\n", cmdtp->usage);
  302. return 1;
  303. }
  304. rcode = flash_sect_protect (p, addr_first, addr_last);
  305. return rcode;
  306. }
  307. int flash_sect_protect (int p, ulong addr_first, ulong addr_last)
  308. {
  309. flash_info_t *info;
  310. ulong bank;
  311. int s_first, s_last;
  312. int protected, i;
  313. int rcode = 0;
  314. protected = 0;
  315. for (bank=0,info=&flash_info[0]; bank < CFG_MAX_FLASH_BANKS; ++bank, ++info) {
  316. ulong b_end;
  317. int sect;
  318. if (info->flash_id == FLASH_UNKNOWN) {
  319. continue;
  320. }
  321. b_end = info->start[0] + info->size - 1; /* bank end addr */
  322. s_first = -1; /* first sector to erase */
  323. s_last = -1; /* last sector to erase */
  324. for (sect=0; sect < info->sector_count; ++sect) {
  325. ulong end; /* last address in current sect */
  326. short s_end;
  327. s_end = info->sector_count - 1;
  328. end = (sect == s_end) ? b_end : info->start[sect + 1] - 1;
  329. if (addr_first > end)
  330. continue;
  331. if (addr_last < info->start[sect])
  332. continue;
  333. if (addr_first == info->start[sect]) {
  334. s_first = sect;
  335. }
  336. if (addr_last == end) {
  337. s_last = sect;
  338. }
  339. }
  340. if (s_first>=0 && s_first<=s_last) {
  341. protected += s_last - s_first + 1;
  342. for (i=s_first; i<=s_last; ++i) {
  343. #if defined(CFG_FLASH_PROTECTION)
  344. if (flash_real_protect(info, i, p))
  345. rcode = 1;
  346. putc ('.');
  347. #else
  348. info->protect[i] = p;
  349. #endif /* CFG_FLASH_PROTECTION */
  350. }
  351. }
  352. #if defined(CFG_FLASH_PROTECTION)
  353. if (!rcode) putc ('\n');
  354. #endif /* CFG_FLASH_PROTECTION */
  355. }
  356. if (protected) {
  357. /* printf ("%sProtected %d sectors\n", */
  358. /* p ? "" : "Un-", protected); */
  359. } else {
  360. printf ("Error: start and/or end address"
  361. " not on sector boundary\n");
  362. rcode = 1;
  363. }
  364. return rcode;
  365. }
  366. #endif /* CFG_CMD_FLASH */