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