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.
23 #include <asm/byteorder.h>
26 #if defined(CONFIG_FIT)
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);
45 source (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(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
168 const char *fit_uname = NULL;
170 /* Find script image */
172 addr = CONFIG_SYS_LOAD_ADDR;
173 debug ("* source: default load address = 0x%08lx\n", addr);
174 #if defined(CONFIG_FIT)
175 } else if (fit_parse_subimage (argv[1], load_addr, &addr, &fit_uname)) {
176 debug ("* source: subimage '%s' from FIT image at 0x%08lx\n",
180 addr = simple_strtoul(argv[1], NULL, 16);
181 debug ("* source: cmdline image address = 0x%08lx\n", addr);
184 printf ("## Executing script at %08lx\n", addr);
185 rcode = source (addr, fit_uname);
189 #ifdef CONFIG_SYS_LONGHELP
190 static char source_help_text[] =
192 "\t- run script starting at addr\n"
193 "\t- A valid image header must be present"
194 #if defined(CONFIG_FIT)
196 "For FIT format uImage addr must include subimage\n"
197 "unit name in the form of addr:<subimg_uname>"
203 source, 2, 0, do_source,
204 "run script from memory", source_help_text