1 /*-
2  * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 #include <stand.h>
29 #include <sys/param.h>
30 #include <sys/reboot.h>
31 #include <sys/linker.h>
32 #include <i386/include/bootinfo.h>
33 
34 #include "bootstrap.h"
35 #include "modinfo.h"
36 #include "libuserboot.h"
37 
38 #ifdef LOADER_GELI_SUPPORT
39 #include "geliboot.h"
40 #endif
41 
42 static struct bootinfo  bi;
43 
44 /*
45  * Load the information expected by an i386 kernel.
46  *
47  * - The 'boothowto' argument is constructed
48  * - The 'bootdev' argument is constructed
49  * - The 'bootinfo' struct is constructed, and copied into the kernel space.
50  * - The kernel environment is copied into kernel space.
51  * - Module metadata are formatted and placed in kernel space.
52  */
53 int
54 bi_load32(char *args, int *howtop, int *bootdevp, vm_offset_t *bip, vm_offset_t *modulep, vm_offset_t *kernendp)
55 {
56     struct preloaded_file	*xp, *kfp;
57     struct devdesc		*rootdev;
58     struct file_metadata	*md;
59     vm_offset_t			addr;
60     vm_offset_t			kernend;
61     vm_offset_t			envp;
62     vm_offset_t			size;
63     vm_offset_t			ssym, esym;
64     char			*rootdevname;
65     int				bootdevnr, howto;
66     char			*kernelname;
67     const char			*kernelpath;
68     uint64_t			lowmem, highmem;
69 
70     howto = bi_getboothowto(args);
71 
72     /*
73      * Allow the environment variable 'rootdev' to override the supplied device
74      * This should perhaps go to MI code and/or have $rootdev tested/set by
75      * MI code before launching the kernel.
76      */
77     rootdevname = getenv("rootdev");
78     userboot_getdev((void **)(&rootdev), rootdevname, NULL);
79     if (rootdev == NULL) {		/* bad $rootdev/$currdev */
80 	printf("can't determine root device\n");
81 	return(EINVAL);
82     }
83 
84     /* Try reading the /etc/fstab file to select the root device */
85     getrootmount(devformat(rootdev));
86 
87     bootdevnr = 0;
88 #if 0
89     if (bootdevnr == -1) {
90 	printf("root device %s invalid\n", devformat(rootdev));
91 	return (EINVAL);
92     }
93 #endif
94     free(rootdev);
95 
96     /* find the last module in the chain */
97     addr = 0;
98     for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) {
99 	if (addr < (xp->f_addr + xp->f_size))
100 	    addr = xp->f_addr + xp->f_size;
101     }
102     /* pad to a page boundary */
103     addr = roundup(addr, PAGE_SIZE);
104 
105     /* copy our environment */
106     envp = addr;
107     addr = md_copyenv(addr);
108 
109     /* pad to a page boundary */
110     addr = roundup(addr, PAGE_SIZE);
111 
112     kfp = file_findfile(NULL, "elf kernel");
113     if (kfp == NULL)
114       kfp = file_findfile(NULL, "elf32 kernel");
115     if (kfp == NULL)
116 	panic("can't find kernel file");
117     kernend = 0;	/* fill it in later */
118     file_addmetadata(kfp, MODINFOMD_HOWTO, sizeof howto, &howto);
119     file_addmetadata(kfp, MODINFOMD_ENVP, sizeof envp, &envp);
120     file_addmetadata(kfp, MODINFOMD_KERNEND, sizeof kernend, &kernend);
121     bios_addsmapdata(kfp);
122 #ifdef LOADER_GELI_SUPPORT
123     geli_export_key_metadata(kfp);
124 #endif
125 
126     /* Figure out the size and location of the metadata */
127     *modulep = addr;
128     size = md_copymodules(0, false);
129     kernend = roundup(addr + size, PAGE_SIZE);
130     *kernendp = kernend;
131 
132     /* patch MODINFOMD_KERNEND */
133     md = file_findmetadata(kfp, MODINFOMD_KERNEND);
134     bcopy(&kernend, md->md_data, sizeof kernend);
135 
136     /* copy module list and metadata */
137     (void)md_copymodules(addr, false);
138 
139     ssym = esym = 0;
140     md = file_findmetadata(kfp, MODINFOMD_SSYM);
141     if (md != NULL)
142 	ssym = *((vm_offset_t *)&(md->md_data));
143     md = file_findmetadata(kfp, MODINFOMD_ESYM);
144     if (md != NULL)
145 	esym = *((vm_offset_t *)&(md->md_data));
146     if (ssym == 0 || esym == 0)
147 	ssym = esym = 0;		/* sanity */
148 
149     /* legacy bootinfo structure */
150     kernelname = getenv("kernelname");
151     userboot_getdev(NULL, kernelname, &kernelpath);
152     bi.bi_version = BOOTINFO_VERSION;
153     bi.bi_size = sizeof(bi);
154     CALLBACK(getmem, &lowmem, &highmem);
155     bi.bi_memsizes_valid = 1;
156     bi.bi_basemem = 640;
157     bi.bi_extmem = (lowmem - 0x100000) / 1024;
158     bi.bi_envp = envp;
159     bi.bi_modulep = *modulep;
160     bi.bi_kernend = kernend;
161     bi.bi_symtab = ssym;       /* XXX this is only the primary kernel symtab */
162     bi.bi_esymtab = esym;
163 
164     /*
165      * Copy the legacy bootinfo and kernel name to the guest at 0x2000
166      */
167     bi.bi_kernelname = 0x2000 + sizeof(bi);
168     CALLBACK(copyin, &bi, 0x2000, sizeof(bi));
169     CALLBACK(copyin, kernelname, 0x2000 + sizeof(bi), strlen(kernelname) + 1);
170 
171     /* legacy boot arguments */
172     *howtop = howto | RB_BOOTINFO;
173     *bootdevp = bootdevnr;
174     *bip = 0x2000;
175 
176     return(0);
177 }
178