storage: Wrap runtime variable sector size with MAX_VARIABLE_LOG_SECTOR

When enabled this allows 512n and 4Kn drives to be used with a single build.
(So far all ATA SSDs use 512 byte logical sectors)

Change-Id: I902d2318ca8abb581699c0bca68d6e3ec227d064
This commit is contained in:
Solomon Peachy 2024-11-07 17:45:24 -05:00
parent bc6c189dcb
commit c61ad40812
9 changed files with 49 additions and 33 deletions

View file

@ -126,7 +126,7 @@ int disk_get_sector_multiplier(IF_MD_NONVOID(int drive))
} }
#endif /* MAX_VIRT_SECTOR_SIZE */ #endif /* MAX_VIRT_SECTOR_SIZE */
#if (CONFIG_STORAGE & STORAGE_ATA) // XXX make this more generic? #ifdef MAX_VARIABLE_LOG_SECTOR
static uint16_t disk_log_sector_size[NUM_DRIVES] = static uint16_t disk_log_sector_size[NUM_DRIVES] =
{ [0 ... NUM_DRIVES-1] = SECTOR_SIZE }; /* Updated from STORAGE_INFO */ { [0 ... NUM_DRIVES-1] = SECTOR_SIZE }; /* Updated from STORAGE_INFO */
int disk_get_log_sector_size(IF_MD_NONVOID(int drive)) int disk_get_log_sector_size(IF_MD_NONVOID(int drive))
@ -140,14 +140,9 @@ int disk_get_log_sector_size(IF_MD_NONVOID(int drive))
return size; return size;
} }
#define LOG_SECTOR_SIZE(__drive) disk_log_sector_size[IF_MD_DRV(__drive)] #define LOG_SECTOR_SIZE(__drive) disk_log_sector_size[IF_MD_DRV(__drive)]
#else /* !STORAGE_ATA */ #else /* !MAX_VARIABLE_LOG_SECTOR */
#define LOG_SECTOR_SIZE(__drive) SECTOR_SIZE #define LOG_SECTOR_SIZE(__drive) SECTOR_SIZE
int disk_get_log_sector_size(IF_MD_NONVOID(int drive)) #endif /* !MAX_VARIABLE_LOG_SECTOR */
{
IF_MD((void)drive);
return SECTOR_SIZE;
}
#endif /* !CONFIG_STORAGE & STORAGE_ATA */
bool disk_init(IF_MD_NONVOID(int drive)) bool disk_init(IF_MD_NONVOID(int drive))
{ {
@ -163,23 +158,23 @@ bool disk_init(IF_MD_NONVOID(int drive))
struct storage_info *info = (struct storage_info*) sector; struct storage_info *info = (struct storage_info*) sector;
storage_get_info(IF_MD_DRV(drive), info); storage_get_info(IF_MD_DRV(drive), info);
disk_writer_lock(); disk_writer_lock();
#ifdef MAX_VIRT_SECTOR_SIZE #ifdef MAX_VARIABLE_LOG_SECTOR
disk_log_sector_size[IF_MD_DRV(drive)] = info->sector_size; disk_log_sector_size[IF_MD_DRV(drive)] = info->sector_size;
#endif #endif
disk_writer_unlock(); disk_writer_unlock();
#ifdef MAX_VIRT_SECTOR_SIZE #ifdef MAX_VARIABLE_LOG_SECTOR
if (info->sector_size > MAX_VIRT_SECTOR_SIZE || info->sector_size > DC_CACHE_BUFSIZE) { if (info->sector_size > MAX_VARIABLE_LOG_SECTOR || info->sector_size > DC_CACHE_BUFSIZE) {
panicf("Unsupported logical sector size: %d", panicf("Unsupported logical sector size: %d",
info->sector_size); info->sector_size);
} }
#else #else /* !MAX_VARIABLE_LOG_SECTOR */
if (info->sector_size != SECTOR_SIZE) { if (info->sector_size != SECTOR_SIZE) {
panicf("Unsupported logical sector size: %d", panicf("Unsupported logical sector size: %d",
info->sector_size); info->sector_size);
} }
#endif #endif /* !MAX_VARIABLE_LOG_SECTOR */
#endif /* CONFIG_STORAGE & STORAGE_ATA */ #endif /* STORAGE_ATA */
memset(sector, 0, DC_CACHE_BUFSIZE); memset(sector, 0, DC_CACHE_BUFSIZE);
storage_read_sectors(IF_MD(drive,) 0, 1, sector); storage_read_sectors(IF_MD(drive,) 0, 1, sector);

View file

@ -22,10 +22,10 @@
#ifdef MAX_PHYS_SECTOR_SIZE #ifdef MAX_PHYS_SECTOR_SIZE
#ifdef MAX_VIRT_SECTOR_SIZE #ifdef MAX_VARIABLE_LOG_SECTOR
#define __MAX_VIRT_SECTOR_SIZE MAX_VIRT_SECTOR_SIZE #define __MAX_VARIABLE_LOG_SECTOR MAX_VARIABLE_LOG_SECTOR
#else #else
#define __MAX_VIRT_SECTOR_SIZE SECTOR_SIZE #define __MAX_VARIABLE_LOG_SECTOR SECTOR_SIZE
#endif #endif
struct sector_cache_entry { struct sector_cache_entry {
@ -226,7 +226,7 @@ static int ata_get_phys_sector_mult(void)
sector 1 then assume the drive supports "512e" and will handle sector 1 then assume the drive supports "512e" and will handle
it better than us, so ignore the large physical sectors. it better than us, so ignore the large physical sectors.
*/ */
char throwaway[__MAX_VIRT_SECTOR_SIZE]; char throwaway[__MAX_VARIABLE_LOG_SECTOR];
rc = ata_transfer_sectors(1, 1, &throwaway, false); rc = ata_transfer_sectors(1, 1, &throwaway, false);
if (rc == 0) if (rc == 0)
phys_sector_mult = 1; phys_sector_mult = 1;

View file

@ -163,7 +163,7 @@ union raw_dirent
#define FAT_NTRES_LC_NAME 0x08 #define FAT_NTRES_LC_NAME 0x08
#define FAT_NTRES_LC_EXT 0x10 #define FAT_NTRES_LC_EXT 0x10
#ifdef MAX_VIRT_SECTOR_SIZE #if defined(MAX_VIRT_SECTOR_SIZE) || defined(MAX_VARIABLE_LOG_SECTOR)
#define LOG_SECTOR_SIZE(bpb) fat_bpb->sector_size #define LOG_SECTOR_SIZE(bpb) fat_bpb->sector_size
#else #else
#define LOG_SECTOR_SIZE(bpb) SECTOR_SIZE #define LOG_SECTOR_SIZE(bpb) SECTOR_SIZE
@ -272,14 +272,14 @@ static struct bpb
int BPB_FN_DECL(update_fat_entry, unsigned long, unsigned long); int BPB_FN_DECL(update_fat_entry, unsigned long, unsigned long);
void BPB_FN_DECL(fat_recalc_free_internal); void BPB_FN_DECL(fat_recalc_free_internal);
#endif /* HAVE_FAT16SUPPORT */ #endif /* HAVE_FAT16SUPPORT */
#ifdef MAX_VIRT_SECTOR_SIZE #if defined(MAX_VIRT_SECTOR_SIZE) || defined(MAX_VARIABLE_LOG_SECTOR)
uint16_t sector_size; uint16_t sector_size;
#endif #endif
} fat_bpbs[NUM_VOLUMES]; /* mounted partition info */ } fat_bpbs[NUM_VOLUMES]; /* mounted partition info */
#ifdef STORAGE_NEEDS_BOUNCE_BUFFER #ifdef STORAGE_NEEDS_BOUNCE_BUFFER
#ifdef MAX_VIRT_SECTOR_SIZE #if defined(MAX_VARIABLE_LOG_SECTOR)
#define BOUNCE_SECTOR_SIZE MAX_VIRT_SECTOR_SIZE #define BOUNCE_SECTOR_SIZE MAX_VARIABLE_LOG_SECTOR
#else #else
#define BOUNCE_SECTOR_SIZE SECTOR_SIZE #define BOUNCE_SECTOR_SIZE SECTOR_SIZE
#endif #endif
@ -1997,14 +1997,14 @@ static int free_cluster_chain(struct bpb *fat_bpb, long startcluster)
/** File entity functions **/ /** File entity functions **/
#if defined(MAX_VARIABLE_LOG_SECTOR)
int fat_file_sector_size(const struct fat_file *file) int fat_file_sector_size(const struct fat_file *file)
{ {
#ifdef MAX_VIRT_SECTOR_SIZE
const struct bpb *fat_bpb = FAT_BPB(file->volume); const struct bpb *fat_bpb = FAT_BPB(file->volume);
#endif
return LOG_SECTOR_SIZE(fat_bpb); return LOG_SECTOR_SIZE(fat_bpb);
} }
#endif
int fat_create_file(struct fat_file *parent, const char *name, int fat_create_file(struct fat_file *parent, const char *name,
uint8_t attr, struct fat_file *file, uint8_t attr, struct fat_file *file,
@ -2769,7 +2769,7 @@ int fat_readdir(struct fat_filestr *dirstr, struct fat_dirscan_info *scan,
scan->entries = 0; scan->entries = 0;
#ifdef MAX_VIRT_SECTOR_SIZE #if defined(MAX_VIRT_SECTOR_SIZE) || defined(MAX_VARIABLE_LOG_SECTOR)
struct fat_file *file = dirstr->fatfilep; struct fat_file *file = dirstr->fatfilep;
const struct bpb *fat_bpb = FAT_BPB(file->volume); const struct bpb *fat_bpb = FAT_BPB(file->volume);
#endif #endif
@ -2916,8 +2916,7 @@ int fat_mount(IF_MV(int volume,) IF_MD(int drive,) unsigned long startsector)
#ifdef HAVE_MULTIDRIVE #ifdef HAVE_MULTIDRIVE
fat_bpb->drive = drive; fat_bpb->drive = drive;
#endif #endif
#if defined(MAX_VIRT_SECTOR_SIZE) || defined(MAX_VARIABLE_LOG_SECTOR)
#ifdef MAX_VIRT_SECTOR_SIZE
fat_bpb->sector_size = disk_get_log_sector_size(IF_MD(drive)); fat_bpb->sector_size = disk_get_log_sector_size(IF_MD(drive));
#endif #endif
@ -2959,7 +2958,7 @@ int fat_unmount(IF_MV_NONVOID(int volume))
/** Debug screen stuff **/ /** Debug screen stuff **/
#ifdef MAX_VIRT_SECTOR_SIZE #if defined(MAX_VIRT_SECTOR_SIZE) || defined(MAX_VARIABLE_LOG_SECTOR)
/* This isn't necessarily the same as storage's logical sector size; /* This isn't necessarily the same as storage's logical sector size;
we can have situations where the filesystem (and partition table) we can have situations where the filesystem (and partition table)
uses a larger "virtual sector" than the underlying storage device */ uses a larger "virtual sector" than the underlying storage device */
@ -2973,7 +2972,7 @@ int fat_get_bytes_per_sector(IF_MV_NONVOID(int volume))
return bytes; return bytes;
} }
#endif /* MAX_VIRT_SECTOR_SIZE */ #endif /* MAX_VIRT_SECTOR_SIZE || MAX_VARIAGLE_LOG_SECTOR */
unsigned int fat_get_cluster_size(IF_MV_NONVOID(int volume)) unsigned int fat_get_cluster_size(IF_MV_NONVOID(int volume))
{ {

View file

@ -924,6 +924,13 @@ Lyre prototype 1 */
#define NUM_VOLUMES (NUM_DRIVES * NUM_VOLUMES_PER_DRIVE) #define NUM_VOLUMES (NUM_DRIVES * NUM_VOLUMES_PER_DRIVE)
/* Sanity check sector size options */
#if defined(MAX_VARIABLE_LOG_SECTOR) && defined(MAX_VIRT_SECTOR_SIZE)
#if (MAX_VIRT_SECTOR_SIZE < MAX_VARIABLE_LOG_SECTOR)
#error "optional MAX_VIRT_SECTOR_SIZE must be at least as large as MAX_VARIABLE_LOG_SECTOR"
#endif
#endif
#if defined(BOOTLOADER) && defined(HAVE_ADJUSTABLE_CPU_FREQ) #if defined(BOOTLOADER) && defined(HAVE_ADJUSTABLE_CPU_FREQ)
/* Bootloaders don't use CPU frequency adjustment */ /* Bootloaders don't use CPU frequency adjustment */
#undef HAVE_ADJUSTABLE_CPU_FREQ #undef HAVE_ADJUSTABLE_CPU_FREQ

View file

@ -197,6 +197,9 @@
/* This is the minimum access size for the device, even if it's larger than the logical sector size */ /* This is the minimum access size for the device, even if it's larger than the logical sector size */
#define MAX_PHYS_SECTOR_SIZE 4096 #define MAX_PHYS_SECTOR_SIZE 4096
/* define this if we want to support 512n and 4Kn drives */
//#define MAX_VARIABLE_LOG_SECTOR 4096
//#define STORAGE_NEEDS_BOUNCE_BUFFER //#define STORAGE_NEEDS_BOUNCE_BUFFER
#define STORAGE_WANTS_ALIGN #define STORAGE_WANTS_ALIGN

View file

@ -221,6 +221,9 @@
/* and doesn't handle them in the drive firmware */ /* and doesn't handle them in the drive firmware */
#define MAX_PHYS_SECTOR_SIZE 1024 #define MAX_PHYS_SECTOR_SIZE 1024
/* define this if we want to support 512n and 4Kn drives */
//#define MAX_VARIABLE_LOG_SECTOR 4096
#define BOOTFILE_EXT "ipod" #define BOOTFILE_EXT "ipod"
#define BOOTFILE "rockbox." BOOTFILE_EXT #define BOOTFILE "rockbox." BOOTFILE_EXT
#define BOOTDIR "/.rockbox" #define BOOTDIR "/.rockbox"

View file

@ -51,8 +51,13 @@ int disk_unmount(int drive);
#ifdef MAX_VIRT_SECTOR_SIZE #ifdef MAX_VIRT_SECTOR_SIZE
int disk_get_sector_multiplier(IF_MD_NONVOID(int drive)); int disk_get_sector_multiplier(IF_MD_NONVOID(int drive));
#endif #endif
#ifdef MAX_VARIABLE_LOG_SECTOR
/* The size of the drive's smallest addressible unit */ /* The size of the drive's smallest addressible unit */
int disk_get_log_sector_size(IF_MD_NONVOID(int drive)); int disk_get_log_sector_size(IF_MD_NONVOID(int drive));
#else
#define disk_get_log_sector_size(...) SECTOR_SIZE
#endif
bool disk_present(IF_MD_NONVOID(int drive)); bool disk_present(IF_MD_NONVOID(int drive));

View file

@ -143,7 +143,11 @@ int fat_rename(struct fat_file *parent, struct fat_file *file,
int fat_modtime(struct fat_file *parent, struct fat_file *file, int fat_modtime(struct fat_file *parent, struct fat_file *file,
time_t modtime); time_t modtime);
#if defined(MAX_VARIABLE_LOG_SECTOR)
int fat_file_sector_size(const struct fat_file *file); int fat_file_sector_size(const struct fat_file *file);
#else
#define fat_file_sector_size(__file) SECTOR_SIZE
#endif
/** File stream functions **/ /** File stream functions **/
int fat_closewrite(struct fat_filestr *filestr, uint32_t size, int fat_closewrite(struct fat_filestr *filestr, uint32_t size,
@ -170,9 +174,9 @@ int fat_mount(IF_MV(int volume,) IF_MD(int drive,) unsigned long startsector);
int fat_unmount(IF_MV_NONVOID(int volume)); int fat_unmount(IF_MV_NONVOID(int volume));
/** Debug screen stuff **/ /** Debug screen stuff **/
#ifdef MAX_VIRT_SECTOR_SIZE #if defined(MAX_VIRT_SECTOR_SIZE) || defined(MAX_VARIABLE_LOG_SECTOR)
int fat_get_bytes_per_sector(IF_MV_NONVOID(int volume)); int fat_get_bytes_per_sector(IF_MV_NONVOID(int volume));
#endif /* MAX_VIRT_SECTOR_SIZE */ #endif /* MAX_VIRT_SECTOR_SIZE || MAX_VARIABLE_LOG_SECTOR */
unsigned int fat_get_cluster_size(IF_MV_NONVOID(int volume)); unsigned int fat_get_cluster_size(IF_MV_NONVOID(int volume));
void fat_recalc_free(IF_MV_NONVOID(int volume)); void fat_recalc_free(IF_MV_NONVOID(int volume));
bool fat_size(IF_MV(int volume,) sector_t *size, sector_t *free); bool fat_size(IF_MV(int volume,) sector_t *size, sector_t *free);

View file

@ -108,8 +108,8 @@
#endif #endif
/* this _could_ be larger than a sector if that would ever be useful */ /* this _could_ be larger than a sector if that would ever be useful */
#ifdef MAX_VIRT_SECTOR_SIZE #ifdef MAX_VARIABLE_LOG_SECTOR
#define DC_CACHE_BUFSIZE MAX_VIRT_SECTOR_SIZE #define DC_CACHE_BUFSIZE MAX_VARIABLE_LOG_SECTOR
#else #else
#define DC_CACHE_BUFSIZE SECTOR_SIZE #define DC_CACHE_BUFSIZE SECTOR_SIZE
#endif #endif