Skip to content

Commit 0c4b273

Browse files
committed
atmel-samd: Give the on board flash filesystem a unique volume ID. This is useful for identifying filesystem mounts and matching them up to hardware.
1 parent 32cad8c commit 0c4b273

File tree

5 files changed

+17
-7
lines changed

5 files changed

+17
-7
lines changed

atmel-samd/main.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,15 @@ void init_flash_fs(void) {
9595
if (res == FR_NO_FILESYSTEM) {
9696
// no filesystem so create a fresh one
9797

98-
res = f_mkfs("/flash", 0, 0);
98+
// XOR the serial number together to create a 32-bit id.
99+
uint32_t volume_id = 0;
100+
uint32_t* addresses[4] = {(uint32_t *) 0x0080A00C, (uint32_t *) 0x0080A040,
101+
(uint32_t *) 0x0080A044, (uint32_t *) 0x0080A048};
102+
for (int i = 0; i < 4; i++) {
103+
volume_id ^= *(addresses[i]);
104+
}
105+
106+
res = f_mkfs("/flash", 0, 0, volume_id);
99107
// Flush the new file system to make sure its repaired immediately.
100108
flash_flush();
101109
if (res != FR_OK) {

extmod/fsusermount.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ fs_user_mount_t *fatfs_mount_mkfs(mp_uint_t n_args, const mp_obj_t *pos_args, mp
122122
}
123123
} else if (res == FR_NO_FILESYSTEM && args[1].u_bool) {
124124
mkfs:
125-
res = f_mkfs(vfs->str, 1, 0);
125+
res = f_mkfs(vfs->str, 1, 0, 0x12345678);
126126
if (res != FR_OK) {
127127
mkfs_error:
128128
MP_STATE_PORT(fs_user_mount)[i] = NULL;

lib/fatfs/ff.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4054,7 +4054,8 @@ FRESULT f_forward (
40544054
FRESULT f_mkfs (
40554055
const TCHAR* path, /* Logical drive number */
40564056
BYTE sfd, /* Partitioning rule 0:FDISK, 1:SFD */
4057-
UINT au /* Size of allocation unit in unit of byte or sector */
4057+
UINT au, /* Size of allocation unit in unit of byte or sector */
4058+
DWORD volume_id
40584059
)
40594060
{
40604061
static const WORD vst[] = { 1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 0};
@@ -4218,7 +4219,7 @@ FRESULT f_mkfs (
42184219
ST_DWORD(tbl + BPB_HiddSec, b_vol); /* Hidden sectors */
42194220
n = GET_FATTIME(); /* Use current time as VSN */
42204221
if (fmt == FS_FAT32) {
4221-
ST_DWORD(tbl + BS_VolID32, n); /* VSN */
4222+
ST_DWORD(tbl + BS_VolID32, volume_id); /* VSN */
42224223
ST_DWORD(tbl + BPB_FATSz32, n_fat); /* Number of sectors per FAT */
42234224
ST_DWORD(tbl + BPB_RootClus, 2); /* Root directory start cluster (2) */
42244225
ST_WORD(tbl + BPB_FSInfo, 1); /* FSINFO record offset (VBR + 1) */
@@ -4227,7 +4228,7 @@ FRESULT f_mkfs (
42274228
tbl[BS_BootSig32] = 0x29; /* Extended boot signature */
42284229
mem_cpy(tbl + BS_VolLab32, "NO NAME " "FAT32 ", 19); /* Volume label, FAT signature */
42294230
} else {
4230-
ST_DWORD(tbl + BS_VolID, n); /* VSN */
4231+
ST_DWORD(tbl + BS_VolID, volume_id); /* VSN */
42314232
ST_WORD(tbl + BPB_FATSz16, n_fat); /* Number of sectors per FAT */
42324233
tbl[BS_DrvNum] = 0x80; /* Drive number */
42334234
tbl[BS_BootSig] = 0x29; /* Extended boot signature */

lib/fatfs/ff.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ FRESULT f_getfree (const TCHAR* path, DWORD* nclst, FATFS** fatfs); /* Get numbe
236236
FRESULT f_getlabel (const TCHAR* path, TCHAR* label, DWORD* vsn); /* Get volume label */
237237
FRESULT f_setlabel (const TCHAR* label); /* Set volume label */
238238
FRESULT f_mount (FATFS* fs, const TCHAR* path, BYTE opt); /* Mount/Unmount a logical drive */
239-
FRESULT f_mkfs (const TCHAR* path, BYTE sfd, UINT au); /* Create a file system on the volume */
239+
FRESULT f_mkfs (const TCHAR* path, BYTE sfd, UINT au, DWORD volume_id); /* Create a file system on the volume */
240240
FRESULT f_fdisk (BYTE pdrv, const DWORD szt[], void* work); /* Divide a physical drive into some partitions */
241241
int f_putc (TCHAR c, FIL* fp); /* Put a character to the file */
242242
int f_puts (const TCHAR* str, FIL* cp); /* Put a string to the file */

stmhal/main.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,8 @@ MP_NOINLINE STATIC void init_flash_fs(uint reset_mode) {
187187
led_state(PYB_LED_R2, 1);
188188
uint32_t start_tick = HAL_GetTick();
189189

190-
res = f_mkfs("/flash", 0, 0);
190+
// TODO(tannewt): Provide a volume id thats based on the MCU id.
191+
res = f_mkfs("/flash", 0, 0, 0x12345678);
191192
if (res == FR_OK) {
192193
// success creating fresh LFS
193194
} else {

0 commit comments

Comments
 (0)