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>
12 #include <linux/printk.h>
23 #if defined(CONFIG_DFU_TFTP) || defined(CONFIG_UPDATE_TFTP)
24 /* env variable holding the location of the update file */
25 #define UPDATE_FILE_ENV "updatefile"
27 extern ulong tftp_timeout_ms;
28 extern int tftp_timeout_count_max;
29 #ifdef CONFIG_MTD_NOR_FLASH
31 #include <mtd/cfi_flash.h>
32 static uchar *saved_prot_info;
34 static int update_load(char *filename, ulong msec_max, int cnt_max, ulong addr)
37 ulong saved_timeout_msecs;
38 int saved_timeout_count;
39 char *saved_netretry, *saved_bootfile;
42 /* save used globals and env variable */
43 saved_timeout_msecs = tftp_timeout_ms;
44 saved_timeout_count = tftp_timeout_count_max;
45 saved_netretry = strdup(env_get("netretry"));
46 saved_bootfile = strdup(net_boot_file_name);
48 /* set timeouts for auto-update */
49 tftp_timeout_ms = msec_max;
50 tftp_timeout_count_max = cnt_max;
52 /* we don't want to retry the connection if errors occur */
53 env_set("netretry", "no");
55 /* download the update file */
56 image_load_addr = addr;
57 copy_filename(net_boot_file_name, filename, sizeof(net_boot_file_name));
58 size = net_loop(TFTPGET);
63 flush_cache(addr, size);
65 /* restore changed globals and env variable */
66 tftp_timeout_ms = saved_timeout_msecs;
67 tftp_timeout_count_max = saved_timeout_count;
69 env_set("netretry", saved_netretry);
70 if (saved_netretry != NULL)
73 if (saved_bootfile != NULL) {
74 copy_filename(net_boot_file_name, saved_bootfile,
75 sizeof(net_boot_file_name));
82 #ifdef CONFIG_MTD_NOR_FLASH
83 static int update_flash_protect(int prot, ulong addr_first, ulong addr_last)
94 calloc(CFI_FLASH_BANKS * CONFIG_SYS_MAX_FLASH_SECT, 1);
99 for (bank = 0; bank < CFI_FLASH_BANKS; ++bank) {
101 info = &flash_info[bank];
103 /* Nothing to do if the bank doesn't exist */
104 if (info->sector_count == 0)
107 /* Point to current bank protection information */
108 sp_info_ptr = saved_prot_info + (bank * CONFIG_SYS_MAX_FLASH_SECT);
111 * Adjust addr_first or addr_last if we are on bank boundary.
112 * Address space between banks must be continuous for other
113 * flash functions (like flash_sect_erase or flash_write) to
114 * succeed. Banks must also be numbered in correct order,
115 * according to increasing addresses.
117 if (addr_last > info->start[0] + info->size - 1)
118 addr_last = info->start[0] + info->size - 1;
119 if (addr_first < info->start[0])
120 addr_first = info->start[0];
122 for (i = 0; i < info->sector_count; i++) {
123 /* Save current information about protected sectors */
126 if ((s >= addr_first) && (s <= addr_last))
127 sp_info_ptr[i] = info->protect[i];
131 /* Protect/unprotect sectors */
132 if (sp_info_ptr[i] == 1) {
133 #if defined(CONFIG_SYS_FLASH_PROTECTION)
134 if (flash_real_protect(info, i, prot))
137 info->protect[i] = prot;
144 printf("%sProtected %d sectors\n",
145 prot ? "": "Un-", cnt);
149 if((prot == 1) && saved_prot_info)
150 free(saved_prot_info);
156 static int update_flash(ulong addr_source, ulong addr_first, ulong size)
158 #ifdef CONFIG_MTD_NOR_FLASH
159 ulong addr_last = addr_first + size - 1;
161 /* round last address to the sector boundary */
162 if (flash_sect_roundb(&addr_last) > 0)
165 if (addr_first >= addr_last) {
166 printf("Error: end address exceeds addressing space\n");
170 /* remove protection on processed sectors */
171 if (update_flash_protect(0, addr_first, addr_last) > 0) {
172 printf("Error: could not unprotect flash sectors\n");
176 printf("Erasing 0x%08lx - 0x%08lx", addr_first, addr_last);
177 if (flash_sect_erase(addr_first, addr_last) > 0) {
178 printf("Error: could not erase flash\n");
182 printf("Copying to flash...");
183 if (flash_write((char *)addr_source, addr_first, size) > 0) {
184 printf("Error: could not copy to flash\n");
189 /* enable protection on processed sectors */
190 if (update_flash_protect(1, addr_first, addr_last) > 0) {
191 printf("Error: could not protect flash sectors\n");
197 #endif /* CONFIG_DFU_TFTP || CONFIG_UPDATE_TFTP */
199 static int update_fit_getparams(const void *fit, int noffset, ulong *addr,
200 ulong *fladdr, ulong *size)
204 if (fit_image_get_data(fit, noffset, &data, (size_t *)size))
207 if (fit_image_get_load(fit, noffset, (ulong *)fladdr))
215 #if defined(CONFIG_DFU_TFTP) || defined(CONFIG_UPDATE_TFTP)
216 int update_tftp(ulong addr, char *interface, char *devstring)
218 char *filename, *env_addr, *fit_image_name;
219 ulong update_addr, update_fladdr, update_size;
220 int images_noffset, ndepth, noffset;
221 bool update_tftp_dfu;
225 if (interface == NULL && devstring == NULL) {
226 update_tftp_dfu = false;
227 } else if (interface && devstring) {
228 update_tftp_dfu = true;
230 pr_err("Interface: %s and devstring: %s not supported!\n",
231 interface, devstring);
235 /* use already present image */
237 goto got_update_file;
239 printf("Auto-update from TFTP: ");
241 /* get the file name of the update file */
242 filename = env_get(UPDATE_FILE_ENV);
243 if (filename == NULL) {
244 printf("failed, env. variable '%s' not found\n",
249 printf("trying update file '%s'\n", filename);
251 /* get load address of downloaded update file */
252 env_addr = env_get("loadaddr");
254 addr = hextoul(env_addr, NULL);
256 addr = CONFIG_UPDATE_LOAD_ADDR;
259 if (update_load(filename, CONFIG_UPDATE_TFTP_MSEC_MAX,
260 CONFIG_UPDATE_TFTP_CNT_MAX, addr)) {
261 printf("Can't load update file, aborting auto-update\n");
266 fit = map_sysmem(addr, 0);
268 if (fit_check_format((void *)fit, IMAGE_SIZE_INVAL)) {
269 printf("Bad FIT format of the update file, aborting "
274 /* process updates */
275 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
278 noffset = fdt_next_node(fit, images_noffset, &ndepth);
279 while (noffset >= 0 && ndepth > 0) {
283 fit_image_name = (char *)fit_get_name(fit, noffset, NULL);
284 printf("Processing update '%s' :", fit_image_name);
286 if (!fit_image_verify(fit, noffset)) {
287 printf("Error: invalid update hash, aborting\n");
293 if (update_fit_getparams(fit, noffset, &update_addr,
294 &update_fladdr, &update_size)) {
295 printf("Error: can't get update parameters, aborting\n");
300 if (!update_tftp_dfu) {
301 if (update_flash(update_addr, update_fladdr,
303 printf("Error: can't flash update, aborting\n");
307 } else if (fit_image_check_type(fit, noffset,
309 ret = dfu_write_by_name(fit_image_name,
311 update_size, interface,
317 noffset = fdt_next_node(fit, noffset, &ndepth);
322 #endif /* CONFIG_DFU_UPDATE || CONFIG_UPDATE_TFTP */
324 #ifdef CONFIG_UPDATE_FIT
326 * fit_update - update storage with FIT image
327 * @fit: Pointer to FIT image
329 * Update firmware on storage using FIT image as input.
330 * The storage area to be update will be identified by the name
331 * in FIT and matching it to "dfu_alt_info" variable.
333 * Return: 0 - on success, non-zero - otherwise
335 int fit_update(const void *fit)
337 char *fit_image_name;
338 ulong update_addr, update_fladdr, update_size;
339 int images_noffset, ndepth, noffset;
345 if (fit_check_format((void *)fit, IMAGE_SIZE_INVAL)) {
346 printf("Bad FIT format of the update file, aborting auto-update\n");
350 /* process updates */
351 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
354 noffset = fdt_next_node(fit, images_noffset, &ndepth);
355 while (noffset >= 0 && ndepth > 0) {
359 fit_image_name = (char *)fit_get_name(fit, noffset, NULL);
360 printf("Processing update '%s' :", fit_image_name);
362 if (!fit_image_verify(fit, noffset)) {
363 printf("Error: invalid update hash, aborting\n");
369 if (update_fit_getparams(fit, noffset, &update_addr,
370 &update_fladdr, &update_size)) {
371 printf("Error: can't get update parameters, aborting\n");
376 if (fit_image_check_type(fit, noffset, IH_TYPE_FIRMWARE)) {
377 ret = dfu_write_by_name(fit_image_name,
379 update_size, NULL, NULL);
384 noffset = fdt_next_node(fit, noffset, &ndepth);
389 #endif /* CONFIG_UPDATE_FIT */