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 TftpRRQTimeoutMSecs
;
43 extern int TftpRRQTimeoutCountMax
;
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
= TftpRRQTimeoutMSecs
;
59 saved_timeout_count
= TftpRRQTimeoutCountMax
;
60 saved_netretry
= strdup(getenv("netretry"));
61 saved_bootfile
= strdup(BootFile
);
63 /* set timeouts for auto-update */
64 TftpRRQTimeoutMSecs
= msec_max
;
65 TftpRRQTimeoutCountMax
= 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(BootFile
, filename
, sizeof(BootFile
));
73 size
= NetLoop(TFTPGET
);
78 flush_cache(addr
, size
);
80 /* restore changed globals and env variable */
81 TftpRRQTimeoutMSecs
= saved_timeout_msecs
;
82 TftpRRQTimeoutCountMax
= saved_timeout_count
;
84 setenv("netretry", saved_netretry
);
85 if (saved_netretry
!= NULL
)
88 if (saved_bootfile
!= NULL
) {
89 copy_filename(BootFile
, saved_bootfile
, sizeof(BootFile
));
96 static int update_flash_protect(int prot
, ulong addr_first
, ulong addr_last
)
107 calloc(CONFIG_SYS_MAX_FLASH_BANKS
* CONFIG_SYS_MAX_FLASH_SECT
, 1);
108 if (!saved_prot_info
)
112 for (bank
= 0; bank
< CONFIG_SYS_MAX_FLASH_BANKS
; ++bank
) {
114 info
= &flash_info
[bank
];
116 /* Nothing to do if the bank doesn't exist */
117 if (info
->sector_count
== 0)
120 /* Point to current bank protection information */
121 sp_info_ptr
= saved_prot_info
+ (bank
* CONFIG_SYS_MAX_FLASH_SECT
);
124 * Adjust addr_first or addr_last if we are on bank boundary.
125 * Address space between banks must be continuous for other
126 * flash functions (like flash_sect_erase or flash_write) to
127 * succeed. Banks must also be numbered in correct order,
128 * according to increasing addresses.
130 if (addr_last
> info
->start
[0] + info
->size
- 1)
131 addr_last
= info
->start
[0] + info
->size
- 1;
132 if (addr_first
< info
->start
[0])
133 addr_first
= info
->start
[0];
135 for (i
= 0; i
< info
->sector_count
; i
++) {
136 /* Save current information about protected sectors */
139 if ((s
>= addr_first
) && (s
<= addr_last
))
140 sp_info_ptr
[i
] = info
->protect
[i
];
144 /* Protect/unprotect sectors */
145 if (sp_info_ptr
[i
] == 1) {
146 #if defined(CONFIG_SYS_FLASH_PROTECTION)
147 if (flash_real_protect(info
, i
, prot
))
150 info
->protect
[i
] = prot
;
157 printf("%sProtected %d sectors\n",
158 prot
? "": "Un-", cnt
);
162 if((prot
== 1) && saved_prot_info
)
163 free(saved_prot_info
);
168 static int update_flash(ulong addr_source
, ulong addr_first
, ulong size
)
170 ulong addr_last
= addr_first
+ size
- 1;
172 /* round last address to the sector boundary */
173 if (flash_sect_roundb(&addr_last
) > 0)
176 if (addr_first
>= addr_last
) {
177 printf("Error: end address exceeds addressing space\n");
181 /* remove protection on processed sectors */
182 if (update_flash_protect(0, addr_first
, addr_last
) > 0) {
183 printf("Error: could not unprotect flash sectors\n");
187 printf("Erasing 0x%08lx - 0x%08lx", addr_first
, addr_last
);
188 if (flash_sect_erase(addr_first
, addr_last
) > 0) {
189 printf("Error: could not erase flash\n");
193 printf("Copying to flash...");
194 if (flash_write((char *)addr_source
, addr_first
, size
) > 0) {
195 printf("Error: could not copy to flash\n");
200 /* enable protection on processed sectors */
201 if (update_flash_protect(1, addr_first
, addr_last
) > 0) {
202 printf("Error: could not protect flash sectors\n");
209 static int update_fit_getparams(const void *fit
, int noffset
, ulong
*addr
,
210 ulong
*fladdr
, ulong
*size
)
214 if (fit_image_get_data(fit
, noffset
, &data
, (size_t *)size
))
217 if (fit_image_get_load(fit
, noffset
, (ulong
*)fladdr
))
225 int update_tftp(ulong addr
)
227 char *filename
, *env_addr
;
228 int images_noffset
, ndepth
, noffset
;
229 ulong update_addr
, update_fladdr
, update_size
;
233 /* use already present image */
235 goto got_update_file
;
237 printf("Auto-update from TFTP: ");
239 /* get the file name of the update file */
240 filename
= getenv(UPDATE_FILE_ENV
);
241 if (filename
== NULL
) {
242 printf("failed, env. variable '%s' not found\n",
247 printf("trying update file '%s'\n", filename
);
249 /* get load address of downloaded update file */
250 if ((env_addr
= getenv("loadaddr")) != NULL
)
251 addr
= simple_strtoul(env_addr
, NULL
, 16);
253 addr
= CONFIG_UPDATE_LOAD_ADDR
;
256 if (update_load(filename
, CONFIG_UPDATE_TFTP_MSEC_MAX
,
257 CONFIG_UPDATE_TFTP_CNT_MAX
, addr
)) {
258 printf("Can't load update file, aborting auto-update\n");
265 if (!fit_check_format((void *)fit
)) {
266 printf("Bad FIT format of the update file, aborting "
271 /* process updates */
272 images_noffset
= fdt_path_offset(fit
, FIT_IMAGES_PATH
);
275 noffset
= fdt_next_node(fit
, images_noffset
, &ndepth
);
276 while (noffset
>= 0 && ndepth
> 0) {
280 printf("Processing update '%s' :",
281 fit_get_name(fit
, noffset
, NULL
));
283 if (!fit_image_verify(fit
, noffset
)) {
284 printf("Error: invalid update hash, aborting\n");
290 if (update_fit_getparams(fit
, noffset
, &update_addr
,
291 &update_fladdr
, &update_size
)) {
292 printf("Error: can't get update parameteres, "
297 if (update_flash(update_addr
, update_fladdr
, update_size
)) {
298 printf("Error: can't flash update, aborting\n");
303 noffset
= fdt_next_node(fit
, noffset
, &ndepth
);