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