update.c 8.1 KB

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