blob: 94d6a82aeb1b19478c07706419334815ac15d8e5 [file] [log] [blame]
Bartlomiej Sieka4bae9092008-10-01 15:26:31 +02001/*
2 * (C) Copyright 2008 Semihalf
3 *
4 * Written by: Rafal Czubak <rcz@semihalf.com>
5 * Bartlomiej Sieka <tur@semihalf.com>
6 *
7 * See file CREDITS for list of people who contributed to this
8 * project.
9 *
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.
14 *
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.
19 *
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,
23 * MA 02111-1307 USA
24 *
25 */
26
27#include <common.h>
28
29#if !(defined(CONFIG_FIT) && defined(CONFIG_OF_LIBFDT))
30#error "CONFIG_FIT and CONFIG_OF_LIBFDT are required for auto-update feature"
31#endif
32
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020033#if defined(CONFIG_SYS_NO_FLASH)
34#error "CONFIG_SYS_NO_FLASH defined, but FLASH is required for auto-update feature"
Bartlomiej Sieka4bae9092008-10-01 15:26:31 +020035#endif
36
37#include <command.h>
38#include <flash.h>
39#include <net.h>
Kim Phillips06370592012-10-29 13:34:33 +000040#include <net/tftp.h>
Bartlomiej Sieka4bae9092008-10-01 15:26:31 +020041#include <malloc.h>
42
43/* env variable holding the location of the update file */
44#define UPDATE_FILE_ENV "updatefile"
45
46/* set configuration defaults if needed */
47#ifndef CONFIG_UPDATE_LOAD_ADDR
48#define CONFIG_UPDATE_LOAD_ADDR 0x100000
49#endif
50
51#ifndef CONFIG_UPDATE_TFTP_MSEC_MAX
52#define CONFIG_UPDATE_TFTP_MSEC_MAX 100
53#endif
54
55#ifndef CONFIG_UPDATE_TFTP_CNT_MAX
56#define CONFIG_UPDATE_TFTP_CNT_MAX 0
57#endif
58
59extern ulong TftpRRQTimeoutMSecs;
60extern int TftpRRQTimeoutCountMax;
61extern flash_info_t flash_info[];
62extern ulong load_addr;
63
64static uchar *saved_prot_info;
65
66static int update_load(char *filename, ulong msec_max, int cnt_max, ulong addr)
67{
68 int size, rv;
69 ulong saved_timeout_msecs;
70 int saved_timeout_count;
71 char *saved_netretry, *saved_bootfile;
72
73 rv = 0;
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);
79
80 /* set timeouts for auto-update */
81 TftpRRQTimeoutMSecs = msec_max;
82 TftpRRQTimeoutCountMax = cnt_max;
83
84 /* we don't want to retry the connection if errors occur */
85 setenv("netretry", "no");
86
87 /* download the update file */
88 load_addr = addr;
89 copy_filename(BootFile, filename, sizeof(BootFile));
Simon Glasse4bf0c52011-10-24 18:00:02 +000090 size = NetLoop(TFTPGET);
Bartlomiej Sieka4bae9092008-10-01 15:26:31 +020091
92 if (size < 0)
93 rv = 1;
94 else if (size > 0)
95 flush_cache(addr, size);
96
97 /* restore changed globals and env variable */
98 TftpRRQTimeoutMSecs = saved_timeout_msecs;
99 TftpRRQTimeoutCountMax = saved_timeout_count;
100
101 setenv("netretry", saved_netretry);
102 if (saved_netretry != NULL)
103 free(saved_netretry);
104
105 if (saved_bootfile != NULL) {
106 copy_filename(BootFile, saved_bootfile, sizeof(BootFile));
107 free(saved_bootfile);
108 }
109
110 return rv;
111}
112
113static int update_flash_protect(int prot, ulong addr_first, ulong addr_last)
114{
115 uchar *sp_info_ptr;
116 ulong s;
117 int i, bank, cnt;
118 flash_info_t *info;
119
120 sp_info_ptr = NULL;
121
122 if (prot == 0) {
123 saved_prot_info =
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200124 calloc(CONFIG_SYS_MAX_FLASH_BANKS * CONFIG_SYS_MAX_FLASH_SECT, 1);
Bartlomiej Sieka4bae9092008-10-01 15:26:31 +0200125 if (!saved_prot_info)
126 return 1;
127 }
128
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200129 for (bank = 0; bank < CONFIG_SYS_MAX_FLASH_BANKS; ++bank) {
Bartlomiej Sieka4bae9092008-10-01 15:26:31 +0200130 cnt = 0;
131 info = &flash_info[bank];
132
133 /* Nothing to do if the bank doesn't exist */
134 if (info->sector_count == 0)
135 return 0;
136
137 /* Point to current bank protection information */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200138 sp_info_ptr = saved_prot_info + (bank * CONFIG_SYS_MAX_FLASH_SECT);
Bartlomiej Sieka4bae9092008-10-01 15:26:31 +0200139
140 /*
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.
146 */
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];
151
152 for (i = 0; i < info->sector_count; i++) {
153 /* Save current information about protected sectors */
154 if (prot == 0) {
155 s = info->start[i];
156 if ((s >= addr_first) && (s <= addr_last))
157 sp_info_ptr[i] = info->protect[i];
158
159 }
160
161 /* Protect/unprotect sectors */
162 if (sp_info_ptr[i] == 1) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200163#if defined(CONFIG_SYS_FLASH_PROTECTION)
Bartlomiej Sieka4bae9092008-10-01 15:26:31 +0200164 if (flash_real_protect(info, i, prot))
165 return 1;
166#else
167 info->protect[i] = prot;
168#endif
169 cnt++;
170 }
171 }
172
173 if (cnt) {
174 printf("%sProtected %d sectors\n",
175 prot ? "": "Un-", cnt);
176 }
177 }
178
179 if((prot == 1) && saved_prot_info)
180 free(saved_prot_info);
181
182 return 0;
183}
184
185static int update_flash(ulong addr_source, ulong addr_first, ulong size)
186{
187 ulong addr_last = addr_first + size - 1;
188
189 /* round last address to the sector boundary */
190 if (flash_sect_roundb(&addr_last) > 0)
191 return 1;
192
193 if (addr_first >= addr_last) {
194 printf("Error: end address exceeds addressing space\n");
195 return 1;
196 }
197
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");
201 return 1;
202 }
203
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");
207 return 1;
208 }
209
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");
213 return 1;
214 }
215 printf("done\n");
216
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");
220 return 1;
221 }
222
223 return 0;
224}
225
226static int update_fit_getparams(const void *fit, int noffset, ulong *addr,
227 ulong *fladdr, ulong *size)
228{
229 const void *data;
230
231 if (fit_image_get_data(fit, noffset, &data, (size_t *)size))
232 return 1;
233
234 if (fit_image_get_load(fit, noffset, (ulong *)fladdr))
235 return 1;
236
237 *addr = (ulong)data;
238
239 return 0;
240}
241
Andreas Pretzsch8d6b7322011-07-16 05:50:59 +0000242int update_tftp(ulong addr)
Bartlomiej Sieka4bae9092008-10-01 15:26:31 +0200243{
244 char *filename, *env_addr;
245 int images_noffset, ndepth, noffset;
246 ulong update_addr, update_fladdr, update_size;
Bartlomiej Sieka4bae9092008-10-01 15:26:31 +0200247 void *fit;
Andreas Pretzsch8d6b7322011-07-16 05:50:59 +0000248 int ret = 0;
249
250 /* use already present image */
251 if (addr)
252 goto got_update_file;
Bartlomiej Sieka4bae9092008-10-01 15:26:31 +0200253
254 printf("Auto-update from TFTP: ");
255
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",
260 UPDATE_FILE_ENV);
Andreas Pretzsch8d6b7322011-07-16 05:50:59 +0000261 return 1;
Bartlomiej Sieka4bae9092008-10-01 15:26:31 +0200262 }
263
264 printf("trying update file '%s'\n", filename);
265
266 /* get load address of downloaded update file */
267 if ((env_addr = getenv("loadaddr")) != NULL)
268 addr = simple_strtoul(env_addr, NULL, 16);
269 else
270 addr = CONFIG_UPDATE_LOAD_ADDR;
271
272
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");
Andreas Pretzsch8d6b7322011-07-16 05:50:59 +0000276 return 1;
Bartlomiej Sieka4bae9092008-10-01 15:26:31 +0200277 }
278
Andreas Pretzsch8d6b7322011-07-16 05:50:59 +0000279got_update_file:
Bartlomiej Sieka4bae9092008-10-01 15:26:31 +0200280 fit = (void *)addr;
281
282 if (!fit_check_format((void *)fit)) {
283 printf("Bad FIT format of the update file, aborting "
284 "auto-update\n");
Andreas Pretzsch8d6b7322011-07-16 05:50:59 +0000285 return 1;
Bartlomiej Sieka4bae9092008-10-01 15:26:31 +0200286 }
287
288 /* process updates */
289 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
290
291 ndepth = 0;
292 noffset = fdt_next_node(fit, images_noffset, &ndepth);
293 while (noffset >= 0 && ndepth > 0) {
294 if (ndepth != 1)
295 goto next_node;
296
297 printf("Processing update '%s' :",
298 fit_get_name(fit, noffset, NULL));
299
300 if (!fit_image_check_hashes(fit, noffset)) {
301 printf("Error: invalid update hash, aborting\n");
Andreas Pretzsch8d6b7322011-07-16 05:50:59 +0000302 ret = 1;
Bartlomiej Sieka4bae9092008-10-01 15:26:31 +0200303 goto next_node;
304 }
305
306 printf("\n");
307 if (update_fit_getparams(fit, noffset, &update_addr,
308 &update_fladdr, &update_size)) {
309 printf("Error: can't get update parameteres, "
310 "aborting\n");
Andreas Pretzsch8d6b7322011-07-16 05:50:59 +0000311 ret = 1;
Bartlomiej Sieka4bae9092008-10-01 15:26:31 +0200312 goto next_node;
313 }
314 if (update_flash(update_addr, update_fladdr, update_size)) {
315 printf("Error: can't flash update, aborting\n");
Andreas Pretzsch8d6b7322011-07-16 05:50:59 +0000316 ret = 1;
Bartlomiej Sieka4bae9092008-10-01 15:26:31 +0200317 goto next_node;
318 }
319next_node:
320 noffset = fdt_next_node(fit, noffset, &ndepth);
321 }
322
Andreas Pretzsch8d6b7322011-07-16 05:50:59 +0000323 return ret;
Bartlomiej Sieka4bae9092008-10-01 15:26:31 +0200324}