Simon Glass | 1d5006c | 2022-10-29 19:47:05 -0600 | [diff] [blame] | 1 | # SPDX-License-Identifier: GPL-2.0+ |
| 2 | # |
Simon Glass | 1d5006c | 2022-10-29 19:47:05 -0600 | [diff] [blame] | 3 | # Copyright (c) 2018, Linaro Limited |
| 4 | # Author: Takahiro Akashi <takahiro.akashi@linaro.org> |
| 5 | |
Simon Glass | fbd9550 | 2022-10-29 19:47:06 -0600 | [diff] [blame] | 6 | """Helper functions for dealing with filesystems""" |
| 7 | |
Simon Glass | 1d5006c | 2022-10-29 19:47:05 -0600 | [diff] [blame] | 8 | import re |
| 9 | import os |
| 10 | from subprocess import call, check_call, check_output, CalledProcessError |
| 11 | |
Richard Weinberger | 41eca32 | 2024-11-21 15:32:07 -0700 | [diff] [blame] | 12 | def mk_fs(config, fs_type, size, prefix, src_dir=None, size_gran = 0x100000): |
Simon Glass | 1d5006c | 2022-10-29 19:47:05 -0600 | [diff] [blame] | 13 | """Create a file system volume |
| 14 | |
| 15 | Args: |
Simon Glass | fbd9550 | 2022-10-29 19:47:06 -0600 | [diff] [blame] | 16 | config (u_boot_config): U-Boot configuration |
Simon Glass | 1d5006c | 2022-10-29 19:47:05 -0600 | [diff] [blame] | 17 | fs_type (str): File system type, e.g. 'ext4' |
| 18 | size (int): Size of file system in bytes |
| 19 | prefix (str): Prefix string of volume's file name |
Richard Weinberger | 41eca32 | 2024-11-21 15:32:07 -0700 | [diff] [blame] | 20 | src_dir (str): Root directory to use, or None for none |
Christian Taedcke | d604880 | 2023-11-15 13:44:23 +0100 | [diff] [blame] | 21 | size_gran (int): Size granularity of file system image in bytes |
Simon Glass | fbd9550 | 2022-10-29 19:47:06 -0600 | [diff] [blame] | 22 | |
| 23 | Raises: |
| 24 | CalledProcessError: if any error occurs when creating the filesystem |
Simon Glass | 1d5006c | 2022-10-29 19:47:05 -0600 | [diff] [blame] | 25 | """ |
Simon Glass | fbd9550 | 2022-10-29 19:47:06 -0600 | [diff] [blame] | 26 | fs_img = f'{prefix}.{fs_type}.img' |
Simon Glass | 0870921 | 2023-08-24 13:55:38 -0600 | [diff] [blame] | 27 | fs_img = os.path.join(config.persistent_data_dir, fs_img) |
Simon Glass | 1d5006c | 2022-10-29 19:47:05 -0600 | [diff] [blame] | 28 | |
Christian Taedcke | ff0ceca | 2023-11-15 13:44:21 +0100 | [diff] [blame] | 29 | if fs_type == 'fat12': |
| 30 | mkfs_opt = '-F 12' |
| 31 | elif fs_type == 'fat16': |
Simon Glass | 1d5006c | 2022-10-29 19:47:05 -0600 | [diff] [blame] | 32 | mkfs_opt = '-F 16' |
| 33 | elif fs_type == 'fat32': |
| 34 | mkfs_opt = '-F 32' |
| 35 | else: |
| 36 | mkfs_opt = '' |
| 37 | |
Marek Vasut | 60b8367 | 2025-03-17 04:12:50 +0100 | [diff] [blame] | 38 | if fs_type == 'exfat': |
| 39 | fs_lnxtype = 'exfat' |
| 40 | elif re.match('fat', fs_type) or fs_type == 'fs_generic': |
Simon Glass | 1d5006c | 2022-10-29 19:47:05 -0600 | [diff] [blame] | 41 | fs_lnxtype = 'vfat' |
| 42 | else: |
| 43 | fs_lnxtype = fs_type |
| 44 | |
Richard Weinberger | 41eca32 | 2024-11-21 15:32:07 -0700 | [diff] [blame] | 45 | if src_dir: |
| 46 | if fs_lnxtype == 'ext4': |
| 47 | mkfs_opt = mkfs_opt + ' -d ' + src_dir |
Marek Vasut | 60b8367 | 2025-03-17 04:12:50 +0100 | [diff] [blame] | 48 | elif fs_lnxtype != 'vfat' and fs_lnxtype != 'exfat': |
Richard Weinberger | 41eca32 | 2024-11-21 15:32:07 -0700 | [diff] [blame] | 49 | raise ValueError(f'src_dir not implemented for fs {fs_lnxtype}') |
| 50 | |
Christian Taedcke | d604880 | 2023-11-15 13:44:23 +0100 | [diff] [blame] | 51 | count = (size + size_gran - 1) // size_gran |
Simon Glass | 1d5006c | 2022-10-29 19:47:05 -0600 | [diff] [blame] | 52 | |
| 53 | # Some distributions do not add /sbin to the default PATH, where mkfs lives |
| 54 | if '/sbin' not in os.environ["PATH"].split(os.pathsep): |
| 55 | os.environ["PATH"] += os.pathsep + '/sbin' |
| 56 | |
| 57 | try: |
Simon Glass | fbd9550 | 2022-10-29 19:47:06 -0600 | [diff] [blame] | 58 | check_call(f'rm -f {fs_img}', shell=True) |
Heinrich Schuchardt | 44cc62e | 2025-03-06 18:46:59 +0100 | [diff] [blame] | 59 | check_call(f'truncate -s $(( {size_gran} * {count} )) {fs_img}', |
Simon Glass | fbd9550 | 2022-10-29 19:47:06 -0600 | [diff] [blame] | 60 | shell=True) |
| 61 | check_call(f'mkfs.{fs_lnxtype} {mkfs_opt} {fs_img}', shell=True) |
Simon Glass | 1d5006c | 2022-10-29 19:47:05 -0600 | [diff] [blame] | 62 | if fs_type == 'ext4': |
Simon Glass | fbd9550 | 2022-10-29 19:47:06 -0600 | [diff] [blame] | 63 | sb_content = check_output(f'tune2fs -l {fs_img}', |
| 64 | shell=True).decode() |
Simon Glass | 1d5006c | 2022-10-29 19:47:05 -0600 | [diff] [blame] | 65 | if 'metadata_csum' in sb_content: |
Simon Glass | fbd9550 | 2022-10-29 19:47:06 -0600 | [diff] [blame] | 66 | check_call(f'tune2fs -O ^metadata_csum {fs_img}', shell=True) |
Richard Weinberger | 41eca32 | 2024-11-21 15:32:07 -0700 | [diff] [blame] | 67 | elif fs_lnxtype == 'vfat' and src_dir: |
| 68 | check_call(f'mcopy -i {fs_img} -vsmpQ {src_dir}/* ::/', shell=True) |
Marek Vasut | 60b8367 | 2025-03-17 04:12:50 +0100 | [diff] [blame] | 69 | elif fs_lnxtype == 'exfat' and src_dir: |
| 70 | check_call(f'fattools cp {src_dir}/* {fs_img}', shell=True) |
Simon Glass | 1d5006c | 2022-10-29 19:47:05 -0600 | [diff] [blame] | 71 | return fs_img |
| 72 | except CalledProcessError: |
Simon Glass | fbd9550 | 2022-10-29 19:47:06 -0600 | [diff] [blame] | 73 | call(f'rm -f {fs_img}', shell=True) |
Simon Glass | 1d5006c | 2022-10-29 19:47:05 -0600 | [diff] [blame] | 74 | raise |
Simon Glass | fbd9550 | 2022-10-29 19:47:06 -0600 | [diff] [blame] | 75 | |
Tom Rini | 20b0919 | 2025-03-20 07:59:24 -0600 | [diff] [blame] | 76 | def setup_image(ubman, devnum, part_type, img_size=20, second_part=False, |
| 77 | basename='mmc'): |
| 78 | """Create a disk image with a single partition |
| 79 | |
| 80 | Args: |
| 81 | ubman (ConsoleBase): Console to use |
| 82 | devnum (int): Device number to use, e.g. 1 |
| 83 | part_type (int): Partition type, e.g. 0xc for FAT32 |
| 84 | img_size (int): Image size in MiB |
| 85 | second_part (bool): True to contain a small second partition |
| 86 | basename (str): Base name to use in the filename, e.g. 'mmc' |
| 87 | |
| 88 | Returns: |
| 89 | tuple: |
| 90 | str: Filename of MMC image |
| 91 | str: Directory name of scratch directory |
| 92 | """ |
| 93 | fname = os.path.join(ubman.config.source_dir, f'{basename}{devnum}.img') |
| 94 | mnt = os.path.join(ubman.config.persistent_data_dir, 'scratch') |
| 95 | |
| 96 | spec = f'type={part_type:x}, size={img_size - 2}M, start=1M, bootable' |
| 97 | if second_part: |
| 98 | spec += '\ntype=c' |
| 99 | |
| 100 | try: |
| 101 | check_call(f'mkdir -p {mnt}', shell=True) |
Tom Rini | 61a6693 | 2025-03-20 07:59:25 -0600 | [diff] [blame] | 102 | check_call(f'qemu-img create {fname} {img_size}M', shell=True) |
Tom Rini | 20b0919 | 2025-03-20 07:59:24 -0600 | [diff] [blame] | 103 | check_call(f'printf "{spec}" | sfdisk {fname}', shell=True) |
| 104 | except CalledProcessError: |
| 105 | call(f'rm -f {fname}', shell=True) |
| 106 | raise |
| 107 | |
| 108 | return fname, mnt |
| 109 | |
Simon Glass | fbd9550 | 2022-10-29 19:47:06 -0600 | [diff] [blame] | 110 | # Just for trying out |
| 111 | if __name__ == "__main__": |
| 112 | import collections |
| 113 | |
| 114 | CNF= collections.namedtuple('config', 'persistent_data_dir') |
| 115 | |
| 116 | mk_fs(CNF('.'), 'ext4', 0x1000000, 'pref') |