- Add support for attaching alternative DLTs to an interface.
[dragonfly.git] / sys / boot / i386 / libi386 / bootinfo64.c
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  * $FreeBSD: src/sys/boot/i386/libi386/bootinfo64.c,v 1.36 2003/08/25 23:28:31 obrien Exp $
27  * $DragonFly: src/sys/boot/i386/libi386/Attic/bootinfo64.c,v 1.1 2003/11/10 06:08:36 dillon Exp $
28  */
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
98 static vm_offset_t
99 bi_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  */
135 int
136 bi_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                         i, howto;
147     char                        *kernelname;
148     const char                  *kernelpath;
149
150     howto = bi_getboothowto(args);
151
152     /* 
153      * Allow the environment variable 'rootdev' to override the supplied device 
154      * This should perhaps go to MI code and/or have $rootdev tested/set by
155      * MI code before launching the kernel.
156      */
157     rootdevname = getenv("rootdev");
158     i386_getdev((void **)(&rootdev), rootdevname, NULL);
159     if (rootdev == NULL) {              /* bad $rootdev/$currdev */
160         printf("can't determine root device\n");
161         return(EINVAL);
162     }
163
164     /* Try reading the /etc/fstab file to select the root device */
165     getrootmount(i386_fmtdev((void *)rootdev));
166
167     /* find the last module in the chain */
168     addr = 0;
169     for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) {
170         if (addr < (xp->f_addr + xp->f_size))
171             addr = xp->f_addr + xp->f_size;
172     }
173     /* pad to a page boundary */
174     addr = roundup(addr, PAGE_SIZE);
175
176     /* copy our environment */
177     envp = addr;
178     addr = bi_copyenv(addr);
179
180     /* pad to a page boundary */
181     addr = roundup(addr, PAGE_SIZE);
182
183     kfp = file_findfile(NULL, "elf kernel");
184     if (kfp == NULL)
185       kfp = file_findfile(NULL, "elf64 kernel");
186     if (kfp == NULL)
187         panic("can't find kernel file");
188     kernend = 0;        /* fill it in later */
189     file_addmetadata(kfp, MODINFOMD_HOWTO, sizeof howto, &howto);
190     file_addmetadata(kfp, MODINFOMD_ENVP, sizeof envp, &envp);
191     file_addmetadata(kfp, MODINFOMD_KERNEND, sizeof kernend, &kernend);
192     bios_addsmapdata(kfp);
193
194     /* Figure out the size and location of the metadata */
195     *modulep = addr;
196     size = bi_copymodules64(0);
197     kernend = roundup(addr + size, PAGE_SIZE);
198     *kernendp = kernend;
199
200     /* patch MODINFOMD_KERNEND */
201     md = file_findmetadata(kfp, MODINFOMD_KERNEND);
202     bcopy(&kernend, md->md_data, sizeof kernend);
203
204     /* copy module list and metadata */
205     (void)bi_copymodules64(addr);
206
207     return(0);
208 }