1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * (C) Copyright 2001
3*4882a593Smuzhiyun * Kyle Harris, kharris@nexus-tech.net
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * SPDX-License-Identifier: GPL-2.0+
6*4882a593Smuzhiyun */
7*4882a593Smuzhiyun
8*4882a593Smuzhiyun /*
9*4882a593Smuzhiyun * The "source" command allows to define "script images", i. e. files
10*4882a593Smuzhiyun * that contain command sequences that can be executed by the command
11*4882a593Smuzhiyun * interpreter. It returns the exit status of the last command
12*4882a593Smuzhiyun * executed from the script. This is very similar to running a shell
13*4882a593Smuzhiyun * script in a UNIX shell, hence the name for the command.
14*4882a593Smuzhiyun */
15*4882a593Smuzhiyun
16*4882a593Smuzhiyun /* #define DEBUG */
17*4882a593Smuzhiyun
18*4882a593Smuzhiyun #include <common.h>
19*4882a593Smuzhiyun #include <command.h>
20*4882a593Smuzhiyun #include <image.h>
21*4882a593Smuzhiyun #include <malloc.h>
22*4882a593Smuzhiyun #include <mapmem.h>
23*4882a593Smuzhiyun #include <asm/byteorder.h>
24*4882a593Smuzhiyun #include <asm/io.h>
25*4882a593Smuzhiyun
26*4882a593Smuzhiyun int
source(ulong addr,const char * fit_uname)27*4882a593Smuzhiyun source (ulong addr, const char *fit_uname)
28*4882a593Smuzhiyun {
29*4882a593Smuzhiyun ulong len;
30*4882a593Smuzhiyun #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
31*4882a593Smuzhiyun const image_header_t *hdr;
32*4882a593Smuzhiyun #endif
33*4882a593Smuzhiyun u32 *data;
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun void *buf;
36*4882a593Smuzhiyun #if defined(CONFIG_FIT)
37*4882a593Smuzhiyun const void* fit_hdr;
38*4882a593Smuzhiyun int noffset;
39*4882a593Smuzhiyun const void *fit_data;
40*4882a593Smuzhiyun size_t fit_len;
41*4882a593Smuzhiyun #endif
42*4882a593Smuzhiyun #if defined(CONFIG_IMAGE_FORMAT_LEGACY) || defined(CONFIG_FIT)
43*4882a593Smuzhiyun #ifdef CONFIG_FIT_SIGNATURE
44*4882a593Smuzhiyun int verify = 1;
45*4882a593Smuzhiyun #else
46*4882a593Smuzhiyun int verify = env_get_yesno("verify");
47*4882a593Smuzhiyun #endif
48*4882a593Smuzhiyun #endif
49*4882a593Smuzhiyun buf = map_sysmem(addr, 0);
50*4882a593Smuzhiyun switch (genimg_get_format(buf)) {
51*4882a593Smuzhiyun #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
52*4882a593Smuzhiyun case IMAGE_FORMAT_LEGACY:
53*4882a593Smuzhiyun hdr = buf;
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun if (!image_check_magic (hdr)) {
56*4882a593Smuzhiyun puts ("Bad magic number\n");
57*4882a593Smuzhiyun return 1;
58*4882a593Smuzhiyun }
59*4882a593Smuzhiyun
60*4882a593Smuzhiyun if (!image_check_hcrc (hdr)) {
61*4882a593Smuzhiyun puts ("Bad header crc\n");
62*4882a593Smuzhiyun return 1;
63*4882a593Smuzhiyun }
64*4882a593Smuzhiyun
65*4882a593Smuzhiyun if (verify) {
66*4882a593Smuzhiyun if (!image_check_dcrc (hdr)) {
67*4882a593Smuzhiyun puts ("Bad data crc\n");
68*4882a593Smuzhiyun return 1;
69*4882a593Smuzhiyun }
70*4882a593Smuzhiyun }
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun if (!image_check_type (hdr, IH_TYPE_SCRIPT)) {
73*4882a593Smuzhiyun puts ("Bad image type\n");
74*4882a593Smuzhiyun return 1;
75*4882a593Smuzhiyun }
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun /* get length of script */
78*4882a593Smuzhiyun data = (u32 *)image_get_data (hdr);
79*4882a593Smuzhiyun
80*4882a593Smuzhiyun if ((len = uimage_to_cpu (*data)) == 0) {
81*4882a593Smuzhiyun puts ("Empty Script\n");
82*4882a593Smuzhiyun return 1;
83*4882a593Smuzhiyun }
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun /*
86*4882a593Smuzhiyun * scripts are just multi-image files with one component, seek
87*4882a593Smuzhiyun * past the zero-terminated sequence of image lengths to get
88*4882a593Smuzhiyun * to the actual image data
89*4882a593Smuzhiyun */
90*4882a593Smuzhiyun while (*data++ != IMAGE_PARAM_INVAL);
91*4882a593Smuzhiyun break;
92*4882a593Smuzhiyun #endif
93*4882a593Smuzhiyun #if defined(CONFIG_FIT)
94*4882a593Smuzhiyun case IMAGE_FORMAT_FIT:
95*4882a593Smuzhiyun if (fit_uname == NULL) {
96*4882a593Smuzhiyun puts ("No FIT subimage unit name\n");
97*4882a593Smuzhiyun return 1;
98*4882a593Smuzhiyun }
99*4882a593Smuzhiyun
100*4882a593Smuzhiyun fit_hdr = buf;
101*4882a593Smuzhiyun if (!fit_check_format (fit_hdr)) {
102*4882a593Smuzhiyun puts ("Bad FIT image format\n");
103*4882a593Smuzhiyun return 1;
104*4882a593Smuzhiyun }
105*4882a593Smuzhiyun
106*4882a593Smuzhiyun /* get script component image node offset */
107*4882a593Smuzhiyun noffset = fit_image_get_node (fit_hdr, fit_uname);
108*4882a593Smuzhiyun if (noffset < 0) {
109*4882a593Smuzhiyun printf ("Can't find '%s' FIT subimage\n", fit_uname);
110*4882a593Smuzhiyun return 1;
111*4882a593Smuzhiyun }
112*4882a593Smuzhiyun
113*4882a593Smuzhiyun if (!fit_image_check_type (fit_hdr, noffset, IH_TYPE_SCRIPT)) {
114*4882a593Smuzhiyun puts ("Not a image image\n");
115*4882a593Smuzhiyun return 1;
116*4882a593Smuzhiyun }
117*4882a593Smuzhiyun
118*4882a593Smuzhiyun /* verify integrity */
119*4882a593Smuzhiyun if (verify) {
120*4882a593Smuzhiyun #ifdef CONFIG_FIT_SIGNATURE
121*4882a593Smuzhiyun int conf_noffset;
122*4882a593Smuzhiyun
123*4882a593Smuzhiyun /* NULL for default conf */
124*4882a593Smuzhiyun conf_noffset = fit_conf_get_node(fit_hdr, NULL);
125*4882a593Smuzhiyun if (conf_noffset < 0)
126*4882a593Smuzhiyun return conf_noffset;
127*4882a593Smuzhiyun
128*4882a593Smuzhiyun if (fit_config_verify(fit_hdr, conf_noffset)) {
129*4882a593Smuzhiyun puts ("Bad Data Hash\n");
130*4882a593Smuzhiyun return 1;
131*4882a593Smuzhiyun }
132*4882a593Smuzhiyun #endif
133*4882a593Smuzhiyun if (!fit_image_verify(fit_hdr, noffset)) {
134*4882a593Smuzhiyun puts ("Bad Data Hash\n");
135*4882a593Smuzhiyun return 1;
136*4882a593Smuzhiyun }
137*4882a593Smuzhiyun }
138*4882a593Smuzhiyun
139*4882a593Smuzhiyun /* get script subimage data address and length */
140*4882a593Smuzhiyun if (fit_image_get_data (fit_hdr, noffset, &fit_data, &fit_len)) {
141*4882a593Smuzhiyun puts ("Could not find script subimage data\n");
142*4882a593Smuzhiyun return 1;
143*4882a593Smuzhiyun }
144*4882a593Smuzhiyun
145*4882a593Smuzhiyun data = (u32 *)fit_data;
146*4882a593Smuzhiyun len = (ulong)fit_len;
147*4882a593Smuzhiyun break;
148*4882a593Smuzhiyun #endif
149*4882a593Smuzhiyun default:
150*4882a593Smuzhiyun puts ("Wrong image format for \"source\" command\n");
151*4882a593Smuzhiyun return 1;
152*4882a593Smuzhiyun }
153*4882a593Smuzhiyun
154*4882a593Smuzhiyun debug ("** Script length: %ld\n", len);
155*4882a593Smuzhiyun return run_command_list((char *)data, len, 0);
156*4882a593Smuzhiyun }
157*4882a593Smuzhiyun
158*4882a593Smuzhiyun /**************************************************/
159*4882a593Smuzhiyun #if defined(CONFIG_CMD_SOURCE)
do_source(cmd_tbl_t * cmdtp,int flag,int argc,char * const argv[])160*4882a593Smuzhiyun static int do_source(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
161*4882a593Smuzhiyun {
162*4882a593Smuzhiyun ulong addr;
163*4882a593Smuzhiyun int rcode;
164*4882a593Smuzhiyun const char *fit_uname = NULL;
165*4882a593Smuzhiyun
166*4882a593Smuzhiyun /* Find script image */
167*4882a593Smuzhiyun if (argc < 2) {
168*4882a593Smuzhiyun addr = CONFIG_SYS_LOAD_ADDR;
169*4882a593Smuzhiyun debug ("* source: default load address = 0x%08lx\n", addr);
170*4882a593Smuzhiyun #if defined(CONFIG_FIT)
171*4882a593Smuzhiyun } else if (fit_parse_subimage (argv[1], load_addr, &addr, &fit_uname)) {
172*4882a593Smuzhiyun debug ("* source: subimage '%s' from FIT image at 0x%08lx\n",
173*4882a593Smuzhiyun fit_uname, addr);
174*4882a593Smuzhiyun #endif
175*4882a593Smuzhiyun } else {
176*4882a593Smuzhiyun addr = simple_strtoul(argv[1], NULL, 16);
177*4882a593Smuzhiyun debug ("* source: cmdline image address = 0x%08lx\n", addr);
178*4882a593Smuzhiyun }
179*4882a593Smuzhiyun
180*4882a593Smuzhiyun printf ("## Executing script at %08lx\n", addr);
181*4882a593Smuzhiyun rcode = source (addr, fit_uname);
182*4882a593Smuzhiyun return rcode;
183*4882a593Smuzhiyun }
184*4882a593Smuzhiyun
185*4882a593Smuzhiyun #ifdef CONFIG_SYS_LONGHELP
186*4882a593Smuzhiyun static char source_help_text[] =
187*4882a593Smuzhiyun "[addr]\n"
188*4882a593Smuzhiyun "\t- run script starting at addr\n"
189*4882a593Smuzhiyun "\t- A valid image header must be present"
190*4882a593Smuzhiyun #if defined(CONFIG_FIT)
191*4882a593Smuzhiyun "\n"
192*4882a593Smuzhiyun "For FIT format uImage addr must include subimage\n"
193*4882a593Smuzhiyun "unit name in the form of addr:<subimg_uname>"
194*4882a593Smuzhiyun #endif
195*4882a593Smuzhiyun "";
196*4882a593Smuzhiyun #endif
197*4882a593Smuzhiyun
198*4882a593Smuzhiyun U_BOOT_CMD(
199*4882a593Smuzhiyun source, 2, 0, do_source,
200*4882a593Smuzhiyun "run script from memory", source_help_text
201*4882a593Smuzhiyun );
202*4882a593Smuzhiyun #endif
203