From 0c4b273f24d9d4291770a20d392ba3275e29e518 Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Thu, 6 Jul 2017 10:45:42 -0700 Subject: [PATCH] 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. --- atmel-samd/main.c | 10 +++++++++- extmod/fsusermount.c | 2 +- lib/fatfs/ff.c | 7 ++++--- lib/fatfs/ff.h | 2 +- stmhal/main.c | 3 ++- 5 files changed, 17 insertions(+), 7 deletions(-) diff --git a/atmel-samd/main.c b/atmel-samd/main.c index 563237a3c0..45676a5b44 100644 --- a/atmel-samd/main.c +++ b/atmel-samd/main.c @@ -95,7 +95,15 @@ void init_flash_fs(void) { if (res == FR_NO_FILESYSTEM) { // no filesystem so create a fresh one - res = f_mkfs("/flash", 0, 0); + // XOR the serial number together to create a 32-bit id. + uint32_t volume_id = 0; + uint32_t* addresses[4] = {(uint32_t *) 0x0080A00C, (uint32_t *) 0x0080A040, + (uint32_t *) 0x0080A044, (uint32_t *) 0x0080A048}; + for (int i = 0; i < 4; i++) { + volume_id ^= *(addresses[i]); + } + + res = f_mkfs("/flash", 0, 0, volume_id); // Flush the new file system to make sure its repaired immediately. flash_flush(); if (res != FR_OK) { diff --git a/extmod/fsusermount.c b/extmod/fsusermount.c index 5882aba991..62aac8d05d 100644 --- a/extmod/fsusermount.c +++ b/extmod/fsusermount.c @@ -122,7 +122,7 @@ fs_user_mount_t *fatfs_mount_mkfs(mp_uint_t n_args, const mp_obj_t *pos_args, mp } } else if (res == FR_NO_FILESYSTEM && args[1].u_bool) { mkfs: - res = f_mkfs(vfs->str, 1, 0); + res = f_mkfs(vfs->str, 1, 0, 0x12345678); if (res != FR_OK) { mkfs_error: MP_STATE_PORT(fs_user_mount)[i] = NULL; diff --git a/lib/fatfs/ff.c b/lib/fatfs/ff.c index 68c529a287..f4a7c7d8fe 100644 --- a/lib/fatfs/ff.c +++ b/lib/fatfs/ff.c @@ -4054,7 +4054,8 @@ FRESULT f_forward ( FRESULT f_mkfs ( const TCHAR* path, /* Logical drive number */ BYTE sfd, /* Partitioning rule 0:FDISK, 1:SFD */ - UINT au /* Size of allocation unit in unit of byte or sector */ + UINT au, /* Size of allocation unit in unit of byte or sector */ + DWORD volume_id ) { static const WORD vst[] = { 1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 0}; @@ -4218,7 +4219,7 @@ FRESULT f_mkfs ( ST_DWORD(tbl + BPB_HiddSec, b_vol); /* Hidden sectors */ n = GET_FATTIME(); /* Use current time as VSN */ if (fmt == FS_FAT32) { - ST_DWORD(tbl + BS_VolID32, n); /* VSN */ + ST_DWORD(tbl + BS_VolID32, volume_id); /* VSN */ ST_DWORD(tbl + BPB_FATSz32, n_fat); /* Number of sectors per FAT */ ST_DWORD(tbl + BPB_RootClus, 2); /* Root directory start cluster (2) */ ST_WORD(tbl + BPB_FSInfo, 1); /* FSINFO record offset (VBR + 1) */ @@ -4227,7 +4228,7 @@ FRESULT f_mkfs ( tbl[BS_BootSig32] = 0x29; /* Extended boot signature */ mem_cpy(tbl + BS_VolLab32, "NO NAME " "FAT32 ", 19); /* Volume label, FAT signature */ } else { - ST_DWORD(tbl + BS_VolID, n); /* VSN */ + ST_DWORD(tbl + BS_VolID, volume_id); /* VSN */ ST_WORD(tbl + BPB_FATSz16, n_fat); /* Number of sectors per FAT */ tbl[BS_DrvNum] = 0x80; /* Drive number */ tbl[BS_BootSig] = 0x29; /* Extended boot signature */ diff --git a/lib/fatfs/ff.h b/lib/fatfs/ff.h index 31d58b4427..21f01188ca 100644 --- a/lib/fatfs/ff.h +++ b/lib/fatfs/ff.h @@ -236,7 +236,7 @@ FRESULT f_getfree (const TCHAR* path, DWORD* nclst, FATFS** fatfs); /* Get numbe FRESULT f_getlabel (const TCHAR* path, TCHAR* label, DWORD* vsn); /* Get volume label */ FRESULT f_setlabel (const TCHAR* label); /* Set volume label */ FRESULT f_mount (FATFS* fs, const TCHAR* path, BYTE opt); /* Mount/Unmount a logical drive */ -FRESULT f_mkfs (const TCHAR* path, BYTE sfd, UINT au); /* Create a file system on the volume */ +FRESULT f_mkfs (const TCHAR* path, BYTE sfd, UINT au, DWORD volume_id); /* Create a file system on the volume */ FRESULT f_fdisk (BYTE pdrv, const DWORD szt[], void* work); /* Divide a physical drive into some partitions */ int f_putc (TCHAR c, FIL* fp); /* Put a character to the file */ int f_puts (const TCHAR* str, FIL* cp); /* Put a string to the file */ diff --git a/stmhal/main.c b/stmhal/main.c index 97150ecac9..1615c71fab 100644 --- a/stmhal/main.c +++ b/stmhal/main.c @@ -187,7 +187,8 @@ MP_NOINLINE STATIC void init_flash_fs(uint reset_mode) { led_state(PYB_LED_R2, 1); uint32_t start_tick = HAL_GetTick(); - res = f_mkfs("/flash", 0, 0); + // TODO(tannewt): Provide a volume id thats based on the MCU id. + res = f_mkfs("/flash", 0, 0, 0x12345678); if (res == FR_OK) { // success creating fresh LFS } else {