Reformat cgptlib to kernel style

No code changes, just reformatting.

BUG=none
BRANCH=none
TEST=make runtests

Change-Id: Ib8748df93c64395c88e1f789805393fcfe3ac419
Signed-off-by: Randall Spangler <rspangler@chromium.org>
Reviewed-on: https://gerrit.chromium.org/gerrit/42397
Reviewed-by: Bill Richardson <wfrichar@chromium.org>
This commit is contained in:
Randall Spangler
2013-01-30 12:52:02 -08:00
committed by ChromeBot
parent 7993f257af
commit cefe12c105
6 changed files with 832 additions and 752 deletions

View File

@@ -9,20 +9,20 @@
#include "sysincludes.h"
enum {
GPT_SUCCESS = 0,
GPT_ERROR_NO_VALID_KERNEL,
GPT_ERROR_INVALID_HEADERS,
GPT_ERROR_INVALID_ENTRIES,
GPT_ERROR_INVALID_SECTOR_SIZE,
GPT_ERROR_INVALID_SECTOR_NUMBER,
GPT_ERROR_INVALID_UPDATE_TYPE,
GPT_ERROR_CRC_CORRUPTED,
GPT_ERROR_OUT_OF_REGION,
GPT_ERROR_START_LBA_OVERLAP,
GPT_ERROR_END_LBA_OVERLAP,
GPT_ERROR_DUP_GUID,
/* Number of errors */
GPT_ERROR_COUNT
GPT_SUCCESS = 0,
GPT_ERROR_NO_VALID_KERNEL,
GPT_ERROR_INVALID_HEADERS,
GPT_ERROR_INVALID_ENTRIES,
GPT_ERROR_INVALID_SECTOR_SIZE,
GPT_ERROR_INVALID_SECTOR_NUMBER,
GPT_ERROR_INVALID_UPDATE_TYPE,
GPT_ERROR_CRC_CORRUPTED,
GPT_ERROR_OUT_OF_REGION,
GPT_ERROR_START_LBA_OVERLAP,
GPT_ERROR_END_LBA_OVERLAP,
GPT_ERROR_DUP_GUID,
/* Number of errors */
GPT_ERROR_COUNT
};
/* Bit masks for GptData.modified field. */
@@ -31,56 +31,64 @@ enum {
#define GPT_MODIFIED_ENTRIES1 0x04
#define GPT_MODIFIED_ENTRIES2 0x08
#define TOTAL_ENTRIES_SIZE 16384 /* Size of GptData.primary_entries
* and secondary_entries: 128
* bytes/entry * 128 entries. */
/*
* Size of GptData.primary_entries and secondary_entries: 128 bytes/entry * 128
* entries.
*/
#define TOTAL_ENTRIES_SIZE 16384
/* The 'update_type' of GptUpdateKernelEntry()
* We expose TRY and BAD only because those are what verified boot needs.
* For more precise control on GPT attribute bits, please refer to
* gpt_internal.h */
/*
* The 'update_type' of GptUpdateKernelEntry(). We expose TRY and BAD only
* because those are what verified boot needs. For more precise control on GPT
* attribute bits, please refer to gpt_internal.h.
*/
enum {
GPT_UPDATE_ENTRY_TRY = 1,
/* System will be trying to boot the currently selected kernel partition.
* Update its try count if necessary. */
GPT_UPDATE_ENTRY_BAD = 2,
/* The currently selected kernel partition failed validation. Mark entry as
* invalid. */
/*
* System will be trying to boot the currently selected kernel
* partition. Update its try count if necessary.
*/
GPT_UPDATE_ENTRY_TRY = 1,
/*
* The currently selected kernel partition failed validation. Mark
* entry as invalid.
*/
GPT_UPDATE_ENTRY_BAD = 2,
};
typedef struct {
/* Fill in the following fields before calling GptInit() */
uint8_t *primary_header; /* GPT primary header, from sector 1 of disk
* (size: 512 bytes) */
uint8_t *secondary_header; /* GPT secondary header, from last sector of
* disk (size: 512 bytes) */
uint8_t *primary_entries; /* primary GPT table, follows primary header
* (size: 16 KB) */
uint8_t *secondary_entries; /* secondary GPT table, precedes secondary
* header (size: 16 KB) */
uint32_t sector_bytes; /* Size of a LBA sector, in bytes */
uint64_t drive_sectors; /* Size of drive in LBA sectors, in sectors */
/* Fill in the following fields before calling GptInit() */
/* GPT primary header, from sector 1 of disk (size: 512 bytes) */
uint8_t *primary_header;
/* GPT secondary header, from last sector of disk (size: 512 bytes) */
uint8_t *secondary_header;
/* Primary GPT table, follows primary header (size: 16 KB) */
uint8_t *primary_entries;
/* Secondary GPT table, precedes secondary header (size: 16 KB) */
uint8_t *secondary_entries;
/* Size of a LBA sector, in bytes */
uint32_t sector_bytes;
/* Size of drive in LBA sectors, in sectors */
uint64_t drive_sectors;
/* Outputs */
uint8_t modified; /* Which inputs have been modified?
* 0x01 = header1
* 0x02 = header2
* 0x04 = table1
* 0x08 = table2 */
int current_kernel; /* the current chromeos kernel index in partition table.
* -1 means not found on drive. Note that GPT partition
* numbers are traditionally 1-based, but we're using
* a zero-based index here.
*/
/* Outputs */
/* Which inputs have been modified? GPT_MODIFIED_* */
uint8_t modified;
/*
* The current chromeos kernel index in partition table. -1 means not
* found on drive. Note that GPT partition numbers are traditionally
* 1-based, but we're using a zero-based index here.
*/
int current_kernel;
/* Internal variables */
uint32_t valid_headers, valid_entries;
int current_priority;
/* Internal variables */
uint32_t valid_headers, valid_entries;
int current_priority;
} GptData;
int GptInit(GptData* gpt);
/* Initializes the GPT data structure's internal state. The following fields
* must be filled before calling this function:
/**
* Initializes the GPT data structure's internal state.
*
* The following fields must be filled before calling this function:
*
* primary_header
* secondary_header
@@ -100,19 +108,23 @@ int GptInit(GptData* gpt);
* GPT_ERROR_INVALID_SECTOR_SIZE, size of a sector is not supported,
* GPT_ERROR_INVALID_SECTOR_NUMBER, number of sectors in drive is invalid (too
* small) */
int GptInit(GptData *gpt);
int GptNextKernelEntry(GptData* gpt, uint64_t* start_sector, uint64_t* size);
/* Provides the location of the next kernel partition, in order of decreasing
* priority. On return the start_sector parameter contains the LBA sector
* for the start of the kernel partition, and the size parameter contains the
* size of the kernel partition in LBA sectors. gpt.current_kernel contains
* the partition index of the current chromeos kernel partition.
/**
* Provides the location of the next kernel partition, in order of decreasing
* priority.
*
* On return the start_sector parameter contains the LBA sector for the start
* of the kernel partition, and the size parameter contains the size of the
* kernel partition in LBA sectors. gpt.current_kernel contains the partition
* index of the current chromeos kernel partition.
*
* Returns GPT_SUCCESS if successful, else
* GPT_ERROR_NO_VALID_KERNEL, no avaliable kernel, enters recovery mode */
int GptNextKernelEntry(GptData *gpt, uint64_t *start_sector, uint64_t *size);
int GptUpdateKernelEntry(GptData* gpt, uint32_t update_type);
/* Updates the kernel entry with the specified index, using the specified type
/**
* Updates the kernel entry with the specified index, using the specified type
* of update (GPT_UPDATE_ENTRY_*).
*
* On return the modified field may be set, if the GPT data has been modified
@@ -121,5 +133,6 @@ int GptUpdateKernelEntry(GptData* gpt, uint32_t update_type);
* Returns GPT_SUCCESS if successful, else
* GPT_ERROR_INVALID_UPDATE_TYPE, invalid 'update_type' is given.
*/
int GptUpdateKernelEntry(GptData *gpt, uint32_t update_type);
#endif /* VBOOT_REFERENCE_CGPTLIB_H_ */

View File

@@ -10,13 +10,15 @@
#include "cgptlib.h"
#include "gpt.h"
/* If gpt->current_kernel is this value, means either:
/*
* If gpt->current_kernel is this value, means either:
* 1. an initial value before scanning GPT entries,
* 2. after scanning, no any valid kernel is found.
*/
#define CGPT_KERNEL_ENTRY_NOT_FOUND (-1)
/* Bit definitions and masks for GPT attributes.
/*
* Bit definitions and masks for GPT attributes.
*
* 63-61 -- (reserved)
* 60 -- read-only
@@ -55,80 +57,107 @@
/* Defines GPT sizes */
#define GPT_PMBR_SECTOR 1 /* size (in sectors) of PMBR */
#define GPT_HEADER_SECTOR 1
#define GPT_ENTRIES_SECTORS 32 /* assume sector size if 512 bytes, then
* (TOTAL_ENTRIES_SIZE / 512) = 32 */
/*
* Entries sectors assumes sector size if 512 bytes; then (TOTAL_ENTRIES_SIZE /
* 512) = 32
*/
#define GPT_ENTRIES_SECTORS 32
/* alias name of index in internal array for primary and secondary header and
* entries. */
/*
* Alias name of index in internal array for primary and secondary header and
* entries.
*/
enum {
/* constants for index */
PRIMARY = 0,
SECONDARY = 1,
ANY_VALID = 9999, /* accept any between primary and secondary */
/* constants for index */
PRIMARY = 0,
SECONDARY = 1,
ANY_VALID = 9999, /* accept any between primary and secondary */
/* constants for bit mask */
MASK_NONE = 0,
MASK_PRIMARY = 1,
MASK_SECONDARY = 2,
MASK_BOTH = 3,
/* constants for bit mask */
MASK_NONE = 0,
MASK_PRIMARY = 1,
MASK_SECONDARY = 2,
MASK_BOTH = 3,
};
/* Verify GptData parameters are sane. */
/**
* Verify GptData parameters are sane.
*/
int CheckParameters(GptData* gpt);
/* Check header fields.
/**
* Check header fields.
*
* Returns 0 if header is valid, 1 if invalid. */
int CheckHeader(GptHeader* h, int is_secondary, uint64_t drive_sectors);
* Returns 0 if header is valid, 1 if invalid.
*/
int CheckHeader(GptHeader *h, int is_secondary, uint64_t drive_sectors);
/* Calculate and return the header CRC. */
uint32_t HeaderCrc(GptHeader* h);
/**
* Calculate and return the header CRC.
*/
uint32_t HeaderCrc(GptHeader *h);
/* Check entries.
/**
* Check entries.
*
* Returns 0 if entries are valid, 1 if invalid. */
int CheckEntries(GptEntry* entries, GptHeader* h);
* Returns 0 if entries are valid, 1 if invalid.
*/
int CheckEntries(GptEntry *entries, GptHeader *h);
/* Return 0 if the GptHeaders are the same for all fields which don't
* differ between the primary and secondary headers - that is, all
* fields other than:
/**
* Return 0 if the GptHeaders are the same for all fields which don't differ
* between the primary and secondary headers - that is, all fields other than:
*
* my_lba
* alternate_lba
* entries_lba */
* entries_lba
*/
int HeaderFieldsSame(GptHeader *h1, GptHeader *h2);
/* Check GptData, headers, entries.
/**
* Check GptData, headers, entries.
*
* If successful, sets gpt->valid_headers and gpt->valid_entries and returns
* GPT_SUCCESS.
*
* On error, returns a GPT_ERROR_* return code. */
int GptSanityCheck(GptData* gpt);
* On error, returns a GPT_ERROR_* return code.
*/
int GptSanityCheck(GptData *gpt);
/* Repairs GPT data by copying from one set of valid headers/entries to the
/**
* Repair GPT data by copying from one set of valid headers/entries to the
* other. Assumes GptSanityCheck() has been run to determine which headers
* and/or entries are already valid. */
void GptRepair(GptData* gpt);
* and/or entries are already valid.
*/
void GptRepair(GptData *gpt);
/* Getters and setters for partition attribute fields. */
int GetEntrySuccessful(const GptEntry* e);
int GetEntryPriority(const GptEntry* e);
int GetEntryTries(const GptEntry* e);
void SetEntrySuccessful(GptEntry* e, int successful);
void SetEntryPriority(GptEntry* e, int priority);
void SetEntryTries(GptEntry* e, int tries);
/* Return 1 if the entry is unused, 0 if it is used. */
int IsUnusedEntry(const GptEntry* e);
int GetEntrySuccessful(const GptEntry *e);
int GetEntryPriority(const GptEntry *e);
int GetEntryTries(const GptEntry *e);
void SetEntrySuccessful(GptEntry *e, int successful);
void SetEntryPriority(GptEntry *e, int priority);
void SetEntryTries(GptEntry *e, int tries);
/* Returns 1 if the entry is a Chrome OS kernel partition, else 0. */
int IsKernelEntry(const GptEntry* e);
/**
* Return 1 if the entry is unused, 0 if it is used.
*/
int IsUnusedEntry(const GptEntry *e);
/* Copies the current kernel partition's UniquePartitionGuid to the dest */
/**
* Return 1 if the entry is a Chrome OS kernel partition, else 0.
*/
int IsKernelEntry(const GptEntry *e);
/**
* Copy the current kernel partition's UniquePartitionGuid to the dest.
*/
void GetCurrentKernelUniqueGuid(GptData *gpt, void *dest);
/* Returns a pointer to text describing the passed in error */
const char* GptErrorText(int error_code);
/**
* Return a pointer to text describing the passed in error.
*/
const char *GptErrorText(int error_code);
#endif /* VBOOT_REFERENCE_CGPTLIB_INTERNAL_H_ */

View File

@@ -1,4 +1,4 @@
/* Copyright (c) 2010-2011 The Chromium OS Authors. All rights reserved.
/* Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*
@@ -19,97 +19,97 @@ __pragma(pack(push,1)) /* Support packing for MSVC. */
#define GPT_HEADER_SIGNATURE_SIZE sizeof(GPT_HEADER_SIGNATURE)
#define GPT_HEADER_REVISION 0x00010000
/* The first 3 numbers should be stored in network-endian format
* according to the GUID RFC. The UEFI spec appendix A claims they
* should be stored in little-endian format. But they need to be
* _displayed_ in network-endian format, which is also how they're
* documented in the specs.
/*
* The first 3 numbers should be stored in network-endian format according to
* the GUID RFC. The UEFI spec appendix A claims they should be stored in
* little-endian format. But they need to be _displayed_ in network-endian
* format, which is also how they're documented in the specs.
*
* Since what we have here are little-endian constants, they're
* byte-swapped from the normal display order. */
* Since what we have here are little-endian constants, they're byte-swapped
* from the normal display order.
*/
#define GPT_ENT_TYPE_UNUSED \
{{{0x00000000,0x0000,0x0000,0x00,0x00,{0x00,0x00,0x00,0x00,0x00,0x00}}}}
{{{0x00000000,0x0000,0x0000,0x00,0x00,{0x00,0x00,0x00,0x00,0x00,0x00}}}}
#define GPT_ENT_TYPE_EFI \
{{{0xc12a7328,0xf81f,0x11d2,0xba,0x4b,{0x00,0xa0,0xc9,0x3e,0xc9,0x3b}}}}
{{{0xc12a7328,0xf81f,0x11d2,0xba,0x4b,{0x00,0xa0,0xc9,0x3e,0xc9,0x3b}}}}
#define GPT_ENT_TYPE_CHROMEOS_FIRMWARE \
{{{0xcab6e88e,0xabf3,0x4102,0xa0,0x7a,{0xd4,0xbb,0x9b,0xe3,0xc1,0xd3}}}}
{{{0xcab6e88e,0xabf3,0x4102,0xa0,0x7a,{0xd4,0xbb,0x9b,0xe3,0xc1,0xd3}}}}
#define GPT_ENT_TYPE_CHROMEOS_KERNEL \
{{{0xfe3a2a5d,0x4f32,0x41a7,0xb7,0x25,{0xac,0xcc,0x32,0x85,0xa3,0x09}}}}
{{{0xfe3a2a5d,0x4f32,0x41a7,0xb7,0x25,{0xac,0xcc,0x32,0x85,0xa3,0x09}}}}
#define GPT_ENT_TYPE_CHROMEOS_ROOTFS \
{{{0x3cb8e202,0x3b7e,0x47dd,0x8a,0x3c,{0x7f,0xf2,0xa1,0x3c,0xfc,0xec}}}}
{{{0x3cb8e202,0x3b7e,0x47dd,0x8a,0x3c,{0x7f,0xf2,0xa1,0x3c,0xfc,0xec}}}}
#define GPT_ENT_TYPE_CHROMEOS_RESERVED \
{{{0x2e0a753d,0x9e48,0x43b0,0x83,0x37,{0xb1,0x51,0x92,0xcb,0x1b,0x5e}}}}
{{{0x2e0a753d,0x9e48,0x43b0,0x83,0x37,{0xb1,0x51,0x92,0xcb,0x1b,0x5e}}}}
#define GPT_ENT_TYPE_LINUX_DATA \
{{{0xebd0a0a2,0xb9e5,0x4433,0x87,0xc0,{0x68,0xb6,0xb7,0x26,0x99,0xc7}}}}
{{{0xebd0a0a2,0xb9e5,0x4433,0x87,0xc0,{0x68,0xb6,0xb7,0x26,0x99,0xc7}}}}
#define UUID_NODE_LEN 6
#define GUID_SIZE 16
/* GUID definition.
* Defined in appendix A of EFI standard.
*/
/* GUID definition. Defined in appendix A of EFI standard. */
typedef struct {
union {
struct {
uint32_t time_low;
uint16_t time_mid;
uint16_t time_high_and_version;
uint8_t clock_seq_high_and_reserved;
uint8_t clock_seq_low;
uint8_t node[UUID_NODE_LEN];
} Uuid;
uint8_t raw[GUID_SIZE];
} u;
union {
struct {
uint32_t time_low;
uint16_t time_mid;
uint16_t time_high_and_version;
uint8_t clock_seq_high_and_reserved;
uint8_t clock_seq_low;
uint8_t node[UUID_NODE_LEN];
} Uuid;
uint8_t raw[GUID_SIZE];
} u;
} __attribute__((packed)) Guid;
#define GUID_EXPECTED_SIZE GUID_SIZE
/* GPT header defines how many partitions exist on a drive and sectors managed.
* For every drive device, there are 2 headers, primary and secondary.
* Most of fields are duplicated except my_lba and entries_lba.
/*
* GPT header defines how many partitions exist on a drive and sectors managed.
* For every drive device, there are 2 headers, primary and secondary. Most of
* fields are duplicated except my_lba and entries_lba.
*
* You may find more details in chapter 5 of EFI standard.
*/
typedef struct {
char signature[8];
uint32_t revision;
uint32_t size;
uint32_t header_crc32;
uint32_t reserved_zero;
uint64_t my_lba;
uint64_t alternate_lba;
uint64_t first_usable_lba;
uint64_t last_usable_lba;
Guid disk_uuid;
uint64_t entries_lba;
uint32_t number_of_entries;
uint32_t size_of_entry;
uint32_t entries_crc32;
/* Remainder of sector is reserved and should be 0 */
char signature[8];
uint32_t revision;
uint32_t size;
uint32_t header_crc32;
uint32_t reserved_zero;
uint64_t my_lba;
uint64_t alternate_lba;
uint64_t first_usable_lba;
uint64_t last_usable_lba;
Guid disk_uuid;
uint64_t entries_lba;
uint32_t number_of_entries;
uint32_t size_of_entry;
uint32_t entries_crc32;
/* Remainder of sector is reserved and should be 0 */
} __attribute__((packed)) GptHeader;
#define GPTHEADER_EXPECTED_SIZE 92
/* GPT partition entry defines the starting and ending LBAs of a partition.
* It also contains the unique GUID, type, and attribute bits.
/*
* GPT partition entry defines the starting and ending LBAs of a partition. It
* also contains the unique GUID, type, and attribute bits.
*
* You may find more details in chapter 5 of EFI standard.
*/
typedef struct {
Guid type;
Guid unique;
uint64_t starting_lba;
uint64_t ending_lba;
union {
struct {
uint16_t reserved[3];
uint16_t gpt_att;
} __attribute__((packed)) fields;
uint64_t whole;
} attrs;
uint16_t name[36]; /* UTF-16 encoded partition name */
/* Remainder of entry is reserved and should be 0 */
Guid type;
Guid unique;
uint64_t starting_lba;
uint64_t ending_lba;
union {
struct {
uint16_t reserved[3];
uint16_t gpt_att;
} __attribute__((packed)) fields;
uint64_t whole;
} attrs;
uint16_t name[36]; /* UTF-16 encoded partition name */
/* Remainder of entry is reserved and should be 0 */
} __attribute__((packed)) GptEntry;
#define GPTENTRY_EXPECTED_SIZE 128