vboot_nvstorage_test.c revision 172602829dc0d79ed65d7ed81225389f090b981f
1/* Copyright (c) 2010 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_FW_USED_TRY_B, 0, 1, 0, "firmware used try B"}, 33 {VBNV_FW_VERIFIED_KERNEL_KEY, 0, 1, 0, "firmware verified kernel key"}, 34 {0, 0, 0, 0, NULL} 35}; 36 37static void VbNvStorageTest(void) { 38 39 VbNvField* vnf; 40 VbNvContext c; 41 uint8_t goodcrc; 42 uint32_t data; 43 44 memset(&c, 0xA6, sizeof(c)); 45 46 /* Open with invalid data should set defaults */ 47 TEST_EQ(VbNvSetup(&c), 0, "VbNvSetup()"); 48 TEST_EQ(c.raw[0], 0x70, "VbNvSetup() reset header byte"); 49 /* Close then regenerates the CRC */ 50 TEST_EQ(VbNvTeardown(&c), 0, "VbNvTeardown()"); 51 TEST_NEQ(c.raw[15], 0, "VbNvTeardown() CRC"); 52 TEST_EQ(c.raw_changed, 1, "VbNvTeardown() changed"); 53 goodcrc = c.raw[15]; 54 /* Another open-close pair should not cause further changes */ 55 VbNvSetup(&c); 56 VbNvTeardown(&c); 57 TEST_EQ(c.raw_changed, 0, "VbNvTeardown() didn't change"); 58 TEST_EQ(c.raw[15], goodcrc, "VbNvTeardown() CRC same"); 59 60 /* Perturbing the header should force defaults */ 61 c.raw[0] ^= 0x40; 62 VbNvSetup(&c); 63 TEST_EQ(c.raw[0], 0x70, "VbNvSetup() reset header byte again"); 64 /* Close then regenerates the CRC */ 65 VbNvTeardown(&c); 66 TEST_EQ(c.raw_changed, 1, "VbNvTeardown() changed again"); 67 TEST_EQ(c.raw[15], goodcrc, "VbNvTeardown() CRC same again"); 68 69 /* So should perturbing some other byte */ 70 TEST_EQ(c.raw[11], 0, "Kernel byte starts at 0"); 71 c.raw[11] = 12; 72 VbNvSetup(&c); 73 TEST_EQ(c.raw[11], 0, "VbNvSetup() reset kernel byte"); 74 /* Close then regenerates the CRC */ 75 VbNvTeardown(&c); 76 TEST_EQ(c.raw_changed, 1, "VbNvTeardown() changed again"); 77 TEST_EQ(c.raw[15], goodcrc, "VbNvTeardown() CRC same again"); 78 79 /* Clear the kernel and firmware flags */ 80 VbNvSetup(&c); 81 TEST_EQ(VbNvGet(&c, VBNV_FIRMWARE_SETTINGS_RESET, &data), 0, 82 "Get firmware settings reset"); 83 TEST_EQ(data, 1, "Firmware settings are reset"); 84 TEST_EQ(VbNvSet(&c, VBNV_FIRMWARE_SETTINGS_RESET, 0), 0, 85 "Clear firmware settings reset"); 86 VbNvGet(&c, VBNV_FIRMWARE_SETTINGS_RESET, &data); 87 TEST_EQ(data, 0, "Firmware settings are clear"); 88 89 TEST_EQ(VbNvGet(&c, VBNV_KERNEL_SETTINGS_RESET, &data), 0, 90 "Get kernel settings reset"); 91 TEST_EQ(data, 1, "Kernel settings are reset"); 92 TEST_EQ(VbNvSet(&c, VBNV_KERNEL_SETTINGS_RESET, 0), 0, 93 "Clear kernel settings reset"); 94 VbNvGet(&c, VBNV_KERNEL_SETTINGS_RESET, &data); 95 TEST_EQ(data, 0, "Kernel settings are clear"); 96 TEST_EQ(c.raw[0], 0x40, "Header byte now just has the header bit"); 97 VbNvTeardown(&c); 98 /* That should have changed the CRC */ 99 TEST_NEQ(c.raw[15], goodcrc, "VbNvTeardown() CRC changed due to flags clear"); 100 101 /* Test other fields */ 102 VbNvSetup(&c); 103 for (vnf = nvfields; vnf->desc; vnf++) { 104 TEST_EQ(VbNvGet(&c, vnf->param, &data), 0, vnf->desc); 105 TEST_EQ(data, vnf->default_value, vnf->desc); 106 107 TEST_EQ(VbNvSet(&c, vnf->param, vnf->test_value), 0, vnf->desc); 108 TEST_EQ(VbNvGet(&c, vnf->param, &data), 0, vnf->desc); 109 TEST_EQ(data, vnf->test_value, vnf->desc); 110 111 TEST_EQ(VbNvSet(&c, vnf->param, vnf->test_value2), 0, vnf->desc); 112 TEST_EQ(VbNvGet(&c, vnf->param, &data), 0, vnf->desc); 113 TEST_EQ(data, vnf->test_value2, vnf->desc); 114 } 115 VbNvTeardown(&c); 116 117 /* None of those changes should have caused a reset to defaults */ 118 VbNvSetup(&c); 119 VbNvGet(&c, VBNV_FIRMWARE_SETTINGS_RESET, &data); 120 TEST_EQ(data, 0, "Firmware settings are still clear"); 121 VbNvGet(&c, VBNV_KERNEL_SETTINGS_RESET, &data); 122 TEST_EQ(data, 0, "Kernel settings are still clear"); 123 VbNvTeardown(&c); 124 125 /* Verify writing identical settings doesn't cause the CRC to regenerate */ 126 VbNvSetup(&c); 127 TEST_EQ(c.regenerate_crc, 0, "No regen CRC on open"); 128 for (vnf = nvfields; vnf->desc; vnf++) 129 TEST_EQ(VbNvSet(&c, vnf->param, vnf->test_value2), 0, vnf->desc); 130 TEST_EQ(c.regenerate_crc, 0, "No regen CRC if data not changed"); 131 VbNvTeardown(&c); 132 TEST_EQ(c.raw_changed, 0, "No raw change if data not changed"); 133} 134 135 136/* disable MSVC warnings on unused arguments */ 137__pragma(warning (disable: 4100)) 138 139int main(int argc, char* argv[]) { 140 int error_code = 0; 141 142 VbNvStorageTest(); 143 144 if (!gTestSuccess) 145 error_code = 255; 146 147 return error_code; 148} 149