update.c 7.2 KB

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