vboot_nvstorage.h revision a2326ee152ab5b8aee924ccf794cee38d54909bd
1/* Copyright (c) 2012 The Chromium OS Authors. All rights reserved. 2 * Use of this source code is governed by a BSD-style license that can be 3 * found in the LICENSE file. 4 */ 5 6/* Non-volatile storage routines for verified boot. 7 */ 8 9#ifndef VBOOT_REFERENCE_NVSTORAGE_H_ 10#define VBOOT_REFERENCE_NVSTORAGE_H_ 11 12#define VBNV_BLOCK_SIZE 16 /* Size of NV storage block in bytes */ 13 14typedef struct VbNvContext { 15 /* Raw NV data. Caller must fill this before calling VbNvSetup(). */ 16 uint8_t raw[VBNV_BLOCK_SIZE]; 17 /* Flag indicating whether raw data has changed. Set by VbNvTeardown() if 18 * the raw data has changed and needs to be stored to the underlying 19 * non-volatile data store. */ 20 int raw_changed; 21 22 /* Internal data for NV storage routines. Caller should not touch 23 * these fields. */ 24 int regenerate_crc; 25 26} VbNvContext; 27 28 29/* Parameter type for VbNvGet(), VbNvSet(). */ 30typedef enum VbNvParam { 31 /* Parameter values have been reset to defaults (flag for firmware). 32 * 0=clear; 1=set. */ 33 VBNV_FIRMWARE_SETTINGS_RESET = 0, 34 /* Parameter values have been reset to defaults (flag for kernel). 35 * 0=clear; 1=set. */ 36 VBNV_KERNEL_SETTINGS_RESET, 37 /* Request debug reset on next S3->S0 transition. 0=clear; 1=set. */ 38 VBNV_DEBUG_RESET_MODE, 39 /* Number of times to try booting RW firmware slot B before slot A. 40 * Valid range: 0-15. */ 41 VBNV_TRY_B_COUNT, 42 /* Request recovery mode on next boot; see VBNB_RECOVERY_* below for 43 * currently defined reason codes. 8-bit value. */ 44 VBNV_RECOVERY_REQUEST, 45 /* Localization index for screen bitmaps displayed by firmware. 46 * 8-bit value. */ 47 VBNV_LOCALIZATION_INDEX, 48 /* Field reserved for kernel/user-mode use; 32-bit value. */ 49 VBNV_KERNEL_FIELD, 50 /* Allow booting from USB in developer mode. 0=no, 1=yes. */ 51 VBNV_DEV_BOOT_USB, 52 /* Allow booting of legacy OSes in developer mode. 0=no, 1=yes. */ 53 VBNV_DEV_BOOT_LEGACY, 54 /* Only boot Google-signed images in developer mode. 0=no, 1=yes. */ 55 VBNV_DEV_BOOT_SIGNED_ONLY, 56 /* Set by userspace to request that RO firmware disable dev-mode on the next 57 * boot. This is likely only possible if the dev-switch is virtual. */ 58 VBNV_DISABLE_DEV_REQUEST, 59 /* Set and cleared by vboot to request that the video Option ROM be loaded at 60 * boot time, so that BIOS screens can be displayed. 0=no, 1=yes. */ 61 VBNV_OPROM_NEEDED, 62 /* Request that the firmware clear the TPM owner on the next boot. */ 63 VBNV_CLEAR_TPM_OWNER_REQUEST, 64 /* Flag that TPM owner was cleared on request. */ 65 VBNV_CLEAR_TPM_OWNER_DONE, 66} VbNvParam; 67 68 69/* Recovery reason codes for VBNV_RECOVERY_REQUEST */ 70/* Recovery not requested. */ 71#define VBNV_RECOVERY_NOT_REQUESTED 0x00 72/* Recovery requested from legacy utility. (Prior to the NV storage 73 * spec, recovery mode was a single bitfield; this value is reserved 74 * so that scripts which wrote 1 to the recovery field are 75 * distinguishable from scripts whch use the recovery reasons listed 76 * here. */ 77#define VBNV_RECOVERY_LEGACY 0x01 78/* User manually requested recovery via recovery button */ 79#define VBNV_RECOVERY_RO_MANUAL 0x02 80/* RW firmware failed signature check (neither RW firmware slot was valid) */ 81#define VBNV_RECOVERY_RO_INVALID_RW 0x03 82/* S3 resume failed */ 83#define VBNV_RECOVERY_RO_S3_RESUME 0x04 84/* TPM error in read-only firmware */ 85#define VBNV_RECOVERY_RO_TPM_ERROR 0x05 86/* Shared data error in read-only firmware */ 87#define VBNV_RECOVERY_RO_SHARED_DATA 0x06 88/* Test error from S3Resume() */ 89#define VBNV_RECOVERY_RO_TEST_S3 0x07 90/* Test error from LoadFirmwareSetup() */ 91#define VBNV_RECOVERY_RO_TEST_LFS 0x08 92/* Test error from LoadFirmware() */ 93#define VBNV_RECOVERY_RO_TEST_LF 0x09 94/* RW firmware failed signature check (neither RW firmware slot was valid). 95 * Recovery reason is VBNV_RECOVERY_RO_INVALID_RW_CHECK_MIN + the check value 96 * for the slot which came closest to validating; see VBSD_LF_CHECK_* in 97 * vboot_struct.h. */ 98#define VBNV_RECOVERY_RO_INVALID_RW_CHECK_MIN 0x10 99#define VBNV_RECOVERY_RO_INVALID_RW_CHECK_MAX 0x1F 100/* Firmware boot failure outside of verified boot (RAM init, missing SSD, 101 * etc.). */ 102#define VBNV_RECOVERY_RO_FIRMWARE 0x20 103/* Recovery mode TPM initialization requires a system reboot. The system was 104 * already in recovery mode for some other reason when this happened. */ 105#define VBNV_RECOVERY_RO_TPM_REBOOT 0x21 106/* EC software sync - other error */ 107#define VBNV_RECOVERY_EC_SOFTWARE_SYNC 0x22 108/* EC software sync - unable to determine active EC image */ 109#define VBNV_RECOVERY_EC_UNKNOWN_IMAGE 0x23 110/* EC software sync - error obtaining EC image hash */ 111#define VBNV_RECOVERY_EC_HASH 0x24 112/* EC software sync - error obtaining expected EC image */ 113#define VBNV_RECOVERY_EC_EXPECTED_IMAGE 0x25 114/* EC software sync - error updating EC */ 115#define VBNV_RECOVERY_EC_UPDATE 0x26 116/* EC software sync - unable to jump to EC-RW */ 117#define VBNV_RECOVERY_EC_JUMP_RW 0x27 118/* EC software sync - unable to protect / unprotect EC-RW */ 119#define VBNV_RECOVERY_EC_PROTECT 0x28 120/* Unspecified/unknown error in read-only firmware */ 121#define VBNV_RECOVERY_RO_UNSPECIFIED 0x3F 122/* User manually requested recovery by pressing a key at developer 123 * warning screen */ 124#define VBNV_RECOVERY_RW_DEV_SCREEN 0x41 125/* No OS kernel detected */ 126#define VBNV_RECOVERY_RW_NO_OS 0x42 127/* OS kernel failed signature check */ 128#define VBNV_RECOVERY_RW_INVALID_OS 0x43 129/* TPM error in rewritable firmware */ 130#define VBNV_RECOVERY_RW_TPM_ERROR 0x44 131/* RW firmware in dev mode, but dev switch is off */ 132#define VBNV_RECOVERY_RW_DEV_MISMATCH 0x45 133/* Shared data error in rewritable firmware */ 134#define VBNV_RECOVERY_RW_SHARED_DATA 0x46 135/* Test error from LoadKernel() */ 136#define VBNV_RECOVERY_RW_TEST_LK 0x47 137/* No bootable disk found */ 138#define VBNV_RECOVERY_RW_NO_DISK 0x48 139/* Unspecified/unknown error in rewritable firmware */ 140#define VBNV_RECOVERY_RW_UNSPECIFIED 0x7F 141/* DM-verity error */ 142#define VBNV_RECOVERY_KE_DM_VERITY 0x81 143/* Unspecified/unknown error in kernel */ 144#define VBNV_RECOVERY_KE_UNSPECIFIED 0xBF 145/* Recovery mode test from user-mode */ 146#define VBNV_RECOVERY_US_TEST 0xC1 147/* Unspecified/unknown error in user-mode */ 148#define VBNV_RECOVERY_US_UNSPECIFIED 0xFF 149 150 151/* Initialize the NV storage library. This must be called before any 152 * other functions in this library. Returns 0 if success, non-zero if 153 * error. 154 * 155 * Proper calling procedure: 156 * 1) Allocate a context struct. 157 * 2) If multi-threaded/multi-process, acquire a lock to prevent 158 * other processes from modifying the underlying storage. 159 * 3) Read underlying storage and fill in context->raw. 160 * 4) Call VbNvSetup(). 161 * 162 * If you have access to global variables, you may want to wrap all 163 * that in your own VbNvOpen() function. We don't do that in here 164 * because there are no global variables in UEFI BIOS during the PEI 165 * phase (that's also why we have to pass around a context pointer). */ 166int VbNvSetup(VbNvContext* context); 167 168/* Clean up and flush changes back to the raw data. This must be 169 * called after other functions in this library. Returns 0 if 170 * success, non-zero if error. 171 * 172 * Proper calling procedure: 173 * 1) Call VbNvExit(). 174 * 2) If context.raw_changed, write data back to underlying storage. 175 * 3) Release any lock you acquired before calling VbNvSetup(). 176 * 4) Free the context struct. 177 * 178 * If you have access to global variables, you may want to wrap this 179 * in your own VbNvClose() function. */ 180int VbNvTeardown(VbNvContext* context); 181 182/* Read a NV storage parameter into *dest. Returns 0 if success, 183 * non-zero if error. 184 * 185 * This may only be called between VbNvSetup() and VbNvTeardown(). */ 186int VbNvGet(VbNvContext* context, VbNvParam param, uint32_t* dest); 187 188/* Set a NV storage param to a new value. Returns 0 if success, 189 * non-zero if error. 190 * 191 * This may only be called between VbNvSetup() and VbNvTeardown(). */ 192int VbNvSet(VbNvContext* context, VbNvParam param, uint32_t value); 193 194 195#endif /* VBOOT_REFERENCE_NVSTORAGE_H_ */ 196