summaryrefslogtreecommitdiff
path: root/tests/vb2_nvstorage_tests.c
diff options
context:
space:
mode:
Diffstat (limited to 'tests/vb2_nvstorage_tests.c')
-rw-r--r--tests/vb2_nvstorage_tests.c98
1 files changed, 78 insertions, 20 deletions
diff --git a/tests/vb2_nvstorage_tests.c b/tests/vb2_nvstorage_tests.c
index f509b0ab..b4d1d4b6 100644
--- a/tests/vb2_nvstorage_tests.c
+++ b/tests/vb2_nvstorage_tests.c
@@ -19,6 +19,7 @@
#include "2common.h"
#include "2misc.h"
#include "2nvstorage.h"
+#include "2nvstorage_fields.h"
/* Single NV storage field to test */
struct nv_field {
@@ -63,6 +64,13 @@ static struct nv_field nvfields[] = {
{0, 0, 0, 0, NULL}
};
+/* Fields added in v2. The test_value field is the default returned for V1. */
+static struct nv_field nv2fields[] = {
+ {VB2_NV_FW_MAX_ROLLFORWARD, 0, VB2_FW_MAX_ROLLFORWARD_V1_DEFAULT,
+ 0x87654321, "firmware max rollforward"},
+ {0, 0, 0, 0, NULL}
+};
+
static void test_changed(struct vb2_context *ctx, int changed, const char *why)
{
if (changed)
@@ -71,58 +79,73 @@ static void test_changed(struct vb2_context *ctx, int changed, const char *why)
TEST_EQ(ctx->flags & VB2_CONTEXT_NVDATA_CHANGED, 0, why);
};
-static void nv_storage_test(void)
+static void nv_storage_test(uint32_t ctxflags)
{
struct nv_field *vnf;
uint8_t goodcrc;
uint8_t workbuf[VB2_WORKBUF_RECOMMENDED_SIZE]
__attribute__ ((aligned (VB2_WORKBUF_ALIGN)));
struct vb2_context c = {
- .flags = 0,
+ .flags = ctxflags,
.workbuf = workbuf,
.workbuf_size = sizeof(workbuf),
};
struct vb2_shared_data *sd = vb2_get_sd(&c);
+ /* Things that change between V1 and V2 */
+ int expect_header = 0x30 | (ctxflags ? VB2_NV_HEADER_SIGNATURE_V2 :
+ VB2_NV_HEADER_SIGNATURE_V1);
+ int crc_offs = ctxflags ? VB2_NV_OFFS_CRC_V2 : VB2_NV_OFFS_CRC_V1;
+
+ TEST_EQ(vb2_nv_get_size(&c), ctxflags ? VB2_NVDATA_SIZE_V2 :
+ VB2_NVDATA_SIZE, "vb2_nv_get_size()");
+
memset(c.nvdata, 0xA6, sizeof(c.nvdata));
vb2_init_context(&c);
/* Init with invalid data should set defaults and regenerate CRC */
vb2_nv_init(&c);
- TEST_EQ(c.nvdata[0], 0x70, "vb2_nv_init() reset header byte");
- TEST_NEQ(c.nvdata[15], 0, "vb2_nv_init() CRC");
+ TEST_EQ(c.nvdata[VB2_NV_OFFS_HEADER], expect_header,
+ "vb2_nv_init() reset header byte");
+ TEST_NEQ(c.nvdata[crc_offs], 0, "vb2_nv_init() CRC");
TEST_EQ(sd->status, VB2_SD_STATUS_NV_INIT | VB2_SD_STATUS_NV_REINIT,
"vb2_nv_init() status changed");
test_changed(&c, 1, "vb2_nv_init() reset changed");
- goodcrc = c.nvdata[15];
+ goodcrc = c.nvdata[crc_offs];
TEST_SUCC(vb2_nv_check_crc(&c), "vb2_nv_check_crc() good");
/* Another init should not cause further changes */
- c.flags = 0;
+ c.flags = ctxflags;
sd->status = 0;
vb2_nv_init(&c);
test_changed(&c, 0, "vb2_nv_init() didn't re-reset");
- TEST_EQ(c.nvdata[15], goodcrc, "vb2_nv_init() CRC same");
- TEST_EQ(sd->status, VB2_SD_STATUS_NV_INIT, "vb2_nv_init() status same");
+ TEST_EQ(c.nvdata[crc_offs], goodcrc,
+ "vb2_nv_init() CRC same");
+ TEST_EQ(sd->status, VB2_SD_STATUS_NV_INIT,
+ "vb2_nv_init() status same");
- /* Perturbing the header should force defaults */
- c.nvdata[0] ^= 0x40;
+ /* Perturbing signature bits in the header should force defaults */
+ c.nvdata[VB2_NV_OFFS_HEADER] ^= 0x40;
TEST_EQ(vb2_nv_check_crc(&c),
VB2_ERROR_NV_HEADER, "vb2_nv_check_crc() bad header");
vb2_nv_init(&c);
- TEST_EQ(c.nvdata[0], 0x70, "vb2_nv_init() reset header byte again");
+ TEST_EQ(c.nvdata[VB2_NV_OFFS_HEADER], expect_header,
+ "vb2_nv_init() reset header byte again");
test_changed(&c, 1, "vb2_nv_init() corrupt changed");
- TEST_EQ(c.nvdata[15], goodcrc, "vb2_nv_init() CRC same again");
+ TEST_EQ(c.nvdata[crc_offs], goodcrc,
+ "vb2_nv_init() CRC same again");
/* So should perturbing some other byte */
- TEST_EQ(c.nvdata[11], 0, "Kernel byte starts at 0");
- c.nvdata[11] = 12;
+ TEST_EQ(c.nvdata[VB2_NV_OFFS_KERNEL1], 0, "Kernel byte starts at 0");
+ c.nvdata[VB2_NV_OFFS_KERNEL1] = 12;
TEST_EQ(vb2_nv_check_crc(&c),
VB2_ERROR_NV_CRC, "vb2_nv_check_crc() bad CRC");
vb2_nv_init(&c);
- TEST_EQ(c.nvdata[11], 0, "vb2_nv_init() reset kernel byte");
+ TEST_EQ(c.nvdata[VB2_NV_OFFS_KERNEL1], 0,
+ "vb2_nv_init() reset kernel byte");
test_changed(&c, 1, "vb2_nv_init() corrupt elsewhere changed");
- TEST_EQ(c.nvdata[15], goodcrc, "vb2_nv_init() CRC same again");
+ TEST_EQ(c.nvdata[crc_offs], goodcrc,
+ "vb2_nv_init() CRC same again");
/* Clear the kernel and firmware flags */
vb2_nv_init(&c);
@@ -138,9 +161,11 @@ static void nv_storage_test(void)
TEST_EQ(vb2_nv_get(&c, VB2_NV_KERNEL_SETTINGS_RESET),
0, "Kernel settings are clear");
- TEST_EQ(c.nvdata[0], 0x40, "Header byte now just has the header bit");
+ TEST_EQ(c.nvdata[VB2_NV_OFFS_HEADER],
+ expect_header & VB2_NV_HEADER_SIGNATURE_MASK,
+ "Header byte now just has the signature");
/* That should have changed the CRC */
- TEST_NEQ(c.nvdata[15], goodcrc,
+ TEST_NEQ(c.nvdata[crc_offs], goodcrc,
"vb2_nv_init() CRC changed due to flags clear");
/* Test explicitly setting the reset flags again */
@@ -172,6 +197,29 @@ static void nv_storage_test(void)
vnf->desc);
}
+ for (vnf = nv2fields; vnf->desc; vnf++) {
+ if (ctxflags) {
+ TEST_EQ(vb2_nv_get(&c, vnf->param), vnf->default_value,
+ vnf->desc);
+ vb2_nv_set(&c, vnf->param, vnf->test_value);
+ TEST_EQ(vb2_nv_get(&c, vnf->param), vnf->test_value,
+ vnf->desc);
+ vb2_nv_set(&c, vnf->param, vnf->test_value2);
+ TEST_EQ(vb2_nv_get(&c, vnf->param), vnf->test_value2,
+ vnf->desc);
+ } else {
+ /*
+ * V2 fields always return defaults and can't be set if
+ * a V1 struct is present.
+ */
+ TEST_EQ(vb2_nv_get(&c, vnf->param), vnf->test_value,
+ vnf->desc);
+ vb2_nv_set(&c, vnf->param, vnf->test_value2);
+ TEST_EQ(vb2_nv_get(&c, vnf->param), vnf->test_value,
+ vnf->desc);
+ }
+ }
+
/* None of those changes should have caused a reset to defaults */
vb2_nv_init(&c);
TEST_EQ(vb2_nv_get(&c, VB2_NV_FIRMWARE_SETTINGS_RESET),
@@ -180,12 +228,19 @@ static void nv_storage_test(void)
0, "Kernel settings are still clear");
/* Writing identical settings doesn't cause the CRC to regenerate */
- c.flags = 0;
+ c.flags = ctxflags;
vb2_nv_init(&c);
test_changed(&c, 0, "No regen CRC on open");
for (vnf = nvfields; vnf->desc; vnf++)
vb2_nv_set(&c, vnf->param, vnf->test_value2);
test_changed(&c, 0, "No regen CRC if data not changed");
+ /*
+ * If struct is V2, this is the same test. If struct is V1, this
+ * verifies that the field couldn't be changed anyway.
+ */
+ for (vnf = nv2fields; vnf->desc; vnf++)
+ vb2_nv_set(&c, vnf->param, vnf->test_value2);
+ test_changed(&c, 0, "No regen CRC if V2 data not changed");
/* Test out-of-range fields mapping to defaults or failing */
vb2_nv_init(&c);
@@ -215,7 +270,10 @@ static void nv_storage_test(void)
int main(int argc, char* argv[])
{
- nv_storage_test();
+ printf("Testing V1\n");
+ nv_storage_test(0);
+ printf("Testing V2\n");
+ nv_storage_test(VB2_CONTEXT_NVDATA_V2);
return gTestSuccess ? 0 : 255;
}