blob: 4d8e1d2762df420ea55017d6f2ec08012b978a6f [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>
17#include <command.h>
18#include <linux/ctype.h>
19#include <net.h>
wdenk458ded32002-09-20 10:59:08 +000020#include <elf.h>
21
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020022#if defined(CONFIG_WALNUT) || defined(CONFIG_SYS_VXWORKS_MAC_PTR)
Wolfgang Denkd87080b2006-03-31 18:32:53 +020023DECLARE_GLOBAL_DATA_PTR;
24#endif
wdenk458ded32002-09-20 10:59:08 +000025
Mike Frysinger017e9b72008-04-13 19:42:18 -040026int valid_elf_image (unsigned long addr);
27unsigned long load_elf_image (unsigned long addr);
28
29/* Allow ports to override the default behavior */
30__attribute__((weak))
31unsigned long do_bootelf_exec (ulong (*entry)(int, char *[]), int argc, char *argv[])
Mike Frysinger1f1d88d2008-01-29 18:21:05 -050032{
Mike Frysinger017e9b72008-04-13 19:42:18 -040033 unsigned long ret;
34
Mike Frysinger1f1d88d2008-01-29 18:21:05 -050035 /*
36 * QNX images require the data cache is disabled.
37 * Data cache is already flushed, so just turn it off.
38 */
Mike Frysinger017e9b72008-04-13 19:42:18 -040039 int dcache = dcache_status ();
40 if (dcache)
Mike Frysinger1f1d88d2008-01-29 18:21:05 -050041 dcache_disable ();
42
Mike Frysinger017e9b72008-04-13 19:42:18 -040043 /*
44 * pass address parameter as argv[0] (aka command name),
45 * and all remaining args
46 */
47 ret = entry (argc, argv);
Mike Frysinger1f1d88d2008-01-29 18:21:05 -050048
Mike Frysinger017e9b72008-04-13 19:42:18 -040049 if (dcache)
50 dcache_enable ();
51
52 return ret;
53}
wdenk458ded32002-09-20 10:59:08 +000054
55/* ======================================================================
56 * Interpreter command to boot an arbitrary ELF image from memory.
57 * ====================================================================== */
58int do_bootelf (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
59{
60 unsigned long addr; /* Address of the ELF image */
61 unsigned long rc; /* Return value from user code */
62
63 /* -------------------------------------------------- */
64 int rcode = 0;
65
66 if (argc < 2)
67 addr = load_addr;
68 else
69 addr = simple_strtoul (argv[1], NULL, 16);
70
71 if (!valid_elf_image (addr))
72 return 1;
73
74 addr = load_elf_image (addr);
75
76 printf ("## Starting application at 0x%08lx ...\n", addr);
77
wdenk458ded32002-09-20 10:59:08 +000078 /*
79 * pass address parameter as argv[0] (aka command name),
80 * and all remaining args
81 */
Mike Frysinger017e9b72008-04-13 19:42:18 -040082 rc = do_bootelf_exec ((void *)addr, argc - 1, argv + 1);
wdenk458ded32002-09-20 10:59:08 +000083 if (rc != 0)
84 rcode = 1;
85
86 printf ("## Application terminated, rc = 0x%lx\n", rc);
87 return rcode;
88}
89
90/* ======================================================================
91 * Interpreter command to boot VxWorks from a memory image. The image can
92 * be either an ELF image or a raw binary. Will attempt to setup the
93 * bootline and other parameters correctly.
94 * ====================================================================== */
Stefan Roese1bdd4682006-11-29 12:53:15 +010095int do_bootvx (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
wdenk458ded32002-09-20 10:59:08 +000096{
wdenk458ded32002-09-20 10:59:08 +000097 unsigned long addr; /* Address of image */
98 unsigned long bootaddr; /* Address to put the bootline */
99 char *bootline; /* Text of the bootline */
100 char *tmp; /* Temporary char pointer */
101
102#if defined(CONFIG_4xx) || defined(CONFIG_IOP480)
103 char build_buf[80]; /* Buffer for building the bootline */
104#endif
105 /* -------------------------------------------------- */
106
107 /*
108 * Check the loadaddr variable.
109 * If we don't know where the image is then we're done.
110 */
111
Stefan Roese1bdd4682006-11-29 12:53:15 +0100112 if (argc < 2)
113 addr = load_addr;
114 else
115 addr = simple_strtoul (argv[1], NULL, 16);
wdenk458ded32002-09-20 10:59:08 +0000116
Jon Loeligerbaa26db2007-07-08 17:51:39 -0500117#if defined(CONFIG_CMD_NET)
wdenk458ded32002-09-20 10:59:08 +0000118 /* Check to see if we need to tftp the image ourselves before starting */
119
120 if ((argc == 2) && (strcmp (argv[1], "tftp") == 0)) {
wdenkeb9401e2002-11-11 02:11:37 +0000121 if (NetLoop (TFTP) <= 0)
wdenk458ded32002-09-20 10:59:08 +0000122 return 1;
123 printf ("Automatic boot of VxWorks image at address 0x%08lx ... \n", addr);
124 }
125#endif
126
127 /* This should equate
128 * to NV_RAM_ADRS + NV_BOOT_OFFSET + NV_ENET_OFFSET
129 * from the VxWorks BSP header files.
130 * This will vary from board to board
131 */
132
Stefan Roesec157d8e2005-08-01 16:41:48 +0200133#if defined(CONFIG_WALNUT)
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200134 tmp = (char *) CONFIG_SYS_NVRAM_BASE_ADDR + 0x500;
wdenk458ded32002-09-20 10:59:08 +0000135 memcpy ((char *) tmp, (char *) &gd->bd->bi_enetaddr[3], 3);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200136#elif defined(CONFIG_SYS_VXWORKS_MAC_PTR)
137 tmp = (char *) CONFIG_SYS_VXWORKS_MAC_PTR;
wdenk458ded32002-09-20 10:59:08 +0000138 memcpy ((char *) tmp, (char *) &gd->bd->bi_enetaddr[0], 6);
139#else
wdenk4b9206e2004-03-23 22:14:11 +0000140 puts ("## Ethernet MAC address not copied to NV RAM\n");
wdenk458ded32002-09-20 10:59:08 +0000141#endif
142
wdenk8bde7f72003-06-27 21:31:46 +0000143 /*
144 * Use bootaddr to find the location in memory that VxWorks
145 * will look for the bootline string. The default value for
146 * PowerPC is LOCAL_MEM_LOCAL_ADRS + BOOT_LINE_OFFSET which
147 * defaults to 0x4200
wdenk458ded32002-09-20 10:59:08 +0000148 */
149
150 if ((tmp = getenv ("bootaddr")) == NULL)
151 bootaddr = 0x4200;
152 else
153 bootaddr = simple_strtoul (tmp, NULL, 16);
154
wdenk8bde7f72003-06-27 21:31:46 +0000155 /*
156 * Check to see if the bootline is defined in the 'bootargs'
157 * parameter. If it is not defined, we may be able to
158 * construct the info
wdenk458ded32002-09-20 10:59:08 +0000159 */
160
161 if ((bootline = getenv ("bootargs")) != NULL) {
162 memcpy ((void *) bootaddr, bootline, MAX(strlen(bootline), 255));
163 flush_cache (bootaddr, MAX(strlen(bootline), 255));
164 } else {
165#if defined(CONFIG_4xx)
166 sprintf (build_buf, "ibmEmac(0,0)");
167
168 if ((tmp = getenv ("hostname")) != NULL) {
169 sprintf (&build_buf[strlen (build_buf - 1)],
170 "host:%s ", tmp);
171 } else {
172 sprintf (&build_buf[strlen (build_buf - 1)],
173 ": ");
174 }
175
176 if ((tmp = getenv ("ipaddr")) != NULL) {
177 sprintf (&build_buf[strlen (build_buf - 1)],
178 "e=%s ", tmp);
179 }
180 memcpy ((void *)bootaddr, build_buf, MAX(strlen(build_buf), 255));
181 flush_cache (bootaddr, MAX(strlen(build_buf), 255));
182#elif defined(CONFIG_IOP480)
183 sprintf (build_buf, "dc(0,0)");
184
185 if ((tmp = getenv ("hostname")) != NULL) {
186 sprintf (&build_buf[strlen (build_buf - 1)],
187 "host:%s ", tmp);
188 } else {
189 sprintf (&build_buf[strlen (build_buf - 1)],
190 ": ");
191 }
192
193 if ((tmp = getenv ("ipaddr")) != NULL) {
194 sprintf (&build_buf[strlen (build_buf - 1)],
195 "e=%s ", tmp);
196 }
197 memcpy ((void *) bootaddr, build_buf, MAX(strlen(build_buf), 255));
198 flush_cache (bootaddr, MAX(strlen(build_buf), 255));
199#else
200
wdenk8bde7f72003-06-27 21:31:46 +0000201 /*
202 * I'm not sure what the device should be for other
203 * PPC flavors, the hostname and ipaddr should be ok
204 * to just copy
wdenk458ded32002-09-20 10:59:08 +0000205 */
206
wdenk4b9206e2004-03-23 22:14:11 +0000207 puts ("No bootargs defined\n");
wdenk458ded32002-09-20 10:59:08 +0000208 return 1;
209#endif
210 }
211
wdenk8bde7f72003-06-27 21:31:46 +0000212 /*
213 * If the data at the load address is an elf image, then
214 * treat it like an elf image. Otherwise, assume that it is a
215 * binary image
wdenk458ded32002-09-20 10:59:08 +0000216 */
217
218 if (valid_elf_image (addr)) {
219 addr = load_elf_image (addr);
220 } else {
wdenk4b9206e2004-03-23 22:14:11 +0000221 puts ("## Not an ELF image, assuming binary\n");
wdenk458ded32002-09-20 10:59:08 +0000222 /* leave addr as load_addr */
223 }
224
225 printf ("## Using bootline (@ 0x%lx): %s\n", bootaddr,
226 (char *) bootaddr);
227 printf ("## Starting vxWorks at 0x%08lx ...\n", addr);
228
229 ((void (*)(void)) addr) ();
230
wdenk4b9206e2004-03-23 22:14:11 +0000231 puts ("## vxWorks terminated\n");
wdenk458ded32002-09-20 10:59:08 +0000232 return 1;
233}
234
235/* ======================================================================
236 * Determine if a valid ELF image exists at the given memory location.
237 * First looks at the ELF header magic field, the makes sure that it is
238 * executable and makes sure that it is for a PowerPC.
239 * ====================================================================== */
240int valid_elf_image (unsigned long addr)
241{
242 Elf32_Ehdr *ehdr; /* Elf header structure pointer */
243
244 /* -------------------------------------------------- */
245
246 ehdr = (Elf32_Ehdr *) addr;
247
248 if (!IS_ELF (*ehdr)) {
249 printf ("## No elf image at address 0x%08lx\n", addr);
250 return 0;
251 }
252
253 if (ehdr->e_type != ET_EXEC) {
254 printf ("## Not a 32-bit elf image at address 0x%08lx\n",
255 addr);
256 return 0;
257 }
258
wdenk6069ff22003-02-28 00:49:47 +0000259#if 0
wdenk458ded32002-09-20 10:59:08 +0000260 if (ehdr->e_machine != EM_PPC) {
261 printf ("## Not a PowerPC elf image at address 0x%08lx\n",
262 addr);
263 return 0;
264 }
wdenk6069ff22003-02-28 00:49:47 +0000265#endif
wdenk458ded32002-09-20 10:59:08 +0000266
267 return 1;
268}
269
270
271/* ======================================================================
272 * A very simple elf loader, assumes the image is valid, returns the
273 * entry point address.
274 * ====================================================================== */
275unsigned long load_elf_image (unsigned long addr)
276{
277 Elf32_Ehdr *ehdr; /* Elf header structure pointer */
278 Elf32_Shdr *shdr; /* Section header structure pointer */
279 unsigned char *strtab = 0; /* String table pointer */
280 unsigned char *image; /* Binary image pointer */
281 int i; /* Loop counter */
282
283 /* -------------------------------------------------- */
284
285 ehdr = (Elf32_Ehdr *) addr;
286
287 /* Find the section header string table for output info */
288 shdr = (Elf32_Shdr *) (addr + ehdr->e_shoff +
289 (ehdr->e_shstrndx * sizeof (Elf32_Shdr)));
290
291 if (shdr->sh_type == SHT_STRTAB)
292 strtab = (unsigned char *) (addr + shdr->sh_offset);
293
294 /* Load each appropriate section */
295 for (i = 0; i < ehdr->e_shnum; ++i) {
296 shdr = (Elf32_Shdr *) (addr + ehdr->e_shoff +
297 (i * sizeof (Elf32_Shdr)));
298
299 if (!(shdr->sh_flags & SHF_ALLOC)
300 || shdr->sh_addr == 0 || shdr->sh_size == 0) {
301 continue;
302 }
303
304 if (strtab) {
305 printf ("%sing %s @ 0x%08lx (%ld bytes)\n",
306 (shdr->sh_type == SHT_NOBITS) ?
307 "Clear" : "Load",
308 &strtab[shdr->sh_name],
309 (unsigned long) shdr->sh_addr,
310 (long) shdr->sh_size);
311 }
312
313 if (shdr->sh_type == SHT_NOBITS) {
314 memset ((void *)shdr->sh_addr, 0, shdr->sh_size);
315 } else {
316 image = (unsigned char *) addr + shdr->sh_offset;
317 memcpy ((void *) shdr->sh_addr,
318 (const void *) image,
319 shdr->sh_size);
320 }
321 flush_cache (shdr->sh_addr, shdr->sh_size);
322 }
323
324 return ehdr->e_entry;
325}
326
327/* ====================================================================== */
wdenk0d498392003-07-01 21:06:45 +0000328U_BOOT_CMD(
329 bootelf, 2, 0, do_bootelf,
wdenk8bde7f72003-06-27 21:31:46 +0000330 "bootelf - Boot from an ELF image in memory\n",
331 " [address] - load address of ELF image.\n"
332);
333
wdenk0d498392003-07-01 21:06:45 +0000334U_BOOT_CMD(
335 bootvx, 2, 0, do_bootvx,
wdenk8bde7f72003-06-27 21:31:46 +0000336 "bootvx - Boot vxWorks from an ELF image\n",
337 " [address] - load address of vxWorks ELF image.\n"
338);