xref: /freebsd/stand/uboot/arch/powerpc/ppc64_elf_freebsd.c (revision 9dc70af83e5992e543542adbf5a6edeb38f187f6)
1*9dc70af8SWarner Losh /*-
2*9dc70af8SWarner Losh  * Copyright (c) 2001 Benno Rice <benno@FreeBSD.org>
3*9dc70af8SWarner Losh  * All rights reserved.
4*9dc70af8SWarner Losh  *
5*9dc70af8SWarner Losh  * Redistribution and use in source and binary forms, with or without
6*9dc70af8SWarner Losh  * modification, are permitted provided that the following conditions
7*9dc70af8SWarner Losh  * are met:
8*9dc70af8SWarner Losh  * 1. Redistributions of source code must retain the above copyright
9*9dc70af8SWarner Losh  *    notice, this list of conditions and the following disclaimer.
10*9dc70af8SWarner Losh  * 2. Redistributions in binary form must reproduce the above copyright
11*9dc70af8SWarner Losh  *    notice, this list of conditions and the following disclaimer in the
12*9dc70af8SWarner Losh  *    documentation and/or other materials provided with the distribution.
13*9dc70af8SWarner Losh  *
14*9dc70af8SWarner Losh  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15*9dc70af8SWarner Losh  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16*9dc70af8SWarner Losh  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17*9dc70af8SWarner Losh  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18*9dc70af8SWarner Losh  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19*9dc70af8SWarner Losh  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20*9dc70af8SWarner Losh  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21*9dc70af8SWarner Losh  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22*9dc70af8SWarner Losh  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23*9dc70af8SWarner Losh  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24*9dc70af8SWarner Losh  * SUCH DAMAGE.
25*9dc70af8SWarner Losh  */
26*9dc70af8SWarner Losh 
27*9dc70af8SWarner Losh #include <sys/cdefs.h>
28*9dc70af8SWarner Losh __FBSDID("$FreeBSD$");
29*9dc70af8SWarner Losh 
30*9dc70af8SWarner Losh #define __ELF_WORD_SIZE 64
31*9dc70af8SWarner Losh 
32*9dc70af8SWarner Losh #include <sys/param.h>
33*9dc70af8SWarner Losh #include <sys/linker.h>
34*9dc70af8SWarner Losh 
35*9dc70af8SWarner Losh #include <machine/metadata.h>
36*9dc70af8SWarner Losh #include <machine/elf.h>
37*9dc70af8SWarner Losh #include <machine/md_var.h>
38*9dc70af8SWarner Losh 
39*9dc70af8SWarner Losh #include <stand.h>
40*9dc70af8SWarner Losh 
41*9dc70af8SWarner Losh #include "bootstrap.h"
42*9dc70af8SWarner Losh #include "libuboot.h"
43*9dc70af8SWarner Losh 
44*9dc70af8SWarner Losh vm_offset_t md_load64(char *args, vm_offset_t *modulep, vm_offset_t *dtb);
45*9dc70af8SWarner Losh extern char		end[];
46*9dc70af8SWarner Losh extern vm_offset_t	reloc;	/* From <arch>/conf.c */
47*9dc70af8SWarner Losh 
48*9dc70af8SWarner Losh int
49*9dc70af8SWarner Losh ppc64_uboot_elf_loadfile(char *filename, uint64_t dest,
50*9dc70af8SWarner Losh     struct preloaded_file **result)
51*9dc70af8SWarner Losh {
52*9dc70af8SWarner Losh 	int	r;
53*9dc70af8SWarner Losh 
54*9dc70af8SWarner Losh 	r = __elfN(loadfile)(filename, dest, result);
55*9dc70af8SWarner Losh 	if (r != 0)
56*9dc70af8SWarner Losh 		return (r);
57*9dc70af8SWarner Losh 
58*9dc70af8SWarner Losh 	/*
59*9dc70af8SWarner Losh 	 * No need to sync the icache for modules: this will
60*9dc70af8SWarner Losh 	 * be done by the kernel after relocation.
61*9dc70af8SWarner Losh 	 */
62*9dc70af8SWarner Losh 	if (!strcmp((*result)->f_type, "elf kernel"))
63*9dc70af8SWarner Losh 		__syncicache((void *) (*result)->f_addr, (*result)->f_size);
64*9dc70af8SWarner Losh 	return (0);
65*9dc70af8SWarner Losh }
66*9dc70af8SWarner Losh 
67*9dc70af8SWarner Losh int
68*9dc70af8SWarner Losh ppc64_uboot_elf_exec(struct preloaded_file *fp)
69*9dc70af8SWarner Losh {
70*9dc70af8SWarner Losh 	struct file_metadata	*fmp;
71*9dc70af8SWarner Losh 	vm_offset_t		mdp, dtbp;
72*9dc70af8SWarner Losh 	Elf_Ehdr		*e;
73*9dc70af8SWarner Losh 	int			error;
74*9dc70af8SWarner Losh 	void			(*entry)(void *);
75*9dc70af8SWarner Losh 
76*9dc70af8SWarner Losh 	if ((fmp = file_findmetadata(fp, MODINFOMD_ELFHDR)) == NULL) {
77*9dc70af8SWarner Losh 		return(EFTYPE);
78*9dc70af8SWarner Losh 	}
79*9dc70af8SWarner Losh 	e = (Elf_Ehdr *)&fmp->md_data;
80*9dc70af8SWarner Losh 
81*9dc70af8SWarner Losh 	/* Handle function descriptor for ELFv1 kernels */
82*9dc70af8SWarner Losh 	if ((e->e_flags & 3) == 2)
83*9dc70af8SWarner Losh 		entry = (void (*)(void*))(intptr_t)e->e_entry;
84*9dc70af8SWarner Losh 	else
85*9dc70af8SWarner Losh 		entry = *(void (*)(void*))(uint64_t *)(intptr_t)e->e_entry;
86*9dc70af8SWarner Losh 
87*9dc70af8SWarner Losh 	if ((error = md_load64(fp->f_args, &mdp, &dtbp)) != 0)
88*9dc70af8SWarner Losh 		return (error);
89*9dc70af8SWarner Losh 
90*9dc70af8SWarner Losh 	dev_cleanup();
91*9dc70af8SWarner Losh 	printf("Kernel args: %s\n", fp->f_args);
92*9dc70af8SWarner Losh 
93*9dc70af8SWarner Losh 	(*entry)((void *)mdp);
94*9dc70af8SWarner Losh 	panic("exec returned");
95*9dc70af8SWarner Losh }
96*9dc70af8SWarner Losh 
97*9dc70af8SWarner Losh struct file_format	uboot_elf64 =
98*9dc70af8SWarner Losh {
99*9dc70af8SWarner Losh 	ppc64_uboot_elf_loadfile,
100*9dc70af8SWarner Losh 	ppc64_uboot_elf_exec
101*9dc70af8SWarner Losh };
102