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" 3827bae615SToomas Soome #include "zlib.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 } 7927bae615SToomas Soome hdr->hdr_crc_table = crc32(0, Z_NULL, 0); 8027bae615SToomas Soome hdr->hdr_crc_table = crc32(hdr->hdr_crc_table, (const Bytef *)table, 8127bae615SToomas Soome hdr->hdr_entries * hdr->hdr_entsz); 82*798ea06fSElliott Mitchell hdr->hdr_crc_self = crc32(0, Z_NULL, 0); 8327bae615SToomas Soome hdr->hdr_crc_self = crc32(hdr->hdr_crc_self, (const Bytef *)hdr, 8427bae615SToomas Soome hdr->hdr_size); 85ca987d46SWarner Losh bzero(secbuf, DEV_BSIZE); 86ca987d46SWarner Losh bcopy(hdr, secbuf, hdr->hdr_size); 87ca987d46SWarner Losh if (drvwrite(dskp, secbuf, hdr->hdr_lba_self, 1)) { 88ca987d46SWarner Losh printf("%s: unable to update %s GPT header\n", BOOTPROG, which); 89ca987d46SWarner Losh return; 90ca987d46SWarner Losh } 91ca987d46SWarner Losh } 92ca987d46SWarner Losh 93ca987d46SWarner Losh int 94ca987d46SWarner Losh gptfind(const uuid_t *uuid, struct dsk *dskp, int part) 95ca987d46SWarner Losh { 96ca987d46SWarner Losh struct gpt_ent *ent; 97ca987d46SWarner Losh int firsttry; 98ca987d46SWarner Losh 99ca987d46SWarner Losh if (part >= 0) { 100ca987d46SWarner Losh if (part == 0 || part > gpthdr->hdr_entries) { 101ca987d46SWarner Losh printf("%s: invalid partition index\n", BOOTPROG); 102ca987d46SWarner Losh return (-1); 103ca987d46SWarner Losh } 104ca987d46SWarner Losh ent = &gpttable[part - 1]; 105ca987d46SWarner Losh if (bcmp(&ent->ent_type, uuid, sizeof(uuid_t)) != 0) { 106ca987d46SWarner Losh printf("%s: specified partition is not UFS\n", 107ca987d46SWarner Losh BOOTPROG); 108ca987d46SWarner Losh return (-1); 109ca987d46SWarner Losh } 110ca987d46SWarner Losh curent = part - 1; 111ca987d46SWarner Losh goto found; 112ca987d46SWarner Losh } 113ca987d46SWarner Losh 114ca987d46SWarner Losh firsttry = (curent == -1); 115ca987d46SWarner Losh curent++; 116ca987d46SWarner Losh if (curent >= gpthdr->hdr_entries) { 117ca987d46SWarner Losh curent = gpthdr->hdr_entries; 118ca987d46SWarner Losh return (-1); 119ca987d46SWarner Losh } 120ca987d46SWarner Losh if (bootonce) { 121ca987d46SWarner Losh /* 122ca987d46SWarner Losh * First look for partition with both GPT_ENT_ATTR_BOOTME and 123ca987d46SWarner Losh * GPT_ENT_ATTR_BOOTONCE flags. 124ca987d46SWarner Losh */ 125ca987d46SWarner Losh for (; curent < gpthdr->hdr_entries; curent++) { 126ca987d46SWarner Losh ent = &gpttable[curent]; 127ca987d46SWarner Losh if (bcmp(&ent->ent_type, uuid, sizeof(uuid_t)) != 0) 128ca987d46SWarner Losh continue; 129ca987d46SWarner Losh if (!(ent->ent_attr & GPT_ENT_ATTR_BOOTME)) 130ca987d46SWarner Losh continue; 131ca987d46SWarner Losh if (!(ent->ent_attr & GPT_ENT_ATTR_BOOTONCE)) 132ca987d46SWarner Losh continue; 133ca987d46SWarner Losh /* Ok, found one. */ 134ca987d46SWarner Losh goto found; 135ca987d46SWarner Losh } 136ca987d46SWarner Losh bootonce = 0; 137ca987d46SWarner Losh curent = 0; 138ca987d46SWarner Losh } 139ca987d46SWarner Losh for (; curent < gpthdr->hdr_entries; curent++) { 140ca987d46SWarner Losh ent = &gpttable[curent]; 141ca987d46SWarner Losh if (bcmp(&ent->ent_type, uuid, sizeof(uuid_t)) != 0) 142ca987d46SWarner Losh continue; 143ca987d46SWarner Losh if (!(ent->ent_attr & GPT_ENT_ATTR_BOOTME)) 144ca987d46SWarner Losh continue; 145ca987d46SWarner Losh if (ent->ent_attr & GPT_ENT_ATTR_BOOTONCE) 146ca987d46SWarner Losh continue; 147ca987d46SWarner Losh /* Ok, found one. */ 148ca987d46SWarner Losh goto found; 149ca987d46SWarner Losh } 150ca987d46SWarner Losh if (firsttry) { 151ca987d46SWarner Losh /* 152ca987d46SWarner Losh * No partition with BOOTME flag was found, try to boot from 153ca987d46SWarner Losh * first UFS partition. 154ca987d46SWarner Losh */ 155ca987d46SWarner Losh for (curent = 0; curent < gpthdr->hdr_entries; curent++) { 156ca987d46SWarner Losh ent = &gpttable[curent]; 157ca987d46SWarner Losh if (bcmp(&ent->ent_type, uuid, sizeof(uuid_t)) != 0) 158ca987d46SWarner Losh continue; 159ca987d46SWarner Losh /* Ok, found one. */ 160ca987d46SWarner Losh goto found; 161ca987d46SWarner Losh } 162ca987d46SWarner Losh } 163ca987d46SWarner Losh return (-1); 164ca987d46SWarner Losh found: 165ca987d46SWarner Losh dskp->part = curent + 1; 166ca987d46SWarner Losh ent = &gpttable[curent]; 167ca987d46SWarner Losh dskp->start = ent->ent_lba_start; 168ca987d46SWarner Losh if (ent->ent_attr & GPT_ENT_ATTR_BOOTONCE) { 169ca987d46SWarner Losh /* 170ca987d46SWarner Losh * Clear BOOTME, but leave BOOTONCE set before trying to 171ca987d46SWarner Losh * boot from this partition. 172ca987d46SWarner Losh */ 173ca987d46SWarner Losh if (hdr_primary_lba > 0) { 174ca987d46SWarner Losh table_primary[curent].ent_attr &= ~GPT_ENT_ATTR_BOOTME; 175ca987d46SWarner Losh gptupdate("primary", dskp, &hdr_primary, table_primary); 176ca987d46SWarner Losh } 177ca987d46SWarner Losh if (hdr_backup_lba > 0) { 178ca987d46SWarner Losh table_backup[curent].ent_attr &= ~GPT_ENT_ATTR_BOOTME; 179ca987d46SWarner Losh gptupdate("backup", dskp, &hdr_backup, table_backup); 180ca987d46SWarner Losh } 181ca987d46SWarner Losh } 182ca987d46SWarner Losh return (0); 183ca987d46SWarner Losh } 184ca987d46SWarner Losh 185ca987d46SWarner Losh static int 186ca987d46SWarner Losh gptread_hdr(const char *which, struct dsk *dskp, struct gpt_hdr *hdr, 187ca987d46SWarner Losh uint64_t hdrlba) 188ca987d46SWarner Losh { 189ca987d46SWarner Losh uint32_t crc; 190ca987d46SWarner Losh 191ca987d46SWarner Losh if (drvread(dskp, secbuf, hdrlba, 1)) { 192ca987d46SWarner Losh printf("%s: unable to read %s GPT header\n", BOOTPROG, which); 193ca987d46SWarner Losh return (-1); 194ca987d46SWarner Losh } 195ca987d46SWarner Losh bcopy(secbuf, hdr, sizeof(*hdr)); 196ca987d46SWarner Losh if (bcmp(hdr->hdr_sig, GPT_HDR_SIG, sizeof(hdr->hdr_sig)) != 0 || 197ca987d46SWarner Losh hdr->hdr_lba_self != hdrlba || hdr->hdr_revision < 0x00010000 || 198ca987d46SWarner Losh hdr->hdr_entsz < sizeof(struct gpt_ent) || 199ca987d46SWarner Losh hdr->hdr_entries > MAXTBLENTS || DEV_BSIZE % hdr->hdr_entsz != 0) { 200ca987d46SWarner Losh printf("%s: invalid %s GPT header\n", BOOTPROG, which); 201ca987d46SWarner Losh return (-1); 202ca987d46SWarner Losh } 203ca987d46SWarner Losh crc = hdr->hdr_crc_self; 20427bae615SToomas Soome hdr->hdr_crc_self = crc32(0, Z_NULL, 0); 20527bae615SToomas Soome if (crc32(hdr->hdr_crc_self, (const Bytef *)hdr, hdr->hdr_size) != 20627bae615SToomas Soome crc) { 207ca987d46SWarner Losh printf("%s: %s GPT header checksum mismatch\n", BOOTPROG, 208ca987d46SWarner Losh which); 209ca987d46SWarner Losh return (-1); 210ca987d46SWarner Losh } 211ca987d46SWarner Losh hdr->hdr_crc_self = crc; 212ca987d46SWarner Losh return (0); 213ca987d46SWarner Losh } 214ca987d46SWarner Losh 215ca987d46SWarner Losh void 216ca987d46SWarner Losh gptbootfailed(struct dsk *dskp) 217ca987d46SWarner Losh { 218ca987d46SWarner Losh 219ca987d46SWarner Losh if (!(gpttable[curent].ent_attr & GPT_ENT_ATTR_BOOTONCE)) 220ca987d46SWarner Losh return; 221ca987d46SWarner Losh 222ca987d46SWarner Losh if (hdr_primary_lba > 0) { 223ca987d46SWarner Losh table_primary[curent].ent_attr &= ~GPT_ENT_ATTR_BOOTONCE; 224ca987d46SWarner Losh table_primary[curent].ent_attr |= GPT_ENT_ATTR_BOOTFAILED; 225ca987d46SWarner Losh gptupdate("primary", dskp, &hdr_primary, table_primary); 226ca987d46SWarner Losh } 227ca987d46SWarner Losh if (hdr_backup_lba > 0) { 228ca987d46SWarner Losh table_backup[curent].ent_attr &= ~GPT_ENT_ATTR_BOOTONCE; 229ca987d46SWarner Losh table_backup[curent].ent_attr |= GPT_ENT_ATTR_BOOTFAILED; 230ca987d46SWarner Losh gptupdate("backup", dskp, &hdr_backup, table_backup); 231ca987d46SWarner Losh } 232ca987d46SWarner Losh } 233ca987d46SWarner Losh 234ca987d46SWarner Losh static void 235ca987d46SWarner Losh gptbootconv(const char *which, struct dsk *dskp, struct gpt_hdr *hdr, 236ca987d46SWarner Losh struct gpt_ent *table) 237ca987d46SWarner Losh { 238ca987d46SWarner Losh struct gpt_ent *ent; 239ca987d46SWarner Losh daddr_t slba; 240ca987d46SWarner Losh int table_updated, sector_updated; 241ca987d46SWarner Losh int entries_per_sec, nent, part; 242ca987d46SWarner Losh 243ca987d46SWarner Losh table_updated = 0; 244ca987d46SWarner Losh entries_per_sec = DEV_BSIZE / hdr->hdr_entsz; 245ca987d46SWarner Losh for (nent = 0, slba = hdr->hdr_lba_table; 246ca987d46SWarner Losh slba < hdr->hdr_lba_table + hdr->hdr_entries / entries_per_sec; 247ca987d46SWarner Losh slba++, nent += entries_per_sec) { 248ca987d46SWarner Losh sector_updated = 0; 249ca987d46SWarner Losh for (part = 0; part < entries_per_sec; part++) { 250ca987d46SWarner Losh ent = &table[nent + part]; 251ca987d46SWarner Losh if ((ent->ent_attr & (GPT_ENT_ATTR_BOOTME | 252ca987d46SWarner Losh GPT_ENT_ATTR_BOOTONCE | 253ca987d46SWarner Losh GPT_ENT_ATTR_BOOTFAILED)) != 254ca987d46SWarner Losh GPT_ENT_ATTR_BOOTONCE) { 255ca987d46SWarner Losh continue; 256ca987d46SWarner Losh } 257ca987d46SWarner Losh ent->ent_attr &= ~GPT_ENT_ATTR_BOOTONCE; 258ca987d46SWarner Losh ent->ent_attr |= GPT_ENT_ATTR_BOOTFAILED; 259ca987d46SWarner Losh table_updated = 1; 260ca987d46SWarner Losh sector_updated = 1; 261ca987d46SWarner Losh } 262ca987d46SWarner Losh if (!sector_updated) 263ca987d46SWarner Losh continue; 264ca987d46SWarner Losh bcopy(&table[nent], secbuf, DEV_BSIZE); 265ca987d46SWarner Losh if (drvwrite(dskp, secbuf, slba, 1)) { 266ca987d46SWarner Losh printf("%s: unable to update %s GPT partition table\n", 267ca987d46SWarner Losh BOOTPROG, which); 268ca987d46SWarner Losh } 269ca987d46SWarner Losh } 270ca987d46SWarner Losh if (!table_updated) 271ca987d46SWarner Losh return; 27227bae615SToomas Soome hdr->hdr_crc_table = crc32(0, Z_NULL, 0); 27327bae615SToomas Soome hdr->hdr_crc_table = crc32(hdr->hdr_crc_table, (const Bytef *)table, 27427bae615SToomas Soome hdr->hdr_entries * hdr->hdr_entsz); 27527bae615SToomas Soome hdr->hdr_crc_self = crc32(0, Z_NULL, 0); 27627bae615SToomas Soome hdr->hdr_crc_self = crc32(hdr->hdr_crc_self, (const Bytef *)hdr, 27727bae615SToomas Soome hdr->hdr_size); 278ca987d46SWarner Losh bzero(secbuf, DEV_BSIZE); 279ca987d46SWarner Losh bcopy(hdr, secbuf, hdr->hdr_size); 280ca987d46SWarner Losh if (drvwrite(dskp, secbuf, hdr->hdr_lba_self, 1)) 281ca987d46SWarner Losh printf("%s: unable to update %s GPT header\n", BOOTPROG, which); 282ca987d46SWarner Losh } 283ca987d46SWarner Losh 284ca987d46SWarner Losh static int 285de357a73SWarner Losh gptread_table(const char *which, struct dsk *dskp, struct gpt_hdr *hdr, 286de357a73SWarner Losh struct gpt_ent *table) 287ca987d46SWarner Losh { 288ca987d46SWarner Losh struct gpt_ent *ent; 289ca987d46SWarner Losh int entries_per_sec; 290ca987d46SWarner Losh int part, nent; 291ca987d46SWarner Losh daddr_t slba; 292ca987d46SWarner Losh 293ca987d46SWarner Losh if (hdr->hdr_entries == 0) 294ca987d46SWarner Losh return (0); 295ca987d46SWarner Losh 296ca987d46SWarner Losh entries_per_sec = DEV_BSIZE / hdr->hdr_entsz; 297ca987d46SWarner Losh slba = hdr->hdr_lba_table; 298ca987d46SWarner Losh nent = 0; 299ca987d46SWarner Losh for (;;) { 300ca987d46SWarner Losh if (drvread(dskp, secbuf, slba, 1)) { 301ca987d46SWarner Losh printf("%s: unable to read %s GPT partition table\n", 302ca987d46SWarner Losh BOOTPROG, which); 303ca987d46SWarner Losh return (-1); 304ca987d46SWarner Losh } 305ca987d46SWarner Losh ent = (struct gpt_ent *)secbuf; 306ca987d46SWarner Losh for (part = 0; part < entries_per_sec; part++, ent++) { 307ca987d46SWarner Losh bcopy(ent, &table[nent], sizeof(table[nent])); 308ca987d46SWarner Losh if (++nent >= hdr->hdr_entries) 309ca987d46SWarner Losh break; 310ca987d46SWarner Losh } 311ca987d46SWarner Losh if (nent >= hdr->hdr_entries) 312ca987d46SWarner Losh break; 313ca987d46SWarner Losh slba++; 314ca987d46SWarner Losh } 31527bae615SToomas Soome if (crc32(0, (const Bytef *)table, nent * hdr->hdr_entsz) != 31627bae615SToomas Soome hdr->hdr_crc_table) { 317ca987d46SWarner Losh printf("%s: %s GPT table checksum mismatch\n", BOOTPROG, which); 318ca987d46SWarner Losh return (-1); 319ca987d46SWarner Losh } 320ca987d46SWarner Losh return (0); 321ca987d46SWarner Losh } 322ca987d46SWarner Losh 323ca987d46SWarner Losh int 324de357a73SWarner Losh gptread(struct dsk *dskp, char *buf) 325ca987d46SWarner Losh { 326ca987d46SWarner Losh uint64_t altlba; 327ca987d46SWarner Losh 328ca987d46SWarner Losh /* 329ca987d46SWarner Losh * Read and verify both GPT headers: primary and backup. 330ca987d46SWarner Losh */ 331ca987d46SWarner Losh 332ca987d46SWarner Losh secbuf = buf; 333ca987d46SWarner Losh hdr_primary_lba = hdr_backup_lba = 0; 334ca987d46SWarner Losh curent = -1; 335ca987d46SWarner Losh bootonce = 1; 336ca987d46SWarner Losh dskp->start = 0; 337ca987d46SWarner Losh 338ca987d46SWarner Losh if (gptread_hdr("primary", dskp, &hdr_primary, 1) == 0 && 339de357a73SWarner Losh gptread_table("primary", dskp, &hdr_primary, table_primary) == 0) { 340ca987d46SWarner Losh hdr_primary_lba = hdr_primary.hdr_lba_self; 341ca987d46SWarner Losh gpthdr = &hdr_primary; 342ca987d46SWarner Losh gpttable = table_primary; 343ca987d46SWarner Losh } 344ca987d46SWarner Losh 345ca987d46SWarner Losh if (hdr_primary_lba > 0) { 346ca987d46SWarner Losh /* 347ca987d46SWarner Losh * If primary header is valid, we can get backup 348ca987d46SWarner Losh * header location from there. 349ca987d46SWarner Losh */ 350ca987d46SWarner Losh altlba = hdr_primary.hdr_lba_alt; 351ca987d46SWarner Losh } else { 352ca987d46SWarner Losh altlba = drvsize(dskp); 353ca987d46SWarner Losh if (altlba > 0) 354ca987d46SWarner Losh altlba--; 355ca987d46SWarner Losh } 356ca987d46SWarner Losh if (altlba == 0) 357ca987d46SWarner Losh printf("%s: unable to locate backup GPT header\n", BOOTPROG); 358ca987d46SWarner Losh else if (gptread_hdr("backup", dskp, &hdr_backup, altlba) == 0 && 359de357a73SWarner Losh gptread_table("backup", dskp, &hdr_backup, table_backup) == 0) { 360ca987d46SWarner Losh hdr_backup_lba = hdr_backup.hdr_lba_self; 361ca987d46SWarner Losh if (hdr_primary_lba == 0) { 362ca987d46SWarner Losh gpthdr = &hdr_backup; 363ca987d46SWarner Losh gpttable = table_backup; 364ca987d46SWarner Losh printf("%s: using backup GPT\n", BOOTPROG); 365ca987d46SWarner Losh } 366ca987d46SWarner Losh } 367ca987d46SWarner Losh 368ca987d46SWarner Losh /* 369ca987d46SWarner Losh * Convert all BOOTONCE without BOOTME flags into BOOTFAILED. 370ca987d46SWarner Losh * BOOTONCE without BOOTME means that we tried to boot from it, 371ca987d46SWarner Losh * but failed after leaving gptboot and machine was rebooted. 372ca987d46SWarner Losh * We don't want to leave partitions marked as BOOTONCE only, 373ca987d46SWarner Losh * because when we boot successfully start-up scripts should 374ca987d46SWarner Losh * find at most one partition with only BOOTONCE flag and this 375ca987d46SWarner Losh * will mean that we booted from that partition. 376ca987d46SWarner Losh */ 377ca987d46SWarner Losh if (hdr_primary_lba != 0) 378ca987d46SWarner Losh gptbootconv("primary", dskp, &hdr_primary, table_primary); 379ca987d46SWarner Losh if (hdr_backup_lba != 0) 380ca987d46SWarner Losh gptbootconv("backup", dskp, &hdr_backup, table_backup); 381ca987d46SWarner Losh 382ca987d46SWarner Losh if (hdr_primary_lba == 0 && hdr_backup_lba == 0) 383ca987d46SWarner Losh return (-1); 384ca987d46SWarner Losh return (0); 385ca987d46SWarner Losh } 386