2 * (C) Copyright 2008 Semihalf
4 * Written by: Rafal Czubak <rcz@semihalf.com>
5 * Bartlomiej Sieka <tur@semihalf.com>
7 * See file CREDITS for list of people who contributed to this
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of
13 * the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
29 #if !(defined(CONFIG_FIT) && defined(CONFIG_OF_LIBFDT))
30 #error "CONFIG_FIT and CONFIG_OF_LIBFDT are required for auto-update feature"
33 #if defined(CONFIG_SYS_NO_FLASH)
34 #error "CONFIG_SYS_NO_FLASH defined, but FLASH is required for auto-update feature"
43 /* env variable holding the location of the update file */
44 #define UPDATE_FILE_ENV "updatefile"
46 /* set configuration defaults if needed */
47 #ifndef CONFIG_UPDATE_LOAD_ADDR
48 #define CONFIG_UPDATE_LOAD_ADDR 0x100000
51 #ifndef CONFIG_UPDATE_TFTP_MSEC_MAX
52 #define CONFIG_UPDATE_TFTP_MSEC_MAX 100
55 #ifndef CONFIG_UPDATE_TFTP_CNT_MAX
56 #define CONFIG_UPDATE_TFTP_CNT_MAX 0
59 extern ulong TftpRRQTimeoutMSecs
;
60 extern int TftpRRQTimeoutCountMax
;
61 extern flash_info_t flash_info
[];
62 extern ulong load_addr
;
64 static uchar
*saved_prot_info
;
66 static int update_load(char *filename
, ulong msec_max
, int cnt_max
, ulong addr
)
69 ulong saved_timeout_msecs
;
70 int saved_timeout_count
;
71 char *saved_netretry
, *saved_bootfile
;
74 /* save used globals and env variable */
75 saved_timeout_msecs
= TftpRRQTimeoutMSecs
;
76 saved_timeout_count
= TftpRRQTimeoutCountMax
;
77 saved_netretry
= strdup(getenv("netretry"));
78 saved_bootfile
= strdup(BootFile
);
80 /* set timeouts for auto-update */
81 TftpRRQTimeoutMSecs
= msec_max
;
82 TftpRRQTimeoutCountMax
= cnt_max
;
84 /* we don't want to retry the connection if errors occur */
85 setenv("netretry", "no");
87 /* download the update file */
89 copy_filename(BootFile
, filename
, sizeof(BootFile
));
90 size
= NetLoop(TFTPGET
);
95 flush_cache(addr
, size
);
97 /* restore changed globals and env variable */
98 TftpRRQTimeoutMSecs
= saved_timeout_msecs
;
99 TftpRRQTimeoutCountMax
= saved_timeout_count
;
101 setenv("netretry", saved_netretry
);
102 if (saved_netretry
!= NULL
)
103 free(saved_netretry
);
105 if (saved_bootfile
!= NULL
) {
106 copy_filename(BootFile
, saved_bootfile
, sizeof(BootFile
));
107 free(saved_bootfile
);
113 static int update_flash_protect(int prot
, ulong addr_first
, ulong addr_last
)
124 calloc(CONFIG_SYS_MAX_FLASH_BANKS
* CONFIG_SYS_MAX_FLASH_SECT
, 1);
125 if (!saved_prot_info
)
129 for (bank
= 0; bank
< CONFIG_SYS_MAX_FLASH_BANKS
; ++bank
) {
131 info
= &flash_info
[bank
];
133 /* Nothing to do if the bank doesn't exist */
134 if (info
->sector_count
== 0)
137 /* Point to current bank protection information */
138 sp_info_ptr
= saved_prot_info
+ (bank
* CONFIG_SYS_MAX_FLASH_SECT
);
141 * Adjust addr_first or addr_last if we are on bank boundary.
142 * Address space between banks must be continuous for other
143 * flash functions (like flash_sect_erase or flash_write) to
144 * succeed. Banks must also be numbered in correct order,
145 * according to increasing addresses.
147 if (addr_last
> info
->start
[0] + info
->size
- 1)
148 addr_last
= info
->start
[0] + info
->size
- 1;
149 if (addr_first
< info
->start
[0])
150 addr_first
= info
->start
[0];
152 for (i
= 0; i
< info
->sector_count
; i
++) {
153 /* Save current information about protected sectors */
156 if ((s
>= addr_first
) && (s
<= addr_last
))
157 sp_info_ptr
[i
] = info
->protect
[i
];
161 /* Protect/unprotect sectors */
162 if (sp_info_ptr
[i
] == 1) {
163 #if defined(CONFIG_SYS_FLASH_PROTECTION)
164 if (flash_real_protect(info
, i
, prot
))
167 info
->protect
[i
] = prot
;
174 printf("%sProtected %d sectors\n",
175 prot
? "": "Un-", cnt
);
179 if((prot
== 1) && saved_prot_info
)
180 free(saved_prot_info
);
185 static int update_flash(ulong addr_source
, ulong addr_first
, ulong size
)
187 ulong addr_last
= addr_first
+ size
- 1;
189 /* round last address to the sector boundary */
190 if (flash_sect_roundb(&addr_last
) > 0)
193 if (addr_first
>= addr_last
) {
194 printf("Error: end address exceeds addressing space\n");
198 /* remove protection on processed sectors */
199 if (update_flash_protect(0, addr_first
, addr_last
) > 0) {
200 printf("Error: could not unprotect flash sectors\n");
204 printf("Erasing 0x%08lx - 0x%08lx", addr_first
, addr_last
);
205 if (flash_sect_erase(addr_first
, addr_last
) > 0) {
206 printf("Error: could not erase flash\n");
210 printf("Copying to flash...");
211 if (flash_write((char *)addr_source
, addr_first
, size
) > 0) {
212 printf("Error: could not copy to flash\n");
217 /* enable protection on processed sectors */
218 if (update_flash_protect(1, addr_first
, addr_last
) > 0) {
219 printf("Error: could not protect flash sectors\n");
226 static int update_fit_getparams(const void *fit
, int noffset
, ulong
*addr
,
227 ulong
*fladdr
, ulong
*size
)
231 if (fit_image_get_data(fit
, noffset
, &data
, (size_t *)size
))
234 if (fit_image_get_load(fit
, noffset
, (ulong
*)fladdr
))
242 int update_tftp(ulong addr
)
244 char *filename
, *env_addr
;
245 int images_noffset
, ndepth
, noffset
;
246 ulong update_addr
, update_fladdr
, update_size
;
250 /* use already present image */
252 goto got_update_file
;
254 printf("Auto-update from TFTP: ");
256 /* get the file name of the update file */
257 filename
= getenv(UPDATE_FILE_ENV
);
258 if (filename
== NULL
) {
259 printf("failed, env. variable '%s' not found\n",
264 printf("trying update file '%s'\n", filename
);
266 /* get load address of downloaded update file */
267 if ((env_addr
= getenv("loadaddr")) != NULL
)
268 addr
= simple_strtoul(env_addr
, NULL
, 16);
270 addr
= CONFIG_UPDATE_LOAD_ADDR
;
273 if (update_load(filename
, CONFIG_UPDATE_TFTP_MSEC_MAX
,
274 CONFIG_UPDATE_TFTP_CNT_MAX
, addr
)) {
275 printf("Can't load update file, aborting auto-update\n");
282 if (!fit_check_format((void *)fit
)) {
283 printf("Bad FIT format of the update file, aborting "
288 /* process updates */
289 images_noffset
= fdt_path_offset(fit
, FIT_IMAGES_PATH
);
292 noffset
= fdt_next_node(fit
, images_noffset
, &ndepth
);
293 while (noffset
>= 0 && ndepth
> 0) {
297 printf("Processing update '%s' :",
298 fit_get_name(fit
, noffset
, NULL
));
300 if (!fit_image_verify(fit
, noffset
)) {
301 printf("Error: invalid update hash, aborting\n");
307 if (update_fit_getparams(fit
, noffset
, &update_addr
,
308 &update_fladdr
, &update_size
)) {
309 printf("Error: can't get update parameteres, "
314 if (update_flash(update_addr
, update_fladdr
, update_size
)) {
315 printf("Error: can't flash update, aborting\n");
320 noffset
= fdt_next_node(fit
, noffset
, &ndepth
);