xref: /freebsd/stand/i386/libi386/bootinfo32.c (revision e895ab3f)
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 __FBSDID("$FreeBSD$");
29 
30 #include <stand.h>
31 #include <sys/param.h>
32 #include <sys/reboot.h>
33 #include <sys/linker.h>
34 #include <machine/bootinfo.h>
35 #include <machine/metadata.h>
36 #include "bootstrap.h"
37 #include "modinfo.h"
38 #include "libi386.h"
39 #include "btxv86.h"
40 
41 #ifdef LOADER_GELI_SUPPORT
42 #include "geliboot.h"
43 #endif
44 
45 static struct bootinfo  bi;
46 
47 /*
48  * Load the information expected by an i386 kernel.
49  *
50  * - The 'boothowto' argument is constructed
51  * - The 'bootdev' argument is constructed
52  * - The 'bootinfo' struct is constructed, and copied into the kernel space.
53  * - The kernel environment is copied into kernel space.
54  * - Module metadata are formatted and placed in kernel space.
55  */
56 int
57 bi_load32(char *args, int *howtop, int *bootdevp, vm_offset_t *bip, vm_offset_t *modulep, vm_offset_t *kernendp)
58 {
59     struct preloaded_file	*xp, *kfp;
60     struct i386_devdesc		*rootdev;
61     struct file_metadata	*md;
62     vm_offset_t			addr;
63     vm_offset_t			kernend;
64     vm_offset_t			envp;
65     vm_offset_t			size;
66     vm_offset_t			ssym, esym;
67     char			*rootdevname;
68     int				bootdevnr, i, howto;
69     char			*kernelname;
70     const char			*kernelpath;
71 
72     howto = bi_getboothowto(args);
73 
74     /*
75      * Allow the environment variable 'rootdev' to override the supplied device
76      * This should perhaps go to MI code and/or have $rootdev tested/set by
77      * MI code before launching the kernel.
78      */
79     rootdevname = getenv("rootdev");
80     i386_getdev((void **)(&rootdev), rootdevname, NULL);
81     if (rootdev == NULL) {		/* bad $rootdev/$currdev */
82 	printf("can't determine root device\n");
83 	return(EINVAL);
84     }
85 
86     /* Try reading the /etc/fstab file to select the root device */
87     getrootmount(devformat(&rootdev->dd));
88 
89     /* Do legacy rootdev guessing */
90 
91     /* XXX - use a default bootdev of 0.  Is this ok??? */
92     bootdevnr = 0;
93 
94     switch(rootdev->dd.d_dev->dv_type) {
95     case DEVT_CD:
96     case DEVT_DISK:
97 	/* pass in the BIOS device number of the current disk */
98 	bi.bi_bios_dev = bd_unit2bios(rootdev);
99 	bootdevnr = bd_getdev(rootdev);
100 	break;
101 
102     case DEVT_NET:
103     case DEVT_ZFS:
104 	    break;
105 
106     default:
107 	printf("WARNING - don't know how to boot from device type %d\n",
108 	    rootdev->dd.d_dev->dv_type);
109     }
110     if (bootdevnr == -1) {
111 	printf("root device %s invalid\n", devformat(&rootdev->dd));
112 	return (EINVAL);
113     }
114     free(rootdev);
115 
116     /* find the last module in the chain */
117     addr = 0;
118     for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) {
119 	if (addr < (xp->f_addr + xp->f_size))
120 	    addr = xp->f_addr + xp->f_size;
121     }
122     /* pad to a page boundary */
123     addr = roundup(addr, PAGE_SIZE);
124 
125     addr = build_font_module(addr);
126 
127     /* copy our environment */
128     envp = addr;
129     addr = md_copyenv(addr);
130 
131     /* pad to a page boundary */
132     addr = roundup(addr, PAGE_SIZE);
133 
134     kfp = file_findfile(NULL, "elf kernel");
135     if (kfp == NULL)
136       kfp = file_findfile(NULL, "elf32 kernel");
137     if (kfp == NULL)
138 	panic("can't find kernel file");
139     kernend = 0;	/* fill it in later */
140     file_addmetadata(kfp, MODINFOMD_HOWTO, sizeof howto, &howto);
141     file_addmetadata(kfp, MODINFOMD_ENVP, sizeof envp, &envp);
142     file_addmetadata(kfp, MODINFOMD_KERNEND, sizeof kernend, &kernend);
143     bios_addsmapdata(kfp);
144 #ifdef LOADER_GELI_SUPPORT
145     geli_export_key_metadata(kfp);
146 #endif
147     bi_load_vbe_data(kfp);
148 
149     /* Figure out the size and location of the metadata */
150     *modulep = addr;
151     size = md_copymodules(0, false);
152     kernend = roundup(addr + size, PAGE_SIZE);
153     *kernendp = kernend;
154 
155     /* patch MODINFOMD_KERNEND */
156     md = file_findmetadata(kfp, MODINFOMD_KERNEND);
157     bcopy(&kernend, md->md_data, sizeof kernend);
158 
159     /* copy module list and metadata */
160     (void)md_copymodules(addr, false);
161 
162     ssym = esym = 0;
163     md = file_findmetadata(kfp, MODINFOMD_SSYM);
164     if (md != NULL)
165 	ssym = *((vm_offset_t *)&(md->md_data));
166     md = file_findmetadata(kfp, MODINFOMD_ESYM);
167     if (md != NULL)
168 	esym = *((vm_offset_t *)&(md->md_data));
169     if (ssym == 0 || esym == 0)
170 	ssym = esym = 0;		/* sanity */
171 
172     /* legacy bootinfo structure */
173     kernelname = getenv("kernelname");
174     i386_getdev(NULL, kernelname, &kernelpath);
175     bi.bi_version = BOOTINFO_VERSION;
176     for (i = 0; i < N_BIOS_GEOM; i++)
177         bi.bi_bios_geom[i] = bd_getbigeom(i);
178     bi.bi_size = sizeof(bi);
179     bi.bi_memsizes_valid = 1;
180     bi.bi_basemem = bios_basemem / 1024;
181     bi.bi_extmem = bios_extmem / 1024;
182     bi.bi_envp = envp;
183     bi.bi_modulep = *modulep;
184     bi.bi_kernend = kernend;
185     bi.bi_kernelname = VTOP(kernelpath);
186     bi.bi_symtab = ssym;       /* XXX this is only the primary kernel symtab */
187     bi.bi_esymtab = esym;
188 
189     /* legacy boot arguments */
190     *howtop = howto | RB_BOOTINFO;
191     *bootdevp = bootdevnr;
192     *bip = VTOP(&bi);
193 
194     return(0);
195 }
196