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