66#include " std/string.h"
77#include " std/memory.h"
88#include " math/math.h"
9+ #include " sysregs.h"
910
1011#define kprintfv (fmt, ...) \
1112 ({ \
@@ -114,12 +115,15 @@ void FAT32FS::parse_shortnames(f32file_entry* entry, char* out){
114115 out[j++] = ' \0 ' ;
115116}
116117
118+ // TODO: we can use lamdas here. But it's after midnight so now's not the time. Or switch this to c
117119sizedptr FAT32FS::walk_directory (uint32_t cluster_count, uint32_t root_index, const char *seek, f32_entry_handler handler) {
118120 uint32_t cluster_size = mbs->sectors_per_cluster ;
119121 sizedptr buf_ptr = read_cluster (data_start_sector, cluster_size, cluster_count, root_index);
120122 char *buffer = (char *)buf_ptr.ptr ;
121123 f32file_entry *entry = 0 ;
122124
125+ handler = (f32_entry_handler)PHYS_TO_VIRT_P (handler);
126+
123127 for (uint64_t i = 0 ; i < cluster_count * cluster_size * 512 ;) {
124128 if (buffer[i] == 0 ) return {0 , 0 };
125129 if (buffer[i] == 0xE5 ){
@@ -129,15 +133,15 @@ sizedptr FAT32FS::walk_directory(uint32_t cluster_count, uint32_t root_index, co
129133 bool long_name = buffer[i + 0xB ] == 0xF ;
130134 char filename[256 ];
131135 if (long_name){
132- f32longname *first_longname = (f32longname*)&buffer[i];
136+ f32longname *first_longname = (f32longname*)PHYS_TO_VIRT_P ( &buffer[i]) ;
133137 uint16_t count = 0 ;
134138 do {
135139 i += sizeof (f32longname);
136140 count++;
137141 } while (buffer[i + 0xB ] == 0xF );
138142 parse_longnames (first_longname, count, filename);
139143 }
140- entry = (f32file_entry *)&buffer[i];
144+ entry = (f32file_entry *)PHYS_TO_VIRT_P ( &buffer[i]) ;
141145 if (!long_name)
142146 parse_shortnames (entry, filename);
143147 sizedptr result = handler (this , entry, filename, seek);
@@ -224,6 +228,8 @@ uint32_t FAT32FS::count_FAT(uint32_t first){
224228 return count;
225229}
226230
231+ typedef sizedptr (FAT32FS::*wlkfunc)(uint32_t cluster_count, uint32_t root_index, const char *seek, f32_entry_handler handler);
232+
227233sizedptr FAT32FS::read_entry_handler (FAT32FS *instance, f32file_entry *entry, char *filename, const char *seek) {
228234 if (entry->flags .volume_id ) return {0 ,0 };
229235
@@ -237,9 +243,12 @@ sizedptr FAT32FS::read_entry_handler(FAT32FS *instance, f32file_entry *entry, ch
237243 uint32_t bpc = bps * spc;
238244 uint32_t count = entry->filesize > 0 ? ((entry->filesize + bpc - 1 ) / bpc) : instance->count_FAT (filecluster);
239245
240- return entry->flags .directory
241- ? instance->walk_directory (count, filecluster, seek_to (seek, ' /' ), read_entry_handler)
242- : instance->read_full_file (instance->data_start_sector , instance->mbs ->sectors_per_cluster , count, entry->filesize , filecluster);
246+ if (entry->flags .directory ){
247+ return walk_directory (count, filecluster, seek_to (seek, ' /' ), [](FAT32FS *instance, f32file_entry *entry, char *filename, const char *seek) -> sizedptr {
248+ return instance->read_entry_handler (instance, entry, filename, seek);
249+ });
250+ }
251+ return read_full_file (instance->data_start_sector , instance->mbs ->sectors_per_cluster , count, entry->filesize , filecluster);
243252}
244253
245254FS_RESULT FAT32FS::open_file (const char * path, file* descriptor){
@@ -254,7 +263,9 @@ FS_RESULT FAT32FS::open_file(const char* path, file* descriptor){
254263 }
255264 path = seek_to (path, ' /' );
256265 uint32_t count = count_FAT (mbs->first_cluster_of_root_directory );
257- sizedptr buf_ptr = walk_directory (count, mbs->first_cluster_of_root_directory , path, read_entry_handler);
266+ sizedptr buf_ptr = walk_directory (count, mbs->first_cluster_of_root_directory , path, [](FAT32FS *instance, f32file_entry *entry, char *filename, const char *seek) -> sizedptr {
267+ return instance->read_entry_handler (instance, entry, filename, seek);
268+ });
258269 void *buf = (void *)buf_ptr.ptr ;
259270 if (!buf) return FS_RESULT_NOTFOUND;
260271 descriptor->id = fid;
@@ -300,7 +311,9 @@ sizedptr FAT32FS::list_entries_handler(FAT32FS *instance, f32file_entry *entry,
300311 uint32_t count = instance->count_FAT (filecluster);
301312
302313 if (is_last) return instance->list_directory (count, filecluster);
303- if (entry->flags .directory ) return instance->walk_directory (count, filecluster, seek_to (seek, ' /' ), list_entries_handler);
314+ if (entry->flags .directory ) return instance->walk_directory (count, filecluster, seek_to (seek, ' /' ), [](FAT32FS *instance, f32file_entry *entry, char *filename, const char *seek) -> sizedptr {
315+ return instance->list_entries_handler (instance, entry, filename, seek);
316+ });
304317 return { 0 , 0 };
305318}
306319
@@ -309,7 +322,9 @@ size_t FAT32FS::list_contents(const char *path, void* buf, size_t size, uint64_t
309322 path = seek_to (path, ' /' );
310323
311324 uint32_t count_sectors = count_FAT (mbs->first_cluster_of_root_directory );
312- sizedptr ptr = walk_directory (count_sectors, mbs->first_cluster_of_root_directory , path, list_entries_handler);
325+ sizedptr ptr = walk_directory (count_sectors, mbs->first_cluster_of_root_directory , path, [](FAT32FS *instance, f32file_entry *entry, char *filename, const char *seek) -> sizedptr {
326+ return instance->list_entries_handler (instance, entry, filename, seek);
327+ });
313328
314329 size = min (size, ptr.size );
315330
0 commit comments