Fully synchronize sys/boot from FreeBSD-5.x, but add / to the module path
[dragonfly.git] / sys / boot / alpha / libalpha / elf_freebsd.c
1 /*
2  * $NetBSD: loadfile.c,v 1.10 1998/06/25 06:45:46 ross Exp $
3  * $FreeBSD: src/sys/boot/alpha/libalpha/elf_freebsd.c,v 1.12 2003/05/01 03:56:28 peter Exp $
4  * $DragonFly: src/sys/boot/alpha/libalpha/Attic/elf_freebsd.c,v 1.3 2003/11/10 06:08:29 dillon Exp $
5  */
6
7 /*-
8  * Copyright (c) 1997 The NetBSD Foundation, Inc.
9  * All rights reserved.
10  *
11  * This code is derived from software contributed to The NetBSD Foundation
12  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
13  * NASA Ames Research Center.
14  *
15  * Redistribution and use in source and binary forms, with or without
16  * modification, are permitted provided that the following conditions
17  * are met:
18  * 1. Redistributions of source code must retain the above copyright
19  *    notice, this list of conditions and the following disclaimer.
20  * 2. Redistributions in binary form must reproduce the above copyright
21  *    notice, this list of conditions and the following disclaimer in the
22  *    documentation and/or other materials provided with the distribution.
23  * 3. All advertising materials mentioning features or use of this software
24  *    must display the following acknowledgement:
25  *      This product includes software developed by the NetBSD
26  *      Foundation, Inc. and its contributors.
27  * 4. Neither the name of The NetBSD Foundation nor the names of its
28  *    contributors may be used to endorse or promote products derived
29  *    from this software without specific prior written permission.
30  *
31  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
32  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
33  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
34  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
35  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
36  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
37  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
38  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
39  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41  * POSSIBILITY OF SUCH DAMAGE.
42  */
43
44 /*
45  * Copyright (c) 1992, 1993
46  *      The Regents of the University of California.  All rights reserved.
47  *
48  * This code is derived from software contributed to Berkeley by
49  * Ralph Campbell.
50  *
51  * Redistribution and use in source and binary forms, with or without
52  * modification, are permitted provided that the following conditions
53  * are met:
54  * 1. Redistributions of source code must retain the above copyright
55  *    notice, this list of conditions and the following disclaimer.
56  * 2. Redistributions in binary form must reproduce the above copyright
57  *    notice, this list of conditions and the following disclaimer in the
58  *    documentation and/or other materials provided with the distribution.
59  * 3. All advertising materials mentioning features or use of this software
60  *    must display the following acknowledgement:
61  *      This product includes software developed by the University of
62  *      California, Berkeley and its contributors.
63  * 4. Neither the name of the University nor the names of its contributors
64  *    may be used to endorse or promote products derived from this software
65  *    without specific prior written permission.
66  *
67  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
68  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
69  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
70  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
71  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
72  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
73  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
74  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
75  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
76  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
77  * SUCH DAMAGE.
78  *
79  *      @(#)boot.c      8.1 (Berkeley) 6/10/93
80  */
81
82 #include <stand.h>
83 #include <string.h>
84
85 #include <sys/param.h>
86 #include <sys/linker.h>
87 #include <machine/elf.h>
88 #include <machine/prom.h>
89 #include <machine/rpb.h>
90 #include <machine/bootinfo.h>
91
92 #include "bootstrap.h"
93
94 #define _KERNEL
95
96 static int      elf64_exec(struct preloaded_file *afp);
97 int             bi_load(struct bootinfo_v1 *, vm_offset_t *,
98                         struct preloaded_file *);
99
100 struct file_format alpha_elf = { elf64_loadfile, elf64_exec };
101
102 vm_offset_t ffp_save, ptbr_save;
103
104 static int
105 elf64_exec(struct preloaded_file *fp)
106 {
107     static struct bootinfo_v1   bootinfo_v1;
108     struct file_metadata        *md;
109     Elf_Ehdr                    *hdr;
110     int                         err;
111     int                         flen;
112
113     if ((md = file_findmetadata(fp, MODINFOMD_ELFHDR)) == NULL)
114         return(EFTYPE);                 /* XXX actually EFUCKUP */
115     hdr = (Elf_Ehdr *)&(md->md_data);
116
117     /* XXX ffp_save does not appear to be used in the kernel.. */
118     bzero(&bootinfo_v1, sizeof(bootinfo_v1));
119     err = bi_load(&bootinfo_v1, &ffp_save, fp);
120     if (err)
121         return(err);
122
123     /*
124      * Fill in rest of bootinfo for the kernel.
125      */
126     flen = prom_getenv(PROM_E_BOOTED_OSFLAGS, bootinfo_v1.boot_flags,
127         sizeof(bootinfo_v1.boot_flags));
128     bootinfo_v1.hwrpb = (void *)HWRPB_ADDR;
129     bootinfo_v1.hwrpbsize = ((struct rpb *)HWRPB_ADDR)->rpb_size;
130     bootinfo_v1.cngetc = NULL;
131     bootinfo_v1.cnputc = NULL;
132     bootinfo_v1.cnpollc = NULL;
133
134     /*
135      * Append the boot command flags.
136      */
137     if (fp->f_args != NULL && *fp->f_args != '\0') {
138         const char *p = fp->f_args;
139
140         do {
141             if (*p == '-') {
142                 while (*++p != ' ' && *p != '\0')
143                     if (flen < sizeof(bootinfo_v1.boot_flags) - 1)
144                         bootinfo_v1.boot_flags[flen++] = *p;
145             } else
146                 while (*p != ' ' && *p != '\0')
147                     p++;
148             while (*p == ' ')
149                 p++;
150         } while (*p != '\0');
151         bootinfo_v1.boot_flags[flen] = '\0';
152     }
153
154     printf("Entering %s at 0x%lx...\n", fp->f_name, hdr->e_entry);
155     closeall();
156     dev_cleanup();
157     alpha_pal_imb();
158     (*(void (*)())hdr->e_entry)(ffp_save, ptbr_save,
159                                BOOTINFO_MAGIC, &bootinfo_v1, 1, 0);
160 }
161
162
163