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.

This commit is contained in:
Scott Shawcroft 2017-07-06 10:45:42 -07:00
parent 32cad8c0f8
commit 0c4b273f24
5 changed files with 17 additions and 7 deletions

View File

@ -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) {

View File

@ -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;

View File

@ -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 */

View File

@ -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 */

View File

@ -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 {