2002-10-20 17:17:16 +00:00
|
|
|
/*
|
|
|
|
* (C) Copyright 2001
|
|
|
|
* Kyle Harris, kharris@nexus-tech.net
|
|
|
|
*
|
|
|
|
* See file CREDITS for list of people who contributed to this
|
|
|
|
* project.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2 of
|
|
|
|
* the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
|
|
|
|
* MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* autoscript allows a remote host to download a command file and,
|
|
|
|
* optionally, binary data for automatically updating the target. For
|
|
|
|
* example, you create a new kernel image and want the user to be
|
|
|
|
* able to simply download the image and the machine does the rest.
|
|
|
|
* The kernel image is postprocessed with mkimage, which creates an
|
|
|
|
* image with a script file prepended. If enabled, autoscript will
|
|
|
|
* verify the script and contents of the download and execute the
|
|
|
|
* script portion. This would be responsible for erasing flash,
|
|
|
|
* copying the new image, and rebooting the machine.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* #define DEBUG */
|
|
|
|
|
|
|
|
#include <common.h>
|
|
|
|
#include <command.h>
|
|
|
|
#include <image.h>
|
|
|
|
#include <malloc.h>
|
|
|
|
#include <asm/byteorder.h>
|
|
|
|
#if defined(CONFIG_8xx)
|
|
|
|
#include <mpc8xx.h>
|
|
|
|
#endif
|
|
|
|
#ifdef CFG_HUSH_PARSER
|
|
|
|
#include <hush.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int
|
2008-03-12 09:33:00 +00:00
|
|
|
autoscript (ulong addr, const char *fit_uname)
|
2002-10-20 17:17:16 +00:00
|
|
|
{
|
2008-05-20 14:00:29 +00:00
|
|
|
ulong len;
|
2008-03-12 09:33:00 +00:00
|
|
|
image_header_t *hdr;
|
|
|
|
ulong *data;
|
|
|
|
char *cmd;
|
|
|
|
int rcode = 0;
|
|
|
|
int verify;
|
|
|
|
#if defined(CONFIG_FIT)
|
|
|
|
const void* fit_hdr;
|
|
|
|
int noffset;
|
|
|
|
const void *fit_data;
|
|
|
|
size_t fit_len;
|
|
|
|
#endif
|
2002-10-20 17:17:16 +00:00
|
|
|
|
2008-04-18 10:39:23 +00:00
|
|
|
verify = getenv_yesno ("verify");
|
2002-10-20 17:17:16 +00:00
|
|
|
|
2008-02-29 13:58:34 +00:00
|
|
|
switch (genimg_get_format ((void *)addr)) {
|
2008-02-04 07:28:09 +00:00
|
|
|
case IMAGE_FORMAT_LEGACY:
|
|
|
|
hdr = (image_header_t *)addr;
|
2002-10-20 17:17:16 +00:00
|
|
|
|
2008-02-04 07:28:09 +00:00
|
|
|
if (!image_check_magic (hdr)) {
|
|
|
|
puts ("Bad magic number\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2002-10-20 17:17:16 +00:00
|
|
|
|
2008-02-04 07:28:09 +00:00
|
|
|
if (!image_check_hcrc (hdr)) {
|
|
|
|
puts ("Bad header crc\n");
|
2002-10-20 17:17:16 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2008-02-04 07:28:09 +00:00
|
|
|
if (verify) {
|
|
|
|
if (!image_check_dcrc (hdr)) {
|
|
|
|
puts ("Bad data crc\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!image_check_type (hdr, IH_TYPE_SCRIPT)) {
|
|
|
|
puts ("Bad image type\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2002-10-20 17:17:16 +00:00
|
|
|
|
2008-02-04 07:28:09 +00:00
|
|
|
/* get length of script */
|
|
|
|
data = (ulong *)image_get_data (hdr);
|
2002-10-20 17:17:16 +00:00
|
|
|
|
2008-02-29 13:58:34 +00:00
|
|
|
if ((len = uimage_to_cpu (*data)) == 0) {
|
2008-02-04 07:28:09 +00:00
|
|
|
puts ("Empty Script\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2008-03-20 22:10:19 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* scripts are just multi-image files with one component, seek
|
|
|
|
* past the zero-terminated sequence of image lengths to get
|
|
|
|
* to the actual image data
|
|
|
|
*/
|
|
|
|
while (*data++);
|
2008-02-04 07:28:09 +00:00
|
|
|
break;
|
|
|
|
#if defined(CONFIG_FIT)
|
|
|
|
case IMAGE_FORMAT_FIT:
|
2008-03-12 09:33:00 +00:00
|
|
|
if (fit_uname == NULL) {
|
|
|
|
puts ("No FIT subimage unit name\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
fit_hdr = (const void *)addr;
|
|
|
|
if (!fit_check_format (fit_hdr)) {
|
|
|
|
puts ("Bad FIT image format\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get script component image node offset */
|
|
|
|
noffset = fit_image_get_node (fit_hdr, fit_uname);
|
|
|
|
if (noffset < 0) {
|
|
|
|
printf ("Can't find '%s' FIT subimage\n", fit_uname);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!fit_image_check_type (fit_hdr, noffset, IH_TYPE_SCRIPT)) {
|
|
|
|
puts ("Not a image image\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* verify integrity */
|
|
|
|
if (verify) {
|
|
|
|
if (!fit_image_check_hashes (fit_hdr, noffset)) {
|
|
|
|
puts ("Bad Data Hash\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get script subimage data address and length */
|
|
|
|
if (fit_image_get_data (fit_hdr, noffset, &fit_data, &fit_len)) {
|
|
|
|
puts ("Could not find script subimage data\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
data = (ulong *)fit_data;
|
|
|
|
len = (ulong)fit_len;
|
|
|
|
break;
|
2008-02-04 07:28:09 +00:00
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
puts ("Wrong image format for autoscript\n");
|
2002-10-20 17:17:16 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2002-11-03 18:03:52 +00:00
|
|
|
debug ("** Script length: %ld\n", len);
|
2002-10-20 17:17:16 +00:00
|
|
|
|
|
|
|
if ((cmd = malloc (len + 1)) == NULL) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* make sure cmd is null terminated */
|
2008-01-08 17:12:17 +00:00
|
|
|
memmove (cmd, (char *)data, len);
|
2002-10-20 17:17:16 +00:00
|
|
|
*(cmd + len) = 0;
|
|
|
|
|
2003-06-27 21:31:46 +00:00
|
|
|
#ifdef CFG_HUSH_PARSER /*?? */
|
2002-10-20 17:17:16 +00:00
|
|
|
rcode = parse_string_outer (cmd, FLAG_PARSE_SEMICOLON);
|
|
|
|
#else
|
|
|
|
{
|
|
|
|
char *line = cmd;
|
|
|
|
char *next = cmd;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* break into individual lines,
|
|
|
|
* and execute each line;
|
|
|
|
* terminate on error.
|
|
|
|
*/
|
|
|
|
while (*next) {
|
|
|
|
if (*next == '\n') {
|
|
|
|
*next = '\0';
|
|
|
|
/* run only non-empty commands */
|
2008-09-10 06:43:49 +00:00
|
|
|
if (*line) {
|
2002-10-20 17:17:16 +00:00
|
|
|
debug ("** exec: \"%s\"\n",
|
|
|
|
line);
|
|
|
|
if (run_command (line, 0) < 0) {
|
|
|
|
rcode = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
line = next + 1;
|
|
|
|
}
|
|
|
|
++next;
|
|
|
|
}
|
2008-09-10 06:43:49 +00:00
|
|
|
if (rcode == 0 && *line)
|
|
|
|
rcode = (run_command(line, 0) >= 0);
|
2002-10-20 17:17:16 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
free (cmd);
|
|
|
|
return rcode;
|
|
|
|
}
|
|
|
|
|
2003-06-27 21:31:46 +00:00
|
|
|
/**************************************************/
|
2007-07-08 22:51:39 +00:00
|
|
|
#if defined(CONFIG_CMD_AUTOSCRIPT)
|
2002-10-20 17:17:16 +00:00
|
|
|
int
|
|
|
|
do_autoscript (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
|
|
|
|
{
|
|
|
|
ulong addr;
|
|
|
|
int rcode;
|
2008-03-12 09:33:00 +00:00
|
|
|
const char *fit_uname = NULL;
|
2002-10-20 17:17:16 +00:00
|
|
|
|
2008-03-12 09:33:00 +00:00
|
|
|
/* Find script image */
|
2002-10-20 17:17:16 +00:00
|
|
|
if (argc < 2) {
|
|
|
|
addr = CFG_LOAD_ADDR;
|
2008-03-12 09:33:00 +00:00
|
|
|
debug ("* autoscr: default load address = 0x%08lx\n", addr);
|
|
|
|
#if defined(CONFIG_FIT)
|
|
|
|
} else if (fit_parse_subimage (argv[1], load_addr, &addr, &fit_uname)) {
|
|
|
|
debug ("* autoscr: subimage '%s' from FIT image at 0x%08lx\n",
|
|
|
|
fit_uname, addr);
|
|
|
|
#endif
|
2002-10-20 17:17:16 +00:00
|
|
|
} else {
|
2008-03-12 09:33:00 +00:00
|
|
|
addr = simple_strtoul(argv[1], NULL, 16);
|
|
|
|
debug ("* autoscr: cmdline image address = 0x%08lx\n", addr);
|
2002-10-20 17:17:16 +00:00
|
|
|
}
|
|
|
|
|
2008-03-12 09:33:00 +00:00
|
|
|
printf ("## Executing script at %08lx\n", addr);
|
|
|
|
rcode = autoscript (addr, fit_uname);
|
2002-10-20 17:17:16 +00:00
|
|
|
return rcode;
|
|
|
|
}
|
2003-06-27 21:31:46 +00:00
|
|
|
|
2003-07-01 21:06:45 +00:00
|
|
|
U_BOOT_CMD(
|
|
|
|
autoscr, 2, 0, do_autoscript,
|
2003-06-27 21:31:46 +00:00
|
|
|
"autoscr - run script from memory\n",
|
|
|
|
"[addr] - run script starting at addr"
|
|
|
|
" - A valid autoscr header must be present\n"
|
2008-03-12 09:33:00 +00:00
|
|
|
#if defined(CONFIG_FIT)
|
|
|
|
"For FIT format uImage addr must include subimage\n"
|
|
|
|
"unit name in the form of addr:<subimg_uname>\n"
|
2007-07-10 16:02:44 +00:00
|
|
|
#endif
|
2008-03-12 09:33:00 +00:00
|
|
|
);
|
2007-07-10 16:02:44 +00:00
|
|
|
#endif
|