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