]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - common/cmd_elf.c
Consolidate MAX/MIN definitions
[karo-tx-uboot.git] / common / cmd_elf.c
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>
18 #include <linux/ctype.h>
19 #include <net.h>
20 #include <elf.h>
21
22 #if defined(CONFIG_WALNUT) || defined(CONFIG_SYS_VXWORKS_MAC_PTR)
23 DECLARE_GLOBAL_DATA_PTR;
24 #endif
25
26 int valid_elf_image (unsigned long addr);
27 unsigned long load_elf_image (unsigned long addr);
28
29 /* Allow ports to override the default behavior */
30 __attribute__((weak))
31 unsigned long do_bootelf_exec (ulong (*entry)(int, char *[]), int argc, char *argv[])
32 {
33         unsigned long ret;
34
35         /*
36          * QNX images require the data cache is disabled.
37          * Data cache is already flushed, so just turn it off.
38          */
39         int dcache = dcache_status ();
40         if (dcache)
41                 dcache_disable ();
42
43         /*
44          * pass address parameter as argv[0] (aka command name),
45          * and all remaining args
46          */
47         ret = entry (argc, argv);
48
49         if (dcache)
50                 dcache_enable ();
51
52         return ret;
53 }
54
55 /* ======================================================================
56  * Interpreter command to boot an arbitrary ELF image from memory.
57  * ====================================================================== */
58 int 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
78         /*
79          * pass address parameter as argv[0] (aka command name),
80          * and all remaining args
81          */
82         rc = do_bootelf_exec ((void *)addr, argc - 1, argv + 1);
83         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  * ====================================================================== */
95 int do_bootvx (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
96 {
97         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
112         if (argc < 2)
113                 addr = load_addr;
114         else
115                 addr = simple_strtoul (argv[1], NULL, 16);
116
117 #if defined(CONFIG_CMD_NET)
118         /* Check to see if we need to tftp the image ourselves before starting */
119
120         if ((argc == 2) && (strcmp (argv[1], "tftp") == 0)) {
121                 if (NetLoop (TFTP) <= 0)
122                         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
133 #if defined(CONFIG_WALNUT)
134         tmp = (char *) CONFIG_SYS_NVRAM_BASE_ADDR + 0x500;
135         memcpy ((char *) tmp, (char *) &gd->bd->bi_enetaddr[3], 3);
136 #elif defined(CONFIG_SYS_VXWORKS_MAC_PTR)
137         tmp = (char *) CONFIG_SYS_VXWORKS_MAC_PTR;
138         memcpy ((char *) tmp, (char *) &gd->bd->bi_enetaddr[0], 6);
139 #else
140         puts ("## Ethernet MAC address not copied to NV RAM\n");
141 #endif
142
143         /*
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
148          */
149
150         if ((tmp = getenv ("bootaddr")) == NULL)
151                 bootaddr = 0x4200;
152         else
153                 bootaddr = simple_strtoul (tmp, NULL, 16);
154
155         /*
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
159          */
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
201                 /*
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
205                  */
206
207                 puts ("No bootargs defined\n");
208                 return 1;
209 #endif
210         }
211
212         /*
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
216          */
217
218         if (valid_elf_image (addr)) {
219                 addr = load_elf_image (addr);
220         } else {
221                 puts ("## Not an ELF image, assuming binary\n");
222                 /* 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
231         puts ("## vxWorks terminated\n");
232         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  * ====================================================================== */
240 int 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
259 #if 0
260         if (ehdr->e_machine != EM_PPC) {
261                 printf ("## Not a PowerPC elf image at address 0x%08lx\n",
262                         addr);
263                 return 0;
264         }
265 #endif
266
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  * ====================================================================== */
275 unsigned 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 /* ====================================================================== */
328 U_BOOT_CMD(
329         bootelf,      2,      0,      do_bootelf,
330         "bootelf - Boot from an ELF image in memory\n",
331         " [address] - load address of ELF image.\n"
332 );
333
334 U_BOOT_CMD(
335         bootvx,      2,      0,      do_bootvx,
336         "bootvx  - Boot vxWorks from an ELF image\n",
337         " [address] - load address of vxWorks ELF image.\n"
338 );