blob: 94a5b94f4796bc5ab0aaced756741b0c46264da5 [file] [log] [blame]
Simon Glass1d5006c2022-10-29 19:47:05 -06001# SPDX-License-Identifier: GPL-2.0+
2#
Simon Glass1d5006c2022-10-29 19:47:05 -06003# Copyright (c) 2018, Linaro Limited
4# Author: Takahiro Akashi <takahiro.akashi@linaro.org>
5
Simon Glassfbd95502022-10-29 19:47:06 -06006"""Helper functions for dealing with filesystems"""
7
Simon Glass1d5006c2022-10-29 19:47:05 -06008import re
9import os
10from subprocess import call, check_call, check_output, CalledProcessError
11
Richard Weinberger41eca322024-11-21 15:32:07 -070012def mk_fs(config, fs_type, size, prefix, src_dir=None, size_gran = 0x100000):
Simon Glass1d5006c2022-10-29 19:47:05 -060013 """Create a file system volume
14
15 Args:
Simon Glassfbd95502022-10-29 19:47:06 -060016 config (u_boot_config): U-Boot configuration
Simon Glass1d5006c2022-10-29 19:47:05 -060017 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 Weinberger41eca322024-11-21 15:32:07 -070020 src_dir (str): Root directory to use, or None for none
Christian Taedcked6048802023-11-15 13:44:23 +010021 size_gran (int): Size granularity of file system image in bytes
Simon Glassfbd95502022-10-29 19:47:06 -060022
23 Raises:
24 CalledProcessError: if any error occurs when creating the filesystem
Simon Glass1d5006c2022-10-29 19:47:05 -060025 """
Simon Glassfbd95502022-10-29 19:47:06 -060026 fs_img = f'{prefix}.{fs_type}.img'
Simon Glass08709212023-08-24 13:55:38 -060027 fs_img = os.path.join(config.persistent_data_dir, fs_img)
Simon Glass1d5006c2022-10-29 19:47:05 -060028
Christian Taedckeff0ceca2023-11-15 13:44:21 +010029 if fs_type == 'fat12':
30 mkfs_opt = '-F 12'
31 elif fs_type == 'fat16':
Simon Glass1d5006c2022-10-29 19:47:05 -060032 mkfs_opt = '-F 16'
33 elif fs_type == 'fat32':
34 mkfs_opt = '-F 32'
35 else:
36 mkfs_opt = ''
37
Marek Vasut60b83672025-03-17 04:12:50 +010038 if fs_type == 'exfat':
39 fs_lnxtype = 'exfat'
40 elif re.match('fat', fs_type) or fs_type == 'fs_generic':
Simon Glass1d5006c2022-10-29 19:47:05 -060041 fs_lnxtype = 'vfat'
42 else:
43 fs_lnxtype = fs_type
44
Richard Weinberger41eca322024-11-21 15:32:07 -070045 if src_dir:
46 if fs_lnxtype == 'ext4':
47 mkfs_opt = mkfs_opt + ' -d ' + src_dir
Marek Vasut60b83672025-03-17 04:12:50 +010048 elif fs_lnxtype != 'vfat' and fs_lnxtype != 'exfat':
Richard Weinberger41eca322024-11-21 15:32:07 -070049 raise ValueError(f'src_dir not implemented for fs {fs_lnxtype}')
50
Christian Taedcked6048802023-11-15 13:44:23 +010051 count = (size + size_gran - 1) // size_gran
Simon Glass1d5006c2022-10-29 19:47:05 -060052
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 Glassfbd95502022-10-29 19:47:06 -060058 check_call(f'rm -f {fs_img}', shell=True)
Christian Taedcked6048802023-11-15 13:44:23 +010059 check_call(f'dd if=/dev/zero of={fs_img} bs={size_gran} count={count}',
Simon Glassfbd95502022-10-29 19:47:06 -060060 shell=True)
61 check_call(f'mkfs.{fs_lnxtype} {mkfs_opt} {fs_img}', shell=True)
Simon Glass1d5006c2022-10-29 19:47:05 -060062 if fs_type == 'ext4':
Simon Glassfbd95502022-10-29 19:47:06 -060063 sb_content = check_output(f'tune2fs -l {fs_img}',
64 shell=True).decode()
Simon Glass1d5006c2022-10-29 19:47:05 -060065 if 'metadata_csum' in sb_content:
Simon Glassfbd95502022-10-29 19:47:06 -060066 check_call(f'tune2fs -O ^metadata_csum {fs_img}', shell=True)
Richard Weinberger41eca322024-11-21 15:32:07 -070067 elif fs_lnxtype == 'vfat' and src_dir:
68 check_call(f'mcopy -i {fs_img} -vsmpQ {src_dir}/* ::/', shell=True)
Marek Vasut60b83672025-03-17 04:12:50 +010069 elif fs_lnxtype == 'exfat' and src_dir:
70 check_call(f'fattools cp {src_dir}/* {fs_img}', shell=True)
Simon Glass1d5006c2022-10-29 19:47:05 -060071 return fs_img
72 except CalledProcessError:
Simon Glassfbd95502022-10-29 19:47:06 -060073 call(f'rm -f {fs_img}', shell=True)
Simon Glass1d5006c2022-10-29 19:47:05 -060074 raise
Simon Glassfbd95502022-10-29 19:47:06 -060075
76# Just for trying out
77if __name__ == "__main__":
78 import collections
79
80 CNF= collections.namedtuple('config', 'persistent_data_dir')
81
82 mk_fs(CNF('.'), 'ext4', 0x1000000, 'pref')