/*-----------------------------------------------------------------------*/1/* Low level disk I/O module skeleton for FatFs */2/* (C) ChaN, 2016 */3/* (C) CTCaer, 2018-2025 */4/*-----------------------------------------------------------------------*/5/* If a working storage control module is available, it should be */6/* attached to the FatFs via a glue function rather than modifying it. */7/* This is an example of glue functions to attach various exsisting */8/* storage control modules to the FatFs module with a defined API. */9/*-----------------------------------------------------------------------*/1011#include <string.h>1213#include <bdk.h>1415#include <libs/fatfs/diskio.h> /* FatFs lower layer API */1617static u32 sd_rsvd_sectors = 0;18static u32 ramdisk_sectors = 0;19static u32 bis_sectors = 0;20static u32 emummc_sectors = 0;2122static bool bis_write_allowed = false;2324/*-----------------------------------------------------------------------*/25/* Get Drive Status */26/*-----------------------------------------------------------------------*/27DSTATUS disk_status (28BYTE pdrv /* Physical drive nmuber to identify the drive */29)30{31return 0;32}3334/*-----------------------------------------------------------------------*/35/* Inidialize a Drive */36/*-----------------------------------------------------------------------*/37DSTATUS disk_initialize (38BYTE pdrv /* Physical drive nmuber to identify the drive */39)40{41return 0;42}4344/*-----------------------------------------------------------------------*/45/* Read Sector(s) */46/*-----------------------------------------------------------------------*/47DRESULT disk_read (48BYTE pdrv, /* Physical drive nmuber to identify the drive */49BYTE *buff, /* Data buffer to store read data */50DWORD sector, /* Start sector in LBA */51UINT count /* Number of sectors to read */52)53{54switch (pdrv)55{56case DRIVE_SD:57return sdmmc_storage_read(&sd_storage, sector, count, (void *)buff) ? RES_OK : RES_ERROR;58case DRIVE_RAM:59return ram_disk_read(sector, count, (void *)buff);60case DRIVE_EMMC:61return sdmmc_storage_read(&emmc_storage, sector, count, (void *)buff) ? RES_OK : RES_ERROR;62case DRIVE_BIS:63case DRIVE_EMU:64return nx_emmc_bis_read(sector, count, (void *)buff) ? RES_OK : RES_ERROR;65}6667return RES_ERROR;68}6970/*-----------------------------------------------------------------------*/71/* Write Sector(s) */72/*-----------------------------------------------------------------------*/73DRESULT disk_write (74BYTE pdrv, /* Physical drive nmuber to identify the drive */75const BYTE *buff, /* Data to be written */76DWORD sector, /* Start sector in LBA */77UINT count /* Number of sectors to write */78)79{80switch (pdrv)81{82case DRIVE_SD:83return sdmmc_storage_write(&sd_storage, sector, count, (void *)buff) ? RES_OK : RES_ERROR;84case DRIVE_RAM:85return ram_disk_write(sector, count, (void *)buff);86case DRIVE_EMMC:87return RES_WRPRT;88case DRIVE_BIS:89case DRIVE_EMU:90if (pdrv == DRIVE_BIS && !bis_write_allowed)91return RES_WRPRT;92return nx_emmc_bis_write(sector, count, (void *)buff) ? RES_OK : RES_ERROR;93}9495return RES_ERROR;96}9798/*-----------------------------------------------------------------------*/99/* Miscellaneous Functions */100/*-----------------------------------------------------------------------*/101DRESULT disk_ioctl (102BYTE pdrv, /* Physical drive nmuber (0..) */103BYTE cmd, /* Control code */104void *buff /* Buffer to send/receive control data */105)106{107DWORD *buf = (DWORD *)buff;108109switch (pdrv)110{111case DRIVE_SD:112switch (cmd)113{114case GET_SECTOR_COUNT:115*buf = sd_storage.sec_cnt - sd_rsvd_sectors;116break;117case GET_BLOCK_SIZE:118*buf = 32768; // Align to 16MB.119break;120}121break;122123case DRIVE_RAM:124switch (cmd)125{126case GET_SECTOR_COUNT:127*buf = ramdisk_sectors;128break;129case GET_BLOCK_SIZE:130*buf = 2048; // Align to 1MB.131break;132}133break;134135case DRIVE_BIS:136switch (cmd)137{138case GET_SECTOR_COUNT:139*buf = bis_sectors;140break;141case GET_BLOCK_SIZE:142*buf = 32768; // Align to 16MB.143break;144}145break;146147case DRIVE_EMU:148switch (cmd)149{150case GET_SECTOR_COUNT:151*buf = emummc_sectors;152break;153case GET_BLOCK_SIZE:154*buf = 16384; // Align to 8MB (With BOOT0/1 data will be at 16MB BU).155break;156}157break;158159default: // Catch all for unknown devices.160switch (cmd)161{162case CTRL_SYNC:163break;164case GET_SECTOR_COUNT:165case GET_BLOCK_SIZE:166*buf = 0; // Zero value to force default or abort.167break;168}169break;170}171172return RES_OK;173}174175DRESULT disk_set_info (176BYTE pdrv, /* Physical drive nmuber (0..) */177BYTE cmd, /* Control code */178void *buff /* Buffer to send/receive control data */179)180{181DWORD *buf = (DWORD *)buff;182183if (cmd == SET_SECTOR_COUNT)184{185switch (pdrv)186{187case DRIVE_SD:188sd_rsvd_sectors = *buf;189break;190191case DRIVE_RAM:192ramdisk_sectors = *buf;193break;194195case DRIVE_BIS:196bis_sectors = *buf;197break;198199case DRIVE_EMU:200emummc_sectors = *buf;201break;202}203}204else if (cmd == SET_WRITE_PROTECT && pdrv == DRIVE_BIS)205bis_write_allowed = *(bool *)buff;206207return RES_OK;208}209210211