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"
30 #include <mtd/cfi_flash.h>
32 #if defined(CONFIG_DFU_TFTP) || defined(CONFIG_UPDATE_TFTP)
33 /* env variable holding the location of the update file */
34 #define UPDATE_FILE_ENV "updatefile"
36 /* set configuration defaults if needed */
37 #ifndef CONFIG_UPDATE_LOAD_ADDR
38 #define CONFIG_UPDATE_LOAD_ADDR 0x100000
41 #ifndef CONFIG_UPDATE_TFTP_MSEC_MAX
42 #define CONFIG_UPDATE_TFTP_MSEC_MAX 100
45 #ifndef CONFIG_UPDATE_TFTP_CNT_MAX
46 #define CONFIG_UPDATE_TFTP_CNT_MAX 0
49 extern ulong tftp_timeout_ms;
50 extern int tftp_timeout_count_max;
51 #ifdef CONFIG_MTD_NOR_FLASH
52 extern flash_info_t flash_info[];
53 static uchar *saved_prot_info;
55 static int update_load(char *filename, ulong msec_max, int cnt_max, ulong addr)
58 ulong saved_timeout_msecs;
59 int saved_timeout_count;
60 char *saved_netretry, *saved_bootfile;
63 /* save used globals and env variable */
64 saved_timeout_msecs = tftp_timeout_ms;
65 saved_timeout_count = tftp_timeout_count_max;
66 saved_netretry = strdup(env_get("netretry"));
67 saved_bootfile = strdup(net_boot_file_name);
69 /* set timeouts for auto-update */
70 tftp_timeout_ms = msec_max;
71 tftp_timeout_count_max = cnt_max;
73 /* we don't want to retry the connection if errors occur */
74 env_set("netretry", "no");
76 /* download the update file */
77 image_load_addr = addr;
78 copy_filename(net_boot_file_name, filename, sizeof(net_boot_file_name));
79 size = net_loop(TFTPGET);
84 flush_cache(addr, size);
86 /* restore changed globals and env variable */
87 tftp_timeout_ms = saved_timeout_msecs;
88 tftp_timeout_count_max = saved_timeout_count;
90 env_set("netretry", saved_netretry);
91 if (saved_netretry != NULL)
94 if (saved_bootfile != NULL) {
95 copy_filename(net_boot_file_name, saved_bootfile,
96 sizeof(net_boot_file_name));
103 #ifdef CONFIG_MTD_NOR_FLASH
104 static int update_flash_protect(int prot, ulong addr_first, ulong addr_last)
115 calloc(CONFIG_SYS_MAX_FLASH_BANKS * CONFIG_SYS_MAX_FLASH_SECT, 1);
116 if (!saved_prot_info)
120 for (bank = 0; bank < CONFIG_SYS_MAX_FLASH_BANKS; ++bank) {
122 info = &flash_info[bank];
124 /* Nothing to do if the bank doesn't exist */
125 if (info->sector_count == 0)
128 /* Point to current bank protection information */
129 sp_info_ptr = saved_prot_info + (bank * CONFIG_SYS_MAX_FLASH_SECT);
132 * Adjust addr_first or addr_last if we are on bank boundary.
133 * Address space between banks must be continuous for other
134 * flash functions (like flash_sect_erase or flash_write) to
135 * succeed. Banks must also be numbered in correct order,
136 * according to increasing addresses.
138 if (addr_last > info->start[0] + info->size - 1)
139 addr_last = info->start[0] + info->size - 1;
140 if (addr_first < info->start[0])
141 addr_first = info->start[0];
143 for (i = 0; i < info->sector_count; i++) {
144 /* Save current information about protected sectors */
147 if ((s >= addr_first) && (s <= addr_last))
148 sp_info_ptr[i] = info->protect[i];
152 /* Protect/unprotect sectors */
153 if (sp_info_ptr[i] == 1) {
154 #if defined(CONFIG_SYS_FLASH_PROTECTION)
155 if (flash_real_protect(info, i, prot))
158 info->protect[i] = prot;
165 printf("%sProtected %d sectors\n",
166 prot ? "": "Un-", cnt);
170 if((prot == 1) && saved_prot_info)
171 free(saved_prot_info);
177 static int update_flash(ulong addr_source, ulong addr_first, ulong size)
179 #ifdef CONFIG_MTD_NOR_FLASH
180 ulong addr_last = addr_first + size - 1;
182 /* round last address to the sector boundary */
183 if (flash_sect_roundb(&addr_last) > 0)
186 if (addr_first >= addr_last) {
187 printf("Error: end address exceeds addressing space\n");
191 /* remove protection on processed sectors */
192 if (update_flash_protect(0, addr_first, addr_last) > 0) {
193 printf("Error: could not unprotect flash sectors\n");
197 printf("Erasing 0x%08lx - 0x%08lx", addr_first, addr_last);
198 if (flash_sect_erase(addr_first, addr_last) > 0) {
199 printf("Error: could not erase flash\n");
203 printf("Copying to flash...");
204 if (flash_write((char *)addr_source, addr_first, size) > 0) {
205 printf("Error: could not copy to flash\n");
210 /* enable protection on processed sectors */
211 if (update_flash_protect(1, addr_first, addr_last) > 0) {
212 printf("Error: could not protect flash sectors\n");
218 #endif /* CONFIG_DFU_TFTP || CONFIG_UPDATE_TFTP */
220 static int update_fit_getparams(const void *fit, int noffset, ulong *addr,
221 ulong *fladdr, ulong *size)
225 if (fit_image_get_data(fit, noffset, &data, (size_t *)size))
228 if (fit_image_get_load(fit, noffset, (ulong *)fladdr))
236 #if defined(CONFIG_DFU_TFTP) || defined(CONFIG_UPDATE_TFTP)
237 int update_tftp(ulong addr, char *interface, char *devstring)
239 char *filename, *env_addr, *fit_image_name;
240 ulong update_addr, update_fladdr, update_size;
241 int images_noffset, ndepth, noffset;
242 bool update_tftp_dfu;
246 if (interface == NULL && devstring == NULL) {
247 update_tftp_dfu = false;
248 } else if (interface && devstring) {
249 update_tftp_dfu = true;
251 pr_err("Interface: %s and devstring: %s not supported!\n",
252 interface, devstring);
256 /* use already present image */
258 goto got_update_file;
260 printf("Auto-update from TFTP: ");
262 /* get the file name of the update file */
263 filename = env_get(UPDATE_FILE_ENV);
264 if (filename == NULL) {
265 printf("failed, env. variable '%s' not found\n",
270 printf("trying update file '%s'\n", filename);
272 /* get load address of downloaded update file */
273 env_addr = env_get("loadaddr");
275 addr = hextoul(env_addr, NULL);
277 addr = CONFIG_UPDATE_LOAD_ADDR;
280 if (update_load(filename, CONFIG_UPDATE_TFTP_MSEC_MAX,
281 CONFIG_UPDATE_TFTP_CNT_MAX, addr)) {
282 printf("Can't load update file, aborting auto-update\n");
287 fit = map_sysmem(addr, 0);
289 if (fit_check_format((void *)fit, IMAGE_SIZE_INVAL)) {
290 printf("Bad FIT format of the update file, aborting "
295 /* process updates */
296 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
299 noffset = fdt_next_node(fit, images_noffset, &ndepth);
300 while (noffset >= 0 && ndepth > 0) {
304 fit_image_name = (char *)fit_get_name(fit, noffset, NULL);
305 printf("Processing update '%s' :", fit_image_name);
307 if (!fit_image_verify(fit, noffset)) {
308 printf("Error: invalid update hash, aborting\n");
314 if (update_fit_getparams(fit, noffset, &update_addr,
315 &update_fladdr, &update_size)) {
316 printf("Error: can't get update parameters, aborting\n");
321 if (!update_tftp_dfu) {
322 if (update_flash(update_addr, update_fladdr,
324 printf("Error: can't flash update, aborting\n");
328 } else if (fit_image_check_type(fit, noffset,
330 ret = dfu_write_by_name(fit_image_name,
332 update_size, interface,
338 noffset = fdt_next_node(fit, noffset, &ndepth);
343 #endif /* CONFIG_DFU_UPDATE || CONFIG_UPDATE_TFTP */
345 #ifdef CONFIG_UPDATE_FIT
347 * fit_update - update storage with FIT image
348 * @fit: Pointer to FIT image
350 * Update firmware on storage using FIT image as input.
351 * The storage area to be update will be identified by the name
352 * in FIT and matching it to "dfu_alt_info" variable.
354 * Return: 0 - on success, non-zero - otherwise
356 int fit_update(const void *fit)
358 char *fit_image_name;
359 ulong update_addr, update_fladdr, update_size;
360 int images_noffset, ndepth, noffset;
366 if (fit_check_format((void *)fit, IMAGE_SIZE_INVAL)) {
367 printf("Bad FIT format of the update file, aborting auto-update\n");
371 /* process updates */
372 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
375 noffset = fdt_next_node(fit, images_noffset, &ndepth);
376 while (noffset >= 0 && ndepth > 0) {
380 fit_image_name = (char *)fit_get_name(fit, noffset, NULL);
381 printf("Processing update '%s' :", fit_image_name);
383 if (!fit_image_verify(fit, noffset)) {
384 printf("Error: invalid update hash, aborting\n");
390 if (update_fit_getparams(fit, noffset, &update_addr,
391 &update_fladdr, &update_size)) {
392 printf("Error: can't get update parameters, aborting\n");
397 if (fit_image_check_type(fit, noffset, IH_TYPE_FIRMWARE)) {
398 ret = dfu_write_by_name(fit_image_name,
400 update_size, NULL, NULL);
405 noffset = fdt_next_node(fit, noffset, &ndepth);
410 #endif /* CONFIG_UPDATE_FIT */