2 * (C) Copyright 2008 Semihalf
4 * Written by: Rafal Czubak <rcz@semihalf.com>
5 * Bartlomiej Sieka <tur@semihalf.com>
7 * SPDX-License-Identifier: GPL-2.0+
12 #if !(defined(CONFIG_FIT) && defined(CONFIG_OF_LIBFDT))
13 #error "CONFIG_FIT and CONFIG_OF_LIBFDT are required for auto-update feature"
16 #if defined(CONFIG_SYS_NO_FLASH)
17 #error "CONFIG_SYS_NO_FLASH defined, but FLASH is required for auto-update feature"
26 /* env variable holding the location of the update file */
27 #define UPDATE_FILE_ENV "updatefile"
29 /* set configuration defaults if needed */
30 #ifndef CONFIG_UPDATE_LOAD_ADDR
31 #define CONFIG_UPDATE_LOAD_ADDR 0x100000
34 #ifndef CONFIG_UPDATE_TFTP_MSEC_MAX
35 #define CONFIG_UPDATE_TFTP_MSEC_MAX 100
38 #ifndef CONFIG_UPDATE_TFTP_CNT_MAX
39 #define CONFIG_UPDATE_TFTP_CNT_MAX 0
42 extern ulong tftp_timeout_ms;
43 extern int tftp_timeout_count_max;
44 extern flash_info_t flash_info[];
45 extern ulong load_addr;
47 static uchar *saved_prot_info;
49 static int update_load(char *filename, ulong msec_max, int cnt_max, ulong addr)
52 ulong saved_timeout_msecs;
53 int saved_timeout_count;
54 char *saved_netretry, *saved_bootfile;
57 /* save used globals and env variable */
58 saved_timeout_msecs = tftp_timeout_ms;
59 saved_timeout_count = tftp_timeout_count_max;
60 saved_netretry = strdup(getenv("netretry"));
61 saved_bootfile = strdup(net_boot_file_name);
63 /* set timeouts for auto-update */
64 tftp_timeout_ms = msec_max;
65 tftp_timeout_count_max = cnt_max;
67 /* we don't want to retry the connection if errors occur */
68 setenv("netretry", "no");
70 /* download the update file */
72 copy_filename(net_boot_file_name, filename, sizeof(net_boot_file_name));
73 size = NetLoop(TFTPGET);
78 flush_cache(addr, size);
80 /* restore changed globals and env variable */
81 tftp_timeout_ms = saved_timeout_msecs;
82 tftp_timeout_count_max = saved_timeout_count;
84 setenv("netretry", saved_netretry);
85 if (saved_netretry != NULL)
88 if (saved_bootfile != NULL) {
89 copy_filename(net_boot_file_name, saved_bootfile,
90 sizeof(net_boot_file_name));
97 static int update_flash_protect(int prot, ulong addr_first, ulong addr_last)
108 calloc(CONFIG_SYS_MAX_FLASH_BANKS * CONFIG_SYS_MAX_FLASH_SECT, 1);
109 if (!saved_prot_info)
113 for (bank = 0; bank < CONFIG_SYS_MAX_FLASH_BANKS; ++bank) {
115 info = &flash_info[bank];
117 /* Nothing to do if the bank doesn't exist */
118 if (info->sector_count == 0)
121 /* Point to current bank protection information */
122 sp_info_ptr = saved_prot_info + (bank * CONFIG_SYS_MAX_FLASH_SECT);
125 * Adjust addr_first or addr_last if we are on bank boundary.
126 * Address space between banks must be continuous for other
127 * flash functions (like flash_sect_erase or flash_write) to
128 * succeed. Banks must also be numbered in correct order,
129 * according to increasing addresses.
131 if (addr_last > info->start[0] + info->size - 1)
132 addr_last = info->start[0] + info->size - 1;
133 if (addr_first < info->start[0])
134 addr_first = info->start[0];
136 for (i = 0; i < info->sector_count; i++) {
137 /* Save current information about protected sectors */
140 if ((s >= addr_first) && (s <= addr_last))
141 sp_info_ptr[i] = info->protect[i];
145 /* Protect/unprotect sectors */
146 if (sp_info_ptr[i] == 1) {
147 #if defined(CONFIG_SYS_FLASH_PROTECTION)
148 if (flash_real_protect(info, i, prot))
151 info->protect[i] = prot;
158 printf("%sProtected %d sectors\n",
159 prot ? "": "Un-", cnt);
163 if((prot == 1) && saved_prot_info)
164 free(saved_prot_info);
169 static int update_flash(ulong addr_source, ulong addr_first, ulong size)
171 ulong addr_last = addr_first + size - 1;
173 /* round last address to the sector boundary */
174 if (flash_sect_roundb(&addr_last) > 0)
177 if (addr_first >= addr_last) {
178 printf("Error: end address exceeds addressing space\n");
182 /* remove protection on processed sectors */
183 if (update_flash_protect(0, addr_first, addr_last) > 0) {
184 printf("Error: could not unprotect flash sectors\n");
188 printf("Erasing 0x%08lx - 0x%08lx", addr_first, addr_last);
189 if (flash_sect_erase(addr_first, addr_last) > 0) {
190 printf("Error: could not erase flash\n");
194 printf("Copying to flash...");
195 if (flash_write((char *)addr_source, addr_first, size) > 0) {
196 printf("Error: could not copy to flash\n");
201 /* enable protection on processed sectors */
202 if (update_flash_protect(1, addr_first, addr_last) > 0) {
203 printf("Error: could not protect flash sectors\n");
210 static int update_fit_getparams(const void *fit, int noffset, ulong *addr,
211 ulong *fladdr, ulong *size)
215 if (fit_image_get_data(fit, noffset, &data, (size_t *)size))
218 if (fit_image_get_load(fit, noffset, (ulong *)fladdr))
226 int update_tftp(ulong addr)
228 char *filename, *env_addr;
229 int images_noffset, ndepth, noffset;
230 ulong update_addr, update_fladdr, update_size;
234 /* use already present image */
236 goto got_update_file;
238 printf("Auto-update from TFTP: ");
240 /* get the file name of the update file */
241 filename = getenv(UPDATE_FILE_ENV);
242 if (filename == NULL) {
243 printf("failed, env. variable '%s' not found\n",
248 printf("trying update file '%s'\n", filename);
250 /* get load address of downloaded update file */
251 if ((env_addr = getenv("loadaddr")) != NULL)
252 addr = simple_strtoul(env_addr, NULL, 16);
254 addr = CONFIG_UPDATE_LOAD_ADDR;
257 if (update_load(filename, CONFIG_UPDATE_TFTP_MSEC_MAX,
258 CONFIG_UPDATE_TFTP_CNT_MAX, addr)) {
259 printf("Can't load update file, aborting auto-update\n");
266 if (!fit_check_format((void *)fit)) {
267 printf("Bad FIT format of the update file, aborting "
272 /* process updates */
273 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
276 noffset = fdt_next_node(fit, images_noffset, &ndepth);
277 while (noffset >= 0 && ndepth > 0) {
281 printf("Processing update '%s' :",
282 fit_get_name(fit, noffset, NULL));
284 if (!fit_image_verify(fit, noffset)) {
285 printf("Error: invalid update hash, aborting\n");
291 if (update_fit_getparams(fit, noffset, &update_addr,
292 &update_fladdr, &update_size)) {
293 printf("Error: can't get update parameteres, "
298 if (update_flash(update_addr, update_fladdr, update_size)) {
299 printf("Error: can't flash update, aborting\n");
304 noffset = fdt_next_node(fit, noffset, &ndepth);