summaryrefslogtreecommitdiff
path: root/test/nvmem_vars.c
blob: 99e059215ef7f721bb218737906be5a442fafb5c (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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
/* Copyright 2016 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.
 *
 * Test of the key=val variable implementation (set, get, delete, etc).
 */

#include "common.h"
#include "compile_time_macros.h"
#include "nvmem.h"
#include "nvmem_vars.h"
#include "printf.h"
#include "shared_mem.h"
#include "test_util.h"

/* Declare the user regions (see test_config.h) */
uint32_t nvmem_user_sizes[] = {
	CONFIG_FLASH_NVMEM_VARS_USER_SIZE,
};
BUILD_ASSERT(ARRAY_SIZE(nvmem_user_sizes) == NVMEM_NUM_USERS);

/****************************************************************************/
/* Mock the flash storage */

static uint8_t ram_buffer[CONFIG_FLASH_NVMEM_VARS_USER_SIZE];
static uint8_t flash_buffer[CONFIG_FLASH_NVMEM_VARS_USER_SIZE];

extern char *rbuf;

/* Internal functions exported for test */
void release_local_copy(void)
{
	rbuf = NULL;
}

int get_local_copy(void)
{
	if (!rbuf) {
		memcpy(ram_buffer, flash_buffer, sizeof(ram_buffer));
		rbuf = (char *)ram_buffer;
	}
	return EC_SUCCESS;
}

int nvmem_read(uint32_t startOffset, uint32_t size,
	       void *data_, enum nvmem_users user)
{
	/* Our mocks make some assumptions */
	if (startOffset != 0 ||
	    size > CONFIG_FLASH_NVMEM_VARS_USER_SIZE ||
	    user != CONFIG_FLASH_NVMEM_VARS_USER_NUM)
		return EC_ERROR_UNIMPLEMENTED;

	if (!data_)
		return EC_ERROR_INVAL;

	memcpy(data_, flash_buffer, size);

	return EC_SUCCESS;
}

int nvmem_write(uint32_t startOffset, uint32_t size,
		void *data_, enum nvmem_users user)
{
	/* Our mocks make some assumptions */
	if (startOffset != 0 ||
	    size > CONFIG_FLASH_NVMEM_VARS_USER_SIZE ||
	    user != CONFIG_FLASH_NVMEM_VARS_USER_NUM)
		return EC_ERROR_UNIMPLEMENTED;

	if (!data_)
		return EC_ERROR_INVAL;

	memcpy(ram_buffer, data_, size);

	return EC_SUCCESS;
}

int nvmem_commit(void)
{
	memcpy(flash_buffer, ram_buffer, CONFIG_FLASH_NVMEM_VARS_USER_SIZE);
	return EC_SUCCESS;
}

int nvmem_erase_user_data(enum nvmem_users user)
{
	memset(ram_buffer, 0xff, sizeof(ram_buffer));
	memset(flash_buffer, 0xff, sizeof(flash_buffer));
	return EC_SUCCESS;
}

/****************************************************************************/
/* Helper routines */

static void erase_flash(void)
{
	/* Invalidate the RAM cache */
	release_local_copy();

	/* Zero flash */
	memset(flash_buffer, 0xff, sizeof(flash_buffer));
}

/* Erase flash, then copy data_ over it */
static void load_flash(const uint8_t *data_, size_t data_len)
{
	erase_flash();
	memcpy(flash_buffer, data_, data_len);
}

/* Return true if flash matches data_, and is followed by 0xff to the end */
static int verify_flash(const uint8_t *data_, size_t data_len)
{
	size_t i;

	/* mismatch means false */
	if (memcmp(flash_buffer, data_, data_len))
		return 0;

	for (i = data_len;
	     i < CONFIG_FLASH_NVMEM_VARS_USER_SIZE - data_len;
	     i++)
		if (flash_buffer[i] != 0xff)
			return 0;
	return 1;
}

/*
 * Treating both as strings, save the <key, value> pair.
 */
int str_setvar(const char *key, const char *val)
{
	/* Only for tests, so assume the length will fit */
	uint8_t key_len, val_len;

	key_len = strlen(key);
	val_len = val ? strlen(val) : 0;

	return setvar(key, key_len, val, val_len);
}

/*
 * Treating both as strings, lookup the key and compare the result with the
 * expected value. Return true if they match.
 */
static int str_matches(const char *key, const char *expected_val)
{
	const struct tuple *t = getvar(key, strlen(key));
	uint8_t expected_len;

	if (!expected_val && !t)
		return 1;

	if (expected_val && !t)
		return 0;

	if (!expected_val && t)
		return 0;

	expected_len = strlen(expected_val);
	return !memcmp(tuple_val(t), expected_val, expected_len);
}

/****************************************************************************/
/* Tests */

static int check_init(void)
{
	/* Valid entries */
	const uint8_t good[] = { 0x01, 0x01, 0x00, 'A', 'a',
				 0x01, 0x01, 0x00, 'B', 'b',
				 0x00 };

	/* Empty variables are 0x00, followed by all 0xff */
	const uint8_t empty[] = { 0x00 };

	/*
	 * This is parsed as though there's only one variable, but it's wrong
	 * because the rest of the storage isn't 0xff.
	 */
	const uint8_t bad_key[] = { 0x01, 0x01, 0x00, 'A', 'a',
				    0x00, 0x01, 0x00, 'B', 'b',
				    0x00 };

	/* Zero-length variables are not allowed */
	const uint8_t bad_val[] = { 0x01, 0x01, 0x00, 'A', 'a',
				    0x01, 0x00, 0x00, 'B', 'b',
				    0x00 };

	/* The next constants use magic numbers based on on the region size */
	BUILD_ASSERT(CONFIG_FLASH_NVMEM_VARS_USER_SIZE == 600);

	/* This is one byte too large */
	const uint8_t too_big[] = { [0] = 0xff, [1] = 0xff, /* 0 - 512 */
				    [513] = 0x01, [514] = 0x53, /* 513 - 599 */
				    [599] = 0x00 };

	/* This should just barely fit */
	const uint8_t just_right[] = { [0] = 0xff, [1] = 0xff, /* 0-512 */
				       [513] = 0x01, [514] = 0x52, /* 513-598 */
				       [599] = 0x00 };

	/* No end marker */
	const uint8_t not_right[] = { [0] = 0xff, [1] = 0xff, /* 0-512 */
				      [513] = 0x01, [514] = 0x52, /* 513-598 */
				      [599] = 0xff };

	load_flash(good, sizeof(good));
	TEST_ASSERT(initvars() == EC_SUCCESS);
	TEST_ASSERT(verify_flash(good, sizeof(good)));

	load_flash(empty, sizeof(empty));
	TEST_ASSERT(initvars() == EC_SUCCESS);
	TEST_ASSERT(verify_flash(empty, sizeof(empty)));

	/* All 0xff quickly runs off the end of the storage */
	erase_flash();
	TEST_ASSERT(initvars() == EC_SUCCESS);
	TEST_ASSERT(verify_flash(empty, sizeof(empty)));

	load_flash(bad_key, sizeof(bad_key));
	TEST_ASSERT(initvars() == EC_SUCCESS);
	TEST_ASSERT(verify_flash(empty, sizeof(empty)));

	load_flash(bad_val, sizeof(bad_val));
	TEST_ASSERT(initvars() == EC_SUCCESS);
	TEST_ASSERT(verify_flash(empty, sizeof(empty)));

	load_flash(too_big, sizeof(too_big));
	TEST_ASSERT(initvars() == EC_SUCCESS);
	TEST_ASSERT(verify_flash(empty, sizeof(empty)));

	load_flash(just_right, sizeof(just_right));
	TEST_ASSERT(initvars() == EC_SUCCESS);
	TEST_ASSERT(verify_flash(just_right, sizeof(just_right)));

	load_flash(not_right, sizeof(not_right));
	TEST_ASSERT(initvars() == EC_SUCCESS);
	TEST_ASSERT(verify_flash(empty, sizeof(empty)));

	return EC_SUCCESS;
}

static int simple_search(void)
{
	const uint8_t preload[] = {
		0x02, 0x02, 0x00, 'h', 'o',  'y', 'o',
		0x02, 0x4,  0x00, 'y', 'o',  'h', 'o', 'y', 'o',
		0x02, 0x06, 0x00, 'm', 'o',  'y', 'o', 'h', 'o', 'y', 'o',
		0x00 };

	load_flash(preload, sizeof(preload));
	TEST_ASSERT(initvars() == EC_SUCCESS);
	TEST_ASSERT(verify_flash(preload, sizeof(preload)));

	TEST_ASSERT(str_matches("no", 0));
	TEST_ASSERT(str_matches("ho", "yo"));
	TEST_ASSERT(str_matches("yo", "hoyo"));
	TEST_ASSERT(str_matches("mo", "yohoyo"));

	return EC_SUCCESS;
}

static int simple_write(void)
{
	const uint8_t after_one[] = {
		0x02, 0x02, 0x00, 'h', 'o',  'y', 'o',
		0x00 };

	const uint8_t after_two[] = {
		0x02, 0x02, 0x00, 'h', 'o',  'y', 'o',
		0x02, 0x4,  0x00, 'y', 'o',  'h', 'o', 'y', 'o',
		0x00 };

	const uint8_t after_three[] = {
		0x02, 0x02, 0x00, 'h', 'o',  'y', 'o',
		0x02, 0x4,  0x00, 'y', 'o',  'h', 'o', 'y', 'o',
		0x02, 0x06, 0x00, 'm', 'o',  'y', 'o', 'h', 'o', 'y', 'o',
		0x00 };

	erase_flash();
	TEST_ASSERT(initvars() == EC_SUCCESS);

	TEST_ASSERT(setvar("ho", 2, "yo", 2) == EC_SUCCESS);
	TEST_ASSERT(writevars() == EC_SUCCESS);
	TEST_ASSERT(verify_flash(after_one, sizeof(after_one)));

	TEST_ASSERT(setvar("yo", 2, "hoyo", 4) == EC_SUCCESS);
	TEST_ASSERT(writevars() == EC_SUCCESS);
	TEST_ASSERT(verify_flash(after_two, sizeof(after_two)));

	TEST_ASSERT(setvar("mo", 2, "yohoyo", 6) == EC_SUCCESS);
	TEST_ASSERT(writevars() == EC_SUCCESS);
	TEST_ASSERT(verify_flash(after_three, sizeof(after_three)));

	return EC_SUCCESS;
}

static int simple_delete(void)
{
	const char start_with[] = {
		0x01, 0x05, 0x00, 'A', 'a',  'a', 'a', 'a', 'a',
		0x02, 0x03, 0x00, 'B', 'B',  'b', 'b', 'b',
		0x03, 0x06, 0x00, 'C', 'C',  'C', 'x', 'y', 'z', 'p', 'd', 'q',
		0x01, 0x03, 0x00, 'M', 'm',  '0', 'm',
		0x04, 0x01, 0x00, 'N', 'N',  'N', 'N', 'n',
		0x00 };

	const char after_one[] = {
		0x02, 0x03, 0x00, 'B', 'B',  'b', 'b', 'b',
		0x03, 0x06, 0x00, 'C', 'C',  'C', 'x', 'y', 'z', 'p', 'd', 'q',
		0x01, 0x03, 0x00, 'M', 'm',  '0', 'm',
		0x04, 0x01, 0x00, 'N', 'N',  'N', 'N', 'n',
		0x00 };

	const char after_two[] = {
		0x02, 0x03, 0x00, 'B', 'B',  'b', 'b', 'b',
		0x03, 0x06, 0x00, 'C', 'C',  'C', 'x', 'y', 'z', 'p', 'd', 'q',
		0x01, 0x03, 0x00, 'M', 'm',  '0', 'm',
		0x00 };

	const char after_three[] = {
		0x02, 0x03, 0x00, 'B', 'B',  'b', 'b', 'b',
		0x01, 0x03, 0x00, 'M', 'm',  '0', 'm',
		0x00 };

	const char empty[] = { 0x00 };

	erase_flash();
	TEST_ASSERT(initvars() == EC_SUCCESS);

	TEST_ASSERT(setvar("A", 1, "aaaaa", 5) == EC_SUCCESS);
	TEST_ASSERT(setvar("BB", 2, "bbb", 3) == EC_SUCCESS);
	TEST_ASSERT(setvar("CCC", 3, "xyzpdq", 6) == EC_SUCCESS);
	TEST_ASSERT(setvar("M", 1, "m0m", 3) == EC_SUCCESS);
	TEST_ASSERT(setvar("NNNN", 4, "n", 1) == EC_SUCCESS);
	TEST_ASSERT(writevars() == EC_SUCCESS);
	TEST_ASSERT(verify_flash(start_with, sizeof(start_with)));

	/* Zap first variable by setting var_len to 0 */
	TEST_ASSERT(setvar("A", 1, "yohoyo", 0) == EC_SUCCESS);
	TEST_ASSERT(writevars() == EC_SUCCESS);
	TEST_ASSERT(verify_flash(after_one, sizeof(after_one)));

	/* Zap last variable by passing null pointer */
	TEST_ASSERT(setvar("NNNN", 4, 0, 3) == EC_SUCCESS);
	TEST_ASSERT(writevars() == EC_SUCCESS);
	TEST_ASSERT(verify_flash(after_two, sizeof(after_two)));

	/* Ensure that zapping nonexistant variable does nothing */
	TEST_ASSERT(setvar("XXX", 3, 0, 0) == EC_SUCCESS);
	TEST_ASSERT(writevars() == EC_SUCCESS);
	TEST_ASSERT(verify_flash(after_two, sizeof(after_two)));

	/* Zap variable in the middle */
	TEST_ASSERT(setvar("CCC", 3, 0, 0) == EC_SUCCESS);
	TEST_ASSERT(writevars() == EC_SUCCESS);
	TEST_ASSERT(verify_flash(after_three, sizeof(after_three)));

	/* Zap the rest */
	TEST_ASSERT(setvar("BB", 2, 0, 0) == EC_SUCCESS);
	TEST_ASSERT(setvar("M", 1, 0, 0) == EC_SUCCESS);
	TEST_ASSERT(writevars() == EC_SUCCESS);
	TEST_ASSERT(verify_flash(empty, sizeof(empty)));

	/* Zapping a nonexistant variable still does nothing */
	TEST_ASSERT(setvar("XXX", 3, 0, 0) == EC_SUCCESS);
	TEST_ASSERT(writevars() == EC_SUCCESS);
	TEST_ASSERT(verify_flash(empty, sizeof(empty)));

	return EC_SUCCESS;
}

static int complex_write(void)
{
	erase_flash();
	TEST_ASSERT(initvars() == EC_SUCCESS);

	/* Do a bunch of writes and erases */
	str_setvar("ho", "aa");
	str_setvar("zo", "nn");
	str_setvar("yo", "CCCCCCCC");
	str_setvar("zooo", "yyyyyyy");
	str_setvar("yo", "AA");
	str_setvar("ho", 0);
	str_setvar("yi", "BBB");
	str_setvar("yi", "AA");
	str_setvar("hixx", 0);
	str_setvar("yo", "BBB");
	str_setvar("zo", "");
	str_setvar("hi", "bbb");
	str_setvar("ho", "cccccc");
	str_setvar("yo", "");
	str_setvar("zo", "ggggg");

	/* What do we expect to find? */
	TEST_ASSERT(str_matches("hi", "bbb"));
	TEST_ASSERT(str_matches("hixx", 0));
	TEST_ASSERT(str_matches("ho", "cccccc"));
	TEST_ASSERT(str_matches("yi", "AA"));
	TEST_ASSERT(str_matches("yo", 0));
	TEST_ASSERT(str_matches("zo", "ggggg"));
	TEST_ASSERT(str_matches("zooo", "yyyyyyy"));

	return EC_SUCCESS;
}

static int weird_keys(void)
{
	uint8_t keyA[255];
	uint8_t keyB[255];
	const char *valA = "this is A";
	const char *valB = "THIS IS b";
	int i;
	const struct tuple *t;

	erase_flash();
	TEST_ASSERT(initvars() == EC_SUCCESS);

	for (i = 0; i < 255; i++) {
		keyA[i] = i;
		keyB[i] = 255 - i;
	}

	TEST_ASSERT(setvar(keyA, sizeof(keyA),
			   valA, strlen(valA)) == EC_SUCCESS);

	TEST_ASSERT(setvar(keyB, sizeof(keyB),
			   valB, strlen(valB)) == EC_SUCCESS);

	TEST_ASSERT(writevars() == EC_SUCCESS);

	t = getvar(keyA, sizeof(keyA));
	TEST_ASSERT(t);
	TEST_ASSERT(t->val_len == strlen(valA));
	TEST_ASSERT(memcmp(tuple_val(t), valA, strlen(valA)) == 0);

	t = getvar(keyB, sizeof(keyB));
	TEST_ASSERT(t);
	TEST_ASSERT(t->val_len == strlen(valB));
	TEST_ASSERT(memcmp(tuple_val(t), valB, strlen(valB)) == 0);

	return EC_SUCCESS;
}

static int weird_values(void)
{
	const char *keyA = "this is A";
	const char *keyB = "THIS IS b";
	char valA[255];
	char valB[255];
	int i;
	const struct tuple *t;

	erase_flash();
	TEST_ASSERT(initvars() == EC_SUCCESS);

	for (i = 0; i < 255; i++) {
		valA[i] = i;
		valB[i] = 255 - i;
	}

	TEST_ASSERT(setvar(keyA, strlen(keyA),
			   valA, sizeof(valA)) == EC_SUCCESS);
	TEST_ASSERT(str_setvar("c", "CcC") == EC_SUCCESS);
	TEST_ASSERT(setvar(keyB, strlen(keyB),
			   valB, sizeof(valB)) == EC_SUCCESS);
	TEST_ASSERT(str_setvar("d", "dDd") == EC_SUCCESS);

	TEST_ASSERT(writevars() == EC_SUCCESS);

	t = getvar(keyA, strlen(keyA));
	TEST_ASSERT(t);
	TEST_ASSERT(memcmp(tuple_val(t), valA, sizeof(valA)) == 0);

	t = getvar(keyB, strlen(keyB));
	TEST_ASSERT(t);
	TEST_ASSERT(memcmp(tuple_val(t), valB, sizeof(valB)) == 0);

	TEST_ASSERT(str_matches("c", "CcC"));
	TEST_ASSERT(str_matches("d", "dDd"));

	return EC_SUCCESS;
}

static int fill_it_up(void)
{
	int i, n;
	char key[20];

	erase_flash();
	TEST_ASSERT(initvars() == EC_SUCCESS);

	/*
	 * Some magic numbers here, because we want to use up 10 bytes at a
	 * time and end up with exactly 9 free bytes left.
	 */
	TEST_ASSERT(CONFIG_FLASH_NVMEM_VARS_USER_SIZE % 10 == 0);
	n = CONFIG_FLASH_NVMEM_VARS_USER_SIZE / 10;
	TEST_ASSERT(n < 1000);

	/* Fill up the storage */
	for (i = 0; i < n - 1; i++) {
		/* 3-byte header, 5-char key, 2-char val, == 10 chars */
		snprintf(key, sizeof(key), "kk%03d", i);
		TEST_ASSERT(setvar(key, 5, "aa", 2) == EC_SUCCESS);
	}

	/*
	 * Should be nine bytes left in rbuf (because we need one more '\0' at
	 * the end). This won't fit.
	 */
	TEST_ASSERT(setvar("kk999", 5, "aa", 2) == EC_ERROR_OVERFLOW);
	/* But this will. */
	TEST_ASSERT(setvar("kk999", 5, "a", 1) == EC_SUCCESS);
	/* And this, because it replaces a previous entry */
	TEST_ASSERT(setvar("kk000", 5, "bc", 2) == EC_SUCCESS);
	/* But this still won't fit */
	TEST_ASSERT(setvar("kk999", 5, "de", 2) == EC_ERROR_OVERFLOW);

	return EC_SUCCESS;
}

void run_test(void)
{
	test_reset();

	RUN_TEST(check_init);
	RUN_TEST(simple_write);
	RUN_TEST(simple_search);
	RUN_TEST(simple_delete);
	RUN_TEST(complex_write);
	RUN_TEST(weird_keys);
	RUN_TEST(weird_values);
	RUN_TEST(fill_it_up);

	test_print_result();
}