BRFS.C 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236
  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. - [x] Stat (returns dir entry)
  51. - [x] Set cursor
  52. - [x] Get cursor
  53. - [x] Read file
  54. - [x] Write file
  55. - [x] 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, 16);
  138. // Datablock dump
  139. uprintln("\nData:");
  140. brfs_dump_section(brfs_ram_storage+SUPERBLOCK_SIZE+fatsize, datasize, 32);
  141. uprintln("\nOpen files:");
  142. word i;
  143. for (i = 0; i < MAX_OPEN_FILES; i++)
  144. {
  145. uprint("FP");
  146. uprintDec(i+1);
  147. uprint(":");
  148. uprint(" FAT idx: ");
  149. uprintDec(brfs_file_pointers[i]);
  150. uprint(" Cursor: ");
  151. uprintDec(brfs_cursors[i]);
  152. uprint(" Size: ");
  153. uprintDec(brfs_dir_entry_pointers[i] ? brfs_dir_entry_pointers[i]->filesize : 0);
  154. uprintc('\n');
  155. }
  156. }
  157. /**
  158. * Return the FAT index of a directory, or -1 if not found
  159. * dir_path: full path of the directory
  160. */
  161. word brfs_get_fat_idx_of_dir(char* dir_path)
  162. {
  163. // Check length of path
  164. if (strlen(dir_path) > MAX_PATH_LENGTH)
  165. {
  166. uprintln("Path too long!");
  167. return -1;
  168. }
  169. // Start with root directory
  170. word current_dir_fat_idx = 0;
  171. // Check if root directory is requested
  172. if (strcmp(dir_path, "/") == 1)
  173. {
  174. return current_dir_fat_idx;
  175. }
  176. // Copy dir_path, size + 1 for null terminator
  177. // Since strtok modifies the string
  178. char dir_path_copy[MAX_PATH_LENGTH+1];
  179. strcpy(dir_path_copy, dir_path);
  180. struct brfs_superblock* superblock = (struct brfs_superblock*) brfs_ram_storage;
  181. word* brfs_data_block_addr = brfs_ram_storage + SUPERBLOCK_SIZE + superblock->total_blocks;
  182. word dir_entries_max = superblock->bytes_per_block / sizeof(struct brfs_dir_entry);
  183. // Split path by '/' and traverse directories
  184. char* token = strtok(dir_path_copy, "/");
  185. while (token != (word*)-1)
  186. {
  187. // Find token in current directory
  188. word* dir_addr = brfs_data_block_addr + (current_dir_fat_idx * superblock->bytes_per_block);
  189. word found_dir = 0; // Keep track if token is found in current directory
  190. word i;
  191. for (i = 0; i < dir_entries_max; i++)
  192. {
  193. struct brfs_dir_entry* dir_entry = (struct brfs_dir_entry*) (dir_addr + (i * sizeof(struct brfs_dir_entry)));
  194. if (dir_entry->filename[0] != 0)
  195. {
  196. char decompressed_filename[16];
  197. strdecompress(decompressed_filename, (char*)&(dir_entry->filename));
  198. // Also check for directory flag
  199. if (strcmp(decompressed_filename, token) == 1 && dir_entry->flags == 1)
  200. {
  201. // Found token in current directory
  202. // Set current directory to token's FAT index
  203. current_dir_fat_idx = dir_entry->fat_idx;
  204. found_dir = 1;
  205. break;
  206. }
  207. }
  208. }
  209. // If token not found in current directory, return -1
  210. if (!found_dir)
  211. {
  212. uprint("Directory ");
  213. uprint(dir_path);
  214. uprintln(" not found!");
  215. return -1;
  216. }
  217. token = strtok((word*)-1, "/");
  218. }
  219. return current_dir_fat_idx;
  220. }
  221. /**
  222. * Given the address of the FAT table and the number of blocks, find the next free block
  223. * Returns -1 if no free block is found
  224. * fat_addr: address of the FAT table
  225. * blocks: number of blocks in the FAT table
  226. */
  227. word brfs_find_next_free_block(word* fat_addr, word blocks)
  228. {
  229. word i = 0;
  230. word* fat_ptr = fat_addr;
  231. while (i < blocks)
  232. {
  233. if (*fat_ptr == 0)
  234. {
  235. return i;
  236. }
  237. fat_ptr++;
  238. i++;
  239. }
  240. return -1;
  241. }
  242. /**
  243. * Given the address of a directory data block and the maximum number of entries, find the next free directory entry
  244. * Returns -1 if no free entry is found
  245. * dir_addr: address of the directory data block (not the FAT idx)
  246. * dir_entries_max: maximum number of entries in the directory
  247. */
  248. word brfs_find_next_free_dir_entry(word* dir_addr, word dir_entries_max)
  249. {
  250. word i = 0;
  251. word* dir_ptr = dir_addr;
  252. while (i < dir_entries_max)
  253. {
  254. if (*dir_ptr == 0)
  255. {
  256. return i;
  257. }
  258. dir_ptr += sizeof(struct brfs_dir_entry);
  259. i++;
  260. }
  261. return -1;
  262. }
  263. /**
  264. * Create a single directory entry
  265. * dir_entry: pointer to the directory entry to be created
  266. * filename: name of the file, max 16 chars and uncompressed
  267. * fat_idx: index of the first FAT block of the file/directory
  268. * filesize: size of the file in words
  269. * flags: flags of the file/directory
  270. */
  271. void brfs_create_single_dir_entry(struct brfs_dir_entry* dir_entry, char* filename, word fat_idx, word filesize, word flags)
  272. {
  273. // Initialize to 0
  274. memset((char*)dir_entry, 0, sizeof(*dir_entry));
  275. // Set filename
  276. char compressed_filename[4] = {0,0,0,0};
  277. strcompress(compressed_filename, filename);
  278. memcpy((char*)&(dir_entry->filename), compressed_filename, sizeof(compressed_filename));
  279. // Set other fields
  280. dir_entry->fat_idx = fat_idx;
  281. dir_entry->flags = flags;
  282. dir_entry->filesize = filesize;
  283. }
  284. /**
  285. * Initialize a directory with . and .. entries
  286. * dir_addr: address of the directory data block
  287. * dir_entries_max: maximum number of entries in the directory
  288. * dir_fat_idx: index of the FAT block of the directory
  289. * parent_fat_idx: index of the FAT block of the parent directory
  290. */
  291. void brfs_init_directory(word* dir_addr, word dir_entries_max, word dir_fat_idx, word parent_fat_idx)
  292. {
  293. // Create . entry
  294. struct brfs_dir_entry dir_entry;
  295. brfs_create_single_dir_entry(&dir_entry, ".", dir_fat_idx, dir_entries_max*sizeof(struct brfs_dir_entry), 1);
  296. // Copy to first data entry
  297. memcpy(dir_addr, (char*)&dir_entry, sizeof(dir_entry));
  298. // Create .. entry
  299. brfs_create_single_dir_entry(&dir_entry, "..", parent_fat_idx, dir_entries_max*sizeof(struct brfs_dir_entry), 1);
  300. // Copy to second data entry
  301. memcpy(dir_addr+sizeof(dir_entry), (char*)&dir_entry, sizeof(dir_entry));
  302. // Set FAT table
  303. brfs_ram_storage[SUPERBLOCK_SIZE + dir_fat_idx] = -1;
  304. }
  305. /**
  306. * Format the ram storage as a BRFS filesystem
  307. * blocks: number of blocks in the filesystem
  308. * bytes_per_block: number of bytes per block
  309. * label: label of the filesystem
  310. * full_format: if 1, initialize data section to 0
  311. */
  312. void brfs_format(word blocks, word bytes_per_block, char* label, word full_format)
  313. {
  314. // Create a superblock
  315. struct brfs_superblock superblock;
  316. // Initialize to 0
  317. memset((char*)&superblock, 0, sizeof(superblock));
  318. // Set values of superblock
  319. superblock.total_blocks = blocks;
  320. superblock.bytes_per_block = bytes_per_block;
  321. strcpy((char*)&superblock.label, label);
  322. superblock.brfs_version = 1;
  323. // Copy superblock to head of ram addr
  324. memcpy(brfs_ram_storage, (char*)&superblock, sizeof(superblock));
  325. // Create FAT
  326. memset(brfs_ram_storage + SUPERBLOCK_SIZE, 0, blocks);
  327. // Create Data section
  328. if (full_format)
  329. {
  330. memset(brfs_ram_storage + SUPERBLOCK_SIZE + blocks, 0, blocks * bytes_per_block);
  331. }
  332. // Initialize root dir
  333. word dir_entries_max = bytes_per_block / sizeof(struct brfs_dir_entry);
  334. brfs_init_directory(brfs_ram_storage + SUPERBLOCK_SIZE + blocks, dir_entries_max, 0, 0);
  335. // Clear open files and cursors
  336. memset(brfs_file_pointers, 0, sizeof(brfs_file_pointers));
  337. memset(brfs_cursors, 0, sizeof(brfs_cursors));
  338. // Set all dir entry pointers to 0
  339. word i;
  340. for (i = 0; i < MAX_OPEN_FILES; i++)
  341. {
  342. brfs_dir_entry_pointers[i] = 0;
  343. }
  344. }
  345. /**
  346. * Create a new directory in the directory of parent_dir_path
  347. * Returns 1 on success, 0 on error
  348. * parent_dir_path: full path of the parent directory
  349. * dirname: name of the new directory
  350. */
  351. word brfs_create_directory(char* parent_dir_path, char* dirname)
  352. {
  353. struct brfs_superblock* superblock = (struct brfs_superblock*) brfs_ram_storage;
  354. word* brfs_data_block_addr = brfs_ram_storage + SUPERBLOCK_SIZE + superblock->total_blocks;
  355. // Find first free FAT block
  356. word next_free_block = brfs_find_next_free_block(brfs_ram_storage + SUPERBLOCK_SIZE, superblock->total_blocks);
  357. if (next_free_block == -1)
  358. {
  359. uprintln("No free blocks left!");
  360. return 0;
  361. }
  362. // Find data block address of parent directory path
  363. word parent_dir_fat_idx = brfs_get_fat_idx_of_dir(parent_dir_path);
  364. if (parent_dir_fat_idx == -1)
  365. {
  366. uprint("Parent directory ");
  367. uprint(parent_dir_path);
  368. uprintln(" not found!");
  369. return 0;
  370. }
  371. // Check if file or folder already exists
  372. word* parent_dir_addr = brfs_data_block_addr + (parent_dir_fat_idx * superblock->bytes_per_block);
  373. word dir_entries_max = superblock->bytes_per_block / sizeof(struct brfs_dir_entry);
  374. word i;
  375. for (i = 0; i < dir_entries_max; i++)
  376. {
  377. struct brfs_dir_entry* dir_entry = (struct brfs_dir_entry*) (parent_dir_addr + (i * sizeof(struct brfs_dir_entry)));
  378. if (dir_entry->filename[0] != 0)
  379. {
  380. char decompressed_filename[16];
  381. strdecompress(decompressed_filename, (char*)&(dir_entry->filename));
  382. if (strcmp(decompressed_filename, dirname) == 1)
  383. {
  384. uprint(dirname);
  385. uprintln(" already exists!");
  386. return 0;
  387. }
  388. }
  389. }
  390. // Find first free dir entry
  391. word next_free_dir_entry = brfs_find_next_free_dir_entry(
  392. brfs_data_block_addr + (parent_dir_fat_idx * superblock->bytes_per_block),
  393. superblock->bytes_per_block / sizeof(struct brfs_dir_entry)
  394. );
  395. if (next_free_dir_entry == -1)
  396. {
  397. uprintln("No free dir entries left!");
  398. return 0;
  399. }
  400. // Create dir entry
  401. struct brfs_dir_entry new_entry;
  402. brfs_create_single_dir_entry(&new_entry, dirname, next_free_block, 0, 1);
  403. // Copy dir entry to first free dir entry
  404. memcpy(
  405. brfs_data_block_addr + (parent_dir_fat_idx * superblock->bytes_per_block) + (next_free_dir_entry * sizeof(struct brfs_dir_entry)),
  406. (char*)&new_entry,
  407. sizeof(new_entry)
  408. );
  409. // Initialize directory
  410. brfs_init_directory(
  411. brfs_data_block_addr + (next_free_block * superblock->bytes_per_block),
  412. dir_entries_max,
  413. next_free_block,
  414. parent_dir_fat_idx
  415. );
  416. return 1;
  417. }
  418. /**
  419. * Create a new file in the directory of parent_dir_path
  420. * Returns 1 on success, 0 on error
  421. * parent_dir_path: full path of the parent directory
  422. * filename: name of the new file
  423. */
  424. word brfs_create_file(char* parent_dir_path, char* filename)
  425. {
  426. struct brfs_superblock* superblock = (struct brfs_superblock*) brfs_ram_storage;
  427. word* brfs_data_block_addr = brfs_ram_storage + SUPERBLOCK_SIZE + superblock->total_blocks;
  428. // Find first free FAT block
  429. word next_free_block = brfs_find_next_free_block(brfs_ram_storage + SUPERBLOCK_SIZE, superblock->total_blocks);
  430. if (next_free_block == -1)
  431. {
  432. uprintln("No free blocks left!");
  433. return 0;
  434. }
  435. // Find data block address of parent directory path
  436. word parent_dir_fat_idx = brfs_get_fat_idx_of_dir(parent_dir_path);
  437. if (parent_dir_fat_idx == -1)
  438. {
  439. uprint("Parent directory ");
  440. uprint(parent_dir_path);
  441. uprintln(" not found!");
  442. return 0;
  443. }
  444. // Check if file or folder already exists
  445. word* parent_dir_addr = brfs_data_block_addr + (parent_dir_fat_idx * superblock->bytes_per_block);
  446. word dir_entries_max = superblock->bytes_per_block / sizeof(struct brfs_dir_entry);
  447. word i;
  448. for (i = 0; i < dir_entries_max; i++)
  449. {
  450. struct brfs_dir_entry* dir_entry = (struct brfs_dir_entry*) (parent_dir_addr + (i * sizeof(struct brfs_dir_entry)));
  451. if (dir_entry->filename[0] != 0)
  452. {
  453. char decompressed_filename[16];
  454. strdecompress(decompressed_filename, (char*)&(dir_entry->filename));
  455. if (strcmp(decompressed_filename, filename) == 1)
  456. {
  457. uprint(filename);
  458. uprintln(" already exists!");
  459. return 0;
  460. }
  461. }
  462. }
  463. // Find first free dir entry
  464. word next_free_dir_entry = brfs_find_next_free_dir_entry(
  465. brfs_data_block_addr + (parent_dir_fat_idx * superblock->bytes_per_block),
  466. superblock->bytes_per_block / sizeof(struct brfs_dir_entry)
  467. );
  468. if (next_free_dir_entry == -1)
  469. {
  470. uprintln("No free dir entries left!");
  471. return 0;
  472. }
  473. // Create file entry
  474. struct brfs_dir_entry new_entry;
  475. brfs_create_single_dir_entry(&new_entry, filename, next_free_block, 0, 0);
  476. // Copy dir entry to first free dir entry
  477. memcpy(
  478. brfs_data_block_addr + (parent_dir_fat_idx * superblock->bytes_per_block) + (next_free_dir_entry * sizeof(struct brfs_dir_entry)),
  479. (char*)&new_entry,
  480. sizeof(new_entry)
  481. );
  482. // Initialize file by setting data to 0
  483. memset(
  484. brfs_data_block_addr + (next_free_block * superblock->bytes_per_block),
  485. 0,
  486. superblock->bytes_per_block
  487. );
  488. // Update FAT
  489. brfs_ram_storage[SUPERBLOCK_SIZE + next_free_block] = -1;
  490. return 1;
  491. }
  492. /**
  493. * List the contents of a directory over UART
  494. * dir_path: full path of the directory
  495. */
  496. void brfs_list_directory(char* dir_path)
  497. {
  498. uprint("Listing directory ");
  499. uprintln(dir_path);
  500. uprintln("-------------------");
  501. // Find data block address of parent directory path
  502. word dir_fat_idx = brfs_get_fat_idx_of_dir(dir_path);
  503. if (dir_fat_idx == -1)
  504. {
  505. uprint("Parent directory ");
  506. uprint(dir_path);
  507. uprintln(" not found!");
  508. return;
  509. }
  510. struct brfs_superblock* superblock = (struct brfs_superblock*) brfs_ram_storage;
  511. word* dir_addr = brfs_ram_storage + SUPERBLOCK_SIZE + superblock->total_blocks + (dir_fat_idx * superblock->bytes_per_block);
  512. word dir_entries_max = superblock->bytes_per_block / sizeof(struct brfs_dir_entry);
  513. word i;
  514. for (i = 0; i < dir_entries_max; i++)
  515. {
  516. struct brfs_dir_entry* dir_entry = (struct brfs_dir_entry*) (dir_addr + (i * sizeof(struct brfs_dir_entry)));
  517. if (dir_entry->filename[0] != 0)
  518. {
  519. uprint("Filename: ");
  520. char decompressed_filename[16];
  521. strdecompress(decompressed_filename, (char*)&(dir_entry->filename));
  522. uprint(decompressed_filename);
  523. uprint(" FAT idx: ");
  524. uprintDec((dir_entry->fat_idx));
  525. uprint(" Flags: ");
  526. uprintDec((dir_entry->flags));
  527. uprint(" Filesize: ");
  528. uprintDec((dir_entry->filesize));
  529. uprintc('\n');
  530. }
  531. }
  532. uprintln("");
  533. }
  534. /**
  535. * Open a file for reading and writing
  536. * Returns the file pointer (FAT idx of file), or -1 on error
  537. * file_path: full path of the file
  538. */
  539. word brfs_open_file(char* file_path)
  540. {
  541. // Split filename from path using basename and dirname
  542. char dirname_output[MAX_PATH_LENGTH];
  543. char* file_path_basename = basename(file_path);
  544. char* file_path_dirname = dirname(dirname_output, file_path);
  545. // Find data block address of parent directory path
  546. word dir_fat_idx = brfs_get_fat_idx_of_dir(file_path_dirname);
  547. if (dir_fat_idx == -1)
  548. {
  549. uprint("Parent directory ");
  550. uprint(file_path_dirname);
  551. uprintln(" not found!");
  552. return -1;
  553. }
  554. // Find file in directory
  555. struct brfs_superblock* superblock = (struct brfs_superblock*) brfs_ram_storage;
  556. word* dir_addr = brfs_ram_storage + SUPERBLOCK_SIZE + superblock->total_blocks + (dir_fat_idx * superblock->bytes_per_block);
  557. word dir_entries_max = superblock->bytes_per_block / sizeof(struct brfs_dir_entry);
  558. word i;
  559. for (i = 0; i < dir_entries_max; i++)
  560. {
  561. struct brfs_dir_entry* dir_entry = (struct brfs_dir_entry*) (dir_addr + (i * sizeof(struct brfs_dir_entry)));
  562. if (dir_entry->filename[0] != 0)
  563. {
  564. char decompressed_filename[16];
  565. strdecompress(decompressed_filename, (char*)&(dir_entry->filename));
  566. // Also check for directory flag to be 0
  567. if (strcmp(decompressed_filename, file_path_basename) == 1 && dir_entry->flags == 0)
  568. {
  569. // Found file
  570. // Check if file is already open
  571. word j;
  572. for (j = 0; j < MAX_OPEN_FILES; j++)
  573. {
  574. if (brfs_file_pointers[j] == dir_entry->fat_idx)
  575. {
  576. uprint("File ");
  577. uprint(file_path_basename);
  578. uprintln(" already open!");
  579. return -1;
  580. }
  581. }
  582. // Find first free file pointer
  583. word next_free_file_pointer = -1;
  584. for (j = 0; j < MAX_OPEN_FILES; j++)
  585. {
  586. if (brfs_file_pointers[j] == 0)
  587. {
  588. next_free_file_pointer = j;
  589. break;
  590. }
  591. }
  592. if (next_free_file_pointer == -1)
  593. {
  594. uprintln("All files already opened!");
  595. return -1;
  596. }
  597. // Open file
  598. brfs_file_pointers[next_free_file_pointer] = dir_entry->fat_idx;
  599. brfs_cursors[next_free_file_pointer] = 0;
  600. brfs_dir_entry_pointers[next_free_file_pointer] = dir_entry;
  601. return brfs_file_pointers[next_free_file_pointer];
  602. }
  603. }
  604. }
  605. uprint("File ");
  606. uprint(file_path_basename);
  607. uprintln(" not found!");
  608. return -1;
  609. }
  610. /**
  611. * Close an opened file
  612. * Returns 1 on success, 0 on error
  613. * file_pointer: file pointer returned by brfs_open_file
  614. */
  615. word brfs_close_file(word file_pointer)
  616. {
  617. // Find file pointer
  618. word i;
  619. for (i = 0; i < MAX_OPEN_FILES; i++)
  620. {
  621. if (brfs_file_pointers[i] == file_pointer)
  622. {
  623. // Close file
  624. brfs_file_pointers[i] = 0;
  625. brfs_cursors[i] = 0;
  626. brfs_dir_entry_pointers[i] = 0;
  627. return 1;
  628. }
  629. }
  630. uprintln("File not found!");
  631. return 0;
  632. }
  633. /**
  634. * Delete a file by removing all FAT blocks and the directory entry
  635. * Returns 1 on success, 0 on error
  636. * file_path: full path of the file
  637. */
  638. word brfs_delete_file(char* file_path)
  639. {
  640. // Split filename from path using basename and dirname
  641. char dirname_output[MAX_PATH_LENGTH];
  642. char* file_path_basename = basename(file_path);
  643. char* file_path_dirname = dirname(dirname_output, file_path);
  644. // Find data block address of parent directory path
  645. word dir_fat_idx = brfs_get_fat_idx_of_dir(file_path_dirname);
  646. if (dir_fat_idx == -1)
  647. {
  648. uprint("Parent directory ");
  649. uprint(file_path_dirname);
  650. uprintln(" not found!");
  651. return 0;
  652. }
  653. // Find file in directory
  654. struct brfs_superblock* superblock = (struct brfs_superblock*) brfs_ram_storage;
  655. word* dir_addr = brfs_ram_storage + SUPERBLOCK_SIZE + superblock->total_blocks + (dir_fat_idx * superblock->bytes_per_block);
  656. word dir_entries_max = superblock->bytes_per_block / sizeof(struct brfs_dir_entry);
  657. word i;
  658. for (i = 0; i < dir_entries_max; i++)
  659. {
  660. struct brfs_dir_entry* dir_entry = (struct brfs_dir_entry*) (dir_addr + (i * sizeof(struct brfs_dir_entry)));
  661. if (dir_entry->filename[0] != 0)
  662. {
  663. char decompressed_filename[16];
  664. strdecompress(decompressed_filename, (char*)&(dir_entry->filename));
  665. // Also check for directory flag to be 0
  666. if (strcmp(decompressed_filename, file_path_basename) == 1 && dir_entry->flags == 0)
  667. {
  668. // Found file
  669. // Check if file is already open
  670. word j;
  671. for (j = 0; j < MAX_OPEN_FILES; j++)
  672. {
  673. if (brfs_file_pointers[j] == dir_entry->fat_idx)
  674. {
  675. uprint("File ");
  676. uprint(file_path_basename);
  677. uprintln(" is open!");
  678. return 0;
  679. }
  680. }
  681. // Delete fat blocks
  682. word current_fat_idx = dir_entry->fat_idx;
  683. word next_fat_idx;
  684. while (current_fat_idx != -1)
  685. {
  686. next_fat_idx = brfs_ram_storage[SUPERBLOCK_SIZE + current_fat_idx];
  687. brfs_ram_storage[SUPERBLOCK_SIZE + current_fat_idx] = 0;
  688. current_fat_idx = next_fat_idx;
  689. }
  690. // Delete file
  691. memset((char*)dir_entry, 0, sizeof(struct brfs_dir_entry));
  692. return 1;
  693. }
  694. }
  695. }
  696. uprint("File ");
  697. uprint(file_path_basename);
  698. uprintln(" not found!");
  699. return 0;
  700. }
  701. /**
  702. * Set the cursor of an opened file
  703. * Returns 1 on success, 0 on error
  704. * file_pointer: file pointer returned by brfs_open_file
  705. * cursor: new cursor position in words
  706. */
  707. word brfs_set_cursor(word file_pointer, word cursor)
  708. {
  709. if (file_pointer == 0)
  710. {
  711. uprintln("File not open!");
  712. return 0;
  713. }
  714. // Find file pointer
  715. word i;
  716. for (i = 0; i < MAX_OPEN_FILES; i++)
  717. {
  718. if (brfs_file_pointers[i] == file_pointer)
  719. {
  720. // Set cursor
  721. if (cursor < 0 || cursor > brfs_dir_entry_pointers[i]->filesize)
  722. {
  723. cursor = brfs_dir_entry_pointers[i]->filesize;
  724. }
  725. brfs_cursors[i] = cursor;
  726. return 1;
  727. }
  728. }
  729. uprintln("File not found!");
  730. return 0;
  731. }
  732. /**
  733. * Get the cursor of an opened file
  734. * Returns the cursor position in words, or -1 on error
  735. * file_pointer: file pointer returned by brfs_open_file
  736. */
  737. word brfs_get_cursor(word file_pointer)
  738. {
  739. if (file_pointer == 0)
  740. {
  741. uprintln("File not open!");
  742. return -1;
  743. }
  744. // Find file pointer
  745. word i;
  746. for (i = 0; i < MAX_OPEN_FILES; i++)
  747. {
  748. if (brfs_file_pointers[i] == file_pointer)
  749. {
  750. // Get cursor
  751. return brfs_cursors[i];
  752. }
  753. }
  754. uprintln("File not found!");
  755. return -1;
  756. }
  757. /**
  758. * Get the FAT index of a file at the cursor
  759. * Returns the FAT index, or 0 on error
  760. * file_pointer: file pointer returned by brfs_open_file
  761. * cursor: cursor position of opened file
  762. */
  763. word brfs_get_fat_idx_at_cursor(word file_pointer, word cursor)
  764. {
  765. if (file_pointer == 0)
  766. {
  767. uprintln("File not open!");
  768. return 0;
  769. }
  770. // Get FAT index of file at cursor
  771. word current_fat_idx = file_pointer;
  772. struct brfs_superblock* superblock = (struct brfs_superblock*) brfs_ram_storage;
  773. // Loop through FAT until cursor is reached
  774. while (cursor > superblock->bytes_per_block)
  775. {
  776. current_fat_idx = brfs_ram_storage[SUPERBLOCK_SIZE + current_fat_idx];
  777. if (current_fat_idx == -1)
  778. {
  779. uprintln("Cursor is out of bounds!");
  780. return 0;
  781. }
  782. cursor -= superblock->bytes_per_block;
  783. }
  784. return current_fat_idx;
  785. }
  786. /**
  787. * Read a file from the cursor position
  788. * Returns 1 on success, or 0 on error
  789. * file_pointer: file pointer returned by brfs_open_file
  790. * buffer: buffer to read the file into
  791. * length: number of words to read
  792. */
  793. word brfs_read(word file_pointer, word* buffer, word length)
  794. {
  795. if (file_pointer == 0)
  796. {
  797. uprintln("File not open!");
  798. return 0;
  799. }
  800. struct brfs_superblock* superblock = (struct brfs_superblock*) brfs_ram_storage;
  801. word* data_block_addr = brfs_ram_storage + SUPERBLOCK_SIZE + superblock->total_blocks;
  802. // Find file pointer
  803. word i;
  804. for (i = 0; i < MAX_OPEN_FILES; i++)
  805. {
  806. if (brfs_file_pointers[i] == file_pointer)
  807. {
  808. if (length < 0)
  809. {
  810. uprintln("Length cannot be negative!");
  811. return 0;
  812. }
  813. // Trunctate length to file size - cursor
  814. if (length > brfs_dir_entry_pointers[i]->filesize - brfs_cursors[i])
  815. {
  816. length = brfs_dir_entry_pointers[i]->filesize - brfs_cursors[i];
  817. }
  818. // Get FAT index of file at cursor
  819. word current_fat_idx = brfs_get_fat_idx_at_cursor(file_pointer, brfs_cursors[i]);
  820. if (current_fat_idx == 0)
  821. {
  822. uprintln("Error getting FAT index at cursor!");
  823. return 0;
  824. }
  825. // Loop:
  826. // - calculate words until end of block (or up to length)
  827. // - read words until end of block (or up to length)
  828. // - decrease length by words read
  829. // - get next block from FAT
  830. // - repeat until length is 0
  831. while (length > 0)
  832. {
  833. word words_until_end_of_block = superblock->bytes_per_block - (MATH_modU(brfs_cursors[i], superblock->bytes_per_block));
  834. word words_to_read = words_until_end_of_block > length ? length : words_until_end_of_block;
  835. // Copy words to buffer
  836. memcpy(buffer, data_block_addr + (current_fat_idx * superblock->bytes_per_block) + brfs_cursors[i], words_to_read);
  837. // Update cursor and length
  838. brfs_cursors[i] += words_to_read;
  839. length -= words_to_read;
  840. buffer += words_to_read;
  841. // Get next block from FAT
  842. current_fat_idx = brfs_ram_storage[SUPERBLOCK_SIZE + current_fat_idx];
  843. if (current_fat_idx == -1 && length > 0)
  844. {
  845. uprintln("There is no next block in the file!");
  846. return 0;
  847. }
  848. }
  849. return 1;
  850. }
  851. }
  852. uprintln("File not found!");
  853. return 0;
  854. }
  855. /**
  856. * Write a file from the cursor position
  857. * Returns 1 on success, or 0 on error
  858. * file_pointer: file pointer returned by brfs_open_file
  859. * buffer: buffer to write to the file
  860. * length: number of words to write
  861. */
  862. word brfs_write(word file_pointer, word* buffer, word length)
  863. {
  864. if (file_pointer == 0)
  865. {
  866. uprintln("File not open!");
  867. return 0;
  868. }
  869. struct brfs_superblock* superblock = (struct brfs_superblock*) brfs_ram_storage;
  870. word* data_block_addr = brfs_ram_storage + SUPERBLOCK_SIZE + superblock->total_blocks;
  871. // Find file pointer
  872. word i;
  873. for (i = 0; i < MAX_OPEN_FILES; i++)
  874. {
  875. if (brfs_file_pointers[i] == file_pointer)
  876. {
  877. if (length < 0)
  878. {
  879. uprintln("Length cannot be negative!");
  880. return 0;
  881. }
  882. // Get FAT index of file at cursor
  883. word current_fat_idx = brfs_get_fat_idx_at_cursor(file_pointer, brfs_cursors[i]);
  884. if (current_fat_idx == 0)
  885. {
  886. uprintln("Error getting FAT index at cursor!");
  887. return 0;
  888. }
  889. // Loop:
  890. // - calculate words until end of block (or up to length)
  891. // - write words until end of block (or up to length)
  892. // - decrease length by words written
  893. // - get next block from FAT, or find next free block if end of block
  894. // - if next block is needed, update FAT
  895. // - repeat until length is 0
  896. while (length > 0)
  897. {
  898. word cursor_in_block = MATH_modU(brfs_cursors[i], superblock->bytes_per_block);
  899. word words_until_end_of_block = superblock->bytes_per_block - cursor_in_block;
  900. word words_to_write = words_until_end_of_block > length ? length : words_until_end_of_block;
  901. // Copy words to buffer
  902. memcpy(data_block_addr + (current_fat_idx * superblock->bytes_per_block) + cursor_in_block, buffer, words_to_write);
  903. // Update cursor and length
  904. brfs_cursors[i] += words_to_write;
  905. length -= words_to_write;
  906. buffer += words_to_write;
  907. // Get next block from FAT, or find next free block if end of block
  908. if (words_until_end_of_block == words_to_write && length > 0)
  909. {
  910. word next_fat_idx = brfs_ram_storage[SUPERBLOCK_SIZE + current_fat_idx];
  911. // Check if next block is already allocated
  912. if (next_fat_idx != -1)
  913. {
  914. current_fat_idx = next_fat_idx;
  915. }
  916. else
  917. {
  918. // Find next free block
  919. word next_free_block = brfs_find_next_free_block(brfs_ram_storage + SUPERBLOCK_SIZE, superblock->total_blocks);
  920. if (next_free_block == -1)
  921. {
  922. uprintln("No free blocks left!");
  923. return 0;
  924. }
  925. // Update FAT
  926. brfs_ram_storage[SUPERBLOCK_SIZE + current_fat_idx] = next_free_block;
  927. // Go to next block
  928. current_fat_idx = next_free_block;
  929. // Set next block to -1 to indicate end of file
  930. brfs_ram_storage[SUPERBLOCK_SIZE + current_fat_idx] = -1;
  931. }
  932. }
  933. }
  934. // Update file size in dir entry if we wrote past the current size
  935. if (brfs_cursors[i] > brfs_dir_entry_pointers[i]->filesize)
  936. {
  937. brfs_dir_entry_pointers[i]->filesize = brfs_cursors[i];
  938. }
  939. return 1;
  940. }
  941. }
  942. uprintln("File not found!");
  943. return 0;
  944. }
  945. /**
  946. * Stat a file or directory
  947. * Returns the directory entry, or -1 on error
  948. */
  949. struct brfs_dir_entry* brfs_stat(char* file_path)
  950. {
  951. // Split filename from path using basename and dirname
  952. char dirname_output[MAX_PATH_LENGTH];
  953. char* file_path_basename = basename(file_path);
  954. char* file_path_dirname = dirname(dirname_output, file_path);
  955. // Find data block address of parent directory path
  956. word dir_fat_idx = brfs_get_fat_idx_of_dir(file_path_dirname);
  957. if (dir_fat_idx == -1)
  958. {
  959. uprint("Parent directory ");
  960. uprint(file_path_dirname);
  961. uprintln(" not found!");
  962. return (struct brfs_dir_entry*)-1;
  963. }
  964. // Find file in directory
  965. struct brfs_superblock* superblock = (struct brfs_superblock*) brfs_ram_storage;
  966. word* dir_addr = brfs_ram_storage + SUPERBLOCK_SIZE + superblock->total_blocks + (dir_fat_idx * superblock->bytes_per_block);
  967. word dir_entries_max = superblock->bytes_per_block / sizeof(struct brfs_dir_entry);
  968. word i;
  969. for (i = 0; i < dir_entries_max; i++)
  970. {
  971. struct brfs_dir_entry* dir_entry = (struct brfs_dir_entry*) (dir_addr + (i * sizeof(struct brfs_dir_entry)));
  972. if (dir_entry->filename[0] != 0)
  973. {
  974. char decompressed_filename[16];
  975. strdecompress(decompressed_filename, (char*)&(dir_entry->filename));
  976. // Also check for directory flag to be 0
  977. if (strcmp(decompressed_filename, file_path_basename) == 1)
  978. {
  979. return dir_entry;
  980. }
  981. }
  982. }
  983. uprint("File or directory ");
  984. uprint(file_path_basename);
  985. uprintln(" not found!");
  986. return (struct brfs_dir_entry*)-1;
  987. }
  988. int main()
  989. {
  990. // Clear UART screen:
  991. uprintc(0x1B);
  992. uprintc(0x5B);
  993. uprintc(0x32);
  994. uprintc(0x4A);
  995. uprintln("------------------------");
  996. uprintln("BRFS test implementation");
  997. uprintln("------------------------");
  998. word blocks = 16;
  999. word bytes_per_block = 32;
  1000. word full_format = 1;
  1001. brfs_format(blocks, bytes_per_block, "Label", full_format);
  1002. //brfs_list_directory("/");
  1003. //brfs_dump(blocks, blocks*bytes_per_block);
  1004. //uprintln("");
  1005. // Create directories
  1006. if (!brfs_create_directory("/", "dir1"))
  1007. {
  1008. uprintln("Error creating dir1!");
  1009. }
  1010. if (!brfs_create_directory("/", "dir2"))
  1011. {
  1012. uprintln("Error creating dir2!");
  1013. }
  1014. // Create files
  1015. if (!brfs_create_file("/dir1", "file1.txt"))
  1016. {
  1017. uprintln("Error creating file1!");
  1018. }
  1019. if (!brfs_create_file("/dir1", "file2.txt"))
  1020. {
  1021. uprintln("Error creating file2!");
  1022. }
  1023. // Open file and write
  1024. word file_pointer = brfs_open_file("/dir1/file1.txt");
  1025. if (file_pointer == -1)
  1026. {
  1027. uprintln("Error opening file1!");
  1028. }
  1029. else
  1030. {
  1031. char* write_string = "This message should exceed the length of a single block, it even should exceed the length of two blocks! I am adding this part here to keep increasing the number of blocks used. This is the end of the message.";
  1032. if (!brfs_write(file_pointer, write_string, strlen(write_string)))
  1033. {
  1034. uprintln("Error writing to file1!");
  1035. }
  1036. // Update two blocks in the middle of the file
  1037. brfs_set_cursor(file_pointer, 57);
  1038. char* write_string2 = "THIS PART IS WRITTEN IN THE MIDDLE OF THE FILE!";
  1039. if (!brfs_write(file_pointer, write_string2, strlen(write_string2)))
  1040. {
  1041. uprintln("Error writing to file1!");
  1042. }
  1043. brfs_close_file(file_pointer);
  1044. }
  1045. // Open second file and write
  1046. word file_pointer2 = brfs_open_file("/dir1/file2.txt");
  1047. if (file_pointer2 == -1)
  1048. {
  1049. uprintln("Error opening file2!");
  1050. }
  1051. else
  1052. {
  1053. char* write_string = "Small message in file2!";
  1054. if (!brfs_write(file_pointer2, write_string, strlen(write_string)))
  1055. {
  1056. uprintln("Error writing to file2!");
  1057. }
  1058. // Update within the first block
  1059. brfs_set_cursor(file_pointer2, 6);
  1060. char* write_string2 = "UPDATES";
  1061. if (!brfs_write(file_pointer2, write_string2, strlen(write_string2)))
  1062. {
  1063. uprintln("Error writing to file2!");
  1064. }
  1065. // Skip closing the file to see data in dump
  1066. //brfs_close_file(file_pointer2);
  1067. }
  1068. // Delete file1
  1069. if (!brfs_delete_file("/dir1/file1.txt"))
  1070. {
  1071. uprintln("Error deleting file1!");
  1072. }
  1073. brfs_list_directory("/");
  1074. brfs_list_directory("/dir1");
  1075. brfs_dump(blocks, blocks*bytes_per_block);
  1076. return 'q';
  1077. }
  1078. void interrupt()
  1079. {
  1080. // handle all interrupts
  1081. word i = getIntID();
  1082. switch(i)
  1083. {
  1084. case INTID_TIMER1:
  1085. timer1Value = 1; // notify ending of timer1
  1086. break;
  1087. default:
  1088. break;
  1089. }
  1090. }