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/param.h>
28ca987d46SWarner Losh #include <sys/gpt.h>
29ca987d46SWarner Losh
30ca987d46SWarner Losh #ifndef LITTLE_ENDIAN
31ca987d46SWarner Losh #error gpt.c works only for little endian architectures
32ca987d46SWarner Losh #endif
33ca987d46SWarner Losh
3465628439SWarner Losh #include "stand.h"
3527bae615SToomas Soome #include "zlib.h"
36ca987d46SWarner Losh #include "drv.h"
37ca987d46SWarner Losh #include "gpt.h"
38ca987d46SWarner Losh
39ca987d46SWarner Losh static struct gpt_hdr hdr_primary, hdr_backup, *gpthdr;
40ca987d46SWarner Losh static uint64_t hdr_primary_lba, hdr_backup_lba;
41ca987d46SWarner Losh static struct gpt_ent table_primary[MAXTBLENTS], table_backup[MAXTBLENTS];
42ca987d46SWarner Losh static struct gpt_ent *gpttable;
43ca987d46SWarner Losh static int curent, bootonce;
44ca987d46SWarner Losh
45ca987d46SWarner Losh /*
46ca987d46SWarner Losh * Buffer below 64kB passed on gptread(), which can hold at least
47ca987d46SWarner Losh * one sector of data (512 bytes).
48ca987d46SWarner Losh */
49ca987d46SWarner Losh static char *secbuf;
50ca987d46SWarner Losh
51ca987d46SWarner Losh static void
gptupdate(const char * which,struct dsk * dskp,struct gpt_hdr * hdr,struct gpt_ent * table)52ca987d46SWarner Losh gptupdate(const char *which, struct dsk *dskp, struct gpt_hdr *hdr,
53ca987d46SWarner Losh struct gpt_ent *table)
54ca987d46SWarner Losh {
55ca987d46SWarner Losh int entries_per_sec, firstent;
56ca987d46SWarner Losh daddr_t slba;
57ca987d46SWarner Losh
58ca987d46SWarner Losh /*
59ca987d46SWarner Losh * We need to update the following for both primary and backup GPT:
60ca987d46SWarner Losh * 1. Sector on disk that contains current partition.
61ca987d46SWarner Losh * 2. Partition table checksum.
62ca987d46SWarner Losh * 3. Header checksum.
63ca987d46SWarner Losh * 4. Header on disk.
64ca987d46SWarner Losh */
65ca987d46SWarner Losh
66ca987d46SWarner Losh entries_per_sec = DEV_BSIZE / hdr->hdr_entsz;
67ca987d46SWarner Losh slba = curent / entries_per_sec;
68ca987d46SWarner Losh firstent = slba * entries_per_sec;
69ca987d46SWarner Losh bcopy(&table[firstent], secbuf, DEV_BSIZE);
70ca987d46SWarner Losh slba += hdr->hdr_lba_table;
71ca987d46SWarner Losh if (drvwrite(dskp, secbuf, slba, 1)) {
72ca987d46SWarner Losh printf("%s: unable to update %s GPT partition table\n",
73ca987d46SWarner Losh BOOTPROG, which);
74ca987d46SWarner Losh return;
75ca987d46SWarner Losh }
7627bae615SToomas Soome hdr->hdr_crc_table = crc32(0, Z_NULL, 0);
7727bae615SToomas Soome hdr->hdr_crc_table = crc32(hdr->hdr_crc_table, (const Bytef *)table,
7827bae615SToomas Soome hdr->hdr_entries * hdr->hdr_entsz);
79*798ea06fSElliott Mitchell hdr->hdr_crc_self = crc32(0, Z_NULL, 0);
8027bae615SToomas Soome hdr->hdr_crc_self = crc32(hdr->hdr_crc_self, (const Bytef *)hdr,
8127bae615SToomas Soome 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
gptfind(const uuid_t * uuid,struct dsk * dskp,int part)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
gptread_hdr(const char * which,struct dsk * dskp,struct gpt_hdr * hdr,uint64_t hdrlba)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;
20127bae615SToomas Soome hdr->hdr_crc_self = crc32(0, Z_NULL, 0);
20227bae615SToomas Soome if (crc32(hdr->hdr_crc_self, (const Bytef *)hdr, hdr->hdr_size) !=
20327bae615SToomas Soome crc) {
204ca987d46SWarner Losh printf("%s: %s GPT header checksum mismatch\n", BOOTPROG,
205ca987d46SWarner Losh which);
206ca987d46SWarner Losh return (-1);
207ca987d46SWarner Losh }
208ca987d46SWarner Losh hdr->hdr_crc_self = crc;
209ca987d46SWarner Losh return (0);
210ca987d46SWarner Losh }
211ca987d46SWarner Losh
212ca987d46SWarner Losh void
gptbootfailed(struct dsk * dskp)213ca987d46SWarner Losh gptbootfailed(struct dsk *dskp)
214ca987d46SWarner Losh {
215ca987d46SWarner Losh
216ca987d46SWarner Losh if (!(gpttable[curent].ent_attr & GPT_ENT_ATTR_BOOTONCE))
217ca987d46SWarner Losh return;
218ca987d46SWarner Losh
219ca987d46SWarner Losh if (hdr_primary_lba > 0) {
220ca987d46SWarner Losh table_primary[curent].ent_attr &= ~GPT_ENT_ATTR_BOOTONCE;
221ca987d46SWarner Losh table_primary[curent].ent_attr |= GPT_ENT_ATTR_BOOTFAILED;
222ca987d46SWarner Losh gptupdate("primary", dskp, &hdr_primary, table_primary);
223ca987d46SWarner Losh }
224ca987d46SWarner Losh if (hdr_backup_lba > 0) {
225ca987d46SWarner Losh table_backup[curent].ent_attr &= ~GPT_ENT_ATTR_BOOTONCE;
226ca987d46SWarner Losh table_backup[curent].ent_attr |= GPT_ENT_ATTR_BOOTFAILED;
227ca987d46SWarner Losh gptupdate("backup", dskp, &hdr_backup, table_backup);
228ca987d46SWarner Losh }
229ca987d46SWarner Losh }
230ca987d46SWarner Losh
231ca987d46SWarner Losh static void
gptbootconv(const char * which,struct dsk * dskp,struct gpt_hdr * hdr,struct gpt_ent * table)232ca987d46SWarner Losh gptbootconv(const char *which, struct dsk *dskp, struct gpt_hdr *hdr,
233ca987d46SWarner Losh struct gpt_ent *table)
234ca987d46SWarner Losh {
235ca987d46SWarner Losh struct gpt_ent *ent;
236ca987d46SWarner Losh daddr_t slba;
237ca987d46SWarner Losh int table_updated, sector_updated;
238ca987d46SWarner Losh int entries_per_sec, nent, part;
239ca987d46SWarner Losh
240ca987d46SWarner Losh table_updated = 0;
241ca987d46SWarner Losh entries_per_sec = DEV_BSIZE / hdr->hdr_entsz;
242ca987d46SWarner Losh for (nent = 0, slba = hdr->hdr_lba_table;
243ca987d46SWarner Losh slba < hdr->hdr_lba_table + hdr->hdr_entries / entries_per_sec;
244ca987d46SWarner Losh slba++, nent += entries_per_sec) {
245ca987d46SWarner Losh sector_updated = 0;
246ca987d46SWarner Losh for (part = 0; part < entries_per_sec; part++) {
247ca987d46SWarner Losh ent = &table[nent + part];
248ca987d46SWarner Losh if ((ent->ent_attr & (GPT_ENT_ATTR_BOOTME |
249ca987d46SWarner Losh GPT_ENT_ATTR_BOOTONCE |
250ca987d46SWarner Losh GPT_ENT_ATTR_BOOTFAILED)) !=
251ca987d46SWarner Losh GPT_ENT_ATTR_BOOTONCE) {
252ca987d46SWarner Losh continue;
253ca987d46SWarner Losh }
254ca987d46SWarner Losh ent->ent_attr &= ~GPT_ENT_ATTR_BOOTONCE;
255ca987d46SWarner Losh ent->ent_attr |= GPT_ENT_ATTR_BOOTFAILED;
256ca987d46SWarner Losh table_updated = 1;
257ca987d46SWarner Losh sector_updated = 1;
258ca987d46SWarner Losh }
259ca987d46SWarner Losh if (!sector_updated)
260ca987d46SWarner Losh continue;
261ca987d46SWarner Losh bcopy(&table[nent], secbuf, DEV_BSIZE);
262ca987d46SWarner Losh if (drvwrite(dskp, secbuf, slba, 1)) {
263ca987d46SWarner Losh printf("%s: unable to update %s GPT partition table\n",
264ca987d46SWarner Losh BOOTPROG, which);
265ca987d46SWarner Losh }
266ca987d46SWarner Losh }
267ca987d46SWarner Losh if (!table_updated)
268ca987d46SWarner Losh return;
26927bae615SToomas Soome hdr->hdr_crc_table = crc32(0, Z_NULL, 0);
27027bae615SToomas Soome hdr->hdr_crc_table = crc32(hdr->hdr_crc_table, (const Bytef *)table,
27127bae615SToomas Soome hdr->hdr_entries * hdr->hdr_entsz);
27227bae615SToomas Soome hdr->hdr_crc_self = crc32(0, Z_NULL, 0);
27327bae615SToomas Soome hdr->hdr_crc_self = crc32(hdr->hdr_crc_self, (const Bytef *)hdr,
27427bae615SToomas Soome hdr->hdr_size);
275ca987d46SWarner Losh bzero(secbuf, DEV_BSIZE);
276ca987d46SWarner Losh bcopy(hdr, secbuf, hdr->hdr_size);
277ca987d46SWarner Losh if (drvwrite(dskp, secbuf, hdr->hdr_lba_self, 1))
278ca987d46SWarner Losh printf("%s: unable to update %s GPT header\n", BOOTPROG, which);
279ca987d46SWarner Losh }
280ca987d46SWarner Losh
281ca987d46SWarner Losh static int
gptread_table(const char * which,struct dsk * dskp,struct gpt_hdr * hdr,struct gpt_ent * table)282de357a73SWarner Losh gptread_table(const char *which, struct dsk *dskp, struct gpt_hdr *hdr,
283de357a73SWarner Losh struct gpt_ent *table)
284ca987d46SWarner Losh {
285ca987d46SWarner Losh struct gpt_ent *ent;
286ca987d46SWarner Losh int entries_per_sec;
287ca987d46SWarner Losh int part, nent;
288ca987d46SWarner Losh daddr_t slba;
289ca987d46SWarner Losh
290ca987d46SWarner Losh if (hdr->hdr_entries == 0)
291ca987d46SWarner Losh return (0);
292ca987d46SWarner Losh
293ca987d46SWarner Losh entries_per_sec = DEV_BSIZE / hdr->hdr_entsz;
294ca987d46SWarner Losh slba = hdr->hdr_lba_table;
295ca987d46SWarner Losh nent = 0;
296ca987d46SWarner Losh for (;;) {
297ca987d46SWarner Losh if (drvread(dskp, secbuf, slba, 1)) {
298ca987d46SWarner Losh printf("%s: unable to read %s GPT partition table\n",
299ca987d46SWarner Losh BOOTPROG, which);
300ca987d46SWarner Losh return (-1);
301ca987d46SWarner Losh }
302ca987d46SWarner Losh ent = (struct gpt_ent *)secbuf;
303ca987d46SWarner Losh for (part = 0; part < entries_per_sec; part++, ent++) {
304ca987d46SWarner Losh bcopy(ent, &table[nent], sizeof(table[nent]));
305ca987d46SWarner Losh if (++nent >= hdr->hdr_entries)
306ca987d46SWarner Losh break;
307ca987d46SWarner Losh }
308ca987d46SWarner Losh if (nent >= hdr->hdr_entries)
309ca987d46SWarner Losh break;
310ca987d46SWarner Losh slba++;
311ca987d46SWarner Losh }
31227bae615SToomas Soome if (crc32(0, (const Bytef *)table, nent * hdr->hdr_entsz) !=
31327bae615SToomas Soome hdr->hdr_crc_table) {
314ca987d46SWarner Losh printf("%s: %s GPT table checksum mismatch\n", BOOTPROG, which);
315ca987d46SWarner Losh return (-1);
316ca987d46SWarner Losh }
317ca987d46SWarner Losh return (0);
318ca987d46SWarner Losh }
319ca987d46SWarner Losh
320ca987d46SWarner Losh int
gptread(struct dsk * dskp,char * buf)321de357a73SWarner Losh gptread(struct dsk *dskp, char *buf)
322ca987d46SWarner Losh {
323ca987d46SWarner Losh uint64_t altlba;
324ca987d46SWarner Losh
325ca987d46SWarner Losh /*
326ca987d46SWarner Losh * Read and verify both GPT headers: primary and backup.
327ca987d46SWarner Losh */
328ca987d46SWarner Losh
329ca987d46SWarner Losh secbuf = buf;
330ca987d46SWarner Losh hdr_primary_lba = hdr_backup_lba = 0;
331ca987d46SWarner Losh curent = -1;
332ca987d46SWarner Losh bootonce = 1;
333ca987d46SWarner Losh dskp->start = 0;
334ca987d46SWarner Losh
335ca987d46SWarner Losh if (gptread_hdr("primary", dskp, &hdr_primary, 1) == 0 &&
336de357a73SWarner Losh gptread_table("primary", dskp, &hdr_primary, table_primary) == 0) {
337ca987d46SWarner Losh hdr_primary_lba = hdr_primary.hdr_lba_self;
338ca987d46SWarner Losh gpthdr = &hdr_primary;
339ca987d46SWarner Losh gpttable = table_primary;
340ca987d46SWarner Losh }
341ca987d46SWarner Losh
342ca987d46SWarner Losh if (hdr_primary_lba > 0) {
343ca987d46SWarner Losh /*
344ca987d46SWarner Losh * If primary header is valid, we can get backup
345ca987d46SWarner Losh * header location from there.
346ca987d46SWarner Losh */
347ca987d46SWarner Losh altlba = hdr_primary.hdr_lba_alt;
348ca987d46SWarner Losh } else {
349ca987d46SWarner Losh altlba = drvsize(dskp);
350ca987d46SWarner Losh if (altlba > 0)
351ca987d46SWarner Losh altlba--;
352ca987d46SWarner Losh }
353ca987d46SWarner Losh if (altlba == 0)
354ca987d46SWarner Losh printf("%s: unable to locate backup GPT header\n", BOOTPROG);
355ca987d46SWarner Losh else if (gptread_hdr("backup", dskp, &hdr_backup, altlba) == 0 &&
356de357a73SWarner Losh gptread_table("backup", dskp, &hdr_backup, table_backup) == 0) {
357ca987d46SWarner Losh hdr_backup_lba = hdr_backup.hdr_lba_self;
358ca987d46SWarner Losh if (hdr_primary_lba == 0) {
359ca987d46SWarner Losh gpthdr = &hdr_backup;
360ca987d46SWarner Losh gpttable = table_backup;
361ca987d46SWarner Losh printf("%s: using backup GPT\n", BOOTPROG);
362ca987d46SWarner Losh }
363ca987d46SWarner Losh }
364ca987d46SWarner Losh
365ca987d46SWarner Losh /*
366ca987d46SWarner Losh * Convert all BOOTONCE without BOOTME flags into BOOTFAILED.
367ca987d46SWarner Losh * BOOTONCE without BOOTME means that we tried to boot from it,
368ca987d46SWarner Losh * but failed after leaving gptboot and machine was rebooted.
369ca987d46SWarner Losh * We don't want to leave partitions marked as BOOTONCE only,
370ca987d46SWarner Losh * because when we boot successfully start-up scripts should
371ca987d46SWarner Losh * find at most one partition with only BOOTONCE flag and this
372ca987d46SWarner Losh * will mean that we booted from that partition.
373ca987d46SWarner Losh */
374ca987d46SWarner Losh if (hdr_primary_lba != 0)
375ca987d46SWarner Losh gptbootconv("primary", dskp, &hdr_primary, table_primary);
376ca987d46SWarner Losh if (hdr_backup_lba != 0)
377ca987d46SWarner Losh gptbootconv("backup", dskp, &hdr_backup, table_backup);
378ca987d46SWarner Losh
379ca987d46SWarner Losh if (hdr_primary_lba == 0 && hdr_backup_lba == 0)
380ca987d46SWarner Losh return (-1);
381ca987d46SWarner Losh return (0);
382ca987d46SWarner Losh }
383