/*-
* Copyright (c) 2010 Pawel Jakub Dawidek <pjd@FreeBSD.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
__FBSDID("$FreeBSD$");
#ifndef LITTLE_ENDIAN
#endif
#include "crc32.h"
#include "drv.h"
#include "util.h"
#include "gpt.h"
/*
* Buffer below 64kB passed on gptread(), which can hold at least
* one sector of data (512 bytes).
*/
static char *secbuf;
static void
{
/*
* We need to update the following for both primary and backup GPT:
* 1. Sector on disk that contains current partition.
* 2. Partition table checksum.
* 3. Header checksum.
* 4. Header on disk.
*/
printf("%s: unable to update %s GPT partition table\n",
return;
}
hdr->hdr_crc_self = 0;
return;
}
}
int
{
int firsttry;
if (part >= 0) {
return (-1);
}
printf("%s: specified partition is not UFS\n",
BOOTPROG);
return (-1);
}
goto found;
}
curent++;
return (-1);
}
if (bootonce) {
/*
* First look for partition with both GPT_ENT_ATTR_BOOTME and
* GPT_ENT_ATTR_BOOTONCE flags.
*/
continue;
continue;
continue;
/* Ok, found one. */
goto found;
}
bootonce = 0;
curent = 0;
}
continue;
continue;
continue;
/* Ok, found one. */
goto found;
}
if (firsttry) {
/*
* No partition with BOOTME flag was found, try to boot from
* first UFS partition.
*/
continue;
/* Ok, found one. */
goto found;
}
}
return (-1);
/*
* Clear BOOTME, but leave BOOTONCE set before trying to
* boot from this partition.
*/
if (hdr_primary_lba > 0) {
}
if (hdr_backup_lba > 0) {
}
}
return (0);
}
static int
{
return (-1);
}
return (-1);
}
hdr->hdr_crc_self = 0;
which);
return (-1);
}
return (0);
}
void
{
return;
if (hdr_primary_lba > 0) {
}
if (hdr_backup_lba > 0) {
}
}
static void
{
table_updated = 0;
sector_updated = 0;
continue;
}
table_updated = 1;
sector_updated = 1;
}
if (!sector_updated)
continue;
printf("%s: unable to update %s GPT partition table\n",
}
}
if (!table_updated)
return;
hdr->hdr_crc_self = 0;
}
static int
{
int entries_per_sec;
if (hdr->hdr_entries == 0)
return (0);
nent = 0;
for (;;) {
printf("%s: unable to read %s GPT partition table\n",
return (-1);
}
break;
}
break;
slba++;
}
return (-1);
}
return (0);
}
int
{
/*
* Read and verify both GPT headers: primary and backup.
*/
hdr_primary_lba = hdr_backup_lba = 0;
curent = -1;
bootonce = 1;
table_primary) == 0) {
gpthdr = &hdr_primary;
}
if (hdr_primary_lba > 0) {
/*
* If primary header is valid, we can get backup
* header location from there.
*/
} else {
if (altlba > 0)
altlba--;
}
if (altlba == 0)
table_backup) == 0) {
if (hdr_primary_lba == 0) {
gpthdr = &hdr_backup;
}
}
/*
* Convert all BOOTONCE without BOOTME flags into BOOTFAILED.
* BOOTONCE without BOOTME means that we tried to boot from it,
* but failed after leaving gptboot and machine was rebooted.
* We don't want to leave partitions marked as BOOTONCE only,
* because when we boot successfully start-up scripts should
* find at most one partition with only BOOTONCE flag and this
* will mean that we booted from that partition.
*/
if (hdr_primary_lba != 0)
if (hdr_backup_lba != 0)
if (hdr_primary_lba == 0 && hdr_backup_lba == 0)
return (-1);
return (0);
}