xref: /freebsd/stand/i386/loader/chain.c (revision b9f654b163bce26de79705e77b872427c9f2afa1)
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 <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36 
37 #include <stand.h>
38 #include <sys/param.h>
39 #include <sys/linker.h>
40 #include <sys/diskmbr.h>
41 
42 #include "bootstrap.h"
43 #include "libi386/libi386.h"
44 #include "btxv86.h"
45 
46 /*
47  * The MBR/VBR is located in first sector of disk/partition.
48  * Read 512B to temporary location and set up relocation. Then
49  * exec relocator.
50  */
51 #define	SECTOR_SIZE	(512)
52 
53 COMMAND_SET(chain, "chain", "chain load boot block from device", command_chain);
54 
55 static int
56 command_chain(int argc, char *argv[])
57 {
58 	int fd, len, size = SECTOR_SIZE;
59 	struct stat st;
60 	vm_offset_t mem = 0x100000;
61 	struct i386_devdesc *rootdev;
62 
63 	if (argc == 1) {
64 		command_errmsg = "no device or file name specified";
65 		return (CMD_ERROR);
66 	}
67 	if (argc != 2) {
68 		command_errmsg = "invalid trailing arguments";
69 		return (CMD_ERROR);
70 	}
71 
72 	fd = open(argv[1], O_RDONLY);
73 	if (fd == -1) {
74 		command_errmsg = "open failed";
75 		return (CMD_ERROR);
76 	}
77 
78 	len = strlen(argv[1]);
79 	if (argv[1][len-1] != ':') {
80 		if (fstat(fd, &st) == -1) {
81 			command_errmsg = "stat failed";
82 			close(fd);
83 			return (CMD_ERROR);
84 		}
85 		size = st.st_size;
86 	} else if (strncmp(argv[1], "disk", 4) != 0) {
87 		command_errmsg = "can only use disk device";
88 		close(fd);
89 		return (CMD_ERROR);
90 	}
91 
92 	i386_getdev((void **)(&rootdev), argv[1], NULL);
93 	if (rootdev == NULL) {
94 		command_errmsg = "can't determine root device";
95 		close(fd);
96 		return (CMD_ERROR);
97 	}
98 
99 	if (archsw.arch_readin(fd, mem, size) != size) {
100 		command_errmsg = "failed to read disk";
101 		close(fd);
102 		return (CMD_ERROR);
103 	}
104 	close(fd);
105 
106 	if (argv[1][len-1] == ':' &&
107 	    *((uint16_t *)PTOV(mem + DOSMAGICOFFSET)) != DOSMAGIC) {
108 		command_errmsg = "wrong magic";
109 		return (CMD_ERROR);
110 	}
111 
112 	relocater_data[0].src = mem;
113 	relocater_data[0].dest = 0x7C00;
114 	relocater_data[0].size = size;
115 
116 	relocator_edx = bd_unit2bios(rootdev);
117 	relocator_esi = relocater_size;
118 	relocator_ds = 0;
119 	relocator_es = 0;
120 	relocator_fs = 0;
121 	relocator_gs = 0;
122 	relocator_ss = 0;
123 	relocator_cs = 0;
124 	relocator_sp = 0x7C00;
125 	relocator_ip = 0x7C00;
126 	relocator_a20_enabled = 0;
127 
128 	i386_copyin(relocater, 0x600, relocater_size);
129 
130 	dev_cleanup();
131 
132 	__exec((void *)0x600);
133 
134 	panic("exec returned");
135 	return (CMD_ERROR);		/* not reached */
136 }
137