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