update.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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 tftp_timeout_ms;
  34. extern int tftp_timeout_count_max;
  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 = tftp_timeout_ms;
  47. saved_timeout_count = tftp_timeout_count_max;
  48. saved_netretry = strdup(getenv("netretry"));
  49. saved_bootfile = strdup(net_boot_file_name);
  50. /* set timeouts for auto-update */
  51. tftp_timeout_ms = msec_max;
  52. tftp_timeout_count_max = 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(net_boot_file_name, filename, sizeof(net_boot_file_name));
  58. size = net_loop(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. tftp_timeout_ms = saved_timeout_msecs;
  65. tftp_timeout_count_max = 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(net_boot_file_name, saved_bootfile,
  71. sizeof(net_boot_file_name));
  72. free(saved_bootfile);
  73. }
  74. return rv;
  75. }
  76. static int update_flash_protect(int prot, ulong addr_first, ulong addr_last)
  77. {
  78. uchar *sp_info_ptr;
  79. ulong s;
  80. int i, bank, cnt;
  81. flash_info_t *info;
  82. sp_info_ptr = NULL;
  83. if (prot == 0) {
  84. saved_prot_info =
  85. calloc(CONFIG_SYS_MAX_FLASH_BANKS * CONFIG_SYS_MAX_FLASH_SECT, 1);
  86. if (!saved_prot_info)
  87. return 1;
  88. }
  89. for (bank = 0; bank < CONFIG_SYS_MAX_FLASH_BANKS; ++bank) {
  90. cnt = 0;
  91. info = &flash_info[bank];
  92. /* Nothing to do if the bank doesn't exist */
  93. if (info->sector_count == 0)
  94. return 0;
  95. /* Point to current bank protection information */
  96. sp_info_ptr = saved_prot_info + (bank * CONFIG_SYS_MAX_FLASH_SECT);
  97. /*
  98. * Adjust addr_first or addr_last if we are on bank boundary.
  99. * Address space between banks must be continuous for other
  100. * flash functions (like flash_sect_erase or flash_write) to
  101. * succeed. Banks must also be numbered in correct order,
  102. * according to increasing addresses.
  103. */
  104. if (addr_last > info->start[0] + info->size - 1)
  105. addr_last = info->start[0] + info->size - 1;
  106. if (addr_first < info->start[0])
  107. addr_first = info->start[0];
  108. for (i = 0; i < info->sector_count; i++) {
  109. /* Save current information about protected sectors */
  110. if (prot == 0) {
  111. s = info->start[i];
  112. if ((s >= addr_first) && (s <= addr_last))
  113. sp_info_ptr[i] = info->protect[i];
  114. }
  115. /* Protect/unprotect sectors */
  116. if (sp_info_ptr[i] == 1) {
  117. #if defined(CONFIG_SYS_FLASH_PROTECTION)
  118. if (flash_real_protect(info, i, prot))
  119. return 1;
  120. #else
  121. info->protect[i] = prot;
  122. #endif
  123. cnt++;
  124. }
  125. }
  126. if (cnt) {
  127. printf("%sProtected %d sectors\n",
  128. prot ? "": "Un-", cnt);
  129. }
  130. }
  131. if((prot == 1) && saved_prot_info)
  132. free(saved_prot_info);
  133. return 0;
  134. }
  135. static int update_flash(ulong addr_source, ulong addr_first, ulong size)
  136. {
  137. ulong addr_last = addr_first + size - 1;
  138. /* round last address to the sector boundary */
  139. if (flash_sect_roundb(&addr_last) > 0)
  140. return 1;
  141. if (addr_first >= addr_last) {
  142. printf("Error: end address exceeds addressing space\n");
  143. return 1;
  144. }
  145. /* remove protection on processed sectors */
  146. if (update_flash_protect(0, addr_first, addr_last) > 0) {
  147. printf("Error: could not unprotect flash sectors\n");
  148. return 1;
  149. }
  150. printf("Erasing 0x%08lx - 0x%08lx", addr_first, addr_last);
  151. if (flash_sect_erase(addr_first, addr_last) > 0) {
  152. printf("Error: could not erase flash\n");
  153. return 1;
  154. }
  155. printf("Copying to flash...");
  156. if (flash_write((char *)addr_source, addr_first, size) > 0) {
  157. printf("Error: could not copy to flash\n");
  158. return 1;
  159. }
  160. printf("done\n");
  161. /* enable protection on processed sectors */
  162. if (update_flash_protect(1, addr_first, addr_last) > 0) {
  163. printf("Error: could not protect flash sectors\n");
  164. return 1;
  165. }
  166. return 0;
  167. }
  168. static int update_fit_getparams(const void *fit, int noffset, ulong *addr,
  169. ulong *fladdr, ulong *size)
  170. {
  171. const void *data;
  172. if (fit_image_get_data(fit, noffset, &data, (size_t *)size))
  173. return 1;
  174. if (fit_image_get_load(fit, noffset, (ulong *)fladdr))
  175. return 1;
  176. *addr = (ulong)data;
  177. return 0;
  178. }
  179. int update_tftp(ulong addr)
  180. {
  181. char *filename, *env_addr;
  182. int images_noffset, ndepth, noffset;
  183. ulong update_addr, update_fladdr, update_size;
  184. void *fit;
  185. int ret = 0;
  186. /* use already present image */
  187. if (addr)
  188. goto got_update_file;
  189. printf("Auto-update from TFTP: ");
  190. /* get the file name of the update file */
  191. filename = getenv(UPDATE_FILE_ENV);
  192. if (filename == NULL) {
  193. printf("failed, env. variable '%s' not found\n",
  194. UPDATE_FILE_ENV);
  195. return 1;
  196. }
  197. printf("trying update file '%s'\n", filename);
  198. /* get load address of downloaded update file */
  199. if ((env_addr = getenv("loadaddr")) != NULL)
  200. addr = simple_strtoul(env_addr, NULL, 16);
  201. else
  202. addr = CONFIG_UPDATE_LOAD_ADDR;
  203. if (update_load(filename, CONFIG_UPDATE_TFTP_MSEC_MAX,
  204. CONFIG_UPDATE_TFTP_CNT_MAX, addr)) {
  205. printf("Can't load update file, aborting auto-update\n");
  206. return 1;
  207. }
  208. got_update_file:
  209. fit = (void *)addr;
  210. if (!fit_check_format((void *)fit)) {
  211. printf("Bad FIT format of the update file, aborting "
  212. "auto-update\n");
  213. return 1;
  214. }
  215. /* process updates */
  216. images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
  217. ndepth = 0;
  218. noffset = fdt_next_node(fit, images_noffset, &ndepth);
  219. while (noffset >= 0 && ndepth > 0) {
  220. if (ndepth != 1)
  221. goto next_node;
  222. printf("Processing update '%s' :",
  223. fit_get_name(fit, noffset, NULL));
  224. if (!fit_image_verify(fit, noffset)) {
  225. printf("Error: invalid update hash, aborting\n");
  226. ret = 1;
  227. goto next_node;
  228. }
  229. printf("\n");
  230. if (update_fit_getparams(fit, noffset, &update_addr,
  231. &update_fladdr, &update_size)) {
  232. printf("Error: can't get update parameteres, "
  233. "aborting\n");
  234. ret = 1;
  235. goto next_node;
  236. }
  237. if (update_flash(update_addr, update_fladdr, update_size)) {
  238. printf("Error: can't flash update, aborting\n");
  239. ret = 1;
  240. goto next_node;
  241. }
  242. next_node:
  243. noffset = fdt_next_node(fit, noffset, &ndepth);
  244. }
  245. return ret;
  246. }