xref: /openbsd-src/sys/arch/i386/stand/libsa/exec_i386.c (revision f2da64fbbbf1b03f09f390ab01267c93dfd77c4c)
1 /*	$OpenBSD: exec_i386.c,v 1.44 2016/09/11 17:52:47 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_i386.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 
77 #ifdef EFIBOOT
78 	if ((av = alloc(ac)) == NULL)
79 		panic("alloc for bootarg");
80 	efi_makebootargs();
81 #endif
82 	if (sa_cleanup != NULL)
83 		(*sa_cleanup)();
84 
85 	cd.consdev = cn_tab->cn_dev;
86 	cd.conspeed = com_speed;
87 	cd.consaddr = com_addr;
88 	cd.consfreq = 0;
89 	addbootarg(BOOTARG_CONSDEV, sizeof(cd), &cd);
90 
91 	if (bootmac != NULL)
92 		addbootarg(BOOTARG_BOOTMAC, sizeof(bios_bootmac_t), bootmac);
93 
94 	if (db_console != -1) {
95 		ddb.db_console = db_console;
96 		addbootarg(BOOTARG_DDB, sizeof(ddb), &ddb);
97 	}
98 
99 	bcopy(bootdev_dip->disklabel.d_uid, &bootduid.duid, sizeof(bootduid));
100 	addbootarg(BOOTARG_BOOTDUID, sizeof(bootduid), &bootduid);
101 
102 #ifdef SOFTRAID
103 	if (bootdev_dip->sr_vol != NULL) {
104 		bv = bootdev_dip->sr_vol;
105 		bzero(&bootsr, sizeof(bootsr));
106 		bcopy(&bv->sbv_uuid, &bootsr.uuid, sizeof(bootsr.uuid));
107 		if (bv->sbv_maskkey != NULL)
108 			bcopy(bv->sbv_maskkey, &bootsr.maskkey,
109 			    sizeof(bootsr.maskkey));
110 		addbootarg(BOOTARG_BOOTSR, sizeof(bios_bootsr_t), &bootsr);
111 		explicit_bzero(&bootsr, sizeof(bootsr));
112 	}
113 
114 	sr_clear_keys();
115 #endif
116 
117 	/* Pass memory map to the kernel */
118 	mem_pass();
119 
120 #ifdef __amd64__
121 	/*
122 	 * This code may be used both for 64bit and 32bit.  Make sure the
123 	 * bootarg is 32bit always on even on amd64.
124 	 */
125 	makebootargs32(av, &ac);
126 #else
127 	makebootargs(av, &ac);
128 #endif
129 
130 	entry = marks[MARK_ENTRY] & 0x0fffffff;
131 
132 	printf("entry point at 0x%x\n", (int)entry);
133 
134 #if defined(EFIBOOT)
135 	efi_cleanup();
136 #endif
137 #if defined(EFIBOOT) && defined(__amd64__)
138 	(*run_i386)((u_long)run_i386, entry, howto, bootdev, BOOTARG_APIVER,
139 	    marks[MARK_END], extmem, cnvmem, ac, (intptr_t)av);
140 #else
141 	/* stack and the gung is ok at this point, so, no need for asm setup */
142 	(*(startfuncp)entry)(howto, bootdev, BOOTARG_APIVER, marks[MARK_END],
143 	    extmem, cnvmem, ac, (int)av);
144 	/* not reached */
145 #endif
146 }
147