Skip to content

oofatfs: enable use of random volume IDs #7410

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions lib/oofatfs/ff.c
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,11 @@
#define GET_FATTIME() get_fattime()
#endif

#if FF_FS_MAKE_VOLID == 1
#define MAKE_VOLID(x) (make_volid())
#else
#define MAKE_VOLID(x) (GET_FATTIME())
#endif

/* File lock controls */
#if FF_FS_LOCK != 0
Expand Down Expand Up @@ -5421,6 +5426,7 @@ FRESULT f_mkfs (
DWORD tbl[3];
#endif

DWORD volid = MAKE_VOLID();

/* Check mounted drive and clear work area */
fs->fs_type = 0; /* Clear mounted volume */
Expand Down Expand Up @@ -5622,7 +5628,7 @@ FRESULT f_mkfs (
st_dword(buf + BPB_DataOfsEx, b_data - b_vol); /* Data offset [sector] */
st_dword(buf + BPB_NumClusEx, n_clst); /* Number of clusters */
st_dword(buf + BPB_RootClusEx, 2 + tbl[0] + tbl[1]); /* Root dir cluster # */
st_dword(buf + BPB_VolIDEx, GET_FATTIME()); /* VSN */
st_dword(buf + BPB_VolIDEx, volid); /* VSN */
st_word(buf + BPB_FSVerEx, 0x100); /* Filesystem version (1.00) */
for (buf[BPB_BytsPerSecEx] = 0, i = ss; i >>= 1; buf[BPB_BytsPerSecEx]++) ; /* Log2 of sector size [byte] */
for (buf[BPB_SecPerClusEx] = 0, i = au; i >>= 1; buf[BPB_SecPerClusEx]++) ; /* Log2 of cluster size [sector] */
Expand Down Expand Up @@ -5758,7 +5764,7 @@ FRESULT f_mkfs (
st_dword(buf + BPB_HiddSec, b_vol); /* Volume offset in the physical drive [sector] */
#if FF_MKFS_FAT32
if (fmt == FS_FAT32) {
st_dword(buf + BS_VolID32, GET_FATTIME()); /* VSN */
st_dword(buf + BS_VolID32, volid); /* VSN */
st_dword(buf + BPB_FATSz32, sz_fat); /* FAT size [sector] */
st_dword(buf + BPB_RootClus32, 2); /* Root directory cluster # (2) */
st_word(buf + BPB_FSInfo32, 1); /* Offset of FSINFO sector (VBR + 1) */
Expand All @@ -5769,7 +5775,7 @@ FRESULT f_mkfs (
} else
#endif
{
st_dword(buf + BS_VolID, GET_FATTIME()); /* VSN */
st_dword(buf + BS_VolID, volid); /* VSN */
st_word(buf + BPB_FATSz16, (WORD)sz_fat); /* FAT size [sector] */
buf[BS_DrvNum] = 0x80; /* Drive number (for int13) */
buf[BS_BootSig] = 0x29; /* Extended boot signature */
Expand Down
4 changes: 4 additions & 0 deletions lib/oofatfs/ff.h
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,10 @@ FRESULT f_setcp (WORD cp); /* Set curre
DWORD get_fattime (void);
#endif

#if FF_FS_MAKE_VOLID
DWORD make_volid (void);
#endif

/* LFN support functions */
#if FF_USE_LFN >= 1 /* Code conversion (defined in unicode.c) */
WCHAR ff_oem2uni (WCHAR oem, WORD cp); /* OEM code to Unicode conversion */
Expand Down
6 changes: 5 additions & 1 deletion lib/oofatfs/ffconf.h
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,10 @@
/ SemaphoreHandle_t and etc. A header file for O/S definitions needs to be
/ included somewhere in the scope of ff.h. */


#ifndef FF_FS_MAKE_VOLID
#define FF_FS_MAKE_VOLID (0)
#endif
/* The option FF_FS_MAKE_VOLID enables the use of a function to return a 32-bit volume identifier.
/ If it is disabled, a Volume ID based on the current time is used. */

/*--- End of configuration options ---*/
1 change: 1 addition & 0 deletions py/circuitpy_mpconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,7 @@ void supervisor_run_background_tasks_if_tick(void);
#define CIRCUITPY_DIGITALIO_HAVE_INVALID_DRIVE_MODE (0)
#endif

#define FF_FS_MAKE_VOLID (1)

#define MICROPY_PY_OPTIMIZE_PROPERTY_FLASH_SIZE (CIRCUITPY_OPTIMIZE_PROPERTY_FLASH_SIZE)

Expand Down
18 changes: 17 additions & 1 deletion supervisor/shared/fatfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@

#include "py/runtime.h"
#include "lib/oofatfs/ff.h"
#include "shared/timeutils/timeutils.h"
#include "shared-bindings/microcontroller/Processor.h"
#include "shared-bindings/os/__init__.h"
#include "shared-bindings/rtc/RTC.h"
#include "shared-bindings/time/__init__.h"
#include "shared/timeutils/timeutils.h"

DWORD _time_override = 0;
DWORD get_fattime(void) {
Expand All @@ -50,3 +52,17 @@ DWORD get_fattime(void) {
void override_fattime(DWORD time) {
_time_override = time;
}

DWORD make_volid(void) {
DWORD result;
if (!common_hal_os_urandom((uint8_t *)&result, sizeof(result))) {
#if CIRCUITPY_MICROCONTROLLER && COMMON_HAL_MCU_PROCESSOR_UID_LENGTH >= 4
uint8_t raw_id[COMMON_HAL_MCU_PROCESSOR_UID_LENGTH];
common_hal_mcu_processor_get_uid(raw_id);
memcpy(&result, raw_id, sizeof(result));
#else
result = (DWORD)common_hal_time_monotonic_ns();
#endif
}
return result;
}