xref: /dragonfly/sbin/gpt/boot.c (revision 335b9e93)
1 /*
2  * Copyright (c) 2008 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #include <sys/types.h>
36 
37 #include <err.h>
38 #include <stddef.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
43 #include <fcntl.h>
44 
45 #include "map.h"
46 #include "gpt.h"
47 
48 static void
49 usage_boot(void)
50 {
51 	fprintf(stderr, "usage: %s device\n", getprogname());
52 	exit(1);
53 }
54 
55 static void
56 bootset(int fd)
57 {
58 	uuid_t uuid;
59 	off_t  block;
60 	off_t  size;
61 	unsigned int entry;
62 	map_t *gpt, *tpg;
63 	map_t *tbl, *lbt;
64 	map_t *map;
65 	u_int32_t status;
66 	struct gpt_hdr *hdr;
67 	struct gpt_ent *ent;
68 	struct mbr *mbr;
69 	int bfd;
70 
71 	/*
72 	 * Paramters for boot partition
73 	 */
74 	uuid_name_lookup(&uuid, "DragonFly Label32", &status);
75 	if (status != uuid_s_ok)
76 		err(1, "unable to find uuid for 'DragonFly Label32'");
77 	entry = 0;
78 	block = 0;
79 	size = (off_t)1024 * 1024 * 1024 / 512;		/* 1GB */
80 
81 	gpt = map_find(MAP_TYPE_PRI_GPT_HDR);
82 	if (gpt == NULL)
83 		errx(1, "%s: error: no primary GPT header", device_name);
84 	tpg = map_find(MAP_TYPE_SEC_GPT_HDR);
85 	if (tpg == NULL)
86 		errx(1, "%s: error: no secondary GPT header", device_name);
87 	tbl = map_find(MAP_TYPE_PRI_GPT_TBL);
88 	lbt = map_find(MAP_TYPE_SEC_GPT_TBL);
89 	if (tbl == NULL || lbt == NULL) {
90 		errx(1, "%s: error: no primary or secondary gpt table",
91 		     device_name);
92 	}
93 
94 	hdr = gpt->map_data;
95 	if (entry > le32toh(hdr->hdr_entries)) {
96 		errx(1, "%s: error: index %u out of range (%u max)",
97 		     device_name, entry, le32toh(hdr->hdr_entries));
98 	}
99 
100 	ent = (void *)((char *)tbl->map_data + entry *
101 		       le32toh(hdr->hdr_entsz));
102 	if (!uuid_is_nil(&ent->ent_type, NULL)) {
103 		errx(1, "%s: error: entry at index %d is not free",
104 		     device_name, entry);
105 	}
106 	map = map_alloc(block, size);
107 	if (map == NULL)
108 		errx(1, "%s: error: no space available on device", device_name);
109 	block = map->map_start;
110 	size  = map->map_size;
111 
112 	le_uuid_enc(&ent->ent_type, &uuid);
113 	ent->ent_lba_start = htole64(map->map_start);
114 	ent->ent_lba_end = htole64(map->map_start + map->map_size - 1LL);
115 
116 	hdr->hdr_crc_table = htole32(crc32(tbl->map_data,
117 				     le32toh(hdr->hdr_entries) *
118 				     le32toh(hdr->hdr_entsz)));
119 	hdr->hdr_crc_self = 0;
120 	hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
121 
122 	gpt_write(fd, gpt);
123 	gpt_write(fd, tbl);
124 
125 	hdr = tpg->map_data;
126 	ent = (void*)((char*)lbt->map_data + entry * le32toh(hdr->hdr_entsz));
127 	le_uuid_enc(&ent->ent_type, &uuid);
128 	ent->ent_lba_start = htole64(map->map_start);
129 	ent->ent_lba_end = htole64(map->map_start + map->map_size - 1LL);
130 
131 	hdr->hdr_crc_table = htole32(crc32(lbt->map_data,
132 				     le32toh(hdr->hdr_entries) *
133 				     le32toh(hdr->hdr_entsz)));
134 	hdr->hdr_crc_self = 0;
135 	hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
136 
137 	gpt_write(fd, lbt);
138 	gpt_write(fd, tpg);
139 
140 	/*
141 	 * Create a dummy partition
142 	 */
143 	map = map_find(MAP_TYPE_PMBR);
144 	if (map == NULL)
145 		errx(1, "I can't find the PMBR!");
146 	mbr = map->map_data;
147 	if (mbr == NULL)
148 		errx(1, "I can't find the PMBR's data!");
149 
150 	/*
151 	 * Copy in real boot code
152 	 */
153 	bfd = open("/boot/boot0", O_RDONLY);
154 	if (bfd < 0 ||
155 	    read(bfd, mbr->mbr_code, sizeof(mbr->mbr_code)) !=
156 	    sizeof(mbr->mbr_code)) {
157 		errx(1, "Cannot read /boot/boot0");
158 	}
159 	close(bfd);
160 
161 	/*
162 	 * Generate partition #1
163 	 */
164 	mbr->mbr_part[1].part_shd = 0xff;
165 	mbr->mbr_part[1].part_ssect = 0xff;
166 	mbr->mbr_part[1].part_scyl = 0xff;
167 	mbr->mbr_part[1].part_ehd = 0xff;
168 	mbr->mbr_part[1].part_esect = 0xff;
169 	mbr->mbr_part[1].part_ecyl = 0xff;
170 	mbr->mbr_part[1].part_start_lo = htole16(block);
171 	mbr->mbr_part[1].part_start_hi = htole16((block) >> 16);
172 	mbr->mbr_part[1].part_size_lo = htole16(size);
173 	mbr->mbr_part[1].part_size_hi = htole16(size >> 16);
174 
175 	mbr->mbr_part[1].part_typ = 108;
176 	mbr->mbr_part[1].part_flag = 0x80;
177 
178 	gpt_write(fd, map);
179 }
180 
181 int
182 cmd_boot(int argc, char *argv[])
183 {
184 	int ch, fd;
185 
186 	while ((ch = getopt(argc, argv, "")) != -1) {
187 		switch(ch) {
188 		default:
189 			usage_boot();
190 		}
191 	}
192 
193 	if (argc == optind)
194 		usage_boot();
195 
196 	while (optind < argc) {
197 		fd = gpt_open(argv[optind++]);
198 		if (fd == -1) {
199 			warn("unable to open device '%s'", device_name);
200 			continue;
201 		}
202 		bootset(fd);
203 		gpt_close(fd);
204 	}
205 	return (0);
206 }
207 
208