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