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