1 /*- 2 * Copyright 2015 Toomas Soome <tsoome@me.com> 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 /* 28 * Chain loader to load BIOS boot block either from MBR or PBR. 29 * 30 * Note the boot block location 0000:7c000 conflicts with loader, so we need to 31 * read in to temporary space and relocate on exec, when btx is stopped. 32 */ 33 34 #include <stand.h> 35 #include <sys/param.h> 36 #include <sys/linker.h> 37 #include <sys/diskmbr.h> 38 39 #include "bootstrap.h" 40 #include "libi386/libi386.h" 41 #include "btxv86.h" 42 43 #ifdef LOADER_VERIEXEC_VECTX 44 #define VECTX_HANDLE(x) vctx 45 #else 46 #define VECTX_HANDLE(x) x 47 #endif 48 49 /* 50 * The MBR/VBR is located in first sector of disk/partition. 51 * Read 512B to temporary location and set up relocation. Then 52 * exec relocator. 53 */ 54 #define SECTOR_SIZE (512) 55 56 COMMAND_SET(chain, "chain", "chain load boot block from device", command_chain); 57 58 static int 59 command_chain(int argc, char *argv[]) 60 { 61 int fd, len, size = SECTOR_SIZE; 62 struct stat st; 63 vm_offset_t mem = 0x100000; 64 struct i386_devdesc *rootdev; 65 #ifdef LOADER_VERIEXEC_VECTX 66 struct vectx *vctx; 67 int verror; 68 #endif 69 70 if (argc == 1) { 71 command_errmsg = "no device or file name specified"; 72 return (CMD_ERROR); 73 } 74 if (argc != 2) { 75 command_errmsg = "invalid trailing arguments"; 76 return (CMD_ERROR); 77 } 78 79 fd = open(argv[1], O_RDONLY); 80 if (fd == -1) { 81 command_errmsg = "open failed"; 82 return (CMD_ERROR); 83 } 84 85 #ifdef LOADER_VERIEXEC_VECTX 86 vctx = vectx_open(fd, argv[1], 0L, NULL, &verror, __func__); 87 if (verror) { 88 sprintf(command_errbuf, "can't verify: %s", argv[1]); 89 close(fd); 90 free(vctx); 91 return (CMD_ERROR); 92 } 93 #else 94 #ifdef LOADER_VERIEXEC 95 if (verify_file(fd, argv[1], 0, VE_MUST, __func__) < 0) { 96 sprintf(command_errbuf, "can't verify: %s", argv[1]); 97 close(fd); 98 return (CMD_ERROR); 99 } 100 #endif 101 #endif 102 len = strlen(argv[1]); 103 if (argv[1][len-1] != ':') { 104 if (fstat(fd, &st) == -1) { 105 command_errmsg = "stat failed"; 106 close(fd); 107 return (CMD_ERROR); 108 } 109 size = st.st_size; 110 } else if (strncmp(argv[1], "disk", 4) != 0) { 111 command_errmsg = "can only use disk device"; 112 close(fd); 113 return (CMD_ERROR); 114 } 115 116 i386_getdev((void **)(&rootdev), argv[1], NULL); 117 if (rootdev == NULL) { 118 command_errmsg = "can't determine root device"; 119 close(fd); 120 return (CMD_ERROR); 121 } 122 123 if (archsw.arch_readin(VECTX_HANDLE(fd), mem, size) != size) { 124 command_errmsg = "failed to read disk"; 125 close(fd); 126 return (CMD_ERROR); 127 } 128 close(fd); 129 #ifdef LOADER_VERIEXEC_VECTX 130 verror = vectx_close(vctx, VE_MUST, __func__); 131 if (verror) { 132 free(vctx); 133 return (CMD_ERROR); 134 } 135 #endif 136 if (argv[1][len-1] == ':' && 137 *((uint16_t *)PTOV(mem + DOSMAGICOFFSET)) != DOSMAGIC) { 138 command_errmsg = "wrong magic"; 139 return (CMD_ERROR); 140 } 141 142 relocater_data[0].src = mem; 143 relocater_data[0].dest = 0x7C00; 144 relocater_data[0].size = size; 145 146 relocator_edx = bd_unit2bios(rootdev); 147 relocator_esi = relocater_size; 148 relocator_ds = 0; 149 relocator_es = 0; 150 relocator_fs = 0; 151 relocator_gs = 0; 152 relocator_ss = 0; 153 relocator_cs = 0; 154 relocator_sp = 0x7C00; 155 relocator_ip = 0x7C00; 156 relocator_a20_enabled = 0; 157 158 i386_copyin(relocater, 0x600, relocater_size); 159 160 dev_cleanup(); 161 162 __exec((void *)0x600); 163 164 panic("exec returned"); 165 return (CMD_ERROR); /* not reached */ 166 } 167