1 // SPDX-License-Identifier: GPL-2.0+
4 * Kyle Harris, kharris@nexus-tech.net
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>
27 #if defined(CONFIG_FIT)
29 * get_default_image() - Return default property from /images
31 * Return: Pointer to value of default property (or NULL)
33 static const char *get_default_image(const void *fit)
37 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
38 if (images_noffset < 0)
41 return fdt_getprop(fit, images_noffset, FIT_DEFAULT_PROP, NULL);
45 int image_source_script(ulong addr, const char *fit_uname)
48 #if defined(CONFIG_LEGACY_IMAGE_FORMAT)
49 const image_header_t *hdr;
54 #if defined(CONFIG_FIT)
61 verify = env_get_yesno("verify");
63 buf = map_sysmem(addr, 0);
64 switch (genimg_get_format(buf)) {
65 #if defined(CONFIG_LEGACY_IMAGE_FORMAT)
66 case IMAGE_FORMAT_LEGACY:
69 if (!image_check_magic (hdr)) {
70 puts ("Bad magic number\n");
74 if (!image_check_hcrc (hdr)) {
75 puts ("Bad header crc\n");
80 if (!image_check_dcrc (hdr)) {
81 puts ("Bad data crc\n");
86 if (!image_check_type (hdr, IH_TYPE_SCRIPT)) {
87 puts ("Bad image type\n");
91 /* get length of script */
92 data = (u32 *)image_get_data (hdr);
94 if ((len = uimage_to_cpu (*data)) == 0) {
95 puts ("Empty Script\n");
100 * scripts are just multi-image files with one component, seek
101 * past the zero-terminated sequence of image lengths to get
102 * to the actual image data
107 #if defined(CONFIG_FIT)
108 case IMAGE_FORMAT_FIT:
110 if (!fit_check_format (fit_hdr)) {
111 puts ("Bad FIT image format\n");
116 fit_uname = get_default_image(fit_hdr);
119 puts("No FIT subimage unit name\n");
123 /* get script component image node offset */
124 noffset = fit_image_get_node (fit_hdr, fit_uname);
126 printf ("Can't find '%s' FIT subimage\n", fit_uname);
130 if (!fit_image_check_type (fit_hdr, noffset, IH_TYPE_SCRIPT)) {
131 puts ("Not a image image\n");
135 /* verify integrity */
137 if (!fit_image_verify(fit_hdr, noffset)) {
138 puts ("Bad Data Hash\n");
143 /* get script subimage data address and length */
144 if (fit_image_get_data (fit_hdr, noffset, &fit_data, &fit_len)) {
145 puts ("Could not find script subimage data\n");
149 data = (u32 *)fit_data;
150 len = (ulong)fit_len;
154 puts ("Wrong image format for \"source\" command\n");
158 debug("** Script length: %ld\n", len);
159 return run_command_list((char *)data, len, 0);
162 /**************************************************/
163 #if defined(CONFIG_CMD_SOURCE)
164 static int do_source(struct cmd_tbl *cmdtp, int flag, int argc,
169 const char *fit_uname = NULL;
171 /* Find script image */
173 addr = CONFIG_SYS_LOAD_ADDR;
174 debug("* source: default load address = 0x%08lx\n", addr);
175 #if defined(CONFIG_FIT)
176 } else if (fit_parse_subimage(argv[1], image_load_addr, &addr,
178 debug("* source: subimage '%s' from FIT image at 0x%08lx\n",
182 addr = simple_strtoul(argv[1], NULL, 16);
183 debug("* source: cmdline image address = 0x%08lx\n", addr);
186 printf ("## Executing script at %08lx\n", addr);
187 rcode = image_source_script(addr, fit_uname);
191 #ifdef CONFIG_SYS_LONGHELP
192 static char source_help_text[] =
194 "\t- run script starting at addr\n"
195 "\t- A valid image header must be present"
196 #if defined(CONFIG_FIT)
198 "For FIT format uImage addr must include subimage\n"
199 "unit name in the form of addr:<subimg_uname>"
205 source, 2, 0, do_source,
206 "run script from memory", source_help_text