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