vb2_nvstorage_tests.c revision 8de992c441e36453a5a08bcf7c2462835007f81e
1/* Copyright (c) 2014 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 <stdint.h> 9#include <stdio.h> 10#include <stdlib.h> 11#include <string.h> 12 13#include "test_common.h" 14#include "vboot_common.h" 15 16#include "2api.h" 17#include "2common.h" 18#include "2misc.h" 19#include "2nvstorage.h" 20 21/* Single NV storage field to test */ 22struct nv_field { 23 enum vb2_nv_param param; /* Parameter index */ 24 uint32_t default_value; /* Expected default value */ 25 uint32_t test_value; /* Value to test writing */ 26 uint32_t test_value2; /* Second value to test writing */ 27 char *desc; /* Field description */ 28}; 29 30/* Array of fields to test, terminated with a field with desc==NULL. */ 31static struct nv_field nvfields[] = { 32 {VB2_NV_DEBUG_RESET_MODE, 0, 1, 0, "debug reset mode"}, 33 {VB2_NV_TRY_NEXT, 0, 1, 0, "try next"}, 34 {VB2_NV_TRY_COUNT, 0, 6, 15, "try B count"}, 35 {VB2_NV_FW_TRIED, 0, 1, 0, "firmware tried"}, 36 {VB2_NV_FW_RESULT, 0, 1, 2, "firmware result"}, 37 {VB2_NV_RECOVERY_REQUEST, 0, 0x42, 0xED, "recovery request"}, 38 {VB2_NV_RECOVERY_SUBCODE, 0, 0x56, 0xAC, "recovery subcode"}, 39 {VB2_NV_LOCALIZATION_INDEX, 0, 0x69, 0xB0, "localization index"}, 40 {VB2_NV_KERNEL_FIELD, 0, 0x12345678, 0xFEDCBA98, "kernel field"}, 41 {VB2_NV_DEV_BOOT_USB, 0, 1, 0, "dev boot usb"}, 42 {VB2_NV_DEV_BOOT_LEGACY, 0, 1, 0, "dev boot legacy"}, 43 {VB2_NV_DEV_BOOT_SIGNED_ONLY, 0, 1, 0, "dev boot custom"}, 44 {VB2_NV_DISABLE_DEV_REQUEST, 0, 1, 0, "disable dev request"}, 45 {VB2_NV_CLEAR_TPM_OWNER_REQUEST, 0, 1, 0, "clear tpm owner request"}, 46 {VB2_NV_CLEAR_TPM_OWNER_DONE, 0, 1, 0, "clear tpm owner done"}, 47 {VB2_NV_OPROM_NEEDED, 0, 1, 0, "oprom needed"}, 48 {VB2_NV_BACKUP_NVRAM_REQUEST, 0, 1, 0, "backup nvram request"}, 49 {0, 0, 0, 0, NULL} 50}; 51 52static void test_changed(struct vb2_context *ctx, int changed, const char *why) 53{ 54 if (changed) 55 TEST_NEQ(ctx->flags & VB2_CONTEXT_NVDATA_CHANGED, 0, why); 56 else 57 TEST_EQ(ctx->flags & VB2_CONTEXT_NVDATA_CHANGED, 0, why); 58}; 59 60static void nv_storage_test(void) 61{ 62 struct nv_field *vnf; 63 uint8_t goodcrc; 64 uint8_t workbuf[VB2_WORKBUF_RECOMMENDED_SIZE]; 65 struct vb2_context c = { 66 .flags = 0, 67 .workbuf = workbuf, 68 .workbuf_size = sizeof(workbuf), 69 }; 70 struct vb2_shared_data *sd = vb2_get_sd(&c); 71 72 memset(c.nvdata, 0xA6, sizeof(c.nvdata)); 73 vb2_init_context(&c); 74 75 /* Init with invalid data should set defaults and regenerate CRC */ 76 vb2_nv_init(&c); 77 TEST_EQ(c.nvdata[0], 0x70, "vb2_nv_init() reset header byte"); 78 TEST_NEQ(c.nvdata[15], 0, "vb2_nv_init() CRC"); 79 TEST_EQ(sd->status, VB2_SD_STATUS_NV_INIT | VB2_SD_STATUS_NV_REINIT, 80 "vb2_nv_init() status changed"); 81 test_changed(&c, 1, "vb2_nv_init() reset changed"); 82 goodcrc = c.nvdata[15]; 83 TEST_SUCC(vb2_nv_check_crc(&c), "vb2_nv_check_crc() good"); 84 85 /* Another init should not cause further changes */ 86 c.flags = 0; 87 sd->status = 0; 88 vb2_nv_init(&c); 89 test_changed(&c, 0, "vb2_nv_init() didn't re-reset"); 90 TEST_EQ(c.nvdata[15], goodcrc, "vb2_nv_init() CRC same"); 91 TEST_EQ(sd->status, VB2_SD_STATUS_NV_INIT, "vb2_nv_init() status same"); 92 93 /* Perturbing the header should force defaults */ 94 c.nvdata[0] ^= 0x40; 95 TEST_EQ(vb2_nv_check_crc(&c), 96 VB2_ERROR_NV_HEADER, "vb2_nv_check_crc() bad header"); 97 vb2_nv_init(&c); 98 TEST_EQ(c.nvdata[0], 0x70, "vb2_nv_init() reset header byte again"); 99 test_changed(&c, 1, "vb2_nv_init() corrupt changed"); 100 TEST_EQ(c.nvdata[15], goodcrc, "vb2_nv_init() CRC same again"); 101 102 /* So should perturbing some other byte */ 103 TEST_EQ(c.nvdata[11], 0, "Kernel byte starts at 0"); 104 c.nvdata[11] = 12; 105 TEST_EQ(vb2_nv_check_crc(&c), 106 VB2_ERROR_NV_CRC, "vb2_nv_check_crc() bad CRC"); 107 vb2_nv_init(&c); 108 TEST_EQ(c.nvdata[11], 0, "vb2_nv_init() reset kernel byte"); 109 test_changed(&c, 1, "vb2_nv_init() corrupt elsewhere changed"); 110 TEST_EQ(c.nvdata[15], goodcrc, "vb2_nv_init() CRC same again"); 111 112 /* Clear the kernel and firmware flags */ 113 vb2_nv_init(&c); 114 TEST_EQ(vb2_nv_get(&c, VB2_NV_FIRMWARE_SETTINGS_RESET), 115 1, "Firmware settings are reset"); 116 vb2_nv_set(&c, VB2_NV_FIRMWARE_SETTINGS_RESET, 0); 117 TEST_EQ(vb2_nv_get(&c, VB2_NV_FIRMWARE_SETTINGS_RESET), 118 0, "Firmware settings are clear"); 119 120 TEST_EQ(vb2_nv_get(&c, VB2_NV_KERNEL_SETTINGS_RESET), 121 1, "Kernel settings are reset"); 122 vb2_nv_set(&c, VB2_NV_KERNEL_SETTINGS_RESET, 0); 123 TEST_EQ(vb2_nv_get(&c, VB2_NV_KERNEL_SETTINGS_RESET), 124 0, "Kernel settings are clear"); 125 126 TEST_EQ(c.nvdata[0], 0x40, "Header byte now just has the header bit"); 127 /* That should have changed the CRC */ 128 TEST_NEQ(c.nvdata[15], goodcrc, 129 "vb2_nv_init() CRC changed due to flags clear"); 130 131 /* Test explicitly setting the reset flags again */ 132 vb2_nv_init(&c); 133 vb2_nv_set(&c, VB2_NV_FIRMWARE_SETTINGS_RESET, 1); 134 TEST_EQ(vb2_nv_get(&c, VB2_NV_FIRMWARE_SETTINGS_RESET), 135 1, "Firmware settings forced reset"); 136 vb2_nv_set(&c, VB2_NV_FIRMWARE_SETTINGS_RESET, 0); 137 138 vb2_nv_set(&c, VB2_NV_KERNEL_SETTINGS_RESET, 1); 139 TEST_EQ(vb2_nv_get(&c, VB2_NV_KERNEL_SETTINGS_RESET), 140 1, "Kernel settings forced reset"); 141 vb2_nv_set(&c, VB2_NV_KERNEL_SETTINGS_RESET, 0); 142 143 /* Get/set an invalid field */ 144 vb2_nv_init(&c); 145 vb2_nv_set(&c, -1, 1); 146 TEST_EQ(vb2_nv_get(&c, -1), 0, "Get invalid setting"); 147 148 /* Test other fields */ 149 vb2_nv_init(&c); 150 for (vnf = nvfields; vnf->desc; vnf++) { 151 TEST_EQ(vb2_nv_get(&c, vnf->param), vnf->default_value, 152 vnf->desc); 153 vb2_nv_set(&c, vnf->param, vnf->test_value); 154 TEST_EQ(vb2_nv_get(&c, vnf->param), vnf->test_value, vnf->desc); 155 vb2_nv_set(&c, vnf->param, vnf->test_value2); 156 TEST_EQ(vb2_nv_get(&c, vnf->param), vnf->test_value2, 157 vnf->desc); 158 } 159 160 /* None of those changes should have caused a reset to defaults */ 161 vb2_nv_init(&c); 162 TEST_EQ(vb2_nv_get(&c, VB2_NV_FIRMWARE_SETTINGS_RESET), 163 0, "Firmware settings are still clear"); 164 TEST_EQ(vb2_nv_get(&c, VB2_NV_KERNEL_SETTINGS_RESET), 165 0, "Kernel settings are still clear"); 166 167 /* Writing identical settings doesn't cause the CRC to regenerate */ 168 c.flags = 0; 169 vb2_nv_init(&c); 170 test_changed(&c, 0, "No regen CRC on open"); 171 for (vnf = nvfields; vnf->desc; vnf++) 172 vb2_nv_set(&c, vnf->param, vnf->test_value2); 173 test_changed(&c, 0, "No regen CRC if data not changed"); 174 175 /* Test out-of-range fields mapping to defaults or failing */ 176 vb2_nv_init(&c); 177 vb2_nv_set(&c, VB2_NV_TRY_COUNT, 16); 178 TEST_EQ(vb2_nv_get(&c, VB2_NV_TRY_COUNT), 179 15, "Try b count out of range"); 180 vb2_nv_set(&c, VB2_NV_RECOVERY_REQUEST, 0x101); 181 TEST_EQ(vb2_nv_get(&c, VB2_NV_RECOVERY_REQUEST), 182 VB2_RECOVERY_LEGACY, "Recovery request out of range"); 183 vb2_nv_set(&c, VB2_NV_LOCALIZATION_INDEX, 0x102); 184 TEST_EQ(vb2_nv_get(&c, VB2_NV_LOCALIZATION_INDEX), 185 0, "Localization index out of range"); 186 187 vb2_nv_set(&c, VB2_NV_FW_RESULT, VB2_FW_RESULT_UNKNOWN + 1); 188 vb2_nv_set(&c, VB2_NV_FW_RESULT, VB2_FW_RESULT_UNKNOWN + 100); 189 TEST_EQ(vb2_nv_get(&c, VB2_NV_FW_RESULT), 190 VB2_FW_RESULT_UNKNOWN, "Firmware result out of range"); 191} 192 193int main(int argc, char* argv[]) 194{ 195 nv_storage_test(); 196 197 return gTestSuccess ? 0 : 255; 198} 199