xref: /freebsd-src/stand/userboot/userboot/bootinfo64.c (revision 86077f4fd11070518a6d04eee7fdb93cbbfb1b52)
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 <stand.h>
28 #include <sys/param.h>
29 #include <sys/reboot.h>
30 #include <sys/linker.h>
31 #include <i386/include/bootinfo.h>
32 #include <machine/cpufunc.h>
33 #include <machine/psl.h>
34 #include <machine/specialreg.h>
35 
36 #include "bootstrap.h"
37 #include "modinfo.h"
38 #include "libuserboot.h"
39 
40 /*
41  * Check to see if this CPU supports long mode.
42  */
43 static int
44 bi_checkcpu(void)
45 {
46 #if 0
47     char *cpu_vendor;
48     int vendor[3];
49     int eflags, regs[4];
50 
51     /* Check for presence of "cpuid". */
52     eflags = read_eflags();
53     write_eflags(eflags ^ PSL_ID);
54     if (!((eflags ^ read_eflags()) & PSL_ID))
55 	return (0);
56 
57     /* Fetch the vendor string. */
58     do_cpuid(0, regs);
59     vendor[0] = regs[1];
60     vendor[1] = regs[3];
61     vendor[2] = regs[2];
62     cpu_vendor = (char *)vendor;
63 
64     /* Check for vendors that support AMD features. */
65     if (strncmp(cpu_vendor, INTEL_VENDOR_ID, 12) != 0 &&
66 	strncmp(cpu_vendor, AMD_VENDOR_ID, 12) != 0 &&
67 	strncmp(cpu_vendor, CENTAUR_VENDOR_ID, 12) != 0)
68 	return (0);
69 
70     /* Has to support AMD features. */
71     do_cpuid(0x80000000, regs);
72     if (!(regs[0] >= 0x80000001))
73 	return (0);
74 
75     /* Check for long mode. */
76     do_cpuid(0x80000001, regs);
77     return (regs[3] & AMDID_LM);
78 #else
79 	return (1);
80 #endif
81 }
82 
83 /*
84  * Load the information expected by an amd64 kernel.
85  *
86  * - The 'boothowto' argument is constructed
87  * - The 'bootdev' argument is constructed
88  * - The 'bootinfo' struct is constructed, and copied into the kernel space.
89  * - The kernel environment is copied into kernel space.
90  * - Module metadata are formatted and placed in kernel space.
91  */
92 int
93 bi_load64(char *args, vm_offset_t *modulep, vm_offset_t *kernendp)
94 {
95     struct preloaded_file	*xp, *kfp;
96     struct devdesc		*rootdev;
97     struct file_metadata	*md;
98     vm_offset_t			addr;
99     uint64_t			kernend;
100     uint64_t			envp;
101     vm_offset_t			size;
102     char			*rootdevname;
103     int				howto;
104 
105     if (!bi_checkcpu()) {
106 	printf("CPU doesn't support long mode\n");
107 	return (EINVAL);
108     }
109 
110     howto = bi_getboothowto(args);
111 
112     /*
113      * Allow the environment variable 'rootdev' to override the supplied device
114      * This should perhaps go to MI code and/or have $rootdev tested/set by
115      * MI code before launching the kernel.
116      */
117     rootdevname = getenv("rootdev");
118     userboot_getdev((void **)(&rootdev), rootdevname, NULL);
119     if (rootdev == NULL) {		/* bad $rootdev/$currdev */
120 	printf("can't determine root device\n");
121 	return(EINVAL);
122     }
123 
124     /* Try reading the /etc/fstab file to select the root device */
125     getrootmount(devformat(rootdev));
126 
127     /* find the last module in the chain */
128     addr = 0;
129     for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) {
130 	if (addr < (xp->f_addr + xp->f_size))
131 	    addr = xp->f_addr + xp->f_size;
132     }
133     /* pad to a page boundary */
134     addr = roundup(addr, PAGE_SIZE);
135 
136     /* copy our environment */
137     envp = addr;
138     addr = md_copyenv(addr);
139 
140     /* pad to a page boundary */
141     addr = roundup(addr, PAGE_SIZE);
142 
143     kfp = file_findfile(NULL, md_kerntype);
144     if (kfp == NULL)
145 	panic("can't find kernel file");
146     kernend = 0;	/* fill it in later */
147     file_addmetadata(kfp, MODINFOMD_HOWTO, sizeof howto, &howto);
148     file_addmetadata(kfp, MODINFOMD_ENVP, sizeof envp, &envp);
149     file_addmetadata(kfp, MODINFOMD_KERNEND, sizeof kernend, &kernend);
150     bios_addsmapdata(kfp);
151 
152     /* Figure out the size and location of the metadata */
153     *modulep = addr;
154     size = md_copymodules(0, true);
155     kernend = roundup(addr + size, PAGE_SIZE);
156     *kernendp = kernend;
157 
158     /* patch MODINFOMD_KERNEND */
159     md = file_findmetadata(kfp, MODINFOMD_KERNEND);
160     bcopy(&kernend, md->md_data, sizeof kernend);
161 
162     /* copy module list and metadata */
163     (void)md_copymodules(addr, true);
164 
165     return(0);
166 }
167