Merge pull request #20 from networkfusion/fatfs

Fatfs integration
This commit is contained in:
Robin Jones 2017-10-27 18:27:04 +01:00 committed by GitHub
commit 036f875a8e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
22 changed files with 24571 additions and 3329 deletions

View File

@ -8,12 +8,13 @@
#ifdef DEBUG
#define TRACEF(disp, text, ...) dbg_printf(disp, text, __VA_ARGS__);
#define TRACE(disp, text) printText(text, 3, -1, disp);
#define TRACE(disp, text) dbg_print(disp, text);
#else
#define TRACEF(disp, text, ...) do { if (0) dbg_printf(disp, text, __VA_ARGS__); } while (0)
#define TRACE(disp, text) do { if (0) printText(text, 3, -1, disp); } while (0)
#define TRACEF(disp, text, ...) do { if (0) dbg_printf(disp, text, __VA_ARGS__); } while (0)
#define TRACE(disp, text) do { if (0) dbg_print(disp, text); } while (0)
#endif
void dbg_printf(display_context_t disp, const char *fmt, ...);
void dbg_print(display_context_t disp, char *fmt);
#endif

80
inc/diskio.h Normal file
View File

@ -0,0 +1,80 @@
/*-----------------------------------------------------------------------/
/ Low level disk interface modlue include file (C)ChaN, 2014 /
/-----------------------------------------------------------------------*/
#ifndef _DISKIO_DEFINED
#define _DISKIO_DEFINED
#ifdef __cplusplus
extern "C" {
#endif
#include "integer.h"
/* Status of Disk Functions */
typedef BYTE DSTATUS;
/* Results of Disk Functions */
typedef enum {
RES_OK = 0, /* 0: Successful */
RES_ERROR, /* 1: R/W Error */
RES_WRPRT, /* 2: Write Protected */
RES_NOTRDY, /* 3: Not Ready */
RES_PARERR /* 4: Invalid Parameter */
} DRESULT;
/*---------------------------------------*/
/* Prototypes for disk control functions */
DSTATUS disk_initialize (BYTE pdrv);
DSTATUS disk_status (BYTE pdrv);
DRESULT disk_read (BYTE pdrv, BYTE* buff, DWORD sector, UINT count);
DRESULT disk_write (BYTE pdrv, const BYTE* buff, DWORD sector, UINT count);
DRESULT disk_ioctl (BYTE pdrv, BYTE cmd, void* buff);
/* Disk Status Bits (DSTATUS) */
#define STA_NOINIT 0x01 /* Drive not initialized */
#define STA_NODISK 0x02 /* No medium in the drive */
#define STA_PROTECT 0x04 /* Write protected */
/* Command code for disk_ioctrl fucntion */
/* Generic command (Used by FatFs) */
#define CTRL_SYNC 0 /* Complete pending write process (needed at _FS_READONLY == 0) */
#define GET_SECTOR_COUNT 1 /* Get media size (needed at _USE_MKFS == 1) */
#define GET_SECTOR_SIZE 2 /* Get sector size (needed at _MAX_SS != _MIN_SS) */
#define GET_BLOCK_SIZE 3 /* Get erase block size (needed at _USE_MKFS == 1) */
#define CTRL_TRIM 4 /* Inform device that the data on the block of sectors is no longer used (needed at _USE_TRIM == 1) */
/* Generic command (Not used by FatFs) */
#define CTRL_POWER 5 /* Get/Set power status */
#define CTRL_LOCK 6 /* Lock/Unlock media removal */
#define CTRL_EJECT 7 /* Eject media */
#define CTRL_FORMAT 8 /* Create physical format on the media */
/* MMC/SDC specific ioctl command */
#define MMC_GET_TYPE 10 /* Get card type */
#define MMC_GET_CSD 11 /* Get CSD */
#define MMC_GET_CID 12 /* Get CID */
#define MMC_GET_OCR 13 /* Get OCR */
#define MMC_GET_SDSTAT 14 /* Get SD status */
#define ISDIO_READ 55 /* Read data form SD iSDIO register */
#define ISDIO_WRITE 56 /* Write data to SD iSDIO register */
#define ISDIO_MRITE 57 /* Masked write data to SD iSDIO register */
/* ATA/CF specific ioctl command */
#define ATA_GET_REV 20 /* Get F/W revision */
#define ATA_GET_MODEL 21 /* Get model name */
#define ATA_GET_SN 22 /* Get serial number */
#ifdef __cplusplus
}
#endif
#endif

122
inc/fat.h
View File

@ -1,122 +0,0 @@
//
// Copyright (c) 2017 The Altra64 project contributors
// Portions (c) 2011 KRIK
// See LICENSE file in the project root for full license information.
//
#ifndef _FAT_H
#define _FAT_H
#include "types.h"
#define FAT_MAX_DIR_SIZE 1024
#define FAT_MAX_NAME_LEN 128
#define FAT_MAX_NAME_LEN_S 256
#define FAT_TYPE_16 1
#define FAT_TYPE_32 2
typedef struct {
u8 name[FAT_MAX_NAME_LEN];
u32 entry_cluster;
u32 size;
u32 hdr_sector;
u16 hdr_idx;
u16 fav_idx;
u8 is_dir;
} FatRecord;
typedef struct {
u8 name[8];
u8 ext[3];
u8 attrib;
u8 x[8];
u16 cluster_hi;
u16 time;
u16 date;
u16 cluster_lo;
u32 size;
} FatRecordHdr;
typedef struct {
FatRecord * rec[FAT_MAX_DIR_SIZE];
FatRecord * s_records[FAT_MAX_DIR_SIZE];
u8 *path;
u16 size;
u32 entry_cluster;
u32 entry_sector;
u32 in_cluster_ptr;
u32 current_cluster;
u8 is_root;
} Dir;
typedef struct {
u32 pbr_entry;
u32 root_entry;
u32 fat_entry;
u32 data_entry;
u8 type;
u8 cluster_size;
u32 loaded_sector;
u32 cluster_byte_size;
u32 sectors_per_fat;
} Fat;
typedef struct {
u8 *table_buff;
u8 *table_sector;
u8 *data_sector;
u32 data_sec_idx;
u32 table_sec_idx;
} FatCache;
typedef struct {
u32 sector;
u32 cluster;
u32 sec_available;
u16 in_cluster_ptr;
FatRecord rec;
u8 mode;
} File;
extern Dir *dir;
//extern Dir dir_o;
extern File file;
extern FatCache *fat_cache;
//extern FatRecord rec_tmp;
#define ED_ROOT "/ED64"
#define ED_SAVE "/ED64/SDSAVE"
#define ED_ROM_CFG "/ED64/SDSAVE/LAST.CRT"
//#define ED_TEST_CFG "/MISC/test.dat"
//#define ED_OPTIONS "/ED64/options.dat"
//#define ED_FAVOR "/ED64/favor.dat"
//#define ED_SAVE_DMP "/EDMD/sav-dmp.bin"
#define FILE_MODE_RD 1
#define FILE_MODE_WR 2
u8 fatInit();
u8 fatLoadDir(u32 cluster);
u8 fatLoadDirByName(u8 *name);
u8 fatWriteFile(void *src, u32 sectors);
u8 fatReadFile(void *dst, u32 sectors);
u8 fatReadPartialFile(void *dst, u32 sectors, int b_offset);
u8 fatCreateRecord(u8 *name, u8 is_dir, u8 check_exist);
u8 fatOpenFileByName(u8 *name, u32 wr_sectors);
u8 fatFindRecord(u8 *name, FatRecord *rec, u8 is_dir);
u8 fatOpenFile(FatRecord *rec, u32 wr_sectors);
u8 fatSkipSectors(u32 sectors);
u8 fatCreateRecIfNotExist(u8 *name, u8 is_dir);
u8 fatGetFullName(u8 *name, u32 dir_entry, u32 rec_entry);
u8 fatOpenDirByName(u8 *name);
void fatInitRam();
void fatSortRecords();
#endif /* _FAT_H */

364
inc/ff.h Normal file
View File

@ -0,0 +1,364 @@
/*----------------------------------------------------------------------------/
/ FatFs - Generic FAT Filesystem module R0.13 /
/-----------------------------------------------------------------------------/
/
/ Copyright (C) 2017, ChaN, all right reserved.
/
/ FatFs module is an open source software. Redistribution and use of FatFs in
/ source and binary forms, with or without modification, are permitted provided
/ that the following condition is met:
/ 1. Redistributions of source code must retain the above copyright notice,
/ this condition and the following disclaimer.
/
/ This software is provided by the copyright holder and contributors "AS IS"
/ and any warranties related to this software are DISCLAIMED.
/ The copyright owner or contributors be NOT LIABLE for any damages caused
/ by use of this software.
/
/----------------------------------------------------------------------------*/
#ifndef FF_DEFINED
#define FF_DEFINED 87030 /* Revision ID */
#ifdef __cplusplus
extern "C" {
#endif
#include "integer.h" /* Basic integer types */
#include "ffconf.h" /* FatFs configuration options */
#if FF_DEFINED != FFCONF_DEF
#error Wrong configuration file (ffconf.h).
#endif
/* Definitions of volume management */
#if FF_MULTI_PARTITION /* Multiple partition configuration */
typedef struct {
BYTE pd; /* Physical drive number */
BYTE pt; /* Partition: 0:Auto detect, 1-4:Forced partition) */
} PARTITION;
extern PARTITION VolToPart[]; /* Volume - Partition resolution table */
#endif
/* Type of path name strings on FatFs API */
#if FF_LFN_UNICODE && FF_USE_LFN /* Unicode (UTF-16) string */
#ifndef _INC_TCHAR
typedef WCHAR TCHAR;
#define _T(x) L ## x
#define _TEXT(x) L ## x
#define _INC_TCHAR
#endif
#else /* ANSI/OEM string */
#ifndef _INC_TCHAR
typedef char TCHAR;
#define _T(x) x
#define _TEXT(x) x
#define _INC_TCHAR
#endif
#endif
/* Type of file size variables */
#if FF_FS_EXFAT
#if !FF_USE_LFN
#error LFN must be enabled when enable exFAT
#endif
typedef QWORD FSIZE_t;
#else
typedef DWORD FSIZE_t;
#endif
/* Filesystem object structure (FATFS) */
typedef struct {
BYTE fs_type; /* Filesystem type (0:N/A) */
BYTE pdrv; /* Physical drive number */
BYTE n_fats; /* Number of FATs (1 or 2) */
BYTE wflag; /* win[] flag (b0:dirty) */
BYTE fsi_flag; /* FSINFO flags (b7:disabled, b0:dirty) */
WORD id; /* Volume mount ID */
WORD n_rootdir; /* Number of root directory entries (FAT12/16) */
WORD csize; /* Cluster size [sectors] */
#if FF_MAX_SS != FF_MIN_SS
WORD ssize; /* Sector size (512, 1024, 2048 or 4096) */
#endif
#if FF_USE_LFN
WCHAR* lfnbuf; /* LFN working buffer */
#endif
#if FF_FS_EXFAT
BYTE* dirbuf; /* Directory entry block scratchpad buffer for exFAT */
#endif
#if FF_FS_REENTRANT
FF_SYNC_t sobj; /* Identifier of sync object */
#endif
#if !FF_FS_READONLY
DWORD last_clst; /* Last allocated cluster */
DWORD free_clst; /* Number of free clusters */
#endif
#if FF_FS_RPATH
DWORD cdir; /* Current directory start cluster (0:root) */
#if FF_FS_EXFAT
DWORD cdc_scl; /* Containing directory start cluster (invalid when cdir is 0) */
DWORD cdc_size; /* b31-b8:Size of containing directory, b7-b0: Chain status */
DWORD cdc_ofs; /* Offset in the containing directory (invalid when cdir is 0) */
#endif
#endif
DWORD n_fatent; /* Number of FAT entries (number of clusters + 2) */
DWORD fsize; /* Size of an FAT [sectors] */
DWORD volbase; /* Volume base sector */
DWORD fatbase; /* FAT base sector */
DWORD dirbase; /* Root directory base sector/cluster */
DWORD database; /* Data base sector */
DWORD winsect; /* Current sector appearing in the win[] */
BYTE win[FF_MAX_SS]; /* Disk access window for Directory, FAT (and file data at tiny cfg) */
} FATFS;
/* Object ID and allocation information (FFOBJID) */
typedef struct {
FATFS* fs; /* Pointer to the hosting volume of this object */
WORD id; /* Hosting volume mount ID */
BYTE attr; /* Object attribute */
BYTE stat; /* Object chain status (b1-0: =0:not contiguous, =2:contiguous, =3:flagmented in this session, b2:sub-directory stretched) */
DWORD sclust; /* Object data start cluster (0:no cluster or root directory) */
FSIZE_t objsize; /* Object size (valid when sclust != 0) */
#if FF_FS_EXFAT
DWORD n_cont; /* Size of first fragment - 1 (valid when stat == 3) */
DWORD n_frag; /* Size of last fragment needs to be written to FAT (valid when not zero) */
DWORD c_scl; /* Containing directory start cluster (valid when sclust != 0) */
DWORD c_size; /* b31-b8:Size of containing directory, b7-b0: Chain status (valid when c_scl != 0) */
DWORD c_ofs; /* Offset in the containing directory (valid when file object and sclust != 0) */
#endif
#if FF_FS_LOCK
UINT lockid; /* File lock ID origin from 1 (index of file semaphore table Files[]) */
#endif
} FFOBJID;
/* File object structure (FIL) */
typedef struct {
FFOBJID obj; /* Object identifier (must be the 1st member to detect invalid object pointer) */
BYTE flag; /* File status flags */
BYTE err; /* Abort flag (error code) */
FSIZE_t fptr; /* File read/write pointer (Zeroed on file open) */
DWORD clust; /* Current cluster of fpter (invalid when fptr is 0) */
DWORD sect; /* Sector number appearing in buf[] (0:invalid) */
#if !FF_FS_READONLY
DWORD dir_sect; /* Sector number containing the directory entry (not used at exFAT) */
BYTE* dir_ptr; /* Pointer to the directory entry in the win[] (not used at exFAT) */
#endif
#if FF_USE_FASTSEEK
DWORD* cltbl; /* Pointer to the cluster link map table (nulled on open, set by application) */
#endif
#if !FF_FS_TINY
BYTE buf[FF_MAX_SS]; /* File private data read/write window */
#endif
} FIL;
/* Directory object structure (DIR) */
typedef struct {
FFOBJID obj; /* Object identifier */
DWORD dptr; /* Current read/write offset */
DWORD clust; /* Current cluster */
DWORD sect; /* Current sector (0:Read operation has terminated) */
BYTE* dir; /* Pointer to the directory item in the win[] */
BYTE fn[12]; /* SFN (in/out) {body[8],ext[3],status[1]} */
#if FF_USE_LFN
DWORD blk_ofs; /* Offset of current entry block being processed (0xFFFFFFFF:Invalid) */
#endif
#if FF_USE_FIND
const TCHAR* pat; /* Pointer to the name matching pattern */
#endif
} DIR;
/* File information structure (FILINFO) */
typedef struct {
FSIZE_t fsize; /* File size */
WORD fdate; /* Modified date */
WORD ftime; /* Modified time */
BYTE fattrib; /* File attribute */
#if FF_USE_LFN
TCHAR altname[13]; /* Altenative file name */
TCHAR fname[FF_MAX_LFN + 1]; /* Primary file name */
#else
TCHAR fname[13]; /* File name */
#endif
} FILINFO;
/* File function return code (FRESULT) */
typedef enum {
FR_OK = 0, /* (0) Succeeded */
FR_DISK_ERR, /* (1) A hard error occurred in the low level disk I/O layer */
FR_INT_ERR, /* (2) Assertion failed */
FR_NOT_READY, /* (3) The physical drive cannot work */
FR_NO_FILE, /* (4) Could not find the file */
FR_NO_PATH, /* (5) Could not find the path */
FR_INVALID_NAME, /* (6) The path name format is invalid */
FR_DENIED, /* (7) Access denied due to prohibited access or directory full */
FR_EXIST, /* (8) Access denied due to prohibited access */
FR_INVALID_OBJECT, /* (9) The file/directory object is invalid */
FR_WRITE_PROTECTED, /* (10) The physical drive is write protected */
FR_INVALID_DRIVE, /* (11) The logical drive number is invalid */
FR_NOT_ENABLED, /* (12) The volume has no work area */
FR_NO_FILESYSTEM, /* (13) There is no valid FAT volume */
FR_MKFS_ABORTED, /* (14) The f_mkfs() aborted due to any problem */
FR_TIMEOUT, /* (15) Could not get a grant to access the volume within defined period */
FR_LOCKED, /* (16) The operation is rejected according to the file sharing policy */
FR_NOT_ENOUGH_CORE, /* (17) LFN working buffer could not be allocated */
FR_TOO_MANY_OPEN_FILES, /* (18) Number of open files > FF_FS_LOCK */
FR_INVALID_PARAMETER /* (19) Given parameter is invalid */
} FRESULT;
/*--------------------------------------------------------------*/
/* FatFs module application interface */
FRESULT f_open (FIL* fp, const TCHAR* path, BYTE mode); /* Open or create a file */
FRESULT f_close (FIL* fp); /* Close an open file object */
FRESULT f_read (FIL* fp, void* buff, UINT btr, UINT* br); /* Read data from the file */
FRESULT f_write (FIL* fp, const void* buff, UINT btw, UINT* bw); /* Write data to the file */
FRESULT f_lseek (FIL* fp, FSIZE_t ofs); /* Move file pointer of the file object */
FRESULT f_truncate (FIL* fp); /* Truncate the file */
FRESULT f_sync (FIL* fp); /* Flush cached data of the writing file */
FRESULT f_opendir (DIR* dp, const TCHAR* path); /* Open a directory */
FRESULT f_closedir (DIR* dp); /* Close an open directory */
FRESULT f_readdir (DIR* dp, FILINFO* fno); /* Read a directory item */
FRESULT f_findfirst (DIR* dp, FILINFO* fno, const TCHAR* path, const TCHAR* pattern); /* Find first file */
FRESULT f_findnext (DIR* dp, FILINFO* fno); /* Find next file */
FRESULT f_mkdir (const TCHAR* path); /* Create a sub directory */
FRESULT f_unlink (const TCHAR* path); /* Delete an existing file or directory */
FRESULT f_rename (const TCHAR* path_old, const TCHAR* path_new); /* Rename/Move a file or directory */
FRESULT f_stat (const TCHAR* path, FILINFO* fno); /* Get file status */
FRESULT f_chmod (const TCHAR* path, BYTE attr, BYTE mask); /* Change attribute of a file/dir */
FRESULT f_utime (const TCHAR* path, const FILINFO* fno); /* Change timestamp of a file/dir */
FRESULT f_chdir (const TCHAR* path); /* Change current directory */
FRESULT f_chdrive (const TCHAR* path); /* Change current drive */
FRESULT f_getcwd (TCHAR* buff, UINT len); /* Get current directory */
FRESULT f_getfree (const TCHAR* path, DWORD* nclst, FATFS** fatfs); /* Get number of free clusters on the drive */
FRESULT f_getlabel (const TCHAR* path, TCHAR* label, DWORD* vsn); /* Get volume label */
FRESULT f_setlabel (const TCHAR* label); /* Set volume label */
FRESULT f_forward (FIL* fp, UINT(*func)(const BYTE*,UINT), UINT btf, UINT* bf); /* Forward data to the stream */
FRESULT f_expand (FIL* fp, FSIZE_t szf, BYTE opt); /* Allocate a contiguous block to the file */
FRESULT f_mount (FATFS* fs, const TCHAR* path, BYTE opt); /* Mount/Unmount a logical drive */
FRESULT f_mkfs (const TCHAR* path, BYTE opt, DWORD au, void* work, UINT len); /* Create a FAT volume */
FRESULT f_fdisk (BYTE pdrv, const DWORD* szt, void* work); /* Divide a physical drive into some partitions */
FRESULT f_setcp (WORD cp); /* Set current code page */
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 */
int f_printf (FIL* fp, const TCHAR* str, ...); /* Put a formatted string to the file */
TCHAR* f_gets (TCHAR* buff, int len, FIL* fp); /* Get a string from the file */
#define f_eof(fp) ((int)((fp)->fptr == (fp)->obj.objsize))
#define f_error(fp) ((fp)->err)
#define f_tell(fp) ((fp)->fptr)
#define f_size(fp) ((fp)->obj.objsize)
#define f_rewind(fp) f_lseek((fp), 0)
#define f_rewinddir(dp) f_readdir((dp), 0)
#define f_rmdir(path) f_unlink(path)
#define f_unmount(path) f_mount(0, path, 0)
#ifndef EOF
#define EOF (-1)
#endif
/*--------------------------------------------------------------*/
/* Additional user defined functions */
/* RTC function */
#if !FF_FS_READONLY && !FF_FS_NORTC
DWORD get_fattime (void);
#endif
/* LFN support functions */
#if FF_USE_LFN /* Code conversion (defined in unicode.c) */
WCHAR ff_oem2uni (WCHAR oem, WORD cp); /* OEM code to Unicode conversion */
WCHAR ff_uni2oem (WCHAR uni, WORD cp); /* Unicode to OEM code conversion */
WCHAR ff_wtoupper (WCHAR uni); /* Unicode upper-case conversion */
#endif
#if FF_USE_LFN == 3 /* Dynamic memory allocation */
void* ff_memalloc (UINT msize); /* Allocate memory block */
void ff_memfree (void* mblock); /* Free memory block */
#endif
/* Sync functions */
#if FF_FS_REENTRANT
int ff_cre_syncobj (BYTE vol, FF_SYNC_t* sobj); /* Create a sync object */
int ff_req_grant (FF_SYNC_t sobj); /* Lock sync object */
void ff_rel_grant (FF_SYNC_t sobj); /* Unlock sync object */
int ff_del_syncobj (FF_SYNC_t sobj); /* Delete a sync object */
#endif
/*--------------------------------------------------------------*/
/* Flags and offset address */
/* File access mode and open method flags (3rd argument of f_open) */
#define FA_READ 0x01
#define FA_WRITE 0x02
#define FA_OPEN_EXISTING 0x00
#define FA_CREATE_NEW 0x04
#define FA_CREATE_ALWAYS 0x08
#define FA_OPEN_ALWAYS 0x10
#define FA_OPEN_APPEND 0x30
/* Fast seek controls (2nd argument of f_lseek) */
#define CREATE_LINKMAP ((FSIZE_t)0 - 1)
/* Format options (2nd argument of f_mkfs) */
#define FM_FAT 0x01
#define FM_FAT32 0x02
#define FM_EXFAT 0x04
#define FM_ANY 0x07
#define FM_SFD 0x08
/* Filesystem type (FATFS.fs_type) */
#define FS_FAT12 1
#define FS_FAT16 2
#define FS_FAT32 3
#define FS_EXFAT 4
/* File attribute bits for directory entry (FILINFO.fattrib) */
#define AM_RDO 0x01 /* Read only */
#define AM_HID 0x02 /* Hidden */
#define AM_SYS 0x04 /* System */
#define AM_DIR 0x10 /* Directory */
#define AM_ARC 0x20 /* Archive */
#ifdef __cplusplus
}
#endif
#endif /* FF_DEFINED */

269
inc/ffconf.h Normal file
View File

@ -0,0 +1,269 @@
/*---------------------------------------------------------------------------/
/ FatFs - Configuration file
/---------------------------------------------------------------------------*/
#define FFCONF_DEF 87030 /* Revision ID */
/*---------------------------------------------------------------------------/
/ Function Configurations
/---------------------------------------------------------------------------*/
#define FF_FS_READONLY 0
/* This option switches read-only configuration. (0:Read/Write or 1:Read-only)
/ Read-only configuration removes writing API functions, f_write(), f_sync(),
/ f_unlink(), f_mkdir(), f_chmod(), f_rename(), f_truncate(), f_getfree()
/ and optional writing functions as well. */
#define FF_FS_MINIMIZE 0
/* This option defines minimization level to remove some basic API functions.
/
/ 0: All basic functions are enabled.
/ 1: f_stat(), f_getfree(), f_unlink(), f_mkdir(), f_truncate() and f_rename()
/ are removed.
/ 2: f_opendir(), f_readdir() and f_closedir() are removed in addition to 1.
/ 3: f_lseek() function is removed in addition to 2. */
#define FF_USE_STRFUNC 1
/* This option switches string functions, f_gets(), f_putc(), f_puts() and f_printf().
/
/ 0: Disable string functions.
/ 1: Enable without LF-CRLF conversion.
/ 2: Enable with LF-CRLF conversion. */
#define FF_USE_FIND 0
/* This option switches filtered directory read functions, f_findfirst() and
/ f_findnext(). (0:Disable, 1:Enable 2:Enable with matching altname[] too) */
#define FF_USE_MKFS 0
/* This option switches f_mkfs() function. (0:Disable or 1:Enable) */
#define FF_USE_FASTSEEK 0
/* This option switches fast seek function. (0:Disable or 1:Enable) */
#define FF_USE_EXPAND 0
/* This option switches f_expand function. (0:Disable or 1:Enable) */
#define FF_USE_CHMOD 0
/* This option switches attribute manipulation functions, f_chmod() and f_utime().
/ (0:Disable or 1:Enable) Also FF_FS_READONLY needs to be 0 to enable this option. */
#define FF_USE_LABEL 0
/* This option switches volume label functions, f_getlabel() and f_setlabel().
/ (0:Disable or 1:Enable) */
#define FF_USE_FORWARD 0
/* This option switches f_forward() function. (0:Disable or 1:Enable) */
/*---------------------------------------------------------------------------/
/ Locale and Namespace Configurations
/---------------------------------------------------------------------------*/
#define FF_CODE_PAGE 437
/* This option specifies the OEM code page to be used on the target system.
/ Incorrect code page setting can cause a file open failure.
/
/ 437 - U.S.
/ 720 - Arabic
/ 737 - Greek
/ 771 - KBL
/ 775 - Baltic
/ 850 - Latin 1
/ 852 - Latin 2
/ 855 - Cyrillic
/ 857 - Turkish
/ 860 - Portuguese
/ 861 - Icelandic
/ 862 - Hebrew
/ 863 - Canadian French
/ 864 - Arabic
/ 865 - Nordic
/ 866 - Russian
/ 869 - Greek 2
/ 932 - Japanese (DBCS)
/ 936 - Simplified Chinese (DBCS)
/ 949 - Korean (DBCS)
/ 950 - Traditional Chinese (DBCS)
/ 0 - Include all code pages above and configured by f_setcp()
*/
#define FF_USE_LFN 1
#define FF_MAX_LFN 255
/* The FF_USE_LFN switches the support for LFN (long file name).
/
/ 0: Disable LFN. FF_MAX_LFN has no effect.
/ 1: Enable LFN with static working buffer on the BSS. Always NOT thread-safe.
/ 2: Enable LFN with dynamic working buffer on the STACK.
/ 3: Enable LFN with dynamic working buffer on the HEAP.
/
/ To enable the LFN, Unicode handling functions (option/unicode.c) must be added
/ to the project. The working buffer occupies (FF_MAX_LFN + 1) * 2 bytes and
/ additional 608 bytes at exFAT enabled. FF_MAX_LFN can be in range from 12 to 255.
/ It should be set 255 to support full featured LFN operations.
/ When use stack for the working buffer, take care on stack overflow. When use heap
/ memory for the working buffer, memory management functions, ff_memalloc() and
/ ff_memfree(), must be added to the project. */
#define FF_LFN_UNICODE 0
/* This option switches character encoding on the API, 0:ANSI/OEM or 1:UTF-16,
/ when LFN is enabled. Also behavior of string I/O functions will be affected by
/ this option. When LFN is not enabled, this option has no effect.
*/
#define FF_STRF_ENCODE 3
/* When FF_LFN_UNICODE = 1 with LFN enabled, string I/O functions, f_gets(),
/ f_putc(), f_puts and f_printf() convert the character encoding in it.
/ This option selects assumption of character encoding ON THE FILE to be
/ read/written via those functions.
/
/ 0: ANSI/OEM
/ 1: UTF-16LE
/ 2: UTF-16BE
/ 3: UTF-8
*/
#define FF_FS_RPATH 0
/* This option configures support for relative path.
/
/ 0: Disable relative path and remove related functions.
/ 1: Enable relative path. f_chdir() and f_chdrive() are available.
/ 2: f_getcwd() function is available in addition to 1.
*/
/*---------------------------------------------------------------------------/
/ Drive/Volume Configurations
/---------------------------------------------------------------------------*/
#define FF_VOLUMES 1
/* Number of volumes (logical drives) to be used. (1-10) */
#define FF_STR_VOLUME_ID 0
#define FF_VOLUME_STRS "RAM","NAND","CF","SD","SD2","USB","USB2","USB3"
/* FF_STR_VOLUME_ID switches string support for volume ID.
/ When FF_STR_VOLUME_ID is set to 1, also pre-defined strings can be used as drive
/ number in the path name. FF_VOLUME_STRS defines the drive ID strings for each
/ logical drives. Number of items must be equal to FF_VOLUMES. Valid characters for
/ the drive ID strings are: A-Z and 0-9. */
#define FF_MULTI_PARTITION 0
/* This option switches support for multiple volumes on the physical drive.
/ By default (0), each logical drive number is bound to the same physical drive
/ number and only an FAT volume found on the physical drive will be mounted.
/ When this function is enabled (1), each logical drive number can be bound to
/ arbitrary physical drive and partition listed in the VolToPart[]. Also f_fdisk()
/ funciton will be available. */
#define FF_MIN_SS 512
#define FF_MAX_SS 512
/* This set of options configures the range of sector size to be supported. (512,
/ 1024, 2048 or 4096) Always set both 512 for most systems, generic memory card and
/ harddisk. But a larger value may be required for on-board flash memory and some
/ type of optical media. When FF_MAX_SS is larger than FF_MIN_SS, FatFs is configured
/ for variable sector size mode and disk_ioctl() function needs to implement
/ GET_SECTOR_SIZE command. */
#define FF_USE_TRIM 0
/* This option switches support for ATA-TRIM. (0:Disable or 1:Enable)
/ To enable Trim function, also CTRL_TRIM command should be implemented to the
/ disk_ioctl() function. */
#define FF_FS_NOFSINFO 0
/* If you need to know correct free space on the FAT32 volume, set bit 0 of this
/ option, and f_getfree() function at first time after volume mount will force
/ a full FAT scan. Bit 1 controls the use of last allocated cluster number.
/
/ bit0=0: Use free cluster count in the FSINFO if available.
/ bit0=1: Do not trust free cluster count in the FSINFO.
/ bit1=0: Use last allocated cluster number in the FSINFO if available.
/ bit1=1: Do not trust last allocated cluster number in the FSINFO.
*/
/*---------------------------------------------------------------------------/
/ System Configurations
/---------------------------------------------------------------------------*/
#define FF_FS_TINY 0
/* This option switches tiny buffer configuration. (0:Normal or 1:Tiny)
/ At the tiny configuration, size of file object (FIL) is shrinked FF_MAX_SS bytes.
/ Instead of private sector buffer eliminated from the file object, common sector
/ buffer in the filesystem object (FATFS) is used for the file data transfer. */
#define FF_FS_EXFAT 1
/* This option switches support for exFAT filesystem. (0:Disable or 1:Enable)
/ When enable exFAT, also LFN needs to be enabled.
/ Note that enabling exFAT discards ANSI C (C89) compatibility. */
#define FF_FS_NORTC 1
#define FF_NORTC_MON 10
#define FF_NORTC_MDAY 1
#define FF_NORTC_YEAR 2017
/* The option FF_FS_NORTC switches timestamp functiton. If the system does not have
/ any RTC function or valid timestamp is not needed, set FF_FS_NORTC = 1 to disable
/ the timestamp function. All objects modified by FatFs will have a fixed timestamp
/ defined by FF_NORTC_MON, FF_NORTC_MDAY and FF_NORTC_YEAR in local time.
/ To enable timestamp function (FF_FS_NORTC = 0), get_fattime() function need to be
/ added to the project to read current time form real-time clock. FF_NORTC_MON,
/ FF_NORTC_MDAY and FF_NORTC_YEAR have no effect.
/ These options have no effect at read-only configuration (FF_FS_READONLY = 1). */
#define FF_FS_LOCK 0
/* The option FF_FS_LOCK switches file lock function to control duplicated file open
/ and illegal operation to open objects. This option must be 0 when FF_FS_READONLY
/ is 1.
/
/ 0: Disable file lock function. To avoid volume corruption, application program
/ should avoid illegal open, remove and rename to the open objects.
/ >0: Enable file lock function. The value defines how many files/sub-directories
/ can be opened simultaneously under file lock control. Note that the file
/ lock control is independent of re-entrancy. */
#define FF_FS_REENTRANT 0
#define FF_FS_TIMEOUT 1000
#define FF_SYNC_t HANDLE
/* The option FF_FS_REENTRANT switches the re-entrancy (thread safe) of the FatFs
/ module itself. Note that regardless of this option, file access to different
/ volume is always re-entrant and volume control functions, f_mount(), f_mkfs()
/ and f_fdisk() function, are always not re-entrant. Only file/directory access
/ to the same volume is under control of this function.
/
/ 0: Disable re-entrancy. FF_FS_TIMEOUT and FF_SYNC_t have no effect.
/ 1: Enable re-entrancy. Also user provided synchronization handlers,
/ ff_req_grant(), ff_rel_grant(), ff_del_syncobj() and ff_cre_syncobj()
/ function, must be added to the project. Samples are available in
/ option/syscall.c.
/
/ The FF_FS_TIMEOUT defines timeout period in unit of time tick.
/ The FF_SYNC_t defines O/S dependent sync object type. e.g. HANDLE, ID, OS_EVENT*,
/ SemaphoreHandle_t and etc. A header file for O/S definitions needs to be
/ included somewhere in the scope of ff.h. */
/* #include <windows.h> // O/S definitions */
/*--- End of configuration options ---*/

38
inc/integer.h Normal file
View File

@ -0,0 +1,38 @@
/*-------------------------------------------*/
/* Integer type definitions for FatFs module */
/*-------------------------------------------*/
#ifndef FF_INTEGER
#define FF_INTEGER
#ifdef _WIN32 /* FatFs development platform */
#include <windows.h>
#include <tchar.h>
typedef unsigned __int64 QWORD;
#else /* Embedded platform */
/* These types MUST be 16-bit or 32-bit */
typedef int INT;
typedef unsigned int UINT;
/* This type MUST be 8-bit */
typedef unsigned char BYTE;
/* These types MUST be 16-bit */
typedef short SHORT;
typedef unsigned short WORD;
typedef unsigned short WCHAR;
/* These types MUST be 32-bit */
typedef long LONG;
typedef unsigned long DWORD;
/* This type MUST be 64-bit (Remove this for ANSI C (C89) compatibility) */
typedef unsigned long long QWORD;
#endif
#endif

View File

@ -26,7 +26,7 @@ void memSpiSetSpeed(u8 speed);
void spiReadBlock(void *dat);
void spiWriteBlock(void *dat);
u8 memSpiRead(void *dst, u16 slen);
u8 memSpiWrite(void *src);
u8 memSpiWrite(const void *src);
//u8 mem_spi(u8 dat);
void memfill(void *dst, u8 val, u16 len);
void memcopy(void *src, void *dst, u16 len);

View File

@ -9,7 +9,7 @@
//TODO: not sure if this is correct!!!
extern char *mempak_path;
void file_to_mpk(display_context_t disp, u8 *filename);
int file_to_mpk(display_context_t disp, u8 *filename);
void mpk_to_file(display_context_t disp, char *mpk_filename, int quick);
void view_mpk_file(display_context_t disp, char *mpk_filename);
void view_mpk(display_context_t disp);

View File

@ -4,16 +4,17 @@
// See LICENSE file in the project root for full license information.
//
#ifndef _DISK_H
#define _DISK_H
#ifndef _SD_H
#define _SD_H
#include "types.h"
u8 diskGetInterface();
u8 diskInit();
u8 diskRead(u32 saddr, void *buff, u16 slen);
u8 diskWrite(u32 saddr, u8 *buff, u16 slen);
void diskSetInterface(u32 interface);
u8 sdGetInterface();
u8 sdInit();
u8 sdRead(u32 sector, u8 *buff, u16 count);
u8 sdWrite(u32 sector, const u8 *buff, u16 count);
void sdSetInterface(u32 interface);

View File

@ -4,10 +4,11 @@
#include "types.h"
#include "debug.h"
#include "menu.h"
#include "sys.h"
void dbg_printf(display_context_t disp, const char *fmt, ...)
{
char buf[32];
char buf[256] = {0};
setbuf(stderr, buf);
va_list args;
@ -15,7 +16,17 @@ void dbg_printf(display_context_t disp, const char *fmt, ...)
vfprintf(stderr, fmt, args);
va_end(args);
u8 tmp[32];
u8 tmp[256] = {0};
sprintf(tmp, "%s", buf);
printText(tmp, 3, -1, disp);
display_show(disp);
sleep(3000);
}
void dbg_print(display_context_t disp, char *fmt)
{
printText(fmt, 3, -1, disp);
display_show(disp);
sleep(3000);
}

300
src/diskio.c Normal file
View File

@ -0,0 +1,300 @@
/*-----------------------------------------------------------------------*/
/* Low level disk I/O module skeleton for FatFs (C)ChaN, 2016 */
/*-----------------------------------------------------------------------*/
/* If a working storage control module is available, it should be */
/* attached to the FatFs via a glue function rather than modifying it. */
/* This is an example of glue functions to attach various exsisting */
/* storage control modules to the FatFs module with a defined API. */
/*-----------------------------------------------------------------------*/
#include "diskio.h" /* FatFs lower layer API */
#include "sd.h"
/* Definitions of physical drive number for each drive */
#define DEV_RAM 0 /* Example: Map Ramdisk to physical drive 0 */
#define DEV_MMC 1 /* Example: Map MMC/SD card to physical drive 1 */
#define DEV_USB 2 /* Example: Map USB MSD to physical drive 2 */
/*-----------------------------------------------------------------------*/
/* Get Drive Status */
/*-----------------------------------------------------------------------*/
DSTATUS disk_status (
BYTE pdrv /* Physical drive nmuber to identify the drive */
)
{
DSTATUS stat;
// int result;
// switch (pdrv) {
// case DEV_RAM :
// result = RAM_disk_status();
// // translate the reslut code here
// return stat;
// case DEV_MMC :
// result = MMC_disk_status();
// // translate the reslut code here
// return stat;
// case DEV_USB :
// result = USB_disk_status();
// // translate the reslut code here
// return stat;
// }
// return STA_NOINIT;
if(pdrv)
{
return STA_NOINIT;
}
return RES_OK;
}
/*-----------------------------------------------------------------------*/
/* Initialize a Drive */
/*-----------------------------------------------------------------------*/
DSTATUS disk_initialize (
BYTE pdrv /* Physical drive nmuber to identify the drive */
)
{
DSTATUS stat;
int result;
// switch (pdrv) {
// case DEV_RAM :
// result = RAM_disk_initialize();
// // translate the reslut code here
// return stat;
// case DEV_MMC :
// result = MMC_disk_initialize();
// // translate the reslut code here
// return stat;
// case DEV_USB :
// result = USB_disk_initialize();
// // translate the reslut code here
// return stat;
// }
stat=sdInit(); //SD card initialization
if(stat == STA_NODISK)
{
return STA_NODISK;
}
else if(stat != 0)
{
return STA_NOINIT;
}
else
{
return 0;
}
return STA_NOINIT;
}
/*-----------------------------------------------------------------------*/
/* Read Sector(s) */
/*-----------------------------------------------------------------------*/
DRESULT disk_read (
BYTE pdrv, /* Physical drive nmuber to identify the drive */
BYTE *buff, /* Data buffer to store read data */
DWORD sector, /* Start sector in LBA */
UINT count /* Number of sectors to read */
)
{
DRESULT res;
// int result;
// switch (pdrv) {
// case DEV_RAM :
// // translate the arguments here
// result = RAM_disk_read(buff, sector, count);
// // translate the reslut code here
// return res;
// case DEV_MMC :
// // translate the arguments here
// result = MMC_disk_read(buff, sector, count);
// // translate the reslut code here
// return res;
// case DEV_USB :
// // translate the arguments here
// result = USB_disk_read(buff, sector, count);
// // translate the reslut code here
// return res;
// }
// return RES_PARERR;
if (pdrv || !count)
{
return RES_PARERR;
}
res = sdRead(sector, buff, count);
if(res == 0x00)
{
return RES_OK;
}
else
{
return RES_ERROR;
}
}
/*-----------------------------------------------------------------------*/
/* Write Sector(s) */
/*-----------------------------------------------------------------------*/
DRESULT disk_write (
BYTE pdrv, /* Physical drive nmuber to identify the drive */
const BYTE *buff, /* Data to be written */
DWORD sector, /* Start sector in LBA */
UINT count /* Number of sectors to write */
)
{
DRESULT res;
// int result;
// switch (pdrv) {
// case DEV_RAM :
// // translate the arguments here
// result = RAM_disk_write(buff, sector, count);
// // translate the reslut code here
// return res;
// case DEV_MMC :
// // translate the arguments here
// result = MMC_disk_write(buff, sector, count);
// // translate the reslut code here
// return res;
// case DEV_USB :
// // translate the arguments here
// result = USB_disk_write(buff, sector, count);
// // translate the reslut code here
// return res;
// }
//return RES_PARERR;
if (pdrv || !count)
{
return RES_PARERR;
}
res = sdWrite(sector, buff, count);
if(res == 0)
{
return RES_OK;
}
else
{
return RES_ERROR;
}
}
/*-----------------------------------------------------------------------*/
/* Miscellaneous Functions */
/*-----------------------------------------------------------------------*/
DRESULT disk_ioctl (
BYTE pdrv, /* Physical drive nmuber (0..) */
BYTE cmd, /* Control code */
void *buff /* Buffer to send/receive control data */
)
{
DRESULT res;
// int result;
// switch (pdrv) {
// case DEV_RAM :
// // Process of the command for the RAM drive
// return res;
// case DEV_MMC :
// // Process of the command for the MMC/SD card
// return res;
// case DEV_USB :
// // Process of the command the USB drive
// return res;
// }
switch (cmd) {
case CTRL_SYNC:
return RES_OK;
case GET_SECTOR_SIZE:
*(WORD*)buff = 512;
return RES_OK;
case GET_SECTOR_COUNT:
//*(DWORD*)buff = sdGetSectors();
return RES_OK;
case GET_BLOCK_SIZE:
//*(DWORD*)buff = sdGetBlockSize();
return RES_OK;
}
return RES_PARERR;
}
DWORD get_fattime (void)
{
//TODO: can we use the V3 RTC?
return 0;
}

1933
src/fat.c

File diff suppressed because it is too large Load Diff

6204
src/ff.c Normal file

File diff suppressed because it is too large Load Diff

171
src/ffsystem.c Normal file
View File

@ -0,0 +1,171 @@
/*------------------------------------------------------------------------*/
/* Sample code of OS dependent controls for FatFs */
/* (C)ChaN, 2017 */
/*------------------------------------------------------------------------*/
#include "ff.h"
#if FF_USE_LFN == 3 /* Dynamic memory allocation */
/*------------------------------------------------------------------------*/
/* Allocate a memory block */
/*------------------------------------------------------------------------*/
void* ff_memalloc ( /* Returns pointer to the allocated memory block (null on not enough core) */
UINT msize /* Number of bytes to allocate */
)
{
return malloc(msize); /* Allocate a new memory block with POSIX API */
}
/*------------------------------------------------------------------------*/
/* Free a memory block */
/*------------------------------------------------------------------------*/
void ff_memfree (
void* mblock /* Pointer to the memory block to free */
)
{
free(mblock); /* Free the memory block with POSIX API */
}
#endif
#if FF_FS_REENTRANT /* Mutal exclusion */
/*------------------------------------------------------------------------*/
/* Create a Synchronization Object
/*------------------------------------------------------------------------*/
/* This function is called in f_mount() function to create a new
/ synchronization object for the volume, such as semaphore and mutex.
/ When a 0 is returned, the f_mount() function fails with FR_INT_ERR.
*/
//const osMutexDef_t Mutex[FF_VOLUMES]; /* CMSIS-RTOS */
int ff_cre_syncobj ( /* 1:Function succeeded, 0:Could not create the sync object */
BYTE vol, /* Corresponding volume (logical drive number) */
FF_SYNC_t *sobj /* Pointer to return the created sync object */
)
{
/* Win32 */
*sobj = CreateMutex(NULL, FALSE, NULL);
return (int)(*sobj != INVALID_HANDLE_VALUE);
/* uITRON */
// T_CSEM csem = {TA_TPRI,1,1};
// *sobj = acre_sem(&csem);
// return (int)(*sobj > 0);
/* uC/OS-II */
// OS_ERR err;
// *sobj = OSMutexCreate(0, &err);
// return (int)(err == OS_NO_ERR);
/* FreeRTOS */
// *sobj = xSemaphoreCreateMutex();
// return (int)(*sobj != NULL);
/* CMSIS-RTOS */
// *sobj = osMutexCreate(Mutex + vol);
// return (int)(*sobj != NULL);
}
/*------------------------------------------------------------------------*/
/* Delete a Synchronization Object */
/*------------------------------------------------------------------------*/
/* This function is called in f_mount() function to delete a synchronization
/ object that created with ff_cre_syncobj() function. When a 0 is returned,
/ the f_mount() function fails with FR_INT_ERR.
*/
int ff_del_syncobj ( /* 1:Function succeeded, 0:Could not delete due to an error */
FF_SYNC_t sobj /* Sync object tied to the logical drive to be deleted */
)
{
/* Win32 */
return (int)CloseHandle(sobj);
/* uITRON */
// return (int)(del_sem(sobj) == E_OK);
/* uC/OS-II */
// OS_ERR err;
// OSMutexDel(sobj, OS_DEL_ALWAYS, &err);
// return (int)(err == OS_NO_ERR);
/* FreeRTOS */
// vSemaphoreDelete(sobj);
// return 1;
/* CMSIS-RTOS */
// return (int)(osMutexDelete(sobj) == osOK);
}
/*------------------------------------------------------------------------*/
/* Request Grant to Access the Volume */
/*------------------------------------------------------------------------*/
/* This function is called on entering file functions to lock the volume.
/ When a 0 is returned, the file function fails with FR_TIMEOUT.
*/
int ff_req_grant ( /* 1:Got a grant to access the volume, 0:Could not get a grant */
FF_SYNC_t sobj /* Sync object to wait */
)
{
/* Win32 */
return (int)(WaitForSingleObject(sobj, FF_FS_TIMEOUT) == WAIT_OBJECT_0);
/* uITRON */
// return (int)(wai_sem(sobj) == E_OK);
/* uC/OS-II */
// OS_ERR err;
// OSMutexPend(sobj, FF_FS_TIMEOUT, &err));
// return (int)(err == OS_NO_ERR);
/* FreeRTOS */
// return (int)(xSemaphoreTake(sobj, FF_FS_TIMEOUT) == pdTRUE);
/* CMSIS-RTOS */
// return (int)(osMutexWait(sobj, FF_FS_TIMEOUT) == osOK);
}
/*------------------------------------------------------------------------*/
/* Release Grant to Access the Volume */
/*------------------------------------------------------------------------*/
/* This function is called on leaving file functions to unlock the volume.
*/
void ff_rel_grant (
FF_SYNC_t sobj /* Sync object to be signaled */
)
{
/* Win32 */
ReleaseMutex(sobj);
/* uITRON */
// sig_sem(sobj);
/* uC/OS-II */
// OSMutexPost(sobj);
/* FreeRTOS */
// xSemaphoreGive(sobj);
/* CMSIS-RTOS */
// osMutexRelease(sobj);
}
#endif

15566
src/ffunicode.c Normal file

File diff suppressed because it is too large Load Diff

2162
src/main.c

File diff suppressed because it is too large Load Diff

View File

@ -110,7 +110,7 @@ u8 memSpiReadDma(void *dst, u16 slen) {
return resp;
}
u8 memSpiWrite(void *src) {
u8 memSpiWrite(const void *src) {
u16 i;

View File

@ -8,13 +8,22 @@
#include <string.h>
#include <stdio.h>
#include "types.h"
#include "mempak.h"
#include "memorypak.h"
#include "fat.h"
#include "ff.h"
#include "menu.h"
#include "mem.h"
#include "debug.h"
#include "strlib.h"
#include "sys.h"
enum MemoryPakFormat
{
None,
DexDrive,
Z64
};
static uint8_t mempak_data[128 * 256];
char *mempak_path;
@ -89,187 +98,214 @@ char ___CountBlocks(char *bMemPakBinary, char *aNoteSizes)
}
//old method to write a file to the mempak at controller 1
void file_to_mpk(display_context_t disp, u8 *filename)
int file_to_mpk(display_context_t disp, u8 *filename)
{
u8 tmp[32];
enum MemoryPakFormat memorypak_format;
u8 buff[64];
u8 ok = 0;
printText(filename, 9, -1, disp);
FatRecord rec_tmpf;
ok = fatFindRecord(filename, &rec_tmpf, 0);
FRESULT result;
FIL file;
UINT bytesread;
result = f_open(&file, filename, FA_READ);
u8 resp = 0;
resp = fatOpenFileByName(filename, 0);
u8 *pch;
pch = strrchr(filename, '.');
sprintf(buff, "%s", (pch + 2));
if (strcmp(buff, "64") == 0)
if (result == FR_OK)
{
printText("Dexdrive format", 9, -1, disp);
printText("skip header", 9, -1, disp);
//int fsize = f_size(&file);
static uint8_t mempak_data_buff[36928];
resp = fatReadFile(&mempak_data_buff, 36928 / 512);
u8 *pch;
pch = strrchr(filename, '.');
sprintf(buff, "%s", (pch + 2));
memcpy(&mempak_data, mempak_data_buff + 4160, 32768);
if (strcmp(buff, "64") == 0)
{
TRACE(disp, "Dexdrive format");
memorypak_format = DexDrive;
//skip header
result = f_lseek (
&file, /* [IN] File object */
4160 /* [IN] File read/write pointer */
);
}
TRACE(disp, "Z64 format");
memorypak_format = Z64;
result =
f_read (
&file, /* [IN] File object */
mempak_data, /* [OUT] Buffer to store read data */
32768, /* [IN] Number of bytes to read */
&bytesread /* [OUT] Number of bytes read */
);
f_close(&file);
int err = 0;
for (int j = 0; j < 128; j++)
{
err |= write_mempak_sector(0, j, &mempak_data[j * MEMPAK_BLOCK_SIZE]);
}
}
else
{
printText("Z64 format", 9, -1, disp);
resp = fatReadFile(&mempak_data, 32768 / 512);
memorypak_format = None;
}
int err = 0;
for (int j = 0; j < 128; j++)
{
err |= write_mempak_sector(0, j, &mempak_data[j * MEMPAK_BLOCK_SIZE]);
}
return (int)memorypak_format; //TODO: should return enum
}
void view_mpk_file(display_context_t disp, char *mpk_filename)
{
u8 tmp[32];
u8 buff[64];
u8 ok = 0;
FatRecord rec_tmpf;
ok = fatFindRecord(mpk_filename, &rec_tmpf, 0);
FRESULT result;
FIL file;
UINT bytesread;
result = f_open(&file, mpk_filename, FA_READ);
u8 resp = 0;
resp = fatOpenFileByName(mpk_filename, 0);
u8 *pch;
pch = strrchr(mpk_filename, '.');
sprintf(buff, "%s", (pch + 2));
if (strcmp(buff, "64") == 0)
if (result == FR_OK)
{
static uint8_t mempak_data_buff[36928];
resp = fatReadFile(&mempak_data_buff, 36928 / 512);
memcpy(&mempak_data, mempak_data_buff + 4160, 32768);
}
else
{
resp = fatReadFile(&mempak_data, 32768 / 512);
}
//int fsize = f_size(&file);
printText("File content:", 11, 5, disp);
printText(" ", 11, -1, disp);
u8 *pch;
pch = strrchr(mpk_filename, '.');
sprintf(buff, "%s", (pch + 2));
int notes_c = 0;
char szBuffer[40],
cAppendix;
int bFirstChar;
int i = 0,
nNotes = 0,
iSum = 0,
iRemainingBlocks;
char aNoteSizes[16];
for (i = 0x10A; i < 0x200; i++)
iSum += mempak_data[i];
if (((iSum % 256) == mempak_data[0x101]))
{
iRemainingBlocks = ___CountBlocks(mempak_data, aNoteSizes);
if (iRemainingBlocks <= 123)
if (strcmp(buff, "64") == 0) //DEXDRIVE format
{
for (notes_c = 0; notes_c < 16; notes_c++)
{
if (mempak_data[0x300 + (notes_c * 32)] ||
mempak_data[0x301 + (notes_c * 32)] ||
mempak_data[0x302 + (notes_c * 32)])
{
cAppendix = ___TranslateNotes(&mempak_data[0x300 + (notes_c * 32)], szBuffer);
if (cAppendix != '\0')
sprintf(szBuffer, "%s. %c", szBuffer, cAppendix);
bFirstChar = 1;
for (i = 0; i < (int)strlen(szBuffer); i++)
{
if (szBuffer[i] == ' ')
bFirstChar = 1;
else
{
if (bFirstChar && (szBuffer[i] >= 'a') && (szBuffer[i] <= 'z'))
{
bFirstChar = 0;
szBuffer[i] -= 0x20;
}
}
}
printText(szBuffer, 11, -1, disp);
switch (mempak_data[0x303 + (notes_c * 32)])
{
case 0x00:
sprintf(szBuffer, "None");
break;
case 0x37:
sprintf(szBuffer, "Beta");
break;
case 0x41:
sprintf(szBuffer, "NTSC");
break;
case 0x44:
sprintf(szBuffer, "Germany");
break;
case 0x45:
sprintf(szBuffer, "USA");
break;
case 0x46:
sprintf(szBuffer, "France");
break;
case 0x49:
sprintf(szBuffer, "Italy");
break;
case 0x4A:
sprintf(szBuffer, "Japan");
break;
case 0x50:
sprintf(szBuffer, "Europe");
break;
case 0x53:
sprintf(szBuffer, "Spain");
break;
case 0x55:
sprintf(szBuffer, "Australia");
break;
case 0x58:
case 0x59:
sprintf(szBuffer, "PAL");
break;
default:
sprintf(szBuffer, "Unknown(%02X)", mempak_data[0x303 + (notes_c * 32)]);
}
sprintf(szBuffer, "%i", aNoteSizes[notes_c]);
nNotes++;
}
}
//skip header
result = f_lseek (
&file, /* [IN] File object */
4160 /* [IN] File read/write pointer */
);
}
int free_c = 0;
for (free_c = nNotes; free_c < 16; free_c++)
printText("[free]", 11, -1, disp);
result =
f_read (
&file, /* [IN] File object */
mempak_data, /* [OUT] Buffer to store read data */
32768, /* [IN] Number of bytes to read */
&bytesread /* [OUT] Number of bytes read */
);
char buff[512];
f_close(&file);
printText("File contents:", 11, 5, disp);
printText(" ", 11, -1, disp);
printText("Free space:", 11, -1, disp);
sprintf(buff, "%i blocks", iRemainingBlocks);
printText(buff, 11, -1, disp);
}
else
{
printText("empty", 11, -1, disp);
int notes_c = 0;
char szBuffer[40],
cAppendix;
int bFirstChar;
int i = 0,
nNotes = 0,
iSum = 0,
iRemainingBlocks;
char aNoteSizes[16];
for (i = 0x10A; i < 0x200; i++)
iSum += mempak_data[i];
if (((iSum % 256) == mempak_data[0x101]))
{
iRemainingBlocks = ___CountBlocks(mempak_data, aNoteSizes);
if (iRemainingBlocks <= 123)
{
for (notes_c = 0; notes_c < 16; notes_c++)
{
if (mempak_data[0x300 + (notes_c * 32)] ||
mempak_data[0x301 + (notes_c * 32)] ||
mempak_data[0x302 + (notes_c * 32)])
{
cAppendix = ___TranslateNotes(&mempak_data[0x300 + (notes_c * 32)], szBuffer);
if (cAppendix != '\0')
sprintf(szBuffer, "%s. %c", szBuffer, cAppendix);
bFirstChar = 1;
for (i = 0; i < (int)strlen(szBuffer); i++)
{
if (szBuffer[i] == ' ')
bFirstChar = 1;
else
{
if (bFirstChar && (szBuffer[i] >= 'a') && (szBuffer[i] <= 'z'))
{
bFirstChar = 0;
szBuffer[i] -= 0x20;
}
}
}
printText(szBuffer, 11, -1, disp);
switch (mempak_data[0x303 + (notes_c * 32)])
{
case 0x00:
sprintf(szBuffer, "None");
break;
case 0x37:
sprintf(szBuffer, "Beta");
break;
case 0x41:
sprintf(szBuffer, "NTSC");
break;
case 0x44:
sprintf(szBuffer, "Germany");
break;
case 0x45:
sprintf(szBuffer, "USA");
break;
case 0x46:
sprintf(szBuffer, "France");
break;
case 0x49:
sprintf(szBuffer, "Italy");
break;
case 0x4A:
sprintf(szBuffer, "Japan");
break;
case 0x50:
sprintf(szBuffer, "Europe");
break;
case 0x53:
sprintf(szBuffer, "Spain");
break;
case 0x55:
sprintf(szBuffer, "Australia");
break;
case 0x58:
case 0x59:
sprintf(szBuffer, "PAL");
break;
default:
sprintf(szBuffer, "Unknown(%02X)", mempak_data[0x303 + (notes_c * 32)]);
}
sprintf(szBuffer, "%i", aNoteSizes[notes_c]);
nNotes++;
}
}
}
int free_c = 0;
for (free_c = nNotes; free_c < 16; free_c++)
printText("[free]", 11, -1, disp);
char buff[512];
printText(" ", 11, -1, disp);
printText("Free space:", 11, -1, disp);
sprintf(buff, "%i blocks", iRemainingBlocks);
printText(buff, 11, -1, disp);
}
else
{
printText("empty", 11, -1, disp);
}
}
}
@ -354,44 +390,57 @@ void mpk_to_file(display_context_t disp, char *mpk_filename, int quick)
else
sprintf(buff, "%s%s.MPK", mempak_path, mpk_filename);
FatRecord rec_tmpf;
FRESULT fr;
FILINFO fno;
if (!fatFindRecord(buff, &rec_tmpf, 0))
{ //filename already exists
fr = f_stat(buff, &fno);
if(fr == FR_OK)
{
printText("File exists", 9, -1, disp);
if (quick)
printText("override", 9, -1, disp);
else
while (ok == 0)
while (fr == FR_OK)
{
sprintf(buff, "%s%s%i.MPK", mempak_path, mpk_filename, v);
ok = fatFindRecord(buff, &rec_tmpf, 0);
if (ok == 0)
fr = f_stat(buff, &fno);
if (fr == FR_OK)
v++;
else
break;
}
}
u8 resp = 0;
resp = fatCreateRecIfNotExist(buff, 0);
resp = fatOpenFileByName(buff, 32768 / 512);
FRESULT result;
FIL file;
result = f_open(&file, buff, FA_WRITE | FA_OPEN_ALWAYS);
controller_init();
int err = 0;
for (int j = 0; j < 128; j++)
if (result == FR_OK)
{
err |= read_mempak_sector(0, j, &mempak_data[j * 256]);
controller_init();
int err = 0;
for (int j = 0; j < 128; j++)
{
err |= read_mempak_sector(0, j, &mempak_data[j * 256]);
}
UINT bw;
result =
f_write (
&file, /* [IN] Pointer to the file object structure */
mempak_data, /* [IN] Pointer to the data to be written */
32768, /* [IN] Number of bytes to write */
&bw /* [OUT] Pointer to the variable to return number of bytes written */
);
f_close(&file);
sprintf(buff, "File: %s%i.MPK", mpk_filename, v);
printText(buff, 9, -1, disp);
printText("backup done...", 9, -1, disp);
}
fatWriteFile(&mempak_data, 32768 / 512);
sleep(500);
sprintf(buff, "File: %s%i.MPK", mpk_filename, v);
printText(buff, 9, -1, disp);
printText("backup done...", 9, -1, disp);
}

View File

@ -29,4 +29,4 @@ void menu_about(display_context_t disp)
printText("ChillyWilly", 9, -1, disp);
printText("ShaunTaylor", 9, -1, disp);
printText("Conle", 9, -1, disp);
}
} //TODO: make scrolling text, should include libraries used.

View File

@ -7,11 +7,11 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <mad.h>
#include "fat.h"
#include "mp3.h"
#include <libdragon.h>
#include <mad.h>
#include "mp3.h"
#include "ff.h"
static struct mad_stream Stream;
static struct mad_header Header;
@ -68,34 +68,41 @@ static int mp3_seek(char* fd, int offset, int whence) {
}
static int mp3_size(char* fd) {
FatRecord rec_tmpf;
u8 resp=0;
resp = fatOpenFileByName(fd, 0); //err if not found ^^
int fsize = file.sec_available*512; //fsize in bytes
mp3File_fsize = fsize;
//todo filesize
FRESULT result;
FILINFO fno;
//TODO: error
result = f_stat (fd, &fno);
mp3File_fsize = fno.fsize;
return mp3File_fsize;
}
static void _f_read(char* fname, unsigned char *readBuffer, int size){
/*
FatRecord rec_tmpf;
u8 resp=0;
resp = fatOpenFileByName(fname, 0); //err if not found ^^
//TODO: function not working... probably worth switching to http://www.underbit.com/products/mad/ anyway...
FRESULT result;
FIL file;
UINT bytesread;
result = f_open(&file, fname, FA_READ);
int fsize = file.sec_available*512; //fsize in bytes
mp3File_fsize = fsize;
if (result == FR_OK)
{
int fsize = f_size(&file);
//injecting in buffer... slow but working :/
if(file.sec_available*512>=size){
resp = fatReadPartialFile(readBuffer, size/512, mp3File_fptr);
//resp = fatReadFile(readBuffer+mp3File_fptr, size/512);//file.sec_available);
mp3File_fptr+=size;
mp3File_fsize = fsize;
f_lseek(&file, mp3File_fptr);
result =
f_read (
&file, /* [IN] File object */
readBuffer, /* [OUT] Buffer to store read data */
size, /* [IN] Number of bytes to read */
&bytesread /* [OUT] Number of bytes read */
);
f_close(&file);
mp3File_fptr+=bytesread;
}
//dma_write_s(buffer, 0xb0000000, fsize);
*/
}
static int mp3_read(char* fd, unsigned char *ptr, int size)

View File

@ -4,34 +4,40 @@
// See LICENSE file in the project root for full license information.
//
#include "disk.h"
#include "types.h"
#include "integer.h"
#include "sd.h"
#include "mem.h"
#include "everdrive.h"
#include "errors.h"
#include "sys.h"
#define CMD0 0x40 // software reset
#define CMD1 0x41 // brings card out of idle state
#define CMD8 0x48 // Reserved
#define CMD12 0x4C // stop transmission on multiple block read
#define CMD17 0x51 // read single block
#define CMD18 0x52 // read multiple block
#define CMD58 0x7A // reads the OCR register
#define CMD55 0x77
#define CMD41 0x69
#define CMD24 0x58 // writes a single block
#define CMD25 0x59 // writes a multi block
#define ACMD41 0x69
#define ACMD6 0x46
#define CMD0 (0x40+0) // GO_IDLE_STATE, Software reset.
#define CMD1 (0x40+1) // SEND_OP_COND, Initiate initialization process.
#define CMD2 (0x40+2) //read cid
#define CMD3 (0x40+3) //read rca
#define CMD6 (0x40+6)
#define CMD7 (0x40+7)
#define CMD8 (0x40+8) // SEND_IF_COND, For only SDC V2. Check voltage range.
#define CMD9 (0x40+9) // SEND_CSD, Read CSD register.
#define CMD10 (0x40+10) // SEND_CID, Read CID register.
#define CMD12 (0x40+12) // STOP_TRANSMISSION, Stop to read data.
#define ACMD13 (0xC0+13) // SD_STATUS (SDC)
#define CMD16 (0x40+16) // SET_BLOCKLEN, Change R/W block size.
#define CMD17 (0x40+17) // READ_SINGLE_BLOCK, Read a block.
#define CMD18 (0x40+18) // READ_MULTIPLE_BLOCK, Read multiple blocks.
#define ACMD23 (0xC0+23) // SET_WR_BLK_ERASE_COUNT (SDC)
#define CMD24 (0x40+24) // WRITE_BLOCK, Write a block.
#define CMD25 (0x40+25) // WRITE_MULTIPLE_BLOCK, Write multiple blocks.
#define CMD41 (0x40+41) // SEND_OP_COND (ACMD)
#define ACMD41 (0xC0+41) // SEND_OP_COND (SDC)
#define CMD55 (0x40+55) // APP_CMD, Leading command of ACMD<n> command.
#define CMD58 (0x40+58) // READ_OCR, Read OCR.
#define SD_V2 2
#define SD_HC 1
#define CMD2 0x42 //read cid
#define CMD3 0x43 //read rca
#define CMD7 0x47
#define CMD9 0x49
#define CMD6 0x46
#define R1 1
#define R2 2
@ -46,19 +52,19 @@ u32 disk_interface;
unsigned int diskCrc7(unsigned char *buff, unsigned int len);
void diskCrc16SD(u8 *data, u16 *crc_out, u16 len);
void diskCrc16SD(const u8 *data, u16 *crc_out, u16 len);
u8 diskGetRespTypeSD(u8 cmd);
u8 diskCmdSD(u8 cmd, u32 arg);
u8 diskInitSD();
u8 diskReadSD(u32 saddr, void *buff, u16 slen);
u8 diskWriteSD(u32 saddr, u8 *buff, u16 slen);
u8 diskReadSD(u32 sector, void *buff, u16 count);
u8 diskWriteSD(u32 sector, const u8 *buff, u16 count);
u8 diskStopRwSD();
u8 diskCmdSPI(u8 cmd, u32 arg);
u8 diskInitSPI();
u8 diskReadSPI(u32 saddr, void *buff, u16 slen);
u8 diskWriteSPI(u32 saddr, u8 *buff, u16 slen);
u8 diskReadSPI(u32 sector, void *buff, u16 count);
u8 diskWriteSPI(u32 sector, const u8 *buff, u16 count);
@ -97,17 +103,17 @@ const u16 sd_crc16_table[] = {
0x6E17, 0x7E36, 0x4E55, 0x5E74, 0x2E93, 0x3EB2, 0x0ED1, 0x1EF0
};
void diskSetInterface(u32 interface) {
void sdSetInterface(u32 interface) {
disk_interface = interface;
}
u8 diskGetInterface() {
u8 sdGetInterface() {
return disk_interface;
}
u8 diskInit() {
u8 sdInit() {
if (disk_interface == DISK_IFACE_SD) {
return diskInitSD();
@ -116,25 +122,25 @@ u8 diskInit() {
}
}
u8 diskRead(u32 saddr, void *buff, u16 slen) {
u8 sdRead(u32 sector, u8 *buff, u16 count) {
if (disk_interface == DISK_IFACE_SD) {
return diskReadSD(saddr, buff, slen);
return diskReadSD(sector, buff, count);
} else {
return diskReadSPI(saddr, buff, slen);
return diskReadSPI(sector, buff, count);
}
}
u8 diskWrite(u32 saddr, u8 *buff, u16 slen) {
u8 sdWrite(u32 sector, const u8 *buff, u16 count) {
if (disk_interface == DISK_IFACE_SD) {
return diskWriteSD(saddr, buff, slen);
return diskWriteSD(sector, buff, count);
} else {
return diskWriteSPI(saddr, buff, slen);
return diskWriteSPI(sector, buff, count);
}
}
void diskCrc16SD(u8 *data, u16 *crc_out, u16 len) {
void diskCrc16SD(const u8 *data, u16 *crc_out, u16 len) {
///u16 len = 512;
u16 i, tmp1, u;
@ -369,20 +375,20 @@ u8 diskInitSD() {
}
u8 diskReadSD(u32 saddr, void *buff, u16 slen) {
u8 diskReadSD(u32 sector, void *buff, u16 count) {
u8 resp;
if (!(card_type & 1))saddr *= 512;
resp = diskCmdSD(CMD18, saddr);
if (!(card_type & 1))sector *= 512;
resp = diskCmdSD(CMD18, sector);
if (resp)return DISK_ERR_RD1;
resp = memSpiRead(buff, slen);
resp = memSpiRead(buff, count);
if (resp)return resp;
//console_printf("drd: %0X\n", saddr);
//console_printf("drd: %0X\n", sector);
resp = diskStopRwSD();
return resp;
@ -409,24 +415,24 @@ u8 diskStopRwSD() {
return 0;
}
u8 diskWriteSD(u32 saddr, u8 *buff, u16 slen) {
u8 diskWriteSD(u32 sector, const u8 *buff, u16 count) {
u8 resp;
u16 crc16[5];
u16 i;
u16 u;
u8 ram_buff[512];
u8 *buff_ptr;
const u8 *buff_ptr;
if (!(card_type & 1))saddr *= 512;
resp = diskCmdSD(CMD25, saddr);
if (!(card_type & 1))sector *= 512;
resp = diskCmdSD(CMD25, sector);
if (resp)return DISK_ERR_WR1;
evd_SDdatWriteMode(0);
while (slen--) {
while (count--) {
if ((u32) buff >= ROM_ADDR && (u32) buff < ROM_END_ADDR) {
dma_read_s(ram_buff, (u32) buff, 512);
@ -626,17 +632,17 @@ u8 diskInitSPI() {
return 0;
}
u8 diskReadSPI(u32 saddr, void *buff, u16 slen) {
u8 diskReadSPI(u32 sector, void *buff, u16 count) {
u8 resp;
if (!(card_type & 1))saddr *= 512;
resp = diskCmdSPI(CMD18, saddr);
if (!(card_type & 1))sector *= 512;
resp = diskCmdSPI(CMD18, sector);
if (resp != 0)return DISK_ERR_RD1;
memSpiSSOn();
resp = memSpiRead(buff, slen);
resp = memSpiRead(buff, count);
memSpiSSOff();
diskCmdSPI(CMD12, 0);
@ -644,18 +650,18 @@ u8 diskReadSPI(u32 saddr, void *buff, u16 slen) {
return resp;
}
u8 diskWriteSPI(u32 saddr, u8 *buff, u16 slen) {
u8 diskWriteSPI(u32 sector, const u8 *buff, u16 count) {
u8 resp;
u16 i;
if (!(card_type & 1))saddr *= 512;
resp = diskCmdSPI(CMD25, saddr);
if (!(card_type & 1))sector *= 512;
resp = diskCmdSPI(CMD25, sector);
if (resp != 0)return DISK_ERR_WR1;
memSpiSSOn();
while (slen--) {
while (count--) {
mem_spi(0xff);
mem_spi(0xff);

View File

@ -242,7 +242,7 @@ int getSRAM( uint8_t *buffer, int size){
PI_Init();
sleep(1000);
sleep(250); //TODO: really... 1 second, changed to 250ms... better check it still works!
while (dma_busy()) ;