blob: a89189e1124c6e65a81a8f7ce71939bd2bd620e9 [file] [log] [blame]
Sean Anderson2d3ffa32023-10-14 16:48:00 -04001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2023 Sean Anderson <seanga2@gmail.com>
4 */
5
6#include <common.h>
7#include <blk.h>
8#include <ext_common.h>
9#include <ext4fs.h>
10#include <fat.h>
11#include <fs.h>
12#include <memalign.h>
Sean Anderson156e31c2023-10-14 16:48:01 -040013#include <spl.h>
Sean Anderson2d3ffa32023-10-14 16:48:00 -040014#include <asm/io.h>
15#include <linux/stat.h>
16#include <test/spl.h>
17#include <test/ut.h>
18
19/**
20 * create_ext2() - Create an "ext2" filesystem with a single file
21 * @dst: The location of the new filesystem; MUST be zeroed
22 * @size: The size of the file
23 * @filename: The name of the file
24 * @data_offset: Filled with the offset of the file data from @dst
25 *
26 * Budget mke2fs. We use 1k blocks (to reduce overhead) with a single block
27 * group, which limits us to 8M of data. Almost every feature which increases
28 * complexity (checksums, hash tree directories, etc.) is disabled. We do cheat
29 * a little and use extents from ext4 to save having to deal with indirects, but
30 * U-Boot doesn't care.
31 *
32 * If @dst is %NULL, nothing is copied.
33 *
34 * Return: The size of the filesystem in bytes
35 */
36static size_t create_ext2(void *dst, size_t size, const char *filename,
37 size_t *data_offset)
38{
39 u32 super_block = 1;
40 u32 group_block = 2;
41 u32 block_bitmap_block = 3;
42 u32 inode_bitmap_block = 4;
43 u32 inode_table_block = 5;
44 u32 root_block = 6;
45 u32 file_block = 7;
46
47 u32 root_ino = EXT2_ROOT_INO;
48 u32 file_ino = EXT2_BOOT_LOADER_INO;
49
50 u32 block_size = EXT2_MIN_BLOCK_SIZE;
51 u32 inode_size = sizeof(struct ext2_inode);
52
53 u32 file_blocks = (size + block_size - 1) / block_size;
54 u32 blocks = file_block + file_blocks;
55 u32 inodes = block_size / inode_size;
56 u32 filename_len = strlen(filename);
57 u32 dirent_len = ALIGN(filename_len, sizeof(struct ext2_dirent)) +
58 sizeof(struct ext2_dirent);
59
60 struct ext2_sblock *sblock = dst + super_block * block_size;
61 struct ext2_block_group *bg = dst + group_block * block_size;
62 struct ext2_inode *inode_table = dst + inode_table_block * block_size;
63 struct ext2_inode *root_inode = &inode_table[root_ino - 1];
64 struct ext2_inode *file_inode = &inode_table[file_ino - 1];
65 struct ext4_extent_header *ext_block = (void *)&file_inode->b;
66 struct ext4_extent *extent = (void *)(ext_block + 1);
67 struct ext2_dirent *dot = dst + root_block * block_size;
68 struct ext2_dirent *dotdot = dot + 2;
69 struct ext2_dirent *dirent = dotdot + 2;
70 struct ext2_dirent *last = ((void *)dirent) + dirent_len;
71
72 /* Make sure we fit in one block group */
73 if (blocks > block_size * 8)
74 return 0;
75
76 if (filename_len > EXT2_NAME_LEN)
77 return 0;
78
79 if (data_offset)
80 *data_offset = file_block * block_size;
81
82 if (!dst)
83 goto out;
84
85 sblock->total_inodes = cpu_to_le32(inodes);
86 sblock->total_blocks = cpu_to_le32(blocks);
87 sblock->first_data_block = cpu_to_le32(super_block);
88 sblock->blocks_per_group = cpu_to_le32(blocks);
89 sblock->fragments_per_group = cpu_to_le32(blocks);
90 sblock->inodes_per_group = cpu_to_le32(inodes);
91 sblock->magic = cpu_to_le16(EXT2_MAGIC);
92 /* Done mostly so we can pretend to be (in)compatible */
93 sblock->revision_level = cpu_to_le32(EXT2_DYNAMIC_REV);
94 /* Not really accurate but it doesn't matter */
95 sblock->first_inode = cpu_to_le32(EXT2_GOOD_OLD_FIRST_INO);
96 sblock->inode_size = cpu_to_le32(inode_size);
97 sblock->feature_incompat = cpu_to_le32(EXT4_FEATURE_INCOMPAT_EXTENTS);
98
99 bg->block_id = cpu_to_le32(block_bitmap_block);
100 bg->inode_id = cpu_to_le32(inode_bitmap_block);
101 bg->inode_table_id = cpu_to_le32(inode_table_block);
102
103 /*
104 * All blocks/inodes are in-use. I don't want to have to deal with
105 * endianness, so just fill everything in.
106 */
107 memset(dst + block_bitmap_block * block_size, 0xff, block_size * 2);
108
109 root_inode->mode = cpu_to_le16(S_IFDIR | 0755);
110 root_inode->size = cpu_to_le32(block_size);
111 root_inode->nlinks = cpu_to_le16(3);
112 root_inode->blockcnt = cpu_to_le32(1);
113 root_inode->flags = cpu_to_le32(EXT4_TOPDIR_FL);
114 root_inode->b.blocks.dir_blocks[0] = root_block;
115
116 file_inode->mode = cpu_to_le16(S_IFREG | 0644);
117 file_inode->size = cpu_to_le32(size);
118 file_inode->nlinks = cpu_to_le16(1);
119 file_inode->blockcnt = cpu_to_le32(file_blocks);
120 file_inode->flags = cpu_to_le32(EXT4_EXTENTS_FL);
121 ext_block->eh_magic = cpu_to_le16(EXT4_EXT_MAGIC);
122 ext_block->eh_entries = cpu_to_le16(1);
123 ext_block->eh_max = cpu_to_le16(sizeof(file_inode->b) /
124 sizeof(*ext_block) - 1);
125 extent->ee_len = cpu_to_le16(file_blocks);
126 extent->ee_start_lo = cpu_to_le16(file_block);
127
128 /* I'm not sure we need these, but it can't hurt */
129 dot->inode = cpu_to_le32(root_ino);
130 dot->direntlen = cpu_to_le16(2 * sizeof(*dot));
131 dot->namelen = 1;
132 dot->filetype = FILETYPE_DIRECTORY;
133 memcpy(dot + 1, ".", dot->namelen);
134
135 dotdot->inode = cpu_to_le32(root_ino);
136 dotdot->direntlen = cpu_to_le16(2 * sizeof(*dotdot));
137 dotdot->namelen = 2;
138 dotdot->filetype = FILETYPE_DIRECTORY;
139 memcpy(dotdot + 1, "..", dotdot->namelen);
140
141 dirent->inode = cpu_to_le32(file_ino);
142 dirent->direntlen = cpu_to_le16(dirent_len);
143 dirent->namelen = filename_len;
144 dirent->filetype = FILETYPE_REG;
145 memcpy(dirent + 1, filename, filename_len);
146
147 last->direntlen = block_size - dirent_len;
148
149out:
150 return (size_t)blocks * block_size;
151}
152
153/**
154 * create_fat() - Create a FAT32 filesystem with a single file
155 * @dst: The location of the new filesystem; MUST be zeroed
156 * @size: The size of the file
157 * @filename: The name of the file
158 * @data_offset: Filled with the offset of the file data from @dst
159 *
160 * Budget mkfs.fat. We use FAT32 (so I don't have to deal with FAT12) with no
161 * info sector, and a single one-sector FAT. This limits us to 64k of data
162 * (enough for anyone). The filename must fit in 8.3.
163 *
164 * If @dst is %NULL, nothing is copied.
165 *
166 * Return: The size of the filesystem in bytes
167 */
168static size_t create_fat(void *dst, size_t size, const char *filename,
169 size_t *data_offset)
170{
171 u16 boot_sector = 0;
172 u16 fat_sector = 1;
173 u32 root_sector = 2;
174 u32 file_sector = 3;
175
176 u16 sector_size = 512;
177 u32 file_sectors = (size + sector_size - 1) / sector_size;
178 u32 sectors = file_sector + file_sectors;
179
180 char *ext;
181 size_t filename_len, ext_len;
182 int i;
183
184 struct boot_sector *bs = dst + boot_sector * sector_size;
185 struct volume_info *vi = (void *)(bs + 1);
186 __le32 *fat = dst + fat_sector * sector_size;
187 struct dir_entry *dirent = dst + root_sector * sector_size;
188
189 /* Make sure we fit in the FAT */
190 if (sectors > sector_size / sizeof(u32))
191 return 0;
192
193 ext = strchr(filename, '.');
194 if (ext) {
195 filename_len = ext - filename;
196 ext++;
197 ext_len = strlen(ext);
198 } else {
199 filename_len = strlen(filename);
200 ext_len = 0;
201 }
202
203 if (filename_len > 8 || ext_len > 3)
204 return 0;
205
206 if (data_offset)
207 *data_offset = file_sector * sector_size;
208
209 if (!dst)
210 goto out;
211
212 bs->sector_size[0] = sector_size & 0xff;
213 bs->sector_size[1] = sector_size >> 8;
214 bs->cluster_size = 1;
215 bs->reserved = cpu_to_le16(fat_sector);
216 bs->fats = 1;
217 bs->media = 0xf8;
218 bs->total_sect = cpu_to_le32(sectors);
219 bs->fat32_length = cpu_to_le32(1);
220 bs->root_cluster = cpu_to_le32(root_sector);
221
222 vi->ext_boot_sign = 0x29;
Christian Taedckec41708c2023-11-15 13:44:17 +0100223 memcpy(vi->fs_type, "FAT32 ", sizeof(vi->fs_type));
Sean Anderson2d3ffa32023-10-14 16:48:00 -0400224
225 memcpy(dst + 0x1fe, "\x55\xAA", 2);
226
227 fat[0] = cpu_to_le32(0x0ffffff8);
228 fat[1] = cpu_to_le32(0x0fffffff);
229 fat[2] = cpu_to_le32(0x0ffffff8);
230 for (i = file_sector; file_sectors > 1; file_sectors--, i++)
231 fat[i] = cpu_to_le32(i + 1);
232 fat[i] = cpu_to_le32(0x0ffffff8);
233
234 for (i = 0; i < sizeof(dirent->nameext.name); i++) {
235 if (i < filename_len)
236 dirent->nameext.name[i] = toupper(filename[i]);
237 else
238 dirent->nameext.name[i] = ' ';
239 }
240
241 for (i = 0; i < sizeof(dirent->nameext.ext); i++) {
242 if (i < ext_len)
243 dirent->nameext.ext[i] = toupper(ext[i]);
244 else
245 dirent->nameext.ext[i] = ' ';
246 }
247
248 dirent->start = cpu_to_le16(file_sector);
249 dirent->size = cpu_to_le32(size);
250
251out:
252 return sectors * sector_size;
253}
254
255typedef size_t (*create_fs_t)(void *, size_t, const char *, size_t *);
256
257static int spl_test_fs(struct unit_test_state *uts, const char *test_name,
258 create_fs_t create)
259{
260 const char *filename = CONFIG_SPL_FS_LOAD_PAYLOAD_NAME;
261 struct blk_desc *dev_desc;
262 char *data_write, *data_read;
263 void *fs;
264 size_t fs_size, fs_data, fs_blocks, data_size = SPL_TEST_DATA_SIZE;
265 loff_t actread;
266
267 fs_size = create(NULL, data_size, filename, &fs_data);
268 ut_assert(fs_size);
269 fs = calloc(fs_size, 1);
270 ut_assertnonnull(fs);
271
272 data_write = fs + fs_data;
273 generate_data(data_write, data_size, test_name);
274 ut_asserteq(fs_size, create(fs, data_size, filename, NULL));
275
276 dev_desc = blk_get_devnum_by_uclass_id(UCLASS_MMC, 0);
277 ut_assertnonnull(dev_desc);
278 ut_asserteq(512, dev_desc->blksz);
279 fs_blocks = fs_size / dev_desc->blksz;
280 ut_asserteq(fs_blocks, blk_dwrite(dev_desc, 0, fs_blocks, fs));
281
282 /* We have to use malloc so we can call virt_to_phys */
283 data_read = malloc_cache_aligned(data_size);
284 ut_assertnonnull(data_read);
285 ut_assertok(fs_set_blk_dev_with_part(dev_desc, 0));
286 ut_assertok(fs_read("/" CONFIG_SPL_FS_LOAD_PAYLOAD_NAME,
287 virt_to_phys(data_read), 0, data_size, &actread));
288 ut_asserteq(data_size, actread);
289 ut_asserteq_mem(data_write, data_read, data_size);
290
291 free(data_read);
292 free(fs);
293 return 0;
294}
295
296static int spl_test_ext(struct unit_test_state *uts)
297{
298 return spl_test_fs(uts, __func__, create_ext2);
299}
300SPL_TEST(spl_test_ext, DM_FLAGS);
301
302static int spl_test_fat(struct unit_test_state *uts)
303{
304 spl_fat_force_reregister();
305 return spl_test_fs(uts, __func__, create_fat);
306}
307SPL_TEST(spl_test_fat, DM_FLAGS);
Sean Anderson156e31c2023-10-14 16:48:01 -0400308
Sean Anderson576295d2023-10-14 16:48:02 -0400309static bool spl_mmc_raw;
310
311u32 spl_mmc_boot_mode(struct mmc *mmc, const u32 boot_device)
312{
313 return spl_mmc_raw ? MMCSD_MODE_RAW : MMCSD_MODE_FS;
314}
315
Sean Anderson156e31c2023-10-14 16:48:01 -0400316static int spl_test_mmc_fs(struct unit_test_state *uts, const char *test_name,
Sean Anderson576295d2023-10-14 16:48:02 -0400317 enum spl_test_image type, create_fs_t create_fs,
318 bool blk_mode)
Sean Anderson156e31c2023-10-14 16:48:01 -0400319{
320 const char *filename = CONFIG_SPL_FS_LOAD_PAYLOAD_NAME;
321 struct blk_desc *dev_desc;
322 size_t fs_size, fs_data, img_size, img_data,
Sean Anderson8cba9882023-11-08 11:48:46 -0500323 plain_size = SPL_TEST_DATA_SIZE;
Sean Anderson156e31c2023-10-14 16:48:01 -0400324 struct spl_image_info info_write = {
325 .name = test_name,
Sean Anderson8cba9882023-11-08 11:48:46 -0500326 .size = type == LEGACY_LZMA ? lzma_compressed_size :
327 plain_size,
Sean Anderson156e31c2023-10-14 16:48:01 -0400328 }, info_read = { };
329 struct disk_partition part = {
330 .start = 1,
331 .sys_ind = 0x83,
332 };
Sean Anderson576295d2023-10-14 16:48:02 -0400333 struct spl_image_loader *loader =
334 SPL_LOAD_IMAGE_GET(0, BOOT_DEVICE_MMC1, spl_mmc_load_image);
335 struct spl_boot_device bootdev = {
336 .boot_device = loader->boot_device,
337 };
Sean Anderson156e31c2023-10-14 16:48:01 -0400338 void *fs;
Sean Anderson8cba9882023-11-08 11:48:46 -0500339 char *data, *plain;
Sean Anderson156e31c2023-10-14 16:48:01 -0400340
341 img_size = create_image(NULL, type, &info_write, &img_data);
342 ut_assert(img_size);
343 fs_size = create_fs(NULL, img_size, filename, &fs_data);
344 ut_assert(fs_size);
345 fs = calloc(fs_size, 1);
346 ut_assertnonnull(fs);
347
348 data = fs + fs_data + img_data;
Sean Anderson8cba9882023-11-08 11:48:46 -0500349 if (type == LEGACY_LZMA) {
350 plain = malloc(plain_size);
351 ut_assertnonnull(plain);
352 generate_data(plain, plain_size, "lzma");
353 memcpy(data, lzma_compressed, lzma_compressed_size);
354 } else {
355 plain = data;
356 generate_data(plain, plain_size, test_name);
357 }
Sean Anderson156e31c2023-10-14 16:48:01 -0400358 ut_asserteq(img_size, create_image(fs + fs_data, type, &info_write,
359 NULL));
360 ut_asserteq(fs_size, create_fs(fs, img_size, filename, NULL));
361
362 dev_desc = blk_get_devnum_by_uclass_id(UCLASS_MMC, 0);
363 ut_assertnonnull(dev_desc);
364
365 ut_asserteq(512, dev_desc->blksz);
366 part.size = fs_size / dev_desc->blksz;
367 ut_assertok(write_mbr_partitions(dev_desc, &part, 1, 0));
368 ut_asserteq(part.size, blk_dwrite(dev_desc, part.start, part.size, fs));
369
Sean Anderson576295d2023-10-14 16:48:02 -0400370 spl_mmc_raw = false;
371 if (blk_mode)
372 ut_assertok(spl_blk_load_image(&info_read, &bootdev, UCLASS_MMC,
373 0, 1));
374 else
375 ut_assertok(loader->load_image(&info_read, &bootdev));
Sean Anderson156e31c2023-10-14 16:48:01 -0400376 if (check_image_info(uts, &info_write, &info_read))
377 return CMD_RET_FAILURE;
Sean Anderson8cba9882023-11-08 11:48:46 -0500378 if (type == LEGACY_LZMA)
379 ut_asserteq(plain_size, info_read.size);
380 ut_asserteq_mem(plain, phys_to_virt(info_write.load_addr), plain_size);
Sean Anderson156e31c2023-10-14 16:48:01 -0400381
Sean Anderson8cba9882023-11-08 11:48:46 -0500382 if (type == LEGACY_LZMA)
383 free(plain);
Sean Anderson156e31c2023-10-14 16:48:01 -0400384 free(fs);
385 return 0;
386}
387
388static int spl_test_blk(struct unit_test_state *uts, const char *test_name,
389 enum spl_test_image type)
390{
391 spl_fat_force_reregister();
Sean Anderson576295d2023-10-14 16:48:02 -0400392 if (spl_test_mmc_fs(uts, test_name, type, create_fat, true))
Sean Anderson156e31c2023-10-14 16:48:01 -0400393 return CMD_RET_FAILURE;
394
Sean Anderson576295d2023-10-14 16:48:02 -0400395 return spl_test_mmc_fs(uts, test_name, type, create_ext2, true);
Sean Anderson156e31c2023-10-14 16:48:01 -0400396}
397SPL_IMG_TEST(spl_test_blk, LEGACY, DM_FLAGS);
Sean Anderson73143252023-11-08 11:48:54 -0500398SPL_IMG_TEST(spl_test_blk, LEGACY_LZMA, DM_FLAGS);
399SPL_IMG_TEST(spl_test_blk, IMX8, DM_FLAGS);
Sean Anderson156e31c2023-10-14 16:48:01 -0400400SPL_IMG_TEST(spl_test_blk, FIT_EXTERNAL, DM_FLAGS);
401SPL_IMG_TEST(spl_test_blk, FIT_INTERNAL, DM_FLAGS);
Sean Anderson576295d2023-10-14 16:48:02 -0400402
403static int spl_test_mmc_write_image(struct unit_test_state *uts, void *img,
404 size_t img_size)
405{
406 struct blk_desc *dev_desc;
407 size_t img_blocks;
408
409 dev_desc = blk_get_devnum_by_uclass_id(UCLASS_MMC, 0);
410 ut_assertnonnull(dev_desc);
411
412 img_blocks = DIV_ROUND_UP(img_size, dev_desc->blksz);
413 ut_asserteq(img_blocks, blk_dwrite(dev_desc,
414 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR,
415 img_blocks, img));
416
417 spl_mmc_raw = true;
418 return 0;
419}
420
421static int spl_test_mmc(struct unit_test_state *uts, const char *test_name,
422 enum spl_test_image type)
423{
424 spl_mmc_clear_cache();
425 spl_fat_force_reregister();
426
Sean Andersonf727cc12023-11-08 11:48:48 -0500427 if (spl_test_mmc_fs(uts, test_name, type, create_ext2, false))
Sean Anderson576295d2023-10-14 16:48:02 -0400428 return CMD_RET_FAILURE;
429
Sean Anderson09a46022023-11-08 11:48:49 -0500430 if (spl_test_mmc_fs(uts, test_name, type, create_fat, false))
Sean Anderson576295d2023-10-14 16:48:02 -0400431 return CMD_RET_FAILURE;
432
433 return do_spl_test_load(uts, test_name, type,
434 SPL_LOAD_IMAGE_GET(0, BOOT_DEVICE_MMC1,
435 spl_mmc_load_image),
436 spl_test_mmc_write_image);
437}
438SPL_IMG_TEST(spl_test_mmc, LEGACY, DM_FLAGS);
Sean Andersonf727cc12023-11-08 11:48:48 -0500439SPL_IMG_TEST(spl_test_mmc, LEGACY_LZMA, DM_FLAGS);
Sean Anderson576295d2023-10-14 16:48:02 -0400440SPL_IMG_TEST(spl_test_mmc, IMX8, DM_FLAGS);
441SPL_IMG_TEST(spl_test_mmc, FIT_EXTERNAL, DM_FLAGS);
442SPL_IMG_TEST(spl_test_mmc, FIT_INTERNAL, DM_FLAGS);