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