1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2018, Google Inc. All rights reserved.
9 #include <asm/global_data.h>
10 #include <test/suites.h>
11 #include <test/test.h>
14 DECLARE_GLOBAL_DATA_PTR;
16 /* Declare a new bloblist test */
17 #define BLOBLIST_TEST(_name, _flags) \
18 UNIT_TEST(_name, _flags, bloblist_test)
21 TEST_TAG = BLOBLISTT_U_BOOT_SPL_HANDOFF,
22 TEST_TAG2 = BLOBLISTT_VBOOT_CTX,
23 TEST_TAG_MISSING = 0x10000,
27 TEST_SIZE_LARGE = 0x3e0,
29 TEST_ADDR = CONFIG_BLOBLIST_ADDR,
30 TEST_BLOBLIST_SIZE = 0x400,
35 static const char test1_str[] = "the eyes are open";
36 static const char test2_str[] = "the mouth moves";
38 static struct bloblist_hdr *clear_bloblist(void)
40 struct bloblist_hdr *hdr;
43 * Clear out any existing bloblist so we have a clean slate. Zero the
44 * header so that existing records are removed, but set everything else
45 * to 0xff for testing purposes.
47 hdr = map_sysmem(CONFIG_BLOBLIST_ADDR, TEST_BLOBLIST_SIZE);
48 memset(hdr, ERASE_BYTE, TEST_BLOBLIST_SIZE);
49 memset(hdr, '\0', sizeof(*hdr));
54 static int check_zero(void *data, int size)
59 for (ptr = data, i = 0; i < size; i++, ptr++) {
67 static int bloblist_test_init(struct unit_test_state *uts)
69 struct bloblist_hdr *hdr;
71 hdr = clear_bloblist();
72 ut_asserteq(-ENOENT, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
73 ut_asserteq_ptr(NULL, bloblist_check_magic(TEST_ADDR));
74 ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0, 0));
75 ut_asserteq_ptr(hdr, bloblist_check_magic(TEST_ADDR));
77 ut_asserteq(-EPROTONOSUPPORT, bloblist_check(TEST_ADDR,
80 ut_asserteq(-ENOSPC, bloblist_new(TEST_ADDR, 0xc, 0, 0));
81 ut_asserteq(-EFAULT, bloblist_new(1, TEST_BLOBLIST_SIZE, 0, 0));
82 ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0, 0));
84 ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
85 ut_assertok(bloblist_finish());
86 ut_assertok(bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
89 ut_asserteq_ptr(NULL, bloblist_check_magic(TEST_ADDR));
93 ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
97 BLOBLIST_TEST(bloblist_test_init, UFT_BLOBLIST);
99 static int bloblist_test_blob(struct unit_test_state *uts)
101 struct bloblist_hdr *hdr;
102 struct bloblist_rec *rec, *rec2;
105 /* At the start there should be no records */
106 hdr = clear_bloblist();
107 ut_assertnull(bloblist_find(TEST_TAG, TEST_BLOBLIST_SIZE));
108 ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0, 0));
109 ut_asserteq(sizeof(struct bloblist_hdr), bloblist_get_size());
110 ut_asserteq(TEST_BLOBLIST_SIZE, bloblist_get_total_size());
111 ut_asserteq(TEST_ADDR, bloblist_get_base());
112 ut_asserteq(map_to_sysmem(hdr), TEST_ADDR);
114 /* Add a record and check that we can find it */
115 data = bloblist_add(TEST_TAG, TEST_SIZE, 0);
116 rec = (void *)(hdr + 1);
117 ut_asserteq_addr(rec + 1, data);
118 data = bloblist_find(TEST_TAG, TEST_SIZE);
119 ut_asserteq_addr(rec + 1, data);
121 /* Check the data is zeroed */
122 ut_assertok(check_zero(data, TEST_SIZE));
124 /* Check the 'ensure' method */
125 ut_asserteq_addr(data, bloblist_ensure(TEST_TAG, TEST_SIZE));
126 ut_assertnull(bloblist_ensure(TEST_TAG, TEST_SIZE2));
127 rec2 = (struct bloblist_rec *)(data + ALIGN(TEST_SIZE, BLOBLIST_ALIGN));
128 ut_assertok(check_zero(data, TEST_SIZE));
130 /* Check for a non-existent record */
131 ut_asserteq_addr(data, bloblist_ensure(TEST_TAG, TEST_SIZE));
132 ut_asserteq_addr(rec2 + 1, bloblist_ensure(TEST_TAG2, TEST_SIZE2));
133 ut_assertnull(bloblist_find(TEST_TAG_MISSING, 0));
137 BLOBLIST_TEST(bloblist_test_blob, UFT_BLOBLIST);
139 /* Check bloblist_ensure_size_ret() */
140 static int bloblist_test_blob_ensure(struct unit_test_state *uts)
145 /* At the start there should be no records */
147 ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0, 0));
149 /* Test with an empty bloblist */
151 ut_assertok(bloblist_ensure_size_ret(TEST_TAG, &size, &data));
152 ut_asserteq(TEST_SIZE, size);
153 ut_assertok(check_zero(data, TEST_SIZE));
155 /* Check that we get the same thing again */
156 ut_assertok(bloblist_ensure_size_ret(TEST_TAG, &size, &data2));
157 ut_asserteq(TEST_SIZE, size);
158 ut_asserteq_addr(data, data2);
160 /* Check that the size remains the same */
162 ut_assertok(bloblist_ensure_size_ret(TEST_TAG, &size, &data));
163 ut_asserteq(TEST_SIZE, size);
165 /* Check running out of space */
166 size = TEST_SIZE_LARGE;
167 ut_asserteq(-ENOSPC, bloblist_ensure_size_ret(TEST_TAG2, &size, &data));
171 BLOBLIST_TEST(bloblist_test_blob_ensure, UFT_BLOBLIST);
173 static int bloblist_test_bad_blob(struct unit_test_state *uts)
175 struct bloblist_hdr *hdr;
178 hdr = clear_bloblist();
179 ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0, 0));
181 data += sizeof(struct bloblist_rec);
182 ut_asserteq_addr(data, bloblist_ensure(TEST_TAG, TEST_SIZE));
183 ut_asserteq_addr(data, bloblist_ensure(TEST_TAG, TEST_SIZE));
187 BLOBLIST_TEST(bloblist_test_bad_blob, UFT_BLOBLIST);
189 static int bloblist_test_checksum(struct unit_test_state *uts)
191 struct bloblist_hdr *hdr;
194 hdr = clear_bloblist();
195 ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0, 0));
196 ut_assertok(bloblist_finish());
197 ut_assertok(bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
200 * Now change things amd make sure that the checksum notices. We cannot
201 * change the size or alloced fields, since that will crash the code.
202 * It has to rely on these being correct.
205 ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
209 ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
213 ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
217 ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
221 ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
224 /* Make sure the checksum changes when we add blobs */
225 data = bloblist_add(TEST_TAG, TEST_SIZE, 0);
226 ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
228 data2 = bloblist_add(TEST_TAG2, TEST_SIZE2, 0);
229 ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
230 ut_assertok(bloblist_finish());
232 /* It should also change if we change the data */
233 ut_assertok(bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
235 ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
238 ut_assertok(bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
240 ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
244 * Changing data outside the range of valid data should affect the
247 ut_assertok(bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
249 ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
251 ut_assertok(bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
254 ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
256 ut_assertok(bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
260 BLOBLIST_TEST(bloblist_test_checksum, UFT_BLOBLIST);
262 /* Test the 'bloblist info' command */
263 static int bloblist_test_cmd_info(struct unit_test_state *uts)
265 struct bloblist_hdr *hdr;
268 hdr = clear_bloblist();
269 ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0, 0));
270 data = bloblist_ensure(TEST_TAG, TEST_SIZE);
271 data2 = bloblist_ensure(TEST_TAG2, TEST_SIZE2);
273 run_command("bloblist info", 0);
274 ut_assert_nextline("base: %lx", (ulong)map_to_sysmem(hdr));
275 ut_assert_nextline("total size: 400 1 KiB");
276 ut_assert_nextline("used size: 50 80 Bytes");
277 ut_assert_nextline("free: 3b0 944 Bytes");
281 BLOBLIST_TEST(bloblist_test_cmd_info, UFT_BLOBLIST | UTF_CONSOLE);
283 /* Test the 'bloblist list' command */
284 static int bloblist_test_cmd_list(struct unit_test_state *uts)
286 struct bloblist_hdr *hdr;
289 hdr = clear_bloblist();
290 ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0, 0));
291 data = bloblist_ensure(TEST_TAG, TEST_SIZE);
292 data2 = bloblist_ensure(TEST_TAG2, TEST_SIZE2);
294 run_command("bloblist list", 0);
295 ut_assert_nextline("Address Size Tag Name");
296 ut_assert_nextline("%08lx %8x fff000 SPL hand-off",
297 (ulong)map_to_sysmem(data), TEST_SIZE);
298 ut_assert_nextline("%08lx %8x 202 Chrome OS vboot context",
299 (ulong)map_to_sysmem(data2), TEST_SIZE2);
303 BLOBLIST_TEST(bloblist_test_cmd_list, UFT_BLOBLIST | UTF_CONSOLE);
305 /* Test alignment of bloblist blobs */
306 static int bloblist_test_align(struct unit_test_state *uts)
308 struct bloblist_hdr *hdr;
313 /* At the start there should be no records */
314 hdr = clear_bloblist();
315 ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0, 0));
316 ut_assertnull(bloblist_find(TEST_TAG, TEST_BLOBLIST_SIZE));
318 /* Check the default alignment */
319 for (i = 0; i < 3; i++) {
325 data = bloblist_add(i, size, 0);
326 ut_assertnonnull(data);
327 addr = map_to_sysmem(data);
328 ut_asserteq(0, addr & (BLOBLIST_BLOB_ALIGN - 1));
330 /* Only the bytes in the blob data should be zeroed */
331 for (j = 0; j < size; j++)
332 ut_asserteq(0, data[j]);
333 for (; j < BLOBLIST_BLOB_ALIGN; j++)
334 ut_asserteq(ERASE_BYTE, data[j]);
337 /* Check larger alignment */
338 for (i = 0; i < 3; i++) {
341 data = bloblist_add(3 + i, i * 4, align);
342 ut_assertnonnull(data);
343 addr = map_to_sysmem(data);
344 ut_asserteq(0, addr & (align - 1));
347 /* Check alignment with an bloblist starting on a smaller alignment */
348 hdr = map_sysmem(TEST_ADDR + BLOBLIST_BLOB_ALIGN, TEST_BLOBLIST_SIZE);
349 memset(hdr, ERASE_BYTE, TEST_BLOBLIST_SIZE);
350 memset(hdr, '\0', sizeof(*hdr));
351 ut_assertok(bloblist_new(TEST_ADDR + BLOBLIST_ALIGN, TEST_BLOBLIST_SIZE,
354 data = bloblist_add(1, 5, BLOBLIST_ALIGN_LOG2 + 1);
355 ut_assertnonnull(data);
356 addr = map_to_sysmem(data);
357 ut_asserteq(0, addr & (BLOBLIST_BLOB_ALIGN * 2 - 1));
361 BLOBLIST_TEST(bloblist_test_align, UFT_BLOBLIST);
363 /* Test relocation of a bloblist */
364 static int bloblist_test_reloc(struct unit_test_state *uts)
366 const uint large_size = TEST_BLOBLIST_SIZE;
367 const uint small_size = 0x20;
373 ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0, 0));
375 /* Add one blob and then one that won't fit */
376 blob1 = bloblist_add(TEST_TAG, small_size, 0);
377 ut_assertnonnull(blob1);
378 blob2 = bloblist_add(TEST_TAG2, large_size, 0);
379 ut_assertnull(blob2);
381 /* Relocate the bloblist somewhere else, a bit larger */
382 new_addr = TEST_ADDR + TEST_BLOBLIST_SIZE;
383 new_size = TEST_BLOBLIST_SIZE + 0x100;
384 new_ptr = map_sysmem(new_addr, TEST_BLOBLIST_SIZE);
385 ut_assertok(bloblist_reloc(new_ptr, new_size));
387 /* Check the old blob is there and that we can now add the bigger one */
388 ut_assertnonnull(bloblist_find(TEST_TAG, small_size));
389 ut_assertnull(bloblist_find(TEST_TAG2, small_size));
390 blob2 = bloblist_add(TEST_TAG2, large_size, 0);
391 ut_assertnonnull(blob2);
395 BLOBLIST_TEST(bloblist_test_reloc, UFT_BLOBLIST);
397 /* Test expansion of a blob */
398 static int bloblist_test_grow(struct unit_test_state *uts)
400 const uint small_size = 0x20;
401 void *blob1, *blob2, *blob1_new;
402 struct bloblist_hdr *hdr;
405 ptr = map_sysmem(TEST_ADDR, TEST_BLOBLIST_SIZE);
407 memset(hdr, ERASE_BYTE, TEST_BLOBLIST_SIZE);
409 /* Create two blobs */
410 ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0, 0));
411 blob1 = bloblist_add(TEST_TAG, small_size, 0);
412 ut_assertnonnull(blob1);
413 ut_assertok(check_zero(blob1, small_size));
414 strcpy(blob1, test1_str);
416 blob2 = bloblist_add(TEST_TAG2, small_size, 0);
417 ut_assertnonnull(blob2);
418 strcpy(blob2, test2_str);
420 ut_asserteq(sizeof(struct bloblist_hdr) +
421 sizeof(struct bloblist_rec) * 2 + small_size * 2,
424 /* Resize the first one */
425 ut_assertok(bloblist_resize(TEST_TAG, small_size + 4));
427 /* The first one should not have moved, just got larger */
428 blob1_new = bloblist_find(TEST_TAG, small_size + 4);
429 ut_asserteq_ptr(blob1, blob1_new);
431 /* The new space should be zeroed */
432 ut_assertok(check_zero(blob1 + small_size, 4));
434 /* The second one should have moved */
435 blob2 = bloblist_find(TEST_TAG2, small_size);
436 ut_assertnonnull(blob2);
437 ut_asserteq_str(test2_str, blob2);
439 /* The header should have more bytes in use */
441 ut_asserteq(sizeof(struct bloblist_hdr) +
442 sizeof(struct bloblist_rec) * 2 + small_size * 2 +
448 BLOBLIST_TEST(bloblist_test_grow, UFT_BLOBLIST);
450 /* Test shrinking of a blob */
451 static int bloblist_test_shrink(struct unit_test_state *uts)
453 const uint small_size = 0x20;
454 void *blob1, *blob2, *blob1_new;
455 struct bloblist_hdr *hdr;
459 ptr = map_sysmem(TEST_ADDR, TEST_BLOBLIST_SIZE);
461 /* Create two blobs */
462 ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0, 0));
463 blob1 = bloblist_add(TEST_TAG, small_size, 0);
464 ut_assertnonnull(blob1);
465 strcpy(blob1, test1_str);
467 blob2 = bloblist_add(TEST_TAG2, small_size, 0);
468 ut_assertnonnull(blob2);
469 strcpy(blob2, test2_str);
472 ut_asserteq(sizeof(struct bloblist_hdr) +
473 sizeof(struct bloblist_rec) * 2 + small_size * 2,
476 /* Resize the first one */
477 new_size = small_size - BLOBLIST_ALIGN - 4;
478 ut_assertok(bloblist_resize(TEST_TAG, new_size));
480 /* The first one should not have moved, just got smaller */
481 blob1_new = bloblist_find(TEST_TAG, new_size);
482 ut_asserteq_ptr(blob1, blob1_new);
484 /* The second one should have moved */
485 blob2 = bloblist_find(TEST_TAG2, small_size);
486 ut_assertnonnull(blob2);
487 ut_asserteq_str(test2_str, blob2);
489 /* The header should have fewer bytes in use */
491 ut_asserteq(sizeof(struct bloblist_hdr) +
492 sizeof(struct bloblist_rec) * 2 + small_size * 2 -
498 BLOBLIST_TEST(bloblist_test_shrink, UFT_BLOBLIST);
500 /* Test failing to adjust a blob size */
501 static int bloblist_test_resize_fail(struct unit_test_state *uts)
503 const uint small_size = 0x20;
504 struct bloblist_hdr *hdr;
509 ptr = map_sysmem(TEST_ADDR, TEST_BLOBLIST_SIZE);
511 /* Create two blobs */
512 ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0, 0));
513 blob1 = bloblist_add(TEST_TAG, small_size, 0);
514 ut_assertnonnull(blob1);
516 blob2 = bloblist_add(TEST_TAG2, small_size, 0);
517 ut_assertnonnull(blob2);
520 ut_asserteq(sizeof(struct bloblist_hdr) +
521 sizeof(struct bloblist_rec) * 2 + small_size * 2,
524 /* Resize the first one, to check the boundary conditions */
525 ut_asserteq(-EINVAL, bloblist_resize(TEST_TAG, -1));
527 new_size = small_size + (hdr->total_size - hdr->used_size);
528 ut_asserteq(-ENOSPC, bloblist_resize(TEST_TAG, new_size + 1));
529 ut_assertok(bloblist_resize(TEST_TAG, new_size));
533 BLOBLIST_TEST(bloblist_test_resize_fail, UFT_BLOBLIST);
535 /* Test expanding the last blob in a bloblist */
536 static int bloblist_test_resize_last(struct unit_test_state *uts)
538 const uint small_size = 0x20;
539 struct bloblist_hdr *hdr;
540 void *blob1, *blob2, *blob2_new;
544 ptr = map_sysmem(TEST_ADDR, TEST_BLOBLIST_SIZE);
545 memset(ptr, ERASE_BYTE, TEST_BLOBLIST_SIZE);
548 /* Create two blobs */
549 ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0, 0));
550 blob1 = bloblist_add(TEST_TAG, small_size, 0);
551 ut_assertnonnull(blob1);
553 blob2 = bloblist_add(TEST_TAG2, small_size, 0);
554 ut_assertnonnull(blob2);
556 /* Check the byte after the last blob */
557 alloced_val = sizeof(struct bloblist_hdr) +
558 sizeof(struct bloblist_rec) * 2 + small_size * 2;
559 ut_asserteq(alloced_val, hdr->used_size);
560 ut_asserteq_ptr((void *)hdr + alloced_val, blob2 + small_size);
561 ut_asserteq((u8)ERASE_BYTE, *((u8 *)hdr + hdr->used_size));
563 /* Resize the second one, checking nothing changes */
564 ut_asserteq(0, bloblist_resize(TEST_TAG2, small_size + 4));
566 blob2_new = bloblist_find(TEST_TAG2, small_size + 4);
567 ut_asserteq_ptr(blob2, blob2_new);
570 * the new blob should encompass the byte we checked now, so it should
571 * be zeroed. This zeroing should affect only the four new bytes added
574 ut_asserteq(0, *((u8 *)hdr + alloced_val));
575 ut_asserteq((u8)ERASE_BYTE, *((u8 *)hdr + alloced_val + 4));
577 /* Check that the new top of the allocated blobs has not been touched */
578 alloced_val += BLOBLIST_BLOB_ALIGN;
579 ut_asserteq(alloced_val, hdr->used_size);
580 ut_asserteq((u8)ERASE_BYTE, *((u8 *)hdr + hdr->used_size));
584 BLOBLIST_TEST(bloblist_test_resize_last, UFT_BLOBLIST);
586 /* Check a completely full bloblist */
587 static int bloblist_test_blob_maxsize(struct unit_test_state *uts)
592 /* At the start there should be no records */
594 ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0, 0));
596 /* Add a blob that takes up all space */
597 size = TEST_BLOBLIST_SIZE - sizeof(struct bloblist_hdr) -
598 sizeof(struct bloblist_rec);
599 ptr = bloblist_add(TEST_TAG, size, 0);
600 ut_assertnonnull(ptr);
602 ptr = bloblist_add(TEST_TAG, size + 1, 0);
607 BLOBLIST_TEST(bloblist_test_blob_maxsize, UFT_BLOBLIST);
609 int do_ut_bloblist(struct cmd_tbl *cmdtp, int flag, int argc,
612 struct unit_test *tests = UNIT_TEST_SUITE_START(bloblist_test);
613 const int n_ents = UNIT_TEST_SUITE_COUNT(bloblist_test);
615 return cmd_ut_category("bloblist", "bloblist_test_",
616 tests, n_ents, argc, argv);