1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2001
4  * Kyle Harris, kharris@nexus-tech.net
5  */
6 
7 /*
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.
13  */
14 
15 /* #define DEBUG */
16 
17 #include <common.h>
18 #include <command.h>
19 #include <env.h>
20 #include <image.h>
21 #include <log.h>
22 #include <malloc.h>
23 #include <mapmem.h>
24 #include <asm/byteorder.h>
25 #include <asm/io.h>
26 
27 #if defined(CONFIG_FIT)
28 /**
29  * get_default_image() - Return default property from /images
30  *
31  * Return: Pointer to value of default property (or NULL)
32  */
get_default_image(const void * fit)33 static const char *get_default_image(const void *fit)
34 {
35 	int images_noffset;
36 
37 	images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
38 	if (images_noffset < 0)
39 		return NULL;
40 
41 	return fdt_getprop(fit, images_noffset, FIT_DEFAULT_PROP, NULL);
42 }
43 #endif
44 
image_source_script(ulong addr,const char * fit_uname)45 int image_source_script(ulong addr, const char *fit_uname)
46 {
47 	ulong		len;
48 #if defined(CONFIG_LEGACY_IMAGE_FORMAT)
49 	const image_header_t *hdr;
50 #endif
51 	u32		*data;
52 	int		verify;
53 	void *buf;
54 #if defined(CONFIG_FIT)
55 	const void*	fit_hdr;
56 	int		noffset;
57 	const void	*fit_data;
58 	size_t		fit_len;
59 #endif
60 
61 	verify = env_get_yesno("verify");
62 
63 	buf = map_sysmem(addr, 0);
64 	switch (genimg_get_format(buf)) {
65 #if defined(CONFIG_LEGACY_IMAGE_FORMAT)
66 	case IMAGE_FORMAT_LEGACY:
67 		hdr = buf;
68 
69 		if (!image_check_magic (hdr)) {
70 			puts ("Bad magic number\n");
71 			return 1;
72 		}
73 
74 		if (!image_check_hcrc (hdr)) {
75 			puts ("Bad header crc\n");
76 			return 1;
77 		}
78 
79 		if (verify) {
80 			if (!image_check_dcrc (hdr)) {
81 				puts ("Bad data crc\n");
82 				return 1;
83 			}
84 		}
85 
86 		if (!image_check_type (hdr, IH_TYPE_SCRIPT)) {
87 			puts ("Bad image type\n");
88 			return 1;
89 		}
90 
91 		/* get length of script */
92 		data = (u32 *)image_get_data (hdr);
93 
94 		if ((len = uimage_to_cpu (*data)) == 0) {
95 			puts ("Empty Script\n");
96 			return 1;
97 		}
98 
99 		/*
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
103 		 */
104 		while (*data++);
105 		break;
106 #endif
107 #if defined(CONFIG_FIT)
108 	case IMAGE_FORMAT_FIT:
109 		fit_hdr = buf;
110 		if (fit_check_format(fit_hdr, IMAGE_SIZE_INVAL)) {
111 			puts ("Bad FIT image format\n");
112 			return 1;
113 		}
114 
115 		if (!fit_uname)
116 			fit_uname = get_default_image(fit_hdr);
117 
118 		if (!fit_uname) {
119 			puts("No FIT subimage unit name\n");
120 			return 1;
121 		}
122 
123 		/* get script component image node offset */
124 		noffset = fit_image_get_node (fit_hdr, fit_uname);
125 		if (noffset < 0) {
126 			printf ("Can't find '%s' FIT subimage\n", fit_uname);
127 			return 1;
128 		}
129 
130 		if (!fit_image_check_type (fit_hdr, noffset, IH_TYPE_SCRIPT)) {
131 			puts ("Not a image image\n");
132 			return 1;
133 		}
134 
135 		/* verify integrity */
136 		if (verify) {
137 			if (!fit_image_verify(fit_hdr, noffset)) {
138 				puts ("Bad Data Hash\n");
139 				return 1;
140 			}
141 		}
142 
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");
146 			return 1;
147 		}
148 
149 		data = (u32 *)fit_data;
150 		len = (ulong)fit_len;
151 		break;
152 #endif
153 	default:
154 		puts ("Wrong image format for \"source\" command\n");
155 		return 1;
156 	}
157 
158 	debug("** Script length: %ld\n", len);
159 	return run_command_list((char *)data, len, 0);
160 }
161 
162 /**************************************************/
163 #if defined(CONFIG_CMD_SOURCE)
do_source(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])164 static int do_source(struct cmd_tbl *cmdtp, int flag, int argc,
165 		     char *const argv[])
166 {
167 	ulong addr;
168 	int rcode;
169 	const char *fit_uname = NULL;
170 
171 	/* Find script image */
172 	if (argc < 2) {
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,
177 				      &fit_uname)) {
178 		debug("*  source: subimage '%s' from FIT image at 0x%08lx\n",
179 		      fit_uname, addr);
180 #endif
181 	} else {
182 		addr = simple_strtoul(argv[1], NULL, 16);
183 		debug("*  source: cmdline image address = 0x%08lx\n", addr);
184 	}
185 
186 	printf ("## Executing script at %08lx\n", addr);
187 	rcode = image_source_script(addr, fit_uname);
188 	return rcode;
189 }
190 
191 #ifdef CONFIG_SYS_LONGHELP
192 static char source_help_text[] =
193 	"[addr]\n"
194 	"\t- run script starting at addr\n"
195 	"\t- A valid image header must be present"
196 #if defined(CONFIG_FIT)
197 	"\n"
198 	"For FIT format uImage addr must include subimage\n"
199 	"unit name in the form of addr:<subimg_uname>"
200 #endif
201 	"";
202 #endif
203 
204 U_BOOT_CMD(
205 	source, 2, 0,	do_source,
206 	"run script from memory", source_help_text
207 );
208 #endif
209