]>
Commit | Line | Data |
---|---|---|
458ded34 WD |
1 | /* |
2 | * Copyright (c) 2001 William L. Pitts | |
3 | * All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms are freely | |
6 | * permitted provided that the above copyright notice and this | |
7 | * paragraph and the following disclaimer are duplicated in all | |
8 | * such forms. | |
9 | * | |
10 | * This software is provided "AS IS" and without any express or | |
11 | * implied warranties, including, without limitation, the implied | |
12 | * warranties of merchantability and fitness for a particular | |
13 | * purpose. | |
14 | */ | |
15 | ||
16 | #include <common.h> | |
17 | #include <command.h> | |
458ded34 | 18 | #include <elf.h> |
ebca3df7 | 19 | #include <net.h> |
29a4c24d | 20 | #include <vxworks.h> |
458ded34 | 21 | |
9dffa52d BM |
22 | /* |
23 | * A very simple elf loader, assumes the image is valid, returns the | |
24 | * entry point address. | |
25 | */ | |
26 | static unsigned long load_elf_image_phdr(unsigned long addr) | |
27 | { | |
28 | Elf32_Ehdr *ehdr; /* Elf header structure pointer */ | |
29 | Elf32_Phdr *phdr; /* Program header structure pointer */ | |
30 | int i; | |
31 | ||
32 | ehdr = (Elf32_Ehdr *)addr; | |
33 | phdr = (Elf32_Phdr *)(addr + ehdr->e_phoff); | |
34 | ||
35 | /* Load each program header */ | |
36 | for (i = 0; i < ehdr->e_phnum; ++i) { | |
37 | void *dst = (void *)(uintptr_t)phdr->p_paddr; | |
38 | void *src = (void *)addr + phdr->p_offset; | |
39 | debug("Loading phdr %i to 0x%p (%i bytes)\n", | |
40 | i, dst, phdr->p_filesz); | |
41 | if (phdr->p_filesz) | |
42 | memcpy(dst, src, phdr->p_filesz); | |
43 | if (phdr->p_filesz != phdr->p_memsz) | |
44 | memset(dst + phdr->p_filesz, 0x00, | |
45 | phdr->p_memsz - phdr->p_filesz); | |
46 | flush_cache((unsigned long)dst, phdr->p_filesz); | |
47 | ++phdr; | |
48 | } | |
49 | ||
50 | return ehdr->e_entry; | |
51 | } | |
52 | ||
53 | static unsigned long load_elf_image_shdr(unsigned long addr) | |
54 | { | |
55 | Elf32_Ehdr *ehdr; /* Elf header structure pointer */ | |
56 | Elf32_Shdr *shdr; /* Section header structure pointer */ | |
57 | unsigned char *strtab = 0; /* String table pointer */ | |
58 | unsigned char *image; /* Binary image pointer */ | |
59 | int i; /* Loop counter */ | |
60 | ||
61 | ehdr = (Elf32_Ehdr *)addr; | |
62 | ||
63 | /* Find the section header string table for output info */ | |
64 | shdr = (Elf32_Shdr *)(addr + ehdr->e_shoff + | |
65 | (ehdr->e_shstrndx * sizeof(Elf32_Shdr))); | |
66 | ||
67 | if (shdr->sh_type == SHT_STRTAB) | |
68 | strtab = (unsigned char *)(addr + shdr->sh_offset); | |
69 | ||
70 | /* Load each appropriate section */ | |
71 | for (i = 0; i < ehdr->e_shnum; ++i) { | |
72 | shdr = (Elf32_Shdr *)(addr + ehdr->e_shoff + | |
73 | (i * sizeof(Elf32_Shdr))); | |
74 | ||
75 | if (!(shdr->sh_flags & SHF_ALLOC) || | |
76 | shdr->sh_addr == 0 || shdr->sh_size == 0) { | |
77 | continue; | |
78 | } | |
79 | ||
80 | if (strtab) { | |
81 | debug("%sing %s @ 0x%08lx (%ld bytes)\n", | |
82 | (shdr->sh_type == SHT_NOBITS) ? "Clear" : "Load", | |
83 | &strtab[shdr->sh_name], | |
84 | (unsigned long)shdr->sh_addr, | |
85 | (long)shdr->sh_size); | |
86 | } | |
87 | ||
88 | if (shdr->sh_type == SHT_NOBITS) { | |
89 | memset((void *)(uintptr_t)shdr->sh_addr, 0, | |
90 | shdr->sh_size); | |
91 | } else { | |
92 | image = (unsigned char *)addr + shdr->sh_offset; | |
93 | memcpy((void *)(uintptr_t)shdr->sh_addr, | |
94 | (const void *)image, shdr->sh_size); | |
95 | } | |
96 | flush_cache(shdr->sh_addr, shdr->sh_size); | |
97 | } | |
98 | ||
99 | return ehdr->e_entry; | |
100 | } | |
017e9b79 MF |
101 | |
102 | /* Allow ports to override the default behavior */ | |
553d8c3a | 103 | static unsigned long do_bootelf_exec(ulong (*entry)(int, char * const[]), |
ebca3df7 | 104 | int argc, char * const argv[]) |
1f1d88dd | 105 | { |
017e9b79 MF |
106 | unsigned long ret; |
107 | ||
1f1d88dd MF |
108 | /* |
109 | * QNX images require the data cache is disabled. | |
110 | * Data cache is already flushed, so just turn it off. | |
111 | */ | |
62e03d33 | 112 | int dcache = dcache_status(); |
017e9b79 | 113 | if (dcache) |
62e03d33 | 114 | dcache_disable(); |
1f1d88dd | 115 | |
017e9b79 MF |
116 | /* |
117 | * pass address parameter as argv[0] (aka command name), | |
118 | * and all remaining args | |
119 | */ | |
62e03d33 | 120 | ret = entry(argc, argv); |
1f1d88dd | 121 | |
017e9b79 | 122 | if (dcache) |
62e03d33 | 123 | dcache_enable(); |
017e9b79 MF |
124 | |
125 | return ret; | |
126 | } | |
458ded34 | 127 | |
ebca3df7 | 128 | /* |
62e03d33 | 129 | * Determine if a valid ELF image exists at the given memory location. |
ebca3df7 BM |
130 | * First look at the ELF header magic field, then make sure that it is |
131 | * executable. | |
132 | */ | |
62e03d33 DS |
133 | int valid_elf_image(unsigned long addr) |
134 | { | |
ebca3df7 | 135 | Elf32_Ehdr *ehdr; /* Elf header structure pointer */ |
62e03d33 | 136 | |
ebca3df7 | 137 | ehdr = (Elf32_Ehdr *)addr; |
62e03d33 DS |
138 | |
139 | if (!IS_ELF(*ehdr)) { | |
140 | printf("## No elf image at address 0x%08lx\n", addr); | |
141 | return 0; | |
142 | } | |
143 | ||
144 | if (ehdr->e_type != ET_EXEC) { | |
145 | printf("## Not a 32-bit elf image at address 0x%08lx\n", addr); | |
146 | return 0; | |
147 | } | |
148 | ||
62e03d33 DS |
149 | return 1; |
150 | } | |
151 | ||
ebca3df7 | 152 | /* Interpreter command to boot an arbitrary ELF image from memory */ |
62e03d33 | 153 | int do_bootelf(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
458ded34 | 154 | { |
ebca3df7 BM |
155 | unsigned long addr; /* Address of the ELF image */ |
156 | unsigned long rc; /* Return value from user code */ | |
f44a928e | 157 | char *sload, *saddr; |
44c8fd3a | 158 | const char *ep = getenv("autostart"); |
458ded34 | 159 | |
458ded34 WD |
160 | int rcode = 0; |
161 | ||
f44a928e MF |
162 | sload = saddr = NULL; |
163 | if (argc == 3) { | |
164 | sload = argv[1]; | |
165 | saddr = argv[2]; | |
166 | } else if (argc == 2) { | |
167 | if (argv[1][0] == '-') | |
168 | sload = argv[1]; | |
169 | else | |
170 | saddr = argv[1]; | |
171 | } | |
172 | ||
173 | if (saddr) | |
174 | addr = simple_strtoul(saddr, NULL, 16); | |
458ded34 | 175 | else |
f44a928e | 176 | addr = load_addr; |
458ded34 | 177 | |
62e03d33 | 178 | if (!valid_elf_image(addr)) |
458ded34 WD |
179 | return 1; |
180 | ||
f44a928e MF |
181 | if (sload && sload[1] == 'p') |
182 | addr = load_elf_image_phdr(addr); | |
183 | else | |
184 | addr = load_elf_image_shdr(addr); | |
458ded34 | 185 | |
44c8fd3a SDPP |
186 | if (ep && !strcmp(ep, "no")) |
187 | return rcode; | |
188 | ||
62e03d33 | 189 | printf("## Starting application at 0x%08lx ...\n", addr); |
458ded34 | 190 | |
458ded34 WD |
191 | /* |
192 | * pass address parameter as argv[0] (aka command name), | |
193 | * and all remaining args | |
194 | */ | |
62e03d33 | 195 | rc = do_bootelf_exec((void *)addr, argc - 1, argv + 1); |
458ded34 WD |
196 | if (rc != 0) |
197 | rcode = 1; | |
198 | ||
62e03d33 | 199 | printf("## Application terminated, rc = 0x%lx\n", rc); |
ebca3df7 | 200 | |
458ded34 WD |
201 | return rcode; |
202 | } | |
203 | ||
ebca3df7 | 204 | /* |
458ded34 WD |
205 | * Interpreter command to boot VxWorks from a memory image. The image can |
206 | * be either an ELF image or a raw binary. Will attempt to setup the | |
207 | * bootline and other parameters correctly. | |
ebca3df7 | 208 | */ |
62e03d33 | 209 | int do_bootvx(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
458ded34 | 210 | { |
ebca3df7 BM |
211 | unsigned long addr; /* Address of image */ |
212 | unsigned long bootaddr; /* Address to put the bootline */ | |
213 | char *bootline; /* Text of the bootline */ | |
214 | char *tmp; /* Temporary char pointer */ | |
215 | char build_buf[128]; /* Buffer for building the bootline */ | |
7f0c3c51 | 216 | int ptr = 0; |
ebca3df7 BM |
217 | |
218 | /* | |
458ded34 WD |
219 | * Check the loadaddr variable. |
220 | * If we don't know where the image is then we're done. | |
221 | */ | |
07a1a0c9 | 222 | if (argc < 2) |
1bdd4683 SR |
223 | addr = load_addr; |
224 | else | |
07a1a0c9 | 225 | addr = simple_strtoul(argv[1], NULL, 16); |
458ded34 | 226 | |
baa26db4 | 227 | #if defined(CONFIG_CMD_NET) |
62e03d33 | 228 | /* |
ebca3df7 BM |
229 | * Check to see if we need to tftp the image ourselves |
230 | * before starting | |
62e03d33 | 231 | */ |
07a1a0c9 | 232 | if ((argc == 2) && (strcmp(argv[1], "tftp") == 0)) { |
bc0571fc | 233 | if (net_loop(TFTPGET) <= 0) |
458ded34 | 234 | return 1; |
62e03d33 DS |
235 | printf("Automatic boot of VxWorks image at address 0x%08lx ...\n", |
236 | addr); | |
458ded34 WD |
237 | } |
238 | #endif | |
239 | ||
ebca3df7 BM |
240 | /* |
241 | * This should equate to | |
242 | * NV_RAM_ADRS + NV_BOOT_OFFSET + NV_ENET_OFFSET | |
458ded34 WD |
243 | * from the VxWorks BSP header files. |
244 | * This will vary from board to board | |
245 | */ | |
c157d8e2 | 246 | #if defined(CONFIG_WALNUT) |
ebca3df7 | 247 | tmp = (char *)CONFIG_SYS_NVRAM_BASE_ADDR + 0x500; |
76756e41 | 248 | eth_getenv_enetaddr("ethaddr", (uchar *)build_buf); |
62c93d92 | 249 | memcpy(tmp, &build_buf[3], 3); |
6d0f6bcf | 250 | #elif defined(CONFIG_SYS_VXWORKS_MAC_PTR) |
ebca3df7 | 251 | tmp = (char *)CONFIG_SYS_VXWORKS_MAC_PTR; |
76756e41 | 252 | eth_getenv_enetaddr("ethaddr", (uchar *)build_buf); |
62c93d92 | 253 | memcpy(tmp, build_buf, 6); |
458ded34 | 254 | #else |
62e03d33 | 255 | puts("## Ethernet MAC address not copied to NV RAM\n"); |
458ded34 WD |
256 | #endif |
257 | ||
8bde7f77 WD |
258 | /* |
259 | * Use bootaddr to find the location in memory that VxWorks | |
9e98b7e3 BM |
260 | * will look for the bootline string. The default value is |
261 | * (LOCAL_MEM_LOCAL_ADRS + BOOT_LINE_OFFSET) as defined by | |
262 | * VxWorks BSP. For example, on PowerPC it defaults to 0x4200. | |
458ded34 | 263 | */ |
62e03d33 | 264 | tmp = getenv("bootaddr"); |
9e98b7e3 BM |
265 | if (!tmp) { |
266 | printf("## VxWorks bootline address not specified\n"); | |
458ded34 | 267 | } else { |
9e98b7e3 | 268 | bootaddr = simple_strtoul(tmp, NULL, 16); |
458ded34 | 269 | |
9e98b7e3 BM |
270 | /* |
271 | * Check to see if the bootline is defined in the 'bootargs' | |
272 | * parameter. If it is not defined, we may be able to | |
273 | * construct the info. | |
274 | */ | |
275 | bootline = getenv("bootargs"); | |
276 | if (bootline) { | |
277 | memcpy((void *)bootaddr, bootline, | |
278 | max(strlen(bootline), (size_t)255)); | |
279 | flush_cache(bootaddr, max(strlen(bootline), | |
280 | (size_t)255)); | |
281 | } else { | |
282 | tmp = getenv("bootdev"); | |
283 | if (tmp) | |
284 | ptr = sprintf(build_buf, tmp); | |
285 | else | |
286 | printf("## VxWorks boot device not specified\n"); | |
287 | ||
288 | tmp = getenv("bootfile"); | |
289 | if (tmp) | |
290 | ptr += sprintf(build_buf + ptr, | |
291 | "host:%s ", tmp); | |
292 | else | |
293 | ptr += sprintf(build_buf + ptr, | |
294 | "host:vxWorks "); | |
295 | ||
296 | /* | |
297 | * The following parameters are only needed if 'bootdev' | |
298 | * is an ethernet device, otherwise they are optional. | |
299 | */ | |
300 | tmp = getenv("ipaddr"); | |
a4092dbd | 301 | if (tmp) { |
9e98b7e3 BM |
302 | ptr += sprintf(build_buf + ptr, "e=%s", tmp); |
303 | tmp = getenv("netmask"); | |
304 | if (tmp) { | |
305 | u32 mask = getenv_ip("netmask").s_addr; | |
306 | ptr += sprintf(build_buf + ptr, | |
307 | ":%08x ", ntohl(mask)); | |
308 | } else { | |
309 | ptr += sprintf(build_buf + ptr, " "); | |
310 | } | |
a4092dbd | 311 | } |
458ded34 | 312 | |
9e98b7e3 BM |
313 | tmp = getenv("serverip"); |
314 | if (tmp) | |
315 | ptr += sprintf(build_buf + ptr, "h=%s ", tmp); | |
62e03d33 | 316 | |
9e98b7e3 BM |
317 | tmp = getenv("gatewayip"); |
318 | if (tmp) | |
319 | ptr += sprintf(build_buf + ptr, "g=%s ", tmp); | |
a4092dbd | 320 | |
9e98b7e3 BM |
321 | tmp = getenv("hostname"); |
322 | if (tmp) | |
323 | ptr += sprintf(build_buf + ptr, "tn=%s ", tmp); | |
458ded34 | 324 | |
9e98b7e3 BM |
325 | tmp = getenv("othbootargs"); |
326 | if (tmp) | |
327 | ptr += sprintf(build_buf + ptr, tmp); | |
328 | ||
329 | memcpy((void *)bootaddr, build_buf, | |
330 | max(strlen(build_buf), (size_t)255)); | |
331 | flush_cache(bootaddr, max(strlen(build_buf), | |
332 | (size_t)255)); | |
333 | } | |
29a4c24d | 334 | |
9e98b7e3 BM |
335 | printf("## Using bootline (@ 0x%lx): %s\n", bootaddr, |
336 | (char *)bootaddr); | |
458ded34 WD |
337 | } |
338 | ||
8bde7f77 WD |
339 | /* |
340 | * If the data at the load address is an elf image, then | |
341 | * treat it like an elf image. Otherwise, assume that it is a | |
ebca3df7 | 342 | * binary image. |
458ded34 | 343 | */ |
ebca3df7 | 344 | if (valid_elf_image(addr)) |
62e03d33 | 345 | addr = load_elf_image_shdr(addr); |
ebca3df7 | 346 | else |
62e03d33 | 347 | puts("## Not an ELF image, assuming binary\n"); |
458ded34 | 348 | |
62e03d33 | 349 | printf("## Starting vxWorks at 0x%08lx ...\n", addr); |
458ded34 | 350 | |
6eee21da | 351 | dcache_disable(); |
ebca3df7 | 352 | ((void (*)(int))addr)(0); |
458ded34 | 353 | |
62e03d33 | 354 | puts("## vxWorks terminated\n"); |
ebca3df7 | 355 | |
458ded34 WD |
356 | return 1; |
357 | } | |
358 | ||
0d498393 | 359 | U_BOOT_CMD( |
ebca3df7 | 360 | bootelf, 3, 0, do_bootelf, |
2fb2604d | 361 | "Boot from an ELF image in memory", |
f44a928e MF |
362 | "[-p|-s] [address]\n" |
363 | "\t- load ELF image at [address] via program headers (-p)\n" | |
364 | "\t or via section headers (-s)" | |
8bde7f77 WD |
365 | ); |
366 | ||
0d498393 | 367 | U_BOOT_CMD( |
ebca3df7 | 368 | bootvx, 2, 0, do_bootvx, |
2fb2604d | 369 | "Boot vxWorks from an ELF image", |
a89c33db | 370 | " [address] - load address of vxWorks ELF image." |
8bde7f77 | 371 | ); |