update.c 9.9 KB

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