2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2002-10-20 17:17:16 +00:00
|
|
|
/*
|
|
|
|
* (C) Copyright 2001
|
|
|
|
* Kyle Harris, kharris@nexus-tech.net
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2009-04-01 21:34:12 +00: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-20 17:17:16 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* #define DEBUG */
|
|
|
|
|
|
|
|
#include <common.h>
|
|
|
|
#include <command.h>
|
2019-08-01 15:46:49 +00:00
|
|
|
#include <env.h>
|
2002-10-20 17:17:16 +00:00
|
|
|
#include <image.h>
|
2020-05-10 17:40:05 +00:00
|
|
|
#include <log.h>
|
2002-10-20 17:17:16 +00:00
|
|
|
#include <malloc.h>
|
2015-03-22 22:08:59 +00:00
|
|
|
#include <mapmem.h>
|
2002-10-20 17:17:16 +00:00
|
|
|
#include <asm/byteorder.h>
|
2013-04-20 08:42:49 +00:00
|
|
|
#include <asm/io.h>
|
2002-10-20 17:17:16 +00:00
|
|
|
|
2018-06-22 14:58:02 +00:00
|
|
|
#if defined(CONFIG_FIT)
|
|
|
|
/**
|
|
|
|
* get_default_image() - Return default property from /images
|
|
|
|
*
|
|
|
|
* Return: Pointer to value of default property (or NULL)
|
|
|
|
*/
|
|
|
|
static const char *get_default_image(const void *fit)
|
|
|
|
{
|
|
|
|
int images_noffset;
|
|
|
|
|
|
|
|
images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
|
|
|
|
if (images_noffset < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return fdt_getprop(fit, images_noffset, FIT_DEFAULT_PROP, NULL);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-12-12 19:12:11 +00:00
|
|
|
int image_source_script(ulong addr, const char *fit_uname, const char *confname)
|
2002-10-20 17:17:16 +00:00
|
|
|
{
|
2008-05-20 14:00:29 +00:00
|
|
|
ulong len;
|
2019-05-23 11:14:07 +00:00
|
|
|
#if defined(CONFIG_LEGACY_IMAGE_FORMAT)
|
2022-09-07 02:26:52 +00:00
|
|
|
const struct legacy_img_hdr *hdr;
|
2014-05-28 09:33:33 +00:00
|
|
|
#endif
|
2015-07-30 06:00:01 +00:00
|
|
|
u32 *data;
|
2008-03-12 09:33:00 +00:00
|
|
|
int verify;
|
2013-04-20 08:42:49 +00:00
|
|
|
void *buf;
|
2008-03-12 09:33:00 +00:00
|
|
|
#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
|
|
|
|
2017-08-03 18:22:13 +00:00
|
|
|
verify = env_get_yesno("verify");
|
2002-10-20 17:17:16 +00:00
|
|
|
|
2013-04-20 08:42:49 +00:00
|
|
|
buf = map_sysmem(addr, 0);
|
|
|
|
switch (genimg_get_format(buf)) {
|
2019-05-23 11:14:07 +00:00
|
|
|
#if defined(CONFIG_LEGACY_IMAGE_FORMAT)
|
2008-02-04 07:28:09 +00:00
|
|
|
case IMAGE_FORMAT_LEGACY:
|
2013-04-20 08:42:49 +00:00
|
|
|
hdr = buf;
|
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 */
|
2015-07-30 06:00:01 +00:00
|
|
|
data = (u32 *)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;
|
2014-05-28 09:33:33 +00:00
|
|
|
#endif
|
2008-02-04 07:28:09 +00:00
|
|
|
#if defined(CONFIG_FIT)
|
|
|
|
case IMAGE_FORMAT_FIT:
|
2013-04-20 08:42:49 +00:00
|
|
|
fit_hdr = buf;
|
2021-02-16 00:08:09 +00:00
|
|
|
if (fit_check_format(fit_hdr, IMAGE_SIZE_INVAL)) {
|
2008-03-12 09:33:00 +00:00
|
|
|
puts ("Bad FIT image format\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-06-22 14:58:02 +00:00
|
|
|
if (!fit_uname) {
|
2022-12-12 19:12:11 +00:00
|
|
|
/* If confname is empty, use the default */
|
|
|
|
if (confname && *confname)
|
|
|
|
noffset = fit_conf_get_node(fit_hdr, confname);
|
|
|
|
else
|
|
|
|
noffset = fit_conf_get_node(fit_hdr, NULL);
|
|
|
|
if (noffset < 0) {
|
|
|
|
if (!confname)
|
|
|
|
goto fallback;
|
|
|
|
printf("Could not find config %s\n", confname);
|
|
|
|
return 1;
|
|
|
|
}
|
2018-06-22 14:58:02 +00:00
|
|
|
|
2022-12-12 19:12:11 +00:00
|
|
|
if (verify && fit_config_verify(fit_hdr, noffset))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
noffset = fit_conf_get_prop_node(fit_hdr, noffset,
|
|
|
|
FIT_SCRIPT_PROP,
|
|
|
|
IH_PHASE_NONE);
|
|
|
|
if (noffset < 0) {
|
|
|
|
if (!confname)
|
|
|
|
goto fallback;
|
|
|
|
printf("Could not find script in %s\n", confname);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fallback:
|
|
|
|
if (!fit_uname || !*fit_uname)
|
|
|
|
fit_uname = get_default_image(fit_hdr);
|
|
|
|
if (!fit_uname) {
|
|
|
|
puts("No FIT subimage unit name\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;
|
|
|
|
}
|
2008-03-12 09:33:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!fit_image_check_type (fit_hdr, noffset, IH_TYPE_SCRIPT)) {
|
2022-12-12 19:12:10 +00:00
|
|
|
puts("Not a script image\n");
|
2008-03-12 09:33:00 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* verify integrity */
|
2022-12-12 19:12:10 +00:00
|
|
|
if (verify && !fit_image_verify(fit_hdr, noffset)) {
|
|
|
|
puts("Bad Data Hash\n");
|
|
|
|
return 1;
|
2008-03-12 09:33:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
2015-07-30 06:00:01 +00:00
|
|
|
data = (u32 *)fit_data;
|
2008-03-12 09:33:00 +00:00
|
|
|
len = (ulong)fit_len;
|
|
|
|
break;
|
2008-02-04 07:28:09 +00:00
|
|
|
#endif
|
|
|
|
default:
|
2009-04-01 21:34:12 +00:00
|
|
|
puts ("Wrong image format for \"source\" command\n");
|
2002-10-20 17:17:16 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-05-10 17:40:04 +00:00
|
|
|
debug("** Script length: %ld\n", len);
|
2012-03-30 21:30:55 +00:00
|
|
|
return run_command_list((char *)data, len, 0);
|
2002-10-20 17:17:16 +00:00
|
|
|
}
|
|
|
|
|
2003-06-27 21:31:46 +00:00
|
|
|
/**************************************************/
|
2009-04-01 21:34:12 +00:00
|
|
|
#if defined(CONFIG_CMD_SOURCE)
|
2020-05-10 17:40:03 +00:00
|
|
|
static int do_source(struct cmd_tbl *cmdtp, int flag, int argc,
|
|
|
|
char *const argv[])
|
2002-10-20 17:17:16 +00:00
|
|
|
{
|
|
|
|
ulong addr;
|
|
|
|
int rcode;
|
2022-12-12 19:12:11 +00:00
|
|
|
const char *fit_uname = NULL, *confname = 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) {
|
2008-10-16 13:01:15 +00:00
|
|
|
addr = CONFIG_SYS_LOAD_ADDR;
|
2020-05-10 17:40:04 +00:00
|
|
|
debug("* source: default load address = 0x%08lx\n", addr);
|
2008-03-12 09:33:00 +00:00
|
|
|
#if defined(CONFIG_FIT)
|
2019-12-28 17:45:02 +00:00
|
|
|
} else if (fit_parse_subimage(argv[1], image_load_addr, &addr,
|
|
|
|
&fit_uname)) {
|
2020-05-10 17:40:04 +00:00
|
|
|
debug("* source: subimage '%s' from FIT image at 0x%08lx\n",
|
|
|
|
fit_uname, addr);
|
2022-12-12 19:12:11 +00:00
|
|
|
} else if (fit_parse_conf(argv[1], image_load_addr, &addr, &confname)) {
|
|
|
|
debug("* source: config '%s' from FIT image at 0x%08lx\n",
|
|
|
|
confname, addr);
|
2008-03-12 09:33:00 +00:00
|
|
|
#endif
|
2002-10-20 17:17:16 +00:00
|
|
|
} else {
|
2021-07-24 15:03:29 +00:00
|
|
|
addr = hextoul(argv[1], NULL);
|
2020-05-10 17:40:04 +00:00
|
|
|
debug("* source: 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);
|
2022-12-12 19:12:11 +00:00
|
|
|
rcode = image_source_script(addr, fit_uname, confname);
|
2002-10-20 17:17:16 +00:00
|
|
|
return rcode;
|
|
|
|
}
|
2003-06-27 21:31:46 +00:00
|
|
|
|
2012-10-29 13:34:31 +00:00
|
|
|
#ifdef CONFIG_SYS_LONGHELP
|
|
|
|
static char source_help_text[] =
|
2008-03-12 09:33:00 +00:00
|
|
|
#if defined(CONFIG_FIT)
|
2022-12-12 19:12:11 +00:00
|
|
|
"[<addr>][:[<image>]|#[<config>]]\n"
|
|
|
|
"\t- Run script starting at addr\n"
|
|
|
|
"\t- A FIT config name or subimage name may be specified with : or #\n"
|
|
|
|
"\t (like bootm). If the image or config name is omitted, the\n"
|
|
|
|
"\t default is used.";
|
|
|
|
#else
|
|
|
|
"[<addr>]\n"
|
|
|
|
"\t- Run script starting at addr";
|
2007-07-10 16:02:44 +00:00
|
|
|
#endif
|
2012-10-29 13:34:31 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
U_BOOT_CMD(
|
|
|
|
source, 2, 0, do_source,
|
|
|
|
"run script from memory", source_help_text
|
2008-03-12 09:33:00 +00:00
|
|
|
);
|
2007-07-10 16:02:44 +00:00
|
|
|
#endif
|