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