vboot_nvstorage_test.c revision 6dbf9d9160b2927d76017d0f51919e0880f9b2cc
1/* Copyright (c) 2013 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 * Tests for firmware NV storage library. 6 */ 7 8#include <stdio.h> 9#include <stdlib.h> 10#include <string.h> 11 12#include "test_common.h" 13#include "vboot_common.h" 14#include "vboot_nvstorage.h" 15 16/* Single NV storage field to test */ 17typedef struct VbNvField { 18 VbNvParam param; /* Parameter index */ 19 uint32_t default_value; /* Expected default value */ 20 uint32_t test_value; /* Value to test writing */ 21 uint32_t test_value2; /* Second value to test writing */ 22 char* desc; /* Field description */ 23} VbNvField; 24 25/* Array of fields to test, terminated with a field with desc==NULL. */ 26static VbNvField nvfields[] = { 27 {VBNV_DEBUG_RESET_MODE, 0, 1, 0, "debug reset mode"}, 28 {VBNV_TRY_B_COUNT, 0, 6, 15, "try B count"}, 29 {VBNV_RECOVERY_REQUEST, 0, 0x42, 0xED, "recovery request"}, 30 {VBNV_LOCALIZATION_INDEX, 0, 0x69, 0xB0, "localization index"}, 31 {VBNV_KERNEL_FIELD, 0, 0x12345678, 0xFEDCBA98, "kernel field"}, 32 {VBNV_DEV_BOOT_USB, 0, 1, 0, "dev boot usb"}, 33 {VBNV_DEV_BOOT_LEGACY, 0, 1, 0, "dev boot legacy"}, 34 {VBNV_DEV_BOOT_SIGNED_ONLY, 0, 1, 0, "dev boot custom"}, 35 {VBNV_DISABLE_DEV_REQUEST, 0, 1, 0, "disable dev request"}, 36 {VBNV_CLEAR_TPM_OWNER_REQUEST, 0, 1, 0, "clear tpm owner request"}, 37 {VBNV_CLEAR_TPM_OWNER_DONE, 0, 1, 0, "clear tpm owner done"}, 38 {VBNV_OPROM_NEEDED, 0, 1, 0, "oprom needed"}, 39 {0, 0, 0, 0, NULL} 40}; 41 42static void VbNvStorageTest(void) { 43 44 VbNvField* vnf; 45 VbNvContext c; 46 uint8_t goodcrc; 47 uint32_t data; 48 49 memset(&c, 0xA6, sizeof(c)); 50 51 /* Open with invalid data should set defaults */ 52 TEST_EQ(VbNvSetup(&c), 0, "VbNvSetup()"); 53 TEST_EQ(c.raw[0], 0x70, "VbNvSetup() reset header byte"); 54 /* Close then regenerates the CRC */ 55 TEST_EQ(VbNvTeardown(&c), 0, "VbNvTeardown()"); 56 TEST_NEQ(c.raw[15], 0, "VbNvTeardown() CRC"); 57 TEST_EQ(c.raw_changed, 1, "VbNvTeardown() changed"); 58 goodcrc = c.raw[15]; 59 /* Another open-close pair should not cause further changes */ 60 VbNvSetup(&c); 61 VbNvTeardown(&c); 62 TEST_EQ(c.raw_changed, 0, "VbNvTeardown() didn't change"); 63 TEST_EQ(c.raw[15], goodcrc, "VbNvTeardown() CRC same"); 64 65 /* Perturbing the header should force defaults */ 66 c.raw[0] ^= 0x40; 67 VbNvSetup(&c); 68 TEST_EQ(c.raw[0], 0x70, "VbNvSetup() reset header byte again"); 69 /* Close then regenerates the CRC */ 70 VbNvTeardown(&c); 71 TEST_EQ(c.raw_changed, 1, "VbNvTeardown() changed again"); 72 TEST_EQ(c.raw[15], goodcrc, "VbNvTeardown() CRC same again"); 73 74 /* So should perturbing some other byte */ 75 TEST_EQ(c.raw[11], 0, "Kernel byte starts at 0"); 76 c.raw[11] = 12; 77 VbNvSetup(&c); 78 TEST_EQ(c.raw[11], 0, "VbNvSetup() reset kernel byte"); 79 /* Close then regenerates the CRC */ 80 VbNvTeardown(&c); 81 TEST_EQ(c.raw_changed, 1, "VbNvTeardown() changed again"); 82 TEST_EQ(c.raw[15], goodcrc, "VbNvTeardown() CRC same again"); 83 84 /* Clear the kernel and firmware flags */ 85 VbNvSetup(&c); 86 TEST_EQ(VbNvGet(&c, VBNV_FIRMWARE_SETTINGS_RESET, &data), 0, 87 "Get firmware settings reset"); 88 TEST_EQ(data, 1, "Firmware settings are reset"); 89 TEST_EQ(VbNvSet(&c, VBNV_FIRMWARE_SETTINGS_RESET, 0), 0, 90 "Clear firmware settings reset"); 91 VbNvGet(&c, VBNV_FIRMWARE_SETTINGS_RESET, &data); 92 TEST_EQ(data, 0, "Firmware settings are clear"); 93 94 TEST_EQ(VbNvGet(&c, VBNV_KERNEL_SETTINGS_RESET, &data), 0, 95 "Get kernel settings reset"); 96 TEST_EQ(data, 1, "Kernel settings are reset"); 97 TEST_EQ(VbNvSet(&c, VBNV_KERNEL_SETTINGS_RESET, 0), 0, 98 "Clear kernel settings reset"); 99 VbNvGet(&c, VBNV_KERNEL_SETTINGS_RESET, &data); 100 TEST_EQ(data, 0, "Kernel settings are clear"); 101 TEST_EQ(c.raw[0], 0x40, "Header byte now just has the header bit"); 102 VbNvTeardown(&c); 103 /* That should have changed the CRC */ 104 TEST_NEQ(c.raw[15], goodcrc, "VbNvTeardown() CRC changed due to flags clear"); 105 106 /* Test explicitly setting the reset flags again */ 107 VbNvSetup(&c); 108 VbNvSet(&c, VBNV_FIRMWARE_SETTINGS_RESET, 1); 109 VbNvGet(&c, VBNV_FIRMWARE_SETTINGS_RESET, &data); 110 TEST_EQ(data, 1, "Firmware settings forced reset"); 111 VbNvSet(&c, VBNV_FIRMWARE_SETTINGS_RESET, 0); 112 113 VbNvSet(&c, VBNV_KERNEL_SETTINGS_RESET, 1); 114 VbNvGet(&c, VBNV_KERNEL_SETTINGS_RESET, &data); 115 TEST_EQ(data, 1, "Kernel settings forced reset"); 116 VbNvSet(&c, VBNV_KERNEL_SETTINGS_RESET, 0); 117 VbNvTeardown(&c); 118 119 /* Get/set an invalid field */ 120 VbNvSetup(&c); 121 TEST_EQ(VbNvGet(&c, -1, &data), 1, "Get invalid setting"); 122 TEST_EQ(VbNvSet(&c, -1, 0), 1, "Set invalid setting"); 123 VbNvTeardown(&c); 124 125 /* Test other fields */ 126 VbNvSetup(&c); 127 for (vnf = nvfields; vnf->desc; vnf++) { 128 TEST_EQ(VbNvGet(&c, vnf->param, &data), 0, vnf->desc); 129 TEST_EQ(data, vnf->default_value, vnf->desc); 130 131 TEST_EQ(VbNvSet(&c, vnf->param, vnf->test_value), 0, vnf->desc); 132 TEST_EQ(VbNvGet(&c, vnf->param, &data), 0, vnf->desc); 133 TEST_EQ(data, vnf->test_value, vnf->desc); 134 135 TEST_EQ(VbNvSet(&c, vnf->param, vnf->test_value2), 0, vnf->desc); 136 TEST_EQ(VbNvGet(&c, vnf->param, &data), 0, vnf->desc); 137 TEST_EQ(data, vnf->test_value2, vnf->desc); 138 } 139 VbNvTeardown(&c); 140 141 /* None of those changes should have caused a reset to defaults */ 142 VbNvSetup(&c); 143 VbNvGet(&c, VBNV_FIRMWARE_SETTINGS_RESET, &data); 144 TEST_EQ(data, 0, "Firmware settings are still clear"); 145 VbNvGet(&c, VBNV_KERNEL_SETTINGS_RESET, &data); 146 TEST_EQ(data, 0, "Kernel settings are still clear"); 147 VbNvTeardown(&c); 148 149 /* Verify writing identical settings doesn't cause the CRC to regenerate */ 150 VbNvSetup(&c); 151 TEST_EQ(c.regenerate_crc, 0, "No regen CRC on open"); 152 for (vnf = nvfields; vnf->desc; vnf++) 153 TEST_EQ(VbNvSet(&c, vnf->param, vnf->test_value2), 0, vnf->desc); 154 TEST_EQ(c.regenerate_crc, 0, "No regen CRC if data not changed"); 155 VbNvTeardown(&c); 156 TEST_EQ(c.raw_changed, 0, "No raw change if data not changed"); 157 158 /* Test out-of-range fields mapping to defaults */ 159 VbNvSetup(&c); 160 VbNvSet(&c, VBNV_TRY_B_COUNT, 16); 161 VbNvGet(&c, VBNV_TRY_B_COUNT, &data); 162 TEST_EQ(data, 15, "Try b count out of range"); 163 VbNvSet(&c, VBNV_RECOVERY_REQUEST, 0x101); 164 VbNvGet(&c, VBNV_RECOVERY_REQUEST, &data); 165 TEST_EQ(data, VBNV_RECOVERY_LEGACY, "Recovery request out of range"); 166 VbNvSet(&c, VBNV_LOCALIZATION_INDEX, 0x102); 167 VbNvGet(&c, VBNV_LOCALIZATION_INDEX, &data); 168 TEST_EQ(data, 0, "Localization index out of range"); 169 VbNvTeardown(&c); 170} 171 172 173/* disable MSVC warnings on unused arguments */ 174__pragma(warning (disable: 4100)) 175 176int main(int argc, char* argv[]) { 177 int error_code = 0; 178 179 VbNvStorageTest(); 180 181 if (!gTestSuccess) 182 error_code = 255; 183 184 return error_code; 185} 186