| OLD | NEW |
| (Empty) | |
| 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 image 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 #if 0 |
| 17 |
| 18 /* Initialize the NV storage library. This must be called before any |
| 19 * other functions in this library. Returns 0 if success, non-zero if |
| 20 * error. */ |
| 21 int VbNvOpen(VbNvContext* context); |
| 22 |
| 23 /* Clean up and flush changes back to the raw data. This must be |
| 24 * called after other functions in this library. Caller must check |
| 25 * context.raw_changed after calling tis function. Returns 0 if |
| 26 * success, non-zero if error. */ |
| 27 int VbNvClose(VbNvContext* context); |
| 28 |
| 29 /* Read a NV storage parameter into *dest. Returns 0 if success, |
| 30 * non-zero if error. */ |
| 31 int VbNvGet(VbNvContext* context, VbNvParam param, uint32_t* dest); |
| 32 |
| 33 /* Set a NV storage param to a new value. Returns 0 if success, |
| 34 * non-zero if error. */ |
| 35 int VbNvSet(VbNvContext* context, VbNvParam param, uint32_t value); |
| 36 |
| 37 typedef struct VbNvContext { |
| 38 /* Raw NV data. Caller must fill this before calling VbNvStart(). */ |
| 39 uint8_t raw[NV_BLOCK_SIZE]; |
| 40 /* Flag indicating whether raw data has changed. Set by VbNvStop() if |
| 41 * the raw data has changed and needs to be stored to the underlying |
| 42 * non-volatile data store. */ |
| 43 int raw_changed; |
| 44 |
| 45 /* Internal data for NV storage routines. Caller should not touch |
| 46 * these fields. */ |
| 47 int regenerate_crc; |
| 48 |
| 49 } VbNvContext; |
| 50 |
| 51 #endif |
| 52 |
| 53 static void VbStorageTest(void) { |
| 54 |
| 55 VbNvContext c; |
| 56 uint8_t goodcrc; |
| 57 uint32_t data; |
| 58 |
| 59 memset(&c, 0xA6, sizeof(c)); |
| 60 |
| 61 /* Open with invalid data should set defaults */ |
| 62 TEST_EQ(VbNvOpen(&c), 0, "VbNvOpen()"); |
| 63 TEST_EQ(c.raw[0], 0x70, "VbNvOpen() reset header byte"); |
| 64 /* Close then regenerates the CRC */ |
| 65 TEST_EQ(VbNvClose(&c), 0, "VbNvClose()"); |
| 66 TEST_NEQ(c.raw[15], 0, "VbNvClose() CRC"); |
| 67 TEST_EQ(c.raw_changed, 1, "VbNvClose() changed"); |
| 68 goodcrc = c.raw[15]; |
| 69 /* Another open-close pair should not cause further changes */ |
| 70 VbNvOpen(&c); |
| 71 VbNvClose(&c); |
| 72 TEST_EQ(c.raw_changed, 0, "VbNvClose() didn't change"); |
| 73 TEST_EQ(c.raw[15], goodcrc, "VbNvClose() CRC same"); |
| 74 |
| 75 /* Perturbing the header should force defaults */ |
| 76 c.raw[0] ^= 0x40; |
| 77 VbNvOpen(&c); |
| 78 TEST_EQ(c.raw[0], 0x70, "VbNvOpen() reset header byte again"); |
| 79 /* Close then regenerates the CRC */ |
| 80 VbNvClose(&c); |
| 81 TEST_EQ(c.raw_changed, 1, "VbNvClose() changed again"); |
| 82 TEST_EQ(c.raw[15], goodcrc, "VbNvClose() CRC same again"); |
| 83 |
| 84 /* So should perturbing some other byte */ |
| 85 TEST_EQ(c.raw[11], 0, "Kernel byte starts at 0"); |
| 86 c.raw[11] = 12; |
| 87 VbNvOpen(&c); |
| 88 TEST_EQ(c.raw[11], 0, "VbNvOpen() reset kernel byte"); |
| 89 /* Close then regenerates the CRC */ |
| 90 VbNvClose(&c); |
| 91 TEST_EQ(c.raw_changed, 1, "VbNvClose() changed again"); |
| 92 TEST_EQ(c.raw[15], goodcrc, "VbNvClose() CRC same again"); |
| 93 |
| 94 /* Clear the kernel and firmware flags */ |
| 95 VbNvOpen(&c); |
| 96 TEST_EQ(VbNvGet(&c, VBNV_FIRMWARE_SETTINGS_RESET, &data), 0, |
| 97 "Get firmware settings reset"); |
| 98 TEST_EQ(data, 1, "Firmware settings are reset"); |
| 99 TEST_EQ(VbNvSet(&c, VBNV_FIRMWARE_SETTINGS_RESET, 0), 0, |
| 100 "Clear firmware settings reset"); |
| 101 VbNvGet(&c, VBNV_FIRMWARE_SETTINGS_RESET, &data); |
| 102 TEST_EQ(data, 0, "Firmware settings are clear"); |
| 103 |
| 104 TEST_EQ(VbNvGet(&c, VBNV_KERNEL_SETTINGS_RESET, &data), 0, |
| 105 "Get kernel settings reset"); |
| 106 TEST_EQ(data, 1, "Kernel settings are reset"); |
| 107 TEST_EQ(VbNvSet(&c, VBNV_KERNEL_SETTINGS_RESET, 0), 0, |
| 108 "Clear kernel settings reset"); |
| 109 VbNvGet(&c, VBNV_KERNEL_SETTINGS_RESET, &data); |
| 110 TEST_EQ(data, 0, "Kernel settings are clear"); |
| 111 TEST_EQ(c.raw[0], 0x40, "Header byte now just has the header bit"); |
| 112 VbNvClose(&c); |
| 113 /* That should have changed the CRC */ |
| 114 TEST_NEQ(c.raw[15], goodcrc, "VbNvClose() CRC changed due to flags clear"); |
| 115 |
| 116 /* Test debug reset mode field */ |
| 117 VbNvOpen(&c); |
| 118 TEST_EQ(VbNvGet(&c, VBNV_DEBUG_RESET_MODE, &data), 0, |
| 119 "Get debug reset mode"); |
| 120 TEST_EQ(data, 0, "Debug reset mode default"); |
| 121 TEST_EQ(VbNvSet(&c, VBNV_DEBUG_RESET_MODE, 1), 0, |
| 122 "Set debug reset mode"); |
| 123 VbNvGet(&c, VBNV_DEBUG_RESET_MODE, &data); |
| 124 TEST_EQ(data, 1, "Debug reset mode set"); |
| 125 VbNvClose(&c); |
| 126 |
| 127 /* Test try B count */ |
| 128 VbNvOpen(&c); |
| 129 TEST_EQ(VbNvGet(&c, VBNV_TRY_B_COUNT, &data), 0, "Get try b count"); |
| 130 TEST_EQ(data, 0, "Try b count default"); |
| 131 TEST_EQ(VbNvSet(&c, VBNV_TRY_B_COUNT, 6), 0, "Set try b count"); |
| 132 VbNvGet(&c, VBNV_TRY_B_COUNT, &data); |
| 133 TEST_EQ(data, 6, "Try b count set"); |
| 134 VbNvSet(&c, VBNV_TRY_B_COUNT, 15); |
| 135 VbNvGet(&c, VBNV_TRY_B_COUNT, &data); |
| 136 TEST_EQ(data, 15, "Try b count set 2"); |
| 137 VbNvClose(&c); |
| 138 |
| 139 /* Test recovery request */ |
| 140 VbNvOpen(&c); |
| 141 TEST_EQ(VbNvGet(&c, VBNV_RECOVERY_REQUEST, &data), 0, "Get recovery request"); |
| 142 TEST_EQ(data, 0, "Default recovery request"); |
| 143 TEST_EQ(VbNvSet(&c, VBNV_RECOVERY_REQUEST, 0x42), 0, "Set recovery request"); |
| 144 VbNvGet(&c, VBNV_RECOVERY_REQUEST, &data); |
| 145 TEST_EQ(data, 0x42, "Set recovery request"); |
| 146 VbNvSet(&c, VBNV_RECOVERY_REQUEST, 0xED); |
| 147 VbNvGet(&c, VBNV_RECOVERY_REQUEST, &data); |
| 148 TEST_EQ(data, 0xED, "Set recovery request 2"); |
| 149 VbNvClose(&c); |
| 150 |
| 151 /* Test localization index */ |
| 152 VbNvOpen(&c); |
| 153 TEST_EQ(VbNvGet(&c, VBNV_LOCALIZATION_INDEX, &data), 0, |
| 154 "Get localization index"); |
| 155 TEST_EQ(data, 0, "Default localization index"); |
| 156 TEST_EQ(VbNvSet(&c, VBNV_LOCALIZATION_INDEX, 0x69), 0, |
| 157 "Set localization index"); |
| 158 VbNvGet(&c, VBNV_LOCALIZATION_INDEX, &data); |
| 159 TEST_EQ(data, 0x69, "Set localization index"); |
| 160 VbNvSet(&c, VBNV_LOCALIZATION_INDEX, 0xB0); |
| 161 VbNvGet(&c, VBNV_LOCALIZATION_INDEX, &data); |
| 162 TEST_EQ(data, 0xB0, "Set localization index 2"); |
| 163 VbNvClose(&c); |
| 164 |
| 165 /* Test kernel field */ |
| 166 VbNvOpen(&c); |
| 167 TEST_EQ(VbNvGet(&c, VBNV_KERNEL_FIELD, &data), 0, "Get kernel field"); |
| 168 TEST_EQ(data, 0, "Default kernel field"); |
| 169 TEST_EQ(VbNvSet(&c, VBNV_KERNEL_FIELD, 0x12345678), 0, "Set kernel field"); |
| 170 VbNvGet(&c, VBNV_KERNEL_FIELD, &data); |
| 171 TEST_EQ(data, 0x12345678, "Set kernel field"); |
| 172 VbNvSet(&c, VBNV_KERNEL_FIELD, 0xFEDCBA98); |
| 173 VbNvGet(&c, VBNV_KERNEL_FIELD, &data); |
| 174 TEST_EQ(data, 0xFEDCBA98, "Set kernel field 2"); |
| 175 VbNvClose(&c); |
| 176 |
| 177 /* None of those changes should have caused a reset to defaults */ |
| 178 VbNvOpen(&c); |
| 179 VbNvGet(&c, VBNV_FIRMWARE_SETTINGS_RESET, &data); |
| 180 TEST_EQ(data, 0, "Firmware settings are still clear"); |
| 181 VbNvGet(&c, VBNV_KERNEL_SETTINGS_RESET, &data); |
| 182 TEST_EQ(data, 0, "Kernel settings are still clear"); |
| 183 VbNvClose(&c); |
| 184 |
| 185 /* Verify writing identical settings doesn't cause the CRC to regenerate */ |
| 186 VbNvOpen(&c); |
| 187 TEST_EQ(c.regenerate_crc, 0, "No regen CRC on open"); |
| 188 VbNvSet(&c, VBNV_DEBUG_RESET_MODE, 1); |
| 189 VbNvSet(&c, VBNV_RECOVERY_REQUEST, 0xED); |
| 190 VbNvSet(&c, VBNV_LOCALIZATION_INDEX, 0xB0); |
| 191 VbNvSet(&c, VBNV_KERNEL_FIELD, 0xFEDCBA98); |
| 192 TEST_EQ(c.regenerate_crc, 0, "No regen CRC if data not changed"); |
| 193 VbNvClose(&c); |
| 194 TEST_EQ(c.raw_changed, 0, "No raw change if data not changed"); |
| 195 } |
| 196 |
| 197 |
| 198 /* disable MSVC warnings on unused arguments */ |
| 199 __pragma(warning (disable: 4100)) |
| 200 |
| 201 int main(int argc, char* argv[]) { |
| 202 int error_code = 0; |
| 203 |
| 204 VbStorageTest(); |
| 205 |
| 206 if (!gTestSuccess) |
| 207 error_code = 255; |
| 208 |
| 209 return error_code; |
| 210 } |
| OLD | NEW |