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