1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2018, Google Inc. All rights reserved.
9 #include <test/suites.h>
10 #include <test/test.h>
13 /* Declare a new bloblist test */
14 #define BLOBLIST_TEST(_name, _flags) \
15 UNIT_TEST(_name, _flags, bloblist)
18 TEST_TAG = BLOBLISTT_U_BOOT_SPL_HANDOFF,
19 TEST_TAG2 = BLOBLISTT_VBOOT_CTX,
20 TEST_TAG_MISSING = 0x10000,
24 TEST_SIZE_LARGE = 0x3e0,
26 TEST_ADDR = CONFIG_BLOBLIST_ADDR,
27 TEST_BLOBLIST_SIZE = 0x400,
32 static const char test1_str[] = "the eyes are open";
33 static const char test2_str[] = "the mouth moves";
35 static struct bloblist_hdr *clear_bloblist(void)
37 struct bloblist_hdr *hdr;
40 * Clear out any existing bloblist so we have a clean slate. Zero the
41 * header so that existing records are removed, but set everything else
42 * to 0xff for testing purposes.
44 hdr = map_sysmem(CONFIG_BLOBLIST_ADDR, TEST_BLOBLIST_SIZE);
45 memset(hdr, ERASE_BYTE, TEST_BLOBLIST_SIZE);
46 memset(hdr, '\0', sizeof(*hdr));
51 static int check_zero(void *data, int size)
56 for (ptr = data, i = 0; i < size; i++, ptr++) {
64 static int bloblist_test_init(struct unit_test_state *uts)
66 struct bloblist_hdr *hdr;
68 hdr = clear_bloblist();
69 ut_asserteq(-ENOENT, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
70 ut_asserteq_ptr(NULL, bloblist_check_magic(TEST_ADDR));
71 ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0, 0));
72 ut_asserteq_ptr(hdr, bloblist_check_magic(TEST_ADDR));
74 ut_asserteq(-EPROTONOSUPPORT, bloblist_check(TEST_ADDR,
77 ut_asserteq(-ENOSPC, bloblist_new(TEST_ADDR, 0xc, 0, 0));
78 ut_asserteq(-EFAULT, bloblist_new(1, TEST_BLOBLIST_SIZE, 0, 0));
79 ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0, 0));
81 ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
82 ut_assertok(bloblist_finish());
83 ut_assertok(bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
86 ut_asserteq_ptr(NULL, bloblist_check_magic(TEST_ADDR));
90 ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
94 BLOBLIST_TEST(bloblist_test_init, UFT_BLOBLIST);
96 static int bloblist_test_blob(struct unit_test_state *uts)
98 struct bloblist_hdr *hdr;
99 struct bloblist_rec *rec, *rec2;
103 /* At the start there should be no records */
104 hdr = clear_bloblist();
105 ut_assertnull(bloblist_find(TEST_TAG, TEST_BLOBLIST_SIZE));
106 ut_assertnull(bloblist_get_blob(TEST_TAG, &size));
107 ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0, 0));
108 ut_asserteq(sizeof(struct bloblist_hdr), bloblist_get_size());
109 ut_asserteq(TEST_BLOBLIST_SIZE, bloblist_get_total_size());
110 ut_asserteq(TEST_ADDR, bloblist_get_base());
111 ut_asserteq(map_to_sysmem(hdr), TEST_ADDR);
113 /* Add a record and check that we can find it */
114 data = bloblist_add(TEST_TAG, TEST_SIZE, 0);
115 rec = (void *)(hdr + 1);
116 ut_asserteq_addr(rec + 1, data);
117 data = bloblist_find(TEST_TAG, TEST_SIZE);
118 ut_asserteq_addr(rec + 1, data);
119 ut_asserteq_addr(bloblist_get_blob(TEST_TAG, &size), data);
120 ut_asserteq(size, TEST_SIZE);
122 /* Check the data is zeroed */
123 ut_assertok(check_zero(data, TEST_SIZE));
125 /* Check the 'ensure' method */
126 ut_asserteq_addr(data, bloblist_ensure(TEST_TAG, TEST_SIZE));
127 ut_assertnull(bloblist_ensure(TEST_TAG, TEST_SIZE2));
128 rec2 = (struct bloblist_rec *)(data + ALIGN(TEST_SIZE, BLOBLIST_ALIGN));
129 ut_assertok(check_zero(data, TEST_SIZE));
131 /* Check for a non-existent record */
132 ut_asserteq_addr(data, bloblist_ensure(TEST_TAG, TEST_SIZE));
133 ut_asserteq_addr(rec2 + 1, bloblist_ensure(TEST_TAG2, TEST_SIZE2));
134 ut_assertnull(bloblist_find(TEST_TAG_MISSING, 0));
138 BLOBLIST_TEST(bloblist_test_blob, UFT_BLOBLIST);
140 /* Check bloblist_ensure_size_ret() */
141 static int bloblist_test_blob_ensure(struct unit_test_state *uts)
146 /* At the start there should be no records */
148 ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0, 0));
150 /* Test with an empty bloblist */
152 ut_assertok(bloblist_ensure_size_ret(TEST_TAG, &size, &data));
153 ut_asserteq(TEST_SIZE, size);
154 ut_assertok(check_zero(data, TEST_SIZE));
156 /* Check that we get the same thing again */
157 ut_assertok(bloblist_ensure_size_ret(TEST_TAG, &size, &data2));
158 ut_asserteq(TEST_SIZE, size);
159 ut_asserteq_addr(data, data2);
161 /* Check that the size remains the same */
163 ut_assertok(bloblist_ensure_size_ret(TEST_TAG, &size, &data));
164 ut_asserteq(TEST_SIZE, size);
166 /* Check running out of space */
167 size = TEST_SIZE_LARGE;
168 ut_asserteq(-ENOSPC, bloblist_ensure_size_ret(TEST_TAG2, &size, &data));
172 BLOBLIST_TEST(bloblist_test_blob_ensure, UFT_BLOBLIST);
174 static int bloblist_test_bad_blob(struct unit_test_state *uts)
176 struct bloblist_hdr *hdr;
179 hdr = clear_bloblist();
180 ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0, 0));
182 data += sizeof(struct bloblist_rec);
183 ut_asserteq_addr(data, bloblist_ensure(TEST_TAG, TEST_SIZE));
184 ut_asserteq_addr(data, bloblist_ensure(TEST_TAG, TEST_SIZE));
188 BLOBLIST_TEST(bloblist_test_bad_blob, UFT_BLOBLIST);
190 static int bloblist_test_checksum(struct unit_test_state *uts)
192 struct bloblist_hdr *hdr;
195 hdr = clear_bloblist();
196 ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0, 0));
197 ut_assertok(bloblist_finish());
198 ut_assertok(bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
201 * Now change things amd make sure that the checksum notices. We cannot
202 * change the size or alloced fields, since that will crash the code.
203 * It has to rely on these being correct.
206 ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
210 ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
214 ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
218 ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
222 ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
225 /* Make sure the checksum changes when we add blobs */
226 data = bloblist_add(TEST_TAG, TEST_SIZE, 0);
227 ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
229 data2 = bloblist_add(TEST_TAG2, TEST_SIZE2, 0);
230 ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
231 ut_assertok(bloblist_finish());
233 /* It should also change if we change the data */
234 ut_assertok(bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
236 ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
239 ut_assertok(bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
241 ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
245 * Changing data outside the range of valid data should affect the
248 ut_assertok(bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
250 ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
252 ut_assertok(bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
255 ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
257 ut_assertok(bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
261 BLOBLIST_TEST(bloblist_test_checksum, UFT_BLOBLIST);
263 /* Test the 'bloblist info' command */
264 static int bloblist_test_cmd_info(struct unit_test_state *uts)
266 struct bloblist_hdr *hdr;
269 hdr = clear_bloblist();
270 ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0, 0));
271 data = bloblist_ensure(TEST_TAG, TEST_SIZE);
272 data2 = bloblist_ensure(TEST_TAG2, TEST_SIZE2);
274 run_command("bloblist info", 0);
275 ut_assert_nextline("base: %lx", (ulong)map_to_sysmem(hdr));
276 ut_assert_nextline("total size: 400 1 KiB");
277 ut_assert_nextline("used size: 50 80 Bytes");
278 ut_assert_nextline("free: 3b0 944 Bytes");
282 BLOBLIST_TEST(bloblist_test_cmd_info, UFT_BLOBLIST | UTF_CONSOLE);
284 /* Test the 'bloblist list' command */
285 static int bloblist_test_cmd_list(struct unit_test_state *uts)
287 struct bloblist_hdr *hdr;
290 hdr = clear_bloblist();
291 ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0, 0));
292 data = bloblist_ensure(TEST_TAG, TEST_SIZE);
293 data2 = bloblist_ensure(TEST_TAG2, TEST_SIZE2);
295 run_command("bloblist list", 0);
296 ut_assert_nextline("Address Size Tag Name");
297 ut_assert_nextline("%08lx %8x fff000 SPL hand-off",
298 (ulong)map_to_sysmem(data), TEST_SIZE);
299 ut_assert_nextline("%08lx %8x 202 Chrome OS vboot context",
300 (ulong)map_to_sysmem(data2), TEST_SIZE2);
304 BLOBLIST_TEST(bloblist_test_cmd_list, UFT_BLOBLIST | UTF_CONSOLE);
306 /* Test alignment of bloblist blobs */
307 static int bloblist_test_align(struct unit_test_state *uts)
309 struct bloblist_hdr *hdr;
314 /* At the start there should be no records */
315 hdr = clear_bloblist();
316 ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0, 0));
317 ut_assertnull(bloblist_find(TEST_TAG, TEST_BLOBLIST_SIZE));
319 /* Check the default alignment */
320 for (i = 0; i < 3; i++) {
326 data = bloblist_add(i, size, 0);
327 ut_assertnonnull(data);
328 addr = map_to_sysmem(data);
329 ut_asserteq(0, addr & (BLOBLIST_BLOB_ALIGN - 1));
331 /* Only the bytes in the blob data should be zeroed */
332 for (j = 0; j < size; j++)
333 ut_asserteq(0, data[j]);
334 for (; j < BLOBLIST_BLOB_ALIGN; j++)
335 ut_asserteq(ERASE_BYTE, data[j]);
338 /* Check larger alignment */
339 for (i = 0; i < 3; i++) {
342 data = bloblist_add(3 + i, i * 4, align);
343 ut_assertnonnull(data);
344 addr = map_to_sysmem(data);
345 ut_asserteq(0, addr & (align - 1));
348 /* Check alignment with an bloblist starting on a smaller alignment */
349 hdr = map_sysmem(TEST_ADDR + BLOBLIST_BLOB_ALIGN, TEST_BLOBLIST_SIZE);
350 memset(hdr, ERASE_BYTE, TEST_BLOBLIST_SIZE);
351 memset(hdr, '\0', sizeof(*hdr));
352 ut_assertok(bloblist_new(TEST_ADDR + BLOBLIST_ALIGN, TEST_BLOBLIST_SIZE,
355 data = bloblist_add(1, 5, BLOBLIST_ALIGN_LOG2 + 1);
356 ut_assertnonnull(data);
357 addr = map_to_sysmem(data);
358 ut_asserteq(0, addr & (BLOBLIST_BLOB_ALIGN * 2 - 1));
362 BLOBLIST_TEST(bloblist_test_align, UFT_BLOBLIST);
364 /* Test relocation of a bloblist */
365 static int bloblist_test_reloc(struct unit_test_state *uts)
367 const uint large_size = TEST_BLOBLIST_SIZE;
368 const uint small_size = 0x20;
374 ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0, 0));
376 /* Add one blob and then one that won't fit */
377 blob1 = bloblist_add(TEST_TAG, small_size, 0);
378 ut_assertnonnull(blob1);
379 blob2 = bloblist_add(TEST_TAG2, large_size, 0);
380 ut_assertnull(blob2);
382 /* Relocate the bloblist somewhere else, a bit larger */
383 new_addr = TEST_ADDR + TEST_BLOBLIST_SIZE;
384 new_size = TEST_BLOBLIST_SIZE + 0x100;
385 new_ptr = map_sysmem(new_addr, TEST_BLOBLIST_SIZE);
386 ut_assertok(bloblist_reloc(new_ptr, new_size));
388 /* Check the old blob is there and that we can now add the bigger one */
389 ut_assertnonnull(bloblist_find(TEST_TAG, small_size));
390 ut_assertnull(bloblist_find(TEST_TAG2, small_size));
391 blob2 = bloblist_add(TEST_TAG2, large_size, 0);
392 ut_assertnonnull(blob2);
396 BLOBLIST_TEST(bloblist_test_reloc, UFT_BLOBLIST);
398 /* Test expansion of a blob */
399 static int bloblist_test_grow(struct unit_test_state *uts)
401 const uint small_size = 0x20;
402 void *blob1, *blob2, *blob1_new;
403 struct bloblist_hdr *hdr;
406 ptr = map_sysmem(TEST_ADDR, TEST_BLOBLIST_SIZE);
408 memset(hdr, ERASE_BYTE, TEST_BLOBLIST_SIZE);
410 /* Create two blobs */
411 ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0, 0));
412 blob1 = bloblist_add(TEST_TAG, small_size, 0);
413 ut_assertnonnull(blob1);
414 ut_assertok(check_zero(blob1, small_size));
415 strcpy(blob1, test1_str);
417 blob2 = bloblist_add(TEST_TAG2, small_size, 0);
418 ut_assertnonnull(blob2);
419 strcpy(blob2, test2_str);
421 ut_asserteq(sizeof(struct bloblist_hdr) +
422 sizeof(struct bloblist_rec) * 2 + small_size * 2,
425 /* Resize the first one */
426 ut_assertok(bloblist_resize(TEST_TAG, small_size + 4));
428 /* The first one should not have moved, just got larger */
429 blob1_new = bloblist_find(TEST_TAG, small_size + 4);
430 ut_asserteq_ptr(blob1, blob1_new);
432 /* The new space should be zeroed */
433 ut_assertok(check_zero(blob1 + small_size, 4));
435 /* The second one should have moved */
436 blob2 = bloblist_find(TEST_TAG2, small_size);
437 ut_assertnonnull(blob2);
438 ut_asserteq_str(test2_str, blob2);
440 /* The header should have more bytes in use */
442 ut_asserteq(sizeof(struct bloblist_hdr) +
443 sizeof(struct bloblist_rec) * 2 + small_size * 2 +
449 BLOBLIST_TEST(bloblist_test_grow, UFT_BLOBLIST);
451 /* Test shrinking of a blob */
452 static int bloblist_test_shrink(struct unit_test_state *uts)
454 const uint small_size = 0x20;
455 void *blob1, *blob2, *blob1_new;
456 struct bloblist_hdr *hdr;
460 ptr = map_sysmem(TEST_ADDR, TEST_BLOBLIST_SIZE);
462 /* Create two blobs */
463 ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0, 0));
464 blob1 = bloblist_add(TEST_TAG, small_size, 0);
465 ut_assertnonnull(blob1);
466 strcpy(blob1, test1_str);
468 blob2 = bloblist_add(TEST_TAG2, small_size, 0);
469 ut_assertnonnull(blob2);
470 strcpy(blob2, test2_str);
473 ut_asserteq(sizeof(struct bloblist_hdr) +
474 sizeof(struct bloblist_rec) * 2 + small_size * 2,
477 /* Resize the first one */
478 new_size = small_size - BLOBLIST_ALIGN - 4;
479 ut_assertok(bloblist_resize(TEST_TAG, new_size));
481 /* The first one should not have moved, just got smaller */
482 blob1_new = bloblist_find(TEST_TAG, new_size);
483 ut_asserteq_ptr(blob1, blob1_new);
485 /* The second one should have moved */
486 blob2 = bloblist_find(TEST_TAG2, small_size);
487 ut_assertnonnull(blob2);
488 ut_asserteq_str(test2_str, blob2);
490 /* The header should have fewer bytes in use */
492 ut_asserteq(sizeof(struct bloblist_hdr) +
493 sizeof(struct bloblist_rec) * 2 + small_size * 2 -
499 BLOBLIST_TEST(bloblist_test_shrink, UFT_BLOBLIST);
501 /* Test failing to adjust a blob size */
502 static int bloblist_test_resize_fail(struct unit_test_state *uts)
504 const uint small_size = 0x20;
505 struct bloblist_hdr *hdr;
510 ptr = map_sysmem(TEST_ADDR, TEST_BLOBLIST_SIZE);
512 /* Create two blobs */
513 ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0, 0));
514 blob1 = bloblist_add(TEST_TAG, small_size, 0);
515 ut_assertnonnull(blob1);
517 blob2 = bloblist_add(TEST_TAG2, small_size, 0);
518 ut_assertnonnull(blob2);
521 ut_asserteq(sizeof(struct bloblist_hdr) +
522 sizeof(struct bloblist_rec) * 2 + small_size * 2,
525 /* Resize the first one, to check the boundary conditions */
526 ut_asserteq(-EINVAL, bloblist_resize(TEST_TAG, -1));
528 new_size = small_size + (hdr->total_size - hdr->used_size);
529 ut_asserteq(-ENOSPC, bloblist_resize(TEST_TAG, new_size + 1));
530 ut_assertok(bloblist_resize(TEST_TAG, new_size));
534 BLOBLIST_TEST(bloblist_test_resize_fail, UFT_BLOBLIST);
536 /* Test expanding the last blob in a bloblist */
537 static int bloblist_test_resize_last(struct unit_test_state *uts)
539 const uint small_size = 0x20;
540 struct bloblist_hdr *hdr;
541 void *blob1, *blob2, *blob2_new;
545 ptr = map_sysmem(TEST_ADDR, TEST_BLOBLIST_SIZE);
546 memset(ptr, ERASE_BYTE, TEST_BLOBLIST_SIZE);
549 /* Create two blobs */
550 ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0, 0));
551 blob1 = bloblist_add(TEST_TAG, small_size, 0);
552 ut_assertnonnull(blob1);
554 blob2 = bloblist_add(TEST_TAG2, small_size, 0);
555 ut_assertnonnull(blob2);
557 /* Check the byte after the last blob */
558 alloced_val = sizeof(struct bloblist_hdr) +
559 sizeof(struct bloblist_rec) * 2 + small_size * 2;
560 ut_asserteq(alloced_val, hdr->used_size);
561 ut_asserteq_ptr((void *)hdr + alloced_val, blob2 + small_size);
562 ut_asserteq((u8)ERASE_BYTE, *((u8 *)hdr + hdr->used_size));
564 /* Resize the second one, checking nothing changes */
565 ut_asserteq(0, bloblist_resize(TEST_TAG2, small_size + 4));
567 blob2_new = bloblist_find(TEST_TAG2, small_size + 4);
568 ut_asserteq_ptr(blob2, blob2_new);
571 * the new blob should encompass the byte we checked now, so it should
572 * be zeroed. This zeroing should affect only the four new bytes added
575 ut_asserteq(0, *((u8 *)hdr + alloced_val));
576 ut_asserteq((u8)ERASE_BYTE, *((u8 *)hdr + alloced_val + 4));
578 /* Check that the new top of the allocated blobs has not been touched */
579 alloced_val += BLOBLIST_BLOB_ALIGN;
580 ut_asserteq(alloced_val, hdr->used_size);
581 ut_asserteq((u8)ERASE_BYTE, *((u8 *)hdr + hdr->used_size));
585 BLOBLIST_TEST(bloblist_test_resize_last, UFT_BLOBLIST);
587 /* Check a completely full bloblist */
588 static int bloblist_test_blob_maxsize(struct unit_test_state *uts)
593 /* At the start there should be no records */
595 ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0, 0));
597 /* Add a blob that takes up all space */
598 size = TEST_BLOBLIST_SIZE - sizeof(struct bloblist_hdr) -
599 sizeof(struct bloblist_rec);
600 ptr = bloblist_add(TEST_TAG, size, 0);
601 ut_assertnonnull(ptr);
603 ptr = bloblist_add(TEST_TAG, size + 1, 0);
608 BLOBLIST_TEST(bloblist_test_blob_maxsize, UFT_BLOBLIST);