xref: /illumos-gate/usr/src/boot/i386/loader/chain.c (revision 9ffcdb10)
1199767f8SToomas Soome /*
2199767f8SToomas Soome  * This file and its contents are supplied under the terms of the
3199767f8SToomas Soome  * Common Development and Distribution License ("CDDL"), version 1.0.
4199767f8SToomas Soome  * You may only use this file in accordance with the terms of version
5199767f8SToomas Soome  * 1.0 of the CDDL.
6199767f8SToomas Soome  *
7199767f8SToomas Soome  * A full copy of the text of the CDDL should have accompanied this
8199767f8SToomas Soome  * source.  A copy of the CDDL is also available via the Internet at
9199767f8SToomas Soome  * http://www.illumos.org/license/CDDL.
10199767f8SToomas Soome  */
11199767f8SToomas Soome 
12199767f8SToomas Soome /*
13199767f8SToomas Soome  * Copyright 2015 Toomas Soome <tsoome@me.com>
14199767f8SToomas Soome  */
15199767f8SToomas Soome 
16199767f8SToomas Soome /*
17199767f8SToomas Soome  * Chain loader to load BIOS boot block either from MBR or PBR.
18199767f8SToomas Soome  *
19199767f8SToomas Soome  * Note the boot block location 0000:7c000 conflicts with loader, so we need to
20199767f8SToomas Soome  * read in to temporary space and relocate on exec, when btx is stopped.
21199767f8SToomas Soome  */
22199767f8SToomas Soome 
23199767f8SToomas Soome #include <sys/cdefs.h>
24199767f8SToomas Soome #include <stand.h>
25199767f8SToomas Soome #include <sys/param.h>
26199767f8SToomas Soome #include <sys/linker.h>
27199767f8SToomas Soome #include <sys/diskmbr.h>
28199767f8SToomas Soome 
29199767f8SToomas Soome #include "bootstrap.h"
309890ff83SToomas Soome #include "libi386/vbe.h"
31199767f8SToomas Soome #include "libi386/libi386.h"
32199767f8SToomas Soome #include "btxv86.h"
33199767f8SToomas Soome 
34199767f8SToomas Soome /*
35199767f8SToomas Soome  * The MBR/VBR is located in first sector of disk/partition.
36199767f8SToomas Soome  * Read 512B to temporary location and set up relocation. Then
37199767f8SToomas Soome  * exec relocator.
38199767f8SToomas Soome  */
39199767f8SToomas Soome #define	SECTOR_SIZE	(512)
40199767f8SToomas Soome 
41199767f8SToomas Soome COMMAND_SET(chain, "chain", "chain load boot block from device", command_chain);
42199767f8SToomas Soome 
43199767f8SToomas Soome static int
command_chain(int argc,char * argv[])44199767f8SToomas Soome command_chain(int argc, char *argv[])
45199767f8SToomas Soome {
46199767f8SToomas Soome 	int fd, len, size = SECTOR_SIZE;
47199767f8SToomas Soome 	struct stat st;
48199767f8SToomas Soome 	vm_offset_t mem = 0x100000;
49199767f8SToomas Soome 	struct i386_devdesc *rootdev;
50199767f8SToomas Soome 
51199767f8SToomas Soome 	if (argc == 1) {
52199767f8SToomas Soome 		command_errmsg = "no device or file name specified";
53199767f8SToomas Soome 		return (CMD_ERROR);
54199767f8SToomas Soome 	}
55199767f8SToomas Soome 	if (argc != 2) {
56199767f8SToomas Soome 		command_errmsg = "invalid trailing arguments";
57199767f8SToomas Soome 		return (CMD_ERROR);
58199767f8SToomas Soome 	}
59199767f8SToomas Soome 
60199767f8SToomas Soome 	fd = open(argv[1], O_RDONLY);
61199767f8SToomas Soome 	if (fd == -1) {
62199767f8SToomas Soome 		command_errmsg = "open failed";
63199767f8SToomas Soome 		return (CMD_ERROR);
64199767f8SToomas Soome 	}
65199767f8SToomas Soome 
66199767f8SToomas Soome 	len = strlen(argv[1]);
67199767f8SToomas Soome 	if (argv[1][len-1] != ':') {
68199767f8SToomas Soome 		if (fstat(fd, &st) == -1) {
69199767f8SToomas Soome 			command_errmsg = "stat failed";
70199767f8SToomas Soome 			close(fd);
71199767f8SToomas Soome 			return (CMD_ERROR);
72199767f8SToomas Soome 		}
73*9ffcdb10SToomas Soome 		if (size != st.st_size) {
74*9ffcdb10SToomas Soome 			command_errmsg = "invalid size";
75*9ffcdb10SToomas Soome 			return (CMD_ERROR);
76*9ffcdb10SToomas Soome 		}
77199767f8SToomas Soome 	} else if (strncmp(argv[1], "disk", 4) != 0) {
78199767f8SToomas Soome 		command_errmsg = "can only use disk device";
79199767f8SToomas Soome 		close(fd);
80199767f8SToomas Soome 		return (CMD_ERROR);
81199767f8SToomas Soome 	}
82199767f8SToomas Soome 
83199767f8SToomas Soome 	i386_getdev((void **)(&rootdev), argv[1], NULL);
84199767f8SToomas Soome 	if (rootdev == NULL) {
85199767f8SToomas Soome 		command_errmsg = "can't determine root device";
86199767f8SToomas Soome 		close(fd);
87199767f8SToomas Soome 		return (CMD_ERROR);
88199767f8SToomas Soome 	}
89199767f8SToomas Soome 
90199767f8SToomas Soome 	if (archsw.arch_readin(fd, mem, size) != size) {
91199767f8SToomas Soome 		command_errmsg = "failed to read disk";
92199767f8SToomas Soome 		close(fd);
93199767f8SToomas Soome 		return (CMD_ERROR);
94199767f8SToomas Soome 	}
95199767f8SToomas Soome 	close(fd);
96199767f8SToomas Soome 
97*9ffcdb10SToomas Soome 	/* File or device, we assume MBR or VBR boot code */
98*9ffcdb10SToomas Soome 	if (*((uint16_t *)PTOV(mem + DOSMAGICOFFSET)) != DOSMAGIC) {
99199767f8SToomas Soome 		command_errmsg = "wrong magic";
100199767f8SToomas Soome 		return (CMD_ERROR);
101199767f8SToomas Soome 	}
102199767f8SToomas Soome 
1039890ff83SToomas Soome 	bios_set_text_mode(3);
1047b2aa502SToomas Soome 	relocater_data[0].src = mem;
1057b2aa502SToomas Soome 	relocater_data[0].dest = 0x7C00;
1067b2aa502SToomas Soome 	relocater_data[0].size = size;
107199767f8SToomas Soome 
108863275a4SToomas Soome 	relocator_edx = bd_unit2bios(rootdev);
109199767f8SToomas Soome 	relocator_esi = relocater_size;
110199767f8SToomas Soome 	relocator_ds = 0;
111199767f8SToomas Soome 	relocator_es = 0;
112199767f8SToomas Soome 	relocator_fs = 0;
113199767f8SToomas Soome 	relocator_gs = 0;
114199767f8SToomas Soome 	relocator_ss = 0;
115199767f8SToomas Soome 	relocator_cs = 0;
116199767f8SToomas Soome 	relocator_sp = 0x7C00;
117199767f8SToomas Soome 	relocator_ip = 0x7C00;
118199767f8SToomas Soome 	relocator_a20_enabled = 0;
119199767f8SToomas Soome 
120199767f8SToomas Soome 	i386_copyin(relocater, 0x600, relocater_size);
121199767f8SToomas Soome 
122199767f8SToomas Soome 	dev_cleanup();
123199767f8SToomas Soome 
124199767f8SToomas Soome 	__exec((void *)0x600);
125199767f8SToomas Soome 
126199767f8SToomas Soome 	panic("exec returned");
127199767f8SToomas Soome 	return (CMD_ERROR);		/* not reached */
128199767f8SToomas Soome }
129