2 * (C) Copyright 2003-2004
3 * Gary Jennejohn, DENX Software Engineering, garyj@denx.de.
4 * Stefan Roese, esd gmbh germany, stefan.roese@esd-electronics.com
6 * SPDX-License-Identifier: GPL-2.0+
13 #include <asm/byteorder.h>
18 #include "auto_update.h"
20 #ifdef CONFIG_AUTO_UPDATE
22 #if !defined(CONFIG_CMD_FAT)
23 #error "must define CONFIG_CMD_FAT"
26 extern au_image_t au_image[];
27 extern int N_AU_IMAGES;
29 /* where to load files into memory */
30 #define LOAD_ADDR ((unsigned char *)0x100000)
31 #define MAX_LOADSZ 0x1c00000
34 long do_fat_read (const char *filename, void *buffer,
35 unsigned long maxsize, int dols);
37 extern block_dev_desc_t ide_dev_desc[CONFIG_SYS_IDE_MAXDEVICE];
39 int au_check_cksum_valid(int i, long nbytes)
43 hdr = (image_header_t *)LOAD_ADDR;
44 #if defined(CONFIG_FIT)
45 if (genimg_get_format ((void *)hdr) != IMAGE_FORMAT_LEGACY) {
46 puts ("Non legacy image format not supported\n");
51 if ((au_image[i].type == AU_FIRMWARE) &&
52 (au_image[i].size != image_get_data_size (hdr))) {
53 printf ("Image %s has wrong size\n", au_image[i].name);
57 if (nbytes != (image_get_image_size (hdr))) {
58 printf ("Image %s bad total SIZE\n", au_image[i].name);
62 /* check the data CRC */
63 if (!image_check_dcrc (hdr)) {
64 printf ("Image %s bad data checksum\n", au_image[i].name);
70 int au_check_header_valid(int i, long nbytes)
74 hdr = (image_header_t *)LOAD_ADDR;
75 #if defined(CONFIG_FIT)
76 if (genimg_get_format ((void *)hdr) != IMAGE_FORMAT_LEGACY) {
77 puts ("Non legacy image format not supported\n");
82 /* check the easy ones first */
83 if (nbytes < image_get_header_size ()) {
84 printf ("Image %s bad header SIZE\n", au_image[i].name);
87 if (!image_check_magic (hdr) || !image_check_arch (hdr, IH_ARCH_PPC)) {
88 printf ("Image %s bad MAGIC or ARCH\n", au_image[i].name);
91 if (!image_check_hcrc (hdr)) {
92 printf ("Image %s bad header checksum\n", au_image[i].name);
96 /* check the type - could do this all in one gigantic if() */
97 if (((au_image[i].type & AU_TYPEMASK) == AU_FIRMWARE) &&
98 !image_check_type (hdr, IH_TYPE_FIRMWARE)) {
99 printf ("Image %s wrong type\n", au_image[i].name);
102 if (((au_image[i].type & AU_TYPEMASK) == AU_SCRIPT) &&
103 !image_check_type (hdr, IH_TYPE_SCRIPT)) {
104 printf ("Image %s wrong type\n", au_image[i].name);
111 int au_do_update(int i, long sz)
120 hdr = (image_header_t *)LOAD_ADDR;
121 #if defined(CONFIG_FIT)
122 if (genimg_get_format ((void *)hdr) != IMAGE_FORMAT_LEGACY) {
123 puts ("Non legacy image format not supported\n");
128 switch (au_image[i].type & AU_TYPEMASK) {
130 printf("Executing script %s\n", au_image[i].name);
132 /* execute a script */
133 if (image_check_type (hdr, IH_TYPE_SCRIPT)) {
134 addr = (char *)((char *)hdr + image_get_header_size ());
135 /* stick a NULL at the end of the script, otherwise */
136 /* parse_string_outer() runs off the end. */
137 addr[image_get_data_size (hdr)] = 0;
141 * Replace cr/lf with ;
144 while (addr[k] != 0) {
145 if ((addr[k] == 10) || (addr[k] == 13)) {
151 run_command(addr, 0);
160 start = au_image[i].start;
161 end = au_image[i].start + au_image[i].size - 1;
164 * do not update firmware when image is already in flash.
166 if (au_image[i].type == AU_FIRMWARE) {
167 char *orig = (char*)start;
168 char *new = (char *)((char *)hdr +
169 image_get_header_size ());
170 nbytes = image_get_data_size (hdr);
173 if (*orig++ != *new++) {
178 printf ("Skipping firmware update - "
179 "images are identical\n");
184 /* unprotect the address range */
185 if (((au_image[i].type & AU_FLAGMASK) == AU_PROTECT) ||
186 (au_image[i].type == AU_FIRMWARE)) {
187 flash_sect_protect (0, start, end);
191 * erase the address range.
193 if (au_image[i].type != AU_NAND) {
194 printf ("Updating NOR FLASH with image %s\n",
196 debug ("flash_sect_erase(%lx, %lx);\n", start, end);
197 flash_sect_erase (start, end);
202 /* strip the header - except for the kernel and ramdisk */
203 if (au_image[i].type != AU_FIRMWARE) {
205 off = image_get_header_size ();
206 nbytes = image_get_image_size (hdr);
208 addr = (char *)((char *)hdr + image_get_header_size ());
210 nbytes = image_get_data_size (hdr);
214 * copy the data from RAM to FLASH
216 if (au_image[i].type != AU_NAND) {
217 debug ("flash_write(%p, %lx, %x)\n",
218 addr, start, nbytes);
219 rc = flash_write ((char *)addr, start,
225 printf ("Flashing failed due to error %d\n", rc);
230 * check the dcrc of the copy
232 if (au_image[i].type != AU_NAND) {
233 rc = crc32 (0, (uchar *)(start + off),
234 image_get_data_size (hdr));
236 if (rc != image_get_dcrc (hdr)) {
237 printf ("Image %s Bad Data Checksum After COPY\n",
242 /* protect the address range */
243 /* this assumes that ONLY the firmware is protected! */
244 if (((au_image[i].type & AU_FLAGMASK) == AU_PROTECT) ||
245 (au_image[i].type == AU_FIRMWARE)) {
246 flash_sect_protect (1, start, end);
252 printf("Wrong image type selected!\n");
258 static void process_macros (const char *input, char *output)
261 const char *varname_start = NULL;
262 int inputcnt = strlen (input);
263 int outputcnt = CONFIG_SYS_CBSIZE;
264 int state = 0; /* 0 = waiting for '$' */
265 /* 1 = waiting for '(' or '{' */
266 /* 2 = waiting for ')' or '}' */
267 /* 3 = waiting for ''' */
269 char *output_start = output;
271 printf ("[PROCESS_MACROS] INPUT len %d: \"%s\"\n",
272 strlen(input), input);
275 prev = '\0'; /* previous character */
277 while (inputcnt && outputcnt) {
282 /* remove one level of escape characters */
283 if ((c == '\\') && (prev != '\\')) {
292 case 0: /* Waiting for (unescaped) $ */
293 if ((c == '\'') && (prev != '\\')) {
297 if ((c == '$') && (prev != '\\')) {
304 case 1: /* Waiting for ( */
305 if (c == '(' || c == '{') {
307 varname_start = input;
319 case 2: /* Waiting for ) */
320 if (c == ')' || c == '}') {
322 char envname[CONFIG_SYS_CBSIZE], *envval;
323 /* Varname # of chars */
324 int envcnt = input - varname_start - 1;
326 /* Get the varname */
327 for (i = 0; i < envcnt; i++) {
328 envname[i] = varname_start[i];
333 envval = getenv (envname);
335 /* Copy into the line if it exists */
337 while ((*envval) && outputcnt) {
338 *(output++) = *(envval++);
341 /* Look for another '$' */
345 case 3: /* Waiting for ' */
346 if ((c == '\'') && (prev != '\\')) {
361 printf ("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n",
362 strlen (output_start), output_start);
367 * this is called from board_init() after the hardware has been set up
368 * and is usable. That seems like a good time to do this.
369 * Right now the return value is ignored.
371 int do_auto_update(void)
373 block_dev_desc_t *stor_dev = NULL;
375 int i, res, old_ctrlc;
380 if (ide_dev_desc[0].type != DEV_TYPE_UNKNOWN) {
381 stor_dev = get_dev ("ide", 0);
382 if (stor_dev == NULL) {
383 debug ("ide: unknown device\n");
388 if (fat_register_device (stor_dev, 1) != 0) {
389 debug ("Unable to register ide disk 0:1\n");
394 * Check if magic file is present
396 if ((n = do_fat_read (AU_MAGIC_FILE, buffer,
397 sizeof(buffer), LS_NO)) <= 0) {
398 debug ("No auto_update magic file (n=%d)\n", n);
402 #ifdef CONFIG_AUTO_UPDATE_SHOW
403 board_auto_update_show (1);
405 puts("\nAutoUpdate Disk detected! Trying to update system...\n");
407 /* make sure that we see CTRL-C and save the old state */
408 old_ctrlc = disable_ctrlc (0);
410 /* just loop thru all the possible files */
411 for (i = 0; i < N_AU_IMAGES; i++) {
413 * Try to expand the environment var in the fname
415 process_macros (au_image[i].name, str);
416 strcpy (au_image[i].name, str);
418 printf("Reading %s ...", au_image[i].name);
419 /* just read the header */
420 sz = do_fat_read (au_image[i].name, LOAD_ADDR,
421 image_get_header_size (), LS_NO);
422 debug ("read %s sz %ld hdr %d\n",
423 au_image[i].name, sz, image_get_header_size ());
424 if (sz <= 0 || sz < image_get_header_size ()) {
425 puts(" not found\n");
428 if (au_check_header_valid (i, sz) < 0) {
429 puts(" header not valid\n");
432 sz = do_fat_read (au_image[i].name, LOAD_ADDR,
434 debug ("read %s sz %ld hdr %d\n",
435 au_image[i].name, sz, image_get_header_size ());
436 if (sz <= 0 || sz <= image_get_header_size ()) {
437 puts(" not found\n");
440 if (au_check_cksum_valid (i, sz) < 0) {
441 puts(" checksum not valid\n");
447 res = au_do_update (i, sz);
448 /* let the user break out of the loop */
449 if (ctrlc() || had_ctrlc ()) {
456 /* restore the old state */
457 disable_ctrlc (old_ctrlc);
459 puts("AutoUpdate finished\n\n");
460 #ifdef CONFIG_AUTO_UPDATE_SHOW
461 board_auto_update_show (0);
467 int auto_update(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
474 autoupd, 1, 1, auto_update,
475 "Automatically update images",
478 #endif /* CONFIG_AUTO_UPDATE */