1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16#include <common.h>
17#include <command.h>
18#include <linux/ctype.h>
19#include <net.h>
20#include <elf.h>
21#include <vxworks.h>
22
23#if defined(CONFIG_WALNUT) || defined(CONFIG_SYS_VXWORKS_MAC_PTR)
24DECLARE_GLOBAL_DATA_PTR;
25#endif
26
27int valid_elf_image (unsigned long addr);
28unsigned long load_elf_image (unsigned long addr);
29
30
31__attribute__((weak))
32unsigned long do_bootelf_exec (ulong (*entry)(int, char *[]), int argc, char *argv[])
33{
34 unsigned long ret;
35
36
37
38
39
40 int dcache = dcache_status ();
41 if (dcache)
42 dcache_disable ();
43
44
45
46
47
48 ret = entry (argc, argv);
49
50 if (dcache)
51 dcache_enable ();
52
53 return ret;
54}
55
56
57
58
59int do_bootelf (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
60{
61 unsigned long addr;
62 unsigned long rc;
63
64
65 int rcode = 0;
66
67 if (argc < 2)
68 addr = load_addr;
69 else
70 addr = simple_strtoul (argv[1], NULL, 16);
71
72 if (!valid_elf_image (addr))
73 return 1;
74
75 addr = load_elf_image (addr);
76
77 printf ("## Starting application at 0x%08lx ...\n", addr);
78
79
80
81
82
83 rc = do_bootelf_exec ((void *)addr, argc - 1, argv + 1);
84 if (rc != 0)
85 rcode = 1;
86
87 printf ("## Application terminated, rc = 0x%lx\n", rc);
88 return rcode;
89}
90
91
92
93
94
95
96int do_bootvx (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
97{
98 unsigned long addr;
99 unsigned long bootaddr;
100 char *bootline;
101 char *tmp;
102 char build_buf[128];
103
104
105
106
107
108
109
110 if (argc < 2)
111 addr = load_addr;
112 else
113 addr = simple_strtoul (argv[1], NULL, 16);
114
115#if defined(CONFIG_CMD_NET)
116
117
118 if ((argc == 2) && (strcmp (argv[1], "tftp") == 0)) {
119 if (NetLoop (TFTP) <= 0)
120 return 1;
121 printf ("Automatic boot of VxWorks image at address 0x%08lx ... \n",
122 addr);
123 }
124#endif
125
126
127
128
129
130
131
132#if defined(CONFIG_WALNUT)
133 tmp = (char *) CONFIG_SYS_NVRAM_BASE_ADDR + 0x500;
134 eth_getenv_enetaddr("ethaddr", (uchar *)build_buf);
135 memcpy(tmp, &build_buf[3], 3);
136#elif defined(CONFIG_SYS_VXWORKS_MAC_PTR)
137 tmp = (char *) CONFIG_SYS_VXWORKS_MAC_PTR;
138 eth_getenv_enetaddr("ethaddr", (uchar *)build_buf);
139 memcpy(tmp, build_buf, 6);
140#else
141 puts ("## Ethernet MAC address not copied to NV RAM\n");
142#endif
143
144
145
146
147
148
149
150
151 if ((tmp = getenv ("bootaddr")) == NULL)
152 bootaddr = CONFIG_SYS_VXWORKS_BOOT_ADDR;
153 else
154 bootaddr = simple_strtoul (tmp, NULL, 16);
155
156
157
158
159
160
161
162 if ((bootline = getenv ("bootargs")) != NULL) {
163 memcpy ((void *) bootaddr, bootline,
164 max (strlen (bootline), 255));
165 flush_cache (bootaddr, max (strlen (bootline), 255));
166 } else {
167
168
169 sprintf (build_buf, CONFIG_SYS_VXWORKS_BOOT_DEVICE);
170 if ((tmp = getenv ("bootfile")) != NULL) {
171 sprintf (&build_buf[strlen (build_buf)],
172 "%s:%s ", CONFIG_SYS_VXWORKS_SERVERNAME, tmp);
173 } else {
174 sprintf (&build_buf[strlen (build_buf)],
175 "%s:file ", CONFIG_SYS_VXWORKS_SERVERNAME);
176 }
177
178 if ((tmp = getenv ("ipaddr")) != NULL) {
179 sprintf (&build_buf[strlen (build_buf)], "e=%s ", tmp);
180 }
181
182 if ((tmp = getenv ("serverip")) != NULL) {
183 sprintf (&build_buf[strlen (build_buf)], "h=%s ", tmp);
184 }
185
186 if ((tmp = getenv ("hostname")) != NULL) {
187 sprintf (&build_buf[strlen (build_buf)], "tn=%s ", tmp);
188 }
189#ifdef CONFIG_SYS_VXWORKS_ADD_PARAMS
190 sprintf (&build_buf[strlen (build_buf)],
191 CONFIG_SYS_VXWORKS_ADD_PARAMS);
192#endif
193
194 memcpy ((void *) bootaddr, build_buf,
195 max (strlen (build_buf), 255));
196 flush_cache (bootaddr, max (strlen (build_buf), 255));
197 }
198
199
200
201
202
203
204
205 if (valid_elf_image (addr)) {
206 addr = load_elf_image (addr);
207 } else {
208 puts ("## Not an ELF image, assuming binary\n");
209
210 }
211
212 printf ("## Using bootline (@ 0x%lx): %s\n", bootaddr,
213 (char *) bootaddr);
214 printf ("## Starting vxWorks at 0x%08lx ...\n", addr);
215
216 ((void (*)(void)) addr) ();
217
218 puts ("## vxWorks terminated\n");
219 return 1;
220}
221
222
223
224
225
226
227int valid_elf_image (unsigned long addr)
228{
229 Elf32_Ehdr *ehdr;
230
231
232
233 ehdr = (Elf32_Ehdr *) addr;
234
235 if (!IS_ELF (*ehdr)) {
236 printf ("## No elf image at address 0x%08lx\n", addr);
237 return 0;
238 }
239
240 if (ehdr->e_type != ET_EXEC) {
241 printf ("## Not a 32-bit elf image at address 0x%08lx\n", addr);
242 return 0;
243 }
244
245#if 0
246 if (ehdr->e_machine != EM_PPC) {
247 printf ("## Not a PowerPC elf image at address 0x%08lx\n",
248 addr);
249 return 0;
250 }
251#endif
252
253 return 1;
254}
255
256
257
258
259
260unsigned long load_elf_image (unsigned long addr)
261{
262 Elf32_Ehdr *ehdr;
263 Elf32_Shdr *shdr;
264 unsigned char *strtab = 0;
265 unsigned char *image;
266 int i;
267
268
269
270 ehdr = (Elf32_Ehdr *) addr;
271
272
273 shdr = (Elf32_Shdr *) (addr + ehdr->e_shoff +
274 (ehdr->e_shstrndx * sizeof (Elf32_Shdr)));
275
276 if (shdr->sh_type == SHT_STRTAB)
277 strtab = (unsigned char *) (addr + shdr->sh_offset);
278
279
280 for (i = 0; i < ehdr->e_shnum; ++i) {
281 shdr = (Elf32_Shdr *) (addr + ehdr->e_shoff +
282 (i * sizeof (Elf32_Shdr)));
283
284 if (!(shdr->sh_flags & SHF_ALLOC)
285 || shdr->sh_addr == 0 || shdr->sh_size == 0) {
286 continue;
287 }
288
289 if (strtab) {
290 debug("%sing %s @ 0x%08lx (%ld bytes)\n",
291 (shdr->sh_type == SHT_NOBITS) ?
292 "Clear" : "Load",
293 &strtab[shdr->sh_name],
294 (unsigned long) shdr->sh_addr,
295 (long) shdr->sh_size);
296 }
297
298 if (shdr->sh_type == SHT_NOBITS) {
299 memset ((void *)shdr->sh_addr, 0, shdr->sh_size);
300 } else {
301 image = (unsigned char *) addr + shdr->sh_offset;
302 memcpy ((void *) shdr->sh_addr,
303 (const void *) image,
304 shdr->sh_size);
305 }
306 flush_cache (shdr->sh_addr, shdr->sh_size);
307 }
308
309 return ehdr->e_entry;
310}
311
312
313U_BOOT_CMD(
314 bootelf, 2, 0, do_bootelf,
315 "Boot from an ELF image in memory",
316 " [address] - load address of ELF image."
317);
318
319U_BOOT_CMD(
320 bootvx, 2, 0, do_bootvx,
321 "Boot vxWorks from an ELF image",
322 " [address] - load address of vxWorks ELF image."
323);
324