xref: /illumos-gate/usr/src/boot/i386/libi386/bootinfo32.c (revision 63f91fbc3c024870d86dc3332a4a0080fb29bc40)
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 
29 #include <stand.h>
30 #include <sys/param.h>
31 #include <sys/reboot.h>
32 #include <sys/linker.h>
33 #include <machine/bootinfo.h>
34 #include "bootstrap.h"
35 #include "libi386.h"
36 #include "btxv86.h"
37 
38 static struct bootinfo  bi;
39 
40 /*
41  * Copy module-related data into the load area, where it can be
42  * used as a directory for loaded modules.
43  *
44  * Module data is presented in a self-describing format.  Each datum
45  * is preceded by a 32-bit identifier and a 32-bit size field.
46  *
47  * Currently, the following data are saved:
48  *
49  * MOD_NAME	(variable)		module name (string)
50  * MOD_TYPE	(variable)		module type (string)
51  * MOD_ARGS	(variable)		module parameters (string)
52  * MOD_ADDR	sizeof(vm_offset_t)	module load address
53  * MOD_SIZE	sizeof(size_t)		module size
54  * MOD_METADATA	(variable)		type-specific metadata
55  */
56 #define COPY32(v, a, c) {			\
57     u_int32_t	x = (v);			\
58     if (c)					\
59 	i386_copyin(&x, a, sizeof(x));		\
60     a += sizeof(x);				\
61 }
62 
63 #define MOD_STR(t, a, s, c) {			\
64     COPY32(t, a, c);				\
65     COPY32(strlen(s) + 1, a, c);		\
66     if (c)					\
67 	i386_copyin(s, a, strlen(s) + 1);	\
68     a += roundup(strlen(s) + 1, sizeof(u_long));\
69 }
70 
71 #define MOD_NAME(a, s, c)	MOD_STR(MODINFO_NAME, a, s, c)
72 #define MOD_TYPE(a, s, c)	MOD_STR(MODINFO_TYPE, a, s, c)
73 #define MOD_ARGS(a, s, c)	MOD_STR(MODINFO_ARGS, a, s, c)
74 
75 #define MOD_VAR(t, a, s, c) {			\
76     COPY32(t, a, c);				\
77     COPY32(sizeof(s), a, c);			\
78     if (c)					\
79 	i386_copyin(&s, a, sizeof(s));		\
80     a += roundup(sizeof(s), sizeof(u_long));	\
81 }
82 
83 #define MOD_ADDR(a, s, c)	MOD_VAR(MODINFO_ADDR, a, s, c)
84 #define MOD_SIZE(a, s, c)	MOD_VAR(MODINFO_SIZE, a, s, c)
85 
86 #define MOD_METADATA(a, mm, c) {		\
87     COPY32(MODINFO_METADATA | mm->md_type, a, c); \
88     COPY32(mm->md_size, a, c);			\
89     if (c)					\
90 	i386_copyin(mm->md_data, a, mm->md_size); \
91     a += roundup(mm->md_size, sizeof(u_long));\
92 }
93 
94 #define MOD_END(a, c) {				\
95     COPY32(MODINFO_END, a, c);			\
96     COPY32(0, a, c);				\
97 }
98 
99 static vm_offset_t
100 bi_copymodules32(vm_offset_t addr)
101 {
102     struct preloaded_file	*fp;
103     struct file_metadata	*md;
104     int				c;
105 
106     c = addr != 0;
107     /* start with the first module on the list, should be the kernel */
108     for (fp = file_findfile(NULL, NULL); fp != NULL; fp = fp->f_next) {
109 
110 	MOD_NAME(addr, fp->f_name, c);	/* this field must come first */
111 	MOD_TYPE(addr, fp->f_type, c);
112 	if (fp->f_args)
113 	    MOD_ARGS(addr, fp->f_args, c);
114 	MOD_ADDR(addr, fp->f_addr, c);
115 	MOD_SIZE(addr, fp->f_size, c);
116 	for (md = fp->f_metadata; md != NULL; md = md->md_next)
117 	    if (!(md->md_type & MODINFOMD_NOCOPY))
118 		MOD_METADATA(addr, md, c);
119     }
120     MOD_END(addr, c);
121     return(addr);
122 }
123 
124 /*
125  * Load the information expected by an i386 kernel.
126  *
127  * - The 'boothowto' argument is constructed
128  * - The 'bootdev' argument is constructed
129  * - The 'bootinfo' struct is constructed, and copied into the kernel space.
130  * - The kernel environment is copied into kernel space.
131  * - Module metadata are formatted and placed in kernel space.
132  */
133 int
134 bi_load32(char *args, int *howtop, int *bootdevp, vm_offset_t *bip, vm_offset_t *modulep, vm_offset_t *kernendp)
135 {
136     struct preloaded_file	*xp, *kfp;
137     struct i386_devdesc		*rootdev;
138     struct file_metadata	*md;
139     vm_offset_t			addr;
140     vm_offset_t			kernend;
141     vm_offset_t			envp;
142     vm_offset_t			size;
143     vm_offset_t			ssym, esym;
144     char			*rootdevname;
145     int				bootdevnr, i, howto;
146     char			*kernelname;
147     const char			*kernelpath;
148 
149     howto = bi_getboothowto(args);
150 
151     /*
152      * Allow the environment variable 'rootdev' to override the supplied device
153      * This should perhaps go to MI code and/or have $rootdev tested/set by
154      * MI code before launching the kernel.
155      */
156     rootdevname = getenv("rootdev");
157     i386_getdev((void **)(&rootdev), rootdevname, NULL);
158     if (rootdev == NULL) {		/* bad $rootdev/$currdev */
159 	printf("can't determine root device\n");
160 	return(EINVAL);
161     }
162 
163     /* Try reading the /etc/fstab file to select the root device */
164     getrootmount(i386_fmtdev((void *)rootdev));
165 
166     /* Do legacy rootdev guessing */
167 
168     /* XXX - use a default bootdev of 0.  Is this ok??? */
169     bootdevnr = 0;
170 
171     switch(rootdev->dd.d_dev->dv_type) {
172     case DEVT_CD:
173     case DEVT_DISK:
174 	/* pass in the BIOS device number of the current disk */
175 	bi.bi_bios_dev = bd_unit2bios(rootdev);
176 	bootdevnr = bd_getdev(rootdev);
177 	break;
178 
179     case DEVT_NET:
180     case DEVT_ZFS:
181 	    break;
182 
183     default:
184 	printf("WARNING - don't know how to boot from device type %d\n",
185 	    rootdev->dd.d_dev->dv_type);
186     }
187     if (bootdevnr == -1) {
188 	printf("root device %s invalid\n", i386_fmtdev(rootdev));
189 	return (EINVAL);
190     }
191     free(rootdev);
192 
193     /* find the last module in the chain */
194     addr = 0;
195     for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) {
196 	if (addr < (xp->f_addr + xp->f_size))
197 	    addr = xp->f_addr + xp->f_size;
198     }
199     /* pad to a page boundary */
200     addr = roundup(addr, PAGE_SIZE);
201 
202     /* copy our environment */
203     envp = addr;
204     addr = bi_copyenv(addr);
205 
206     /* pad to a page boundary */
207     addr = roundup(addr, PAGE_SIZE);
208 
209     kfp = file_findfile(NULL, "elf kernel");
210     if (kfp == NULL)
211       kfp = file_findfile(NULL, "elf32 kernel");
212     if (kfp == NULL)
213 	panic("can't find kernel file");
214     kernend = 0;	/* fill it in later */
215     file_addmetadata(kfp, MODINFOMD_HOWTO, sizeof howto, &howto);
216     file_addmetadata(kfp, MODINFOMD_ENVP, sizeof envp, &envp);
217     file_addmetadata(kfp, MODINFOMD_KERNEND, sizeof kernend, &kernend);
218     bios_addsmapdata(kfp);
219 
220     /* Figure out the size and location of the metadata */
221     *modulep = addr;
222     size = bi_copymodules32(0);
223     kernend = roundup(addr + size, PAGE_SIZE);
224     *kernendp = kernend;
225 
226     /* patch MODINFOMD_KERNEND */
227     md = file_findmetadata(kfp, MODINFOMD_KERNEND);
228     bcopy(&kernend, md->md_data, sizeof kernend);
229 
230     /* copy module list and metadata */
231     (void)bi_copymodules32(addr);
232 
233     ssym = esym = 0;
234     md = file_findmetadata(kfp, MODINFOMD_SSYM);
235     if (md != NULL)
236 	bcopy(&md->md_data, &ssym, sizeof (vm_offset_t));
237     md = file_findmetadata(kfp, MODINFOMD_ESYM);
238     if (md != NULL)
239 	bcopy(&md->md_data, &esym, sizeof (vm_offset_t));
240     if (ssym == 0 || esym == 0)
241 	ssym = esym = 0;		/* sanity */
242 
243     /* legacy bootinfo structure */
244     kernelname = getenv("kernelname");
245     i386_getdev(NULL, kernelname, &kernelpath);
246     bi.bi_version = BOOTINFO_VERSION;
247     bi.bi_kernelname = 0;		/* XXX char * -> kernel name */
248     bi.bi_nfs_diskless = 0;		/* struct nfs_diskless * */
249     bi.bi_n_bios_used = 0;		/* XXX would have to hook biosdisk driver for these */
250     for (i = 0; i < N_BIOS_GEOM; i++)
251         bi.bi_bios_geom[i] = bd_getbigeom(i);
252     bi.bi_size = sizeof(bi);
253     bi.bi_memsizes_valid = 1;
254     bi.bi_basemem = bios_basemem / 1024;
255     bi.bi_extmem = bios_extmem / 1024;
256     bi.bi_envp = envp;
257     bi.bi_modulep = *modulep;
258     bi.bi_kernend = kernend;
259     bi.bi_kernelname = VTOP(kernelpath);
260     bi.bi_symtab = ssym;       /* XXX this is only the primary kernel symtab */
261     bi.bi_esymtab = esym;
262 
263     /* legacy boot arguments */
264     *howtop = howto | RB_BOOTINFO;
265     *bootdevp = bootdevnr;
266     *bip = VTOP(&bi);
267 
268     return(0);
269 }
270