BRFS.C 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  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. - [] Open file (not a dir!) (allow multiple files open at once)
  49. - [] Close file (update dir entry and check/update all FAT entries)
  50. - [] Stat (returns dir entry)
  51. - [] Set cursor
  52. - [] 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. // Length of structs, should not be changed
  65. #define SUPERBLOCK_SIZE 16
  66. #define DIR_ENTRY_SIZE 8
  67. word *brfs_ram_storage = (word*) BRFS_RAM_STORAGE_ADDR; // RAM storage of file system
  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. /**
  87. * Create a hexdump like dump of a section of memory
  88. * addr: address of the section
  89. * len: length of the section in words
  90. * linesize: number of words per line to print
  91. */
  92. void brfs_dump_section(word* addr, word len, word linesize)
  93. {
  94. char buf[16];
  95. word i;
  96. for (i = 0; i < len; i++)
  97. {
  98. itoah(addr[i], buf);
  99. if (strlen(buf+2) == 1)
  100. uprintc('0');
  101. uprint(buf+2);
  102. uprintc(' ');
  103. // newline every linesize words
  104. // also print last linesize words as chars if alphanum
  105. if (i != 0 && MATH_modU(i+1, linesize) == 0)
  106. {
  107. uprint(" ");
  108. word j;
  109. for (j = i - (linesize-1); j < i+1; j++)
  110. {
  111. if (isalnum(addr[j]) || addr[j] == ' ')
  112. uprintc(addr[j]);
  113. else
  114. uprintc('.');
  115. }
  116. uprintc('\n');
  117. }
  118. }
  119. }
  120. /**
  121. * Create a raw filesystem dump over UART
  122. * fatsize: size of the FAT table in words
  123. * datasize: size of the data section in words
  124. */
  125. void brfs_dump(word fatsize, word datasize)
  126. {
  127. // Superblock dump
  128. uprintln("Superblock:");
  129. brfs_dump_section(brfs_ram_storage, SUPERBLOCK_SIZE, 16);
  130. // FAT dump
  131. uprintln("\nFAT:");
  132. brfs_dump_section(brfs_ram_storage+SUPERBLOCK_SIZE, fatsize, 8);
  133. // Datablock dump
  134. uprintln("\nData:");
  135. brfs_dump_section(brfs_ram_storage+SUPERBLOCK_SIZE+fatsize, datasize, 32);
  136. uprintc('\n');
  137. }
  138. /**
  139. * Return the FAT index of a directory, or -1 if not found
  140. * dir_path: full path of the directory
  141. */
  142. word brfs_get_fat_idx_of_dir(char* dir_path)
  143. {
  144. // Check length of path
  145. if (strlen(dir_path) > MAX_PATH_LENGTH)
  146. {
  147. uprintln("Path too long!");
  148. return -1;
  149. }
  150. // Start with root directory
  151. word current_dir_fat_idx = 0;
  152. // Check if root directory is requested
  153. if (strcmp(dir_path, "/") == 1)
  154. {
  155. return current_dir_fat_idx;
  156. }
  157. // Copy dir_path, size + 1 for null terminator
  158. // Since strtok modifies the string
  159. char dir_path_copy[MAX_PATH_LENGTH+1];
  160. strcpy(dir_path_copy, dir_path);
  161. struct brfs_superblock* superblock = (struct brfs_superblock*) brfs_ram_storage;
  162. word* brfs_data_block_addr = brfs_ram_storage + SUPERBLOCK_SIZE + superblock->total_blocks;
  163. word dir_entries_max = superblock->bytes_per_block / sizeof(struct brfs_dir_entry);
  164. // Split path by '/' and traverse directories
  165. char* token = strtok(dir_path_copy, "/");
  166. while (token != (word*)-1)
  167. {
  168. // Find token in current directory
  169. word* dir_addr = brfs_data_block_addr + (current_dir_fat_idx * superblock->bytes_per_block);
  170. word found_dir = 0; // Keep track if token is found in current directory
  171. word i;
  172. for (i = 0; i < dir_entries_max; i++)
  173. {
  174. struct brfs_dir_entry* dir_entry = (struct brfs_dir_entry*) (dir_addr + (i * sizeof(struct brfs_dir_entry)));
  175. if (dir_entry->filename[0] != 0)
  176. {
  177. char decompressed_filename[16];
  178. strdecompress(decompressed_filename, (char*)&(dir_entry->filename));
  179. if (strcmp(decompressed_filename, token) == 1)
  180. {
  181. // Found token in current directory
  182. // Set current directory to token's FAT index
  183. current_dir_fat_idx = dir_entry->fat_idx;
  184. found_dir = 1;
  185. break;
  186. }
  187. }
  188. }
  189. // If token not found in current directory, return -1
  190. if (!found_dir)
  191. {
  192. uprint("Directory ");
  193. uprint(dir_path);
  194. uprintln(" not found!");
  195. return -1;
  196. }
  197. token = strtok((word*)-1, "/");
  198. }
  199. return current_dir_fat_idx;
  200. }
  201. /**
  202. * Given the address of the FAT table and the number of blocks, find the next free block
  203. * Returns -1 if no free block is found
  204. * fat_addr: address of the FAT table
  205. * blocks: number of blocks in the FAT table
  206. */
  207. word brfs_find_next_free_block(word* fat_addr, word blocks)
  208. {
  209. word i = 0;
  210. word* fat_ptr = fat_addr;
  211. while (i < blocks)
  212. {
  213. if (*fat_ptr == 0)
  214. {
  215. return i;
  216. }
  217. fat_ptr++;
  218. i++;
  219. }
  220. return -1;
  221. }
  222. /**
  223. * Given the address of a directory data block and the maximum number of entries, find the next free directory entry
  224. * Returns -1 if no free entry is found
  225. * dir_addr: address of the directory data block (not the FAT idx)
  226. * dir_entries_max: maximum number of entries in the directory
  227. */
  228. word brfs_find_next_free_dir_entry(word* dir_addr, word dir_entries_max)
  229. {
  230. word i = 0;
  231. word* dir_ptr = dir_addr;
  232. while (i < dir_entries_max)
  233. {
  234. if (*dir_ptr == 0)
  235. {
  236. return i;
  237. }
  238. dir_ptr += sizeof(struct brfs_dir_entry);
  239. i++;
  240. }
  241. return -1;
  242. }
  243. /**
  244. * Create a single directory entry
  245. * dir_entry: pointer to the directory entry to be created
  246. * filename: name of the file, max 16 chars and uncompressed
  247. * fat_idx: index of the first FAT block of the file/directory
  248. * filesize: size of the file in words
  249. * flags: flags of the file/directory
  250. */
  251. void brfs_create_single_dir_entry(struct brfs_dir_entry* dir_entry, char* filename, word fat_idx, word filesize, word flags)
  252. {
  253. // Initialize to 0
  254. memset((char*)dir_entry, 0, sizeof(*dir_entry));
  255. // Set filename
  256. char compressed_filename[4] = {0,0,0,0};
  257. strcompress(compressed_filename, filename);
  258. memcpy((char*)&(dir_entry->filename), compressed_filename, sizeof(compressed_filename));
  259. // Set other fields
  260. dir_entry->fat_idx = fat_idx;
  261. dir_entry->flags = flags;
  262. dir_entry->filesize = filesize;
  263. }
  264. /**
  265. * Initialize a directory with . and .. entries
  266. * dir_addr: address of the directory data block
  267. * dir_entries_max: maximum number of entries in the directory
  268. * dir_fat_idx: index of the FAT block of the directory
  269. * parent_fat_idx: index of the FAT block of the parent directory
  270. */
  271. void brfs_init_directory(word* dir_addr, word dir_entries_max, word dir_fat_idx, word parent_fat_idx)
  272. {
  273. // Create . entry
  274. struct brfs_dir_entry dir_entry;
  275. brfs_create_single_dir_entry(&dir_entry, ".", dir_fat_idx, dir_entries_max*sizeof(struct brfs_dir_entry), 1);
  276. // Copy to first data entry
  277. memcpy(dir_addr, (char*)&dir_entry, sizeof(dir_entry));
  278. // Create .. entry
  279. brfs_create_single_dir_entry(&dir_entry, "..", parent_fat_idx, dir_entries_max*sizeof(struct brfs_dir_entry), 1);
  280. // Copy to second data entry
  281. memcpy(dir_addr+sizeof(dir_entry), (char*)&dir_entry, sizeof(dir_entry));
  282. // Set FAT table
  283. brfs_ram_storage[SUPERBLOCK_SIZE + dir_fat_idx] = -1;
  284. }
  285. /**
  286. * Format the ram storage as a BRFS filesystem
  287. * blocks: number of blocks in the filesystem
  288. * bytes_per_block: number of bytes per block
  289. * label: label of the filesystem
  290. * full_format: if 1, initialize data section to 0
  291. */
  292. void brfs_format(word blocks, word bytes_per_block, char* label, word full_format)
  293. {
  294. // Create a superblock
  295. struct brfs_superblock superblock;
  296. // Initialize to 0
  297. memset((char*)&superblock, 0, sizeof(superblock));
  298. // Set values of superblock
  299. superblock.total_blocks = blocks;
  300. superblock.bytes_per_block = bytes_per_block;
  301. strcpy((char*)&superblock.label, label);
  302. superblock.brfs_version = 1;
  303. // Copy superblock to head of ram addr
  304. memcpy(brfs_ram_storage, (char*)&superblock, sizeof(superblock));
  305. // Create FAT
  306. memset(brfs_ram_storage + SUPERBLOCK_SIZE, 0, blocks);
  307. // Create Data section
  308. if (full_format)
  309. {
  310. memset(brfs_ram_storage + SUPERBLOCK_SIZE + blocks, 0, blocks * bytes_per_block);
  311. }
  312. // Initialize root dir
  313. word dir_entries_max = bytes_per_block / sizeof(struct brfs_dir_entry);
  314. brfs_init_directory(brfs_ram_storage + SUPERBLOCK_SIZE + blocks, dir_entries_max, 0, 0);
  315. }
  316. /**
  317. * Create a new directory in the directory of parent_dir_path
  318. * parent_dir_path: full path of the parent directory
  319. * dirname: name of the new directory
  320. */
  321. void brfs_create_directory(char* parent_dir_path, char* dirname)
  322. {
  323. struct brfs_superblock* superblock = (struct brfs_superblock*) brfs_ram_storage;
  324. word* brfs_data_block_addr = brfs_ram_storage + SUPERBLOCK_SIZE + superblock->total_blocks;
  325. // Find first free FAT block
  326. word next_free_block = brfs_find_next_free_block(brfs_ram_storage + SUPERBLOCK_SIZE, superblock->total_blocks);
  327. if (next_free_block == -1)
  328. {
  329. uprintln("No free blocks left!");
  330. return;
  331. }
  332. // Find data block address of parent directory path
  333. word parent_dir_fat_idx = brfs_get_fat_idx_of_dir(parent_dir_path);
  334. if (parent_dir_fat_idx == -1)
  335. {
  336. uprint("Parent directory ");
  337. uprint(parent_dir_path);
  338. uprintln(" not found!");
  339. return;
  340. }
  341. // Find first free dir entry
  342. word next_free_dir_entry = brfs_find_next_free_dir_entry(
  343. brfs_data_block_addr + (parent_dir_fat_idx * superblock->bytes_per_block),
  344. superblock->bytes_per_block / sizeof(struct brfs_dir_entry)
  345. );
  346. if (next_free_dir_entry == -1)
  347. {
  348. uprintln("No free dir entries left!");
  349. return;
  350. }
  351. // Create dir entry
  352. struct brfs_dir_entry new_entry;
  353. brfs_create_single_dir_entry(&new_entry, dirname, next_free_block, 0, 1);
  354. // Copy dir entry to first free dir entry
  355. memcpy(
  356. brfs_data_block_addr + (parent_dir_fat_idx * superblock->bytes_per_block) + (next_free_dir_entry * sizeof(struct brfs_dir_entry)),
  357. (char*)&new_entry,
  358. sizeof(new_entry)
  359. );
  360. // Initialize directory
  361. word dir_entries_max = superblock->bytes_per_block / sizeof(struct brfs_dir_entry);
  362. brfs_init_directory(
  363. brfs_data_block_addr + (next_free_block * superblock->bytes_per_block),
  364. dir_entries_max,
  365. next_free_block,
  366. parent_dir_fat_idx
  367. );
  368. }
  369. /**
  370. * Create a new file in the directory of parent_dir_path
  371. * parent_dir_path: full path of the parent directory
  372. * filename: name of the new file
  373. */
  374. void brfs_create_file(char* parent_dir_path, char* filename)
  375. {
  376. struct brfs_superblock* superblock = (struct brfs_superblock*) brfs_ram_storage;
  377. word* brfs_data_block_addr = brfs_ram_storage + SUPERBLOCK_SIZE + superblock->total_blocks;
  378. // Find first free FAT block
  379. word next_free_block = brfs_find_next_free_block(brfs_ram_storage + SUPERBLOCK_SIZE, superblock->total_blocks);
  380. if (next_free_block == -1)
  381. {
  382. uprintln("No free blocks left!");
  383. return;
  384. }
  385. // Find data block address of parent directory path
  386. word parent_dir_fat_idx = brfs_get_fat_idx_of_dir(parent_dir_path);
  387. if (parent_dir_fat_idx == -1)
  388. {
  389. uprint("Parent directory ");
  390. uprint(parent_dir_path);
  391. uprintln(" not found!");
  392. return;
  393. }
  394. // Find first free dir entry
  395. word next_free_dir_entry = brfs_find_next_free_dir_entry(
  396. brfs_data_block_addr + (parent_dir_fat_idx * superblock->bytes_per_block),
  397. superblock->bytes_per_block / sizeof(struct brfs_dir_entry)
  398. );
  399. if (next_free_dir_entry == -1)
  400. {
  401. uprintln("No free dir entries left!");
  402. return;
  403. }
  404. // Create file entry
  405. struct brfs_dir_entry new_entry;
  406. brfs_create_single_dir_entry(&new_entry, filename, next_free_block, 0, 0);
  407. // Copy dir entry to first free dir entry
  408. memcpy(
  409. brfs_data_block_addr + (parent_dir_fat_idx * superblock->bytes_per_block) + (next_free_dir_entry * sizeof(struct brfs_dir_entry)),
  410. (char*)&new_entry,
  411. sizeof(new_entry)
  412. );
  413. // Initialize file by setting data to 0
  414. memset(
  415. brfs_data_block_addr + (next_free_block * superblock->bytes_per_block),
  416. 0,
  417. superblock->bytes_per_block
  418. );
  419. // Update FAT
  420. brfs_ram_storage[SUPERBLOCK_SIZE + next_free_block] = -1;
  421. }
  422. /**
  423. * List the contents of a directory over UART
  424. * dir_path: full path of the directory
  425. */
  426. void brfs_list_directory(char* dir_path)
  427. {
  428. uprint("Listing directory ");
  429. uprintln(dir_path);
  430. uprintln("-------------------");
  431. // Find data block address of parent directory path
  432. word dir_fat_idx = brfs_get_fat_idx_of_dir(dir_path);
  433. if (dir_fat_idx == -1)
  434. {
  435. uprint("Parent directory ");
  436. uprint(dir_path);
  437. uprintln(" not found!");
  438. return;
  439. }
  440. struct brfs_superblock* superblock = (struct brfs_superblock*) brfs_ram_storage;
  441. word* dir_addr = brfs_ram_storage + SUPERBLOCK_SIZE + superblock->total_blocks + (dir_fat_idx * superblock->bytes_per_block);
  442. word dir_entries_max = superblock->bytes_per_block / sizeof(struct brfs_dir_entry);
  443. word i;
  444. for (i = 0; i < dir_entries_max; i++)
  445. {
  446. struct brfs_dir_entry* dir_entry = (struct brfs_dir_entry*) (dir_addr + (i * sizeof(struct brfs_dir_entry)));
  447. if (dir_entry->filename[0] != 0)
  448. {
  449. uprint("Filename: ");
  450. char decompressed_filename[16];
  451. strdecompress(decompressed_filename, (char*)&(dir_entry->filename));
  452. uprint(decompressed_filename);
  453. uprint(" FAT idx: ");
  454. uprintDec((dir_entry->fat_idx));
  455. uprint(" Flags: ");
  456. uprintDec((dir_entry->flags));
  457. uprint(" Filesize: ");
  458. uprintDec((dir_entry->filesize));
  459. uprintc('\n');
  460. }
  461. }
  462. uprintln("");
  463. }
  464. int main()
  465. {
  466. // Clear UART screen:
  467. uprintc(0x1B);
  468. uprintc(0x5B);
  469. uprintc(0x32);
  470. uprintc(0x4A);
  471. uprintln("------------------------");
  472. uprintln("BRFS test implementation");
  473. uprintln("------------------------");
  474. word blocks = 8;
  475. word bytes_per_block = 32;
  476. word full_format = 1;
  477. brfs_format(blocks, bytes_per_block, "Label", full_format);
  478. brfs_dump(blocks, blocks*bytes_per_block);
  479. brfs_list_directory("/");
  480. brfs_create_file("/", "file1.txt");
  481. brfs_list_directory(".");
  482. brfs_create_directory("..", "dir1");
  483. brfs_create_file("dir1", "file2.txt");
  484. brfs_list_directory(".");
  485. brfs_list_directory("dir1");
  486. return 'q';
  487. }
  488. void interrupt()
  489. {
  490. // handle all interrupts
  491. word i = getIntID();
  492. switch(i)
  493. {
  494. case INTID_TIMER1:
  495. timer1Value = 1; // notify ending of timer1
  496. break;
  497. default:
  498. break;
  499. }
  500. }