bootinfo64.c revision 162814
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: head/sys/boot/i386/libi386/bootinfo64.c 162814 2006-09-29 20:27:41Z ru $");
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 "bootstrap.h"
36#include "libi386.h"
37#include "btxv86.h"
38
39/*
40 * Copy module-related data into the load area, where it can be
41 * used as a directory for loaded modules.
42 *
43 * Module data is presented in a self-describing format.  Each datum
44 * is preceded by a 32-bit identifier and a 32-bit size field.
45 *
46 * Currently, the following data are saved:
47 *
48 * MOD_NAME	(variable)		module name (string)
49 * MOD_TYPE	(variable)		module type (string)
50 * MOD_ARGS	(variable)		module parameters (string)
51 * MOD_ADDR	sizeof(vm_offset_t)	module load address
52 * MOD_SIZE	sizeof(size_t)		module size
53 * MOD_METADATA	(variable)		type-specific metadata
54 */
55#define COPY32(v, a, c) {			\
56    u_int32_t	x = (v);			\
57    if (c)					\
58	i386_copyin(&x, a, sizeof(x));		\
59    a += sizeof(x);				\
60}
61
62#define MOD_STR(t, a, s, c) {			\
63    COPY32(t, a, c);				\
64    COPY32(strlen(s) + 1, a, c);		\
65    if (c)					\
66	i386_copyin(s, a, strlen(s) + 1);	\
67    a += roundup(strlen(s) + 1, sizeof(u_int64_t));\
68}
69
70#define MOD_NAME(a, s, c)	MOD_STR(MODINFO_NAME, a, s, c)
71#define MOD_TYPE(a, s, c)	MOD_STR(MODINFO_TYPE, a, s, c)
72#define MOD_ARGS(a, s, c)	MOD_STR(MODINFO_ARGS, a, s, c)
73
74#define MOD_VAR(t, a, s, c) {			\
75    COPY32(t, a, c);				\
76    COPY32(sizeof(s), a, c);			\
77    if (c)					\
78	i386_copyin(&s, a, sizeof(s));		\
79    a += roundup(sizeof(s), sizeof(u_int64_t));	\
80}
81
82#define MOD_ADDR(a, s, c)	MOD_VAR(MODINFO_ADDR, a, s, c)
83#define MOD_SIZE(a, s, c)	MOD_VAR(MODINFO_SIZE, a, s, c)
84
85#define MOD_METADATA(a, mm, c) {		\
86    COPY32(MODINFO_METADATA | mm->md_type, a, c); \
87    COPY32(mm->md_size, a, c);			\
88    if (c)					\
89	i386_copyin(mm->md_data, a, mm->md_size); \
90    a += roundup(mm->md_size, sizeof(u_int64_t));\
91}
92
93#define MOD_END(a, c) {				\
94    COPY32(MODINFO_END, a, c);			\
95    COPY32(0, a, c);				\
96}
97
98static vm_offset_t
99bi_copymodules64(vm_offset_t addr)
100{
101    struct preloaded_file	*fp;
102    struct file_metadata	*md;
103    int				c;
104    u_int64_t			v;
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	v = fp->f_addr;
115	MOD_ADDR(addr, v, c);
116	v = fp->f_size;
117	MOD_SIZE(addr, v, c);
118	for (md = fp->f_metadata; md != NULL; md = md->md_next)
119	    if (!(md->md_type & MODINFOMD_NOCOPY))
120		MOD_METADATA(addr, md, c);
121    }
122    MOD_END(addr, c);
123    return(addr);
124}
125
126/*
127 * Load the information expected by an i386 kernel.
128 *
129 * - The 'boothowto' argument is constructed
130 * - The 'bootdev' argument is constructed
131 * - The 'bootinfo' struct is constructed, and copied into the kernel space.
132 * - The kernel environment is copied into kernel space.
133 * - Module metadata are formatted and placed in kernel space.
134 */
135int
136bi_load64(char *args, vm_offset_t *modulep, vm_offset_t *kernendp)
137{
138    struct preloaded_file	*xp, *kfp;
139    struct i386_devdesc		*rootdev;
140    struct file_metadata	*md;
141    vm_offset_t			addr;
142    u_int64_t			kernend;
143    u_int64_t			envp;
144    vm_offset_t			size;
145    char			*rootdevname;
146    int				howto;
147
148    howto = bi_getboothowto(args);
149
150    /*
151     * Allow the environment variable 'rootdev' to override the supplied device
152     * This should perhaps go to MI code and/or have $rootdev tested/set by
153     * MI code before launching the kernel.
154     */
155    rootdevname = getenv("rootdev");
156    i386_getdev((void **)(&rootdev), rootdevname, NULL);
157    if (rootdev == NULL) {		/* bad $rootdev/$currdev */
158	printf("can't determine root device\n");
159	return(EINVAL);
160    }
161
162    /* Try reading the /etc/fstab file to select the root device */
163    getrootmount(i386_fmtdev((void *)rootdev));
164
165    /* find the last module in the chain */
166    addr = 0;
167    for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) {
168	if (addr < (xp->f_addr + xp->f_size))
169	    addr = xp->f_addr + xp->f_size;
170    }
171    /* pad to a page boundary */
172    addr = roundup(addr, PAGE_SIZE);
173
174    /* copy our environment */
175    envp = addr;
176    addr = bi_copyenv(addr);
177
178    /* pad to a page boundary */
179    addr = roundup(addr, PAGE_SIZE);
180
181    kfp = file_findfile(NULL, "elf kernel");
182    if (kfp == NULL)
183      kfp = file_findfile(NULL, "elf64 kernel");
184    if (kfp == NULL)
185	panic("can't find kernel file");
186    kernend = 0;	/* fill it in later */
187    file_addmetadata(kfp, MODINFOMD_HOWTO, sizeof howto, &howto);
188    file_addmetadata(kfp, MODINFOMD_ENVP, sizeof envp, &envp);
189    file_addmetadata(kfp, MODINFOMD_KERNEND, sizeof kernend, &kernend);
190    bios_addsmapdata(kfp);
191
192    /* Figure out the size and location of the metadata */
193    *modulep = addr;
194    size = bi_copymodules64(0);
195    kernend = roundup(addr + size, PAGE_SIZE);
196    *kernendp = kernend;
197
198    /* patch MODINFOMD_KERNEND */
199    md = file_findmetadata(kfp, MODINFOMD_KERNEND);
200    bcopy(&kernend, md->md_data, sizeof kernend);
201
202    /* copy module list and metadata */
203    (void)bi_copymodules64(addr);
204
205    return(0);
206}
207