1
0
mirror of https://github.com/mist-devel/mist-firmware.git synced 2026-05-03 14:59:36 +00:00

Add a tester for the FAT driver

This commit is contained in:
Gyorgy Szombathelyi
2021-02-19 18:17:07 +01:00
parent c82743773a
commit a557c97650
4 changed files with 149 additions and 37 deletions

17
Makefile.fattest Normal file
View File

@@ -0,0 +1,17 @@
PRJ = fattest
SRC = fat_test.c fat.c
OBJ = $(SRC:.c=.o)
DEP = $(SRC:.c=.d)
CFLAGS = -Wno-attributes
CPPFLAGS = -DFAT_TEST
# Our target.
all: $(PRJ)
$(PRJ): $(OBJ)
$(CC) -o $@ $(OBJ)
clean:
rm -f $(OBJ) $(PRJ)

34
fat.c
View File

@@ -55,36 +55,36 @@ unsigned short directory_cluster; // first cluster of directory (0 if root
unsigned short entries_per_cluster; // number of directory entries per cluster
// internal global variables
unsigned char fattype; // volume format
unsigned char fat32; // volume format is FAT32
unsigned long boot_sector; // partition boot sector
unsigned long fat_start; // start LBA of first FAT table
unsigned long data_start; // start LBA of data field
unsigned long root_directory_cluster; // root directory cluster (used in FAT32)
unsigned long root_directory_start; // start LBA of directory table
unsigned long root_directory_size; // size of directory region in sectors
unsigned char fattype; // volume format
unsigned char fat32; // volume format is FAT32
uint32_t boot_sector; // partition boot sector
uint32_t fat_start; // start LBA of first FAT table
uint32_t data_start; // start LBA of data field
uint32_t root_directory_cluster; // root directory cluster (used in FAT32)
uint32_t root_directory_start; // start LBA of directory table
uint32_t root_directory_size; // size of directory region in sectors
unsigned char fat_number; // number of FAT tables
unsigned char cluster_size; // size of a cluster in sectors
unsigned long cluster_mask; // binary mask of cluster number
uint32_t cluster_mask; // binary mask of cluster number
unsigned short dir_entries; // number of entry's in directory table
unsigned long fat_size; // size of fat
uint32_t fat_size; // size of fat
unsigned short info_sector; // fat32 info sector
struct PartitionEntry partitions[4]; // lbastart and sectors will be byteswapped as necessary
struct PartitionEntry partitions[4]; // lbastart and sectors will be byteswapped as necessary
int partitioncount;
unsigned char sector_buffer[1024]; // sector buffer - room for two consecutive sectors...
FATBUFFER fat_buffer; // buffer for caching fat entries
unsigned long buffered_fat_index; // index of buffered FAT sector
uint32_t buffered_fat_index; // index of buffered FAT sector
char DirEntryLFN[MAXDIRENTRIES][261];
DIRENTRY DirEntry[MAXDIRENTRIES];
unsigned char sort_table[MAXDIRENTRIES];
unsigned char nDirEntries = 0; // entries in DirEntry table
unsigned char iSelectedEntry = 0; // selected entry index
unsigned long iCurrentDirectory = 0; // cluster number of current directory, 0 for root
unsigned long iPreviousDirectory = 0; // cluster number of previous directory
uint32_t iCurrentDirectory = 0; // cluster number of current directory, 0 for root
uint32_t iPreviousDirectory = 0; // cluster number of previous directory
// temporary storage buffers
char t_DirEntryLFN[MAXDIRENTRIES][261];
@@ -128,7 +128,7 @@ void fat_switch_to_usb(void) {
#pragma section_code_init
// read sector of FAT if not already in the buffer
RAMFUNC static char FatLoad(unsigned long index) {
RAMFUNC static char FatLoad(uint32_t index) {
// load fat sector
if(buffered_fat_index != index) {
if (!lread(fat_start + index, (unsigned char*)&fat_buffer)) {
@@ -141,8 +141,8 @@ RAMFUNC static char FatLoad(unsigned long index) {
return 1;
}
RAMFUNC unsigned long NextCluster(unsigned long cluster) {
unsigned long fat_index = CLUSTER2SECTOR(cluster);
RAMFUNC uint32_t NextCluster(uint32_t cluster) {
uint32_t fat_index = CLUSTER2SECTOR(cluster);
unsigned short buffer_index = CLUSTER2OFFSET(cluster);
if(!FatLoad(fat_index)) return(0);

40
fat.h
View File

@@ -7,8 +7,8 @@
typedef struct
{
unsigned long sector;
unsigned long index;
uint32_t sector;
uint32_t index;
} entryTYPE;
typedef struct
@@ -16,20 +16,20 @@ typedef struct
char name[11]; /* name of file */
unsigned char attributes; /* file attributes */
entryTYPE entry; /* file entry location */
unsigned long sector; /* sector index in file */
unsigned long size; /* file size */
unsigned long cluster; /* current cluster */
unsigned long start_cluster; /* first cluster of file */
uint32_t sector; /* sector index in file */
uint32_t size; /* file size */
uint32_t cluster; /* current cluster */
uint32_t start_cluster; /* first cluster of file */
unsigned char device; /* device index (0=sd, 1=usb) */
long cluster_change; /* counter to keep track of file length changes */
int32_t cluster_change; /* counter to keep track of file length changes */
char long_name[261];
} fileTYPE;
struct PartitionEntry
{
unsigned char geometry[8]; // ignored
unsigned long startlba;
unsigned long sectors;
uint32_t startlba;
uint32_t sectors;
} __attribute__ ((packed));
struct MasterBootRecord
@@ -69,22 +69,22 @@ typedef struct
unsigned short ModifyTime; /* last update time */
unsigned short ModifyDate; /* last update date */
unsigned short StartCluster; /* starting cluster of file */
unsigned long FileSize; /* size of file in bytes */
} DIRENTRY;
uint32_t FileSize; /* size of file in bytes */
} __attribute__ ((packed)) DIRENTRY;
typedef union {
unsigned short fat16[256];
unsigned long fat32[128];
uint32_t fat32[128];
} FATBUFFER;
struct InfoSector {
unsigned long magic; /* Magic for info sector ('RRaA') */
uint32_t magic; /* Magic for info sector ('RRaA') */
unsigned char junk[0x1dc];
unsigned long reserved1; /* Nothing as far as I can tell */
unsigned long signature; /* 0x61417272 ('rrAa') */
unsigned long free_clusters; /* Free cluster count. -1 if unknown */
unsigned long next_cluster; /* Most recently allocated cluster. */
unsigned long reserved2[3];
uint32_t reserved1; /* Nothing as far as I can tell */
uint32_t signature; /* 0x61417272 ('rrAa') */
uint32_t free_clusters; /* Free cluster count. -1 if unknown */
uint32_t next_cluster; /* Most recently allocated cluster. */
uint32_t reserved2[3];
unsigned short reserved3;
unsigned short boot_sign;
} __attribute__ ((packed));
@@ -96,7 +96,7 @@ struct InfoSector {
// BEWARE, this buffer is also used and thus trashed by all other functions
extern unsigned char sector_buffer[1024]; // sector buffer - room for 2 sectors, to ease reading data not sector-aligned...
extern unsigned char cluster_size;
extern unsigned long cluster_mask;
extern uint32_t cluster_mask;
extern unsigned char fat32;
// constants
@@ -125,7 +125,7 @@ extern unsigned char fat32;
// functions
unsigned char FindDrive(void);
unsigned long GetFATLink(unsigned long cluster);
uint32_t NextCluster(uint32_t cluster);
unsigned char FileNextSector(fileTYPE *file) RAMFUNC;
unsigned char FileNextSectorExpand(fileTYPE *file);
unsigned char FileOpen(fileTYPE *file, const char *name);

95
fat_test.c Normal file
View File

@@ -0,0 +1,95 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include "fat.h"
#define FAT_IMG "/dev/sdd1"
//#define FAT_IMG "test.img"
extern DIRENTRY DirEntry[MAXDIRENTRIES];
extern unsigned char sort_table[MAXDIRENTRIES];
extern unsigned char nDirEntries;
extern unsigned char iSelectedEntry;
extern unsigned long iCurrentDirectory;
extern char DirEntryLFN[MAXDIRENTRIES][261];
FILE * fp;
void iprintf(const char *format, ...) {
va_list arg;
va_start(arg, format);
vprintf(format, arg);
va_end(arg);
}
unsigned char MMC_CheckCard() {
return 1;
}
unsigned char MMC_Read(unsigned long lba, unsigned char *pReadBuffer) {
// printf("MMC_Read lba: %d\n", lba);
fseek(fp, lba << 9, SEEK_SET);
fread(pReadBuffer, 512, 1, fp);
#if 0
for (int i = 0; i < 512; i++) {
if (i%16 == 0) printf("\n");
printf("%02x ", pReadBuffer[i]);
}
printf("\n");
#endif
}
unsigned char MMC_Write(unsigned long lba, unsigned char *pWriteBuffer) {
}
unsigned char MMC_ReadMultiple(unsigned long lba, unsigned char *pReadBuffer, unsigned long nBlockCount) {
fseek(fp, lba << 9, SEEK_SET);
fread(pReadBuffer, 512, nBlockCount, fp);
}
void ErrorMessage(const char *message, unsigned char code) {
printf(message);
}
void BootPrint(const char *message) {
printf(message);
}
int main () {
unsigned char i;
unsigned char k;
char *lfn;
int page = 0;
fp = fopen(FAT_IMG, "r+");
if (!fp) {
perror(0);
return(-1);
}
FindDrive();
ChangeDirectory(DIRECTORY_ROOT);
printf("sizeof(DirEntry) = %d %d\n", sizeof(DIRENTRY), sizeof(unsigned long));
ScanDirectory(SCAN_INIT, "RBF", SCAN_DIR | SCAN_LFN);
printf("nDirEntries = %d\n", nDirEntries);
while (1) {
for (i = 0; i < nDirEntries; i++)
{
k = sort_table[i];
lfn = DirEntryLFN[k];
if (lfn) printf("%c %s\n",i == iSelectedEntry ? '*' : ' ', lfn);
else printf("%c %s %d %d\n", i == iSelectedEntry ? '*' : ' ', DirEntry[k].Name, DirEntry[k].StartCluster, DirEntry[k].FileSize);
}
if (page == 2) break;
if (nDirEntries == 8) {
printf("Next Page\n");
ScanDirectory(SCAN_NEXT_PAGE, "RBF", SCAN_DIR | SCAN_LFN);
page++;
} else {
break;
}
}
fclose(fp);
return(0);
}