cmd_flash.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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 defined(CONFIG_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 > CONFIG_SYS_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 <CONFIG_SYS_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 > CONFIG_SYS_MAX_FLASH_BANKS)) {
  94. printf ("Only FLASH Banks # 1 ... # %d supported\n",
  95. CONFIG_SYS_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. return cmd_usage(cmdtp);
  110. if (strcmp(argv[1], "all") == 0) {
  111. for (bank=1; bank<=CONFIG_SYS_MAX_FLASH_BANKS; ++bank) {
  112. printf ("Erase Flash Bank # %ld ", bank);
  113. info = &flash_info[bank-1];
  114. rcode = flash_erase (info, 0, info->sector_count-1);
  115. }
  116. return rcode;
  117. }
  118. if ((n = abbrev_spec(argv[1], &info, &sect_first, &sect_last)) != 0) {
  119. if (n < 0) {
  120. printf("Bad sector specification\n");
  121. return 1;
  122. }
  123. printf ("Erase Flash Sectors %d-%d in Bank # %d ",
  124. sect_first, sect_last, (info-flash_info)+1);
  125. rcode = flash_erase(info, sect_first, sect_last);
  126. return rcode;
  127. }
  128. if (argc != 3)
  129. return cmd_usage(cmdtp);
  130. if (strcmp(argv[1], "bank") == 0) {
  131. bank = simple_strtoul(argv[2], NULL, 16);
  132. if ((bank < 1) || (bank > CONFIG_SYS_MAX_FLASH_BANKS)) {
  133. printf ("Only FLASH Banks # 1 ... # %d supported\n",
  134. CONFIG_SYS_MAX_FLASH_BANKS);
  135. return 1;
  136. }
  137. printf ("Erase Flash Bank # %ld ", bank);
  138. info = &flash_info[bank-1];
  139. rcode = flash_erase (info, 0, info->sector_count-1);
  140. return rcode;
  141. }
  142. addr_first = simple_strtoul(argv[1], NULL, 16);
  143. addr_last = simple_strtoul(argv[2], NULL, 16);
  144. if (addr_first >= addr_last)
  145. return cmd_usage(cmdtp);
  146. printf ("Erase Flash from 0x%08lx to 0x%08lx ", addr_first, addr_last);
  147. rcode = flash_sect_erase(addr_first, addr_last);
  148. return rcode;
  149. }
  150. int flash_sect_erase (ulong addr_first, ulong addr_last)
  151. {
  152. flash_info_t *info;
  153. ulong bank;
  154. int s_first, s_last;
  155. int erased;
  156. int rcode = 0;
  157. erased = 0;
  158. for (bank=0,info = &flash_info[0]; bank < CONFIG_SYS_MAX_FLASH_BANKS; ++bank, ++info) {
  159. ulong b_end;
  160. int sect;
  161. if (info->flash_id == FLASH_UNKNOWN) {
  162. continue;
  163. }
  164. b_end = info->start[0] + info->size - 1; /* bank end addr */
  165. s_first = -1; /* first sector to erase */
  166. s_last = -1; /* last sector to erase */
  167. for (sect=0; sect < info->sector_count; ++sect) {
  168. ulong end; /* last address in current sect */
  169. short s_end;
  170. s_end = info->sector_count - 1;
  171. end = (sect == s_end) ? b_end : info->start[sect + 1] - 1;
  172. if (addr_first > end)
  173. continue;
  174. if (addr_last < info->start[sect])
  175. continue;
  176. if (addr_first == info->start[sect]) {
  177. s_first = sect;
  178. }
  179. if (addr_last == end) {
  180. s_last = sect;
  181. }
  182. }
  183. if (s_first>=0 && s_first<=s_last) {
  184. erased += s_last - s_first + 1;
  185. rcode = flash_erase (info, s_first, s_last);
  186. }
  187. }
  188. if (erased) {
  189. /* printf ("Erased %d sectors\n", erased); */
  190. } else {
  191. printf ("Error: start and/or end address"
  192. " not on sector boundary\n");
  193. rcode = 1;
  194. }
  195. return rcode;
  196. }
  197. int do_protect(cmd_tbl_t *cmdtp, bd_t *bd, int flag, int argc, char *argv[])
  198. {
  199. flash_info_t *info;
  200. ulong bank, addr_first, addr_last;
  201. int i, p, n, sect_first, sect_last;
  202. int rcode = 0;
  203. if (argc < 3)
  204. return cmd_usage(cmdtp);
  205. if (strcmp(argv[1], "off") == 0)
  206. p = 0;
  207. else if (strcmp(argv[1], "on") == 0)
  208. p = 1;
  209. else
  210. return cmd_usage(cmdtp);
  211. if (strcmp(argv[2], "all") == 0) {
  212. for (bank=1; bank<=CONFIG_SYS_MAX_FLASH_BANKS; ++bank) {
  213. info = &flash_info[bank-1];
  214. if (info->flash_id == FLASH_UNKNOWN) {
  215. continue;
  216. }
  217. /*printf ("%sProtect Flash Bank # %ld\n", */
  218. /* p ? "" : "Un-", bank); */
  219. for (i=0; i<info->sector_count; ++i) {
  220. #if defined(CONFIG_SYS_FLASH_PROTECTION)
  221. if (flash_real_protect(info, i, p))
  222. rcode = 1;
  223. putc ('.');
  224. #else
  225. info->protect[i] = p;
  226. #endif /* CONFIG_SYS_FLASH_PROTECTION */
  227. }
  228. }
  229. #if defined(CONFIG_SYS_FLASH_PROTECTION)
  230. if (!rcode) puts (" done\n");
  231. #endif /* CONFIG_SYS_FLASH_PROTECTION */
  232. return rcode;
  233. }
  234. if ((n = abbrev_spec(argv[2], &info, &sect_first, &sect_last)) != 0) {
  235. if (n < 0) {
  236. printf("Bad sector specification\n");
  237. return 1;
  238. }
  239. /*printf("%sProtect Flash Sectors %d-%d in Bank # %d\n", */
  240. /* p ? "" : "Un-", sect_first, sect_last, */
  241. /* (info-flash_info)+1); */
  242. for (i = sect_first; i <= sect_last; i++) {
  243. #if defined(CONFIG_SYS_FLASH_PROTECTION)
  244. if (flash_real_protect(info, i, p))
  245. rcode = 1;
  246. putc ('.');
  247. #else
  248. info->protect[i] = p;
  249. #endif /* CONFIG_SYS_FLASH_PROTECTION */
  250. }
  251. #if defined(CONFIG_SYS_FLASH_PROTECTION)
  252. if (!rcode) puts (" done\n");
  253. #endif /* CONFIG_SYS_FLASH_PROTECTION */
  254. return rcode;
  255. }
  256. if (argc != 4)
  257. return cmd_usage(cmdtp);
  258. if (strcmp(argv[2], "bank") == 0) {
  259. bank = simple_strtoul(argv[3], NULL, 16);
  260. if ((bank < 1) || (bank > CONFIG_SYS_MAX_FLASH_BANKS)) {
  261. printf ("Only FLASH Banks # 1 ... # %d supported\n",
  262. CONFIG_SYS_MAX_FLASH_BANKS);
  263. return 1;
  264. }
  265. printf ("%sProtect Flash Bank # %ld\n",
  266. p ? "" : "Un-", bank);
  267. info = &flash_info[bank-1];
  268. if (info->flash_id == FLASH_UNKNOWN) {
  269. printf ("missing or unknown FLASH type\n");
  270. return 1;
  271. }
  272. for (i=0; i<info->sector_count; ++i) {
  273. #if defined(CONFIG_SYS_FLASH_PROTECTION)
  274. if (flash_real_protect(info, i, p))
  275. rcode = 1;
  276. putc ('.');
  277. #else
  278. info->protect[i] = p;
  279. #endif /* CONFIG_SYS_FLASH_PROTECTION */
  280. }
  281. #if defined(CONFIG_SYS_FLASH_PROTECTION)
  282. if (!rcode)
  283. puts(" done\n");
  284. #endif /* CONFIG_SYS_FLASH_PROTECTION */
  285. return rcode;
  286. }
  287. addr_first = simple_strtoul(argv[2], NULL, 16);
  288. addr_last = simple_strtoul(argv[3], NULL, 16);
  289. if (addr_first >= addr_last)
  290. return cmd_usage(cmdtp);
  291. return flash_sect_protect (p, addr_first, addr_last);
  292. }
  293. int flash_sect_protect (int p, ulong addr_first, ulong addr_last)
  294. {
  295. flash_info_t *info;
  296. ulong bank;
  297. int s_first, s_last;
  298. int protected, i;
  299. int rcode = 0;
  300. protected = 0;
  301. for (bank=0,info = &flash_info[0]; bank < CONFIG_SYS_MAX_FLASH_BANKS; ++bank, ++info) {
  302. ulong b_end;
  303. int sect;
  304. if (info->flash_id == FLASH_UNKNOWN) {
  305. continue;
  306. }
  307. b_end = info->start[0] + info->size - 1; /* bank end addr */
  308. s_first = -1; /* first sector to erase */
  309. s_last = -1; /* last sector to erase */
  310. for (sect=0; sect < info->sector_count; ++sect) {
  311. ulong end; /* last address in current sect */
  312. short s_end;
  313. s_end = info->sector_count - 1;
  314. end = (sect == s_end) ? b_end : info->start[sect + 1] - 1;
  315. if (addr_first > end)
  316. continue;
  317. if (addr_last < info->start[sect])
  318. continue;
  319. if (addr_first == info->start[sect]) {
  320. s_first = sect;
  321. }
  322. if (addr_last == end) {
  323. s_last = sect;
  324. }
  325. }
  326. if (s_first>=0 && s_first<=s_last) {
  327. protected += s_last - s_first + 1;
  328. for (i=s_first; i<=s_last; ++i) {
  329. #if defined(CONFIG_SYS_FLASH_PROTECTION)
  330. if (flash_real_protect(info, i, p))
  331. rcode = 1;
  332. putc ('.');
  333. #else
  334. info->protect[i] = p;
  335. #endif /* CONFIG_SYS_FLASH_PROTECTION */
  336. }
  337. }
  338. #if defined(CONFIG_SYS_FLASH_PROTECTION)
  339. if (!rcode) putc ('\n');
  340. #endif /* CONFIG_SYS_FLASH_PROTECTION */
  341. }
  342. if (protected) {
  343. /* printf ("%sProtected %d sectors\n", */
  344. /* p ? "" : "Un-", protected); */
  345. } else {
  346. printf ("Error: start and/or end address"
  347. " not on sector boundary\n");
  348. rcode = 1;
  349. }
  350. return rcode;
  351. }
  352. #endif