1 // SPDX-License-Identifier: GPL-2.0+
8 * The "source" command allows to define "script images", i. e. files
9 * that contain command sequences that can be executed by the command
10 * interpreter. It returns the exit status of the last command
11 * executed from the script. This is very similar to running a shell
12 * script in a UNIX shell, hence the name for the command.
24 #include <asm/byteorder.h>
28 * get_default_image() - Return default property from /images
30 * Return: Pointer to value of default property (or NULL)
32 static const char *get_default_image(const void *fit)
36 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
37 if (images_noffset < 0)
40 return fdt_getprop(fit, images_noffset, FIT_DEFAULT_PROP, NULL);
43 int image_locate_script(void *buf, int size, const char *fit_uname,
44 const char *confname, char **datap, uint *lenp)
47 const struct legacy_img_hdr *hdr;
55 verify = env_get_yesno("verify");
57 switch (genimg_get_format(buf)) {
58 case IMAGE_FORMAT_LEGACY:
59 if (IS_ENABLED(CONFIG_LEGACY_IMAGE_FORMAT)) {
62 if (!image_check_magic(hdr)) {
63 puts("Bad magic number\n");
67 if (!image_check_hcrc(hdr)) {
68 puts("Bad header crc\n");
73 if (!image_check_dcrc(hdr)) {
74 puts("Bad data crc\n");
79 if (!image_check_type(hdr, IH_TYPE_SCRIPT)) {
80 puts("Bad image type\n");
84 /* get length of script */
85 data = (u32 *)image_get_data(hdr);
87 len = uimage_to_cpu(*data);
89 puts("Empty Script\n");
94 * scripts are just multi-image files with one
95 * component, so seek past the zero-terminated sequence
96 * of image lengths to get to the actual image data
101 case IMAGE_FORMAT_FIT:
102 if (IS_ENABLED(CONFIG_FIT)) {
104 if (fit_check_format(fit_hdr, IMAGE_SIZE_INVAL)) {
105 puts("Bad FIT image format\n");
110 /* If confname is empty, use the default */
111 if (confname && *confname)
112 noffset = fit_conf_get_node(fit_hdr, confname);
114 noffset = fit_conf_get_node(fit_hdr, NULL);
118 printf("Could not find config %s\n", confname);
122 if (verify && fit_config_verify(fit_hdr, noffset))
125 noffset = fit_conf_get_prop_node(fit_hdr,
132 printf("Could not find script in %s\n", confname);
137 if (!fit_uname || !*fit_uname)
138 fit_uname = get_default_image(fit_hdr);
140 puts("No FIT subimage unit name\n");
144 /* get script component image node offset */
145 noffset = fit_image_get_node(fit_hdr, fit_uname);
147 printf("Can't find '%s' FIT subimage\n",
153 if (!fit_image_check_type(fit_hdr, noffset,
155 puts("Not a image image\n");
159 /* verify integrity */
160 if (verify && !fit_image_verify(fit_hdr, noffset)) {
161 puts("Bad Data Hash\n");
165 /* get script subimage data address and length */
166 if (fit_image_get_data(fit_hdr, noffset, &fit_data, &fit_len)) {
167 puts("Could not find script subimage data\n");
171 data = (u32 *)fit_data;
172 len = (ulong)fit_len;
176 puts("Wrong image format for \"source\" command\n");
180 *datap = (char *)data;
186 int image_source_script(ulong addr, const char *fit_uname, const char *confname)
193 buf = map_sysmem(addr, 0);
194 ret = image_locate_script(buf, 0, fit_uname, confname, &data, &len);
197 return CMD_RET_FAILURE;
199 debug("** Script length: %d\n", len);
200 return run_command_list(data, len, 0);
203 /**************************************************/
204 #if defined(CONFIG_CMD_SOURCE)
205 static int do_source(struct cmd_tbl *cmdtp, int flag, int argc,
210 const char *fit_uname = NULL, *confname = NULL;
212 /* Find script image */
214 addr = CONFIG_SYS_LOAD_ADDR;
215 debug("* source: default load address = 0x%08lx\n", addr);
216 #if defined(CONFIG_FIT)
217 } else if (fit_parse_subimage(argv[1], image_load_addr, &addr,
219 debug("* source: subimage '%s' from FIT image at 0x%08lx\n",
221 } else if (fit_parse_conf(argv[1], image_load_addr, &addr, &confname)) {
222 debug("* source: config '%s' from FIT image at 0x%08lx\n",
226 addr = hextoul(argv[1], NULL);
227 debug("* source: cmdline image address = 0x%08lx\n", addr);
230 printf ("## Executing script at %08lx\n", addr);
231 rcode = image_source_script(addr, fit_uname, confname);
235 #ifdef CONFIG_SYS_LONGHELP
236 static char source_help_text[] =
237 #if defined(CONFIG_FIT)
238 "[<addr>][:[<image>]|#[<config>]]\n"
239 "\t- Run script starting at addr\n"
240 "\t- A FIT config name or subimage name may be specified with : or #\n"
241 "\t (like bootm). If the image or config name is omitted, the\n"
242 "\t default is used.";
245 "\t- Run script starting at addr";
250 source, 2, 0, do_source,
251 "run script from memory", source_help_text