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