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_UPDATE_TFTP) && !defined(CONFIG_MTD_NOR_FLASH)
17 #error "CONFIG_UPDATE_TFTP and !CONFIG_MTD_NOR_FLASH needed for legacy behaviour"
27 #include <mtd/cfi_flash.h>
29 /* env variable holding the location of the update file */
30 #define UPDATE_FILE_ENV "updatefile"
32 /* set configuration defaults if needed */
33 #ifndef CONFIG_UPDATE_LOAD_ADDR
34 #define CONFIG_UPDATE_LOAD_ADDR 0x100000
37 #ifndef CONFIG_UPDATE_TFTP_MSEC_MAX
38 #define CONFIG_UPDATE_TFTP_MSEC_MAX 100
41 #ifndef CONFIG_UPDATE_TFTP_CNT_MAX
42 #define CONFIG_UPDATE_TFTP_CNT_MAX 0
45 extern ulong tftp_timeout_ms
;
46 extern int tftp_timeout_count_max
;
47 extern ulong load_addr
;
48 #ifdef CONFIG_MTD_NOR_FLASH
49 extern flash_info_t flash_info
[];
50 static uchar
*saved_prot_info
;
52 static int update_load(char *filename
, ulong msec_max
, int cnt_max
, ulong addr
)
55 ulong saved_timeout_msecs
;
56 int saved_timeout_count
;
57 char *saved_netretry
, *saved_bootfile
;
60 /* save used globals and env variable */
61 saved_timeout_msecs
= tftp_timeout_ms
;
62 saved_timeout_count
= tftp_timeout_count_max
;
63 saved_netretry
= strdup(env_get("netretry"));
64 saved_bootfile
= strdup(net_boot_file_name
);
66 /* set timeouts for auto-update */
67 tftp_timeout_ms
= msec_max
;
68 tftp_timeout_count_max
= cnt_max
;
70 /* we don't want to retry the connection if errors occur */
71 env_set("netretry", "no");
73 /* download the update file */
75 copy_filename(net_boot_file_name
, filename
, sizeof(net_boot_file_name
));
76 size
= net_loop(TFTPGET
);
81 flush_cache(addr
, size
);
83 /* restore changed globals and env variable */
84 tftp_timeout_ms
= saved_timeout_msecs
;
85 tftp_timeout_count_max
= saved_timeout_count
;
87 env_set("netretry", saved_netretry
);
88 if (saved_netretry
!= NULL
)
91 if (saved_bootfile
!= NULL
) {
92 copy_filename(net_boot_file_name
, saved_bootfile
,
93 sizeof(net_boot_file_name
));
100 #ifdef CONFIG_MTD_NOR_FLASH
101 static int update_flash_protect(int prot
, ulong addr_first
, ulong addr_last
)
112 calloc(CONFIG_SYS_MAX_FLASH_BANKS
* CONFIG_SYS_MAX_FLASH_SECT
, 1);
113 if (!saved_prot_info
)
117 for (bank
= 0; bank
< CONFIG_SYS_MAX_FLASH_BANKS
; ++bank
) {
119 info
= &flash_info
[bank
];
121 /* Nothing to do if the bank doesn't exist */
122 if (info
->sector_count
== 0)
125 /* Point to current bank protection information */
126 sp_info_ptr
= saved_prot_info
+ (bank
* CONFIG_SYS_MAX_FLASH_SECT
);
129 * Adjust addr_first or addr_last if we are on bank boundary.
130 * Address space between banks must be continuous for other
131 * flash functions (like flash_sect_erase or flash_write) to
132 * succeed. Banks must also be numbered in correct order,
133 * according to increasing addresses.
135 if (addr_last
> info
->start
[0] + info
->size
- 1)
136 addr_last
= info
->start
[0] + info
->size
- 1;
137 if (addr_first
< info
->start
[0])
138 addr_first
= info
->start
[0];
140 for (i
= 0; i
< info
->sector_count
; i
++) {
141 /* Save current information about protected sectors */
144 if ((s
>= addr_first
) && (s
<= addr_last
))
145 sp_info_ptr
[i
] = info
->protect
[i
];
149 /* Protect/unprotect sectors */
150 if (sp_info_ptr
[i
] == 1) {
151 #if defined(CONFIG_SYS_FLASH_PROTECTION)
152 if (flash_real_protect(info
, i
, prot
))
155 info
->protect
[i
] = prot
;
162 printf("%sProtected %d sectors\n",
163 prot
? "": "Un-", cnt
);
167 if((prot
== 1) && saved_prot_info
)
168 free(saved_prot_info
);
174 static int update_flash(ulong addr_source
, ulong addr_first
, ulong size
)
176 #ifdef CONFIG_MTD_NOR_FLASH
177 ulong addr_last
= addr_first
+ size
- 1;
179 /* round last address to the sector boundary */
180 if (flash_sect_roundb(&addr_last
) > 0)
183 if (addr_first
>= addr_last
) {
184 printf("Error: end address exceeds addressing space\n");
188 /* remove protection on processed sectors */
189 if (update_flash_protect(0, addr_first
, addr_last
) > 0) {
190 printf("Error: could not unprotect flash sectors\n");
194 printf("Erasing 0x%08lx - 0x%08lx", addr_first
, addr_last
);
195 if (flash_sect_erase(addr_first
, addr_last
) > 0) {
196 printf("Error: could not erase flash\n");
200 printf("Copying to flash...");
201 if (flash_write((char *)addr_source
, addr_first
, size
) > 0) {
202 printf("Error: could not copy to flash\n");
207 /* enable protection on processed sectors */
208 if (update_flash_protect(1, addr_first
, addr_last
) > 0) {
209 printf("Error: could not protect flash sectors\n");
216 static int update_fit_getparams(const void *fit
, int noffset
, ulong
*addr
,
217 ulong
*fladdr
, ulong
*size
)
221 if (fit_image_get_data(fit
, noffset
, &data
, (size_t *)size
))
224 if (fit_image_get_load(fit
, noffset
, (ulong
*)fladdr
))
232 int update_tftp(ulong addr
, char *interface
, char *devstring
)
234 char *filename
, *env_addr
, *fit_image_name
;
235 ulong update_addr
, update_fladdr
, update_size
;
236 int images_noffset
, ndepth
, noffset
;
237 bool update_tftp_dfu
;
241 if (interface
== NULL
&& devstring
== NULL
) {
242 update_tftp_dfu
= false;
243 } else if (interface
&& devstring
) {
244 update_tftp_dfu
= true;
246 pr_err("Interface: %s and devstring: %s not supported!\n",
247 interface
, devstring
);
251 /* use already present image */
253 goto got_update_file
;
255 printf("Auto-update from TFTP: ");
257 /* get the file name of the update file */
258 filename
= env_get(UPDATE_FILE_ENV
);
259 if (filename
== NULL
) {
260 printf("failed, env. variable '%s' not found\n",
265 printf("trying update file '%s'\n", filename
);
267 /* get load address of downloaded update file */
268 env_addr
= env_get("loadaddr");
270 addr
= simple_strtoul(env_addr
, NULL
, 16);
272 addr
= CONFIG_UPDATE_LOAD_ADDR
;
275 if (update_load(filename
, CONFIG_UPDATE_TFTP_MSEC_MAX
,
276 CONFIG_UPDATE_TFTP_CNT_MAX
, addr
)) {
277 printf("Can't load update file, aborting auto-update\n");
284 if (!fit_check_format((void *)fit
)) {
285 printf("Bad FIT format of the update file, aborting "
290 /* process updates */
291 images_noffset
= fdt_path_offset(fit
, FIT_IMAGES_PATH
);
294 noffset
= fdt_next_node(fit
, images_noffset
, &ndepth
);
295 while (noffset
>= 0 && ndepth
> 0) {
299 fit_image_name
= (char *)fit_get_name(fit
, noffset
, NULL
);
300 printf("Processing update '%s' :", fit_image_name
);
302 if (!fit_image_verify(fit
, noffset
)) {
303 printf("Error: invalid update hash, aborting\n");
309 if (update_fit_getparams(fit
, noffset
, &update_addr
,
310 &update_fladdr
, &update_size
)) {
311 printf("Error: can't get update parameteres, "
317 if (!update_tftp_dfu
) {
318 if (update_flash(update_addr
, update_fladdr
,
320 printf("Error: can't flash update, aborting\n");
324 } else if (fit_image_check_type(fit
, noffset
,
326 ret
= dfu_tftp_write(fit_image_name
, update_addr
,
327 update_size
, interface
, devstring
);
332 noffset
= fdt_next_node(fit
, noffset
, &ndepth
);