BRFS.C 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  1. // Test implementation of Bart's RAM File System (BRFS)
  2. /*
  3. General Idea:
  4. - HDD for the average home user was <100MB until after 1990
  5. - SPI NOR Flash provides around the same amount of storage, with very little wear
  6. - Very fast reads in QSPI mode
  7. - However, writes are extremely slow and need to be performed in pages of 256 bytes (64 words)
  8. - FPGC has 64MiB RAM, which is a lot even for 32 bit addressable words
  9. - 32MiB is already more than enough for the FPGC in its current form
  10. - Use the other 32MiB as a fully in RAM filesystem
  11. - That initializes from SPI flash and writes back to Flash at a chosen time
  12. */
  13. /*
  14. Implementation Idea:
  15. - Use superblock for info/addresses, no hard-coded sizes!
  16. - Allows for different storage media, easier testing on tiny size, and more future proof
  17. */
  18. /*
  19. Implementation Details:
  20. --------------------------------------------------
  21. | superblock | FAT | Data+Dir blocks (same size) |
  22. --------------------------------------------------
  23. 16 word superblock:
  24. - (1) total blocks
  25. - (1) bytes per block
  26. - (10) label [1 char per word]
  27. - (1) brfs version
  28. - (3) reserved
  29. 8 word Dir entries:
  30. - (4) filename.ext [4 chars per word -> 16 chars total]
  31. - (1) modify date [to be implemented when RTC]
  32. - (1) flags [max 32 flags, from right to left: directory, hidden]
  33. - (1) 1st FAT idx
  34. - (1) file size [in words, not bytes]
  35. */
  36. /*
  37. Implementation Notes:
  38. - Current directory is managed by the application/OS, not the FS. Directory (or file) can be checked to exist using stat()
  39. - Updating a file: might be better to delete and create a new one, but this is better be done by the application instead of the FS
  40. - Write should start writing at the cursor and jump to the next block (also create in FAT) if the end is reached
  41. - No delete/backspace, only delete entire file or overwrite data
  42. */
  43. /*
  44. Required operations:
  45. - [x] Format
  46. - [x] Create directory
  47. - [x] Create file
  48. - [x] Open file (not a dir!) (allow multiple files open at once)
  49. - [x] Close file
  50. - [] Stat (returns dir entry)
  51. - [/] Set cursor
  52. - [x] Get cursor
  53. - [] Read file
  54. - [] Write file
  55. - [] Delete entire file (deleting part of file is not a thing)
  56. - [x] List directory
  57. */
  58. #define word char
  59. #include "LIB/MATH.C"
  60. #include "LIB/SYS.C"
  61. #include "LIB/STDLIB.C"
  62. #define BRFS_RAM_STORAGE_ADDR 0x600000
  63. #define MAX_PATH_LENGTH 127
  64. #define MAX_OPEN_FILES 4 // Can be set higher, but 4 is good for testing
  65. // Length of structs, should not be changed
  66. #define SUPERBLOCK_SIZE 16
  67. #define DIR_ENTRY_SIZE 8
  68. // 16 words long
  69. struct brfs_superblock
  70. {
  71. word total_blocks;
  72. word bytes_per_block;
  73. word label[10]; // 1 char per word
  74. word brfs_version;
  75. word reserved[3];
  76. };
  77. // 8 words long
  78. struct brfs_dir_entry
  79. {
  80. word filename[4]; // 4 chars per word
  81. word modify_date; // TBD when RTC added to FPGC
  82. word flags; // 32 flags, from right to left: directory, hidden
  83. word fat_idx; // idx of first FAT block
  84. word filesize; // file size in words, not bytes
  85. };
  86. word *brfs_ram_storage = (word*) BRFS_RAM_STORAGE_ADDR; // RAM storage of file system
  87. // Variables for open files
  88. word brfs_cursors[MAX_OPEN_FILES]; // Cursor position offset from start of file
  89. word brfs_file_pointers[MAX_OPEN_FILES]; // FAT idx of open file
  90. struct brfs_dir_entry* brfs_dir_entry_pointers[MAX_OPEN_FILES]; // Pointer to dir entry of open file
  91. /**
  92. * Create a hexdump like dump of a section of memory
  93. * addr: address of the section
  94. * len: length of the section in words
  95. * linesize: number of words per line to print
  96. */
  97. void brfs_dump_section(word* addr, word len, word linesize)
  98. {
  99. char buf[16];
  100. word i;
  101. for (i = 0; i < len; i++)
  102. {
  103. itoah(addr[i], buf);
  104. if (strlen(buf+2) == 1)
  105. uprintc('0');
  106. uprint(buf+2);
  107. uprintc(' ');
  108. // newline every linesize words
  109. // also print last linesize words as chars if alphanum
  110. if (i != 0 && MATH_modU(i+1, linesize) == 0)
  111. {
  112. uprint(" ");
  113. word j;
  114. for (j = i - (linesize-1); j < i+1; j++)
  115. {
  116. if (isalnum(addr[j]) || addr[j] == ' ')
  117. uprintc(addr[j]);
  118. else
  119. uprintc('.');
  120. }
  121. uprintc('\n');
  122. }
  123. }
  124. }
  125. /**
  126. * Create a raw filesystem dump over UART
  127. * fatsize: size of the FAT table in words
  128. * datasize: size of the data section in words
  129. */
  130. void brfs_dump(word fatsize, word datasize)
  131. {
  132. // Superblock dump
  133. uprintln("Superblock:");
  134. brfs_dump_section(brfs_ram_storage, SUPERBLOCK_SIZE, 16);
  135. // FAT dump
  136. uprintln("\nFAT:");
  137. brfs_dump_section(brfs_ram_storage+SUPERBLOCK_SIZE, fatsize, 8);
  138. // Datablock dump
  139. uprintln("\nData:");
  140. brfs_dump_section(brfs_ram_storage+SUPERBLOCK_SIZE+fatsize, datasize, 32);
  141. uprintln("\nFile pointers and cursors:");
  142. word i;
  143. for (i = 0; i < MAX_OPEN_FILES; i++)
  144. {
  145. uprint("File pointer ");
  146. uprintDec(i);
  147. uprint(": ");
  148. uprintDec(brfs_file_pointers[i]);
  149. uprint(" Cursor: ");
  150. uprintDec(brfs_cursors[i]);
  151. uprint(" Size: ");
  152. uprintDec(brfs_dir_entry_pointers[i] ? brfs_dir_entry_pointers[i]->filesize : 0);
  153. uprintc('\n');
  154. }
  155. }
  156. /**
  157. * Return the FAT index of a directory, or -1 if not found
  158. * dir_path: full path of the directory
  159. */
  160. word brfs_get_fat_idx_of_dir(char* dir_path)
  161. {
  162. // Check length of path
  163. if (strlen(dir_path) > MAX_PATH_LENGTH)
  164. {
  165. uprintln("Path too long!");
  166. return -1;
  167. }
  168. // Start with root directory
  169. word current_dir_fat_idx = 0;
  170. // Check if root directory is requested
  171. if (strcmp(dir_path, "/") == 1)
  172. {
  173. return current_dir_fat_idx;
  174. }
  175. // Copy dir_path, size + 1 for null terminator
  176. // Since strtok modifies the string
  177. char dir_path_copy[MAX_PATH_LENGTH+1];
  178. strcpy(dir_path_copy, dir_path);
  179. struct brfs_superblock* superblock = (struct brfs_superblock*) brfs_ram_storage;
  180. word* brfs_data_block_addr = brfs_ram_storage + SUPERBLOCK_SIZE + superblock->total_blocks;
  181. word dir_entries_max = superblock->bytes_per_block / sizeof(struct brfs_dir_entry);
  182. // Split path by '/' and traverse directories
  183. char* token = strtok(dir_path_copy, "/");
  184. while (token != (word*)-1)
  185. {
  186. // Find token in current directory
  187. word* dir_addr = brfs_data_block_addr + (current_dir_fat_idx * superblock->bytes_per_block);
  188. word found_dir = 0; // Keep track if token is found in current directory
  189. word i;
  190. for (i = 0; i < dir_entries_max; i++)
  191. {
  192. struct brfs_dir_entry* dir_entry = (struct brfs_dir_entry*) (dir_addr + (i * sizeof(struct brfs_dir_entry)));
  193. if (dir_entry->filename[0] != 0)
  194. {
  195. char decompressed_filename[16];
  196. strdecompress(decompressed_filename, (char*)&(dir_entry->filename));
  197. // Also check for directory flag
  198. if (strcmp(decompressed_filename, token) == 1 && dir_entry->flags == 1)
  199. {
  200. // Found token in current directory
  201. // Set current directory to token's FAT index
  202. current_dir_fat_idx = dir_entry->fat_idx;
  203. found_dir = 1;
  204. break;
  205. }
  206. }
  207. }
  208. // If token not found in current directory, return -1
  209. if (!found_dir)
  210. {
  211. uprint("Directory ");
  212. uprint(dir_path);
  213. uprintln(" not found!");
  214. return -1;
  215. }
  216. token = strtok((word*)-1, "/");
  217. }
  218. return current_dir_fat_idx;
  219. }
  220. /**
  221. * Given the address of the FAT table and the number of blocks, find the next free block
  222. * Returns -1 if no free block is found
  223. * fat_addr: address of the FAT table
  224. * blocks: number of blocks in the FAT table
  225. */
  226. word brfs_find_next_free_block(word* fat_addr, word blocks)
  227. {
  228. word i = 0;
  229. word* fat_ptr = fat_addr;
  230. while (i < blocks)
  231. {
  232. if (*fat_ptr == 0)
  233. {
  234. return i;
  235. }
  236. fat_ptr++;
  237. i++;
  238. }
  239. return -1;
  240. }
  241. /**
  242. * Given the address of a directory data block and the maximum number of entries, find the next free directory entry
  243. * Returns -1 if no free entry is found
  244. * dir_addr: address of the directory data block (not the FAT idx)
  245. * dir_entries_max: maximum number of entries in the directory
  246. */
  247. word brfs_find_next_free_dir_entry(word* dir_addr, word dir_entries_max)
  248. {
  249. word i = 0;
  250. word* dir_ptr = dir_addr;
  251. while (i < dir_entries_max)
  252. {
  253. if (*dir_ptr == 0)
  254. {
  255. return i;
  256. }
  257. dir_ptr += sizeof(struct brfs_dir_entry);
  258. i++;
  259. }
  260. return -1;
  261. }
  262. /**
  263. * Create a single directory entry
  264. * dir_entry: pointer to the directory entry to be created
  265. * filename: name of the file, max 16 chars and uncompressed
  266. * fat_idx: index of the first FAT block of the file/directory
  267. * filesize: size of the file in words
  268. * flags: flags of the file/directory
  269. */
  270. void brfs_create_single_dir_entry(struct brfs_dir_entry* dir_entry, char* filename, word fat_idx, word filesize, word flags)
  271. {
  272. // Initialize to 0
  273. memset((char*)dir_entry, 0, sizeof(*dir_entry));
  274. // Set filename
  275. char compressed_filename[4] = {0,0,0,0};
  276. strcompress(compressed_filename, filename);
  277. memcpy((char*)&(dir_entry->filename), compressed_filename, sizeof(compressed_filename));
  278. // Set other fields
  279. dir_entry->fat_idx = fat_idx;
  280. dir_entry->flags = flags;
  281. dir_entry->filesize = filesize;
  282. }
  283. /**
  284. * Initialize a directory with . and .. entries
  285. * dir_addr: address of the directory data block
  286. * dir_entries_max: maximum number of entries in the directory
  287. * dir_fat_idx: index of the FAT block of the directory
  288. * parent_fat_idx: index of the FAT block of the parent directory
  289. */
  290. void brfs_init_directory(word* dir_addr, word dir_entries_max, word dir_fat_idx, word parent_fat_idx)
  291. {
  292. // Create . entry
  293. struct brfs_dir_entry dir_entry;
  294. brfs_create_single_dir_entry(&dir_entry, ".", dir_fat_idx, dir_entries_max*sizeof(struct brfs_dir_entry), 1);
  295. // Copy to first data entry
  296. memcpy(dir_addr, (char*)&dir_entry, sizeof(dir_entry));
  297. // Create .. entry
  298. brfs_create_single_dir_entry(&dir_entry, "..", parent_fat_idx, dir_entries_max*sizeof(struct brfs_dir_entry), 1);
  299. // Copy to second data entry
  300. memcpy(dir_addr+sizeof(dir_entry), (char*)&dir_entry, sizeof(dir_entry));
  301. // Set FAT table
  302. brfs_ram_storage[SUPERBLOCK_SIZE + dir_fat_idx] = -1;
  303. }
  304. /**
  305. * Format the ram storage as a BRFS filesystem
  306. * blocks: number of blocks in the filesystem
  307. * bytes_per_block: number of bytes per block
  308. * label: label of the filesystem
  309. * full_format: if 1, initialize data section to 0
  310. */
  311. void brfs_format(word blocks, word bytes_per_block, char* label, word full_format)
  312. {
  313. // Create a superblock
  314. struct brfs_superblock superblock;
  315. // Initialize to 0
  316. memset((char*)&superblock, 0, sizeof(superblock));
  317. // Set values of superblock
  318. superblock.total_blocks = blocks;
  319. superblock.bytes_per_block = bytes_per_block;
  320. strcpy((char*)&superblock.label, label);
  321. superblock.brfs_version = 1;
  322. // Copy superblock to head of ram addr
  323. memcpy(brfs_ram_storage, (char*)&superblock, sizeof(superblock));
  324. // Create FAT
  325. memset(brfs_ram_storage + SUPERBLOCK_SIZE, 0, blocks);
  326. // Create Data section
  327. if (full_format)
  328. {
  329. memset(brfs_ram_storage + SUPERBLOCK_SIZE + blocks, 0, blocks * bytes_per_block);
  330. }
  331. // Initialize root dir
  332. word dir_entries_max = bytes_per_block / sizeof(struct brfs_dir_entry);
  333. brfs_init_directory(brfs_ram_storage + SUPERBLOCK_SIZE + blocks, dir_entries_max, 0, 0);
  334. // Clear open files and cursors
  335. memset(brfs_file_pointers, 0, sizeof(brfs_file_pointers));
  336. memset(brfs_cursors, 0, sizeof(brfs_cursors));
  337. // Set all dir entry pointers to 0
  338. word i;
  339. for (i = 0; i < MAX_OPEN_FILES; i++)
  340. {
  341. brfs_dir_entry_pointers[i] = 0;
  342. }
  343. }
  344. /**
  345. * Create a new directory in the directory of parent_dir_path
  346. * parent_dir_path: full path of the parent directory
  347. * dirname: name of the new directory
  348. */
  349. void brfs_create_directory(char* parent_dir_path, char* dirname)
  350. {
  351. struct brfs_superblock* superblock = (struct brfs_superblock*) brfs_ram_storage;
  352. word* brfs_data_block_addr = brfs_ram_storage + SUPERBLOCK_SIZE + superblock->total_blocks;
  353. // Find first free FAT block
  354. word next_free_block = brfs_find_next_free_block(brfs_ram_storage + SUPERBLOCK_SIZE, superblock->total_blocks);
  355. if (next_free_block == -1)
  356. {
  357. uprintln("No free blocks left!");
  358. return;
  359. }
  360. // Find data block address of parent directory path
  361. word parent_dir_fat_idx = brfs_get_fat_idx_of_dir(parent_dir_path);
  362. if (parent_dir_fat_idx == -1)
  363. {
  364. uprint("Parent directory ");
  365. uprint(parent_dir_path);
  366. uprintln(" not found!");
  367. return;
  368. }
  369. // Find first free dir entry
  370. word next_free_dir_entry = brfs_find_next_free_dir_entry(
  371. brfs_data_block_addr + (parent_dir_fat_idx * superblock->bytes_per_block),
  372. superblock->bytes_per_block / sizeof(struct brfs_dir_entry)
  373. );
  374. if (next_free_dir_entry == -1)
  375. {
  376. uprintln("No free dir entries left!");
  377. return;
  378. }
  379. // Create dir entry
  380. struct brfs_dir_entry new_entry;
  381. brfs_create_single_dir_entry(&new_entry, dirname, next_free_block, 0, 1);
  382. // Copy dir entry to first free dir entry
  383. memcpy(
  384. brfs_data_block_addr + (parent_dir_fat_idx * superblock->bytes_per_block) + (next_free_dir_entry * sizeof(struct brfs_dir_entry)),
  385. (char*)&new_entry,
  386. sizeof(new_entry)
  387. );
  388. // Initialize directory
  389. word dir_entries_max = superblock->bytes_per_block / sizeof(struct brfs_dir_entry);
  390. brfs_init_directory(
  391. brfs_data_block_addr + (next_free_block * superblock->bytes_per_block),
  392. dir_entries_max,
  393. next_free_block,
  394. parent_dir_fat_idx
  395. );
  396. }
  397. /**
  398. * Create a new file in the directory of parent_dir_path
  399. * parent_dir_path: full path of the parent directory
  400. * filename: name of the new file
  401. */
  402. void brfs_create_file(char* parent_dir_path, char* filename)
  403. {
  404. struct brfs_superblock* superblock = (struct brfs_superblock*) brfs_ram_storage;
  405. word* brfs_data_block_addr = brfs_ram_storage + SUPERBLOCK_SIZE + superblock->total_blocks;
  406. // Find first free FAT block
  407. word next_free_block = brfs_find_next_free_block(brfs_ram_storage + SUPERBLOCK_SIZE, superblock->total_blocks);
  408. if (next_free_block == -1)
  409. {
  410. uprintln("No free blocks left!");
  411. return;
  412. }
  413. // Find data block address of parent directory path
  414. word parent_dir_fat_idx = brfs_get_fat_idx_of_dir(parent_dir_path);
  415. if (parent_dir_fat_idx == -1)
  416. {
  417. uprint("Parent directory ");
  418. uprint(parent_dir_path);
  419. uprintln(" not found!");
  420. return;
  421. }
  422. // Find first free dir entry
  423. word next_free_dir_entry = brfs_find_next_free_dir_entry(
  424. brfs_data_block_addr + (parent_dir_fat_idx * superblock->bytes_per_block),
  425. superblock->bytes_per_block / sizeof(struct brfs_dir_entry)
  426. );
  427. if (next_free_dir_entry == -1)
  428. {
  429. uprintln("No free dir entries left!");
  430. return;
  431. }
  432. // Create file entry
  433. struct brfs_dir_entry new_entry;
  434. brfs_create_single_dir_entry(&new_entry, filename, next_free_block, 0, 0);
  435. // Copy dir entry to first free dir entry
  436. memcpy(
  437. brfs_data_block_addr + (parent_dir_fat_idx * superblock->bytes_per_block) + (next_free_dir_entry * sizeof(struct brfs_dir_entry)),
  438. (char*)&new_entry,
  439. sizeof(new_entry)
  440. );
  441. // Initialize file by setting data to 0
  442. memset(
  443. brfs_data_block_addr + (next_free_block * superblock->bytes_per_block),
  444. 0,
  445. superblock->bytes_per_block
  446. );
  447. // Update FAT
  448. brfs_ram_storage[SUPERBLOCK_SIZE + next_free_block] = -1;
  449. }
  450. /**
  451. * List the contents of a directory over UART
  452. * dir_path: full path of the directory
  453. */
  454. void brfs_list_directory(char* dir_path)
  455. {
  456. uprint("Listing directory ");
  457. uprintln(dir_path);
  458. uprintln("-------------------");
  459. // Find data block address of parent directory path
  460. word dir_fat_idx = brfs_get_fat_idx_of_dir(dir_path);
  461. if (dir_fat_idx == -1)
  462. {
  463. uprint("Parent directory ");
  464. uprint(dir_path);
  465. uprintln(" not found!");
  466. return;
  467. }
  468. struct brfs_superblock* superblock = (struct brfs_superblock*) brfs_ram_storage;
  469. word* dir_addr = brfs_ram_storage + SUPERBLOCK_SIZE + superblock->total_blocks + (dir_fat_idx * superblock->bytes_per_block);
  470. word dir_entries_max = superblock->bytes_per_block / sizeof(struct brfs_dir_entry);
  471. word i;
  472. for (i = 0; i < dir_entries_max; i++)
  473. {
  474. struct brfs_dir_entry* dir_entry = (struct brfs_dir_entry*) (dir_addr + (i * sizeof(struct brfs_dir_entry)));
  475. if (dir_entry->filename[0] != 0)
  476. {
  477. uprint("Filename: ");
  478. char decompressed_filename[16];
  479. strdecompress(decompressed_filename, (char*)&(dir_entry->filename));
  480. uprint(decompressed_filename);
  481. uprint(" FAT idx: ");
  482. uprintDec((dir_entry->fat_idx));
  483. uprint(" Flags: ");
  484. uprintDec((dir_entry->flags));
  485. uprint(" Filesize: ");
  486. uprintDec((dir_entry->filesize));
  487. uprintc('\n');
  488. }
  489. }
  490. uprintln("");
  491. }
  492. /**
  493. * Open a file for reading and writing
  494. * Returns the file pointer (FAT idx of file), or -1 on error
  495. * file_path: full path of the file
  496. */
  497. word brfs_open_file(char* file_path)
  498. {
  499. // Split filename from path using basename and dirname
  500. char dirname_output[MAX_PATH_LENGTH];
  501. char* file_path_basename = basename(file_path);
  502. char* file_path_dirname = dirname(dirname_output, file_path);
  503. // Find data block address of parent directory path
  504. word dir_fat_idx = brfs_get_fat_idx_of_dir(file_path_dirname);
  505. if (dir_fat_idx == -1)
  506. {
  507. uprint("Parent directory ");
  508. uprint(file_path_dirname);
  509. uprintln(" not found!");
  510. return -1;
  511. }
  512. // Find file in directory
  513. struct brfs_superblock* superblock = (struct brfs_superblock*) brfs_ram_storage;
  514. word* dir_addr = brfs_ram_storage + SUPERBLOCK_SIZE + superblock->total_blocks + (dir_fat_idx * superblock->bytes_per_block);
  515. word dir_entries_max = superblock->bytes_per_block / sizeof(struct brfs_dir_entry);
  516. word i;
  517. for (i = 0; i < dir_entries_max; i++)
  518. {
  519. struct brfs_dir_entry* dir_entry = (struct brfs_dir_entry*) (dir_addr + (i * sizeof(struct brfs_dir_entry)));
  520. if (dir_entry->filename[0] != 0)
  521. {
  522. char decompressed_filename[16];
  523. strdecompress(decompressed_filename, (char*)&(dir_entry->filename));
  524. // Also check for directory flag to be 0
  525. if (strcmp(decompressed_filename, file_path_basename) == 1 && dir_entry->flags == 0)
  526. {
  527. // Found file
  528. // Check if file is already open
  529. word j;
  530. for (j = 0; j < MAX_OPEN_FILES; j++)
  531. {
  532. if (brfs_file_pointers[j] == dir_entry->fat_idx)
  533. {
  534. uprint("File ");
  535. uprint(file_path_basename);
  536. uprintln(" already open!");
  537. return -1;
  538. }
  539. }
  540. // Find first free file pointer
  541. word next_free_file_pointer = -1;
  542. for (j = 0; j < MAX_OPEN_FILES; j++)
  543. {
  544. if (brfs_file_pointers[j] == 0)
  545. {
  546. next_free_file_pointer = j;
  547. break;
  548. }
  549. }
  550. if (next_free_file_pointer == -1)
  551. {
  552. uprintln("All files already opened!");
  553. return -1;
  554. }
  555. // Open file
  556. brfs_file_pointers[next_free_file_pointer] = dir_entry->fat_idx;
  557. brfs_cursors[next_free_file_pointer] = 0;
  558. brfs_dir_entry_pointers[next_free_file_pointer] = dir_entry;
  559. return brfs_file_pointers[next_free_file_pointer];
  560. }
  561. }
  562. }
  563. uprint("File ");
  564. uprint(file_path_basename);
  565. uprintln(" not found!");
  566. return -1;
  567. }
  568. /**
  569. * Close an opened file
  570. * Returns 1 on success, 0 on error
  571. * file_pointer: file pointer returned by brfs_open_file
  572. */
  573. word brfs_close_file(word file_pointer)
  574. {
  575. // Find file pointer
  576. word i;
  577. for (i = 0; i < MAX_OPEN_FILES; i++)
  578. {
  579. if (brfs_file_pointers[i] == file_pointer)
  580. {
  581. // Close file
  582. brfs_file_pointers[i] = 0;
  583. brfs_cursors[i] = 0;
  584. brfs_dir_entry_pointers[i] = 0;
  585. return 1;
  586. }
  587. }
  588. uprintln("File not found!");
  589. return 0;
  590. }
  591. /**
  592. * Set the cursor of an opened file
  593. * Returns 1 on success, 0 on error
  594. * file_pointer: file pointer returned by brfs_open_file
  595. * cursor: new cursor position in words
  596. */
  597. word brfs_set_cursor(word file_pointer, word cursor)
  598. {
  599. if (file_pointer == 0)
  600. {
  601. uprintln("File not open!");
  602. return 0;
  603. }
  604. // Find file pointer
  605. word i;
  606. for (i = 0; i < MAX_OPEN_FILES; i++)
  607. {
  608. if (brfs_file_pointers[i] == file_pointer)
  609. {
  610. // Set cursor
  611. if (cursor < 0 || cursor > brfs_dir_entry_pointers[i]->filesize)
  612. {
  613. cursor = brfs_dir_entry_pointers[i]->filesize;
  614. }
  615. // TODO: add block logic
  616. brfs_cursors[i] = cursor;
  617. return 1;
  618. }
  619. }
  620. uprintln("File not found!");
  621. return 0;
  622. }
  623. /**
  624. * Get the cursor of an opened file
  625. * Returns the cursor position in words, or -1 on error
  626. * file_pointer: file pointer returned by brfs_open_file
  627. */
  628. word brfs_get_cursor(word file_pointer)
  629. {
  630. if (file_pointer == 0)
  631. {
  632. uprintln("File not open!");
  633. return 0;
  634. }
  635. // Find file pointer
  636. word i;
  637. for (i = 0; i < MAX_OPEN_FILES; i++)
  638. {
  639. if (brfs_file_pointers[i] == file_pointer)
  640. {
  641. // Get cursor
  642. return brfs_cursors[i];
  643. }
  644. }
  645. uprintln("File not found!");
  646. return -1;
  647. }
  648. /**
  649. * Read a file from the cursor position
  650. * Returns the number of words read, or -1 on error
  651. * file_pointer: file pointer returned by brfs_open_file
  652. * buffer: buffer to read the file into
  653. * length: number of words to read
  654. */
  655. word brfs_read(word file_pointer, word* buffer, word length)
  656. {
  657. if (file_pointer == 0)
  658. {
  659. uprintln("File not open!");
  660. return 0;
  661. }
  662. struct brfs_superblock* superblock = (struct brfs_superblock*) brfs_ram_storage;
  663. word* data_block_addr = brfs_ram_storage + SUPERBLOCK_SIZE + superblock->total_blocks + (file_pointer * superblock->bytes_per_block);
  664. // Find file pointer
  665. word i;
  666. for (i = 0; i < MAX_OPEN_FILES; i++)
  667. {
  668. if (brfs_file_pointers[i] == file_pointer)
  669. {
  670. if (length < 0)
  671. {
  672. uprintln("Length cannot be negative!");
  673. return 0;
  674. }
  675. // Trunctate length to file size - cursor
  676. if (length > brfs_dir_entry_pointers[i]->filesize - brfs_cursors[i])
  677. {
  678. length = brfs_dir_entry_pointers[i]->filesize - brfs_cursors[i];
  679. }
  680. // Get FAT index of file at cursor
  681. // Loop:
  682. // - calculate words until end of block (or up to length)
  683. // - read words until end of block (or up to length)
  684. // - decrease length by words read
  685. // - get next block from FAT
  686. // - repeat until length is 0
  687. }
  688. }
  689. }
  690. /**
  691. * Stat a file or directory
  692. * Returns the directory entry, or -1 on error
  693. */
  694. struct brfs_dir_entry* brfs_stat(char* file_path)
  695. {
  696. // Split filename from path using basename and dirname
  697. char dirname_output[MAX_PATH_LENGTH];
  698. char* file_path_basename = basename(file_path);
  699. char* file_path_dirname = dirname(dirname_output, file_path);
  700. // Find data block address of parent directory path
  701. word dir_fat_idx = brfs_get_fat_idx_of_dir(file_path_dirname);
  702. if (dir_fat_idx == -1)
  703. {
  704. uprint("Parent directory ");
  705. uprint(file_path_dirname);
  706. uprintln(" not found!");
  707. return (struct brfs_dir_entry*)-1;
  708. }
  709. // Find file in directory
  710. struct brfs_superblock* superblock = (struct brfs_superblock*) brfs_ram_storage;
  711. word* dir_addr = brfs_ram_storage + SUPERBLOCK_SIZE + superblock->total_blocks + (dir_fat_idx * superblock->bytes_per_block);
  712. word dir_entries_max = superblock->bytes_per_block / sizeof(struct brfs_dir_entry);
  713. word i;
  714. for (i = 0; i < dir_entries_max; i++)
  715. {
  716. struct brfs_dir_entry* dir_entry = (struct brfs_dir_entry*) (dir_addr + (i * sizeof(struct brfs_dir_entry)));
  717. if (dir_entry->filename[0] != 0)
  718. {
  719. char decompressed_filename[16];
  720. strdecompress(decompressed_filename, (char*)&(dir_entry->filename));
  721. // Also check for directory flag to be 0
  722. if (strcmp(decompressed_filename, file_path_basename) == 1)
  723. {
  724. return dir_entry;
  725. }
  726. }
  727. }
  728. uprint("File or directory ");
  729. uprint(file_path_basename);
  730. uprintln(" not found!");
  731. return (struct brfs_dir_entry*)-1;
  732. }
  733. int main()
  734. {
  735. // Clear UART screen:
  736. uprintc(0x1B);
  737. uprintc(0x5B);
  738. uprintc(0x32);
  739. uprintc(0x4A);
  740. uprintln("------------------------");
  741. uprintln("BRFS test implementation");
  742. uprintln("------------------------");
  743. word blocks = 8;
  744. word bytes_per_block = 32;
  745. word full_format = 1;
  746. brfs_format(blocks, bytes_per_block, "Label", full_format);
  747. brfs_create_file("/", "file1.txt");
  748. brfs_create_directory(".", "dir1");
  749. brfs_create_file("dir1", "file2.txt");
  750. brfs_list_directory("/");
  751. brfs_list_directory("dir1");
  752. word fp = brfs_open_file("/dir1");
  753. brfs_dump(blocks, blocks*bytes_per_block);
  754. return 'q';
  755. }
  756. void interrupt()
  757. {
  758. // handle all interrupts
  759. word i = getIntID();
  760. switch(i)
  761. {
  762. case INTID_TIMER1:
  763. timer1Value = 1; // notify ending of timer1
  764. break;
  765. default:
  766. break;
  767. }
  768. }