xref: /freebsd/stand/libsa/gpt.c (revision fe815331bb40604ba31312acf7e4619674631777)
1 /*-
2  * Copyright (c) 2010 Pawel Jakub Dawidek <pjd@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <sys/gpt.h>
32 
33 #ifndef LITTLE_ENDIAN
34 #error gpt.c works only for little endian architectures
35 #endif
36 
37 #include "stand.h"
38 #include "zlib.h"
39 #include "drv.h"
40 #include "gpt.h"
41 
42 static struct gpt_hdr hdr_primary, hdr_backup, *gpthdr;
43 static uint64_t hdr_primary_lba, hdr_backup_lba;
44 static struct gpt_ent table_primary[MAXTBLENTS], table_backup[MAXTBLENTS];
45 static struct gpt_ent *gpttable;
46 static int curent, bootonce;
47 
48 /*
49  * Buffer below 64kB passed on gptread(), which can hold at least
50  * one sector of data (512 bytes).
51  */
52 static char *secbuf;
53 
54 static void
55 gptupdate(const char *which, struct dsk *dskp, struct gpt_hdr *hdr,
56     struct gpt_ent *table)
57 {
58 	int entries_per_sec, firstent;
59 	daddr_t slba;
60 
61 	/*
62 	 * We need to update the following for both primary and backup GPT:
63 	 * 1. Sector on disk that contains current partition.
64 	 * 2. Partition table checksum.
65 	 * 3. Header checksum.
66 	 * 4. Header on disk.
67 	 */
68 
69 	entries_per_sec = DEV_BSIZE / hdr->hdr_entsz;
70 	slba = curent / entries_per_sec;
71 	firstent = slba * entries_per_sec;
72 	bcopy(&table[firstent], secbuf, DEV_BSIZE);
73 	slba += hdr->hdr_lba_table;
74 	if (drvwrite(dskp, secbuf, slba, 1)) {
75 		printf("%s: unable to update %s GPT partition table\n",
76 		    BOOTPROG, which);
77 		return;
78 	}
79 	hdr->hdr_crc_table = crc32(0, Z_NULL, 0);
80 	hdr->hdr_crc_table = crc32(hdr->hdr_crc_table, (const Bytef *)table,
81 	    hdr->hdr_entries * hdr->hdr_entsz);
82 	hdr->hdr_crc_self = crc32(0, Z_NULL, 0);;
83 	hdr->hdr_crc_self = crc32(hdr->hdr_crc_self, (const Bytef *)hdr,
84 	    hdr->hdr_size);
85 	bzero(secbuf, DEV_BSIZE);
86 	bcopy(hdr, secbuf, hdr->hdr_size);
87 	if (drvwrite(dskp, secbuf, hdr->hdr_lba_self, 1)) {
88 		printf("%s: unable to update %s GPT header\n", BOOTPROG, which);
89 		return;
90 	}
91 }
92 
93 int
94 gptfind(const uuid_t *uuid, struct dsk *dskp, int part)
95 {
96 	struct gpt_ent *ent;
97 	int firsttry;
98 
99 	if (part >= 0) {
100 		if (part == 0 || part > gpthdr->hdr_entries) {
101 			printf("%s: invalid partition index\n", BOOTPROG);
102 			return (-1);
103 		}
104 		ent = &gpttable[part - 1];
105 		if (bcmp(&ent->ent_type, uuid, sizeof(uuid_t)) != 0) {
106 			printf("%s: specified partition is not UFS\n",
107 			    BOOTPROG);
108 			return (-1);
109 		}
110 		curent = part - 1;
111 		goto found;
112 	}
113 
114 	firsttry = (curent == -1);
115 	curent++;
116 	if (curent >= gpthdr->hdr_entries) {
117 		curent = gpthdr->hdr_entries;
118 		return (-1);
119 	}
120 	if (bootonce) {
121 		/*
122 		 * First look for partition with both GPT_ENT_ATTR_BOOTME and
123 		 * GPT_ENT_ATTR_BOOTONCE flags.
124 		 */
125 		for (; curent < gpthdr->hdr_entries; curent++) {
126 			ent = &gpttable[curent];
127 			if (bcmp(&ent->ent_type, uuid, sizeof(uuid_t)) != 0)
128 				continue;
129 			if (!(ent->ent_attr & GPT_ENT_ATTR_BOOTME))
130 				continue;
131 			if (!(ent->ent_attr & GPT_ENT_ATTR_BOOTONCE))
132 				continue;
133 			/* Ok, found one. */
134 			goto found;
135 		}
136 		bootonce = 0;
137 		curent = 0;
138 	}
139 	for (; curent < gpthdr->hdr_entries; curent++) {
140 		ent = &gpttable[curent];
141 		if (bcmp(&ent->ent_type, uuid, sizeof(uuid_t)) != 0)
142 			continue;
143 		if (!(ent->ent_attr & GPT_ENT_ATTR_BOOTME))
144 			continue;
145 		if (ent->ent_attr & GPT_ENT_ATTR_BOOTONCE)
146 			continue;
147 		/* Ok, found one. */
148 		goto found;
149 	}
150 	if (firsttry) {
151 		/*
152 		 * No partition with BOOTME flag was found, try to boot from
153 		 * first UFS partition.
154 		 */
155 		for (curent = 0; curent < gpthdr->hdr_entries; curent++) {
156 			ent = &gpttable[curent];
157 			if (bcmp(&ent->ent_type, uuid, sizeof(uuid_t)) != 0)
158 				continue;
159 			/* Ok, found one. */
160 			goto found;
161 		}
162 	}
163 	return (-1);
164 found:
165 	dskp->part = curent + 1;
166 	ent = &gpttable[curent];
167 	dskp->start = ent->ent_lba_start;
168 	if (ent->ent_attr & GPT_ENT_ATTR_BOOTONCE) {
169 		/*
170 		 * Clear BOOTME, but leave BOOTONCE set before trying to
171 		 * boot from this partition.
172 		 */
173 		if (hdr_primary_lba > 0) {
174 			table_primary[curent].ent_attr &= ~GPT_ENT_ATTR_BOOTME;
175 			gptupdate("primary", dskp, &hdr_primary, table_primary);
176 		}
177 		if (hdr_backup_lba > 0) {
178 			table_backup[curent].ent_attr &= ~GPT_ENT_ATTR_BOOTME;
179 			gptupdate("backup", dskp, &hdr_backup, table_backup);
180 		}
181 	}
182 	return (0);
183 }
184 
185 static int
186 gptread_hdr(const char *which, struct dsk *dskp, struct gpt_hdr *hdr,
187     uint64_t hdrlba)
188 {
189 	uint32_t crc;
190 
191 	if (drvread(dskp, secbuf, hdrlba, 1)) {
192 		printf("%s: unable to read %s GPT header\n", BOOTPROG, which);
193 		return (-1);
194 	}
195 	bcopy(secbuf, hdr, sizeof(*hdr));
196 	if (bcmp(hdr->hdr_sig, GPT_HDR_SIG, sizeof(hdr->hdr_sig)) != 0 ||
197 	    hdr->hdr_lba_self != hdrlba || hdr->hdr_revision < 0x00010000 ||
198 	    hdr->hdr_entsz < sizeof(struct gpt_ent) ||
199 	    hdr->hdr_entries > MAXTBLENTS || DEV_BSIZE % hdr->hdr_entsz != 0) {
200 		printf("%s: invalid %s GPT header\n", BOOTPROG, which);
201 		return (-1);
202 	}
203 	crc = hdr->hdr_crc_self;
204 	hdr->hdr_crc_self = crc32(0, Z_NULL, 0);
205 	if (crc32(hdr->hdr_crc_self, (const Bytef *)hdr, hdr->hdr_size) !=
206 	    crc) {
207 		printf("%s: %s GPT header checksum mismatch\n", BOOTPROG,
208 		    which);
209 		return (-1);
210 	}
211 	hdr->hdr_crc_self = crc;
212 	return (0);
213 }
214 
215 void
216 gptbootfailed(struct dsk *dskp)
217 {
218 
219 	if (!(gpttable[curent].ent_attr & GPT_ENT_ATTR_BOOTONCE))
220 		return;
221 
222 	if (hdr_primary_lba > 0) {
223 		table_primary[curent].ent_attr &= ~GPT_ENT_ATTR_BOOTONCE;
224 		table_primary[curent].ent_attr |= GPT_ENT_ATTR_BOOTFAILED;
225 		gptupdate("primary", dskp, &hdr_primary, table_primary);
226 	}
227 	if (hdr_backup_lba > 0) {
228 		table_backup[curent].ent_attr &= ~GPT_ENT_ATTR_BOOTONCE;
229 		table_backup[curent].ent_attr |= GPT_ENT_ATTR_BOOTFAILED;
230 		gptupdate("backup", dskp, &hdr_backup, table_backup);
231 	}
232 }
233 
234 static void
235 gptbootconv(const char *which, struct dsk *dskp, struct gpt_hdr *hdr,
236     struct gpt_ent *table)
237 {
238 	struct gpt_ent *ent;
239 	daddr_t slba;
240 	int table_updated, sector_updated;
241 	int entries_per_sec, nent, part;
242 
243 	table_updated = 0;
244 	entries_per_sec = DEV_BSIZE / hdr->hdr_entsz;
245 	for (nent = 0, slba = hdr->hdr_lba_table;
246 	     slba < hdr->hdr_lba_table + hdr->hdr_entries / entries_per_sec;
247 	     slba++, nent += entries_per_sec) {
248 		sector_updated = 0;
249 		for (part = 0; part < entries_per_sec; part++) {
250 			ent = &table[nent + part];
251 			if ((ent->ent_attr & (GPT_ENT_ATTR_BOOTME |
252 			    GPT_ENT_ATTR_BOOTONCE |
253 			    GPT_ENT_ATTR_BOOTFAILED)) !=
254 			    GPT_ENT_ATTR_BOOTONCE) {
255 				continue;
256 			}
257 			ent->ent_attr &= ~GPT_ENT_ATTR_BOOTONCE;
258 			ent->ent_attr |= GPT_ENT_ATTR_BOOTFAILED;
259 			table_updated = 1;
260 			sector_updated = 1;
261 		}
262 		if (!sector_updated)
263 			continue;
264 		bcopy(&table[nent], secbuf, DEV_BSIZE);
265 		if (drvwrite(dskp, secbuf, slba, 1)) {
266 			printf("%s: unable to update %s GPT partition table\n",
267 			    BOOTPROG, which);
268 		}
269 	}
270 	if (!table_updated)
271 		return;
272 	hdr->hdr_crc_table = crc32(0, Z_NULL, 0);
273 	hdr->hdr_crc_table = crc32(hdr->hdr_crc_table, (const Bytef *)table,
274 	    hdr->hdr_entries * hdr->hdr_entsz);
275 	hdr->hdr_crc_self = crc32(0, Z_NULL, 0);
276 	hdr->hdr_crc_self = crc32(hdr->hdr_crc_self, (const Bytef *)hdr,
277 	    hdr->hdr_size);
278 	bzero(secbuf, DEV_BSIZE);
279 	bcopy(hdr, secbuf, hdr->hdr_size);
280 	if (drvwrite(dskp, secbuf, hdr->hdr_lba_self, 1))
281 		printf("%s: unable to update %s GPT header\n", BOOTPROG, which);
282 }
283 
284 static int
285 gptread_table(const char *which, struct dsk *dskp, struct gpt_hdr *hdr,
286     struct gpt_ent *table)
287 {
288 	struct gpt_ent *ent;
289 	int entries_per_sec;
290 	int part, nent;
291 	daddr_t slba;
292 
293 	if (hdr->hdr_entries == 0)
294 		return (0);
295 
296 	entries_per_sec = DEV_BSIZE / hdr->hdr_entsz;
297 	slba = hdr->hdr_lba_table;
298 	nent = 0;
299 	for (;;) {
300 		if (drvread(dskp, secbuf, slba, 1)) {
301 			printf("%s: unable to read %s GPT partition table\n",
302 			    BOOTPROG, which);
303 			return (-1);
304 		}
305 		ent = (struct gpt_ent *)secbuf;
306 		for (part = 0; part < entries_per_sec; part++, ent++) {
307 			bcopy(ent, &table[nent], sizeof(table[nent]));
308 			if (++nent >= hdr->hdr_entries)
309 				break;
310 		}
311 		if (nent >= hdr->hdr_entries)
312 			break;
313 		slba++;
314 	}
315 	if (crc32(0, (const Bytef *)table, nent * hdr->hdr_entsz) !=
316 	    hdr->hdr_crc_table) {
317 		printf("%s: %s GPT table checksum mismatch\n", BOOTPROG, which);
318 		return (-1);
319 	}
320 	return (0);
321 }
322 
323 int
324 gptread(struct dsk *dskp, char *buf)
325 {
326 	uint64_t altlba;
327 
328 	/*
329 	 * Read and verify both GPT headers: primary and backup.
330 	 */
331 
332 	secbuf = buf;
333 	hdr_primary_lba = hdr_backup_lba = 0;
334 	curent = -1;
335 	bootonce = 1;
336 	dskp->start = 0;
337 
338 	if (gptread_hdr("primary", dskp, &hdr_primary, 1) == 0 &&
339 	    gptread_table("primary", dskp, &hdr_primary, table_primary) == 0) {
340 		hdr_primary_lba = hdr_primary.hdr_lba_self;
341 		gpthdr = &hdr_primary;
342 		gpttable = table_primary;
343 	}
344 
345 	if (hdr_primary_lba > 0) {
346 		/*
347 		 * If primary header is valid, we can get backup
348 		 * header location from there.
349 		 */
350 		altlba = hdr_primary.hdr_lba_alt;
351 	} else {
352 		altlba = drvsize(dskp);
353 		if (altlba > 0)
354 			altlba--;
355 	}
356 	if (altlba == 0)
357 		printf("%s: unable to locate backup GPT header\n", BOOTPROG);
358 	else if (gptread_hdr("backup", dskp, &hdr_backup, altlba) == 0 &&
359 	    gptread_table("backup", dskp, &hdr_backup, table_backup) == 0) {
360 		hdr_backup_lba = hdr_backup.hdr_lba_self;
361 		if (hdr_primary_lba == 0) {
362 			gpthdr = &hdr_backup;
363 			gpttable = table_backup;
364 			printf("%s: using backup GPT\n", BOOTPROG);
365 		}
366 	}
367 
368 	/*
369 	 * Convert all BOOTONCE without BOOTME flags into BOOTFAILED.
370 	 * BOOTONCE without BOOTME means that we tried to boot from it,
371 	 * but failed after leaving gptboot and machine was rebooted.
372 	 * We don't want to leave partitions marked as BOOTONCE only,
373 	 * because when we boot successfully start-up scripts should
374 	 * find at most one partition with only BOOTONCE flag and this
375 	 * will mean that we booted from that partition.
376 	 */
377 	if (hdr_primary_lba != 0)
378 		gptbootconv("primary", dskp, &hdr_primary, table_primary);
379 	if (hdr_backup_lba != 0)
380 		gptbootconv("backup", dskp, &hdr_backup, table_backup);
381 
382 	if (hdr_primary_lba == 0 && hdr_backup_lba == 0)
383 		return (-1);
384 	return (0);
385 }
386