update.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. * (C) Copyright 2008 Semihalf
  3. *
  4. * Written by: Rafal Czubak <rcz@semihalf.com>
  5. * Bartlomiej Sieka <tur@semihalf.com>
  6. *
  7. * See file CREDITS for list of people who contributed to this
  8. * project.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License as
  12. * published by the Free Software Foundation; either version 2 of
  13. * the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  23. * MA 02111-1307 USA
  24. *
  25. */
  26. #include <common.h>
  27. #if !(defined(CONFIG_FIT) && defined(CONFIG_OF_LIBFDT))
  28. #error "CONFIG_FIT and CONFIG_OF_LIBFDT are required for auto-update feature"
  29. #endif
  30. #if defined(CONFIG_SYS_NO_FLASH)
  31. #error "CONFIG_SYS_NO_FLASH defined, but FLASH is required for auto-update feature"
  32. #endif
  33. #include <command.h>
  34. #include <flash.h>
  35. #include <net.h>
  36. #include <malloc.h>
  37. /* env variable holding the location of the update file */
  38. #define UPDATE_FILE_ENV "updatefile"
  39. /* set configuration defaults if needed */
  40. #ifndef CONFIG_UPDATE_LOAD_ADDR
  41. #define CONFIG_UPDATE_LOAD_ADDR 0x100000
  42. #endif
  43. #ifndef CONFIG_UPDATE_TFTP_MSEC_MAX
  44. #define CONFIG_UPDATE_TFTP_MSEC_MAX 100
  45. #endif
  46. #ifndef CONFIG_UPDATE_TFTP_CNT_MAX
  47. #define CONFIG_UPDATE_TFTP_CNT_MAX 0
  48. #endif
  49. extern ulong TftpRRQTimeoutMSecs;
  50. extern int TftpRRQTimeoutCountMax;
  51. extern flash_info_t flash_info[];
  52. extern ulong load_addr;
  53. static uchar *saved_prot_info;
  54. static int update_load(char *filename, ulong msec_max, int cnt_max, ulong addr)
  55. {
  56. int size, rv;
  57. ulong saved_timeout_msecs;
  58. int saved_timeout_count;
  59. char *saved_netretry, *saved_bootfile;
  60. rv = 0;
  61. /* save used globals and env variable */
  62. saved_timeout_msecs = TftpRRQTimeoutMSecs;
  63. saved_timeout_count = TftpRRQTimeoutCountMax;
  64. saved_netretry = strdup(getenv("netretry"));
  65. saved_bootfile = strdup(BootFile);
  66. /* set timeouts for auto-update */
  67. TftpRRQTimeoutMSecs = msec_max;
  68. TftpRRQTimeoutCountMax = cnt_max;
  69. /* we don't want to retry the connection if errors occur */
  70. setenv("netretry", "no");
  71. /* download the update file */
  72. load_addr = addr;
  73. copy_filename(BootFile, filename, sizeof(BootFile));
  74. size = NetLoop(TFTP);
  75. if (size < 0)
  76. rv = 1;
  77. else if (size > 0)
  78. flush_cache(addr, size);
  79. /* restore changed globals and env variable */
  80. TftpRRQTimeoutMSecs = saved_timeout_msecs;
  81. TftpRRQTimeoutCountMax = saved_timeout_count;
  82. setenv("netretry", saved_netretry);
  83. if (saved_netretry != NULL)
  84. free(saved_netretry);
  85. if (saved_bootfile != NULL) {
  86. copy_filename(BootFile, saved_bootfile, sizeof(BootFile));
  87. free(saved_bootfile);
  88. }
  89. return rv;
  90. }
  91. static int update_flash_protect(int prot, ulong addr_first, ulong addr_last)
  92. {
  93. uchar *sp_info_ptr;
  94. ulong s;
  95. int i, bank, cnt;
  96. flash_info_t *info;
  97. sp_info_ptr = NULL;
  98. if (prot == 0) {
  99. saved_prot_info =
  100. calloc(CONFIG_SYS_MAX_FLASH_BANKS * CONFIG_SYS_MAX_FLASH_SECT, 1);
  101. if (!saved_prot_info)
  102. return 1;
  103. }
  104. for (bank = 0; bank < CONFIG_SYS_MAX_FLASH_BANKS; ++bank) {
  105. cnt = 0;
  106. info = &flash_info[bank];
  107. /* Nothing to do if the bank doesn't exist */
  108. if (info->sector_count == 0)
  109. return 0;
  110. /* Point to current bank protection information */
  111. sp_info_ptr = saved_prot_info + (bank * CONFIG_SYS_MAX_FLASH_SECT);
  112. /*
  113. * Adjust addr_first or addr_last if we are on bank boundary.
  114. * Address space between banks must be continuous for other
  115. * flash functions (like flash_sect_erase or flash_write) to
  116. * succeed. Banks must also be numbered in correct order,
  117. * according to increasing addresses.
  118. */
  119. if (addr_last > info->start[0] + info->size - 1)
  120. addr_last = info->start[0] + info->size - 1;
  121. if (addr_first < info->start[0])
  122. addr_first = info->start[0];
  123. for (i = 0; i < info->sector_count; i++) {
  124. /* Save current information about protected sectors */
  125. if (prot == 0) {
  126. s = info->start[i];
  127. if ((s >= addr_first) && (s <= addr_last))
  128. sp_info_ptr[i] = info->protect[i];
  129. }
  130. /* Protect/unprotect sectors */
  131. if (sp_info_ptr[i] == 1) {
  132. #if defined(CONFIG_SYS_FLASH_PROTECTION)
  133. if (flash_real_protect(info, i, prot))
  134. return 1;
  135. #else
  136. info->protect[i] = prot;
  137. #endif
  138. cnt++;
  139. }
  140. }
  141. if (cnt) {
  142. printf("%sProtected %d sectors\n",
  143. prot ? "": "Un-", cnt);
  144. }
  145. }
  146. if((prot == 1) && saved_prot_info)
  147. free(saved_prot_info);
  148. return 0;
  149. }
  150. static int update_flash(ulong addr_source, ulong addr_first, ulong size)
  151. {
  152. ulong addr_last = addr_first + size - 1;
  153. /* round last address to the sector boundary */
  154. if (flash_sect_roundb(&addr_last) > 0)
  155. return 1;
  156. if (addr_first >= addr_last) {
  157. printf("Error: end address exceeds addressing space\n");
  158. return 1;
  159. }
  160. /* remove protection on processed sectors */
  161. if (update_flash_protect(0, addr_first, addr_last) > 0) {
  162. printf("Error: could not unprotect flash sectors\n");
  163. return 1;
  164. }
  165. printf("Erasing 0x%08lx - 0x%08lx", addr_first, addr_last);
  166. if (flash_sect_erase(addr_first, addr_last) > 0) {
  167. printf("Error: could not erase flash\n");
  168. return 1;
  169. }
  170. printf("Copying to flash...");
  171. if (flash_write((char *)addr_source, addr_first, size) > 0) {
  172. printf("Error: could not copy to flash\n");
  173. return 1;
  174. }
  175. printf("done\n");
  176. /* enable protection on processed sectors */
  177. if (update_flash_protect(1, addr_first, addr_last) > 0) {
  178. printf("Error: could not protect flash sectors\n");
  179. return 1;
  180. }
  181. return 0;
  182. }
  183. static int update_fit_getparams(const void *fit, int noffset, ulong *addr,
  184. ulong *fladdr, ulong *size)
  185. {
  186. const void *data;
  187. if (fit_image_get_data(fit, noffset, &data, (size_t *)size))
  188. return 1;
  189. if (fit_image_get_load(fit, noffset, (ulong *)fladdr))
  190. return 1;
  191. *addr = (ulong)data;
  192. return 0;
  193. }
  194. void update_tftp(void)
  195. {
  196. char *filename, *env_addr;
  197. int images_noffset, ndepth, noffset;
  198. ulong update_addr, update_fladdr, update_size;
  199. ulong addr;
  200. void *fit;
  201. printf("Auto-update from TFTP: ");
  202. /* get the file name of the update file */
  203. filename = getenv(UPDATE_FILE_ENV);
  204. if (filename == NULL) {
  205. printf("failed, env. variable '%s' not found\n",
  206. UPDATE_FILE_ENV);
  207. return;
  208. }
  209. printf("trying update file '%s'\n", filename);
  210. /* get load address of downloaded update file */
  211. if ((env_addr = getenv("loadaddr")) != NULL)
  212. addr = simple_strtoul(env_addr, NULL, 16);
  213. else
  214. addr = CONFIG_UPDATE_LOAD_ADDR;
  215. if (update_load(filename, CONFIG_UPDATE_TFTP_MSEC_MAX,
  216. CONFIG_UPDATE_TFTP_CNT_MAX, addr)) {
  217. printf("Can't load update file, aborting auto-update\n");
  218. return;
  219. }
  220. fit = (void *)addr;
  221. if (!fit_check_format((void *)fit)) {
  222. printf("Bad FIT format of the update file, aborting "
  223. "auto-update\n");
  224. return;
  225. }
  226. /* process updates */
  227. images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
  228. ndepth = 0;
  229. noffset = fdt_next_node(fit, images_noffset, &ndepth);
  230. while (noffset >= 0 && ndepth > 0) {
  231. if (ndepth != 1)
  232. goto next_node;
  233. printf("Processing update '%s' :",
  234. fit_get_name(fit, noffset, NULL));
  235. if (!fit_image_check_hashes(fit, noffset)) {
  236. printf("Error: invalid update hash, aborting\n");
  237. goto next_node;
  238. }
  239. printf("\n");
  240. if (update_fit_getparams(fit, noffset, &update_addr,
  241. &update_fladdr, &update_size)) {
  242. printf("Error: can't get update parameteres, "
  243. "aborting\n");
  244. goto next_node;
  245. }
  246. if (update_flash(update_addr, update_fladdr, update_size)) {
  247. printf("Error: can't flash update, aborting\n");
  248. goto next_node;
  249. }
  250. next_node:
  251. noffset = fdt_next_node(fit, noffset, &ndepth);
  252. }
  253. return;
  254. }