feat(core/sdbackup): allow partitioning

- configure FatFS library for f_fdisk
- mkfs call now also creates the partition based on the intended use of
  the card
- WIP
pull/3441/head
obrusvit 6 months ago
parent d6791dcfc7
commit 8f26f9d9a8

@ -241,8 +241,8 @@
/* Definitions of logical drive - physical location conversion */
#define LD2PD(vol) (BYTE)(vol) /* Each logical drive is associated with the same physical drive number */
#define LD2PT(vol) 0 /* Auto partition search */
#define LD2PD(vol) VolToPart[vol].pd /* Get physical drive number */
#define LD2PT(vol) VolToPart[vol].pt /* Get partition number (0:auto search, 1..:forced partition number) */
/* Definitions of sector size */
@ -2967,7 +2967,7 @@ FRESULT f_mkdir (
FATFS *fs = NULL;
DIR dj = {0};
FFOBJID sobj = {0};
DWORD dcl = 0, pcl = 0, tm = 0;
DWORD dcl = 0, pcl = 0, tm = 0;
DEF_NAMBUF
@ -3638,6 +3638,33 @@ FRESULT f_mkfs (
/*-----------------------------------------------------------------------*/
/* Create Partition Table on the Physical Drive */
/*-----------------------------------------------------------------------*/
FRESULT f_fdisk (
BYTE pdrv, /* Physical drive number */
const LBA_t ptbl[], /* Pointer to the size table for each partitions */
void* work /* Pointer to the working buffer (null: use heap memory) */
)
{
BYTE *buf = (BYTE*)work;
DSTATUS stat = 0;
FRESULT res = 0;
/* Initialize the physical drive */
stat = disk_initialize(pdrv);
if (stat & STA_NOINIT) return FR_NOT_READY;
if (stat & STA_PROTECT) return FR_WRITE_PROTECTED;
if (!buf) return FR_NOT_ENOUGH_CORE;
res = create_partition(pdrv, ptbl, 0x07, buf); /* Create partitions (system ID is temporary setting and determined by f_mkfs) */
LEAVE_MKFS(res);
}
#endif /* !FF_FS_READONLY && FF_USE_MKFS */

@ -118,7 +118,7 @@ typedef struct {
BYTE pd; /* Physical drive number */
BYTE pt; /* Partition: 0:Auto detect, 1-4:Forced partition) */
} PARTITION;
extern PARTITION VolToPart[]; /* Volume - Partition mapping table */
extern const PARTITION VolToPart[]; /* Volume - Partition mapping table */
#endif
#if FF_STR_VOLUME_ID

@ -15,7 +15,7 @@ unifdef \
-DFF_FS_RPATH=0 \
-DFF_VOLUMES=1 \
-DFF_STR_VOLUME_ID=0 \
-DFF_MULTI_PARTITION=0 \
-DFF_MULTI_PARTITION=1 \
-DFF_USE_TRIM=0 \
-DFF_FS_NOFSINFO=0 \
-DFF_FS_TINY=0 \
@ -24,7 +24,6 @@ unifdef \
-DFF_FS_LOCK=0 \
-DFF_FS_REENTRANT=0 \
-DFF_LBA64=0 \
-DFF_MULTI_PARTITION=0 \
ff.c -o ff.c
unifdef \

@ -186,7 +186,7 @@
*/
#define FF_MULTI_PARTITION 0
#define FF_MULTI_PARTITION 1
/* This option switches support for multiple volumes on the physical drive.
/ By default (0), each logical drive number is bound to the same physical drive
/ number and only an FAT volume found on the physical drive will be mounted.

@ -87,6 +87,27 @@ MP_DEFINE_EXCEPTION(NoFilesystem, FatFSError)
} \
}
// Define and initialize the VolToPart array
// For more info, see: http://elm-chan.org/fsw/ff/doc/filename.html#vol
// Assumption: Trezor always operate with only one Volume
const PARTITION VolToPart[] = {
{0, 1} // Logical Volume 0 => Physical Disk 0, Partition 1
};
// Helper function to create a partition spanned over a portition (in
// percentage) of the card.
void make_partition(int disk_portion_p100) {
if ((disk_portion_p100 <= 0) || (disk_portion_p100 > 100)) {
FATFS_RAISE(FatFSError, FR_MKFS_ABORTED);
}
uint8_t working_buf[FF_MAX_SS] = {0};
LBA_t plist[] = {disk_portion_p100, 0};
FRESULT res = f_fdisk(0, plist, working_buf);
if (res != FR_OK) {
FATFS_RAISE(FatFSError, res);
}
}
DSTATUS disk_initialize(BYTE pdrv) { return disk_status(pdrv); }
DSTATUS disk_status(BYTE pdrv) {
@ -528,24 +549,38 @@ STATIC mp_obj_t mod_trezorio_fatfs_is_mounted() {
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_trezorio_fatfs_is_mounted_obj,
mod_trezorio_fatfs_is_mounted);
/// def mkfs() -> None:
/// def mkfs(for_sd_backup: bool=False) -> None:
/// """
/// Create a FAT volume on the SD card,
/// Create a FAT volume on the SD card.
/// If for_sd_backup is True, the volume consumes only a portion of the
/// card. Otherwise, the volume is created over the whole card.
/// """
STATIC mp_obj_t mod_trezorio_fatfs_mkfs() {
STATIC mp_obj_t mod_trezorio_fatfs_mkfs(size_t n_args, const mp_obj_t *args) {
if (_fatfs_instance_is_mounted()) {
FATFS_RAISE(FatFSError, FR_LOCKED);
}
// create partition
if (n_args > 0 && args[0] == mp_const_true) {
// for SD card backup: we need small partition and keep the rest unallocated
make_partition(60); // TODO decide on the exact portion
} else {
// for other use (SD salt): make the partitio over the whole space.
make_partition(100);
}
// create FAT volume mapped to the created partition
MKFS_PARM params = {FM_FAT32, 0, 0, 0, 0};
uint8_t working_buf[FF_MAX_SS] = {0};
FRESULT res = f_mkfs("", &params, working_buf, sizeof(working_buf));
if (res != FR_OK) {
FATFS_RAISE(FatFSError, res);
}
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_trezorio_fatfs_mkfs_obj,
mod_trezorio_fatfs_mkfs);
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_trezorio_fatfs_mkfs_obj, 0, 1,
mod_trezorio_fatfs_mkfs);
/// def setlabel(label: str) -> None:
/// """

@ -78,7 +78,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_trezorio_sdcard_capacity_obj,
/// """
/// Reads blocks starting with block_num from the SD card into buf.
/// Number of bytes read is length of buf rounded down to multiply of
/// SDCARD_BLOCK_SIZE. Returns True if in case of success, False otherwise.
/// SDCARD_BLOCK_SIZE.
/// """
STATIC mp_obj_t mod_trezorio_sdcard_read(mp_obj_t block_num, mp_obj_t buf) {
uint32_t block = trezor_obj_get_uint(block_num);
@ -97,7 +97,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorio_sdcard_read_obj,
/// """
/// Writes blocks starting with block_num from buf to the SD card.
/// Number of bytes written is length of buf rounded down to multiply of
/// SDCARD_BLOCK_SIZE. Returns True if in case of success, False otherwise.
/// SDCARD_BLOCK_SIZE.
/// """
STATIC mp_obj_t mod_trezorio_sdcard_write(mp_obj_t block_num, mp_obj_t buf) {
uint32_t block = trezor_obj_get_uint(block_num);

@ -166,9 +166,11 @@ def is_mounted() -> bool:
# extmod/modtrezorio/modtrezorio-fatfs.h
def mkfs() -> None:
def mkfs(for_sd_backup: bool=False) -> None:
"""
Create a FAT volume on the SD card,
Create a FAT volume on the SD card.
If for_sd_backup is True, the volume consumes only a portion of the
card. Otherwise, the volume is created over the whole card.
"""

@ -37,7 +37,7 @@ def read(block_num: int, buf: bytearray) -> None:
"""
Reads blocks starting with block_num from the SD card into buf.
Number of bytes read is length of buf rounded down to multiply of
SDCARD_BLOCK_SIZE. Returns True if in case of success, False otherwise.
SDCARD_BLOCK_SIZE.
"""
@ -46,5 +46,5 @@ def write(block_num: int, buf: bytes) -> None:
"""
Writes blocks starting with block_num from buf to the SD card.
Number of bytes written is length of buf rounded down to multiply of
SDCARD_BLOCK_SIZE. Returns True if in case of success, False otherwise.
SDCARD_BLOCK_SIZE.
"""

Loading…
Cancel
Save