xref: /titanic_51/usr/src/boot/sys/boot/i386/loader/chain.c (revision d4f75c945065a64cbb0535077fbbcb67c8301624)
1 /*
2  * This file and its contents are supplied under the terms of the
3  * Common Development and Distribution License ("CDDL"), version 1.0.
4  * You may only use this file in accordance with the terms of version
5  * 1.0 of the CDDL.
6  *
7  * A full copy of the text of the CDDL should have accompanied this
8  * source.  A copy of the CDDL is also available via the Internet at
9  * http://www.illumos.org/license/CDDL.
10  */
11 
12 /*
13  * Copyright 2015 Toomas Soome <tsoome@me.com>
14  */
15 
16 /*
17  * Chain loader to load BIOS boot block either from MBR or PBR.
18  *
19  * Note the boot block location 0000:7c000 conflicts with loader, so we need to
20  * read in to temporary space and relocate on exec, when btx is stopped.
21  */
22 
23 #include <sys/cdefs.h>
24 #include <stand.h>
25 #include <sys/param.h>
26 #include <sys/linker.h>
27 #include <sys/diskmbr.h>
28 
29 #include "bootstrap.h"
30 #include "libi386/libi386.h"
31 #include "btxv86.h"
32 
33 /*
34  * The MBR/VBR is located in first sector of disk/partition.
35  * Read 512B to temporary location and set up relocation. Then
36  * exec relocator.
37  */
38 #define	SECTOR_SIZE	(512)
39 
40 COMMAND_SET(chain, "chain", "chain load boot block from device", command_chain);
41 
42 static int
43 command_chain(int argc, char *argv[])
44 {
45 	int fd, len, size = SECTOR_SIZE;
46 	struct stat st;
47 	vm_offset_t mem = 0x100000;
48 	struct i386_devdesc *rootdev;
49 
50 	if (argc == 1) {
51 		command_errmsg = "no device or file name specified";
52 		return (CMD_ERROR);
53 	}
54 	if (argc != 2) {
55 		command_errmsg = "invalid trailing arguments";
56 		return (CMD_ERROR);
57 	}
58 
59 	fd = open(argv[1], O_RDONLY);
60 	if (fd == -1) {
61 		command_errmsg = "open failed";
62 		return (CMD_ERROR);
63 	}
64 
65 	len = strlen(argv[1]);
66 	if (argv[1][len-1] != ':') {
67 		if (fstat(fd, &st) == -1) {
68 			command_errmsg = "stat failed";
69 			close(fd);
70 			return (CMD_ERROR);
71 		}
72 		size = st.st_size;
73 	} else if (strncmp(argv[1], "disk", 4) != 0) {
74 		command_errmsg = "can only use disk device";
75 		close(fd);
76 		return (CMD_ERROR);
77 	}
78 
79 	i386_getdev((void **)(&rootdev), argv[1], NULL);
80 	if (rootdev == NULL) {
81 		command_errmsg = "can't determine root device";
82 		close(fd);
83 		return (CMD_ERROR);
84 	}
85 
86 	if (archsw.arch_readin(fd, mem, size) != size) {
87 		command_errmsg = "failed to read disk";
88 		close(fd);
89 		return (CMD_ERROR);
90 	}
91 	close(fd);
92 
93 	if (argv[1][len-1] == ':' &&
94 	    *((uint16_t *)PTOV(mem + DOSMAGICOFFSET)) != DOSMAGIC) {
95 		command_errmsg = "wrong magic";
96 		return (CMD_ERROR);
97 	}
98 
99 	relocater_data[0].src = mem;
100 	relocater_data[0].dest = 0x7C00;
101 	relocater_data[0].size = size;
102 
103 	relocator_edx = bd_unit2bios(rootdev->d_unit);
104 	relocator_esi = relocater_size;
105 	relocator_ds = 0;
106 	relocator_es = 0;
107 	relocator_fs = 0;
108 	relocator_gs = 0;
109 	relocator_ss = 0;
110 	relocator_cs = 0;
111 	relocator_sp = 0x7C00;
112 	relocator_ip = 0x7C00;
113 	relocator_a20_enabled = 0;
114 
115 	i386_copyin(relocater, 0x600, relocater_size);
116 
117 	dev_cleanup();
118 
119 	__exec((void *)0x600);
120 
121 	panic("exec returned");
122 	return (CMD_ERROR);		/* not reached */
123 }
124