Skip to content

Commit 4d33cf4

Browse files
authored
Merge pull request #7410 from jepler/unique-fat-volid
oofatfs: enable use of random volume IDs
2 parents af3a2df + 3d66ed2 commit 4d33cf4

File tree

5 files changed

+36
-5
lines changed

5 files changed

+36
-5
lines changed

lib/oofatfs/ff.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,11 @@
257257
#define GET_FATTIME() get_fattime()
258258
#endif
259259

260+
#if FF_FS_MAKE_VOLID == 1
261+
#define MAKE_VOLID(x) (make_volid())
262+
#else
263+
#define MAKE_VOLID(x) (GET_FATTIME())
264+
#endif
260265

261266
/* File lock controls */
262267
#if FF_FS_LOCK != 0
@@ -5421,6 +5426,7 @@ FRESULT f_mkfs (
54215426
DWORD tbl[3];
54225427
#endif
54235428

5429+
DWORD volid = MAKE_VOLID();
54245430

54255431
/* Check mounted drive and clear work area */
54265432
fs->fs_type = 0; /* Clear mounted volume */
@@ -5622,7 +5628,7 @@ FRESULT f_mkfs (
56225628
st_dword(buf + BPB_DataOfsEx, b_data - b_vol); /* Data offset [sector] */
56235629
st_dword(buf + BPB_NumClusEx, n_clst); /* Number of clusters */
56245630
st_dword(buf + BPB_RootClusEx, 2 + tbl[0] + tbl[1]); /* Root dir cluster # */
5625-
st_dword(buf + BPB_VolIDEx, GET_FATTIME()); /* VSN */
5631+
st_dword(buf + BPB_VolIDEx, volid); /* VSN */
56265632
st_word(buf + BPB_FSVerEx, 0x100); /* Filesystem version (1.00) */
56275633
for (buf[BPB_BytsPerSecEx] = 0, i = ss; i >>= 1; buf[BPB_BytsPerSecEx]++) ; /* Log2 of sector size [byte] */
56285634
for (buf[BPB_SecPerClusEx] = 0, i = au; i >>= 1; buf[BPB_SecPerClusEx]++) ; /* Log2 of cluster size [sector] */
@@ -5758,7 +5764,7 @@ FRESULT f_mkfs (
57585764
st_dword(buf + BPB_HiddSec, b_vol); /* Volume offset in the physical drive [sector] */
57595765
#if FF_MKFS_FAT32
57605766
if (fmt == FS_FAT32) {
5761-
st_dword(buf + BS_VolID32, GET_FATTIME()); /* VSN */
5767+
st_dword(buf + BS_VolID32, volid); /* VSN */
57625768
st_dword(buf + BPB_FATSz32, sz_fat); /* FAT size [sector] */
57635769
st_dword(buf + BPB_RootClus32, 2); /* Root directory cluster # (2) */
57645770
st_word(buf + BPB_FSInfo32, 1); /* Offset of FSINFO sector (VBR + 1) */
@@ -5769,7 +5775,7 @@ FRESULT f_mkfs (
57695775
} else
57705776
#endif
57715777
{
5772-
st_dword(buf + BS_VolID, GET_FATTIME()); /* VSN */
5778+
st_dword(buf + BS_VolID, volid); /* VSN */
57735779
st_word(buf + BPB_FATSz16, (WORD)sz_fat); /* FAT size [sector] */
57745780
buf[BS_DrvNum] = 0x80; /* Drive number (for int13) */
57755781
buf[BS_BootSig] = 0x29; /* Extended boot signature */

lib/oofatfs/ff.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,10 @@ FRESULT f_setcp (WORD cp); /* Set curre
334334
DWORD get_fattime (void);
335335
#endif
336336

337+
#if FF_FS_MAKE_VOLID
338+
DWORD make_volid (void);
339+
#endif
340+
337341
/* LFN support functions */
338342
#if FF_USE_LFN >= 1 /* Code conversion (defined in unicode.c) */
339343
WCHAR ff_oem2uni (WCHAR oem, WORD cp); /* OEM code to Unicode conversion */

lib/oofatfs/ffconf.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,10 @@
373373
/ SemaphoreHandle_t and etc. A header file for O/S definitions needs to be
374374
/ included somewhere in the scope of ff.h. */
375375

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

378382
/*--- End of configuration options ---*/

py/circuitpy_mpconfig.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,7 @@ void supervisor_run_background_tasks_if_tick(void);
594594
#define CIRCUITPY_DIGITALIO_HAVE_INVALID_DRIVE_MODE (0)
595595
#endif
596596

597+
#define FF_FS_MAKE_VOLID (1)
597598

598599
#define MICROPY_PY_OPTIMIZE_PROPERTY_FLASH_SIZE (CIRCUITPY_OPTIMIZE_PROPERTY_FLASH_SIZE)
599600

supervisor/shared/fatfs.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@
2828

2929
#include "py/runtime.h"
3030
#include "lib/oofatfs/ff.h"
31-
#include "shared/timeutils/timeutils.h"
31+
#include "shared-bindings/microcontroller/Processor.h"
32+
#include "shared-bindings/os/__init__.h"
3233
#include "shared-bindings/rtc/RTC.h"
3334
#include "shared-bindings/time/__init__.h"
35+
#include "shared/timeutils/timeutils.h"
3436

3537
DWORD _time_override = 0;
3638
DWORD get_fattime(void) {
@@ -50,3 +52,17 @@ DWORD get_fattime(void) {
5052
void override_fattime(DWORD time) {
5153
_time_override = time;
5254
}
55+
56+
DWORD make_volid(void) {
57+
DWORD result;
58+
if (!common_hal_os_urandom((uint8_t *)&result, sizeof(result))) {
59+
#if CIRCUITPY_MICROCONTROLLER && COMMON_HAL_MCU_PROCESSOR_UID_LENGTH >= 4
60+
uint8_t raw_id[COMMON_HAL_MCU_PROCESSOR_UID_LENGTH];
61+
common_hal_mcu_processor_get_uid(raw_id);
62+
memcpy(&result, raw_id, sizeof(result));
63+
#else
64+
result = (DWORD)common_hal_time_monotonic_ns();
65+
#endif
66+
}
67+
return result;
68+
}

0 commit comments

Comments
 (0)