update.c 9.3 KB

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