Blame view

common/cmd_source.c 3.95 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
22
   */
  
  /* #define DEBUG */
  
  #include <common.h>
  #include <command.h>
  #include <image.h>
  #include <malloc.h>
  #include <asm/byteorder.h>
4ca30d602   Simon Glass   sandbox: Support ...
23
  #include <asm/io.h>
d0dd10777   wdenk   Initial revision
24
25
26
  #if defined(CONFIG_8xx)
  #include <mpc8xx.h>
  #endif
d0dd10777   wdenk   Initial revision
27

d0dd10777   wdenk   Initial revision
28
  int
74de7aefd   Wolfgang Denk   Add "source" comm...
29
  source (ulong addr, const char *fit_uname)
d0dd10777   wdenk   Initial revision
30
  {
53677ef18   Wolfgang Denk   Big white-space c...
31
  	ulong		len;
4ca30d602   Simon Glass   sandbox: Support ...
32
  	const image_header_t *hdr;
424c4abdd   Marian Balakowicz   [new uImage] Add ...
33
  	ulong		*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

edbed247a   Bartlomiej Sieka   Memory footprint ...
43
  	verify = getenv_yesno ("verify");
d0dd10777   wdenk   Initial revision
44

4ca30d602   Simon Glass   sandbox: Support ...
45
46
  	buf = map_sysmem(addr, 0);
  	switch (genimg_get_format(buf)) {
d5934ad77   Marian Balakowicz   [new uImage] Add ...
47
  	case IMAGE_FORMAT_LEGACY:
4ca30d602   Simon Glass   sandbox: Support ...
48
  		hdr = buf;
d0dd10777   wdenk   Initial revision
49

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

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

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

d5934ad77   Marian Balakowicz   [new uImage] Add ...
76
77
  		/* get length of script */
  		data = (ulong *)image_get_data (hdr);
d0dd10777   wdenk   Initial revision
78

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