123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480 |
- /* 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.
- */
- /* Non-volatile storage routines.
- */
- #include "sysincludes.h"
- #include "2crc8.h"
- #include "utility.h"
- #include "vboot_common.h"
- #include "vboot_nvstorage.h"
- /*
- * Constants for NV storage. We use this rather than structs and bitfields so
- * the data format is consistent across platforms and compilers.
- *
- * These constants must match the equivalent constants in 2lib/2nvstorage.c.
- * (We currently don't share a common header file because we're tring to keep
- * the two libs independent, and we hope to deprecate this one.)
- */
- #define HEADER_OFFSET 0
- #define HEADER_MASK 0xC0
- #define HEADER_SIGNATURE 0x40
- #define HEADER_FIRMWARE_SETTINGS_RESET 0x20
- #define HEADER_KERNEL_SETTINGS_RESET 0x10
- #define HEADER_WIPEOUT 0x08
- #define BOOT_OFFSET 1
- #define BOOT_DEBUG_RESET_MODE 0x80
- #define BOOT_DISABLE_DEV_REQUEST 0x40
- #define BOOT_OPROM_NEEDED 0x20
- #define BOOT_BACKUP_NVRAM 0x10
- #define BOOT_TRY_B_COUNT_MASK 0x0F
- #define RECOVERY_OFFSET 2
- #define LOCALIZATION_OFFSET 3
- #define DEV_FLAGS_OFFSET 4
- #define DEV_BOOT_USB_MASK 0x01
- #define DEV_BOOT_SIGNED_ONLY_MASK 0x02
- #define DEV_BOOT_LEGACY_MASK 0x04
- #define DEV_BOOT_FASTBOOT_FULL_CAP_MASK 0x08
- #define DEV_DEFAULT_BOOT_MASK 0x30
- #define DEV_DEFAULT_BOOT_SHIFT 4 /* Number of bits to shift */
- #define TPM_FLAGS_OFFSET 5
- #define TPM_CLEAR_OWNER_REQUEST 0x01
- #define TPM_CLEAR_OWNER_DONE 0x02
- #define TPM_REBOOTED 0x04
- #define RECOVERY_SUBCODE_OFFSET 6
- #define BOOT2_OFFSET 7
- #define BOOT2_RESULT_MASK 0x03
- #define BOOT2_TRIED 0x04
- #define BOOT2_TRY_NEXT 0x08
- #define BOOT2_PREV_RESULT_MASK 0x30
- #define BOOT2_PREV_RESULT_SHIFT 4 /* Number of bits to shift result */
- #define BOOT2_PREV_TRIED 0x40
- #define MISC_OFFSET 8
- #define MISC_UNLOCK_FASTBOOT 0x01
- #define MISC_BOOT_ON_AC_DETECT 0x02
- #define MISC_TRY_RO_SYNC 0x04
- #define MISC_BATTERY_CUTOFF_REQUEST 0x08
- #define KERNEL_FIELD_OFFSET 11
- #define CRC_OFFSET 15
- int VbNvSetup(VbNvContext *context)
- {
- uint8_t *raw = context->raw;
- /* Nothing has changed yet. */
- context->raw_changed = 0;
- context->regenerate_crc = 0;
- /* Check data for consistency */
- if ((HEADER_SIGNATURE != (raw[HEADER_OFFSET] & HEADER_MASK))
- || (vb2_crc8(raw, CRC_OFFSET) != raw[CRC_OFFSET])) {
- /* Data is inconsistent (bad CRC or header); reset defaults */
- memset(raw, 0, VBNV_BLOCK_SIZE);
- raw[HEADER_OFFSET] = (HEADER_SIGNATURE |
- HEADER_FIRMWARE_SETTINGS_RESET |
- HEADER_KERNEL_SETTINGS_RESET);
- /* Regenerate CRC on exit */
- context->regenerate_crc = 1;
- }
- return 0;
- }
- int VbNvTeardown(VbNvContext *context)
- {
- if (context->regenerate_crc) {
- context->raw[CRC_OFFSET] = vb2_crc8(context->raw, CRC_OFFSET);
- context->regenerate_crc = 0;
- context->raw_changed = 1;
- }
- return 0;
- }
- int VbNvGet(VbNvContext *context, VbNvParam param, uint32_t *dest)
- {
- const uint8_t *raw = context->raw;
- switch (param) {
- case VBNV_FIRMWARE_SETTINGS_RESET:
- *dest = (raw[HEADER_OFFSET] & HEADER_FIRMWARE_SETTINGS_RESET ?
- 1 : 0);
- return 0;
- case VBNV_KERNEL_SETTINGS_RESET:
- *dest = (raw[HEADER_OFFSET] & HEADER_KERNEL_SETTINGS_RESET ?
- 1 : 0);
- return 0;
- case VBNV_DEBUG_RESET_MODE:
- *dest = (raw[BOOT_OFFSET] & BOOT_DEBUG_RESET_MODE ? 1 : 0);
- return 0;
- case VBNV_TRY_B_COUNT:
- case VBNV_FW_TRY_COUNT:
- *dest = raw[BOOT_OFFSET] & BOOT_TRY_B_COUNT_MASK;
- return 0;
- case VBNV_RECOVERY_REQUEST:
- *dest = raw[RECOVERY_OFFSET];
- return 0;
- case VBNV_RECOVERY_SUBCODE:
- *dest = raw[RECOVERY_SUBCODE_OFFSET];
- return 0;
- case VBNV_LOCALIZATION_INDEX:
- *dest = raw[LOCALIZATION_OFFSET];
- return 0;
- case VBNV_KERNEL_FIELD:
- *dest = (raw[KERNEL_FIELD_OFFSET]
- | (raw[KERNEL_FIELD_OFFSET + 1] << 8)
- | (raw[KERNEL_FIELD_OFFSET + 2] << 16)
- | (raw[KERNEL_FIELD_OFFSET + 3] << 24));
- return 0;
- case VBNV_DEV_BOOT_USB:
- *dest = (raw[DEV_FLAGS_OFFSET] & DEV_BOOT_USB_MASK ? 1 : 0);
- return 0;
- case VBNV_DEV_BOOT_LEGACY:
- *dest = (raw[DEV_FLAGS_OFFSET] & DEV_BOOT_LEGACY_MASK ? 1 : 0);
- return 0;
- case VBNV_DEV_DEFAULT_BOOT:
- *dest = (raw[DEV_FLAGS_OFFSET] & DEV_DEFAULT_BOOT_MASK)
- >> DEV_DEFAULT_BOOT_SHIFT;
- return 0;
- case VBNV_DEV_BOOT_SIGNED_ONLY:
- *dest = (raw[DEV_FLAGS_OFFSET] & DEV_BOOT_SIGNED_ONLY_MASK ?
- 1 : 0);
- return 0;
- case VBNV_DEV_BOOT_FASTBOOT_FULL_CAP:
- *dest = (raw[DEV_FLAGS_OFFSET] & DEV_BOOT_FASTBOOT_FULL_CAP_MASK
- ? 1 : 0);
- return 0;
- case VBNV_DISABLE_DEV_REQUEST:
- *dest = (raw[BOOT_OFFSET] & BOOT_DISABLE_DEV_REQUEST ? 1 : 0);
- return 0;
- case VBNV_OPROM_NEEDED:
- *dest = (raw[BOOT_OFFSET] & BOOT_OPROM_NEEDED ? 1 : 0);
- return 0;
- case VBNV_CLEAR_TPM_OWNER_REQUEST:
- *dest = (raw[TPM_FLAGS_OFFSET] & TPM_CLEAR_OWNER_REQUEST ?
- 1 : 0);
- return 0;
- case VBNV_CLEAR_TPM_OWNER_DONE:
- *dest = (raw[TPM_FLAGS_OFFSET] & TPM_CLEAR_OWNER_DONE ? 1 : 0);
- return 0;
- case VBNV_TPM_REQUESTED_REBOOT:
- *dest = (raw[TPM_FLAGS_OFFSET] & TPM_REBOOTED ? 1 : 0);
- return 0;
- case VBNV_BACKUP_NVRAM_REQUEST:
- *dest = (raw[BOOT_OFFSET] & BOOT_BACKUP_NVRAM ? 1 : 0);
- return 0;
- case VBNV_FW_TRY_NEXT:
- *dest = (raw[BOOT2_OFFSET] & BOOT2_TRY_NEXT ? 1 : 0);
- return 0;
- case VBNV_FW_TRIED:
- *dest = (raw[BOOT2_OFFSET] & BOOT2_TRIED ? 1 : 0);
- return 0;
- case VBNV_FW_RESULT:
- *dest = raw[BOOT2_OFFSET] & BOOT2_RESULT_MASK;
- return 0;
- case VBNV_FW_PREV_TRIED:
- *dest = (raw[BOOT2_OFFSET] & BOOT2_PREV_TRIED ? 1 : 0);
- return 0;
- case VBNV_FW_PREV_RESULT:
- *dest = (raw[BOOT2_OFFSET] & BOOT2_PREV_RESULT_MASK)
- >> BOOT2_PREV_RESULT_SHIFT;
- return 0;
- case VBNV_FW_REQ_WIPEOUT:
- *dest = (raw[HEADER_OFFSET] & HEADER_WIPEOUT) ? 1 : 0;
- return 0;
- case VBNV_FASTBOOT_UNLOCK_IN_FW:
- *dest = (raw[MISC_OFFSET] & MISC_UNLOCK_FASTBOOT) ? 1 : 0;
- return 0;
- case VBNV_BOOT_ON_AC_DETECT:
- *dest = (raw[MISC_OFFSET] & MISC_BOOT_ON_AC_DETECT) ? 1 : 0;
- return 0;
- case VBNV_TRY_RO_SYNC:
- *dest = (raw[MISC_OFFSET] & MISC_TRY_RO_SYNC) ? 1 : 0;
- return 0;
- case VBNV_BATTERY_CUTOFF_REQUEST:
- *dest = (raw[MISC_OFFSET] & MISC_BATTERY_CUTOFF_REQUEST)
- ? 1 : 0;
- return 0;
- default:
- return 1;
- }
- }
- int VbNvSet(VbNvContext *context, VbNvParam param, uint32_t value)
- {
- uint8_t *raw = context->raw;
- uint32_t current;
- /* If not changing the value, don't regenerate the CRC. */
- if (0 == VbNvGet(context, param, ¤t) && current == value)
- return 0;
- switch (param) {
- case VBNV_FIRMWARE_SETTINGS_RESET:
- if (value)
- raw[HEADER_OFFSET] |= HEADER_FIRMWARE_SETTINGS_RESET;
- else
- raw[HEADER_OFFSET] &= ~HEADER_FIRMWARE_SETTINGS_RESET;
- break;
- case VBNV_KERNEL_SETTINGS_RESET:
- if (value)
- raw[HEADER_OFFSET] |= HEADER_KERNEL_SETTINGS_RESET;
- else
- raw[HEADER_OFFSET] &= ~HEADER_KERNEL_SETTINGS_RESET;
- break;
- case VBNV_DEBUG_RESET_MODE:
- if (value)
- raw[BOOT_OFFSET] |= BOOT_DEBUG_RESET_MODE;
- else
- raw[BOOT_OFFSET] &= ~BOOT_DEBUG_RESET_MODE;
- break;
- case VBNV_TRY_B_COUNT:
- case VBNV_FW_TRY_COUNT:
- /* Clip to valid range. */
- if (value > BOOT_TRY_B_COUNT_MASK)
- value = BOOT_TRY_B_COUNT_MASK;
- raw[BOOT_OFFSET] &= ~BOOT_TRY_B_COUNT_MASK;
- raw[BOOT_OFFSET] |= (uint8_t)value;
- break;
- case VBNV_RECOVERY_REQUEST:
- /*
- * Map values outside the valid range to the legacy reason,
- * since we can't determine if we're called from kernel or user
- * mode.
- */
- if (value > 0xFF)
- value = VBNV_RECOVERY_LEGACY;
- raw[RECOVERY_OFFSET] = (uint8_t)value;
- break;
- case VBNV_RECOVERY_SUBCODE:
- raw[RECOVERY_SUBCODE_OFFSET] = (uint8_t)value;
- break;
- case VBNV_LOCALIZATION_INDEX:
- /* Map values outside the valid range to the default index. */
- if (value > 0xFF)
- value = 0;
- raw[LOCALIZATION_OFFSET] = (uint8_t)value;
- break;
- case VBNV_KERNEL_FIELD:
- raw[KERNEL_FIELD_OFFSET] = (uint8_t)(value);
- raw[KERNEL_FIELD_OFFSET + 1] = (uint8_t)(value >> 8);
- raw[KERNEL_FIELD_OFFSET + 2] = (uint8_t)(value >> 16);
- raw[KERNEL_FIELD_OFFSET + 3] = (uint8_t)(value >> 24);
- break;
- case VBNV_DEV_BOOT_USB:
- if (value)
- raw[DEV_FLAGS_OFFSET] |= DEV_BOOT_USB_MASK;
- else
- raw[DEV_FLAGS_OFFSET] &= ~DEV_BOOT_USB_MASK;
- break;
- case VBNV_DEV_BOOT_LEGACY:
- if (value)
- raw[DEV_FLAGS_OFFSET] |= DEV_BOOT_LEGACY_MASK;
- else
- raw[DEV_FLAGS_OFFSET] &= ~DEV_BOOT_LEGACY_MASK;
- break;
- case VBNV_DEV_DEFAULT_BOOT:
- /* Map out of range values to boot disk */
- if (value > (DEV_DEFAULT_BOOT_MASK >>
- DEV_DEFAULT_BOOT_SHIFT))
- value = VBNV_DEV_DEFAULT_BOOT_DISK;
- raw[DEV_FLAGS_OFFSET] &= ~DEV_DEFAULT_BOOT_MASK;
- raw[DEV_FLAGS_OFFSET] |= (uint8_t)value <<
- DEV_DEFAULT_BOOT_SHIFT;
- break;
- case VBNV_DEV_BOOT_SIGNED_ONLY:
- if (value)
- raw[DEV_FLAGS_OFFSET] |= DEV_BOOT_SIGNED_ONLY_MASK;
- else
- raw[DEV_FLAGS_OFFSET] &= ~DEV_BOOT_SIGNED_ONLY_MASK;
- break;
- case VBNV_DEV_BOOT_FASTBOOT_FULL_CAP:
- if (value)
- raw[DEV_FLAGS_OFFSET] |=
- DEV_BOOT_FASTBOOT_FULL_CAP_MASK;
- else
- raw[DEV_FLAGS_OFFSET] &=
- ~DEV_BOOT_FASTBOOT_FULL_CAP_MASK;
- break;
- case VBNV_DISABLE_DEV_REQUEST:
- if (value)
- raw[BOOT_OFFSET] |= BOOT_DISABLE_DEV_REQUEST;
- else
- raw[BOOT_OFFSET] &= ~BOOT_DISABLE_DEV_REQUEST;
- break;
- case VBNV_OPROM_NEEDED:
- if (value)
- raw[BOOT_OFFSET] |= BOOT_OPROM_NEEDED;
- else
- raw[BOOT_OFFSET] &= ~BOOT_OPROM_NEEDED;
- break;
- case VBNV_CLEAR_TPM_OWNER_REQUEST:
- if (value)
- raw[TPM_FLAGS_OFFSET] |= TPM_CLEAR_OWNER_REQUEST;
- else
- raw[TPM_FLAGS_OFFSET] &= ~TPM_CLEAR_OWNER_REQUEST;
- break;
- case VBNV_CLEAR_TPM_OWNER_DONE:
- if (value)
- raw[TPM_FLAGS_OFFSET] |= TPM_CLEAR_OWNER_DONE;
- else
- raw[TPM_FLAGS_OFFSET] &= ~TPM_CLEAR_OWNER_DONE;
- break;
- case VBNV_TPM_REQUESTED_REBOOT:
- if (value)
- raw[TPM_FLAGS_OFFSET] |= TPM_REBOOTED;
- else
- raw[TPM_FLAGS_OFFSET] &= ~TPM_REBOOTED;
- break;
- case VBNV_BACKUP_NVRAM_REQUEST:
- if (value)
- raw[BOOT_OFFSET] |= BOOT_BACKUP_NVRAM;
- else
- raw[BOOT_OFFSET] &= ~BOOT_BACKUP_NVRAM;
- break;
- case VBNV_FW_TRY_NEXT:
- if (value)
- raw[BOOT2_OFFSET] |= BOOT2_TRY_NEXT;
- else
- raw[BOOT2_OFFSET] &= ~BOOT2_TRY_NEXT;
- break;
- case VBNV_FW_TRIED:
- if (value)
- raw[BOOT2_OFFSET] |= BOOT2_TRIED;
- else
- raw[BOOT2_OFFSET] &= ~BOOT2_TRIED;
- break;
- case VBNV_FW_RESULT:
- /* Map out of range values to unknown */
- if (value > BOOT2_RESULT_MASK)
- value = VBNV_FW_RESULT_UNKNOWN;
- raw[BOOT2_OFFSET] &= ~BOOT2_RESULT_MASK;
- raw[BOOT2_OFFSET] |= (uint8_t)value;
- break;
- case VBNV_FW_PREV_TRIED:
- if (value)
- raw[BOOT2_OFFSET] |= BOOT2_PREV_TRIED;
- else
- raw[BOOT2_OFFSET] &= ~BOOT2_PREV_TRIED;
- break;
- case VBNV_FW_PREV_RESULT:
- /* Map out of range values to unknown */
- if (value > BOOT2_RESULT_MASK)
- value = VBNV_FW_RESULT_UNKNOWN;
- raw[BOOT2_OFFSET] &= ~BOOT2_PREV_RESULT_MASK;
- raw[BOOT2_OFFSET] |= (uint8_t)value << BOOT2_PREV_RESULT_SHIFT;
- break;
- case VBNV_FW_REQ_WIPEOUT:
- if (value)
- raw[HEADER_OFFSET] |= HEADER_WIPEOUT;
- else
- raw[HEADER_OFFSET] &= ~HEADER_WIPEOUT;
- break;
- case VBNV_FASTBOOT_UNLOCK_IN_FW:
- if (value)
- raw[MISC_OFFSET] |= MISC_UNLOCK_FASTBOOT;
- else
- raw[MISC_OFFSET] &= ~MISC_UNLOCK_FASTBOOT;
- break;
- case VBNV_BOOT_ON_AC_DETECT:
- if (value)
- raw[MISC_OFFSET] |= MISC_BOOT_ON_AC_DETECT;
- else
- raw[MISC_OFFSET] &= ~MISC_BOOT_ON_AC_DETECT;
- break;
- case VBNV_TRY_RO_SYNC:
- if (value)
- raw[MISC_OFFSET] |= MISC_TRY_RO_SYNC;
- else
- raw[MISC_OFFSET] &= ~MISC_TRY_RO_SYNC;
- break;
- case VBNV_BATTERY_CUTOFF_REQUEST:
- if (value)
- raw[MISC_OFFSET] |= MISC_BATTERY_CUTOFF_REQUEST;
- else
- raw[MISC_OFFSET] &= ~MISC_BATTERY_CUTOFF_REQUEST;
- break;
- default:
- return 1;
- }
- /* Need to regenerate CRC, since the value changed. */
- context->regenerate_crc = 1;
- return 0;
- }
|