1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17#include <common.h>
18#include <command.h>
19#include <env.h>
20#include <image.h>
21#include <malloc.h>
22#include <mapmem.h>
23#include <asm/byteorder.h>
24#include <asm/io.h>
25
26#if defined(CONFIG_FIT)
27
28
29
30
31
32static const char *get_default_image(const void *fit)
33{
34 int images_noffset;
35
36 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
37 if (images_noffset < 0)
38 return NULL;
39
40 return fdt_getprop(fit, images_noffset, FIT_DEFAULT_PROP, NULL);
41}
42#endif
43
44int
45source (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
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
101
102
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)) {
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
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
136 if (verify) {
137 if (!fit_image_verify(fit_hdr, noffset)) {
138 puts ("Bad Data Hash\n");
139 return 1;
140 }
141 }
142
143
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)
164static int do_source(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
165{
166 ulong addr;
167 int rcode;
168 const char *fit_uname = NULL;
169
170
171 if (argc < 2) {
172 addr = CONFIG_SYS_LOAD_ADDR;
173 debug ("* source: default load address = 0x%08lx\n", addr);
174#if defined(CONFIG_FIT)
175 } else if (fit_parse_subimage (argv[1], load_addr, &addr, &fit_uname)) {
176 debug ("* source: subimage '%s' from FIT image at 0x%08lx\n",
177 fit_uname, addr);
178#endif
179 } else {
180 addr = simple_strtoul(argv[1], NULL, 16);
181 debug ("* source: cmdline image address = 0x%08lx\n", addr);
182 }
183
184 printf ("## Executing script at %08lx\n", addr);
185 rcode = source (addr, fit_uname);
186 return rcode;
187}
188
189#ifdef CONFIG_SYS_LONGHELP
190static char source_help_text[] =
191 "[addr]\n"
192 "\t- run script starting at addr\n"
193 "\t- A valid image header must be present"
194#if defined(CONFIG_FIT)
195 "\n"
196 "For FIT format uImage addr must include subimage\n"
197 "unit name in the form of addr:<subimg_uname>"
198#endif
199 "";
200#endif
201
202U_BOOT_CMD(
203 source, 2, 0, do_source,
204 "run script from memory", source_help_text
205);
206#endif
207