xref: /illumos-gate/usr/src/boot/i386/loader/chain.c (revision 9ffcdb10)
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
command_chain(int argc,char * argv[])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 		if (size != st.st_size) {
74 			command_errmsg = "invalid size";
75 			return (CMD_ERROR);
76 		}
77 	} else if (strncmp(argv[1], "disk", 4) != 0) {
78 		command_errmsg = "can only use disk device";
79 		close(fd);
80 		return (CMD_ERROR);
81 	}
82 
83 	i386_getdev((void **)(&rootdev), argv[1], NULL);
84 	if (rootdev == NULL) {
85 		command_errmsg = "can't determine root device";
86 		close(fd);
87 		return (CMD_ERROR);
88 	}
89 
90 	if (archsw.arch_readin(fd, mem, size) != size) {
91 		command_errmsg = "failed to read disk";
92 		close(fd);
93 		return (CMD_ERROR);
94 	}
95 	close(fd);
96 
97 	/* File or device, we assume MBR or VBR boot code */
98 	if (*((uint16_t *)PTOV(mem + DOSMAGICOFFSET)) != DOSMAGIC) {
99 		command_errmsg = "wrong magic";
100 		return (CMD_ERROR);
101 	}
102 
103 	bios_set_text_mode(3);
104 	relocater_data[0].src = mem;
105 	relocater_data[0].dest = 0x7C00;
106 	relocater_data[0].size = size;
107 
108 	relocator_edx = bd_unit2bios(rootdev);
109 	relocator_esi = relocater_size;
110 	relocator_ds = 0;
111 	relocator_es = 0;
112 	relocator_fs = 0;
113 	relocator_gs = 0;
114 	relocator_ss = 0;
115 	relocator_cs = 0;
116 	relocator_sp = 0x7C00;
117 	relocator_ip = 0x7C00;
118 	relocator_a20_enabled = 0;
119 
120 	i386_copyin(relocater, 0x600, relocater_size);
121 
122 	dev_cleanup();
123 
124 	__exec((void *)0x600);
125 
126 	panic("exec returned");
127 	return (CMD_ERROR);		/* not reached */
128 }
129