| Index: tests/vboot_nvstorage_test.c
|
| diff --git a/tests/vboot_nvstorage_test.c b/tests/vboot_nvstorage_test.c
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..bc13cedd916348498936b809b9fa9504eadafd5a
|
| --- /dev/null
|
| +++ b/tests/vboot_nvstorage_test.c
|
| @@ -0,0 +1,210 @@
|
| +/* Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
|
| + * Use of this source code is governed by a BSD-style license that can be
|
| + * found in the LICENSE file.
|
| + *
|
| + * Tests for firmware image library.
|
| + */
|
| +
|
| +#include <stdio.h>
|
| +#include <stdlib.h>
|
| +#include <string.h>
|
| +
|
| +#include "test_common.h"
|
| +#include "vboot_common.h"
|
| +#include "vboot_nvstorage.h"
|
| +
|
| +#if 0
|
| +
|
| +/* Initialize the NV storage library. This must be called before any
|
| + * other functions in this library. Returns 0 if success, non-zero if
|
| + * error. */
|
| +int VbNvOpen(VbNvContext* context);
|
| +
|
| +/* Clean up and flush changes back to the raw data. This must be
|
| + * called after other functions in this library. Caller must check
|
| + * context.raw_changed after calling tis function. Returns 0 if
|
| + * success, non-zero if error. */
|
| +int VbNvClose(VbNvContext* context);
|
| +
|
| +/* Read a NV storage parameter into *dest. Returns 0 if success,
|
| + * non-zero if error. */
|
| +int VbNvGet(VbNvContext* context, VbNvParam param, uint32_t* dest);
|
| +
|
| +/* Set a NV storage param to a new value. Returns 0 if success,
|
| + * non-zero if error. */
|
| +int VbNvSet(VbNvContext* context, VbNvParam param, uint32_t value);
|
| +
|
| +typedef struct VbNvContext {
|
| + /* Raw NV data. Caller must fill this before calling VbNvStart(). */
|
| + uint8_t raw[NV_BLOCK_SIZE];
|
| + /* Flag indicating whether raw data has changed. Set by VbNvStop() if
|
| + * the raw data has changed and needs to be stored to the underlying
|
| + * non-volatile data store. */
|
| + int raw_changed;
|
| +
|
| + /* Internal data for NV storage routines. Caller should not touch
|
| + * these fields. */
|
| + int regenerate_crc;
|
| +
|
| +} VbNvContext;
|
| +
|
| +#endif
|
| +
|
| +static void VbStorageTest(void) {
|
| +
|
| + VbNvContext c;
|
| + uint8_t goodcrc;
|
| + uint32_t data;
|
| +
|
| + memset(&c, 0xA6, sizeof(c));
|
| +
|
| + /* Open with invalid data should set defaults */
|
| + TEST_EQ(VbNvOpen(&c), 0, "VbNvOpen()");
|
| + TEST_EQ(c.raw[0], 0x70, "VbNvOpen() reset header byte");
|
| + /* Close then regenerates the CRC */
|
| + TEST_EQ(VbNvClose(&c), 0, "VbNvClose()");
|
| + TEST_NEQ(c.raw[15], 0, "VbNvClose() CRC");
|
| + TEST_EQ(c.raw_changed, 1, "VbNvClose() changed");
|
| + goodcrc = c.raw[15];
|
| + /* Another open-close pair should not cause further changes */
|
| + VbNvOpen(&c);
|
| + VbNvClose(&c);
|
| + TEST_EQ(c.raw_changed, 0, "VbNvClose() didn't change");
|
| + TEST_EQ(c.raw[15], goodcrc, "VbNvClose() CRC same");
|
| +
|
| + /* Perturbing the header should force defaults */
|
| + c.raw[0] ^= 0x40;
|
| + VbNvOpen(&c);
|
| + TEST_EQ(c.raw[0], 0x70, "VbNvOpen() reset header byte again");
|
| + /* Close then regenerates the CRC */
|
| + VbNvClose(&c);
|
| + TEST_EQ(c.raw_changed, 1, "VbNvClose() changed again");
|
| + TEST_EQ(c.raw[15], goodcrc, "VbNvClose() CRC same again");
|
| +
|
| + /* So should perturbing some other byte */
|
| + TEST_EQ(c.raw[11], 0, "Kernel byte starts at 0");
|
| + c.raw[11] = 12;
|
| + VbNvOpen(&c);
|
| + TEST_EQ(c.raw[11], 0, "VbNvOpen() reset kernel byte");
|
| + /* Close then regenerates the CRC */
|
| + VbNvClose(&c);
|
| + TEST_EQ(c.raw_changed, 1, "VbNvClose() changed again");
|
| + TEST_EQ(c.raw[15], goodcrc, "VbNvClose() CRC same again");
|
| +
|
| + /* Clear the kernel and firmware flags */
|
| + VbNvOpen(&c);
|
| + TEST_EQ(VbNvGet(&c, VBNV_FIRMWARE_SETTINGS_RESET, &data), 0,
|
| + "Get firmware settings reset");
|
| + TEST_EQ(data, 1, "Firmware settings are reset");
|
| + TEST_EQ(VbNvSet(&c, VBNV_FIRMWARE_SETTINGS_RESET, 0), 0,
|
| + "Clear firmware settings reset");
|
| + VbNvGet(&c, VBNV_FIRMWARE_SETTINGS_RESET, &data);
|
| + TEST_EQ(data, 0, "Firmware settings are clear");
|
| +
|
| + TEST_EQ(VbNvGet(&c, VBNV_KERNEL_SETTINGS_RESET, &data), 0,
|
| + "Get kernel settings reset");
|
| + TEST_EQ(data, 1, "Kernel settings are reset");
|
| + TEST_EQ(VbNvSet(&c, VBNV_KERNEL_SETTINGS_RESET, 0), 0,
|
| + "Clear kernel settings reset");
|
| + VbNvGet(&c, VBNV_KERNEL_SETTINGS_RESET, &data);
|
| + TEST_EQ(data, 0, "Kernel settings are clear");
|
| + TEST_EQ(c.raw[0], 0x40, "Header byte now just has the header bit");
|
| + VbNvClose(&c);
|
| + /* That should have changed the CRC */
|
| + TEST_NEQ(c.raw[15], goodcrc, "VbNvClose() CRC changed due to flags clear");
|
| +
|
| + /* Test debug reset mode field */
|
| + VbNvOpen(&c);
|
| + TEST_EQ(VbNvGet(&c, VBNV_DEBUG_RESET_MODE, &data), 0,
|
| + "Get debug reset mode");
|
| + TEST_EQ(data, 0, "Debug reset mode default");
|
| + TEST_EQ(VbNvSet(&c, VBNV_DEBUG_RESET_MODE, 1), 0,
|
| + "Set debug reset mode");
|
| + VbNvGet(&c, VBNV_DEBUG_RESET_MODE, &data);
|
| + TEST_EQ(data, 1, "Debug reset mode set");
|
| + VbNvClose(&c);
|
| +
|
| + /* Test try B count */
|
| + VbNvOpen(&c);
|
| + TEST_EQ(VbNvGet(&c, VBNV_TRY_B_COUNT, &data), 0, "Get try b count");
|
| + TEST_EQ(data, 0, "Try b count default");
|
| + TEST_EQ(VbNvSet(&c, VBNV_TRY_B_COUNT, 6), 0, "Set try b count");
|
| + VbNvGet(&c, VBNV_TRY_B_COUNT, &data);
|
| + TEST_EQ(data, 6, "Try b count set");
|
| + VbNvSet(&c, VBNV_TRY_B_COUNT, 15);
|
| + VbNvGet(&c, VBNV_TRY_B_COUNT, &data);
|
| + TEST_EQ(data, 15, "Try b count set 2");
|
| + VbNvClose(&c);
|
| +
|
| + /* Test recovery request */
|
| + VbNvOpen(&c);
|
| + TEST_EQ(VbNvGet(&c, VBNV_RECOVERY_REQUEST, &data), 0, "Get recovery request");
|
| + TEST_EQ(data, 0, "Default recovery request");
|
| + TEST_EQ(VbNvSet(&c, VBNV_RECOVERY_REQUEST, 0x42), 0, "Set recovery request");
|
| + VbNvGet(&c, VBNV_RECOVERY_REQUEST, &data);
|
| + TEST_EQ(data, 0x42, "Set recovery request");
|
| + VbNvSet(&c, VBNV_RECOVERY_REQUEST, 0xED);
|
| + VbNvGet(&c, VBNV_RECOVERY_REQUEST, &data);
|
| + TEST_EQ(data, 0xED, "Set recovery request 2");
|
| + VbNvClose(&c);
|
| +
|
| + /* Test localization index */
|
| + VbNvOpen(&c);
|
| + TEST_EQ(VbNvGet(&c, VBNV_LOCALIZATION_INDEX, &data), 0,
|
| + "Get localization index");
|
| + TEST_EQ(data, 0, "Default localization index");
|
| + TEST_EQ(VbNvSet(&c, VBNV_LOCALIZATION_INDEX, 0x69), 0,
|
| + "Set localization index");
|
| + VbNvGet(&c, VBNV_LOCALIZATION_INDEX, &data);
|
| + TEST_EQ(data, 0x69, "Set localization index");
|
| + VbNvSet(&c, VBNV_LOCALIZATION_INDEX, 0xB0);
|
| + VbNvGet(&c, VBNV_LOCALIZATION_INDEX, &data);
|
| + TEST_EQ(data, 0xB0, "Set localization index 2");
|
| + VbNvClose(&c);
|
| +
|
| + /* Test kernel field */
|
| + VbNvOpen(&c);
|
| + TEST_EQ(VbNvGet(&c, VBNV_KERNEL_FIELD, &data), 0, "Get kernel field");
|
| + TEST_EQ(data, 0, "Default kernel field");
|
| + TEST_EQ(VbNvSet(&c, VBNV_KERNEL_FIELD, 0x12345678), 0, "Set kernel field");
|
| + VbNvGet(&c, VBNV_KERNEL_FIELD, &data);
|
| + TEST_EQ(data, 0x12345678, "Set kernel field");
|
| + VbNvSet(&c, VBNV_KERNEL_FIELD, 0xFEDCBA98);
|
| + VbNvGet(&c, VBNV_KERNEL_FIELD, &data);
|
| + TEST_EQ(data, 0xFEDCBA98, "Set kernel field 2");
|
| + VbNvClose(&c);
|
| +
|
| + /* None of those changes should have caused a reset to defaults */
|
| + VbNvOpen(&c);
|
| + VbNvGet(&c, VBNV_FIRMWARE_SETTINGS_RESET, &data);
|
| + TEST_EQ(data, 0, "Firmware settings are still clear");
|
| + VbNvGet(&c, VBNV_KERNEL_SETTINGS_RESET, &data);
|
| + TEST_EQ(data, 0, "Kernel settings are still clear");
|
| + VbNvClose(&c);
|
| +
|
| + /* Verify writing identical settings doesn't cause the CRC to regenerate */
|
| + VbNvOpen(&c);
|
| + TEST_EQ(c.regenerate_crc, 0, "No regen CRC on open");
|
| + VbNvSet(&c, VBNV_DEBUG_RESET_MODE, 1);
|
| + VbNvSet(&c, VBNV_RECOVERY_REQUEST, 0xED);
|
| + VbNvSet(&c, VBNV_LOCALIZATION_INDEX, 0xB0);
|
| + VbNvSet(&c, VBNV_KERNEL_FIELD, 0xFEDCBA98);
|
| + TEST_EQ(c.regenerate_crc, 0, "No regen CRC if data not changed");
|
| + VbNvClose(&c);
|
| + TEST_EQ(c.raw_changed, 0, "No raw change if data not changed");
|
| +}
|
| +
|
| +
|
| +/* disable MSVC warnings on unused arguments */
|
| +__pragma(warning (disable: 4100))
|
| +
|
| +int main(int argc, char* argv[]) {
|
| + int error_code = 0;
|
| +
|
| + VbStorageTest();
|
| +
|
| + if (!gTestSuccess)
|
| + error_code = 255;
|
| +
|
| + return error_code;
|
| +}
|
|
|