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