blob: 42a52965c2722282c36e6ebeaa0049c8c21b4c9f [file] [log] [blame]
wdenk458ded32002-09-20 10:59:08 +00001/*
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>
Jeroen Hofsteeb869cae2014-10-08 22:57:51 +020017#include <bootm.h>
wdenk458ded32002-09-20 10:59:08 +000018#include <command.h>
19#include <linux/ctype.h>
20#include <net.h>
wdenk458ded32002-09-20 10:59:08 +000021#include <elf.h>
Niklaus Gigerf662b232008-11-03 22:15:34 +010022#include <vxworks.h>
wdenk458ded32002-09-20 10:59:08 +000023
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020024#if defined(CONFIG_WALNUT) || defined(CONFIG_SYS_VXWORKS_MAC_PTR)
Wolfgang Denk6405a152006-03-31 18:32:53 +020025DECLARE_GLOBAL_DATA_PTR;
26#endif
wdenk458ded32002-09-20 10:59:08 +000027
Mike Frysingere6dc29f2010-10-02 15:44:53 -040028static unsigned long load_elf_image_phdr(unsigned long addr);
29static unsigned long load_elf_image_shdr(unsigned long addr);
Mike Frysingere60644a2008-04-13 19:42:18 -040030
31/* Allow ports to override the default behavior */
Jeroen Hofstee3cc2a742014-10-08 22:57:31 +020032static unsigned long do_bootelf_exec(ulong (*entry)(int, char * const[]),
Wolfgang Denk6262d0212010-06-28 22:00:46 +020033 int argc, char * const argv[])
Mike Frysingerf79507e2008-01-29 18:21:05 -050034{
Mike Frysingere60644a2008-04-13 19:42:18 -040035 unsigned long ret;
36
Mike Frysingerf79507e2008-01-29 18:21:05 -050037 /*
38 * QNX images require the data cache is disabled.
39 * Data cache is already flushed, so just turn it off.
40 */
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +000041 int dcache = dcache_status();
Mike Frysingere60644a2008-04-13 19:42:18 -040042 if (dcache)
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +000043 dcache_disable();
Mike Frysingerf79507e2008-01-29 18:21:05 -050044
Mike Frysingere60644a2008-04-13 19:42:18 -040045 /*
46 * pass address parameter as argv[0] (aka command name),
47 * and all remaining args
48 */
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +000049 ret = entry(argc, argv);
Mike Frysingerf79507e2008-01-29 18:21:05 -050050
Mike Frysingere60644a2008-04-13 19:42:18 -040051 if (dcache)
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +000052 dcache_enable();
Mike Frysingere60644a2008-04-13 19:42:18 -040053
54 return ret;
55}
wdenk458ded32002-09-20 10:59:08 +000056
57/* ======================================================================
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +000058 * Determine if a valid ELF image exists at the given memory location.
59 * First looks at the ELF header magic field, the makes sure that it is
60 * executable and makes sure that it is for a PowerPC.
61 * ====================================================================== */
62int valid_elf_image(unsigned long addr)
63{
64 Elf32_Ehdr *ehdr; /* Elf header structure pointer */
65
66 /* -------------------------------------------------- */
67
68 ehdr = (Elf32_Ehdr *) addr;
69
70 if (!IS_ELF(*ehdr)) {
71 printf("## No elf image at address 0x%08lx\n", addr);
72 return 0;
73 }
74
75 if (ehdr->e_type != ET_EXEC) {
76 printf("## Not a 32-bit elf image at address 0x%08lx\n", addr);
77 return 0;
78 }
79
80#if 0
81 if (ehdr->e_machine != EM_PPC) {
82 printf("## Not a PowerPC elf image at address 0x%08lx\n", addr);
83 return 0;
84 }
85#endif
86
87 return 1;
88}
89
90/* ======================================================================
wdenk458ded32002-09-20 10:59:08 +000091 * Interpreter command to boot an arbitrary ELF image from memory.
92 * ====================================================================== */
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +000093int do_bootelf(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk458ded32002-09-20 10:59:08 +000094{
95 unsigned long addr; /* Address of the ELF image */
96 unsigned long rc; /* Return value from user code */
Mike Frysingere6dc29f2010-10-02 15:44:53 -040097 char *sload, *saddr;
wdenk458ded32002-09-20 10:59:08 +000098
99 /* -------------------------------------------------- */
100 int rcode = 0;
101
Mike Frysingere6dc29f2010-10-02 15:44:53 -0400102 sload = saddr = NULL;
103 if (argc == 3) {
104 sload = argv[1];
105 saddr = argv[2];
106 } else if (argc == 2) {
107 if (argv[1][0] == '-')
108 sload = argv[1];
109 else
110 saddr = argv[1];
111 }
112
113 if (saddr)
114 addr = simple_strtoul(saddr, NULL, 16);
wdenk458ded32002-09-20 10:59:08 +0000115 else
Mike Frysingere6dc29f2010-10-02 15:44:53 -0400116 addr = load_addr;
wdenk458ded32002-09-20 10:59:08 +0000117
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +0000118 if (!valid_elf_image(addr))
wdenk458ded32002-09-20 10:59:08 +0000119 return 1;
120
Mike Frysingere6dc29f2010-10-02 15:44:53 -0400121 if (sload && sload[1] == 'p')
122 addr = load_elf_image_phdr(addr);
123 else
124 addr = load_elf_image_shdr(addr);
wdenk458ded32002-09-20 10:59:08 +0000125
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +0000126 printf("## Starting application at 0x%08lx ...\n", addr);
wdenk458ded32002-09-20 10:59:08 +0000127
wdenk458ded32002-09-20 10:59:08 +0000128 /*
129 * pass address parameter as argv[0] (aka command name),
130 * and all remaining args
131 */
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +0000132 rc = do_bootelf_exec((void *)addr, argc - 1, argv + 1);
wdenk458ded32002-09-20 10:59:08 +0000133 if (rc != 0)
134 rcode = 1;
135
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +0000136 printf("## Application terminated, rc = 0x%lx\n", rc);
wdenk458ded32002-09-20 10:59:08 +0000137 return rcode;
138}
139
140/* ======================================================================
141 * Interpreter command to boot VxWorks from a memory image. The image can
142 * be either an ELF image or a raw binary. Will attempt to setup the
143 * bootline and other parameters correctly.
144 * ====================================================================== */
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +0000145int do_bootvx(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk458ded32002-09-20 10:59:08 +0000146{
wdenk458ded32002-09-20 10:59:08 +0000147 unsigned long addr; /* Address of image */
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +0000148 unsigned long bootaddr; /* Address to put the bootline */
wdenk458ded32002-09-20 10:59:08 +0000149 char *bootline; /* Text of the bootline */
150 char *tmp; /* Temporary char pointer */
Niklaus Gigerf662b232008-11-03 22:15:34 +0100151 char build_buf[128]; /* Buffer for building the bootline */
wdenk458ded32002-09-20 10:59:08 +0000152
Niklaus Gigerf662b232008-11-03 22:15:34 +0100153 /* ---------------------------------------------------
154 *
wdenk458ded32002-09-20 10:59:08 +0000155 * Check the loadaddr variable.
156 * If we don't know where the image is then we're done.
157 */
158
Stany MARCELe5deaf582013-11-27 14:48:43 +0100159 if (argc < 2)
Stefan Roese00487782006-11-29 12:53:15 +0100160 addr = load_addr;
161 else
Stany MARCELe5deaf582013-11-27 14:48:43 +0100162 addr = simple_strtoul(argv[1], NULL, 16);
wdenk458ded32002-09-20 10:59:08 +0000163
Jon Loeliger54324d02007-07-08 17:51:39 -0500164#if defined(CONFIG_CMD_NET)
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +0000165 /*
166 * Check to see if we need to tftp the image ourselves before starting
167 */
Stany MARCELe5deaf582013-11-27 14:48:43 +0100168 if ((argc == 2) && (strcmp(argv[1], "tftp") == 0)) {
Simon Glassd6c5f552011-10-24 18:00:02 +0000169 if (NetLoop(TFTPGET) <= 0)
wdenk458ded32002-09-20 10:59:08 +0000170 return 1;
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +0000171 printf("Automatic boot of VxWorks image at address 0x%08lx ...\n",
172 addr);
wdenk458ded32002-09-20 10:59:08 +0000173 }
174#endif
175
176 /* This should equate
177 * to NV_RAM_ADRS + NV_BOOT_OFFSET + NV_ENET_OFFSET
178 * from the VxWorks BSP header files.
179 * This will vary from board to board
180 */
181
Stefan Roese326c9712005-08-01 16:41:48 +0200182#if defined(CONFIG_WALNUT)
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200183 tmp = (char *) CONFIG_SYS_NVRAM_BASE_ADDR + 0x500;
Heiko Schocher50219e62009-03-26 07:33:59 +0100184 eth_getenv_enetaddr("ethaddr", (uchar *)build_buf);
Mike Frysinger8bcee042009-02-11 18:51:43 -0500185 memcpy(tmp, &build_buf[3], 3);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200186#elif defined(CONFIG_SYS_VXWORKS_MAC_PTR)
187 tmp = (char *) CONFIG_SYS_VXWORKS_MAC_PTR;
Heiko Schocher50219e62009-03-26 07:33:59 +0100188 eth_getenv_enetaddr("ethaddr", (uchar *)build_buf);
Mike Frysinger8bcee042009-02-11 18:51:43 -0500189 memcpy(tmp, build_buf, 6);
wdenk458ded32002-09-20 10:59:08 +0000190#else
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +0000191 puts("## Ethernet MAC address not copied to NV RAM\n");
wdenk458ded32002-09-20 10:59:08 +0000192#endif
193
wdenk57b2d802003-06-27 21:31:46 +0000194 /*
195 * Use bootaddr to find the location in memory that VxWorks
196 * will look for the bootline string. The default value for
197 * PowerPC is LOCAL_MEM_LOCAL_ADRS + BOOT_LINE_OFFSET which
198 * defaults to 0x4200
wdenk458ded32002-09-20 10:59:08 +0000199 */
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +0000200 tmp = getenv("bootaddr");
Reinhard Arlt41cfa2d2013-02-04 09:37:11 +0000201 if (!tmp)
Niklaus Gigerf662b232008-11-03 22:15:34 +0100202 bootaddr = CONFIG_SYS_VXWORKS_BOOT_ADDR;
wdenk458ded32002-09-20 10:59:08 +0000203 else
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +0000204 bootaddr = simple_strtoul(tmp, NULL, 16);
wdenk458ded32002-09-20 10:59:08 +0000205
wdenk57b2d802003-06-27 21:31:46 +0000206 /*
207 * Check to see if the bootline is defined in the 'bootargs'
208 * parameter. If it is not defined, we may be able to
209 * construct the info
wdenk458ded32002-09-20 10:59:08 +0000210 */
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +0000211 bootline = getenv("bootargs");
212 if (bootline) {
213 memcpy((void *) bootaddr, bootline,
214 max(strlen(bootline), 255));
215 flush_cache(bootaddr, max(strlen(bootline), 255));
wdenk458ded32002-09-20 10:59:08 +0000216 } else {
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +0000217 sprintf(build_buf, CONFIG_SYS_VXWORKS_BOOT_DEVICE);
218 tmp = getenv("bootfile");
219 if (tmp)
220 sprintf(&build_buf[strlen(build_buf)],
Niklaus Gigerf662b232008-11-03 22:15:34 +0100221 "%s:%s ", CONFIG_SYS_VXWORKS_SERVERNAME, tmp);
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +0000222 else
223 sprintf(&build_buf[strlen(build_buf)],
Niklaus Gigerf662b232008-11-03 22:15:34 +0100224 "%s:file ", CONFIG_SYS_VXWORKS_SERVERNAME);
wdenk458ded32002-09-20 10:59:08 +0000225
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +0000226 tmp = getenv("ipaddr");
227 if (tmp)
228 sprintf(&build_buf[strlen(build_buf)], "e=%s ", tmp);
wdenk458ded32002-09-20 10:59:08 +0000229
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +0000230 tmp = getenv("serverip");
231 if (tmp)
232 sprintf(&build_buf[strlen(build_buf)], "h=%s ", tmp);
wdenk458ded32002-09-20 10:59:08 +0000233
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +0000234 tmp = getenv("hostname");
235 if (tmp)
236 sprintf(&build_buf[strlen(build_buf)], "tn=%s ", tmp);
237
Niklaus Gigerf662b232008-11-03 22:15:34 +0100238#ifdef CONFIG_SYS_VXWORKS_ADD_PARAMS
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +0000239 sprintf(&build_buf[strlen(build_buf)],
Niklaus Gigerf662b232008-11-03 22:15:34 +0100240 CONFIG_SYS_VXWORKS_ADD_PARAMS);
wdenk458ded32002-09-20 10:59:08 +0000241#endif
Niklaus Gigerf662b232008-11-03 22:15:34 +0100242
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +0000243 memcpy((void *) bootaddr, build_buf,
244 max(strlen(build_buf), 255));
245 flush_cache(bootaddr, max(strlen(build_buf), 255));
wdenk458ded32002-09-20 10:59:08 +0000246 }
247
wdenk57b2d802003-06-27 21:31:46 +0000248 /*
249 * If the data at the load address is an elf image, then
250 * treat it like an elf image. Otherwise, assume that it is a
251 * binary image
wdenk458ded32002-09-20 10:59:08 +0000252 */
253
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +0000254 if (valid_elf_image(addr)) {
255 addr = load_elf_image_shdr(addr);
wdenk458ded32002-09-20 10:59:08 +0000256 } else {
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +0000257 puts("## Not an ELF image, assuming binary\n");
wdenk458ded32002-09-20 10:59:08 +0000258 /* leave addr as load_addr */
259 }
260
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +0000261 printf("## Using bootline (@ 0x%lx): %s\n", bootaddr,
wdenk458ded32002-09-20 10:59:08 +0000262 (char *) bootaddr);
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +0000263 printf("## Starting vxWorks at 0x%08lx ...\n", addr);
wdenk458ded32002-09-20 10:59:08 +0000264
Reinhard Arlt4fa117c2011-11-18 09:06:52 +0000265 dcache_disable();
266 ((void (*)(int)) addr) (0);
wdenk458ded32002-09-20 10:59:08 +0000267
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +0000268 puts("## vxWorks terminated\n");
wdenk458ded32002-09-20 10:59:08 +0000269 return 1;
270}
271
wdenk458ded32002-09-20 10:59:08 +0000272/* ======================================================================
273 * A very simple elf loader, assumes the image is valid, returns the
274 * entry point address.
275 * ====================================================================== */
Mike Frysingere6dc29f2010-10-02 15:44:53 -0400276static unsigned long load_elf_image_phdr(unsigned long addr)
277{
278 Elf32_Ehdr *ehdr; /* Elf header structure pointer */
279 Elf32_Phdr *phdr; /* Program header structure pointer */
280 int i;
281
282 ehdr = (Elf32_Ehdr *) addr;
283 phdr = (Elf32_Phdr *) (addr + ehdr->e_phoff);
284
285 /* Load each program header */
286 for (i = 0; i < ehdr->e_phnum; ++i) {
Daniel Schwierzeckc336f872012-09-16 06:55:06 +0000287 void *dst = (void *)(uintptr_t) phdr->p_paddr;
Mike Frysingere6dc29f2010-10-02 15:44:53 -0400288 void *src = (void *) addr + phdr->p_offset;
289 debug("Loading phdr %i to 0x%p (%i bytes)\n",
290 i, dst, phdr->p_filesz);
291 if (phdr->p_filesz)
292 memcpy(dst, src, phdr->p_filesz);
293 if (phdr->p_filesz != phdr->p_memsz)
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +0000294 memset(dst + phdr->p_filesz, 0x00,
295 phdr->p_memsz - phdr->p_filesz);
Mike Frysingere6dc29f2010-10-02 15:44:53 -0400296 flush_cache((unsigned long)dst, phdr->p_filesz);
297 ++phdr;
298 }
299
300 return ehdr->e_entry;
301}
302
303static unsigned long load_elf_image_shdr(unsigned long addr)
wdenk458ded32002-09-20 10:59:08 +0000304{
305 Elf32_Ehdr *ehdr; /* Elf header structure pointer */
306 Elf32_Shdr *shdr; /* Section header structure pointer */
307 unsigned char *strtab = 0; /* String table pointer */
308 unsigned char *image; /* Binary image pointer */
309 int i; /* Loop counter */
310
311 /* -------------------------------------------------- */
312
313 ehdr = (Elf32_Ehdr *) addr;
314
315 /* Find the section header string table for output info */
316 shdr = (Elf32_Shdr *) (addr + ehdr->e_shoff +
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +0000317 (ehdr->e_shstrndx * sizeof(Elf32_Shdr)));
wdenk458ded32002-09-20 10:59:08 +0000318
319 if (shdr->sh_type == SHT_STRTAB)
320 strtab = (unsigned char *) (addr + shdr->sh_offset);
321
322 /* Load each appropriate section */
323 for (i = 0; i < ehdr->e_shnum; ++i) {
324 shdr = (Elf32_Shdr *) (addr + ehdr->e_shoff +
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +0000325 (i * sizeof(Elf32_Shdr)));
wdenk458ded32002-09-20 10:59:08 +0000326
327 if (!(shdr->sh_flags & SHF_ALLOC)
328 || shdr->sh_addr == 0 || shdr->sh_size == 0) {
329 continue;
330 }
331
332 if (strtab) {
Niklaus Gigerdaff3fe2009-07-23 23:31:58 +0200333 debug("%sing %s @ 0x%08lx (%ld bytes)\n",
wdenk458ded32002-09-20 10:59:08 +0000334 (shdr->sh_type == SHT_NOBITS) ?
335 "Clear" : "Load",
336 &strtab[shdr->sh_name],
337 (unsigned long) shdr->sh_addr,
338 (long) shdr->sh_size);
339 }
340
341 if (shdr->sh_type == SHT_NOBITS) {
Daniel Schwierzeckc336f872012-09-16 06:55:06 +0000342 memset((void *)(uintptr_t) shdr->sh_addr, 0,
343 shdr->sh_size);
wdenk458ded32002-09-20 10:59:08 +0000344 } else {
345 image = (unsigned char *) addr + shdr->sh_offset;
Daniel Schwierzeckc336f872012-09-16 06:55:06 +0000346 memcpy((void *)(uintptr_t) shdr->sh_addr,
wdenk458ded32002-09-20 10:59:08 +0000347 (const void *) image,
348 shdr->sh_size);
349 }
Daniel Schwierzeckfa87af32012-09-16 06:55:05 +0000350 flush_cache(shdr->sh_addr, shdr->sh_size);
wdenk458ded32002-09-20 10:59:08 +0000351 }
352
353 return ehdr->e_entry;
354}
355
356/* ====================================================================== */
wdenkf287a242003-07-01 21:06:45 +0000357U_BOOT_CMD(
Mike Frysingere6dc29f2010-10-02 15:44:53 -0400358 bootelf, 3, 0, do_bootelf,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600359 "Boot from an ELF image in memory",
Mike Frysingere6dc29f2010-10-02 15:44:53 -0400360 "[-p|-s] [address]\n"
361 "\t- load ELF image at [address] via program headers (-p)\n"
362 "\t or via section headers (-s)"
wdenk57b2d802003-06-27 21:31:46 +0000363);
364
wdenkf287a242003-07-01 21:06:45 +0000365U_BOOT_CMD(
366 bootvx, 2, 0, do_bootvx,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600367 "Boot vxWorks from an ELF image",
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200368 " [address] - load address of vxWorks ELF image."
wdenk57b2d802003-06-27 21:31:46 +0000369);