blob: ef61790f4bc83e190ed5dc10ad4161bd71ba55cf [file] [log] [blame]
Bartlomiej Siekae273e9f2008-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 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02007 * SPDX-License-Identifier: GPL-2.0+
Bartlomiej Siekae273e9f2008-10-01 15:26:31 +02008 */
9
10#include <common.h>
11
12#if !(defined(CONFIG_FIT) && defined(CONFIG_OF_LIBFDT))
13#error "CONFIG_FIT and CONFIG_OF_LIBFDT are required for auto-update feature"
14#endif
15
Masahiro Yamada8cea9b52017-02-11 22:43:54 +090016#if defined(CONFIG_UPDATE_TFTP) && !defined(CONFIG_MTD_NOR_FLASH)
17#error "CONFIG_UPDATE_TFTP and !CONFIG_MTD_NOR_FLASH needed for legacy behaviour"
Lukasz Majewskie32125a2015-08-24 00:21:47 +020018#endif
19
Bartlomiej Siekae273e9f2008-10-01 15:26:31 +020020#include <command.h>
21#include <flash.h>
22#include <net.h>
Kim Phillips40c2c032012-10-29 13:34:33 +000023#include <net/tftp.h>
Bartlomiej Siekae273e9f2008-10-01 15:26:31 +020024#include <malloc.h>
Lukasz Majewskie32125a2015-08-24 00:21:47 +020025#include <dfu.h>
26#include <errno.h>
Marek Vasutc6468512018-02-10 16:22:07 +010027#include <mtd/cfi_flash.h>
Bartlomiej Siekae273e9f2008-10-01 15:26:31 +020028
29/* env variable holding the location of the update file */
30#define UPDATE_FILE_ENV "updatefile"
31
32/* set configuration defaults if needed */
33#ifndef CONFIG_UPDATE_LOAD_ADDR
34#define CONFIG_UPDATE_LOAD_ADDR 0x100000
35#endif
36
37#ifndef CONFIG_UPDATE_TFTP_MSEC_MAX
38#define CONFIG_UPDATE_TFTP_MSEC_MAX 100
39#endif
40
41#ifndef CONFIG_UPDATE_TFTP_CNT_MAX
42#define CONFIG_UPDATE_TFTP_CNT_MAX 0
43#endif
44
Joe Hershberger64701592015-04-08 01:41:07 -050045extern ulong tftp_timeout_ms;
46extern int tftp_timeout_count_max;
Bartlomiej Siekae273e9f2008-10-01 15:26:31 +020047extern ulong load_addr;
Masahiro Yamada8cea9b52017-02-11 22:43:54 +090048#ifdef CONFIG_MTD_NOR_FLASH
Lukasz Majewskidc65f6d2015-08-24 00:21:44 +020049extern flash_info_t flash_info[];
Bartlomiej Siekae273e9f2008-10-01 15:26:31 +020050static uchar *saved_prot_info;
Lukasz Majewskidc65f6d2015-08-24 00:21:44 +020051#endif
Bartlomiej Siekae273e9f2008-10-01 15:26:31 +020052static int update_load(char *filename, ulong msec_max, int cnt_max, ulong addr)
53{
54 int size, rv;
55 ulong saved_timeout_msecs;
56 int saved_timeout_count;
57 char *saved_netretry, *saved_bootfile;
58
59 rv = 0;
60 /* save used globals and env variable */
Joe Hershberger64701592015-04-08 01:41:07 -050061 saved_timeout_msecs = tftp_timeout_ms;
62 saved_timeout_count = tftp_timeout_count_max;
Simon Glass64b723f2017-08-03 12:22:12 -060063 saved_netretry = strdup(env_get("netretry"));
Joe Hershberger290c8992015-04-08 01:41:02 -050064 saved_bootfile = strdup(net_boot_file_name);
Bartlomiej Siekae273e9f2008-10-01 15:26:31 +020065
66 /* set timeouts for auto-update */
Joe Hershberger64701592015-04-08 01:41:07 -050067 tftp_timeout_ms = msec_max;
68 tftp_timeout_count_max = cnt_max;
Bartlomiej Siekae273e9f2008-10-01 15:26:31 +020069
70 /* we don't want to retry the connection if errors occur */
Simon Glass6a38e412017-08-03 12:22:09 -060071 env_set("netretry", "no");
Bartlomiej Siekae273e9f2008-10-01 15:26:31 +020072
73 /* download the update file */
74 load_addr = addr;
Joe Hershberger290c8992015-04-08 01:41:02 -050075 copy_filename(net_boot_file_name, filename, sizeof(net_boot_file_name));
Joe Hershbergerc80b41b02015-04-08 01:41:21 -050076 size = net_loop(TFTPGET);
Bartlomiej Siekae273e9f2008-10-01 15:26:31 +020077
78 if (size < 0)
79 rv = 1;
80 else if (size > 0)
81 flush_cache(addr, size);
82
83 /* restore changed globals and env variable */
Joe Hershberger64701592015-04-08 01:41:07 -050084 tftp_timeout_ms = saved_timeout_msecs;
85 tftp_timeout_count_max = saved_timeout_count;
Bartlomiej Siekae273e9f2008-10-01 15:26:31 +020086
Simon Glass6a38e412017-08-03 12:22:09 -060087 env_set("netretry", saved_netretry);
Bartlomiej Siekae273e9f2008-10-01 15:26:31 +020088 if (saved_netretry != NULL)
89 free(saved_netretry);
90
91 if (saved_bootfile != NULL) {
Joe Hershberger290c8992015-04-08 01:41:02 -050092 copy_filename(net_boot_file_name, saved_bootfile,
93 sizeof(net_boot_file_name));
Bartlomiej Siekae273e9f2008-10-01 15:26:31 +020094 free(saved_bootfile);
95 }
96
97 return rv;
98}
99
Masahiro Yamada8cea9b52017-02-11 22:43:54 +0900100#ifdef CONFIG_MTD_NOR_FLASH
Bartlomiej Siekae273e9f2008-10-01 15:26:31 +0200101static int update_flash_protect(int prot, ulong addr_first, ulong addr_last)
102{
103 uchar *sp_info_ptr;
104 ulong s;
105 int i, bank, cnt;
106 flash_info_t *info;
107
108 sp_info_ptr = NULL;
109
110 if (prot == 0) {
111 saved_prot_info =
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200112 calloc(CONFIG_SYS_MAX_FLASH_BANKS * CONFIG_SYS_MAX_FLASH_SECT, 1);
Bartlomiej Siekae273e9f2008-10-01 15:26:31 +0200113 if (!saved_prot_info)
114 return 1;
115 }
116
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200117 for (bank = 0; bank < CONFIG_SYS_MAX_FLASH_BANKS; ++bank) {
Bartlomiej Siekae273e9f2008-10-01 15:26:31 +0200118 cnt = 0;
119 info = &flash_info[bank];
120
121 /* Nothing to do if the bank doesn't exist */
122 if (info->sector_count == 0)
123 return 0;
124
125 /* Point to current bank protection information */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200126 sp_info_ptr = saved_prot_info + (bank * CONFIG_SYS_MAX_FLASH_SECT);
Bartlomiej Siekae273e9f2008-10-01 15:26:31 +0200127
128 /*
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.
134 */
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];
139
140 for (i = 0; i < info->sector_count; i++) {
141 /* Save current information about protected sectors */
142 if (prot == 0) {
143 s = info->start[i];
144 if ((s >= addr_first) && (s <= addr_last))
145 sp_info_ptr[i] = info->protect[i];
146
147 }
148
149 /* Protect/unprotect sectors */
150 if (sp_info_ptr[i] == 1) {
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200151#if defined(CONFIG_SYS_FLASH_PROTECTION)
Bartlomiej Siekae273e9f2008-10-01 15:26:31 +0200152 if (flash_real_protect(info, i, prot))
153 return 1;
154#else
155 info->protect[i] = prot;
156#endif
157 cnt++;
158 }
159 }
160
161 if (cnt) {
162 printf("%sProtected %d sectors\n",
163 prot ? "": "Un-", cnt);
164 }
165 }
166
167 if((prot == 1) && saved_prot_info)
168 free(saved_prot_info);
169
170 return 0;
171}
Lukasz Majewskidc65f6d2015-08-24 00:21:44 +0200172#endif
Bartlomiej Siekae273e9f2008-10-01 15:26:31 +0200173
174static int update_flash(ulong addr_source, ulong addr_first, ulong size)
175{
Masahiro Yamada8cea9b52017-02-11 22:43:54 +0900176#ifdef CONFIG_MTD_NOR_FLASH
Bartlomiej Siekae273e9f2008-10-01 15:26:31 +0200177 ulong addr_last = addr_first + size - 1;
178
179 /* round last address to the sector boundary */
180 if (flash_sect_roundb(&addr_last) > 0)
181 return 1;
182
183 if (addr_first >= addr_last) {
184 printf("Error: end address exceeds addressing space\n");
185 return 1;
186 }
187
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");
191 return 1;
192 }
193
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");
197 return 1;
198 }
199
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");
203 return 1;
204 }
205 printf("done\n");
206
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");
210 return 1;
211 }
Lukasz Majewskidc65f6d2015-08-24 00:21:44 +0200212#endif
Bartlomiej Siekae273e9f2008-10-01 15:26:31 +0200213 return 0;
214}
215
216static int update_fit_getparams(const void *fit, int noffset, ulong *addr,
217 ulong *fladdr, ulong *size)
218{
219 const void *data;
220
221 if (fit_image_get_data(fit, noffset, &data, (size_t *)size))
222 return 1;
223
224 if (fit_image_get_load(fit, noffset, (ulong *)fladdr))
225 return 1;
226
227 *addr = (ulong)data;
228
229 return 0;
230}
231
Lukasz Majewskie32125a2015-08-24 00:21:47 +0200232int update_tftp(ulong addr, char *interface, char *devstring)
Bartlomiej Siekae273e9f2008-10-01 15:26:31 +0200233{
Lukasz Majewskie32125a2015-08-24 00:21:47 +0200234 char *filename, *env_addr, *fit_image_name;
Bartlomiej Siekae273e9f2008-10-01 15:26:31 +0200235 ulong update_addr, update_fladdr, update_size;
Lukasz Majewskie32125a2015-08-24 00:21:47 +0200236 int images_noffset, ndepth, noffset;
237 bool update_tftp_dfu;
Andreas Pretzsche3fd7c02011-07-16 05:50:59 +0000238 int ret = 0;
Lukasz Majewskie32125a2015-08-24 00:21:47 +0200239 void *fit;
240
241 if (interface == NULL && devstring == NULL) {
242 update_tftp_dfu = false;
243 } else if (interface && devstring) {
244 update_tftp_dfu = true;
245 } else {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900246 pr_err("Interface: %s and devstring: %s not supported!\n",
Lukasz Majewskie32125a2015-08-24 00:21:47 +0200247 interface, devstring);
248 return -EINVAL;
249 }
Andreas Pretzsche3fd7c02011-07-16 05:50:59 +0000250
251 /* use already present image */
252 if (addr)
253 goto got_update_file;
Bartlomiej Siekae273e9f2008-10-01 15:26:31 +0200254
255 printf("Auto-update from TFTP: ");
256
257 /* get the file name of the update file */
Simon Glass64b723f2017-08-03 12:22:12 -0600258 filename = env_get(UPDATE_FILE_ENV);
Bartlomiej Siekae273e9f2008-10-01 15:26:31 +0200259 if (filename == NULL) {
260 printf("failed, env. variable '%s' not found\n",
261 UPDATE_FILE_ENV);
Andreas Pretzsche3fd7c02011-07-16 05:50:59 +0000262 return 1;
Bartlomiej Siekae273e9f2008-10-01 15:26:31 +0200263 }
264
265 printf("trying update file '%s'\n", filename);
266
267 /* get load address of downloaded update file */
Simon Glass64b723f2017-08-03 12:22:12 -0600268 env_addr = env_get("loadaddr");
269 if (env_addr)
Bartlomiej Siekae273e9f2008-10-01 15:26:31 +0200270 addr = simple_strtoul(env_addr, NULL, 16);
271 else
272 addr = CONFIG_UPDATE_LOAD_ADDR;
273
274
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");
Andreas Pretzsche3fd7c02011-07-16 05:50:59 +0000278 return 1;
Bartlomiej Siekae273e9f2008-10-01 15:26:31 +0200279 }
280
Andreas Pretzsche3fd7c02011-07-16 05:50:59 +0000281got_update_file:
Bartlomiej Siekae273e9f2008-10-01 15:26:31 +0200282 fit = (void *)addr;
283
284 if (!fit_check_format((void *)fit)) {
285 printf("Bad FIT format of the update file, aborting "
286 "auto-update\n");
Andreas Pretzsche3fd7c02011-07-16 05:50:59 +0000287 return 1;
Bartlomiej Siekae273e9f2008-10-01 15:26:31 +0200288 }
289
290 /* process updates */
291 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
292
293 ndepth = 0;
294 noffset = fdt_next_node(fit, images_noffset, &ndepth);
295 while (noffset >= 0 && ndepth > 0) {
296 if (ndepth != 1)
297 goto next_node;
298
Lukasz Majewskie32125a2015-08-24 00:21:47 +0200299 fit_image_name = (char *)fit_get_name(fit, noffset, NULL);
300 printf("Processing update '%s' :", fit_image_name);
Bartlomiej Siekae273e9f2008-10-01 15:26:31 +0200301
Simon Glass7428ad12013-05-07 06:11:57 +0000302 if (!fit_image_verify(fit, noffset)) {
Bartlomiej Siekae273e9f2008-10-01 15:26:31 +0200303 printf("Error: invalid update hash, aborting\n");
Andreas Pretzsche3fd7c02011-07-16 05:50:59 +0000304 ret = 1;
Bartlomiej Siekae273e9f2008-10-01 15:26:31 +0200305 goto next_node;
306 }
307
308 printf("\n");
309 if (update_fit_getparams(fit, noffset, &update_addr,
310 &update_fladdr, &update_size)) {
311 printf("Error: can't get update parameteres, "
312 "aborting\n");
Andreas Pretzsche3fd7c02011-07-16 05:50:59 +0000313 ret = 1;
Bartlomiej Siekae273e9f2008-10-01 15:26:31 +0200314 goto next_node;
315 }
Lukasz Majewskie32125a2015-08-24 00:21:47 +0200316
317 if (!update_tftp_dfu) {
318 if (update_flash(update_addr, update_fladdr,
319 update_size)) {
320 printf("Error: can't flash update, aborting\n");
321 ret = 1;
322 goto next_node;
323 }
324 } else if (fit_image_check_type(fit, noffset,
325 IH_TYPE_FIRMWARE)) {
326 ret = dfu_tftp_write(fit_image_name, update_addr,
327 update_size, interface, devstring);
328 if (ret)
329 return ret;
Bartlomiej Siekae273e9f2008-10-01 15:26:31 +0200330 }
331next_node:
332 noffset = fdt_next_node(fit, noffset, &ndepth);
333 }
334
Andreas Pretzsche3fd7c02011-07-16 05:50:59 +0000335 return ret;
Bartlomiej Siekae273e9f2008-10-01 15:26:31 +0200336}