]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - common/update.c
Merge branch 'master' of git://git.denx.de/u-boot-video
[karo-tx-uboot.git] / common / update.c
1 /*
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
33 #if defined(CONFIG_SYS_NO_FLASH)
34 #error "CONFIG_SYS_NO_FLASH defined, but FLASH is required for auto-update feature"
35 #endif
36
37 #include <command.h>
38 #include <flash.h>
39 #include <net.h>
40 #include <net/tftp.h>
41 #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
59 extern ulong TftpRRQTimeoutMSecs;
60 extern int TftpRRQTimeoutCountMax;
61 extern flash_info_t flash_info[];
62 extern ulong load_addr;
63
64 static uchar *saved_prot_info;
65
66 static 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));
90         size = NetLoop(TFTPGET);
91
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
113 static 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 =
124                         calloc(CONFIG_SYS_MAX_FLASH_BANKS * CONFIG_SYS_MAX_FLASH_SECT, 1);
125                 if (!saved_prot_info)
126                         return 1;
127         }
128
129         for (bank = 0; bank < CONFIG_SYS_MAX_FLASH_BANKS; ++bank) {
130                 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 */
138                 sp_info_ptr = saved_prot_info + (bank * CONFIG_SYS_MAX_FLASH_SECT);
139
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) {
163 #if defined(CONFIG_SYS_FLASH_PROTECTION)
164                                 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
185 static 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
226 static 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
242 int update_tftp(ulong addr)
243 {
244         char *filename, *env_addr;
245         int images_noffset, ndepth, noffset;
246         ulong update_addr, update_fladdr, update_size;
247         void *fit;
248         int ret = 0;
249
250         /* use already present image */
251         if (addr)
252                 goto got_update_file;
253
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);
261                 return 1;
262         }
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");
276                 return 1;
277         }
278
279 got_update_file:
280         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");
285                 return 1;
286         }
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_verify(fit, noffset)) {
301                         printf("Error: invalid update hash, aborting\n");
302                         ret = 1;
303                         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");
311                         ret = 1;
312                         goto next_node;
313                 }
314                 if (update_flash(update_addr, update_fladdr, update_size)) {
315                         printf("Error: can't flash update, aborting\n");
316                         ret = 1;
317                         goto next_node;
318                 }
319 next_node:
320                 noffset = fdt_next_node(fit, noffset, &ndepth);
321         }
322
323         return ret;
324 }