2002-10-21 01:17:16 +08:00
|
|
|
/*
|
|
|
|
* (C) Copyright 2001
|
|
|
|
* Kyle Harris, kharris@nexus-tech.net
|
|
|
|
*
|
2013-07-08 15:37:19 +08:00
|
|
|
* SPDX-License-Identifier: GPL-2.0+
|
2002-10-21 01:17:16 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2009-04-02 05:34:12 +08:00
|
|
|
* The "source" command allows to define "script images", i. e. files
|
|
|
|
* that contain command sequences that can be executed by the command
|
|
|
|
* interpreter. It returns the exit status of the last command
|
|
|
|
* executed from the script. This is very similar to running a shell
|
|
|
|
* script in a UNIX shell, hence the name for the command.
|
2002-10-21 01:17:16 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* #define DEBUG */
|
|
|
|
|
|
|
|
#include <common.h>
|
|
|
|
#include <command.h>
|
|
|
|
#include <image.h>
|
|
|
|
#include <malloc.h>
|
|
|
|
#include <asm/byteorder.h>
|
2013-04-20 16:42:49 +08:00
|
|
|
#include <asm/io.h>
|
2002-10-21 01:17:16 +08:00
|
|
|
#if defined(CONFIG_8xx)
|
|
|
|
#include <mpc8xx.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int
|
2009-04-02 05:34:12 +08:00
|
|
|
source (ulong addr, const char *fit_uname)
|
2002-10-21 01:17:16 +08:00
|
|
|
{
|
2008-05-20 22:00:29 +08:00
|
|
|
ulong len;
|
2014-05-28 17:33:33 +08:00
|
|
|
#if defined(CONFIG_IMAGE_FORMAT_LEGACY)
|
2013-04-20 16:42:49 +08:00
|
|
|
const image_header_t *hdr;
|
2014-05-28 17:33:33 +08:00
|
|
|
#endif
|
2008-03-12 17:33:00 +08:00
|
|
|
ulong *data;
|
|
|
|
int verify;
|
2013-04-20 16:42:49 +08:00
|
|
|
void *buf;
|
2008-03-12 17:33:00 +08:00
|
|
|
#if defined(CONFIG_FIT)
|
|
|
|
const void* fit_hdr;
|
|
|
|
int noffset;
|
|
|
|
const void *fit_data;
|
|
|
|
size_t fit_len;
|
|
|
|
#endif
|
2002-10-21 01:17:16 +08:00
|
|
|
|
2008-04-18 18:39:23 +08:00
|
|
|
verify = getenv_yesno ("verify");
|
2002-10-21 01:17:16 +08:00
|
|
|
|
2013-04-20 16:42:49 +08:00
|
|
|
buf = map_sysmem(addr, 0);
|
|
|
|
switch (genimg_get_format(buf)) {
|
2014-05-28 17:33:33 +08:00
|
|
|
#if defined(CONFIG_IMAGE_FORMAT_LEGACY)
|
2008-02-04 15:28:09 +08:00
|
|
|
case IMAGE_FORMAT_LEGACY:
|
2013-04-20 16:42:49 +08:00
|
|
|
hdr = buf;
|
2002-10-21 01:17:16 +08:00
|
|
|
|
2008-02-04 15:28:09 +08:00
|
|
|
if (!image_check_magic (hdr)) {
|
|
|
|
puts ("Bad magic number\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2002-10-21 01:17:16 +08:00
|
|
|
|
2008-02-04 15:28:09 +08:00
|
|
|
if (!image_check_hcrc (hdr)) {
|
|
|
|
puts ("Bad header crc\n");
|
2002-10-21 01:17:16 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2008-02-04 15:28:09 +08: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-21 01:17:16 +08:00
|
|
|
|
2008-02-04 15:28:09 +08:00
|
|
|
/* get length of script */
|
|
|
|
data = (ulong *)image_get_data (hdr);
|
2002-10-21 01:17:16 +08:00
|
|
|
|
2008-02-29 21:58:34 +08:00
|
|
|
if ((len = uimage_to_cpu (*data)) == 0) {
|
2008-02-04 15:28:09 +08:00
|
|
|
puts ("Empty Script\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2008-03-21 06:10:19 +08: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 15:28:09 +08:00
|
|
|
break;
|
2014-05-28 17:33:33 +08:00
|
|
|
#endif
|
2008-02-04 15:28:09 +08:00
|
|
|
#if defined(CONFIG_FIT)
|
|
|
|
case IMAGE_FORMAT_FIT:
|
2008-03-12 17:33:00 +08:00
|
|
|
if (fit_uname == NULL) {
|
|
|
|
puts ("No FIT subimage unit name\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-04-20 16:42:49 +08:00
|
|
|
fit_hdr = buf;
|
2008-03-12 17:33:00 +08:00
|
|
|
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) {
|
2013-05-07 14:11:57 +08:00
|
|
|
if (!fit_image_verify(fit_hdr, noffset)) {
|
2008-03-12 17:33:00 +08:00
|
|
|
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 15:28:09 +08:00
|
|
|
#endif
|
|
|
|
default:
|
2009-04-02 05:34:12 +08:00
|
|
|
puts ("Wrong image format for \"source\" command\n");
|
2002-10-21 01:17:16 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2002-11-04 02:03:52 +08:00
|
|
|
debug ("** Script length: %ld\n", len);
|
2012-03-31 05:30:55 +08:00
|
|
|
return run_command_list((char *)data, len, 0);
|
2002-10-21 01:17:16 +08:00
|
|
|
}
|
|
|
|
|
2003-06-28 05:31:46 +08:00
|
|
|
/**************************************************/
|
2009-04-02 05:34:12 +08:00
|
|
|
#if defined(CONFIG_CMD_SOURCE)
|
2002-10-21 01:17:16 +08:00
|
|
|
int
|
2010-06-29 04:00:46 +08:00
|
|
|
do_source (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
|
2002-10-21 01:17:16 +08:00
|
|
|
{
|
|
|
|
ulong addr;
|
|
|
|
int rcode;
|
2008-03-12 17:33:00 +08:00
|
|
|
const char *fit_uname = NULL;
|
2002-10-21 01:17:16 +08:00
|
|
|
|
2008-03-12 17:33:00 +08:00
|
|
|
/* Find script image */
|
2002-10-21 01:17:16 +08:00
|
|
|
if (argc < 2) {
|
2008-10-16 21:01:15 +08:00
|
|
|
addr = CONFIG_SYS_LOAD_ADDR;
|
2009-04-02 05:34:12 +08:00
|
|
|
debug ("* source: default load address = 0x%08lx\n", addr);
|
2008-03-12 17:33:00 +08:00
|
|
|
#if defined(CONFIG_FIT)
|
|
|
|
} else if (fit_parse_subimage (argv[1], load_addr, &addr, &fit_uname)) {
|
2009-04-02 05:34:12 +08:00
|
|
|
debug ("* source: subimage '%s' from FIT image at 0x%08lx\n",
|
2008-03-12 17:33:00 +08:00
|
|
|
fit_uname, addr);
|
|
|
|
#endif
|
2002-10-21 01:17:16 +08:00
|
|
|
} else {
|
2008-03-12 17:33:00 +08:00
|
|
|
addr = simple_strtoul(argv[1], NULL, 16);
|
2009-04-02 05:34:12 +08:00
|
|
|
debug ("* source: cmdline image address = 0x%08lx\n", addr);
|
2002-10-21 01:17:16 +08:00
|
|
|
}
|
|
|
|
|
2008-03-12 17:33:00 +08:00
|
|
|
printf ("## Executing script at %08lx\n", addr);
|
2009-04-02 05:34:12 +08:00
|
|
|
rcode = source (addr, fit_uname);
|
2002-10-21 01:17:16 +08:00
|
|
|
return rcode;
|
|
|
|
}
|
2003-06-28 05:31:46 +08:00
|
|
|
|
2012-10-29 21:34:31 +08:00
|
|
|
#ifdef CONFIG_SYS_LONGHELP
|
|
|
|
static char source_help_text[] =
|
2009-04-02 05:34:12 +08:00
|
|
|
"[addr]\n"
|
|
|
|
"\t- run script starting at addr\n"
|
2009-05-24 23:06:54 +08:00
|
|
|
"\t- A valid image header must be present"
|
2008-03-12 17:33:00 +08:00
|
|
|
#if defined(CONFIG_FIT)
|
2009-05-24 23:06:54 +08:00
|
|
|
"\n"
|
2008-03-12 17:33:00 +08:00
|
|
|
"For FIT format uImage addr must include subimage\n"
|
2009-05-24 23:06:54 +08:00
|
|
|
"unit name in the form of addr:<subimg_uname>"
|
2007-07-11 00:02:44 +08:00
|
|
|
#endif
|
2012-10-29 21:34:31 +08:00
|
|
|
"";
|
|
|
|
#endif
|
|
|
|
|
|
|
|
U_BOOT_CMD(
|
|
|
|
source, 2, 0, do_source,
|
|
|
|
"run script from memory", source_help_text
|
2008-03-12 17:33:00 +08:00
|
|
|
);
|
2007-07-11 00:02:44 +08:00
|
|
|
#endif
|