Blame view

cmd/source.c 4.51 KB
83d290c56   Tom Rini   SPDX: Convert all...
1
  // SPDX-License-Identifier: GPL-2.0+
d0dd10777   wdenk   Initial revision
2
3
4
  /*
   * (C) Copyright 2001
   * Kyle Harris, kharris@nexus-tech.net
d0dd10777   wdenk   Initial revision
5
6
7
   */
  
  /*
74de7aefd   Wolfgang Denk   Add "source" comm...
8
9
10
11
12
   * The "source" command allows to define "script images", i. e. files
   * that contain command sequences that can be executed by the command
   * interpreter. It returns the exit status of the last command
   * executed from the script. This is very similar to running a shell
   * script in a UNIX shell, hence the name for the command.
d0dd10777   wdenk   Initial revision
13
14
15
16
17
18
   */
  
  /* #define DEBUG */
  
  #include <common.h>
  #include <command.h>
6bf6dbee0   Simon Glass   env: Move env_get...
19
  #include <env.h>
d0dd10777   wdenk   Initial revision
20
21
  #include <image.h>
  #include <malloc.h>
0eb25b619   Joe Hershberger   common: Make sure...
22
  #include <mapmem.h>
d0dd10777   wdenk   Initial revision
23
  #include <asm/byteorder.h>
4ca30d602   Simon Glass   sandbox: Support ...
24
  #include <asm/io.h>
d0dd10777   wdenk   Initial revision
25

201d9cd2b   Alex Kiernan   cmd: fit_image: A...
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
  #if defined(CONFIG_FIT)
  /**
   * get_default_image() - Return default property from /images
   *
   * Return: Pointer to value of default property (or NULL)
   */
  static const char *get_default_image(const void *fit)
  {
  	int images_noffset;
  
  	images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
  	if (images_noffset < 0)
  		return NULL;
  
  	return fdt_getprop(fit, images_noffset, FIT_DEFAULT_PROP, NULL);
  }
  #endif
220a3a44a   Simon Glass   common: Rename an...
43
  int image_source_script(ulong addr, const char *fit_uname)
d0dd10777   wdenk   Initial revision
44
  {
53677ef18   Wolfgang Denk   Big white-space c...
45
  	ulong		len;
c76c93a3d   Tom Rini   configs: Rename C...
46
  #if defined(CONFIG_LEGACY_IMAGE_FORMAT)
4ca30d602   Simon Glass   sandbox: Support ...
47
  	const image_header_t *hdr;
21d29f7f9   Heiko Schocher   bootm: make use o...
48
  #endif
210fbee90   Gong Qianyu   common/cmd_source...
49
  	u32		*data;
424c4abdd   Marian Balakowicz   [new uImage] Add ...
50
  	int		verify;
4ca30d602   Simon Glass   sandbox: Support ...
51
  	void *buf;
424c4abdd   Marian Balakowicz   [new uImage] Add ...
52
53
54
55
56
57
  #if defined(CONFIG_FIT)
  	const void*	fit_hdr;
  	int		noffset;
  	const void	*fit_data;
  	size_t		fit_len;
  #endif
d0dd10777   wdenk   Initial revision
58

bfebc8c96   Simon Glass   env: Rename geten...
59
  	verify = env_get_yesno("verify");
d0dd10777   wdenk   Initial revision
60

4ca30d602   Simon Glass   sandbox: Support ...
61
62
  	buf = map_sysmem(addr, 0);
  	switch (genimg_get_format(buf)) {
c76c93a3d   Tom Rini   configs: Rename C...
63
  #if defined(CONFIG_LEGACY_IMAGE_FORMAT)
d5934ad77   Marian Balakowicz   [new uImage] Add ...
64
  	case IMAGE_FORMAT_LEGACY:
4ca30d602   Simon Glass   sandbox: Support ...
65
  		hdr = buf;
d0dd10777   wdenk   Initial revision
66

d5934ad77   Marian Balakowicz   [new uImage] Add ...
67
68
69
70
71
  		if (!image_check_magic (hdr)) {
  			puts ("Bad magic number
  ");
  			return 1;
  		}
d0dd10777   wdenk   Initial revision
72

d5934ad77   Marian Balakowicz   [new uImage] Add ...
73
74
75
  		if (!image_check_hcrc (hdr)) {
  			puts ("Bad header crc
  ");
d0dd10777   wdenk   Initial revision
76
77
  			return 1;
  		}
d0dd10777   wdenk   Initial revision
78

d5934ad77   Marian Balakowicz   [new uImage] Add ...
79
80
81
82
83
84
85
86
87
88
89
90
91
  		if (verify) {
  			if (!image_check_dcrc (hdr)) {
  				puts ("Bad data crc
  ");
  				return 1;
  			}
  		}
  
  		if (!image_check_type (hdr, IH_TYPE_SCRIPT)) {
  			puts ("Bad image type
  ");
  			return 1;
  		}
d0dd10777   wdenk   Initial revision
92

d5934ad77   Marian Balakowicz   [new uImage] Add ...
93
  		/* get length of script */
210fbee90   Gong Qianyu   common/cmd_source...
94
  		data = (u32 *)image_get_data (hdr);
d0dd10777   wdenk   Initial revision
95

9a4daad0a   Marian Balakowicz   [new uImage] Upda...
96
  		if ((len = uimage_to_cpu (*data)) == 0) {
d5934ad77   Marian Balakowicz   [new uImage] Add ...
97
98
99
100
  			puts ("Empty Script
  ");
  			return 1;
  		}
36cc8cbb3   Bartlomiej Sieka   [new uImage] Fix ...
101
102
103
104
105
106
107
  
  		/*
  		 * scripts are just multi-image files with one component, seek
  		 * past the zero-terminated sequence of image lengths to get
  		 * to the actual image data
  		 */
  		while (*data++);
d5934ad77   Marian Balakowicz   [new uImage] Add ...
108
  		break;
21d29f7f9   Heiko Schocher   bootm: make use o...
109
  #endif
d5934ad77   Marian Balakowicz   [new uImage] Add ...
110
111
  #if defined(CONFIG_FIT)
  	case IMAGE_FORMAT_FIT:
4ca30d602   Simon Glass   sandbox: Support ...
112
  		fit_hdr = buf;
424c4abdd   Marian Balakowicz   [new uImage] Add ...
113
114
115
116
117
  		if (!fit_check_format (fit_hdr)) {
  			puts ("Bad FIT image format
  ");
  			return 1;
  		}
201d9cd2b   Alex Kiernan   cmd: fit_image: A...
118
119
120
121
122
123
124
125
  		if (!fit_uname)
  			fit_uname = get_default_image(fit_hdr);
  
  		if (!fit_uname) {
  			puts("No FIT subimage unit name
  ");
  			return 1;
  		}
424c4abdd   Marian Balakowicz   [new uImage] Add ...
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
  		/* get script component image node offset */
  		noffset = fit_image_get_node (fit_hdr, fit_uname);
  		if (noffset < 0) {
  			printf ("Can't find '%s' FIT subimage
  ", fit_uname);
  			return 1;
  		}
  
  		if (!fit_image_check_type (fit_hdr, noffset, IH_TYPE_SCRIPT)) {
  			puts ("Not a image image
  ");
  			return 1;
  		}
  
  		/* verify integrity */
  		if (verify) {
b8da83665   Simon Glass   image: Rename fit...
142
  			if (!fit_image_verify(fit_hdr, noffset)) {
424c4abdd   Marian Balakowicz   [new uImage] Add ...
143
144
145
146
147
148
149
150
151
152
153
154
  				puts ("Bad Data Hash
  ");
  				return 1;
  			}
  		}
  
  		/* get script subimage data address and length */
  		if (fit_image_get_data (fit_hdr, noffset, &fit_data, &fit_len)) {
  			puts ("Could not find script subimage data
  ");
  			return 1;
  		}
210fbee90   Gong Qianyu   common/cmd_source...
155
  		data = (u32 *)fit_data;
424c4abdd   Marian Balakowicz   [new uImage] Add ...
156
157
  		len = (ulong)fit_len;
  		break;
d5934ad77   Marian Balakowicz   [new uImage] Add ...
158
159
  #endif
  	default:
74de7aefd   Wolfgang Denk   Add "source" comm...
160
161
  		puts ("Wrong image format for \"source\" command
  ");
d0dd10777   wdenk   Initial revision
162
163
  		return 1;
  	}
699b13a60   wdenk   * Fix mdelay() on...
164
165
  	debug ("** Script length: %ld
  ", len);
d51004a83   Simon Glass   Add run_command_l...
166
  	return run_command_list((char *)data, len, 0);
d0dd10777   wdenk   Initial revision
167
  }
8bde7f776   wdenk   * Code cleanup:
168
  /**************************************************/
74de7aefd   Wolfgang Denk   Add "source" comm...
169
  #if defined(CONFIG_CMD_SOURCE)
0e350f81e   Jeroen Hofstee   common: commands:...
170
  static int do_source(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
d0dd10777   wdenk   Initial revision
171
172
173
  {
  	ulong addr;
  	int rcode;
424c4abdd   Marian Balakowicz   [new uImage] Add ...
174
  	const char *fit_uname = NULL;
d0dd10777   wdenk   Initial revision
175

424c4abdd   Marian Balakowicz   [new uImage] Add ...
176
  	/* Find script image */
d0dd10777   wdenk   Initial revision
177
  	if (argc < 2) {
6d0f6bcf3   Jean-Christophe PLAGNIOL-VILLARD   rename CFG_ macro...
178
  		addr = CONFIG_SYS_LOAD_ADDR;
74de7aefd   Wolfgang Denk   Add "source" comm...
179
180
  		debug ("*  source: default load address = 0x%08lx
  ", addr);
424c4abdd   Marian Balakowicz   [new uImage] Add ...
181
  #if defined(CONFIG_FIT)
bb872dd93   Simon Glass   image: Rename loa...
182
183
  	} else if (fit_parse_subimage(argv[1], image_load_addr, &addr,
  				      &fit_uname)) {
74de7aefd   Wolfgang Denk   Add "source" comm...
184
185
  		debug ("*  source: subimage '%s' from FIT image at 0x%08lx
  ",
424c4abdd   Marian Balakowicz   [new uImage] Add ...
186
187
  				fit_uname, addr);
  #endif
d0dd10777   wdenk   Initial revision
188
  	} else {
424c4abdd   Marian Balakowicz   [new uImage] Add ...
189
  		addr = simple_strtoul(argv[1], NULL, 16);
74de7aefd   Wolfgang Denk   Add "source" comm...
190
191
  		debug ("*  source: cmdline image address = 0x%08lx
  ", addr);
d0dd10777   wdenk   Initial revision
192
  	}
424c4abdd   Marian Balakowicz   [new uImage] Add ...
193
194
  	printf ("## Executing script at %08lx
  ", addr);
220a3a44a   Simon Glass   common: Rename an...
195
  	rcode = image_source_script(addr, fit_uname);
d0dd10777   wdenk   Initial revision
196
197
  	return rcode;
  }
8bde7f776   wdenk   * Code cleanup:
198

088f1b199   Kim Phillips   common/cmd_*.c: s...
199
200
  #ifdef CONFIG_SYS_LONGHELP
  static char source_help_text[] =
74de7aefd   Wolfgang Denk   Add "source" comm...
201
202
203
204
  	"[addr]
  "
  	"\t- run script starting at addr
  "
a89c33db9   Wolfgang Denk   General help mess...
205
  	"\t- A valid image header must be present"
424c4abdd   Marian Balakowicz   [new uImage] Add ...
206
  #if defined(CONFIG_FIT)
a89c33db9   Wolfgang Denk   General help mess...
207
208
  	"
  "
424c4abdd   Marian Balakowicz   [new uImage] Add ...
209
210
  	"For FIT format uImage addr must include subimage
  "
a89c33db9   Wolfgang Denk   General help mess...
211
  	"unit name in the form of addr:<subimg_uname>"
902531788   Jon Loeliger   common/: Remove l...
212
  #endif
088f1b199   Kim Phillips   common/cmd_*.c: s...
213
214
215
216
217
218
  	"";
  #endif
  
  U_BOOT_CMD(
  	source, 2, 0,	do_source,
  	"run script from memory", source_help_text
424c4abdd   Marian Balakowicz   [new uImage] Add ...
219
  );
902531788   Jon Loeliger   common/: Remove l...
220
  #endif