summaryrefslogtreecommitdiff
path: root/tests/vb2_host_nvdata_flashrom_tests.c
blob: 80438a4352cb93f7a10d340716005e36b2fd0a02 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/* Copyright 2020 The ChromiumOS Authors
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 *
 * Tests for crossystem flashrom-based nvdata functions.
 */

#include <fcntl.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "2api.h"
#include "2common.h"
#include "2constants.h"
#include "2nvstorage.h"
#include "2return_codes.h"
#include "common/tests.h"
#include "crossystem_vbnv.h"
#include "flashrom.h"

/* Mocked flashrom only supports host programmer, and RW_NVRAM
   region. */
static void assert_mock_params(const char *programmer, const char *region)
{
	TEST_STR_EQ(programmer, FLASHROM_PROGRAMMER_INTERNAL_AP,
		    "Using internal AP programmer");
	TEST_STR_EQ(region, "RW_NVRAM", "Using NVRAM region");
}

static bool mock_flashrom_fail;

/* To support both 16-byte and 64-byte nvdata with the same fake
   eeprom, we can size the flash chip to be 16x64. So, for 16-byte
   nvdata, this is a flash chip with 64 entries, and for 64-byte
   nvdata, this is a flash chip with 16 entries. */
static uint8_t fake_flash_region[VB2_NVDATA_SIZE * VB2_NVDATA_SIZE_V2];
static int fake_flash_entry_count;

static const uint8_t test_nvdata_16b[] = {
	0x60, 0x10, 0x00, 0x00, 0x00, 0x02, 0x00, 0x4e,
	0x00, 0xfe, 0xff, 0x00, 0x00, 0xff, 0xff, 0x5e,
};

static const uint8_t test_nvdata2_16b[] = {
	0x60, 0x10, 0x00, 0x00, 0x00, 0x02, 0x00, 0x4c,
	0x00, 0xfe, 0xff, 0x00, 0x00, 0xff, 0xff, 0x78,
};

static void reset_test_data(struct vb2_context *ctx, int nvdata_size)
{
	/* Initialize the context value. */
	ctx->flags = 0;

	switch (nvdata_size) {
	case VB2_NVDATA_SIZE:
		fake_flash_entry_count = VB2_NVDATA_SIZE_V2;
		memcpy(ctx->nvdata, test_nvdata_16b, sizeof(test_nvdata_16b));
		break;
	case VB2_NVDATA_SIZE_V2:
		ctx->flags |= VB2_CONTEXT_NVDATA_V2;
		fake_flash_entry_count = VB2_NVDATA_SIZE;
		/* TODO: create some test data for 64-byte nvdata and
		   put it here. Right now, this only tests 16-byte
		   nvdata. */
		break;
	default:
		/* This is not valid. */
		TEST_TRUE(false, "Test failed, invalid nvdata size");
		fake_flash_entry_count = 0;
		break;
	}

	/* Clear the fake flash chip. */
	memset(fake_flash_region, 0xff, sizeof(fake_flash_region));

	/* Flashrom succeeds unless the test says otherwise. */
	mock_flashrom_fail = false;
}

/* Mocked flashrom_read for tests. */
vb2_error_t flashrom_read(struct firmware_image *image, const char *region)
{
	if (mock_flashrom_fail) {
		image->data = NULL;
		image->size = 0;
		return VB2_ERROR_FLASHROM;
	}

	assert_mock_params(image->programmer, region);

	image->data = malloc(sizeof(fake_flash_region));
	image->size = sizeof(fake_flash_region);
	memcpy(image->data, fake_flash_region, sizeof(fake_flash_region));
	return VB2_SUCCESS;
}

/* Mocked flashrom_write for tests. */
vb2_error_t flashrom_write(struct firmware_image *image, const char *region)
{
	if (mock_flashrom_fail)
		return VB2_ERROR_FLASHROM;

	assert_mock_params(image->programmer, region);

	TEST_EQ(image->size, sizeof(fake_flash_region),
		"The flash size is correct");
	memcpy(fake_flash_region, image->data, image->size);
	return VB2_SUCCESS;
}

static void test_read_ok_beginning(void)
{
	struct vb2_context ctx;

	reset_test_data(&ctx, sizeof(test_nvdata_16b));
	memcpy(fake_flash_region, test_nvdata2_16b, sizeof(test_nvdata2_16b));

	TEST_EQ(vb2_read_nv_storage_flashrom(&ctx), 0,
		"Reading storage succeeds");
	TEST_EQ(memcmp(ctx.nvdata, test_nvdata2_16b, sizeof(test_nvdata2_16b)),
		0, "The nvdata in the vb2_context was updated from flash");
}

static void test_read_ok_2ndentry(void)
{
	struct vb2_context ctx;

	reset_test_data(&ctx, sizeof(test_nvdata_16b));
	memcpy(fake_flash_region, test_nvdata_16b, sizeof(test_nvdata_16b));
	memcpy(fake_flash_region + VB2_NVDATA_SIZE, test_nvdata2_16b,
	       sizeof(test_nvdata2_16b));

	TEST_EQ(vb2_read_nv_storage_flashrom(&ctx), 0,
		"Reading storage succeeds");
	TEST_EQ(memcmp(ctx.nvdata, test_nvdata2_16b, sizeof(test_nvdata2_16b)),
		0, "The nvdata in the vb2_context was updated from flash");
}

static void test_read_ok_full(void)
{
	struct vb2_context ctx;

	reset_test_data(&ctx, sizeof(test_nvdata_16b));

	for (int entry = 0; entry < fake_flash_entry_count - 1; entry++)
		memcpy(fake_flash_region + (entry * VB2_NVDATA_SIZE),
		       test_nvdata_16b, sizeof(test_nvdata_16b));

	memcpy(fake_flash_region +
	       ((fake_flash_entry_count - 1) * VB2_NVDATA_SIZE),
	       test_nvdata2_16b, sizeof(test_nvdata2_16b));

	TEST_EQ(vb2_read_nv_storage_flashrom(&ctx), 0,
		"Reading storage succeeds");
	TEST_EQ(memcmp(ctx.nvdata, test_nvdata2_16b, sizeof(test_nvdata2_16b)),
		0, "The nvdata in the vb2_context was updated from flash");
}

static void test_read_fail_uninitialized(void)
{
	struct vb2_context ctx;

	reset_test_data(&ctx, sizeof(test_nvdata_16b));

	TEST_NEQ(vb2_read_nv_storage_flashrom(&ctx), 0,
		 "Reading storage fails when flash is erased");
}

static void test_read_fail_flashrom(void)
{
	struct vb2_context ctx;

	reset_test_data(&ctx, sizeof(test_nvdata_16b));
	memcpy(fake_flash_region, test_nvdata_16b, sizeof(test_nvdata_16b));
	mock_flashrom_fail = true;

	TEST_NEQ(vb2_read_nv_storage_flashrom(&ctx), 0,
		 "Reading storage fails when flashrom fails");
}

static void test_write_ok_beginning(void)
{
	struct vb2_context ctx;

	reset_test_data(&ctx, sizeof(test_nvdata_16b));
	memcpy(fake_flash_region, test_nvdata_16b, sizeof(test_nvdata_16b));
	memcpy(ctx.nvdata, test_nvdata2_16b, sizeof(test_nvdata2_16b));

	TEST_EQ(vb2_write_nv_storage_flashrom(&ctx), 0,
		"Writing storage succeeds");
	TEST_EQ(memcmp(fake_flash_region + VB2_NVDATA_SIZE, test_nvdata2_16b,
		       sizeof(test_nvdata2_16b)),
		0, "The flash was updated with a new entry");
}

static void test_write_ok_2ndentry(void)
{
	struct vb2_context ctx;

	reset_test_data(&ctx, sizeof(test_nvdata_16b));
	memcpy(fake_flash_region, test_nvdata_16b, sizeof(test_nvdata_16b));
	memcpy(fake_flash_region + VB2_NVDATA_SIZE, test_nvdata_16b,
	       sizeof(test_nvdata_16b));
	memcpy(ctx.nvdata, test_nvdata2_16b, sizeof(test_nvdata2_16b));

	TEST_EQ(vb2_write_nv_storage_flashrom(&ctx), 0,
		"Writing storage succeeds");
	TEST_EQ(memcmp(fake_flash_region + (2 * VB2_NVDATA_SIZE),
		       test_nvdata2_16b, sizeof(test_nvdata2_16b)),
		0, "The flash was updated with a new entry");
}

static void test_write_ok_full(void)
{
	struct vb2_context ctx;
	uint8_t expected_flash[sizeof(fake_flash_region)];

	reset_test_data(&ctx, sizeof(test_nvdata_16b));

	for (int entry = 0; entry < fake_flash_entry_count; entry++)
		memcpy(fake_flash_region + (entry * VB2_NVDATA_SIZE),
		       test_nvdata_16b, sizeof(test_nvdata_16b));

	memcpy(expected_flash, test_nvdata2_16b, sizeof(test_nvdata2_16b));
	memset(expected_flash + VB2_NVDATA_SIZE, 0xff,
	       sizeof(expected_flash) - VB2_NVDATA_SIZE);
	memcpy(ctx.nvdata, test_nvdata2_16b, sizeof(test_nvdata2_16b));

	TEST_EQ(vb2_write_nv_storage_flashrom(&ctx), 0,
		"Writing storage succeeds");
	TEST_EQ(memcmp(fake_flash_region, expected_flash,
		       sizeof(expected_flash)),
		0,
		"The flash was erased and the new entry was placed at "
		"the beginning");
}

static void test_write_fail_uninitialized(void)
{
	struct vb2_context ctx;

	reset_test_data(&ctx, sizeof(test_nvdata_16b));

	TEST_NEQ(vb2_write_nv_storage_flashrom(&ctx), 0,
		 "Writing storage fails when the flash is erased");
}

static void test_write_fail_flashrom(void)
{
	struct vb2_context ctx;

	reset_test_data(&ctx, sizeof(test_nvdata_16b));
	memcpy(fake_flash_region, test_nvdata_16b, sizeof(test_nvdata_16b));
	mock_flashrom_fail = true;

	TEST_NEQ(vb2_write_nv_storage_flashrom(&ctx), 0,
		 "Writing storage fails when flashrom fails");
}

int main(int argc, char *argv[])
{
	test_read_ok_beginning();
	test_read_ok_2ndentry();
	test_read_ok_full();
	test_read_fail_uninitialized();
	test_read_fail_flashrom();
	test_write_ok_beginning();
	test_write_ok_2ndentry();
	test_write_ok_full();
	test_write_fail_uninitialized();
	test_write_fail_flashrom();

	return gTestSuccess ? 0 : 255;
}