]> git.ipfire.org Git - people/ms/u-boot.git/blame - common/update.c
imx: hab: Check if CSF is valid before authenticating image
[people/ms/u-boot.git] / common / update.c
CommitLineData
4bae9090
BS
1/*
2 * (C) Copyright 2008 Semihalf
3 *
4 * Written by: Rafal Czubak <rcz@semihalf.com>
5 * Bartlomiej Sieka <tur@semihalf.com>
6 *
1a459660 7 * SPDX-License-Identifier: GPL-2.0+
4bae9090
BS
8 */
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
e856bdcf
MY
16#if defined(CONFIG_UPDATE_TFTP) && !defined(CONFIG_MTD_NOR_FLASH)
17#error "CONFIG_UPDATE_TFTP and !CONFIG_MTD_NOR_FLASH needed for legacy behaviour"
c7ff5528
LM
18#endif
19
4bae9090
BS
20#include <command.h>
21#include <flash.h>
22#include <net.h>
06370590 23#include <net/tftp.h>
4bae9090 24#include <malloc.h>
c7ff5528
LM
25#include <dfu.h>
26#include <errno.h>
175c3e3a 27#include <mtd/cfi_flash.h>
4bae9090
BS
28
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
8885c5fe
JH
45extern ulong tftp_timeout_ms;
46extern int tftp_timeout_count_max;
4bae9090 47extern ulong load_addr;
e856bdcf 48#ifdef CONFIG_MTD_NOR_FLASH
66a64723 49extern flash_info_t flash_info[];
4bae9090 50static uchar *saved_prot_info;
66a64723 51#endif
4bae9090
BS
52static 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 */
8885c5fe
JH
61 saved_timeout_msecs = tftp_timeout_ms;
62 saved_timeout_count = tftp_timeout_count_max;
00caae6d 63 saved_netretry = strdup(env_get("netretry"));
1411157d 64 saved_bootfile = strdup(net_boot_file_name);
4bae9090
BS
65
66 /* set timeouts for auto-update */
8885c5fe
JH
67 tftp_timeout_ms = msec_max;
68 tftp_timeout_count_max = cnt_max;
4bae9090
BS
69
70 /* we don't want to retry the connection if errors occur */
382bee57 71 env_set("netretry", "no");
4bae9090
BS
72
73 /* download the update file */
74 load_addr = addr;
1411157d 75 copy_filename(net_boot_file_name, filename, sizeof(net_boot_file_name));
bc0571fc 76 size = net_loop(TFTPGET);
4bae9090
BS
77
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 */
8885c5fe
JH
84 tftp_timeout_ms = saved_timeout_msecs;
85 tftp_timeout_count_max = saved_timeout_count;
4bae9090 86
382bee57 87 env_set("netretry", saved_netretry);
4bae9090
BS
88 if (saved_netretry != NULL)
89 free(saved_netretry);
90
91 if (saved_bootfile != NULL) {
1411157d
JH
92 copy_filename(net_boot_file_name, saved_bootfile,
93 sizeof(net_boot_file_name));
4bae9090
BS
94 free(saved_bootfile);
95 }
96
97 return rv;
98}
99
e856bdcf 100#ifdef CONFIG_MTD_NOR_FLASH
4bae9090
BS
101static 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 =
6d0f6bcf 112 calloc(CONFIG_SYS_MAX_FLASH_BANKS * CONFIG_SYS_MAX_FLASH_SECT, 1);
4bae9090
BS
113 if (!saved_prot_info)
114 return 1;
115 }
116
6d0f6bcf 117 for (bank = 0; bank < CONFIG_SYS_MAX_FLASH_BANKS; ++bank) {
4bae9090
BS
118 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 */
6d0f6bcf 126 sp_info_ptr = saved_prot_info + (bank * CONFIG_SYS_MAX_FLASH_SECT);
4bae9090
BS
127
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) {
6d0f6bcf 151#if defined(CONFIG_SYS_FLASH_PROTECTION)
4bae9090
BS
152 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}
66a64723 172#endif
4bae9090
BS
173
174static int update_flash(ulong addr_source, ulong addr_first, ulong size)
175{
e856bdcf 176#ifdef CONFIG_MTD_NOR_FLASH
4bae9090
BS
177 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 }
66a64723 212#endif
4bae9090
BS
213 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
c7ff5528 232int update_tftp(ulong addr, char *interface, char *devstring)
4bae9090 233{
c7ff5528 234 char *filename, *env_addr, *fit_image_name;
4bae9090 235 ulong update_addr, update_fladdr, update_size;
c7ff5528
LM
236 int images_noffset, ndepth, noffset;
237 bool update_tftp_dfu;
8d6b7320 238 int ret = 0;
c7ff5528
LM
239 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 {
9b643e31 246 pr_err("Interface: %s and devstring: %s not supported!\n",
c7ff5528
LM
247 interface, devstring);
248 return -EINVAL;
249 }
8d6b7320
AP
250
251 /* use already present image */
252 if (addr)
253 goto got_update_file;
4bae9090
BS
254
255 printf("Auto-update from TFTP: ");
256
257 /* get the file name of the update file */
00caae6d 258 filename = env_get(UPDATE_FILE_ENV);
4bae9090
BS
259 if (filename == NULL) {
260 printf("failed, env. variable '%s' not found\n",
261 UPDATE_FILE_ENV);
8d6b7320 262 return 1;
4bae9090
BS
263 }
264
265 printf("trying update file '%s'\n", filename);
266
267 /* get load address of downloaded update file */
00caae6d
SG
268 env_addr = env_get("loadaddr");
269 if (env_addr)
4bae9090
BS
270 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");
8d6b7320 278 return 1;
4bae9090
BS
279 }
280
8d6b7320 281got_update_file:
4bae9090
BS
282 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");
8d6b7320 287 return 1;
4bae9090
BS
288 }
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
c7ff5528
LM
299 fit_image_name = (char *)fit_get_name(fit, noffset, NULL);
300 printf("Processing update '%s' :", fit_image_name);
4bae9090 301
b8da8366 302 if (!fit_image_verify(fit, noffset)) {
4bae9090 303 printf("Error: invalid update hash, aborting\n");
8d6b7320 304 ret = 1;
4bae9090
BS
305 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");
8d6b7320 313 ret = 1;
4bae9090
BS
314 goto next_node;
315 }
c7ff5528
LM
316
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;
4bae9090
BS
330 }
331next_node:
332 noffset = fdt_next_node(fit, noffset, &ndepth);
333 }
334
8d6b7320 335 return ret;
4bae9090 336}