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