blob: e0ad94e77d81016ca206efb52de25e977e251878 [file] [log] [blame]
Simon Glass78b0ef52018-11-15 18:43:53 -07001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (c) 2018, Google Inc. All rights reserved.
4 */
5
Simon Glass78b0ef52018-11-15 18:43:53 -07006#include <bloblist.h>
7#include <log.h>
8#include <mapmem.h>
Simon Glass3ba929a2020-10-30 21:38:53 -06009#include <asm/global_data.h>
Simon Glass78b0ef52018-11-15 18:43:53 -070010#include <test/suites.h>
11#include <test/test.h>
12#include <test/ut.h>
13
14DECLARE_GLOBAL_DATA_PTR;
15
Simon Glass723ef9d2021-08-18 21:40:28 -060016/* Declare a new bloblist test */
Simon Glass78b0ef52018-11-15 18:43:53 -070017#define BLOBLIST_TEST(_name, _flags) \
18 UNIT_TEST(_name, _flags, bloblist_test)
19
20enum {
Simon Glassfd20df42022-01-12 19:26:19 -070021 TEST_TAG = BLOBLISTT_U_BOOT_SPL_HANDOFF,
22 TEST_TAG2 = BLOBLISTT_VBOOT_CTX,
23 TEST_TAG_MISSING = 0x10000,
Simon Glass78b0ef52018-11-15 18:43:53 -070024
25 TEST_SIZE = 10,
26 TEST_SIZE2 = 20,
Simon Glass06373972020-09-19 18:49:29 -060027 TEST_SIZE_LARGE = 0x3e0,
Simon Glass78b0ef52018-11-15 18:43:53 -070028
29 TEST_ADDR = CONFIG_BLOBLIST_ADDR,
Simon Glass06373972020-09-19 18:49:29 -060030 TEST_BLOBLIST_SIZE = 0x400,
Simon Glassd7985e62020-09-19 18:49:28 -060031
32 ERASE_BYTE = '\xff',
Simon Glass78b0ef52018-11-15 18:43:53 -070033};
34
Simon Glassf20a1132021-07-05 16:32:53 -060035static const char test1_str[] = "the eyes are open";
36static const char test2_str[] = "the mouth moves";
37
Simon Glass78b0ef52018-11-15 18:43:53 -070038static struct bloblist_hdr *clear_bloblist(void)
39{
40 struct bloblist_hdr *hdr;
41
Simon Glass3e85ea02020-01-27 08:49:52 -070042 /*
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.
46 */
Simon Glass78b0ef52018-11-15 18:43:53 -070047 hdr = map_sysmem(CONFIG_BLOBLIST_ADDR, TEST_BLOBLIST_SIZE);
Simon Glassd7985e62020-09-19 18:49:28 -060048 memset(hdr, ERASE_BYTE, TEST_BLOBLIST_SIZE);
Simon Glass3e85ea02020-01-27 08:49:52 -070049 memset(hdr, '\0', sizeof(*hdr));
Simon Glass78b0ef52018-11-15 18:43:53 -070050
51 return hdr;
52}
53
Simon Glass3e85ea02020-01-27 08:49:52 -070054static int check_zero(void *data, int size)
55{
56 u8 *ptr;
57 int i;
58
59 for (ptr = data, i = 0; i < size; i++, ptr++) {
60 if (*ptr)
61 return -EINVAL;
62 }
63
64 return 0;
65}
66
Simon Glass78b0ef52018-11-15 18:43:53 -070067static int bloblist_test_init(struct unit_test_state *uts)
68{
69 struct bloblist_hdr *hdr;
70
71 hdr = clear_bloblist();
72 ut_asserteq(-ENOENT, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
Simon Glass9341a3f2022-01-12 19:26:16 -070073 ut_asserteq_ptr(NULL, bloblist_check_magic(TEST_ADDR));
Simon Glassaee5a9c2023-12-27 13:07:09 -080074 ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0, 0));
Simon Glass9341a3f2022-01-12 19:26:16 -070075 ut_asserteq_ptr(hdr, bloblist_check_magic(TEST_ADDR));
Simon Glass78b0ef52018-11-15 18:43:53 -070076 hdr->version++;
77 ut_asserteq(-EPROTONOSUPPORT, bloblist_check(TEST_ADDR,
78 TEST_BLOBLIST_SIZE));
79
Simon Glassaee5a9c2023-12-27 13:07:09 -080080 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));
Simon Glass78b0ef52018-11-15 18:43:53 -070083
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));
Simon Glass9341a3f2022-01-12 19:26:16 -070087
88 hdr->magic++;
89 ut_asserteq_ptr(NULL, bloblist_check_magic(TEST_ADDR));
90 hdr->magic--;
91
Simon Glass78b0ef52018-11-15 18:43:53 -070092 hdr->flags++;
93 ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
94
95 return 1;
96}
Simon Glassd7ae4862024-10-28 13:47:54 +010097BLOBLIST_TEST(bloblist_test_init, UFT_BLOBLIST);
Simon Glass78b0ef52018-11-15 18:43:53 -070098
99static int bloblist_test_blob(struct unit_test_state *uts)
100{
101 struct bloblist_hdr *hdr;
102 struct bloblist_rec *rec, *rec2;
103 char *data;
104
105 /* At the start there should be no records */
106 hdr = clear_bloblist();
107 ut_assertnull(bloblist_find(TEST_TAG, TEST_BLOBLIST_SIZE));
Simon Glassaee5a9c2023-12-27 13:07:09 -0800108 ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0, 0));
Simon Glass39d5ad32023-12-27 13:07:08 -0800109 ut_asserteq(sizeof(struct bloblist_hdr), bloblist_get_size());
110 ut_asserteq(TEST_BLOBLIST_SIZE, bloblist_get_total_size());
Simon Glass01fd56a2022-01-12 19:26:23 -0700111 ut_asserteq(TEST_ADDR, bloblist_get_base());
Simon Glass7c58b082020-09-19 18:49:27 -0600112 ut_asserteq(map_to_sysmem(hdr), TEST_ADDR);
Simon Glass78b0ef52018-11-15 18:43:53 -0700113
114 /* Add a record and check that we can find it */
Simon Glass06373972020-09-19 18:49:29 -0600115 data = bloblist_add(TEST_TAG, TEST_SIZE, 0);
Simon Glass78b0ef52018-11-15 18:43:53 -0700116 rec = (void *)(hdr + 1);
Simon Glass7c58b082020-09-19 18:49:27 -0600117 ut_asserteq_addr(rec + 1, data);
Simon Glass78b0ef52018-11-15 18:43:53 -0700118 data = bloblist_find(TEST_TAG, TEST_SIZE);
Simon Glass7c58b082020-09-19 18:49:27 -0600119 ut_asserteq_addr(rec + 1, data);
Simon Glass78b0ef52018-11-15 18:43:53 -0700120
Simon Glass3e85ea02020-01-27 08:49:52 -0700121 /* Check the data is zeroed */
122 ut_assertok(check_zero(data, TEST_SIZE));
123
Simon Glass78b0ef52018-11-15 18:43:53 -0700124 /* Check the 'ensure' method */
Simon Glass7c58b082020-09-19 18:49:27 -0600125 ut_asserteq_addr(data, bloblist_ensure(TEST_TAG, TEST_SIZE));
Simon Glass78b0ef52018-11-15 18:43:53 -0700126 ut_assertnull(bloblist_ensure(TEST_TAG, TEST_SIZE2));
127 rec2 = (struct bloblist_rec *)(data + ALIGN(TEST_SIZE, BLOBLIST_ALIGN));
Simon Glass3e85ea02020-01-27 08:49:52 -0700128 ut_assertok(check_zero(data, TEST_SIZE));
Simon Glass78b0ef52018-11-15 18:43:53 -0700129
130 /* Check for a non-existent record */
Simon Glass7c58b082020-09-19 18:49:27 -0600131 ut_asserteq_addr(data, bloblist_ensure(TEST_TAG, TEST_SIZE));
132 ut_asserteq_addr(rec2 + 1, bloblist_ensure(TEST_TAG2, TEST_SIZE2));
Simon Glass78b0ef52018-11-15 18:43:53 -0700133 ut_assertnull(bloblist_find(TEST_TAG_MISSING, 0));
134
135 return 0;
136}
Simon Glassd7ae4862024-10-28 13:47:54 +0100137BLOBLIST_TEST(bloblist_test_blob, UFT_BLOBLIST);
Simon Glass78b0ef52018-11-15 18:43:53 -0700138
Simon Glass1e0304e2020-01-27 08:49:50 -0700139/* Check bloblist_ensure_size_ret() */
140static int bloblist_test_blob_ensure(struct unit_test_state *uts)
141{
142 void *data, *data2;
143 int size;
144
145 /* At the start there should be no records */
146 clear_bloblist();
Simon Glassaee5a9c2023-12-27 13:07:09 -0800147 ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0, 0));
Simon Glass1e0304e2020-01-27 08:49:50 -0700148
149 /* Test with an empty bloblist */
150 size = TEST_SIZE;
151 ut_assertok(bloblist_ensure_size_ret(TEST_TAG, &size, &data));
152 ut_asserteq(TEST_SIZE, size);
Simon Glass3e85ea02020-01-27 08:49:52 -0700153 ut_assertok(check_zero(data, TEST_SIZE));
Simon Glass1e0304e2020-01-27 08:49:50 -0700154
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);
Simon Glass7c58b082020-09-19 18:49:27 -0600158 ut_asserteq_addr(data, data2);
Simon Glass1e0304e2020-01-27 08:49:50 -0700159
160 /* Check that the size remains the same */
161 size = TEST_SIZE2;
162 ut_assertok(bloblist_ensure_size_ret(TEST_TAG, &size, &data));
163 ut_asserteq(TEST_SIZE, size);
164
165 /* Check running out of space */
166 size = TEST_SIZE_LARGE;
167 ut_asserteq(-ENOSPC, bloblist_ensure_size_ret(TEST_TAG2, &size, &data));
168
169 return 0;
170}
Simon Glassd7ae4862024-10-28 13:47:54 +0100171BLOBLIST_TEST(bloblist_test_blob_ensure, UFT_BLOBLIST);
Simon Glass1e0304e2020-01-27 08:49:50 -0700172
Simon Glass78b0ef52018-11-15 18:43:53 -0700173static int bloblist_test_bad_blob(struct unit_test_state *uts)
174{
175 struct bloblist_hdr *hdr;
176 void *data;
177
178 hdr = clear_bloblist();
Simon Glassaee5a9c2023-12-27 13:07:09 -0800179 ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0, 0));
Simon Glass78b0ef52018-11-15 18:43:53 -0700180 data = hdr + 1;
181 data += sizeof(struct bloblist_rec);
Simon Glass7c58b082020-09-19 18:49:27 -0600182 ut_asserteq_addr(data, bloblist_ensure(TEST_TAG, TEST_SIZE));
183 ut_asserteq_addr(data, bloblist_ensure(TEST_TAG, TEST_SIZE));
Simon Glass78b0ef52018-11-15 18:43:53 -0700184
185 return 0;
186}
Simon Glassd7ae4862024-10-28 13:47:54 +0100187BLOBLIST_TEST(bloblist_test_bad_blob, UFT_BLOBLIST);
Simon Glass78b0ef52018-11-15 18:43:53 -0700188
189static int bloblist_test_checksum(struct unit_test_state *uts)
190{
191 struct bloblist_hdr *hdr;
192 char *data, *data2;
193
194 hdr = clear_bloblist();
Simon Glassaee5a9c2023-12-27 13:07:09 -0800195 ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0, 0));
Simon Glass78b0ef52018-11-15 18:43:53 -0700196 ut_assertok(bloblist_finish());
197 ut_assertok(bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
198
199 /*
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.
203 */
204 hdr->flags--;
205 ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
206 hdr->flags++;
207
Simon Glass39d5ad32023-12-27 13:07:08 -0800208 hdr->total_size--;
Raymond Maobc7f0ab2024-02-03 08:36:21 -0800209 ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
Simon Glass39d5ad32023-12-27 13:07:08 -0800210 hdr->total_size++;
Simon Glass78b0ef52018-11-15 18:43:53 -0700211
212 hdr->spare++;
213 ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
214 hdr->spare--;
215
216 hdr->chksum++;
217 ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
218 hdr->chksum--;
219
Simon Glassaee5a9c2023-12-27 13:07:09 -0800220 hdr->align_log2++;
221 ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
222 hdr->align_log2--;
223
Simon Glass78b0ef52018-11-15 18:43:53 -0700224 /* Make sure the checksum changes when we add blobs */
Simon Glass06373972020-09-19 18:49:29 -0600225 data = bloblist_add(TEST_TAG, TEST_SIZE, 0);
Simon Glass78b0ef52018-11-15 18:43:53 -0700226 ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
227
Simon Glass06373972020-09-19 18:49:29 -0600228 data2 = bloblist_add(TEST_TAG2, TEST_SIZE2, 0);
Simon Glass78b0ef52018-11-15 18:43:53 -0700229 ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
230 ut_assertok(bloblist_finish());
231
232 /* It should also change if we change the data */
233 ut_assertok(bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
234 *data += 1;
235 ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
236 *data -= 1;
237
238 ut_assertok(bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
239 *data2 += 1;
240 ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
241 *data2 -= 1;
242
243 /*
Simon Glass8968c052023-12-27 13:07:05 -0800244 * Changing data outside the range of valid data should affect the
245 * checksum.
Simon Glass78b0ef52018-11-15 18:43:53 -0700246 */
247 ut_assertok(bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
248 data[TEST_SIZE]++;
Simon Glass8968c052023-12-27 13:07:05 -0800249 ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
250 data[TEST_SIZE]--;
251 ut_assertok(bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
252
Simon Glass78b0ef52018-11-15 18:43:53 -0700253 data2[TEST_SIZE2]++;
Simon Glass8968c052023-12-27 13:07:05 -0800254 ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
255 data[TEST_SIZE]--;
Simon Glass78b0ef52018-11-15 18:43:53 -0700256 ut_assertok(bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
257
258 return 0;
259}
Simon Glassd7ae4862024-10-28 13:47:54 +0100260BLOBLIST_TEST(bloblist_test_checksum, UFT_BLOBLIST);
Simon Glass78b0ef52018-11-15 18:43:53 -0700261
Simon Glassb936a972020-09-19 18:49:26 -0600262/* Test the 'bloblist info' command */
263static int bloblist_test_cmd_info(struct unit_test_state *uts)
264{
Simon Glassb936a972020-09-19 18:49:26 -0600265 struct bloblist_hdr *hdr;
266 char *data, *data2;
267
268 hdr = clear_bloblist();
Simon Glassaee5a9c2023-12-27 13:07:09 -0800269 ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0, 0));
Simon Glassb936a972020-09-19 18:49:26 -0600270 data = bloblist_ensure(TEST_TAG, TEST_SIZE);
271 data2 = bloblist_ensure(TEST_TAG2, TEST_SIZE2);
272
Simon Glassb936a972020-09-19 18:49:26 -0600273 run_command("bloblist info", 0);
Simon Glass39d5ad32023-12-27 13:07:08 -0800274 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");
Simon Glassb936a972020-09-19 18:49:26 -0600278
279 return 0;
280}
Simon Glassd7ae4862024-10-28 13:47:54 +0100281BLOBLIST_TEST(bloblist_test_cmd_info, UFT_BLOBLIST | UTF_CONSOLE);
Simon Glassb936a972020-09-19 18:49:26 -0600282
283/* Test the 'bloblist list' command */
284static int bloblist_test_cmd_list(struct unit_test_state *uts)
285{
Simon Glassb936a972020-09-19 18:49:26 -0600286 struct bloblist_hdr *hdr;
287 char *data, *data2;
288
289 hdr = clear_bloblist();
Simon Glassaee5a9c2023-12-27 13:07:09 -0800290 ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0, 0));
Simon Glassb936a972020-09-19 18:49:26 -0600291 data = bloblist_ensure(TEST_TAG, TEST_SIZE);
292 data2 = bloblist_ensure(TEST_TAG2, TEST_SIZE2);
293
Simon Glassb936a972020-09-19 18:49:26 -0600294 run_command("bloblist list", 0);
Simon Glassfd20df42022-01-12 19:26:19 -0700295 ut_assert_nextline("Address Size Tag Name");
Simon Glass871e5d02023-12-27 13:06:59 -0800296 ut_assert_nextline("%08lx %8x fff000 SPL hand-off",
Simon Glass7c58b082020-09-19 18:49:27 -0600297 (ulong)map_to_sysmem(data), TEST_SIZE);
Simon Glass871e5d02023-12-27 13:06:59 -0800298 ut_assert_nextline("%08lx %8x 202 Chrome OS vboot context",
Simon Glass7c58b082020-09-19 18:49:27 -0600299 (ulong)map_to_sysmem(data2), TEST_SIZE2);
Simon Glassb936a972020-09-19 18:49:26 -0600300
301 return 0;
302}
Simon Glassd7ae4862024-10-28 13:47:54 +0100303BLOBLIST_TEST(bloblist_test_cmd_list, UFT_BLOBLIST | UTF_CONSOLE);
Simon Glassb936a972020-09-19 18:49:26 -0600304
Simon Glassd7985e62020-09-19 18:49:28 -0600305/* Test alignment of bloblist blobs */
306static int bloblist_test_align(struct unit_test_state *uts)
307{
308 struct bloblist_hdr *hdr;
Simon Glass06373972020-09-19 18:49:29 -0600309 ulong addr;
310 char *data;
Simon Glassd7985e62020-09-19 18:49:28 -0600311 int i;
312
313 /* At the start there should be no records */
314 hdr = clear_bloblist();
Simon Glassaee5a9c2023-12-27 13:07:09 -0800315 ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0, 0));
Simon Glassd7985e62020-09-19 18:49:28 -0600316 ut_assertnull(bloblist_find(TEST_TAG, TEST_BLOBLIST_SIZE));
317
Simon Glass06373972020-09-19 18:49:29 -0600318 /* Check the default alignment */
Simon Glassd7985e62020-09-19 18:49:28 -0600319 for (i = 0; i < 3; i++) {
320 int size = i * 3;
321 ulong addr;
322 char *data;
323 int j;
324
Simon Glass06373972020-09-19 18:49:29 -0600325 data = bloblist_add(i, size, 0);
Simon Glassd7985e62020-09-19 18:49:28 -0600326 ut_assertnonnull(data);
327 addr = map_to_sysmem(data);
Simon Glass703a5da2023-12-27 13:07:07 -0800328 ut_asserteq(0, addr & (BLOBLIST_BLOB_ALIGN - 1));
Simon Glassd7985e62020-09-19 18:49:28 -0600329
330 /* Only the bytes in the blob data should be zeroed */
331 for (j = 0; j < size; j++)
332 ut_asserteq(0, data[j]);
Simon Glass703a5da2023-12-27 13:07:07 -0800333 for (; j < BLOBLIST_BLOB_ALIGN; j++)
Simon Glassd7985e62020-09-19 18:49:28 -0600334 ut_asserteq(ERASE_BYTE, data[j]);
335 }
336
Simon Glass06373972020-09-19 18:49:29 -0600337 /* Check larger alignment */
338 for (i = 0; i < 3; i++) {
Simon Glassc2aa4ef2023-12-27 13:07:00 -0800339 int align = 5 - i;
Simon Glass06373972020-09-19 18:49:29 -0600340
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));
345 }
346
347 /* Check alignment with an bloblist starting on a smaller alignment */
Simon Glass703a5da2023-12-27 13:07:07 -0800348 hdr = map_sysmem(TEST_ADDR + BLOBLIST_BLOB_ALIGN, TEST_BLOBLIST_SIZE);
Simon Glass06373972020-09-19 18:49:29 -0600349 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,
Simon Glassaee5a9c2023-12-27 13:07:09 -0800352 0, 0));
Simon Glass06373972020-09-19 18:49:29 -0600353
Simon Glassc2aa4ef2023-12-27 13:07:00 -0800354 data = bloblist_add(1, 5, BLOBLIST_ALIGN_LOG2 + 1);
Simon Glass06373972020-09-19 18:49:29 -0600355 ut_assertnonnull(data);
356 addr = map_to_sysmem(data);
Simon Glass703a5da2023-12-27 13:07:07 -0800357 ut_asserteq(0, addr & (BLOBLIST_BLOB_ALIGN * 2 - 1));
Simon Glass06373972020-09-19 18:49:29 -0600358
Simon Glassd7985e62020-09-19 18:49:28 -0600359 return 0;
360}
Simon Glassd7ae4862024-10-28 13:47:54 +0100361BLOBLIST_TEST(bloblist_test_align, UFT_BLOBLIST);
Simon Glassd7985e62020-09-19 18:49:28 -0600362
Simon Glassab7e7462021-01-13 20:29:43 -0700363/* Test relocation of a bloblist */
364static int bloblist_test_reloc(struct unit_test_state *uts)
365{
366 const uint large_size = TEST_BLOBLIST_SIZE;
367 const uint small_size = 0x20;
Raymond Mao1a99d2c2024-02-03 08:36:22 -0800368 void *new_ptr;
Simon Glassab7e7462021-01-13 20:29:43 -0700369 void *blob1, *blob2;
370 ulong new_addr;
371 ulong new_size;
372
Simon Glassaee5a9c2023-12-27 13:07:09 -0800373 ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0, 0));
Simon Glassab7e7462021-01-13 20:29:43 -0700374
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);
380
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);
Raymond Mao1a99d2c2024-02-03 08:36:22 -0800385 ut_assertok(bloblist_reloc(new_ptr, new_size));
Simon Glassab7e7462021-01-13 20:29:43 -0700386
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);
392
393 return 0;
394}
Simon Glassd7ae4862024-10-28 13:47:54 +0100395BLOBLIST_TEST(bloblist_test_reloc, UFT_BLOBLIST);
Simon Glassab7e7462021-01-13 20:29:43 -0700396
Simon Glassf20a1132021-07-05 16:32:53 -0600397/* Test expansion of a blob */
398static int bloblist_test_grow(struct unit_test_state *uts)
399{
400 const uint small_size = 0x20;
401 void *blob1, *blob2, *blob1_new;
402 struct bloblist_hdr *hdr;
403 void *ptr;
404
405 ptr = map_sysmem(TEST_ADDR, TEST_BLOBLIST_SIZE);
406 hdr = ptr;
407 memset(hdr, ERASE_BYTE, TEST_BLOBLIST_SIZE);
408
409 /* Create two blobs */
Simon Glassaee5a9c2023-12-27 13:07:09 -0800410 ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0, 0));
Simon Glassf20a1132021-07-05 16:32:53 -0600411 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);
415
416 blob2 = bloblist_add(TEST_TAG2, small_size, 0);
417 ut_assertnonnull(blob2);
418 strcpy(blob2, test2_str);
419
420 ut_asserteq(sizeof(struct bloblist_hdr) +
421 sizeof(struct bloblist_rec) * 2 + small_size * 2,
Simon Glass39d5ad32023-12-27 13:07:08 -0800422 hdr->used_size);
Simon Glassf20a1132021-07-05 16:32:53 -0600423
424 /* Resize the first one */
425 ut_assertok(bloblist_resize(TEST_TAG, small_size + 4));
426
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);
430
431 /* The new space should be zeroed */
432 ut_assertok(check_zero(blob1 + small_size, 4));
433
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);
438
439 /* The header should have more bytes in use */
440 hdr = ptr;
441 ut_asserteq(sizeof(struct bloblist_hdr) +
442 sizeof(struct bloblist_rec) * 2 + small_size * 2 +
Simon Glass703a5da2023-12-27 13:07:07 -0800443 BLOBLIST_BLOB_ALIGN,
Simon Glass39d5ad32023-12-27 13:07:08 -0800444 hdr->used_size);
Simon Glassf20a1132021-07-05 16:32:53 -0600445
446 return 0;
447}
Simon Glassd7ae4862024-10-28 13:47:54 +0100448BLOBLIST_TEST(bloblist_test_grow, UFT_BLOBLIST);
Simon Glassf20a1132021-07-05 16:32:53 -0600449
450/* Test shrinking of a blob */
451static int bloblist_test_shrink(struct unit_test_state *uts)
452{
453 const uint small_size = 0x20;
454 void *blob1, *blob2, *blob1_new;
455 struct bloblist_hdr *hdr;
456 int new_size;
457 void *ptr;
458
459 ptr = map_sysmem(TEST_ADDR, TEST_BLOBLIST_SIZE);
460
461 /* Create two blobs */
Simon Glassaee5a9c2023-12-27 13:07:09 -0800462 ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0, 0));
Simon Glassf20a1132021-07-05 16:32:53 -0600463 blob1 = bloblist_add(TEST_TAG, small_size, 0);
464 ut_assertnonnull(blob1);
465 strcpy(blob1, test1_str);
466
467 blob2 = bloblist_add(TEST_TAG2, small_size, 0);
468 ut_assertnonnull(blob2);
469 strcpy(blob2, test2_str);
470
471 hdr = ptr;
472 ut_asserteq(sizeof(struct bloblist_hdr) +
473 sizeof(struct bloblist_rec) * 2 + small_size * 2,
Simon Glass39d5ad32023-12-27 13:07:08 -0800474 hdr->used_size);
Simon Glassf20a1132021-07-05 16:32:53 -0600475
476 /* Resize the first one */
477 new_size = small_size - BLOBLIST_ALIGN - 4;
478 ut_assertok(bloblist_resize(TEST_TAG, new_size));
479
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);
483
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);
488
489 /* The header should have fewer bytes in use */
490 hdr = ptr;
491 ut_asserteq(sizeof(struct bloblist_hdr) +
492 sizeof(struct bloblist_rec) * 2 + small_size * 2 -
493 BLOBLIST_ALIGN,
Simon Glass39d5ad32023-12-27 13:07:08 -0800494 hdr->used_size);
Simon Glassf20a1132021-07-05 16:32:53 -0600495
496 return 0;
497}
Simon Glassd7ae4862024-10-28 13:47:54 +0100498BLOBLIST_TEST(bloblist_test_shrink, UFT_BLOBLIST);
Simon Glassf20a1132021-07-05 16:32:53 -0600499
500/* Test failing to adjust a blob size */
501static int bloblist_test_resize_fail(struct unit_test_state *uts)
502{
503 const uint small_size = 0x20;
504 struct bloblist_hdr *hdr;
505 void *blob1, *blob2;
506 int new_size;
507 void *ptr;
508
509 ptr = map_sysmem(TEST_ADDR, TEST_BLOBLIST_SIZE);
510
511 /* Create two blobs */
Simon Glassaee5a9c2023-12-27 13:07:09 -0800512 ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0, 0));
Simon Glassf20a1132021-07-05 16:32:53 -0600513 blob1 = bloblist_add(TEST_TAG, small_size, 0);
514 ut_assertnonnull(blob1);
515
516 blob2 = bloblist_add(TEST_TAG2, small_size, 0);
517 ut_assertnonnull(blob2);
518
519 hdr = ptr;
520 ut_asserteq(sizeof(struct bloblist_hdr) +
521 sizeof(struct bloblist_rec) * 2 + small_size * 2,
Simon Glass39d5ad32023-12-27 13:07:08 -0800522 hdr->used_size);
Simon Glassf20a1132021-07-05 16:32:53 -0600523
524 /* Resize the first one, to check the boundary conditions */
525 ut_asserteq(-EINVAL, bloblist_resize(TEST_TAG, -1));
526
Simon Glass39d5ad32023-12-27 13:07:08 -0800527 new_size = small_size + (hdr->total_size - hdr->used_size);
Simon Glassf20a1132021-07-05 16:32:53 -0600528 ut_asserteq(-ENOSPC, bloblist_resize(TEST_TAG, new_size + 1));
529 ut_assertok(bloblist_resize(TEST_TAG, new_size));
530
531 return 0;
532}
Simon Glassd7ae4862024-10-28 13:47:54 +0100533BLOBLIST_TEST(bloblist_test_resize_fail, UFT_BLOBLIST);
Simon Glassf20a1132021-07-05 16:32:53 -0600534
535/* Test expanding the last blob in a bloblist */
536static int bloblist_test_resize_last(struct unit_test_state *uts)
537{
538 const uint small_size = 0x20;
539 struct bloblist_hdr *hdr;
540 void *blob1, *blob2, *blob2_new;
541 int alloced_val;
542 void *ptr;
543
544 ptr = map_sysmem(TEST_ADDR, TEST_BLOBLIST_SIZE);
545 memset(ptr, ERASE_BYTE, TEST_BLOBLIST_SIZE);
546 hdr = ptr;
547
548 /* Create two blobs */
Simon Glassaee5a9c2023-12-27 13:07:09 -0800549 ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0, 0));
Simon Glassf20a1132021-07-05 16:32:53 -0600550 blob1 = bloblist_add(TEST_TAG, small_size, 0);
551 ut_assertnonnull(blob1);
552
553 blob2 = bloblist_add(TEST_TAG2, small_size, 0);
554 ut_assertnonnull(blob2);
555
556 /* Check the byte after the last blob */
557 alloced_val = sizeof(struct bloblist_hdr) +
558 sizeof(struct bloblist_rec) * 2 + small_size * 2;
Simon Glass39d5ad32023-12-27 13:07:08 -0800559 ut_asserteq(alloced_val, hdr->used_size);
Simon Glassf20a1132021-07-05 16:32:53 -0600560 ut_asserteq_ptr((void *)hdr + alloced_val, blob2 + small_size);
Simon Glass39d5ad32023-12-27 13:07:08 -0800561 ut_asserteq((u8)ERASE_BYTE, *((u8 *)hdr + hdr->used_size));
Simon Glassf20a1132021-07-05 16:32:53 -0600562
563 /* Resize the second one, checking nothing changes */
564 ut_asserteq(0, bloblist_resize(TEST_TAG2, small_size + 4));
565
566 blob2_new = bloblist_find(TEST_TAG2, small_size + 4);
567 ut_asserteq_ptr(blob2, blob2_new);
568
569 /*
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
572 * to the blob.
573 */
574 ut_asserteq(0, *((u8 *)hdr + alloced_val));
575 ut_asserteq((u8)ERASE_BYTE, *((u8 *)hdr + alloced_val + 4));
576
577 /* Check that the new top of the allocated blobs has not been touched */
Simon Glass703a5da2023-12-27 13:07:07 -0800578 alloced_val += BLOBLIST_BLOB_ALIGN;
Simon Glass39d5ad32023-12-27 13:07:08 -0800579 ut_asserteq(alloced_val, hdr->used_size);
580 ut_asserteq((u8)ERASE_BYTE, *((u8 *)hdr + hdr->used_size));
Simon Glassf20a1132021-07-05 16:32:53 -0600581
582 return 0;
583}
Simon Glassd7ae4862024-10-28 13:47:54 +0100584BLOBLIST_TEST(bloblist_test_resize_last, UFT_BLOBLIST);
Simon Glassf20a1132021-07-05 16:32:53 -0600585
Simon Glass3e26de52021-07-05 16:32:55 -0600586/* Check a completely full bloblist */
587static int bloblist_test_blob_maxsize(struct unit_test_state *uts)
588{
589 void *ptr;
590 int size;
591
592 /* At the start there should be no records */
593 clear_bloblist();
Simon Glassaee5a9c2023-12-27 13:07:09 -0800594 ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0, 0));
Simon Glass3e26de52021-07-05 16:32:55 -0600595
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);
601
602 ptr = bloblist_add(TEST_TAG, size + 1, 0);
603 ut_assertnull(ptr);
604
605 return 0;
606}
Simon Glassd7ae4862024-10-28 13:47:54 +0100607BLOBLIST_TEST(bloblist_test_blob_maxsize, UFT_BLOBLIST);
Simon Glass3e26de52021-07-05 16:32:55 -0600608
Simon Glassed38aef2020-05-10 11:40:03 -0600609int do_ut_bloblist(struct cmd_tbl *cmdtp, int flag, int argc,
610 char *const argv[])
Simon Glass78b0ef52018-11-15 18:43:53 -0700611{
Simon Glassb50211f2021-03-07 17:35:10 -0700612 struct unit_test *tests = UNIT_TEST_SUITE_START(bloblist_test);
613 const int n_ents = UNIT_TEST_SUITE_COUNT(bloblist_test);
Simon Glass78b0ef52018-11-15 18:43:53 -0700614
Philippe Reynes1f99f842019-12-17 19:07:04 +0100615 return cmd_ut_category("bloblist", "bloblist_test_",
616 tests, n_ents, argc, argv);
Simon Glass78b0ef52018-11-15 18:43:53 -0700617}