📄 fileio.h
字号:
#ifndef FILEIO_DOT_H
#define FILEIO_DOT_H
//#include "generic.h"
#include "system/sdcard/sdcard.h"
//#include "Param.h"
//#include "salloc.h"
#define NOFAT12SUPPORT
#ifdef EXTERNAL_RAM_BUFFER
// generic macros to read data from external ram
#define RAMwrite( a, f, d) ERAMwrite( a + f, d)
#define RAMread( a, f) ERAMread( a + f)
#define RAMreadW( a, f) ERAMreadW( a + f)
#define RAMreadD( a, f) ERAMreadD( a + f)
void ERAMwrite( word add, byte d);
byte ERAMread( word a);
#else
#define RAMwrite( a, f, d) *(a+f) = d
#define RAMread( a, f) *(a+f)
#define RAMreadW( a, f) *(word *)(a+f)
#define RAMreadD( a, f) *(dword *)(a+f)
#endif
#define NULL 0
#define FALSE 0
#define TRUE !FALSE
#define OK 0
#define FAIL -1
// FILEIO ERROR CODES
#define CE_GOOD 0 // Everything is fine
#define CE_ERASE_FAIL 1 // Internal Card erase failed
#define CE_NOT_PRESENT 2 // CARD not present
#define CE_NOT_FORMATTED 3 // The disk is of an unsupported format
#define CE_BAD_PARTITION 4 // The boot record is bad
#define CE_INIT_ERROR 5 // Initialization error has occured
#define CE_NOT_INIT 6 // Card is not yet initialized because of some error
#define CE_BAD_SECTOR_READ 7 // A bad read occured of a sector
#define CE_WRITE_ERROR 8 // Could not write to the sector
#define CE_INVALID_CLUSTER 9 // invalid cluster value > maxcls
#define CE_FILE_NOT_FOUND 10 // Could not find the file on the card
#define CE_DIR_NOT_FOUND 11 // Could not find the directory
#define CE_BAD_FILE 12 // file is corrupted
#define CE_DONE 13 // No more files in this directory
#define CE_FILENAME_2_LONG 14 // The purposed file name is too long to use. Shorten the name and then resend.
#define CE_FILENAME_EXISTS 15 // This filename already exists
#define CE_NO_MORE_TAILS 16 // Long file name could not be created
#define CE_DIR_FULL 17 // all root dir entry are taken
#define CE_DISK_FULL 18 // all clusters in partition are taken
#define CE_DIR_NOT_EMPTY 19 // This directory is not empty yet, remove files b4 deleting
#define CE_NONSUPPORTED_SIZE 20 // The disk is too big to format as FAT16
#define CE_TEMP_MOUNT_FAILED 21 // A temporary mount failed (format)
#define CE_WRITE_PROTECTED 22 // Card is write protected
#define CE_UNLOCK_FAILED 23 // Card unlock failed
#define CE_INVALIDCSUM 24 // Invalid checksum
#define CE_ENVLOADFAIL 25 // The environment failed to load
#define CE_TMPMEMFULL 26 // No more external RAM
#define CE_FILENOTOPENED 27 // File not openned for the write
#define CE_BADCACHEREAD 28 // Bad cache read
#define CE_CARDFAT32 29 // FAT 32 - card not supported
#define CE_IMAGENOTAVAIL 30 // The PC has tried to tell me to use a SQTP file without a part selected
#define CE_READONLY 31 // The File is readonly
#define CE_CARDFAT12 32 // FAT12 during intial testing we are not supporting FAT12
// internal errors
#define CE_FAT_EOF 60 // fat attempt to read beyond EOF
#define CE_EOF 61 // reached the end of file
typedef byte CETYPE;
// directory entry management
#define DIR_ESIZE 32 // size of a directory entry (BYTEs)
#define DIR_NAME 0 // offset of file name
#define DIR_EXT 8 // offset of file extension
#define DIR_ATTRIB 11 // offset of attribute( 00ARSHDV) (BYTE)
#define DIR_TIME 22 // offset of last use time (WORD)
#define DIR_DATE 24 // offset of last use date (WORD)
#define DIR_CLST 26 // offset of first cluster in FAT (WORD)
#define DIR_SIZE 28 // offset of file size (DWORD)
#define DIR_DEL 0xE5 // marker of a deleted entry
#define DIR_EMPTY 0 // marker of last entry in directory
//----------------------------------------------------------------------
// Mount a drive and fill the DISK structure
//
// Master Boot Record offsets
#define FO_MBR 0L // master boot record sector LBA
#define FAT12 1 // internal flags for FAT type 12 and 16
#define FAT16 2
#define FAT32 3
#define FAT_EOF 0xfff // last cluster in a file or last entry in a DIR
#define FILE_NAME_SIZE 11
#define FAT_GOOD_SIGN_0 0x55
#define FAT_GOOD_SIGN_1 0xAA
#define BOOTABLE 0x80
#define NON_BOOTABLE 0x00
typedef struct
{
byte* buffer; // address of sector buffer in ext RAM
dword firsts; // lba of first sector in (first) partition
dword fat; // lba of FAT
dword root; // lba of root directory
dword data; // lba of the data area
word maxroot; // max number of entries in root dir
dword maxcls; // max number of clusters in partition
word fatsize; // number of sectors
byte fatcopy; // number of copies
byte SecPerClus; // number of sectors per cluster
byte type; // type of FAT (FAT12, FAT16...)
byte mount; // flag (TRUE= mounted, FALSE= invalid)
} DISK;
#if defined(MCHP_C18)
typedef unsigned short long SWORD; // 24-bit
#else
typedef short int SWORD;
#endif
// PTE_FAT - Partition Table Entry
typedef struct _PTE_FAT
{
byte PTE_BootDes; // Boot Descriptor, 0x80
SWORD PTE_FrstPartSect; // First Partion Sector
byte PTE_FSDesc; // File System Descriptor
SWORD PTE_LstPartSect; // Last Partion Sector
dword PTE_FrstSect; // First Sector Position
dword PTE_NumSect; // Number of Sectors in partion
}PTE_FAT;
// PT_FAT - Partition Table
typedef struct __PT_FAT
{
byte ConsChkRtn[446];
PTE_FAT Partition0;
PTE_FAT Partition1;
PTE_FAT Partition2;
PTE_FAT Partition3;
byte Signature0; // 0x55
byte Signature1; // 0xAA
}_PT_FAT;
typedef _PT_FAT * PT_FAT;
// BPB FAT12
typedef struct __BPB_FAT12 {
SWORD BS_jmpBoot; // Jump Command
byte BS_OEMName[8]; // OEM name
word BPB_BytsPerSec; // BYTEs per sector
byte BPB_SecPerClus; // sectors per allocation unit
word BPB_RsvdSecCnt; // number of reserved sectors after start
byte BPB_NumFATs; // number of FATs
word BPB_RootEntCnt; // number of root directory entries
word BPB_TotSec16; // total number of sectors
byte BPB_Media; // media descriptor
word BPB_FATSz16; // number of sectors per FAT
word BPB_SecPerTrk; // sectors per track
word BPB_NumHeads; // number of heads
dword BPB_HiddSec; // number of hidden sectors
byte BS_Reserved0; // Nothing
byte BS_DrvNum; // Int 13 drive number
byte BS_Reserved1; // Nothing
byte BS_BootSig; // 0x29
byte BS_VolID[4]; // Volume Id
byte BS_VolLab[11]; // Volume Label
byte BS_FilSysType[8]; // File system type, not used for determination
}_BPB_FAT12;
typedef _BPB_FAT12 * BPB_FAT12;
// BPB FAT16
typedef struct __BPB_FAT16 {
SWORD BS_jmpBoot; // Jump Command
byte BS_OEMName[8]; // OEM name
word BPB_BytsPerSec; // BYTEs per sector
byte BPB_SecPerClus; // sectors per allocation unit
word BPB_RsvdSecCnt; // number of reserved sectors after start
byte BPB_NumFATs; // number of FATs
word BPB_RootEntCnt; // number of root directory entries
word BPB_TotSec16; // total number of sectors
byte BPB_Media; // media descriptor
word BPB_FATSz16; // number of sectors per FAT
word BPB_SecPerTrk; // sectors per track
word BPB_NumHeads; // number of heads
dword BPB_HiddSec; // number of hidden sectors
dword BPB_TotSec32; // New 32bit total sec count
byte BS_DrvNum; // Int 13 drive number
byte BS_Reserved1; // Nothing
byte BS_BootSig; // 0x29
byte BS_VolID[4]; // Volume Id
byte BS_VolLab[11]; // Volume Label
byte BS_FilSysType[8]; // File system type, not used for determination
}_BPB_FAT16;
typedef _BPB_FAT16 * BPB_FAT16;
// BPB FAT32
typedef struct __BPB_FAT32 {
SWORD BPB_jmpBoot; // Jump Command
byte BPB_OEMName[8]; // OEM name
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -