xref: /openbsd-src/sys/arch/amd64/stand/libsa/exec_i386.c (revision f2da64fbbbf1b03f09f390ab01267c93dfd77c4c)
1 /*	$OpenBSD: exec_i386.c,v 1.18 2016/09/11 17:51:21 jsing 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 <stand/boot/bootarg.h>
37 
38 #include "disk.h"
39 #include "libsa.h"
40 
41 #ifdef SOFTRAID
42 #include <dev/softraidvar.h>
43 #include <lib/libsa/softraid.h>
44 #include "softraid_amd64.h"
45 #endif
46 
47 #ifdef EFIBOOT
48 #include "efiboot.h"
49 #endif
50 
51 typedef void (*startfuncp)(int, int, int, int, int, int, int, int)
52     __attribute__ ((noreturn));
53 
54 char *bootmac = NULL;
55 
56 void
57 run_loadfile(u_long *marks, int howto)
58 {
59 	u_long entry;
60 #ifdef EXEC_DEBUG
61 	extern int debug;
62 #endif
63 	dev_t bootdev = bootdev_dip->bootdev;
64 	size_t ac = BOOTARG_LEN;
65 	caddr_t av = (caddr_t)BOOTARG_OFF;
66 	bios_consdev_t cd;
67 	extern int com_speed; /* from bioscons.c */
68 	extern int com_addr;
69 	bios_ddb_t ddb;
70 	extern int db_console;
71 	bios_bootduid_t bootduid;
72 #ifdef SOFTRAID
73 	bios_bootsr_t bootsr;
74 	struct sr_boot_volume *bv;
75 #endif
76 #if defined(EFIBOOT)
77 	int i;
78 	u_long delta;
79 	extern u_long efi_loadaddr;
80 
81 	if ((av = alloc(ac)) == NULL)
82 		panic("alloc for bootarg");
83 	efi_makebootargs();
84 #endif
85 	if (sa_cleanup != NULL)
86 		(*sa_cleanup)();
87 
88 	cd.consdev = cn_tab->cn_dev;
89 	cd.conspeed = com_speed;
90 	cd.consaddr = com_addr;
91 	cd.consfreq = 0;
92 	addbootarg(BOOTARG_CONSDEV, sizeof(cd), &cd);
93 
94 	if (bootmac != NULL)
95 		addbootarg(BOOTARG_BOOTMAC, sizeof(bios_bootmac_t), bootmac);
96 
97 	if (db_console != -1) {
98 		ddb.db_console = db_console;
99 		addbootarg(BOOTARG_DDB, sizeof(ddb), &ddb);
100 	}
101 
102 	bcopy(bootdev_dip->disklabel.d_uid, &bootduid.duid, sizeof(bootduid));
103 	addbootarg(BOOTARG_BOOTDUID, sizeof(bootduid), &bootduid);
104 
105 #ifdef SOFTRAID
106 	if (bootdev_dip->sr_vol != NULL) {
107 		bv = bootdev_dip->sr_vol;
108 		bzero(&bootsr, sizeof(bootsr));
109 		bcopy(&bv->sbv_uuid, &bootsr.uuid, sizeof(bootsr.uuid));
110 		if (bv->sbv_maskkey != NULL)
111 			bcopy(bv->sbv_maskkey, &bootsr.maskkey,
112 			    sizeof(bootsr.maskkey));
113 		addbootarg(BOOTARG_BOOTSR, sizeof(bios_bootsr_t), &bootsr);
114 		explicit_bzero(&bootsr, sizeof(bootsr));
115 	}
116 
117 	sr_clear_keys();
118 #endif
119 
120 	/* Pass memory map to the kernel */
121 	mem_pass();
122 
123 	/*
124 	 * This code may be used both for 64bit and 32bit.  Make sure the
125 	 * bootarg is 32bit always on even on amd64.
126 	 */
127 #ifdef __amd64__
128 	makebootargs32(av, &ac);
129 #else
130 	makebootargs(av, &ac);
131 #endif
132 
133 	entry = marks[MARK_ENTRY] & 0x0fffffff;
134 
135 	printf("entry point at 0x%lx [%x, %x, %x, %x]\n", entry,
136 	    ((int *)entry)[0], ((int *)entry)[1],
137 	    ((int *)entry)[2], ((int *)entry)[3]);
138 #ifndef EFIBOOT
139 	/* stack and the gung is ok at this point, so, no need for asm setup */
140 	(*(startfuncp)entry)(howto, bootdev, BOOTARG_APIVER, marks[MARK_END],
141 	    extmem, cnvmem, ac, (int)av);
142 #else
143 	/*
144 	 * Move the loaded kernel image to the usual place after calling
145 	 * ExitBootServices().
146 	 */
147 	delta = DEFAULT_KERNEL_ADDRESS - efi_loadaddr;
148 	efi_cleanup();
149 	memcpy((void *)marks[MARK_START] + delta, (void *)marks[MARK_START],
150 	    marks[MARK_END] - marks[MARK_START]);
151 	for (i = 0; i < MARK_MAX; i++)
152 		marks[i] += delta;
153 	entry += delta;
154 #ifdef __amd64__
155 	(*run_i386)((u_long)run_i386, entry, howto, bootdev, BOOTARG_APIVER,
156 	    marks[MARK_END], extmem, cnvmem, ac, (intptr_t)av);
157 #else
158 	(*(startfuncp)entry)(howto, bootdev, BOOTARG_APIVER, marks[MARK_END],
159 	    extmem, cnvmem, ac, (int)av);
160 #endif
161 #endif
162 	/* not reached */
163 }
164