xref: /openbsd/sys/arch/i386/stand/libsa/exec_i386.c (revision 4bdff4be)
1 /*	$OpenBSD: exec_i386.c,v 1.53 2023/07/22 10:11:20 jsg Exp $	*/
2 
3 /*
4  * Copyright (c) 1997-1998 Michael Shalayeff
5  * Copyright (c) 1997 Tobias Weingartner
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  */
30 
31 #include <sys/param.h>
32 #include <sys/disklabel.h>
33 #include <dev/cons.h>
34 #include <lib/libsa/loadfile.h>
35 #include <machine/biosvar.h>
36 #include <machine/specialreg.h>
37 #include <machine/psl.h>
38 #include <stand/boot/bootarg.h>
39 
40 #include "cmd.h"
41 #include "disk.h"
42 #include "libsa.h"
43 
44 #ifdef SOFTRAID
45 #include <dev/softraidvar.h>
46 #include <lib/libsa/softraid.h>
47 #include "softraid_i386.h"
48 #endif
49 
50 #ifdef EFIBOOT
51 #include "efiboot.h"
52 #endif
53 
54 typedef void (*startfuncp)(int, int, int, int, int, int, int, int)
55     __attribute__ ((noreturn));
56 
57 void ucode_load(void);
58 extern struct cmd_state cmd;
59 
60 char *bootmac = NULL;
61 
62 void
63 run_loadfile(uint64_t *marks, int howto)
64 {
65 	u_long entry;
66 #ifdef EXEC_DEBUG
67 	extern int debug;
68 #endif
69 	dev_t bootdev = bootdev_dip->bootdev;
70 	size_t ac = BOOTARG_LEN;
71 	caddr_t av = (caddr_t)BOOTARG_OFF;
72 	bios_consdev_t cd;
73 	extern int com_speed; /* from bioscons.c */
74 	extern int com_addr;
75 	bios_ddb_t ddb;
76 	extern int db_console;
77 	bios_bootduid_t bootduid;
78 #ifdef SOFTRAID
79 	bios_bootsr_t bootsr;
80 	struct sr_boot_volume *bv;
81 #endif
82 
83 #ifdef EFIBOOT
84 	if ((av = alloc(ac)) == NULL)
85 		panic("alloc for bootarg");
86 	efi_makebootargs();
87 #endif
88 	if (sa_cleanup != NULL)
89 		(*sa_cleanup)();
90 
91 	cd.consdev = cn_tab->cn_dev;
92 	cd.conspeed = com_speed;
93 	cd.consaddr = com_addr;
94 	cd.consfreq = 0;
95 	addbootarg(BOOTARG_CONSDEV, sizeof(cd), &cd);
96 
97 	if (bootmac != NULL)
98 		addbootarg(BOOTARG_BOOTMAC, sizeof(bios_bootmac_t), bootmac);
99 
100 	if (db_console != -1) {
101 		ddb.db_console = db_console;
102 		addbootarg(BOOTARG_DDB, sizeof(ddb), &ddb);
103 	}
104 
105 	bcopy(bootdev_dip->disklabel.d_uid, &bootduid.duid, sizeof(bootduid));
106 	addbootarg(BOOTARG_BOOTDUID, sizeof(bootduid), &bootduid);
107 
108 	ucode_load();
109 
110 #ifdef SOFTRAID
111 	if (bootdev_dip->sr_vol != NULL) {
112 		bv = bootdev_dip->sr_vol;
113 		bzero(&bootsr, sizeof(bootsr));
114 		bcopy(&bv->sbv_uuid, &bootsr.uuid, sizeof(bootsr.uuid));
115 		if (bv->sbv_maskkey != NULL)
116 			bcopy(bv->sbv_maskkey, &bootsr.maskkey,
117 			    sizeof(bootsr.maskkey));
118 		addbootarg(BOOTARG_BOOTSR, sizeof(bios_bootsr_t), &bootsr);
119 		explicit_bzero(&bootsr, sizeof(bootsr));
120 	}
121 
122 	sr_clear_keys();
123 #endif
124 
125 	/* Pass memory map to the kernel */
126 	mem_pass();
127 
128 #ifdef __amd64__
129 	/*
130 	 * This code may be used both for 64bit and 32bit.  Make sure the
131 	 * bootarg is 32bit always on even on amd64.
132 	 */
133 	makebootargs32(av, &ac);
134 #else
135 	makebootargs(av, &ac);
136 #endif
137 
138 	entry = marks[MARK_ENTRY] & 0x0fffffff;
139 
140 	printf("entry point at 0x%x\n", (int)entry);
141 
142 #if defined(EFIBOOT)
143 	efi_cleanup();
144 #endif
145 #if defined(EFIBOOT) && defined(__amd64__)
146 	(*run_i386)((u_long)run_i386, entry, howto, bootdev, BOOTARG_APIVER,
147 	    marks[MARK_END], extmem, cnvmem, ac, (intptr_t)av);
148 #else
149 	/* stack and the gung is ok at this point, so, no need for asm setup */
150 	(*(startfuncp)entry)(howto, bootdev, BOOTARG_APIVER, marks[MARK_END],
151 	    extmem, cnvmem, ac, (int)av);
152 	/* not reached */
153 #endif
154 }
155 
156 void
157 ucode_load(void)
158 {
159 	uint32_t model, family, stepping;
160 	uint32_t dummy, signature;
161 	uint32_t vendor[4];
162 	bios_ucode_t uc;
163 	struct stat sb;
164 	char path[128];
165 	size_t buflen;
166 	char *buf;
167 	int fd, psl_check;
168 
169 	/*
170 	 * The following is a simple check to see if cpuid is supported.
171 	 * We try to toggle bit 21 (PSL_ID) in eflags.  If it works, then
172 	 * cpuid is supported.  If not, there's no cpuid, and we don't
173 	 * try it (don't want /boot to get an invalid opcode exception).
174 	 */
175 	__asm volatile(
176 	    "pushfl\n\t"
177 	    "popl	%2\n\t"
178 	    "xorl	%2, %0\n\t"
179 	    "pushl	%0\n\t"
180 	    "popfl\n\t"
181 	    "pushfl\n\t"
182 	    "popl	%0\n\t"
183 	    "xorl	%2, %0\n\t"		/* If %2 == %0, no cpuid */
184 	    : "=r" (psl_check)
185 	    : "0" (PSL_ID), "r" (0)
186 	    : "cc");
187 
188 	if (psl_check != PSL_ID)
189 		return;
190 
191 	CPUID(0, dummy, vendor[0], vendor[2], vendor[1]);
192 	vendor[3] = 0; /* NULL-terminate */
193 	if (strcmp((char *)vendor, "GenuineIntel") != 0 &&
194 	    strcmp((char *)vendor, "AuthenticAMD") != 0)
195 		return;
196 
197 	CPUID(1, signature, dummy, dummy, dummy);
198 	family = (signature >> 8) & 0x0f;
199 	model = (signature >> 4) & 0x0f;
200 	if (family == 0x6 || family == 0xf) {
201 		family += (signature >> 20) & 0xff;
202 		model += ((signature >> 16) & 0x0f) << 4;
203 	}
204 	stepping = (signature >> 0) & 0x0f;
205 
206 	if (strcmp((char *)vendor, "GenuineIntel") == 0) {
207 		snprintf(path, sizeof(path),
208 		    "%s:/etc/firmware/intel/%02x-%02x-%02x",
209 		    cmd.bootdev, family, model, stepping);
210 	} else if (strcmp((char *)vendor, "AuthenticAMD") == 0) {
211 		if (family < 0x10)
212 			return;
213 		else if (family <= 0x14)
214 			snprintf(path, sizeof(path),
215 			    "%s:/etc/firmware/amd/microcode_amd.bin",
216 			    cmd.bootdev);
217 		else
218 			snprintf(path, sizeof(path),
219 			    "%s:/etc/firmware/amd/microcode_amd_fam%02xh.bin",
220 			    cmd.bootdev, family);
221 	}
222 
223 	fd = open(path, O_RDONLY);
224 	if (fd == -1)
225 		return;
226 
227 	if (fstat(fd, &sb) == -1)
228 		return;
229 
230 	buflen = sb.st_size;
231 	if (buflen > 256*1024) {
232 		printf("ucode too large\n");
233 		return;
234 	}
235 
236 	buf = (char *)(1*1024*1024);
237 
238 	if (read(fd, buf, buflen) != buflen) {
239 		close(fd);
240 		return;
241 	}
242 
243 	uc.uc_addr = (uint64_t)buf;
244 	uc.uc_size = (uint64_t)buflen;
245 	addbootarg(BOOTARG_UCODE, sizeof(uc), &uc);
246 
247 	close(fd);
248 }
249