Blame view

cmd/source.c 4.01 KB
d0dd10777   wdenk   Initial revision
1
2
3
4
  /*
   * (C) Copyright 2001
   * Kyle Harris, kharris@nexus-tech.net
   *
1a4596601   Wolfgang Denk   Add GPL-2.0+ SPDX...
5
   * SPDX-License-Identifier:	GPL-2.0+
d0dd10777   wdenk   Initial revision
6
7
8
   */
  
  /*
74de7aefd   Wolfgang Denk   Add "source" comm...
9
10
11
12
13
   * 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
14
15
16
17
18
19
20
21
   */
  
  /* #define DEBUG */
  
  #include <common.h>
  #include <command.h>
  #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

d0dd10777   wdenk   Initial revision
26
  int
74de7aefd   Wolfgang Denk   Add "source" comm...
27
  source (ulong addr, const char *fit_uname)
d0dd10777   wdenk   Initial revision
28
  {
53677ef18   Wolfgang Denk   Big white-space c...
29
  	ulong		len;
21d29f7f9   Heiko Schocher   bootm: make use o...
30
  #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
4ca30d602   Simon Glass   sandbox: Support ...
31
  	const image_header_t *hdr;
21d29f7f9   Heiko Schocher   bootm: make use o...
32
  #endif
210fbee90   Gong Qianyu   common/cmd_source...
33
  	u32		*data;
424c4abdd   Marian Balakowicz   [new uImage] Add ...
34
  	int		verify;
4ca30d602   Simon Glass   sandbox: Support ...
35
  	void *buf;
424c4abdd   Marian Balakowicz   [new uImage] Add ...
36
37
38
39
40
41
  #if defined(CONFIG_FIT)
  	const void*	fit_hdr;
  	int		noffset;
  	const void	*fit_data;
  	size_t		fit_len;
  #endif
d0dd10777   wdenk   Initial revision
42

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

4ca30d602   Simon Glass   sandbox: Support ...
45
46
  	buf = map_sysmem(addr, 0);
  	switch (genimg_get_format(buf)) {
21d29f7f9   Heiko Schocher   bootm: make use o...
47
  #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
d5934ad77   Marian Balakowicz   [new uImage] Add ...
48
  	case IMAGE_FORMAT_LEGACY:
4ca30d602   Simon Glass   sandbox: Support ...
49
  		hdr = buf;
d0dd10777   wdenk   Initial revision
50

d5934ad77   Marian Balakowicz   [new uImage] Add ...
51
52
53
54
55
  		if (!image_check_magic (hdr)) {
  			puts ("Bad magic number
  ");
  			return 1;
  		}
d0dd10777   wdenk   Initial revision
56

d5934ad77   Marian Balakowicz   [new uImage] Add ...
57
58
59
  		if (!image_check_hcrc (hdr)) {
  			puts ("Bad header crc
  ");
d0dd10777   wdenk   Initial revision
60
61
  			return 1;
  		}
d0dd10777   wdenk   Initial revision
62

d5934ad77   Marian Balakowicz   [new uImage] Add ...
63
64
65
66
67
68
69
70
71
72
73
74
75
  		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
76

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

9a4daad0a   Marian Balakowicz   [new uImage] Upda...
80
  		if ((len = uimage_to_cpu (*data)) == 0) {
d5934ad77   Marian Balakowicz   [new uImage] Add ...
81
82
83
84
  			puts ("Empty Script
  ");
  			return 1;
  		}
36cc8cbb3   Bartlomiej Sieka   [new uImage] Fix ...
85
86
87
88
89
90
91
  
  		/*
  		 * 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 ...
92
  		break;
21d29f7f9   Heiko Schocher   bootm: make use o...
93
  #endif
d5934ad77   Marian Balakowicz   [new uImage] Add ...
94
95
  #if defined(CONFIG_FIT)
  	case IMAGE_FORMAT_FIT:
424c4abdd   Marian Balakowicz   [new uImage] Add ...
96
97
98
99
100
  		if (fit_uname == NULL) {
  			puts ("No FIT subimage unit name
  ");
  			return 1;
  		}
4ca30d602   Simon Glass   sandbox: Support ...
101
  		fit_hdr = buf;
424c4abdd   Marian Balakowicz   [new uImage] Add ...
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
  		if (!fit_check_format (fit_hdr)) {
  			puts ("Bad FIT image format
  ");
  			return 1;
  		}
  
  		/* 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...
124
  			if (!fit_image_verify(fit_hdr, noffset)) {
424c4abdd   Marian Balakowicz   [new uImage] Add ...
125
126
127
128
129
130
131
132
133
134
135
136
  				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...
137
  		data = (u32 *)fit_data;
424c4abdd   Marian Balakowicz   [new uImage] Add ...
138
139
  		len = (ulong)fit_len;
  		break;
d5934ad77   Marian Balakowicz   [new uImage] Add ...
140
141
  #endif
  	default:
74de7aefd   Wolfgang Denk   Add "source" comm...
142
143
  		puts ("Wrong image format for \"source\" command
  ");
d0dd10777   wdenk   Initial revision
144
145
  		return 1;
  	}
699b13a60   wdenk   * Fix mdelay() on...
146
147
  	debug ("** Script length: %ld
  ", len);
d51004a83   Simon Glass   Add run_command_l...
148
  	return run_command_list((char *)data, len, 0);
d0dd10777   wdenk   Initial revision
149
  }
8bde7f776   wdenk   * Code cleanup:
150
  /**************************************************/
74de7aefd   Wolfgang Denk   Add "source" comm...
151
  #if defined(CONFIG_CMD_SOURCE)
0e350f81e   Jeroen Hofstee   common: commands:...
152
  static int do_source(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
d0dd10777   wdenk   Initial revision
153
154
155
  {
  	ulong addr;
  	int rcode;
424c4abdd   Marian Balakowicz   [new uImage] Add ...
156
  	const char *fit_uname = NULL;
d0dd10777   wdenk   Initial revision
157

424c4abdd   Marian Balakowicz   [new uImage] Add ...
158
  	/* Find script image */
d0dd10777   wdenk   Initial revision
159
  	if (argc < 2) {
6d0f6bcf3   Jean-Christophe PLAGNIOL-VILLARD   rename CFG_ macro...
160
  		addr = CONFIG_SYS_LOAD_ADDR;
74de7aefd   Wolfgang Denk   Add "source" comm...
161
162
  		debug ("*  source: default load address = 0x%08lx
  ", addr);
424c4abdd   Marian Balakowicz   [new uImage] Add ...
163
164
  #if defined(CONFIG_FIT)
  	} else if (fit_parse_subimage (argv[1], load_addr, &addr, &fit_uname)) {
74de7aefd   Wolfgang Denk   Add "source" comm...
165
166
  		debug ("*  source: subimage '%s' from FIT image at 0x%08lx
  ",
424c4abdd   Marian Balakowicz   [new uImage] Add ...
167
168
  				fit_uname, addr);
  #endif
d0dd10777   wdenk   Initial revision
169
  	} else {
424c4abdd   Marian Balakowicz   [new uImage] Add ...
170
  		addr = simple_strtoul(argv[1], NULL, 16);
74de7aefd   Wolfgang Denk   Add "source" comm...
171
172
  		debug ("*  source: cmdline image address = 0x%08lx
  ", addr);
d0dd10777   wdenk   Initial revision
173
  	}
424c4abdd   Marian Balakowicz   [new uImage] Add ...
174
175
  	printf ("## Executing script at %08lx
  ", addr);
74de7aefd   Wolfgang Denk   Add "source" comm...
176
  	rcode = source (addr, fit_uname);
d0dd10777   wdenk   Initial revision
177
178
  	return rcode;
  }
8bde7f776   wdenk   * Code cleanup:
179

088f1b199   Kim Phillips   common/cmd_*.c: s...
180
181
  #ifdef CONFIG_SYS_LONGHELP
  static char source_help_text[] =
74de7aefd   Wolfgang Denk   Add "source" comm...
182
183
184
185
  	"[addr]
  "
  	"\t- run script starting at addr
  "
a89c33db9   Wolfgang Denk   General help mess...
186
  	"\t- A valid image header must be present"
424c4abdd   Marian Balakowicz   [new uImage] Add ...
187
  #if defined(CONFIG_FIT)
a89c33db9   Wolfgang Denk   General help mess...
188
189
  	"
  "
424c4abdd   Marian Balakowicz   [new uImage] Add ...
190
191
  	"For FIT format uImage addr must include subimage
  "
a89c33db9   Wolfgang Denk   General help mess...
192
  	"unit name in the form of addr:<subimg_uname>"
902531788   Jon Loeliger   common/: Remove l...
193
  #endif
088f1b199   Kim Phillips   common/cmd_*.c: s...
194
195
196
197
198
199
  	"";
  #endif
  
  U_BOOT_CMD(
  	source, 2, 0,	do_source,
  	"run script from memory", source_help_text
424c4abdd   Marian Balakowicz   [new uImage] Add ...
200
  );
902531788   Jon Loeliger   common/: Remove l...
201
  #endif