1
0

brfs.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406
  1. // Bart's RAM File System (BRFS)
  2. /*
  3. Implementing in BDOS on PCB v3:
  4. - BDOS only uses ~340 pages of 256 bytes -> 90000 bytes
  5. - SPI flash has 65536 pages of 256 bytes -> 16777216 bytes
  6. - With current verilog implementation, only 32MiB of RAM is addressable, so BRFS should be used somewhere in the first 32MiB
  7. - With current BDOS memory map, BRFS should be placed in the first 8MiB available as BDOS Program Code
  8. - Lets use the last 4MiB of this space for BRFS (0x100000 - 0x200000)
  9. */
  10. #define BRFS_SUPPORTED_VERSION 1
  11. #define BRFS_RAM_STORAGE_ADDR 0x100000 // From 4th MiB
  12. // Addresses in SPI Flash
  13. #define SPIFLASH_MEMMAP_ADDR 0x800000
  14. // Note that each section should be in a different 4KiB sector in SPI Flash
  15. #define BRFS_SPIFLASH_SUPERBLOCK_ADDR 0xDF000 // One sector before FAT
  16. #define BRFS_SPIFLASH_FAT_ADDR 0xE0000 // Can be 32768 words (128KiB) for 32MiB of 256word blocks
  17. #define BRFS_SPIFLASH_BLOCK_ADDR 0x100000 // From first MiB
  18. //#define MAX_PATH_LENGTH 127 // Set by BDOS
  19. #define MAX_OPEN_FILES 16 // Can be set higher, but 4 is good for testing
  20. // Length of structs, should not be changed
  21. #define SUPERBLOCK_SIZE 16
  22. #define DIR_ENTRY_SIZE 8
  23. #define BRFS_MAX_BLOCKS 65536 // 64KiB
  24. word brfs_changed_blocks[BRFS_MAX_BLOCKS >> 5]; // Bitmap of changed blocks, each block has 1 bit
  25. // 16 words long
  26. struct brfs_superblock
  27. {
  28. word total_blocks;
  29. word words_per_block;
  30. word label[10]; // 1 char per word
  31. word brfs_version;
  32. word reserved[3];
  33. };
  34. // 8 words long
  35. struct brfs_dir_entry
  36. {
  37. word filename[4]; // 4 chars per word
  38. word modify_date; // TBD when RTC added to FPGC
  39. word flags; // 32 flags, from right to left: directory, hidden
  40. word fat_idx; // idx of first FAT block
  41. word filesize; // file size in words, not bytes
  42. };
  43. word *brfs_ram_storage = (word*) BRFS_RAM_STORAGE_ADDR; // RAM storage of file system
  44. // Variables for open files
  45. word brfs_cursors[MAX_OPEN_FILES]; // Cursor position offset from start of file
  46. word brfs_file_pointers[MAX_OPEN_FILES]; // FAT idx of open file
  47. struct brfs_dir_entry* brfs_dir_entry_pointers[MAX_OPEN_FILES]; // Pointer to dir entry of open file
  48. /**
  49. * Create a hexdump like dump of a section of memory
  50. * addr: address of the section
  51. * len: length of the section in words
  52. * linesize: number of words per line to print
  53. */
  54. void brfs_dump_section(word* addr, word len, word linesize)
  55. {
  56. char buf[16];
  57. word i;
  58. for (i = 0; i < len; i++)
  59. {
  60. itoah(addr[i], buf);
  61. if (strlen(buf+2) == 1)
  62. uprintc('0');
  63. uprint(buf+2);
  64. uprintc(' ');
  65. // newline every linesize words
  66. // also print last linesize words as chars if alphanum
  67. if (i != 0 && MATH_modU(i+1, linesize) == 0)
  68. {
  69. uprint(" ");
  70. word j;
  71. for (j = i - (linesize-1); j < i+1; j++)
  72. {
  73. if (isalnum(addr[j]) || addr[j] == ' ')
  74. uprintc(addr[j]);
  75. else
  76. uprintc('.');
  77. }
  78. uprintc('\n');
  79. }
  80. }
  81. }
  82. /**
  83. * Create a raw filesystem dump over UART
  84. * fatsize: size of the FAT table in words
  85. * datasize: size of the data section in words
  86. */
  87. void brfs_dump(word fatsize, word datasize)
  88. {
  89. // Superblock dump
  90. uprintln("Superblock:");
  91. brfs_dump_section(brfs_ram_storage, SUPERBLOCK_SIZE, 16);
  92. // FAT dump
  93. uprintln("\nFAT:");
  94. brfs_dump_section(brfs_ram_storage+SUPERBLOCK_SIZE, fatsize, 16);
  95. // Datablock dump
  96. uprintln("\nData:");
  97. brfs_dump_section(brfs_ram_storage+SUPERBLOCK_SIZE+fatsize, datasize, 32);
  98. uprintln("\nOpen files:");
  99. word i;
  100. for (i = 0; i < MAX_OPEN_FILES; i++)
  101. {
  102. uprint("FP");
  103. uprintDec(i+1);
  104. uprint(":");
  105. uprint(" FAT idx: ");
  106. uprintDec(brfs_file_pointers[i]);
  107. uprint(" Cursor: ");
  108. uprintDec(brfs_cursors[i]);
  109. uprint(" Size: ");
  110. uprintDec(brfs_dir_entry_pointers[i] ? brfs_dir_entry_pointers[i]->filesize : 0);
  111. uprintc('\n');
  112. }
  113. }
  114. /**
  115. * Return the FAT index of a directory, or -1 if not found
  116. * dir_path: full path of the directory
  117. */
  118. word brfs_get_fat_idx_of_dir(char* dir_path)
  119. {
  120. // Check length of path
  121. if (strlen(dir_path) > MAX_PATH_LENGTH)
  122. {
  123. uprintln("Path too long!");
  124. return -1;
  125. }
  126. // Start with root directory
  127. word current_dir_fat_idx = 0;
  128. // Check if root directory is requested
  129. if (strcmp(dir_path, "/") == 0)
  130. {
  131. return current_dir_fat_idx;
  132. }
  133. // Copy dir_path, size + 1 for null terminator
  134. // Since strtok modifies the string
  135. char dir_path_copy[MAX_PATH_LENGTH+1];
  136. strcpy(dir_path_copy, dir_path);
  137. struct brfs_superblock* superblock = (struct brfs_superblock*) brfs_ram_storage;
  138. word* brfs_data_block_addr = brfs_ram_storage + SUPERBLOCK_SIZE + superblock->total_blocks;
  139. word dir_entries_max = superblock->words_per_block / sizeof(struct brfs_dir_entry);
  140. // Split path by '/' and traverse directories
  141. char* token = strtok(dir_path_copy, "/");
  142. while (token != (word*)-1)
  143. {
  144. // Find token in current directory
  145. word* dir_addr = brfs_data_block_addr + (current_dir_fat_idx * superblock->words_per_block);
  146. word found_dir = 0; // Keep track if token is found in current directory
  147. word i;
  148. for (i = 0; i < dir_entries_max; i++)
  149. {
  150. struct brfs_dir_entry* dir_entry = (struct brfs_dir_entry*) (dir_addr + (i * sizeof(struct brfs_dir_entry)));
  151. if (dir_entry->filename[0] != 0)
  152. {
  153. char decompressed_filename[17];
  154. strdecompress(decompressed_filename, (char*)&(dir_entry->filename));
  155. // Also check for directory flag
  156. if (strcmp(decompressed_filename, token) == 0 && dir_entry->flags == 1)
  157. {
  158. // Found token in current directory
  159. // Set current directory to token's FAT index
  160. current_dir_fat_idx = dir_entry->fat_idx;
  161. found_dir = 1;
  162. break;
  163. }
  164. }
  165. }
  166. // If token not found in current directory, return -1
  167. if (!found_dir)
  168. {
  169. uprint("Directory ");
  170. uprint(dir_path);
  171. uprintln(" not found!");
  172. return -1;
  173. }
  174. token = strtok((word*)-1, "/");
  175. }
  176. return current_dir_fat_idx;
  177. }
  178. /**
  179. * Given the address of the FAT table and the number of blocks, find the next free block
  180. * Returns -1 if no free block is found
  181. * fat_addr: address of the FAT table
  182. * blocks: number of blocks in the FAT table
  183. */
  184. word brfs_find_next_free_block(word* fat_addr, word blocks)
  185. {
  186. word i = 0;
  187. word* fat_ptr = fat_addr;
  188. while (i < blocks)
  189. {
  190. if (*fat_ptr == 0)
  191. {
  192. return i;
  193. }
  194. fat_ptr++;
  195. i++;
  196. }
  197. return -1;
  198. }
  199. /**
  200. * Given the address of a directory data block and the maximum number of entries, find the next free directory entry
  201. * Returns -1 if no free entry is found
  202. * dir_addr: address of the directory data block (not the FAT idx)
  203. * dir_entries_max: maximum number of entries in the directory
  204. */
  205. word brfs_find_next_free_dir_entry(word* dir_addr, word dir_entries_max)
  206. {
  207. word i = 0;
  208. word* dir_ptr = dir_addr;
  209. while (i < dir_entries_max)
  210. {
  211. if (*dir_ptr == 0)
  212. {
  213. return i;
  214. }
  215. dir_ptr += sizeof(struct brfs_dir_entry);
  216. i++;
  217. }
  218. return -1;
  219. }
  220. /**
  221. * Create a single directory entry
  222. * dir_entry: pointer to the directory entry to be created
  223. * filename: name of the file, max 16 chars and uncompressed
  224. * fat_idx: index of the first FAT block of the file/directory
  225. * filesize: size of the file in words
  226. * flags: flags of the file/directory
  227. */
  228. void brfs_create_single_dir_entry(struct brfs_dir_entry* dir_entry, char* filename, word fat_idx, word filesize, word flags)
  229. {
  230. // Initialize to 0
  231. memset((char*)dir_entry, 0, sizeof(*dir_entry));
  232. // Set filename
  233. char compressed_filename[4] = {0,0,0,0};
  234. strcompress(compressed_filename, filename);
  235. memcpy((char*)&(dir_entry->filename), compressed_filename, sizeof(compressed_filename));
  236. // Set other fields
  237. dir_entry->fat_idx = fat_idx;
  238. dir_entry->flags = flags;
  239. dir_entry->filesize = filesize;
  240. }
  241. /**
  242. * Initialize a directory with . and .. entries
  243. * dir_addr: address of the directory data block
  244. * dir_entries_max: maximum number of entries in the directory
  245. * dir_fat_idx: index of the FAT block of the directory
  246. * parent_fat_idx: index of the FAT block of the parent directory
  247. */
  248. void brfs_init_directory(word* dir_addr, word dir_entries_max, word dir_fat_idx, word parent_fat_idx)
  249. {
  250. // Get block size from superblock
  251. struct brfs_superblock* superblock = (struct brfs_superblock*) brfs_ram_storage;
  252. word block_size = superblock->words_per_block;
  253. // Set data block of dir_fat_idx to 0
  254. memset(dir_addr, 0, block_size);
  255. // Create . entry
  256. struct brfs_dir_entry dir_entry;
  257. brfs_create_single_dir_entry(&dir_entry, ".", dir_fat_idx, dir_entries_max*sizeof(struct brfs_dir_entry), 1);
  258. // Copy to first data entry
  259. memcpy(dir_addr, (char*)&dir_entry, sizeof(dir_entry));
  260. // Create .. entry
  261. brfs_create_single_dir_entry(&dir_entry, "..", parent_fat_idx, dir_entries_max*sizeof(struct brfs_dir_entry), 1);
  262. // Copy to second data entry
  263. memcpy(dir_addr+sizeof(dir_entry), (char*)&dir_entry, sizeof(dir_entry));
  264. // Set FAT table
  265. brfs_ram_storage[SUPERBLOCK_SIZE + dir_fat_idx] = -1;
  266. // Set changed block
  267. brfs_changed_blocks[dir_fat_idx >> 5] |= (1 << (dir_fat_idx & 31));
  268. }
  269. /**
  270. * Format the ram storage as a BRFS filesystem
  271. * Also writes the superblock to SPI Flash
  272. * blocks: number of blocks in the filesystem
  273. * words_per_block: number of bytes per block
  274. * label: label of the filesystem
  275. * full_format: if 1, initialize data section to 0
  276. */
  277. void brfs_format(word blocks, word words_per_block, char* label, word full_format)
  278. {
  279. // Create a superblock
  280. struct brfs_superblock superblock;
  281. // Initialize to 0
  282. memset((char*)&superblock, 0, sizeof(superblock));
  283. // Set values of superblock
  284. superblock.total_blocks = blocks;
  285. superblock.words_per_block = words_per_block;
  286. strcpy((char*)&superblock.label, label);
  287. superblock.brfs_version = BRFS_SUPPORTED_VERSION;
  288. // Copy superblock to head of ram addr
  289. memcpy(brfs_ram_storage, (char*)&superblock, sizeof(superblock));
  290. // Create FAT
  291. memset(brfs_ram_storage + SUPERBLOCK_SIZE, 0, blocks);
  292. // Create Data section
  293. if (full_format)
  294. {
  295. memset(brfs_ram_storage + SUPERBLOCK_SIZE + blocks, 0, blocks * words_per_block);
  296. }
  297. // Initialize root dir
  298. word dir_entries_max = words_per_block / sizeof(struct brfs_dir_entry);
  299. brfs_init_directory(brfs_ram_storage + SUPERBLOCK_SIZE + blocks, dir_entries_max, 0, 0);
  300. // Clear open files and cursors
  301. memset(brfs_file_pointers, 0, sizeof(brfs_file_pointers));
  302. memset(brfs_cursors, 0, sizeof(brfs_cursors));
  303. // Set all dir entry pointers to 0
  304. word i;
  305. for (i = 0; i < MAX_OPEN_FILES; i++)
  306. {
  307. brfs_dir_entry_pointers[i] = 0;
  308. }
  309. // For all blocks that have just been formatted, set changed block
  310. word j;
  311. for (j = 0; j < blocks; j++)
  312. {
  313. brfs_changed_blocks[j >> 5] |= (1 << (j & 31));
  314. }
  315. // Write superblock to SPI Flash
  316. spiflash_sector_erase(BRFS_SPIFLASH_SUPERBLOCK_ADDR);
  317. spiflash_write_page_in_words((char*)&superblock, BRFS_SPIFLASH_SUPERBLOCK_ADDR, sizeof(superblock));
  318. }
  319. /**
  320. * Create a new directory in the directory of parent_dir_path
  321. * Returns 1 on success, 0 on error
  322. * parent_dir_path: full path of the parent directory
  323. * dirname: name of the new directory
  324. */
  325. word brfs_create_directory(char* parent_dir_path, char* dirname)
  326. {
  327. // Check length of dirname
  328. if (strlen(dirname) >= 16)
  329. {
  330. uprintln("Directory name too long!");
  331. return 0;
  332. }
  333. struct brfs_superblock* superblock = (struct brfs_superblock*) brfs_ram_storage;
  334. word* brfs_data_block_addr = brfs_ram_storage + SUPERBLOCK_SIZE + superblock->total_blocks;
  335. // Find first free FAT block
  336. word next_free_block = brfs_find_next_free_block(brfs_ram_storage + SUPERBLOCK_SIZE, superblock->total_blocks);
  337. if (next_free_block == -1)
  338. {
  339. uprintln("No free blocks left!");
  340. return 0;
  341. }
  342. // Find data block address of parent directory path
  343. word parent_dir_fat_idx = brfs_get_fat_idx_of_dir(parent_dir_path);
  344. if (parent_dir_fat_idx == -1)
  345. {
  346. uprint("Parent directory ");
  347. uprint(parent_dir_path);
  348. uprintln(" not found!");
  349. return 0;
  350. }
  351. // Check if file or folder already exists
  352. word* parent_dir_addr = brfs_data_block_addr + (parent_dir_fat_idx * superblock->words_per_block);
  353. word dir_entries_max = superblock->words_per_block / sizeof(struct brfs_dir_entry);
  354. word i;
  355. for (i = 0; i < dir_entries_max; i++)
  356. {
  357. struct brfs_dir_entry* dir_entry = (struct brfs_dir_entry*) (parent_dir_addr + (i * sizeof(struct brfs_dir_entry)));
  358. if (dir_entry->filename[0] != 0)
  359. {
  360. char decompressed_filename[17];
  361. strdecompress(decompressed_filename, (char*)&(dir_entry->filename));
  362. if (strcmp(decompressed_filename, dirname) == 0)
  363. {
  364. uprint(dirname);
  365. uprintln(" already exists!");
  366. return 0;
  367. }
  368. }
  369. }
  370. // Find first free dir entry
  371. word next_free_dir_entry = brfs_find_next_free_dir_entry(
  372. brfs_data_block_addr + (parent_dir_fat_idx * superblock->words_per_block),
  373. superblock->words_per_block / sizeof(struct brfs_dir_entry)
  374. );
  375. if (next_free_dir_entry == -1)
  376. {
  377. uprintln("No free dir entries left!");
  378. return 0;
  379. }
  380. // Create dir entry
  381. struct brfs_dir_entry new_entry;
  382. brfs_create_single_dir_entry(&new_entry, dirname, next_free_block, dir_entries_max*sizeof(struct brfs_dir_entry), 1);
  383. // Copy dir entry to first free dir entry
  384. memcpy(
  385. brfs_data_block_addr + (parent_dir_fat_idx * superblock->words_per_block) + (next_free_dir_entry * sizeof(struct brfs_dir_entry)),
  386. (char*)&new_entry,
  387. sizeof(new_entry)
  388. );
  389. // Initialize directory
  390. brfs_init_directory(
  391. brfs_data_block_addr + (next_free_block * superblock->words_per_block),
  392. dir_entries_max,
  393. next_free_block,
  394. parent_dir_fat_idx
  395. );
  396. // Update changed block
  397. brfs_changed_blocks[next_free_block >> 5] |= (1 << (next_free_block & 31));
  398. brfs_changed_blocks[parent_dir_fat_idx >> 5] |= (1 << (parent_dir_fat_idx & 31));
  399. return 1;
  400. }
  401. /**
  402. * Create a new file in the directory of parent_dir_path
  403. * Returns 1 on success, 0 on error
  404. * parent_dir_path: full path of the parent directory
  405. * filename: name of the new file
  406. */
  407. word brfs_create_file(char* parent_dir_path, char* filename)
  408. {
  409. // Check length of filename
  410. if (strlen(filename) >= 16)
  411. {
  412. uprintln("Filename too long!");
  413. return 0;
  414. }
  415. struct brfs_superblock* superblock = (struct brfs_superblock*) brfs_ram_storage;
  416. word* brfs_data_block_addr = brfs_ram_storage + SUPERBLOCK_SIZE + superblock->total_blocks;
  417. // Find first free FAT block
  418. word next_free_block = brfs_find_next_free_block(brfs_ram_storage + SUPERBLOCK_SIZE, superblock->total_blocks);
  419. if (next_free_block == -1)
  420. {
  421. uprintln("No free blocks left!");
  422. return 0;
  423. }
  424. // Find data block address of parent directory path
  425. word parent_dir_fat_idx = brfs_get_fat_idx_of_dir(parent_dir_path);
  426. if (parent_dir_fat_idx == -1)
  427. {
  428. uprint("Parent directory ");
  429. uprint(parent_dir_path);
  430. uprintln(" not found!");
  431. return 0;
  432. }
  433. // Check if file or folder already exists
  434. word* parent_dir_addr = brfs_data_block_addr + (parent_dir_fat_idx * superblock->words_per_block);
  435. word dir_entries_max = superblock->words_per_block / sizeof(struct brfs_dir_entry);
  436. word i;
  437. for (i = 0; i < dir_entries_max; i++)
  438. {
  439. struct brfs_dir_entry* dir_entry = (struct brfs_dir_entry*) (parent_dir_addr + (i * sizeof(struct brfs_dir_entry)));
  440. if (dir_entry->filename[0] != 0)
  441. {
  442. char decompressed_filename[17];
  443. strdecompress(decompressed_filename, (char*)&(dir_entry->filename));
  444. if (strcmp(decompressed_filename, filename) == 0)
  445. {
  446. uprint(filename);
  447. uprintln(" already exists!");
  448. return 0;
  449. }
  450. }
  451. }
  452. // Find first free dir entry
  453. word next_free_dir_entry = brfs_find_next_free_dir_entry(
  454. brfs_data_block_addr + (parent_dir_fat_idx * superblock->words_per_block),
  455. superblock->words_per_block / sizeof(struct brfs_dir_entry)
  456. );
  457. if (next_free_dir_entry == -1)
  458. {
  459. uprintln("No free dir entries left!");
  460. return 0;
  461. }
  462. // Create file entry
  463. struct brfs_dir_entry new_entry;
  464. brfs_create_single_dir_entry(&new_entry, filename, next_free_block, 0, 0);
  465. // Copy dir entry to first free dir entry
  466. memcpy(
  467. brfs_data_block_addr + (parent_dir_fat_idx * superblock->words_per_block) + (next_free_dir_entry * sizeof(struct brfs_dir_entry)),
  468. (char*)&new_entry,
  469. sizeof(new_entry)
  470. );
  471. // Initialize file by setting data to 0
  472. memset(
  473. brfs_data_block_addr + (next_free_block * superblock->words_per_block),
  474. 0,
  475. superblock->words_per_block
  476. );
  477. // Update FAT
  478. brfs_ram_storage[SUPERBLOCK_SIZE + next_free_block] = -1;
  479. // Update changed block
  480. brfs_changed_blocks[next_free_block >> 5] |= (1 << (next_free_block & 31));
  481. return 1;
  482. }
  483. /**
  484. * Reads all directory entries of a directory into a buffer
  485. * dir_path: full path of the directory
  486. * buffer: buffer to store the directory entries
  487. * Returns the number of entries read, or -1 on error
  488. */
  489. word brfs_read_directory(char* dir_path, struct brfs_dir_entry* buffer)
  490. {
  491. // Find data block address of parent directory path
  492. word dir_fat_idx = brfs_get_fat_idx_of_dir(dir_path);
  493. if (dir_fat_idx == -1)
  494. {
  495. uprint("Parent directory ");
  496. uprint(dir_path);
  497. uprintln(" not found!");
  498. return -1;
  499. }
  500. struct brfs_superblock* superblock = (struct brfs_superblock*) brfs_ram_storage;
  501. word* dir_addr = brfs_ram_storage + SUPERBLOCK_SIZE + superblock->total_blocks + (dir_fat_idx * superblock->words_per_block);
  502. word dir_entries_max = superblock->words_per_block / sizeof(struct brfs_dir_entry);
  503. word entries_read = 0;
  504. word i;
  505. for (i = 0; i < dir_entries_max; i++)
  506. {
  507. struct brfs_dir_entry* dir_entry = (struct brfs_dir_entry*) (dir_addr + (i * sizeof(struct brfs_dir_entry)));
  508. if (dir_entry->filename[0] != 0)
  509. {
  510. memcpy(buffer, dir_entry, sizeof(struct brfs_dir_entry));
  511. buffer++;
  512. entries_read++;
  513. }
  514. }
  515. return entries_read;
  516. }
  517. /**
  518. * Open a file for reading and writing
  519. * Returns the file pointer (FAT idx of file), or -1 on error
  520. * file_path: full path of the file
  521. */
  522. word brfs_open_file(char* file_path)
  523. {
  524. // Split filename from path using basename and dirname
  525. char dirname_output[MAX_PATH_LENGTH];
  526. char* file_path_basename = basename(file_path);
  527. char* file_path_dirname = dirname(dirname_output, file_path);
  528. // Find data block address of parent directory path
  529. word dir_fat_idx = brfs_get_fat_idx_of_dir(file_path_dirname);
  530. if (dir_fat_idx == -1)
  531. {
  532. uprint("Parent directory ");
  533. uprint(file_path_dirname);
  534. uprintln(" not found!");
  535. return -1;
  536. }
  537. // Find file in directory
  538. struct brfs_superblock* superblock = (struct brfs_superblock*) brfs_ram_storage;
  539. word* dir_addr = brfs_ram_storage + SUPERBLOCK_SIZE + superblock->total_blocks + (dir_fat_idx * superblock->words_per_block);
  540. word dir_entries_max = superblock->words_per_block / sizeof(struct brfs_dir_entry);
  541. word i;
  542. for (i = 0; i < dir_entries_max; i++)
  543. {
  544. struct brfs_dir_entry* dir_entry = (struct brfs_dir_entry*) (dir_addr + (i * sizeof(struct brfs_dir_entry)));
  545. if (dir_entry->filename[0] != 0)
  546. {
  547. char decompressed_filename[17];
  548. strdecompress(decompressed_filename, (char*)&(dir_entry->filename));
  549. // Also check for directory flag to be 0
  550. if (strcmp(decompressed_filename, file_path_basename) == 0 && dir_entry->flags == 0)
  551. {
  552. // Found file
  553. // Check if file is already open
  554. word j;
  555. for (j = 0; j < MAX_OPEN_FILES; j++)
  556. {
  557. if (brfs_file_pointers[j] == dir_entry->fat_idx)
  558. {
  559. uprint("File ");
  560. uprint(file_path_basename);
  561. uprintln(" already open!");
  562. return -1;
  563. }
  564. }
  565. // Find first free file pointer
  566. word next_free_file_pointer = -1;
  567. for (j = 0; j < MAX_OPEN_FILES; j++)
  568. {
  569. if (brfs_file_pointers[j] == 0)
  570. {
  571. next_free_file_pointer = j;
  572. break;
  573. }
  574. }
  575. if (next_free_file_pointer == -1)
  576. {
  577. uprintln("All files already opened!");
  578. return -1;
  579. }
  580. // Open file
  581. brfs_file_pointers[next_free_file_pointer] = dir_entry->fat_idx;
  582. brfs_cursors[next_free_file_pointer] = 0;
  583. brfs_dir_entry_pointers[next_free_file_pointer] = dir_entry;
  584. return brfs_file_pointers[next_free_file_pointer];
  585. }
  586. }
  587. }
  588. uprint("File ");
  589. uprint(file_path_basename);
  590. uprintln(" not found!");
  591. return -1;
  592. }
  593. /**
  594. * Close an opened file
  595. * Returns 1 on success, 0 on error
  596. * file_pointer: file pointer returned by brfs_open_file
  597. */
  598. word brfs_close_file(word file_pointer)
  599. {
  600. // Find file pointer
  601. word i;
  602. for (i = 0; i < MAX_OPEN_FILES; i++)
  603. {
  604. if (brfs_file_pointers[i] == file_pointer)
  605. {
  606. // Close file
  607. brfs_file_pointers[i] = 0;
  608. brfs_cursors[i] = 0;
  609. brfs_dir_entry_pointers[i] = 0;
  610. return 1;
  611. }
  612. }
  613. uprintln("File not found!");
  614. return 0;
  615. }
  616. /**
  617. * Delete a file by removing all FAT blocks and the directory entry
  618. * Deletes a directory only if it is empty
  619. * Returns 1 on success, 0 on error
  620. * file_path: full path of the file
  621. */
  622. word brfs_delete(char* file_path)
  623. {
  624. // Split filename from path using basename and dirname
  625. char dirname_output[MAX_PATH_LENGTH];
  626. char* file_path_basename = basename(file_path);
  627. char* file_path_dirname = dirname(dirname_output, file_path);
  628. // Find data block address of parent directory path
  629. word dir_fat_idx = brfs_get_fat_idx_of_dir(file_path_dirname);
  630. if (dir_fat_idx == -1)
  631. {
  632. uprint("Parent directory ");
  633. uprint(file_path_dirname);
  634. uprintln(" not found!");
  635. return 0;
  636. }
  637. // Find file in directory
  638. struct brfs_superblock* superblock = (struct brfs_superblock*) brfs_ram_storage;
  639. word* dir_addr = brfs_ram_storage + SUPERBLOCK_SIZE + superblock->total_blocks + (dir_fat_idx * superblock->words_per_block);
  640. word dir_entries_max = superblock->words_per_block / sizeof(struct brfs_dir_entry);
  641. word i;
  642. for (i = 0; i < dir_entries_max; i++)
  643. {
  644. struct brfs_dir_entry* dir_entry = (struct brfs_dir_entry*) (dir_addr + (i * sizeof(struct brfs_dir_entry)));
  645. if (dir_entry->filename[0] != 0)
  646. {
  647. char decompressed_filename[17];
  648. strdecompress(decompressed_filename, (char*)&(dir_entry->filename));
  649. if (strcmp(decompressed_filename, file_path_basename) == 0)
  650. {
  651. if ((dir_entry->flags & 0x01) == 1)
  652. {
  653. // Check if directory is empty
  654. struct brfs_dir_entry buffer[128]; // 128 to be safe
  655. word num_entries = brfs_read_directory(file_path, buffer);
  656. if (num_entries > 2)
  657. {
  658. uprint("Directory ");
  659. uprint(file_path_basename);
  660. uprintln(" is not empty!");
  661. return 0;
  662. }
  663. }
  664. // Check if file is already open
  665. word j;
  666. for (j = 0; j < MAX_OPEN_FILES; j++)
  667. {
  668. if (brfs_file_pointers[j] == dir_entry->fat_idx)
  669. {
  670. uprint("File ");
  671. uprint(file_path_basename);
  672. uprintln(" is open!");
  673. return 0;
  674. }
  675. }
  676. // Delete fat blocks
  677. word current_fat_idx = dir_entry->fat_idx;
  678. word next_fat_idx;
  679. while (current_fat_idx != -1)
  680. {
  681. next_fat_idx = brfs_ram_storage[SUPERBLOCK_SIZE + current_fat_idx];
  682. brfs_ram_storage[SUPERBLOCK_SIZE + current_fat_idx] = 0;
  683. brfs_changed_blocks[current_fat_idx >> 5] |= (1 << (current_fat_idx & 31));
  684. current_fat_idx = next_fat_idx;
  685. }
  686. // Delete entry
  687. memset((char*)dir_entry, 0, sizeof(struct brfs_dir_entry));
  688. // Update changed block
  689. brfs_changed_blocks[dir_fat_idx >> 5] |= (1 << (dir_fat_idx & 31));
  690. return 1;
  691. }
  692. }
  693. }
  694. uprint("File ");
  695. uprint(file_path_basename);
  696. uprintln(" not found!");
  697. return 0;
  698. }
  699. /**
  700. * Set the cursor of an opened file
  701. * Returns 1 on success, 0 on error
  702. * file_pointer: file pointer returned by brfs_open_file
  703. * cursor: new cursor position in words
  704. */
  705. word brfs_set_cursor(word file_pointer, word cursor)
  706. {
  707. if (file_pointer == 0)
  708. {
  709. uprintln("File not open!");
  710. return 0;
  711. }
  712. // Find file pointer
  713. word i;
  714. for (i = 0; i < MAX_OPEN_FILES; i++)
  715. {
  716. if (brfs_file_pointers[i] == file_pointer)
  717. {
  718. // Set cursor
  719. if (cursor < 0 || cursor > brfs_dir_entry_pointers[i]->filesize)
  720. {
  721. cursor = brfs_dir_entry_pointers[i]->filesize;
  722. }
  723. brfs_cursors[i] = cursor;
  724. return 1;
  725. }
  726. }
  727. uprintln("File not found!");
  728. return 0;
  729. }
  730. /**
  731. * Get the cursor of an opened file
  732. * Returns the cursor position in words, or -1 on error
  733. * file_pointer: file pointer returned by brfs_open_file
  734. */
  735. word brfs_get_cursor(word file_pointer)
  736. {
  737. if (file_pointer == 0)
  738. {
  739. uprintln("File not open!");
  740. return -1;
  741. }
  742. // Find file pointer
  743. word i;
  744. for (i = 0; i < MAX_OPEN_FILES; i++)
  745. {
  746. if (brfs_file_pointers[i] == file_pointer)
  747. {
  748. // Get cursor
  749. return brfs_cursors[i];
  750. }
  751. }
  752. uprintln("File not found!");
  753. return -1;
  754. }
  755. /**
  756. * Get the FAT index of a file at the cursor
  757. * Returns the FAT index, or 0 on error
  758. * file_pointer: file pointer returned by brfs_open_file
  759. * cursor: cursor position of opened file
  760. */
  761. word brfs_get_fat_idx_at_cursor(word file_pointer, word cursor)
  762. {
  763. if (file_pointer == 0)
  764. {
  765. uprintln("File not open!");
  766. return 0;
  767. }
  768. // Get FAT index of file at cursor
  769. word current_fat_idx = file_pointer;
  770. struct brfs_superblock* superblock = (struct brfs_superblock*) brfs_ram_storage;
  771. // Loop through FAT until cursor is reached
  772. while (cursor > superblock->words_per_block)
  773. {
  774. current_fat_idx = brfs_ram_storage[SUPERBLOCK_SIZE + current_fat_idx];
  775. if (current_fat_idx == -1)
  776. {
  777. uprintln("Cursor is out of bounds!");
  778. return 0;
  779. }
  780. cursor -= superblock->words_per_block;
  781. }
  782. return current_fat_idx;
  783. }
  784. /**
  785. * Read a file from the cursor position
  786. * Returns 1 on success, or 0 on error
  787. * file_pointer: file pointer returned by brfs_open_file
  788. * buffer: buffer to read the file into
  789. * length: number of words to read
  790. */
  791. word brfs_read(word file_pointer, word* buffer, word length)
  792. {
  793. if (file_pointer == 0)
  794. {
  795. uprintln("File not open!");
  796. return 0;
  797. }
  798. struct brfs_superblock* superblock = (struct brfs_superblock*) brfs_ram_storage;
  799. word* data_block_addr = brfs_ram_storage + SUPERBLOCK_SIZE + superblock->total_blocks;
  800. // Find file pointer
  801. word i;
  802. for (i = 0; i < MAX_OPEN_FILES; i++)
  803. {
  804. if (brfs_file_pointers[i] == file_pointer)
  805. {
  806. if (length < 0)
  807. {
  808. uprintln("Length cannot be negative!");
  809. return 0;
  810. }
  811. // Trunctate length to file size - cursor
  812. if (length > brfs_dir_entry_pointers[i]->filesize - brfs_cursors[i])
  813. {
  814. length = brfs_dir_entry_pointers[i]->filesize - brfs_cursors[i];
  815. }
  816. // Get FAT index of file at cursor
  817. word current_fat_idx = brfs_get_fat_idx_at_cursor(file_pointer, brfs_cursors[i]);
  818. if (current_fat_idx == 0)
  819. {
  820. uprintln("Error getting FAT index at cursor!");
  821. return 0;
  822. }
  823. // Loop:
  824. // - calculate words until end of block (or up to length)
  825. // - read words until end of block (or up to length)
  826. // - decrease length by words read
  827. // - get next block from FAT
  828. // - repeat until length is 0
  829. while (length > 0)
  830. {
  831. word words_until_end_of_block = superblock->words_per_block - (MATH_modU(brfs_cursors[i], superblock->words_per_block));
  832. word words_to_read = words_until_end_of_block > length ? length : words_until_end_of_block;
  833. // Copy words to buffer
  834. memcpy(buffer, data_block_addr + (current_fat_idx * superblock->words_per_block) + MATH_modU(brfs_cursors[i], superblock->words_per_block), words_to_read);
  835. // Update cursor and length
  836. brfs_cursors[i] += words_to_read;
  837. length -= words_to_read;
  838. buffer += words_to_read;
  839. // Get next block from FAT
  840. current_fat_idx = brfs_ram_storage[SUPERBLOCK_SIZE + current_fat_idx];
  841. if (current_fat_idx == -1 && length > 0)
  842. {
  843. uprintln("There is no next block in the file!");
  844. return 0;
  845. }
  846. }
  847. return 1;
  848. }
  849. }
  850. uprintln("File not found!");
  851. return 0;
  852. }
  853. /**
  854. * Write a file from the cursor position
  855. * Returns 1 on success, or 0 on error
  856. * file_pointer: file pointer returned by brfs_open_file
  857. * buffer: buffer to write to the file
  858. * length: number of words to write
  859. */
  860. word brfs_write(word file_pointer, word* buffer, word length)
  861. {
  862. if (file_pointer == 0)
  863. {
  864. uprintln("File not open!");
  865. return 0;
  866. }
  867. struct brfs_superblock* superblock = (struct brfs_superblock*) brfs_ram_storage;
  868. word* data_block_addr = brfs_ram_storage + SUPERBLOCK_SIZE + superblock->total_blocks;
  869. // Find file pointer
  870. word i;
  871. for (i = 0; i < MAX_OPEN_FILES; i++)
  872. {
  873. if (brfs_file_pointers[i] == file_pointer)
  874. {
  875. if (length < 0)
  876. {
  877. uprintln("Length cannot be negative!");
  878. return 0;
  879. }
  880. // Get FAT index of file at cursor
  881. word current_fat_idx = brfs_get_fat_idx_at_cursor(file_pointer, brfs_cursors[i]);
  882. if (current_fat_idx == 0)
  883. {
  884. uprintln("Error getting FAT index at cursor!");
  885. return 0;
  886. }
  887. // Loop:
  888. // - calculate words until end of block (or up to length)
  889. // - write words until end of block (or up to length)
  890. // - decrease length by words written
  891. // - get next block from FAT, or find next free block if end of block
  892. // - if next block is needed, update FAT
  893. // - repeat until length is 0
  894. while (length > 0)
  895. {
  896. word cursor_in_block = MATH_modU(brfs_cursors[i], superblock->words_per_block);
  897. word words_until_end_of_block = superblock->words_per_block - cursor_in_block;
  898. word words_to_write = words_until_end_of_block > length ? length : words_until_end_of_block;
  899. // Copy words to buffer
  900. memcpy(data_block_addr + (current_fat_idx * superblock->words_per_block) + cursor_in_block, buffer, words_to_write);
  901. // Update changed block
  902. brfs_changed_blocks[current_fat_idx >> 5] |= (1 << (current_fat_idx & 31));
  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. // Update changed block
  932. brfs_changed_blocks[current_fat_idx >> 5] |= (1 << (current_fat_idx & 31));
  933. }
  934. }
  935. }
  936. // Update file size in dir entry if we wrote past the current size
  937. if (brfs_cursors[i] > brfs_dir_entry_pointers[i]->filesize)
  938. {
  939. brfs_dir_entry_pointers[i]->filesize = brfs_cursors[i];
  940. }
  941. return 1;
  942. }
  943. }
  944. uprintln("File not found!");
  945. return 0;
  946. }
  947. /**
  948. * Stat a file or directory
  949. * Returns the directory entry, or -1 on error
  950. */
  951. struct brfs_dir_entry* brfs_stat(char* file_path)
  952. {
  953. // Remove all trailing slashes
  954. while (strlen(file_path) > 1 && file_path[strlen(file_path)-1] == '/')
  955. {
  956. file_path[strlen(file_path)-1] = 0;
  957. }
  958. // Split filename from path using basename and dirname
  959. char dirname_output[MAX_PATH_LENGTH];
  960. char* file_path_basename = basename(file_path);
  961. char* file_path_dirname = dirname(dirname_output, file_path);
  962. // Find data block address of parent directory path
  963. word dir_fat_idx = brfs_get_fat_idx_of_dir(file_path_dirname);
  964. if (dir_fat_idx == -1)
  965. {
  966. uprint("Parent directory ");
  967. uprint(file_path_dirname);
  968. uprintln(" not found!");
  969. return (struct brfs_dir_entry*)-1;
  970. }
  971. // Find file in directory
  972. struct brfs_superblock* superblock = (struct brfs_superblock*) brfs_ram_storage;
  973. word* dir_addr = brfs_ram_storage + SUPERBLOCK_SIZE + superblock->total_blocks + (dir_fat_idx * superblock->words_per_block);
  974. word dir_entries_max = superblock->words_per_block / sizeof(struct brfs_dir_entry);
  975. word i;
  976. for (i = 0; i < dir_entries_max; i++)
  977. {
  978. struct brfs_dir_entry* dir_entry = (struct brfs_dir_entry*) (dir_addr + (i * sizeof(struct brfs_dir_entry)));
  979. if (dir_entry->filename[0] != 0)
  980. {
  981. char decompressed_filename[17];
  982. strdecompress(decompressed_filename, (char*)&(dir_entry->filename));
  983. // Also check for directory flag to be 0
  984. if (strcmp(decompressed_filename, file_path_basename) == 0)
  985. {
  986. return dir_entry;
  987. }
  988. }
  989. }
  990. uprint("File or directory ");
  991. uprint(file_path_basename);
  992. uprintln(" not found!");
  993. return (struct brfs_dir_entry*)-1;
  994. }
  995. /**
  996. * Write the FAT table to SPI flash by performing three steps:
  997. * 1. Check which FAT entries have changed
  998. * 2. Erase the 4KiB sectors that contain these FAT entries
  999. * 3. Write each changed FAT entry to flash by using 16 page writes per sector
  1000. */
  1001. void brfs_write_fat_to_flash()
  1002. {
  1003. struct brfs_superblock* superblock = (struct brfs_superblock*) brfs_ram_storage;
  1004. word* data_block_addr = brfs_ram_storage + SUPERBLOCK_SIZE + superblock->total_blocks;
  1005. // 1 sector = 4KiB = 1024 words = 1024 FAT entries
  1006. // 1 word contains 32 flags for changed blocks/FAT entries
  1007. // 1024/32 = 32 words in the changed_blocks array per sector
  1008. uprintln("---Writing FAT to SPI Flash---");
  1009. // Loop over brfs_changed_blocks in 32 word parts
  1010. // Assumes length of brfs_changed_blocks is a multiple of 32
  1011. word i;
  1012. for (i = 0; i < sizeof(brfs_changed_blocks); i+=32)
  1013. {
  1014. // Check if any value within brfs_changed_blocks[i:i+32] is not 0
  1015. word j;
  1016. word changed = 0;
  1017. for (j = 0; j < 32; j++)
  1018. {
  1019. if (brfs_changed_blocks[i+j] != 0)
  1020. {
  1021. changed = 1;
  1022. break;
  1023. }
  1024. }
  1025. if (changed)
  1026. {
  1027. // Erase sector
  1028. word addr = BRFS_SPIFLASH_FAT_ADDR; // Workaround because of large static number
  1029. addr += (i >> 5) * 4096; // Sector idx * bytes per sector
  1030. spiflash_sector_erase(addr);
  1031. uprint("Erased sector ");
  1032. uprintDec(i >> 5);
  1033. uprint(" at address ");
  1034. uprintHex(addr);
  1035. uprintln("");
  1036. // Write sector by writing 16 pages k of 64 words
  1037. // Does not check for boundaries of actual FAT table size,
  1038. // so it can write garbage if block size is not a multiple of 1024
  1039. word k;
  1040. for (k = 0; k < 1024; k+=64)
  1041. {
  1042. addr = BRFS_SPIFLASH_FAT_ADDR; // Workaround because of large static number
  1043. addr += (i >> 5) * 4096; // Sector idx * bytes per sector
  1044. addr += k << 2; // 64 words * 4 bytes per word
  1045. word* fat_addr_ram = brfs_ram_storage + SUPERBLOCK_SIZE + (i << 5) + k;
  1046. spiflash_write_page_in_words(fat_addr_ram, addr, 64);
  1047. uprint("Wrote FAT entries ");
  1048. uprintDec((i << 5) + k);
  1049. uprint(":");
  1050. uprintDec((i << 5) + k + 63);
  1051. uprint(" from RAM addr ");
  1052. uprintHex((word)fat_addr_ram);
  1053. uprint(" to SPI Flash addr ");
  1054. uprintHex(addr);
  1055. uprintln("");
  1056. }
  1057. }
  1058. }
  1059. uprintln("---Finished writing FAT to SPI Flash---");
  1060. }
  1061. /**
  1062. * Write a sector (4KiB) to SPI flash
  1063. * sector_idx: index of the sector
  1064. */
  1065. void brfs_write_sector_to_flash(word sector_idx)
  1066. {
  1067. word spi_addr = BRFS_SPIFLASH_BLOCK_ADDR; // Workaround because of large static number
  1068. spi_addr += sector_idx * 4096; // Sector idx * bytes per sector
  1069. struct brfs_superblock* superblock = (struct brfs_superblock*) brfs_ram_storage;
  1070. word* data_block_addr = brfs_ram_storage + SUPERBLOCK_SIZE + superblock->total_blocks;
  1071. word brfs_sector_addr = data_block_addr + sector_idx * (4096 >> 2); // Divided by 4 because of word size
  1072. // Write sector by writing 16 pages k of 64 words
  1073. // Does not check for boundaries of actual FAT table size,
  1074. // so it can write garbage if block size is not a multiple of 1024
  1075. word k;
  1076. for (k = 0; k < 1024; k+=64)
  1077. {
  1078. spiflash_write_page_in_words(brfs_sector_addr + k, spi_addr + (k << 2), 64);
  1079. uprint("Wrote sector ");
  1080. uprintDec(sector_idx);
  1081. uprint(":");
  1082. uprintDec(sector_idx + 15);
  1083. uprint(" from RAM addr ");
  1084. uprintHex((word)(brfs_sector_addr + k));
  1085. uprint(" to SPI Flash addr ");
  1086. uprintHex(spi_addr + (k << 2));
  1087. uprintln("");
  1088. }
  1089. }
  1090. /**
  1091. * Write the data blocks to SPI flash by performing three steps:
  1092. * 1. Check which blocks have changed
  1093. * 2. Erase the 4KiB sectors that contain these blocks
  1094. * 3. Write each erased sector with the new block data by using 16 page writes per sector
  1095. */
  1096. void brfs_write_blocks_to_flash()
  1097. {
  1098. // Loop over all blocks
  1099. struct brfs_superblock* superblock = (struct brfs_superblock*) brfs_ram_storage;
  1100. word* data_block_addr = brfs_ram_storage + SUPERBLOCK_SIZE + superblock->total_blocks;
  1101. // Check if block size is <= 4KiB
  1102. if (superblock->words_per_block > 1024)
  1103. {
  1104. uprintln("Error: block size should be <= 4KiB");
  1105. return;
  1106. }
  1107. // Check if block size is a multiple of 64
  1108. if (superblock->words_per_block & 63)
  1109. {
  1110. uprintln("Error: block size should be a multiple of 64");
  1111. return;
  1112. }
  1113. uprintln("---Writing blocks to SPI Flash---");
  1114. word blocks_per_sector = MATH_divU(4096, superblock->words_per_block * 4);
  1115. uprint("Blocks per sector: ");
  1116. uprintDec(blocks_per_sector);
  1117. uprintln("");
  1118. // Erase 4KiB sectors that contain changed blocks
  1119. // This code is written such that it only erases each sector once, even if multiple blocks in the sector have changed
  1120. word i;
  1121. word sector_to_erase = -1;
  1122. for (i = 0; i < superblock->total_blocks; i++)
  1123. {
  1124. if (brfs_changed_blocks[i >> 5] & (1 << (i & 31)))
  1125. {
  1126. if (sector_to_erase == -1)
  1127. {
  1128. sector_to_erase = MATH_divU(i, blocks_per_sector);
  1129. }
  1130. else if (sector_to_erase != MATH_divU(i, blocks_per_sector))
  1131. {
  1132. word addr = BRFS_SPIFLASH_BLOCK_ADDR; // Workaround because of large static number
  1133. addr += sector_to_erase * 4096;
  1134. spiflash_sector_erase(addr);
  1135. uprint("Erased sector ");
  1136. uprintDec(sector_to_erase);
  1137. uprint(" at address ");
  1138. uprintHex(addr);
  1139. uprintln("");
  1140. brfs_write_sector_to_flash(sector_to_erase);
  1141. sector_to_erase = MATH_divU(i, blocks_per_sector);
  1142. }
  1143. }
  1144. }
  1145. if (sector_to_erase != -1)
  1146. {
  1147. word addr = BRFS_SPIFLASH_BLOCK_ADDR; // Workaround because of large static number
  1148. addr += sector_to_erase * 4096;
  1149. spiflash_sector_erase(addr);
  1150. uprint("Erased sector ");
  1151. uprintDec(sector_to_erase);
  1152. uprint(" at address ");
  1153. uprintHex(addr);
  1154. uprintln("");
  1155. brfs_write_sector_to_flash(sector_to_erase);
  1156. }
  1157. uprintln("---Finished writing blocks to SPI Flash---");
  1158. }
  1159. /**
  1160. * Write the FAT and data blocks to SPI flash
  1161. * Superblock should already be written to flash during format
  1162. */
  1163. void brfs_write_to_flash()
  1164. {
  1165. brfs_write_fat_to_flash();
  1166. brfs_write_blocks_to_flash();
  1167. // Reset changed blocks
  1168. memset(brfs_changed_blocks, 0, sizeof(brfs_changed_blocks));
  1169. }
  1170. /**
  1171. * Checks if given superblock is valid
  1172. * Returns 1 if valid, 0 if invalid
  1173. */
  1174. word brfs_superblock_is_valid(struct brfs_superblock* superblock)
  1175. {
  1176. // Check if brfs version is correct
  1177. if (superblock->brfs_version != BRFS_SUPPORTED_VERSION)
  1178. {
  1179. uprint("BRFS version ");
  1180. uprintDec(superblock->brfs_version);
  1181. uprint(" is not supported by this implementation (");
  1182. uprintDec(BRFS_SUPPORTED_VERSION);
  1183. uprintln(")!");
  1184. return 0;
  1185. }
  1186. // Check if total blocks is > 0 and a multiple of 64
  1187. if (superblock->total_blocks == 0 || superblock->total_blocks & 63)
  1188. {
  1189. uprintln("Error: total blocks should be > 0 and a multiple of 64");
  1190. return 0;
  1191. }
  1192. // Check if block size is > 0
  1193. if (superblock->words_per_block == 0)
  1194. {
  1195. uprintln("Error: block size should be > 0");
  1196. return 0;
  1197. }
  1198. // Check if words per block is > 0 and <= 2048
  1199. if (superblock->words_per_block == 0 || superblock->words_per_block > 2048)
  1200. {
  1201. uprintln("Error: words per block should be > 0 and <= 2048");
  1202. return 0;
  1203. }
  1204. return 1;
  1205. }
  1206. /**
  1207. * Read the superblock, FAT and data blocks from SPI flash
  1208. * Reads in QSPI mode and returns to SPI mode after reading
  1209. * Returns 1 on success, or 0 on error
  1210. */
  1211. word brfs_read_from_flash()
  1212. {
  1213. // Set QSPI memory mapped mode
  1214. spiflash_qspi();
  1215. // Read superblock from flash
  1216. char* spi_flash_read_addr = (char*) SPIFLASH_MEMMAP_ADDR + (BRFS_SPIFLASH_SUPERBLOCK_ADDR >> 2);
  1217. memcpy(brfs_ram_storage, spi_flash_read_addr, SUPERBLOCK_SIZE);
  1218. // Perform validity checks on superblock
  1219. struct brfs_superblock* superblock = (struct brfs_superblock*) brfs_ram_storage;
  1220. if (!brfs_superblock_is_valid(superblock))
  1221. {
  1222. uprintln("Error: superblock is not valid!");
  1223. spiflash_init(); // Return to SPI mode
  1224. return 0;
  1225. }
  1226. // Read FAT from flash
  1227. spi_flash_read_addr = (char*) SPIFLASH_MEMMAP_ADDR + (BRFS_SPIFLASH_FAT_ADDR >> 2);
  1228. memcpy(brfs_ram_storage + SUPERBLOCK_SIZE, spi_flash_read_addr, superblock->total_blocks);
  1229. // Read data blocks from flash
  1230. word* data_block_addr = brfs_ram_storage + SUPERBLOCK_SIZE + superblock->total_blocks;
  1231. spi_flash_read_addr = (char*) SPIFLASH_MEMMAP_ADDR + (BRFS_SPIFLASH_BLOCK_ADDR >> 2);
  1232. memcpy(data_block_addr, spi_flash_read_addr, superblock->total_blocks * superblock->words_per_block);
  1233. spiflash_init(); // Return to SPI mode
  1234. return 1;
  1235. }