17c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg/* Copyright (c) 2013 The Chromium OS Authors. All rights reserved. 27c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg * Use of this source code is governed by a BSD-style license that can be 37c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg * found in the LICENSE file. 47c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg */ 57c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg 67c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg#ifndef VBOOT_REFERENCE_CGPT_MISC_H_ 77c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg#define VBOOT_REFERENCE_CGPT_MISC_H_ 87c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg 932a999d2c0e5bf8a1c0f6141d3db77a1dcc6f5afDan Ehrenberg#include "gpt.h" 107c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg#include "vboot_api.h" 117c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg 127c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenbergenum { 137c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg GPT_SUCCESS = 0, 147c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg GPT_ERROR_NO_VALID_KERNEL, 157c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg GPT_ERROR_INVALID_HEADERS, 167c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg GPT_ERROR_INVALID_ENTRIES, 177c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg GPT_ERROR_INVALID_SECTOR_SIZE, 187c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg GPT_ERROR_INVALID_SECTOR_NUMBER, 197c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg GPT_ERROR_INVALID_UPDATE_TYPE, 207c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg GPT_ERROR_CRC_CORRUPTED, 217c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg GPT_ERROR_OUT_OF_REGION, 227c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg GPT_ERROR_START_LBA_OVERLAP, 237c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg GPT_ERROR_END_LBA_OVERLAP, 247c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg GPT_ERROR_DUP_GUID, 257c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg GPT_ERROR_INVALID_FLASH_GEOMETRY, 267c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg GPT_ERROR_NO_SUCH_ENTRY, 277c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg /* Number of errors */ 287c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg GPT_ERROR_COUNT 297c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg}; 307c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg 317c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg/* Bit masks for GptData.modified field. */ 327c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg#define GPT_MODIFIED_HEADER1 0x01 337c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg#define GPT_MODIFIED_HEADER2 0x02 347c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg#define GPT_MODIFIED_ENTRIES1 0x04 357c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg#define GPT_MODIFIED_ENTRIES2 0x08 367c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg 377c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg/* 387c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg * The 'update_type' of GptUpdateKernelEntry(). We expose TRY and BAD only 397c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg * because those are what verified boot needs. For more precise control on GPT 407c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg * attribute bits, please refer to gpt_internal.h. 417c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg */ 427c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenbergenum { 437c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg /* 447c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg * System will be trying to boot the currently selected kernel 457c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg * partition. Update its try count if necessary. 467c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg */ 477c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg GPT_UPDATE_ENTRY_TRY = 1, 487c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg /* 497c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg * The currently selected kernel partition failed validation. Mark 507c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg * entry as invalid. 517c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg */ 527c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg GPT_UPDATE_ENTRY_BAD = 2, 53f620c0d6560dcb5c6b6b004bd50f4713f191d381Furquan Shaikh /* 54f620c0d6560dcb5c6b6b004bd50f4713f191d381Furquan Shaikh * Used for fastboot mode. When an image is written to kernel partition, 55f620c0d6560dcb5c6b6b004bd50f4713f191d381Furquan Shaikh * its GPT entry is marked with S1,P1,T15. 56f620c0d6560dcb5c6b6b004bd50f4713f191d381Furquan Shaikh */ 57f620c0d6560dcb5c6b6b004bd50f4713f191d381Furquan Shaikh GPT_UPDATE_ENTRY_RESET = 3, 58f620c0d6560dcb5c6b6b004bd50f4713f191d381Furquan Shaikh /* 59f620c0d6560dcb5c6b6b004bd50f4713f191d381Furquan Shaikh * Used for fastboot mode. When an image is written to kernel partition, 60f620c0d6560dcb5c6b6b004bd50f4713f191d381Furquan Shaikh * its GPT entry is marked with S0,P0,T0. 61f620c0d6560dcb5c6b6b004bd50f4713f191d381Furquan Shaikh */ 62f620c0d6560dcb5c6b6b004bd50f4713f191d381Furquan Shaikh GPT_UPDATE_ENTRY_INVALID = 4, 637c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg}; 647c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg 65b3d38f5c620da89662deb1a08971c5025d6c1132Dan Ehrenberg/* If this bit is 1, the GPT is stored in another from the streaming data */ 66b3d38f5c620da89662deb1a08971c5025d6c1132Dan Ehrenberg#define GPT_FLAG_EXTERNAL 0x1 676ee52d9a929d00e871e7316240b54f381146fbc6Nam T. Nguyen 686ee52d9a929d00e871e7316240b54f381146fbc6Nam T. Nguyen/* 696ee52d9a929d00e871e7316240b54f381146fbc6Nam T. Nguyen * A note about stored_on_device and gpt_drive_sectors: 706ee52d9a929d00e871e7316240b54f381146fbc6Nam T. Nguyen * 716ee52d9a929d00e871e7316240b54f381146fbc6Nam T. Nguyen * This code is used by both the "cgpt" utility and depthcharge/vboot. ATM, 726ee52d9a929d00e871e7316240b54f381146fbc6Nam T. Nguyen * depthcharge does not have logic to properly setup stored_on_device and 736ee52d9a929d00e871e7316240b54f381146fbc6Nam T. Nguyen * gpt_drive_sectors, but it does do a memset(gpt, 0, sizeof(GptData)). And so, 746ee52d9a929d00e871e7316240b54f381146fbc6Nam T. Nguyen * GPT_STORED_ON_DEVICE should be 0 to make stored_on_device compatible with 756ee52d9a929d00e871e7316240b54f381146fbc6Nam T. Nguyen * present behavior. At the same time, in vboot_kernel:LoadKernel(), and 766ee52d9a929d00e871e7316240b54f381146fbc6Nam T. Nguyen * cgpt_common:GptLoad(), we need to have simple shims to set gpt_drive_sectors 776ee52d9a929d00e871e7316240b54f381146fbc6Nam T. Nguyen * to drive_sectors. 786ee52d9a929d00e871e7316240b54f381146fbc6Nam T. Nguyen * 796ee52d9a929d00e871e7316240b54f381146fbc6Nam T. Nguyen * TODO(namnguyen): Remove those shims when the firmware can set these fields. 806ee52d9a929d00e871e7316240b54f381146fbc6Nam T. Nguyen */ 817c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenbergtypedef struct { 827c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg /* Fill in the following fields before calling GptInit() */ 837c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg /* GPT primary header, from sector 1 of disk (size: 512 bytes) */ 847c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg uint8_t *primary_header; 857c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg /* GPT secondary header, from last sector of disk (size: 512 bytes) */ 867c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg uint8_t *secondary_header; 877c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg /* Primary GPT table, follows primary header (size: 16 KB) */ 887c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg uint8_t *primary_entries; 897c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg /* Secondary GPT table, precedes secondary header (size: 16 KB) */ 907c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg uint8_t *secondary_entries; 917c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg /* Size of a LBA sector, in bytes */ 927c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg uint32_t sector_bytes; 936ee52d9a929d00e871e7316240b54f381146fbc6Nam T. Nguyen /* Size of drive (that the partitions are on) in LBA sectors */ 94b3d38f5c620da89662deb1a08971c5025d6c1132Dan Ehrenberg uint64_t streaming_drive_sectors; 956ee52d9a929d00e871e7316240b54f381146fbc6Nam T. Nguyen /* Size of the device that holds the GPT structures, 512-byte sectors */ 966ee52d9a929d00e871e7316240b54f381146fbc6Nam T. Nguyen uint64_t gpt_drive_sectors; 97b3d38f5c620da89662deb1a08971c5025d6c1132Dan Ehrenberg /* Flags */ 98b3d38f5c620da89662deb1a08971c5025d6c1132Dan Ehrenberg uint32_t flags; 997c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg 1007c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg /* Outputs */ 1017c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg /* Which inputs have been modified? GPT_MODIFIED_* */ 1027c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg uint8_t modified; 1037c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg /* 1047c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg * The current chromeos kernel index in partition table. -1 means not 1057c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg * found on drive. Note that GPT partition numbers are traditionally 1067c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg * 1-based, but we're using a zero-based index here. 1077c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg */ 1087c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg int current_kernel; 1097c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg 1107c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg /* Internal variables */ 1117c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg uint32_t valid_headers, valid_entries; 1127c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg int current_priority; 1137c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg} GptData; 1147c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg 1157c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg/** 1167c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg * Initializes the GPT data structure's internal state. 1177c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg * 1187c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg * The following fields must be filled before calling this function: 1197c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg * 1207c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg * primary_header 1217c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg * secondary_header 1227c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg * primary_entries 1237c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg * secondary_entries 1247c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg * sector_bytes 1257c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg * drive_sectors 1266ee52d9a929d00e871e7316240b54f381146fbc6Nam T. Nguyen * stored_on_device 1276ee52d9a929d00e871e7316240b54f381146fbc6Nam T. Nguyen * gpt_device_sectors 1287c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg * 1297c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg * On return the modified field may be set, if the GPT data has been modified 1307c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg * and should be written to disk. 1317c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg * 1327c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg * Returns GPT_SUCCESS if successful, non-zero if error: 1337c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg * GPT_ERROR_INVALID_HEADERS, both partition table headers are invalid, enters 1347c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg * recovery mode, 1357c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg * GPT_ERROR_INVALID_ENTRIES, both partition table entries are invalid, enters 1367c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg * recovery mode, 1377c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg * GPT_ERROR_INVALID_SECTOR_SIZE, size of a sector is not supported, 1387c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg * GPT_ERROR_INVALID_SECTOR_NUMBER, number of sectors in drive is invalid (too 1397c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg * small) */ 1407c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenbergint GptInit(GptData *gpt); 1417c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg 1427c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg/** 143f620c0d6560dcb5c6b6b004bd50f4713f191d381Furquan Shaikh * Return the nth instance of parition entry matching the partition type guid 144f620c0d6560dcb5c6b6b004bd50f4713f191d381Furquan Shaikh * from the gpt table. Instance value starts from 0. If the entry is not found, 145f620c0d6560dcb5c6b6b004bd50f4713f191d381Furquan Shaikh * it returns NULL. 146f620c0d6560dcb5c6b6b004bd50f4713f191d381Furquan Shaikh */ 147f620c0d6560dcb5c6b6b004bd50f4713f191d381Furquan ShaikhGptEntry *GptFindNthEntry(GptData *gpt, const Guid *guid, unsigned int n); 148f620c0d6560dcb5c6b6b004bd50f4713f191d381Furquan Shaikh 149f620c0d6560dcb5c6b6b004bd50f4713f191d381Furquan Shaikh/** 1507c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg * Allocate and read GPT data from the drive. The sector_bytes and 1517c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg * drive_sectors fields should be filled on input. The primary and secondary 1527c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg * header and entries are filled on output. 1537c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg * 1547c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg * Returns 0 if successful, 1 if error. 1557c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg */ 1567c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenbergint AllocAndReadGptData(VbExDiskHandle_t disk_handle, GptData *gptdata); 1577c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg 1587c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg/** 1597c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg * Write any changes for the GPT data back to the drive, then free the buffers. 1607c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg */ 1617c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenbergint WriteAndFreeGptData(VbExDiskHandle_t disk_handle, GptData *gptdata); 1627c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg 16332a999d2c0e5bf8a1c0f6141d3db77a1dcc6f5afDan Ehrenberg/** 16432a999d2c0e5bf8a1c0f6141d3db77a1dcc6f5afDan Ehrenberg * Return 1 if the entry is unused, 0 if it is used. 16532a999d2c0e5bf8a1c0f6141d3db77a1dcc6f5afDan Ehrenberg */ 16632a999d2c0e5bf8a1c0f6141d3db77a1dcc6f5afDan Ehrenbergint IsUnusedEntry(const GptEntry *e); 16732a999d2c0e5bf8a1c0f6141d3db77a1dcc6f5afDan Ehrenberg 168f620c0d6560dcb5c6b6b004bd50f4713f191d381Furquan Shaikh/** 169f620c0d6560dcb5c6b6b004bd50f4713f191d381Furquan Shaikh * Return size(in lba) of a partition represented by given GPT entry. 170f620c0d6560dcb5c6b6b004bd50f4713f191d381Furquan Shaikh */ 171f620c0d6560dcb5c6b6b004bd50f4713f191d381Furquan Shaikhsize_t GptGetEntrySizeLba(const GptEntry *e); 172f620c0d6560dcb5c6b6b004bd50f4713f191d381Furquan Shaikh 173f620c0d6560dcb5c6b6b004bd50f4713f191d381Furquan Shaikh/** 174f620c0d6560dcb5c6b6b004bd50f4713f191d381Furquan Shaikh * Return size(in bytes) of a partition represented by given GPT entry. 175f620c0d6560dcb5c6b6b004bd50f4713f191d381Furquan Shaikh */ 176f620c0d6560dcb5c6b6b004bd50f4713f191d381Furquan Shaikhsize_t GptGetEntrySizeBytes(const GptData *gpt, const GptEntry *e); 177f620c0d6560dcb5c6b6b004bd50f4713f191d381Furquan Shaikh 178f620c0d6560dcb5c6b6b004bd50f4713f191d381Furquan Shaikh/** 179f620c0d6560dcb5c6b6b004bd50f4713f191d381Furquan Shaikh * Updates the kernel entry with the specified index, using the specified type 180f620c0d6560dcb5c6b6b004bd50f4713f191d381Furquan Shaikh * of update (GPT_UPDATE_ENTRY_*). 181f620c0d6560dcb5c6b6b004bd50f4713f191d381Furquan Shaikh * 182f620c0d6560dcb5c6b6b004bd50f4713f191d381Furquan Shaikh * On return the modified field may be set, if the GPT data has been modified 183f620c0d6560dcb5c6b6b004bd50f4713f191d381Furquan Shaikh * and should be written to disk. 184f620c0d6560dcb5c6b6b004bd50f4713f191d381Furquan Shaikh * 185f620c0d6560dcb5c6b6b004bd50f4713f191d381Furquan Shaikh * Returns GPT_SUCCESS if successful, else 186f620c0d6560dcb5c6b6b004bd50f4713f191d381Furquan Shaikh * GPT_ERROR_INVALID_UPDATE_TYPE, invalid 'update_type' is given. 187f620c0d6560dcb5c6b6b004bd50f4713f191d381Furquan Shaikh */ 188f620c0d6560dcb5c6b6b004bd50f4713f191d381Furquan Shaikhint GptUpdateKernelWithEntry(GptData *gpt, GptEntry *e, uint32_t update_type); 189f620c0d6560dcb5c6b6b004bd50f4713f191d381Furquan Shaikh 190f620c0d6560dcb5c6b6b004bd50f4713f191d381Furquan Shaikh/** 191f620c0d6560dcb5c6b6b004bd50f4713f191d381Furquan Shaikh * Updates the kernel entry identified by current_kernel field. If 192f620c0d6560dcb5c6b6b004bd50f4713f191d381Furquan Shaikh * current_kernel is not set it returns an error. 193f620c0d6560dcb5c6b6b004bd50f4713f191d381Furquan Shaikh * 194f620c0d6560dcb5c6b6b004bd50f4713f191d381Furquan Shaikh * Returns GPT_SUCCESS if successful, else 195f620c0d6560dcb5c6b6b004bd50f4713f191d381Furquan Shaikh * GPT_ERROR_INVALID_UPDATE_TYPE, invalid 'update_type' is given. 196f620c0d6560dcb5c6b6b004bd50f4713f191d381Furquan Shaikh */ 197f620c0d6560dcb5c6b6b004bd50f4713f191d381Furquan Shaikhint GptUpdateKernelEntry(GptData *gpt, uint32_t update_type); 198f620c0d6560dcb5c6b6b004bd50f4713f191d381Furquan Shaikh 1997c2beb08380410ca6847abdac23e11ded2d1b625Dan Ehrenberg#endif /* VBOOT_REFERENCE_CGPT_MISC_H_ */ 200