From 13a4c120ce2cdeb5aa1c210d9426d31e02898486 Mon Sep 17 00:00:00 2001
From: Damien George <damien.p.george@gmail.com>
Date: Tue, 9 Feb 2016 14:31:26 +0000
Subject: [PATCH] lib/fatfs: Add support for sector sizes larger than 512
 bytes.

If MICROPY_FATFS_MAX_SS is defined to power of 2 value between 1024 and
4096, support for dynamic sector size in FatFs will be enabled.  Note
that FatFs reserves static buffer of MICROPY_FATFS_MAX_SS size for each
filesystem in use, so that value should be set sparingly.

Initial patch provided by @pfalcon.
---
 lib/fatfs/ffconf.h |  4 ++++
 stmhal/diskio.c    | 14 ++++++++++++++
 2 files changed, 18 insertions(+)

diff --git a/lib/fatfs/ffconf.h b/lib/fatfs/ffconf.h
index 27e73e2b3..7bfb03add 100644
--- a/lib/fatfs/ffconf.h
+++ b/lib/fatfs/ffconf.h
@@ -217,7 +217,11 @@
 
 
 #define	_MIN_SS		512
+#ifdef MICROPY_FATFS_MAX_SS
+#define _MAX_SS		(MICROPY_FATFS_MAX_SS)
+#else
 #define	_MAX_SS		512
+#endif
 /* These options configure the range of sector size to be supported. (512, 1024,
 /  2048 or 4096) Always set both 512 for most systems, all type of memory cards and
 /  harddisk. But a larger value may be required for on-board flash memory and some
diff --git a/stmhal/diskio.c b/stmhal/diskio.c
index 14eb9e297..9a4efd1a4 100644
--- a/stmhal/diskio.c
+++ b/stmhal/diskio.c
@@ -44,6 +44,7 @@
 //#define BP_IOCTL_DEINIT         (2) // unused
 #define BP_IOCTL_SYNC           (3)
 #define BP_IOCTL_SEC_COUNT      (4)
+#define BP_IOCTL_SEC_SIZE       (5)
 
 /*-----------------------------------------------------------------------*/
 /* Initialize a Drive                                                    */
@@ -273,6 +274,15 @@ DRESULT disk_ioctl (
                         return RES_OK;
                     }
 
+                    case GET_SECTOR_SIZE: {
+                        vfs->u.ioctl[2] = MP_OBJ_NEW_SMALL_INT(BP_IOCTL_SEC_SIZE);
+                        vfs->u.ioctl[3] = MP_OBJ_NEW_SMALL_INT(0); // unused
+                        mp_obj_t ret = mp_call_method_n_kw(2, 0, vfs->u.ioctl);
+                        *((WORD*)buff) = mp_obj_get_int(ret);
+                        vfs->u.ioctl[3] = MP_OBJ_SENTINEL; // indicate new protocol
+                        return RES_OK;
+                    }
+
                     case GET_BLOCK_SIZE:
                         *((DWORD*)buff) = 1; // erase block size in units of sector size
                         return RES_OK;
@@ -292,6 +302,10 @@ DRESULT disk_ioctl (
                         return RES_OK;
                     }
 
+                    case GET_SECTOR_SIZE:
+                        *((WORD*)buff) = 512; // old protocol had fixed sector size
+                        return RES_OK;
+
                     case GET_BLOCK_SIZE:
                         *((DWORD*)buff) = 1; // erase block size in units of sector size
                         return RES_OK;
-- 
GitLab