update.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /*
  2. * (C) Copyright 2008 Semihalf
  3. *
  4. * Written by: Rafal Czubak <rcz@semihalf.com>
  5. * Bartlomiej Sieka <tur@semihalf.com>
  6. *
  7. * See file CREDITS for list of people who contributed to this
  8. * project.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License as
  12. * published by the Free Software Foundation; either version 2 of
  13. * the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  23. * MA 02111-1307 USA
  24. *
  25. */
  26. #include <common.h>
  27. #if !(defined(CONFIG_FIT) && defined(CONFIG_OF_LIBFDT))
  28. #error "CONFIG_FIT and CONFIG_OF_LIBFDT are required for auto-update feature"
  29. #endif
  30. #if defined(CONFIG_SYS_NO_FLASH)
  31. #error "CONFIG_SYS_NO_FLASH defined, but FLASH is required for auto-update feature"
  32. #endif
  33. #include <command.h>
  34. #include <flash.h>
  35. #include <net.h>
  36. #include <net/tftp.h>
  37. #include <malloc.h>
  38. /* env variable holding the location of the update file */
  39. #define UPDATE_FILE_ENV "updatefile"
  40. /* set configuration defaults if needed */
  41. #ifndef CONFIG_UPDATE_LOAD_ADDR
  42. #define CONFIG_UPDATE_LOAD_ADDR 0x100000
  43. #endif
  44. #ifndef CONFIG_UPDATE_TFTP_MSEC_MAX
  45. #define CONFIG_UPDATE_TFTP_MSEC_MAX 100
  46. #endif
  47. #ifndef CONFIG_UPDATE_TFTP_CNT_MAX
  48. #define CONFIG_UPDATE_TFTP_CNT_MAX 0
  49. #endif
  50. extern ulong TftpRRQTimeoutMSecs;
  51. extern int TftpRRQTimeoutCountMax;
  52. extern flash_info_t flash_info[];
  53. extern ulong load_addr;
  54. static uchar *saved_prot_info;
  55. static int update_load(char *filename, ulong msec_max, int cnt_max, ulong addr)
  56. {
  57. int size, rv;
  58. ulong saved_timeout_msecs;
  59. int saved_timeout_count;
  60. char *saved_netretry, *saved_bootfile;
  61. rv = 0;
  62. /* save used globals and env variable */
  63. saved_timeout_msecs = TftpRRQTimeoutMSecs;
  64. saved_timeout_count = TftpRRQTimeoutCountMax;
  65. saved_netretry = strdup(getenv("netretry"));
  66. saved_bootfile = strdup(BootFile);
  67. /* set timeouts for auto-update */
  68. TftpRRQTimeoutMSecs = msec_max;
  69. TftpRRQTimeoutCountMax = cnt_max;
  70. /* we don't want to retry the connection if errors occur */
  71. setenv("netretry", "no");
  72. /* download the update file */
  73. load_addr = addr;
  74. copy_filename(BootFile, filename, sizeof(BootFile));
  75. size = NetLoop(TFTPGET);
  76. if (size < 0)
  77. rv = 1;
  78. else if (size > 0)
  79. flush_cache(addr, size);
  80. /* restore changed globals and env variable */
  81. TftpRRQTimeoutMSecs = saved_timeout_msecs;
  82. TftpRRQTimeoutCountMax = saved_timeout_count;
  83. setenv("netretry", saved_netretry);
  84. if (saved_netretry != NULL)
  85. free(saved_netretry);
  86. if (saved_bootfile != NULL) {
  87. copy_filename(BootFile, saved_bootfile, sizeof(BootFile));
  88. free(saved_bootfile);
  89. }
  90. return rv;
  91. }
  92. static int update_flash_protect(int prot, ulong addr_first, ulong addr_last)
  93. {
  94. uchar *sp_info_ptr;
  95. ulong s;
  96. int i, bank, cnt;
  97. flash_info_t *info;
  98. sp_info_ptr = NULL;
  99. if (prot == 0) {
  100. saved_prot_info =
  101. calloc(CONFIG_SYS_MAX_FLASH_BANKS * CONFIG_SYS_MAX_FLASH_SECT, 1);
  102. if (!saved_prot_info)
  103. return 1;
  104. }
  105. for (bank = 0; bank < CONFIG_SYS_MAX_FLASH_BANKS; ++bank) {
  106. cnt = 0;
  107. info = &flash_info[bank];
  108. /* Nothing to do if the bank doesn't exist */
  109. if (info->sector_count == 0)
  110. return 0;
  111. /* Point to current bank protection information */
  112. sp_info_ptr = saved_prot_info + (bank * CONFIG_SYS_MAX_FLASH_SECT);
  113. /*
  114. * Adjust addr_first or addr_last if we are on bank boundary.
  115. * Address space between banks must be continuous for other
  116. * flash functions (like flash_sect_erase or flash_write) to
  117. * succeed. Banks must also be numbered in correct order,
  118. * according to increasing addresses.
  119. */
  120. if (addr_last > info->start[0] + info->size - 1)
  121. addr_last = info->start[0] + info->size - 1;
  122. if (addr_first < info->start[0])
  123. addr_first = info->start[0];
  124. for (i = 0; i < info->sector_count; i++) {
  125. /* Save current information about protected sectors */
  126. if (prot == 0) {
  127. s = info->start[i];
  128. if ((s >= addr_first) && (s <= addr_last))
  129. sp_info_ptr[i] = info->protect[i];
  130. }
  131. /* Protect/unprotect sectors */
  132. if (sp_info_ptr[i] == 1) {
  133. #if defined(CONFIG_SYS_FLASH_PROTECTION)
  134. if (flash_real_protect(info, i, prot))
  135. return 1;
  136. #else
  137. info->protect[i] = prot;
  138. #endif
  139. cnt++;
  140. }
  141. }
  142. if (cnt) {
  143. printf("%sProtected %d sectors\n",
  144. prot ? "": "Un-", cnt);
  145. }
  146. }
  147. if((prot == 1) && saved_prot_info)
  148. free(saved_prot_info);
  149. return 0;
  150. }
  151. static int update_flash(ulong addr_source, ulong addr_first, ulong size)
  152. {
  153. ulong addr_last = addr_first + size - 1;
  154. /* round last address to the sector boundary */
  155. if (flash_sect_roundb(&addr_last) > 0)
  156. return 1;
  157. if (addr_first >= addr_last) {
  158. printf("Error: end address exceeds addressing space\n");
  159. return 1;
  160. }
  161. /* remove protection on processed sectors */
  162. if (update_flash_protect(0, addr_first, addr_last) > 0) {
  163. printf("Error: could not unprotect flash sectors\n");
  164. return 1;
  165. }
  166. printf("Erasing 0x%08lx - 0x%08lx", addr_first, addr_last);
  167. if (flash_sect_erase(addr_first, addr_last) > 0) {
  168. printf("Error: could not erase flash\n");
  169. return 1;
  170. }
  171. printf("Copying to flash...");
  172. if (flash_write((char *)addr_source, addr_first, size) > 0) {
  173. printf("Error: could not copy to flash\n");
  174. return 1;
  175. }
  176. printf("done\n");
  177. /* enable protection on processed sectors */
  178. if (update_flash_protect(1, addr_first, addr_last) > 0) {
  179. printf("Error: could not protect flash sectors\n");
  180. return 1;
  181. }
  182. return 0;
  183. }
  184. static int update_fit_getparams(const void *fit, int noffset, ulong *addr,
  185. ulong *fladdr, ulong *size)
  186. {
  187. const void *data;
  188. if (fit_image_get_data(fit, noffset, &data, (size_t *)size))
  189. return 1;
  190. if (fit_image_get_load(fit, noffset, (ulong *)fladdr))
  191. return 1;
  192. *addr = (ulong)data;
  193. return 0;
  194. }
  195. int update_tftp(ulong addr)
  196. {
  197. char *filename, *env_addr;
  198. int images_noffset, ndepth, noffset;
  199. ulong update_addr, update_fladdr, update_size;
  200. void *fit;
  201. int ret = 0;
  202. /* use already present image */
  203. if (addr)
  204. goto got_update_file;
  205. printf("Auto-update from TFTP: ");
  206. /* get the file name of the update file */
  207. filename = getenv(UPDATE_FILE_ENV);
  208. if (filename == NULL) {
  209. printf("failed, env. variable '%s' not found\n",
  210. UPDATE_FILE_ENV);
  211. return 1;
  212. }
  213. printf("trying update file '%s'\n", filename);
  214. /* get load address of downloaded update file */
  215. if ((env_addr = getenv("loadaddr")) != NULL)
  216. addr = simple_strtoul(env_addr, NULL, 16);
  217. else
  218. addr = CONFIG_UPDATE_LOAD_ADDR;
  219. if (update_load(filename, CONFIG_UPDATE_TFTP_MSEC_MAX,
  220. CONFIG_UPDATE_TFTP_CNT_MAX, addr)) {
  221. printf("Can't load update file, aborting auto-update\n");
  222. return 1;
  223. }
  224. got_update_file:
  225. fit = (void *)addr;
  226. if (!fit_check_format((void *)fit)) {
  227. printf("Bad FIT format of the update file, aborting "
  228. "auto-update\n");
  229. return 1;
  230. }
  231. /* process updates */
  232. images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
  233. ndepth = 0;
  234. noffset = fdt_next_node(fit, images_noffset, &ndepth);
  235. while (noffset >= 0 && ndepth > 0) {
  236. if (ndepth != 1)
  237. goto next_node;
  238. printf("Processing update '%s' :",
  239. fit_get_name(fit, noffset, NULL));
  240. if (!fit_image_verify(fit, noffset)) {
  241. printf("Error: invalid update hash, aborting\n");
  242. ret = 1;
  243. goto next_node;
  244. }
  245. printf("\n");
  246. if (update_fit_getparams(fit, noffset, &update_addr,
  247. &update_fladdr, &update_size)) {
  248. printf("Error: can't get update parameteres, "
  249. "aborting\n");
  250. ret = 1;
  251. goto next_node;
  252. }
  253. if (update_flash(update_addr, update_fladdr, update_size)) {
  254. printf("Error: can't flash update, aborting\n");
  255. ret = 1;
  256. goto next_node;
  257. }
  258. next_node:
  259. noffset = fdt_next_node(fit, noffset, &ndepth);
  260. }
  261. return ret;
  262. }