update.c 8.0 KB

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