Look through files without trying too hard

This commit is contained in:
Byron Lathi
2023-12-05 08:08:20 -08:00
parent 2859055f98
commit 946234381d
4 changed files with 32 additions and 8 deletions

View File

@@ -10,11 +10,13 @@ extern uint16_t fat_start_sector;
extern uint32_t data_start_sector;
extern uint32_t fat_size;
extern uint8_t* sd_buf;
extern uint8_t sectors_per_cluster;
struct fat32_directory_entry {
char file_name[8];
char file_ext[3];
uint8_t attr1;
uint8_t attr2;
uint8_t create_time_10ms;
uint16_t create_time;
uint16_t create_date;
@@ -37,7 +39,7 @@ struct lfn_entry {
uint16_t name_2[2];
};
uint8_t fat32_get_cluster_by_name(char* name, struct fat32_directory_entry* dentry);
uint8_t fat32_read_cluster(uint32_t cluster, void* buf);
int8_t fat32_get_cluster_by_name(char* name, struct fat32_directory_entry* dentry);
int8_t fat32_read_cluster(uint32_t cluster, void* buf);
#endif

View File

@@ -9,7 +9,8 @@
.export _fat32_init
.export _root_cluster, _fat_start_sector, _data_start_sector, _fat_size, _sd_buf
.export _root_cluster, _fat_start_sector, _data_start_sector
.export _fat_size, _sd_buf, _sectors_per_cluster
.data
@@ -17,6 +18,7 @@ _root_cluster: .res 4
_fat_start_sector: .res 2
_data_start_sector: .res 4
_fat_size: .res 4
_sectors_per_cluster: .res 1
_sd_buf: .res 512
@@ -47,6 +49,9 @@ root_cluster_offs = _sd_buf + $2C
ldx #>ptr1
jsr _SD_readSingleBlock
lda sectors_per_cluster
sta _sectors_per_cluster
ldx #$00
L1: lda root_cluster_offs,x
sta _root_cluster,x

View File

@@ -1,19 +1,23 @@
#include "fat32.h"
#include <devices/sd_card.h>
#include <conio.h>
#include <stdint.h>
#include <string.h>
uint8_t fat32_read_cluster(uint32_t cluster, void* buf) {
int8_t fat32_read_cluster(uint32_t cluster, void* buf) {
uint8_t error;
uint32_t addr = (cluster - 2) + data_start_sector;
SD_readSingleBlock(addr, buf, &error);
return error;
}
uint8_t fat32_get_cluster_by_name(char* name, struct fat32_directory_entry* dentry) {
int8_t fat32_get_cluster_by_name(char* name, struct fat32_directory_entry* dentry) {
struct fat32_directory_entry* local_entry;
int i = 0;
cprintf("Sectors per cluster: %hhx\n", sectors_per_cluster);
fat32_read_cluster(root_cluster, sd_buf);
for (i = 0; i < 16; i++){
local_entry = sd_buf + i*32;
@@ -21,6 +25,19 @@ uint8_t fat32_get_cluster_by_name(char* name, struct fat32_directory_entry* dent
continue;
}
cprintf("Name: %.11s\n", local_entry->file_name, local_entry->file_ext);
cprintf("attr1: %x\n", local_entry->attr1);
if (!strncmp(local_entry->file_name, name, 11)) {
i = -1;
break;
}
}
if (i != -1) {
cprintf("Failed to find file.\n");
return -1;
}
cprintf("Found file!\n");
cprintf("attr1: %x\n", local_entry->attr1);
cprintf("cluster: %x %x\n", local_entry->cluster_high, local_entry->cluster_low);
cprintf("File Size: %lx\n", local_entry->file_size);
}

View File

@@ -6,6 +6,6 @@ void fat32_init(void);
int main(void) {
struct fat32_directory_entry dentry;
fat32_init();
fat32_get_cluster_by_name("KERNEL O65", &dentry);
fat32_get_cluster_by_name("TEST TXT", &dentry);
return 0;
}