1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2008 Semihalf
5 * Written by: Rafal Czubak <rcz@semihalf.com>
6 * Bartlomiej Sieka <tur@semihalf.com>
13 #if !(defined(CONFIG_FIT) && defined(CONFIG_OF_LIBFDT))
14 #error "CONFIG_FIT and CONFIG_OF_LIBFDT are required for auto-update feature"
17 #if defined(CONFIG_UPDATE_TFTP) && !defined(CONFIG_MTD_NOR_FLASH)
18 #error "CONFIG_UPDATE_TFTP and !CONFIG_MTD_NOR_FLASH needed for legacy behaviour"
29 #include <mtd/cfi_flash.h>
31 /* env variable holding the location of the update file */
32 #define UPDATE_FILE_ENV "updatefile"
34 /* set configuration defaults if needed */
35 #ifndef CONFIG_UPDATE_LOAD_ADDR
36 #define CONFIG_UPDATE_LOAD_ADDR 0x100000
39 #ifndef CONFIG_UPDATE_TFTP_MSEC_MAX
40 #define CONFIG_UPDATE_TFTP_MSEC_MAX 100
43 #ifndef CONFIG_UPDATE_TFTP_CNT_MAX
44 #define CONFIG_UPDATE_TFTP_CNT_MAX 0
47 extern ulong tftp_timeout_ms;
48 extern int tftp_timeout_count_max;
49 #ifdef CONFIG_MTD_NOR_FLASH
50 extern flash_info_t flash_info[];
51 static uchar *saved_prot_info;
53 static int update_load(char *filename, ulong msec_max, int cnt_max, ulong addr)
56 ulong saved_timeout_msecs;
57 int saved_timeout_count;
58 char *saved_netretry, *saved_bootfile;
61 /* save used globals and env variable */
62 saved_timeout_msecs = tftp_timeout_ms;
63 saved_timeout_count = tftp_timeout_count_max;
64 saved_netretry = strdup(env_get("netretry"));
65 saved_bootfile = strdup(net_boot_file_name);
67 /* set timeouts for auto-update */
68 tftp_timeout_ms = msec_max;
69 tftp_timeout_count_max = cnt_max;
71 /* we don't want to retry the connection if errors occur */
72 env_set("netretry", "no");
74 /* download the update file */
75 image_load_addr = addr;
76 copy_filename(net_boot_file_name, filename, sizeof(net_boot_file_name));
77 size = net_loop(TFTPGET);
82 flush_cache(addr, size);
84 /* restore changed globals and env variable */
85 tftp_timeout_ms = saved_timeout_msecs;
86 tftp_timeout_count_max = saved_timeout_count;
88 env_set("netretry", saved_netretry);
89 if (saved_netretry != NULL)
92 if (saved_bootfile != NULL) {
93 copy_filename(net_boot_file_name, saved_bootfile,
94 sizeof(net_boot_file_name));
101 #ifdef CONFIG_MTD_NOR_FLASH
102 static int update_flash_protect(int prot, ulong addr_first, ulong addr_last)
113 calloc(CONFIG_SYS_MAX_FLASH_BANKS * CONFIG_SYS_MAX_FLASH_SECT, 1);
114 if (!saved_prot_info)
118 for (bank = 0; bank < CONFIG_SYS_MAX_FLASH_BANKS; ++bank) {
120 info = &flash_info[bank];
122 /* Nothing to do if the bank doesn't exist */
123 if (info->sector_count == 0)
126 /* Point to current bank protection information */
127 sp_info_ptr = saved_prot_info + (bank * CONFIG_SYS_MAX_FLASH_SECT);
130 * Adjust addr_first or addr_last if we are on bank boundary.
131 * Address space between banks must be continuous for other
132 * flash functions (like flash_sect_erase or flash_write) to
133 * succeed. Banks must also be numbered in correct order,
134 * according to increasing addresses.
136 if (addr_last > info->start[0] + info->size - 1)
137 addr_last = info->start[0] + info->size - 1;
138 if (addr_first < info->start[0])
139 addr_first = info->start[0];
141 for (i = 0; i < info->sector_count; i++) {
142 /* Save current information about protected sectors */
145 if ((s >= addr_first) && (s <= addr_last))
146 sp_info_ptr[i] = info->protect[i];
150 /* Protect/unprotect sectors */
151 if (sp_info_ptr[i] == 1) {
152 #if defined(CONFIG_SYS_FLASH_PROTECTION)
153 if (flash_real_protect(info, i, prot))
156 info->protect[i] = prot;
163 printf("%sProtected %d sectors\n",
164 prot ? "": "Un-", cnt);
168 if((prot == 1) && saved_prot_info)
169 free(saved_prot_info);
175 static int update_flash(ulong addr_source, ulong addr_first, ulong size)
177 #ifdef CONFIG_MTD_NOR_FLASH
178 ulong addr_last = addr_first + size - 1;
180 /* round last address to the sector boundary */
181 if (flash_sect_roundb(&addr_last) > 0)
184 if (addr_first >= addr_last) {
185 printf("Error: end address exceeds addressing space\n");
189 /* remove protection on processed sectors */
190 if (update_flash_protect(0, addr_first, addr_last) > 0) {
191 printf("Error: could not unprotect flash sectors\n");
195 printf("Erasing 0x%08lx - 0x%08lx", addr_first, addr_last);
196 if (flash_sect_erase(addr_first, addr_last) > 0) {
197 printf("Error: could not erase flash\n");
201 printf("Copying to flash...");
202 if (flash_write((char *)addr_source, addr_first, size) > 0) {
203 printf("Error: could not copy to flash\n");
208 /* enable protection on processed sectors */
209 if (update_flash_protect(1, addr_first, addr_last) > 0) {
210 printf("Error: could not protect flash sectors\n");
217 static int update_fit_getparams(const void *fit, int noffset, ulong *addr,
218 ulong *fladdr, ulong *size)
222 if (fit_image_get_data(fit, noffset, &data, (size_t *)size))
225 if (fit_image_get_load(fit, noffset, (ulong *)fladdr))
233 int update_tftp(ulong addr, char *interface, char *devstring)
235 char *filename, *env_addr, *fit_image_name;
236 ulong update_addr, update_fladdr, update_size;
237 int images_noffset, ndepth, noffset;
238 bool update_tftp_dfu;
242 if (interface == NULL && devstring == NULL) {
243 update_tftp_dfu = false;
244 } else if (interface && devstring) {
245 update_tftp_dfu = true;
247 pr_err("Interface: %s and devstring: %s not supported!\n",
248 interface, devstring);
252 /* use already present image */
254 goto got_update_file;
256 printf("Auto-update from TFTP: ");
258 /* get the file name of the update file */
259 filename = env_get(UPDATE_FILE_ENV);
260 if (filename == NULL) {
261 printf("failed, env. variable '%s' not found\n",
266 printf("trying update file '%s'\n", filename);
268 /* get load address of downloaded update file */
269 env_addr = env_get("loadaddr");
271 addr = simple_strtoul(env_addr, NULL, 16);
273 addr = CONFIG_UPDATE_LOAD_ADDR;
276 if (update_load(filename, CONFIG_UPDATE_TFTP_MSEC_MAX,
277 CONFIG_UPDATE_TFTP_CNT_MAX, addr)) {
278 printf("Can't load update file, aborting auto-update\n");
285 if (!fit_check_format((void *)fit)) {
286 printf("Bad FIT format of the update file, aborting "
291 /* process updates */
292 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
295 noffset = fdt_next_node(fit, images_noffset, &ndepth);
296 while (noffset >= 0 && ndepth > 0) {
300 fit_image_name = (char *)fit_get_name(fit, noffset, NULL);
301 printf("Processing update '%s' :", fit_image_name);
303 if (!fit_image_verify(fit, noffset)) {
304 printf("Error: invalid update hash, aborting\n");
310 if (update_fit_getparams(fit, noffset, &update_addr,
311 &update_fladdr, &update_size)) {
312 printf("Error: can't get update parameteres, "
318 if (!update_tftp_dfu) {
319 if (update_flash(update_addr, update_fladdr,
321 printf("Error: can't flash update, aborting\n");
325 } else if (fit_image_check_type(fit, noffset,
327 ret = dfu_tftp_write(fit_image_name, update_addr,
328 update_size, interface, devstring);
333 noffset = fdt_next_node(fit, noffset, &ndepth);