update.c 8.0 KB

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