microbitfs.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  1. /*
  2. * This file is part of the Micro Python project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2016 Mark Shannon
  7. * Copyright (c) 2017 Ayke van Laethem
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. */
  27. #include <string.h>
  28. #include <stdio.h>
  29. #include <sys/stat.h>
  30. #include "microbitfs.h"
  31. #include "drivers/flash.h"
  32. #include "modrandom.h"
  33. #include "py/obj.h"
  34. #include "py/stream.h"
  35. #include "py/runtime.h"
  36. #include "extmod/vfs.h"
  37. #include "mpconfigport.h"
  38. #if MICROPY_MBFS
  39. #define DEBUG_FILE 0
  40. #if DEBUG_FILE
  41. #define DEBUG(s) printf s
  42. #else
  43. #define DEBUG(s) (void)0
  44. #endif
  45. /** How it works:
  46. * The File System consists of up to MAX_CHUNKS_IN_FILE_SYSTEM chunks of CHUNK_SIZE each,
  47. * plus one spare page which holds persistent configuration data and is used. for bulk erasing.
  48. * The spare page is either the first or the last page and will be switched by a bulk erase.
  49. * The exact number of chunks will depend on the amount of flash available.
  50. *
  51. * Each chunk consists of a one byte marker and a one byte tail
  52. * The marker shows whether this chunk is the start of a file, the midst of a file
  53. * (in which case it refers to the previous chunk in the file) or whether it is UNUSED
  54. * (and erased) or FREED (which means it is unused, but not erased).
  55. * Chunks are selected in a randomised round-robin fashion to even out wear on the flash
  56. * memory as much as possible.
  57. * A file consists of a linked list of chunks. The first chunk in a file contains its name
  58. * as well as the end chunk and offset.
  59. * Files are found by linear search of the chunks, this means that no meta-data needs to be stored
  60. * outside of the file, which prevents wear hot-spots. Since there are fewer than 250 chunks,
  61. * the search is fast enough.
  62. *
  63. * Chunks are numbered from 1 as we need to reserve 0 as the FREED marker.
  64. *
  65. * Writing to files relies on the persistent API which is high-level wrapper on top of the Nordic SDK.
  66. */
  67. #define CHUNK_SIZE (1<<MBFS_LOG_CHUNK_SIZE)
  68. #define DATA_PER_CHUNK (CHUNK_SIZE-2)
  69. #define UNUSED_CHUNK 255
  70. #define FREED_CHUNK 0
  71. #define FILE_START 254
  72. #define PERSISTENT_DATA_MARKER 253
  73. /** Must be such that sizeof(file_header) < DATA_PER_CHUNK */
  74. #define MAX_FILENAME_LENGTH 120
  75. //Minimum number of free chunks to justify sweeping.
  76. //If this is too low it may cause excessive wear
  77. #define MIN_CHUNKS_FOR_SWEEP (FLASH_PAGESIZE / CHUNK_SIZE)
  78. #define FILE_NOT_FOUND ((uint8_t)-1)
  79. /** Maximum number of chunks allowed in filesystem. 252 chunks is 31.5kB */
  80. #define MAX_CHUNKS_IN_FILE_SYSTEM 252
  81. #define STATIC_ASSERT(e) extern char static_assert_failed[(e) ? 1 : -1]
  82. typedef struct _file_descriptor_obj {
  83. mp_obj_base_t base;
  84. uint8_t start_chunk;
  85. uint8_t seek_chunk;
  86. uint8_t seek_offset;
  87. bool writable;
  88. bool open;
  89. bool binary;
  90. } file_descriptor_obj;
  91. typedef struct _file_header {
  92. uint8_t end_offset;
  93. uint8_t name_len;
  94. char filename[MAX_FILENAME_LENGTH];
  95. } file_header;
  96. typedef struct _file_chunk {
  97. uint8_t marker;
  98. union {
  99. char data[DATA_PER_CHUNK];
  100. file_header header;
  101. };
  102. uint8_t next_chunk;
  103. } file_chunk;
  104. typedef struct _persistent_config_t {
  105. // Must start with a marker, so that we can identify it.
  106. uint8_t marker; // Should always be PERSISTENT_DATA_MARKER
  107. } persistent_config_t;
  108. extern const mp_obj_type_t uos_mbfs_fileio_type;
  109. extern const mp_obj_type_t uos_mbfs_textio_type;
  110. // Page indexes count down from the end of ROM.
  111. STATIC uint8_t first_page_index;
  112. STATIC uint8_t last_page_index;
  113. // The number of useable chunks in the file system.
  114. STATIC uint8_t chunks_in_file_system;
  115. // Index of chunk to start searches. This is randomised to even out wear.
  116. STATIC uint8_t start_index;
  117. STATIC file_chunk *file_system_chunks;
  118. // Defined by the linker
  119. extern byte _fs_start[];
  120. extern byte _fs_end[];
  121. STATIC_ASSERT((sizeof(file_chunk) == CHUNK_SIZE));
  122. // From micro:bit memory.h
  123. STATIC inline byte *rounddown(byte *addr, uint32_t align) {
  124. return (byte*)(((uint32_t)addr)&(-align));
  125. }
  126. // From micro:bit memory.h
  127. STATIC inline byte *roundup(byte *addr, uint32_t align) {
  128. return (byte*)((((uint32_t)addr)+align-1)&(-align));
  129. }
  130. STATIC inline void *first_page(void) {
  131. return _fs_end - FLASH_PAGESIZE * first_page_index;
  132. }
  133. STATIC inline void *last_page(void) {
  134. return _fs_end - FLASH_PAGESIZE * last_page_index;
  135. }
  136. STATIC void init_limits(void) {
  137. // First determine where to end
  138. byte *end = _fs_end;
  139. end = rounddown(end, FLASH_PAGESIZE)-FLASH_PAGESIZE;
  140. last_page_index = (_fs_end - end)/FLASH_PAGESIZE;
  141. // Now find the start
  142. byte *start = roundup(end - CHUNK_SIZE*MAX_CHUNKS_IN_FILE_SYSTEM, FLASH_PAGESIZE);
  143. while (start < _fs_start) {
  144. start += FLASH_PAGESIZE;
  145. }
  146. first_page_index = (_fs_end - start)/FLASH_PAGESIZE;
  147. chunks_in_file_system = (end-start)>>MBFS_LOG_CHUNK_SIZE;
  148. }
  149. STATIC void randomise_start_index(void) {
  150. start_index = machine_rng_generate_random_word() % chunks_in_file_system + 1;
  151. }
  152. void microbit_filesystem_init(void) {
  153. init_limits();
  154. randomise_start_index();
  155. file_chunk *base = first_page();
  156. if (base->marker == PERSISTENT_DATA_MARKER) {
  157. file_system_chunks = &base[(FLASH_PAGESIZE>>MBFS_LOG_CHUNK_SIZE)-1];
  158. } else if (((file_chunk *)last_page())->marker == PERSISTENT_DATA_MARKER) {
  159. file_system_chunks = &base[-1];
  160. } else {
  161. flash_write_byte((uint32_t)&((file_chunk *)last_page())->marker, PERSISTENT_DATA_MARKER);
  162. file_system_chunks = &base[-1];
  163. }
  164. }
  165. STATIC void copy_page(void *dest, void *src) {
  166. DEBUG(("FILE DEBUG: Copying page from %lx to %lx.\r\n", (uint32_t)src, (uint32_t)dest));
  167. flash_page_erase((uint32_t)dest);
  168. file_chunk *src_chunk = src;
  169. file_chunk *dest_chunk = dest;
  170. uint32_t chunks = FLASH_PAGESIZE>>MBFS_LOG_CHUNK_SIZE;
  171. for (uint32_t i = 0; i < chunks; i++) {
  172. if (src_chunk[i].marker != FREED_CHUNK) {
  173. flash_write_bytes((uint32_t)&dest_chunk[i], (uint8_t*)&src_chunk[i], CHUNK_SIZE);
  174. }
  175. }
  176. }
  177. // Move entire file system up or down one page, copying all used chunks
  178. // Freed chunks are not copied, so become erased.
  179. // There should be no erased chunks before the sweep (or it would be unnecessary)
  180. // but if there are this should work correctly.
  181. //
  182. // The direction of the sweep depends on whether the persistent data is in the first or last page
  183. // The persistent data is copied to RAM, leaving its page unused.
  184. // Then all the pages are copied, one by one, into the adjacent newly unused page.
  185. // Finally, the persistent data is saved back to the opposite end of the filesystem from whence it came.
  186. //
  187. STATIC void filesystem_sweep(void) {
  188. persistent_config_t config;
  189. uint8_t *page;
  190. uint8_t *end_page;
  191. int step;
  192. uint32_t page_size = FLASH_PAGESIZE;
  193. DEBUG(("FILE DEBUG: Sweeping file system\r\n"));
  194. if (((file_chunk *)first_page())->marker == PERSISTENT_DATA_MARKER) {
  195. config = *(persistent_config_t *)first_page();
  196. page = first_page();
  197. end_page = last_page();
  198. step = page_size;
  199. } else {
  200. config = *(persistent_config_t *)last_page();
  201. page = last_page();
  202. end_page = first_page();
  203. step = -page_size;
  204. }
  205. while (page != end_page) {
  206. uint8_t *next_page = page+step;
  207. flash_page_erase((uint32_t)page);
  208. copy_page(page, next_page);
  209. page = next_page;
  210. }
  211. flash_page_erase((uint32_t)end_page);
  212. flash_write_bytes((uint32_t)end_page, (uint8_t*)&config, sizeof(config));
  213. microbit_filesystem_init();
  214. }
  215. STATIC inline byte *seek_address(file_descriptor_obj *self) {
  216. return (byte*)&(file_system_chunks[self->seek_chunk].data[self->seek_offset]);
  217. }
  218. STATIC uint8_t microbit_find_file(const char *name, int name_len) {
  219. for (uint8_t index = 1; index <= chunks_in_file_system; index++) {
  220. const file_chunk *p = &file_system_chunks[index];
  221. if (p->marker != FILE_START)
  222. continue;
  223. if (p->header.name_len != name_len)
  224. continue;
  225. if (memcmp(name, &p->header.filename[0], name_len) == 0) {
  226. DEBUG(("FILE DEBUG: File found. index %d\r\n", index));
  227. return index;
  228. }
  229. }
  230. DEBUG(("FILE DEBUG: File not found.\r\n"));
  231. return FILE_NOT_FOUND;
  232. }
  233. // Return a free, erased chunk.
  234. // Search the chunks:
  235. // 1 If an UNUSED chunk is found, then return that.
  236. // 2. If an entire page of FREED chunks is found, then erase the page and return the first chunk
  237. // 3. If the number of FREED chunks is >= MIN_CHUNKS_FOR_SWEEP, then
  238. // 3a. Sweep the filesystem and restart.
  239. // 3b. Fail and return FILE_NOT_FOUND
  240. //
  241. STATIC uint8_t find_chunk_and_erase(void) {
  242. // Start search at a random chunk to spread the wear more evenly.
  243. // Search for unused chunk
  244. uint8_t index = start_index;
  245. do {
  246. const file_chunk *p = &file_system_chunks[index];
  247. if (p->marker == UNUSED_CHUNK) {
  248. DEBUG(("FILE DEBUG: Unused chunk found: %d\r\n", index));
  249. return index;
  250. }
  251. index++;
  252. if (index == chunks_in_file_system+1) index = 1;
  253. } while (index != start_index);
  254. // Search for FREED page, and total up FREED chunks
  255. uint32_t freed_chunks = 0;
  256. index = start_index;
  257. uint32_t chunks_per_page = FLASH_PAGESIZE>>MBFS_LOG_CHUNK_SIZE;
  258. do {
  259. const file_chunk *p = &file_system_chunks[index];
  260. if (p->marker == FREED_CHUNK) {
  261. freed_chunks++;
  262. }
  263. if (FLASH_IS_PAGE_ALIGNED(p)) {
  264. uint32_t i;
  265. for (i = 0; i < chunks_per_page; i++) {
  266. if (p[i].marker != FREED_CHUNK)
  267. break;
  268. }
  269. if (i == chunks_per_page) {
  270. DEBUG(("FILE DEBUG: Found freed page of chunks: %d\r\n", index));
  271. flash_page_erase((uint32_t)&file_system_chunks[index]);
  272. return index;
  273. }
  274. }
  275. index++;
  276. if (index == chunks_in_file_system+1) index = 1;
  277. } while (index != start_index);
  278. DEBUG(("FILE DEBUG: %lu free chunks\r\n", freed_chunks));
  279. if (freed_chunks < MIN_CHUNKS_FOR_SWEEP) {
  280. return FILE_NOT_FOUND;
  281. }
  282. // No freed pages, so sweep file system.
  283. filesystem_sweep();
  284. // This is guaranteed to succeed.
  285. return find_chunk_and_erase();
  286. }
  287. STATIC mp_obj_t microbit_file_name(file_descriptor_obj *fd) {
  288. return mp_obj_new_str(&(file_system_chunks[fd->start_chunk].header.filename[0]), file_system_chunks[fd->start_chunk].header.name_len);
  289. }
  290. STATIC file_descriptor_obj *microbit_file_descriptor_new(uint8_t start_chunk, bool write, bool binary);
  291. STATIC void clear_file(uint8_t chunk) {
  292. do {
  293. flash_write_byte((uint32_t)&(file_system_chunks[chunk].marker), FREED_CHUNK);
  294. DEBUG(("FILE DEBUG: Freeing chunk %d.\n", chunk));
  295. chunk = file_system_chunks[chunk].next_chunk;
  296. } while (chunk <= chunks_in_file_system);
  297. }
  298. STATIC file_descriptor_obj *microbit_file_open(const char *name, size_t name_len, bool write, bool binary) {
  299. if (name_len > MAX_FILENAME_LENGTH) {
  300. return NULL;
  301. }
  302. uint8_t index = microbit_find_file(name, name_len);
  303. if (write) {
  304. if (index != FILE_NOT_FOUND) {
  305. // Free old file
  306. clear_file(index);
  307. }
  308. index = find_chunk_and_erase();
  309. if (index == FILE_NOT_FOUND) {
  310. mp_raise_OSError(MP_ENOSPC);
  311. }
  312. flash_write_byte((uint32_t)&(file_system_chunks[index].marker), FILE_START);
  313. flash_write_byte((uint32_t)&(file_system_chunks[index].header.name_len), name_len);
  314. flash_write_bytes((uint32_t)&(file_system_chunks[index].header.filename[0]), (uint8_t*)name, name_len);
  315. } else {
  316. if (index == FILE_NOT_FOUND) {
  317. return NULL;
  318. }
  319. }
  320. return microbit_file_descriptor_new(index, write, binary);
  321. }
  322. STATIC file_descriptor_obj *microbit_file_descriptor_new(uint8_t start_chunk, bool write, bool binary) {
  323. file_descriptor_obj *res = m_new_obj(file_descriptor_obj);
  324. if (binary) {
  325. res->base.type = &uos_mbfs_fileio_type;
  326. } else {
  327. res->base.type = &uos_mbfs_textio_type;
  328. }
  329. res->start_chunk = start_chunk;
  330. res->seek_chunk = start_chunk;
  331. res->seek_offset = file_system_chunks[start_chunk].header.name_len+2;
  332. res->writable = write;
  333. res->open = true;
  334. res->binary = binary;
  335. return res;
  336. }
  337. STATIC mp_obj_t microbit_remove(mp_obj_t filename) {
  338. mp_uint_t name_len;
  339. const char *name = mp_obj_str_get_data(filename, &name_len);
  340. mp_uint_t index = microbit_find_file(name, name_len);
  341. if (index == 255) {
  342. mp_raise_OSError(MP_ENOENT);
  343. }
  344. clear_file(index);
  345. return mp_const_none;
  346. }
  347. STATIC void check_file_open(file_descriptor_obj *self) {
  348. if (!self->open) {
  349. mp_raise_ValueError("I/O operation on closed file");
  350. }
  351. }
  352. STATIC int advance(file_descriptor_obj *self, uint32_t n, bool write) {
  353. DEBUG(("FILE DEBUG: Advancing from chunk %d, offset %d.\r\n", self->seek_chunk, self->seek_offset));
  354. self->seek_offset += n;
  355. if (self->seek_offset == DATA_PER_CHUNK) {
  356. self->seek_offset = 0;
  357. if (write) {
  358. uint8_t next_chunk = find_chunk_and_erase();
  359. if (next_chunk == FILE_NOT_FOUND) {
  360. clear_file(self->start_chunk);
  361. self->open = false;
  362. return MP_ENOSPC;
  363. }
  364. // Link next chunk to this one
  365. flash_write_byte((uint32_t)&(file_system_chunks[self->seek_chunk].next_chunk), next_chunk);
  366. flash_write_byte((uint32_t)&(file_system_chunks[next_chunk].marker), self->seek_chunk);
  367. }
  368. self->seek_chunk = file_system_chunks[self->seek_chunk].next_chunk;
  369. }
  370. DEBUG(("FILE DEBUG: Advanced to chunk %d, offset %d.\r\n", self->seek_chunk, self->seek_offset));
  371. return 0;
  372. }
  373. STATIC mp_uint_t microbit_file_read(mp_obj_t obj, void *buf, mp_uint_t size, int *errcode) {
  374. file_descriptor_obj *self = (file_descriptor_obj *)obj;
  375. check_file_open(self);
  376. if (self->writable || file_system_chunks[self->start_chunk].marker == FREED_CHUNK) {
  377. *errcode = MP_EBADF;
  378. return MP_STREAM_ERROR;
  379. }
  380. uint32_t bytes_read = 0;
  381. uint8_t *data = buf;
  382. while (1) {
  383. mp_uint_t to_read = DATA_PER_CHUNK - self->seek_offset;
  384. if (file_system_chunks[self->seek_chunk].next_chunk == UNUSED_CHUNK) {
  385. uint8_t end_offset = file_system_chunks[self->start_chunk].header.end_offset;
  386. if (end_offset == UNUSED_CHUNK) {
  387. to_read = 0;
  388. } else {
  389. to_read = MIN(to_read, (mp_uint_t)end_offset-self->seek_offset);
  390. }
  391. }
  392. to_read = MIN(to_read, size-bytes_read);
  393. if (to_read == 0) {
  394. break;
  395. }
  396. memcpy(data+bytes_read, seek_address(self), to_read);
  397. advance(self, to_read, false);
  398. bytes_read += to_read;
  399. }
  400. return bytes_read;
  401. }
  402. STATIC mp_uint_t microbit_file_write(mp_obj_t obj, const void *buf, mp_uint_t size, int *errcode) {
  403. file_descriptor_obj *self = (file_descriptor_obj *)obj;
  404. check_file_open(self);
  405. if (!self->writable || file_system_chunks[self->start_chunk].marker == FREED_CHUNK) {
  406. *errcode = MP_EBADF;
  407. return MP_STREAM_ERROR;
  408. }
  409. uint32_t len = size;
  410. const uint8_t *data = buf;
  411. while (len) {
  412. uint32_t to_write = MIN(((uint32_t)(DATA_PER_CHUNK - self->seek_offset)), len);
  413. flash_write_bytes((uint32_t)seek_address(self), data, to_write);
  414. int err = advance(self, to_write, true);
  415. if (err) {
  416. *errcode = err;
  417. return MP_STREAM_ERROR;
  418. }
  419. data += to_write;
  420. len -= to_write;
  421. }
  422. return size;
  423. }
  424. STATIC void microbit_file_close(file_descriptor_obj *fd) {
  425. if (fd->writable) {
  426. flash_write_byte((uint32_t)&(file_system_chunks[fd->start_chunk].header.end_offset), fd->seek_offset);
  427. }
  428. fd->open = false;
  429. }
  430. STATIC mp_obj_t microbit_file_list(void) {
  431. mp_obj_t res = mp_obj_new_list(0, NULL);
  432. for (uint8_t index = 1; index <= chunks_in_file_system; index++) {
  433. if (file_system_chunks[index].marker == FILE_START) {
  434. mp_obj_t name = mp_obj_new_str(&file_system_chunks[index].header.filename[0], file_system_chunks[index].header.name_len);
  435. mp_obj_list_append(res, name);
  436. }
  437. }
  438. return res;
  439. }
  440. STATIC mp_obj_t microbit_file_size(mp_obj_t filename) {
  441. mp_uint_t name_len;
  442. const char *name = mp_obj_str_get_data(filename, &name_len);
  443. uint8_t chunk = microbit_find_file(name, name_len);
  444. if (chunk == 255) {
  445. mp_raise_OSError(MP_ENOENT);
  446. }
  447. mp_uint_t len = 0;
  448. uint8_t end_offset = file_system_chunks[chunk].header.end_offset;
  449. uint8_t offset = file_system_chunks[chunk].header.name_len+2;
  450. while (file_system_chunks[chunk].next_chunk != UNUSED_CHUNK) {
  451. len += DATA_PER_CHUNK - offset;
  452. chunk = file_system_chunks[chunk].next_chunk;
  453. offset = 0;
  454. }
  455. len += end_offset - offset;
  456. return mp_obj_new_int(len);
  457. }
  458. STATIC mp_uint_t file_read_byte(file_descriptor_obj *fd) {
  459. if (file_system_chunks[fd->seek_chunk].next_chunk == UNUSED_CHUNK) {
  460. uint8_t end_offset = file_system_chunks[fd->start_chunk].header.end_offset;
  461. if (end_offset == UNUSED_CHUNK || fd->seek_offset == end_offset) {
  462. return (mp_uint_t)-1;
  463. }
  464. }
  465. mp_uint_t res = file_system_chunks[fd->seek_chunk].data[fd->seek_offset];
  466. advance(fd, 1, false);
  467. return res;
  468. }
  469. // Now follows the code to integrate this filesystem into the uos module.
  470. mp_lexer_t *uos_mbfs_new_reader(const char *filename) {
  471. file_descriptor_obj *fd = microbit_file_open(filename, strlen(filename), false, false);
  472. if (fd == NULL) {
  473. mp_raise_OSError(MP_ENOENT);
  474. }
  475. mp_reader_t reader;
  476. reader.data = fd;
  477. reader.readbyte = (mp_uint_t(*)(void*))file_read_byte;
  478. reader.close = (void(*)(void*))microbit_file_close; // no-op
  479. return mp_lexer_new(qstr_from_str(filename), reader);
  480. }
  481. mp_import_stat_t uos_mbfs_import_stat(const char *path) {
  482. uint8_t chunk = microbit_find_file(path, strlen(path));
  483. if (chunk == FILE_NOT_FOUND) {
  484. return MP_IMPORT_STAT_NO_EXIST;
  485. } else {
  486. return MP_IMPORT_STAT_FILE;
  487. }
  488. }
  489. STATIC mp_obj_t uos_mbfs_file_name(mp_obj_t self) {
  490. file_descriptor_obj *fd = (file_descriptor_obj*)self;
  491. return microbit_file_name(fd);
  492. }
  493. STATIC MP_DEFINE_CONST_FUN_OBJ_1(uos_mbfs_file_name_obj, uos_mbfs_file_name);
  494. STATIC mp_obj_t uos_mbfs_file_close(mp_obj_t self) {
  495. file_descriptor_obj *fd = (file_descriptor_obj*)self;
  496. microbit_file_close(fd);
  497. return mp_const_none;
  498. }
  499. STATIC MP_DEFINE_CONST_FUN_OBJ_1(uos_mbfs_file_close_obj, uos_mbfs_file_close);
  500. STATIC mp_obj_t uos_mbfs_remove(mp_obj_t name) {
  501. return microbit_remove(name);
  502. }
  503. MP_DEFINE_CONST_FUN_OBJ_1(uos_mbfs_remove_obj, uos_mbfs_remove);
  504. typedef struct {
  505. mp_obj_base_t base;
  506. mp_fun_1_t iternext;
  507. uint8_t index;
  508. } uos_mbfs_ilistdir_it_t;
  509. STATIC mp_obj_t uos_mbfs_ilistdir_it_iternext(mp_obj_t self_in) {
  510. uos_mbfs_ilistdir_it_t *self = MP_OBJ_TO_PTR(self_in);
  511. // Read until the next FILE_START chunk.
  512. for (; self->index <= chunks_in_file_system; self->index++) {
  513. if (file_system_chunks[self->index].marker != FILE_START) {
  514. continue;
  515. }
  516. // Get the file name as str object.
  517. mp_obj_t name = mp_obj_new_str(&file_system_chunks[self->index].header.filename[0], file_system_chunks[self->index].header.name_len);
  518. // make 3-tuple with info about this entry
  519. mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(3, NULL));
  520. t->items[0] = name;
  521. t->items[1] = MP_OBJ_NEW_SMALL_INT(MP_S_IFREG); // all entries are files
  522. t->items[2] = MP_OBJ_NEW_SMALL_INT(0); // no inode number
  523. self->index++;
  524. return MP_OBJ_FROM_PTR(t);
  525. }
  526. return MP_OBJ_STOP_ITERATION;
  527. }
  528. STATIC mp_obj_t uos_mbfs_ilistdir(void) {
  529. uos_mbfs_ilistdir_it_t *iter = m_new_obj(uos_mbfs_ilistdir_it_t);
  530. iter->base.type = &mp_type_polymorph_iter;
  531. iter->iternext = uos_mbfs_ilistdir_it_iternext;
  532. iter->index = 1;
  533. return MP_OBJ_FROM_PTR(iter);
  534. }
  535. MP_DEFINE_CONST_FUN_OBJ_0(uos_mbfs_ilistdir_obj, uos_mbfs_ilistdir);
  536. MP_DEFINE_CONST_FUN_OBJ_0(uos_mbfs_listdir_obj, microbit_file_list);
  537. STATIC mp_obj_t microbit_file_writable(mp_obj_t self) {
  538. return mp_obj_new_bool(((file_descriptor_obj *)self)->writable);
  539. }
  540. STATIC MP_DEFINE_CONST_FUN_OBJ_1(microbit_file_writable_obj, microbit_file_writable);
  541. STATIC const mp_map_elem_t uos_mbfs_file_locals_dict_table[] = {
  542. { MP_OBJ_NEW_QSTR(MP_QSTR_close), (mp_obj_t)&uos_mbfs_file_close_obj },
  543. { MP_OBJ_NEW_QSTR(MP_QSTR_name), (mp_obj_t)&uos_mbfs_file_name_obj },
  544. //{ MP_ROM_QSTR(MP_QSTR___enter__), (mp_obj_t)&mp_identity_obj },
  545. //{ MP_ROM_QSTR(MP_QSTR___exit__), (mp_obj_t)&file___exit___obj },
  546. { MP_OBJ_NEW_QSTR(MP_QSTR_writable), (mp_obj_t)&microbit_file_writable_obj },
  547. /* Stream methods */
  548. { MP_OBJ_NEW_QSTR(MP_QSTR_read), (mp_obj_t)&mp_stream_read_obj },
  549. { MP_OBJ_NEW_QSTR(MP_QSTR_readinto), (mp_obj_t)&mp_stream_readinto_obj },
  550. { MP_OBJ_NEW_QSTR(MP_QSTR_readline), (mp_obj_t)&mp_stream_unbuffered_readline_obj},
  551. { MP_OBJ_NEW_QSTR(MP_QSTR_write), (mp_obj_t)&mp_stream_write_obj},
  552. };
  553. STATIC MP_DEFINE_CONST_DICT(uos_mbfs_file_locals_dict, uos_mbfs_file_locals_dict_table);
  554. STATIC const mp_stream_p_t textio_stream_p = {
  555. .read = microbit_file_read,
  556. .write = microbit_file_write,
  557. .is_text = true,
  558. };
  559. const mp_obj_type_t uos_mbfs_textio_type = {
  560. { &mp_type_type },
  561. .name = MP_QSTR_TextIO,
  562. .protocol = &textio_stream_p,
  563. .locals_dict = (mp_obj_dict_t*)&uos_mbfs_file_locals_dict,
  564. };
  565. STATIC const mp_stream_p_t fileio_stream_p = {
  566. .read = microbit_file_read,
  567. .write = microbit_file_write,
  568. };
  569. const mp_obj_type_t uos_mbfs_fileio_type = {
  570. { &mp_type_type },
  571. .name = MP_QSTR_FileIO,
  572. .protocol = &fileio_stream_p,
  573. .locals_dict = (mp_obj_dict_t*)&uos_mbfs_file_locals_dict,
  574. };
  575. // From micro:bit fileobj.c
  576. mp_obj_t uos_mbfs_open(size_t n_args, const mp_obj_t *args) {
  577. /// -1 means default; 0 explicitly false; 1 explicitly true.
  578. int read = -1;
  579. int text = -1;
  580. if (n_args == 2) {
  581. mp_uint_t len;
  582. const char *mode = mp_obj_str_get_data(args[1], &len);
  583. for (mp_uint_t i = 0; i < len; i++) {
  584. if (mode[i] == 'r' || mode[i] == 'w') {
  585. if (read >= 0) {
  586. goto mode_error;
  587. }
  588. read = (mode[i] == 'r');
  589. } else if (mode[i] == 'b' || mode[i] == 't') {
  590. if (text >= 0) {
  591. goto mode_error;
  592. }
  593. text = (mode[i] == 't');
  594. } else {
  595. goto mode_error;
  596. }
  597. }
  598. }
  599. mp_uint_t name_len;
  600. const char *filename = mp_obj_str_get_data(args[0], &name_len);
  601. file_descriptor_obj *res = microbit_file_open(filename, name_len, read == 0, text == 0);
  602. if (res == NULL) {
  603. mp_raise_OSError(MP_ENOENT);
  604. }
  605. return res;
  606. mode_error:
  607. mp_raise_ValueError("illegal mode");
  608. }
  609. STATIC mp_obj_t uos_mbfs_stat(mp_obj_t filename) {
  610. mp_obj_t file_size = microbit_file_size(filename);
  611. mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(10, NULL));
  612. t->items[0] = MP_OBJ_NEW_SMALL_INT(MP_S_IFREG); // st_mode
  613. t->items[1] = MP_OBJ_NEW_SMALL_INT(0); // st_ino
  614. t->items[2] = MP_OBJ_NEW_SMALL_INT(0); // st_dev
  615. t->items[3] = MP_OBJ_NEW_SMALL_INT(0); // st_nlink
  616. t->items[4] = MP_OBJ_NEW_SMALL_INT(0); // st_uid
  617. t->items[5] = MP_OBJ_NEW_SMALL_INT(0); // st_gid
  618. t->items[6] = file_size; // st_size
  619. t->items[7] = MP_OBJ_NEW_SMALL_INT(0); // st_atime
  620. t->items[8] = MP_OBJ_NEW_SMALL_INT(0); // st_mtime
  621. t->items[9] = MP_OBJ_NEW_SMALL_INT(0); // st_ctime
  622. return MP_OBJ_FROM_PTR(t);
  623. }
  624. MP_DEFINE_CONST_FUN_OBJ_1(uos_mbfs_stat_obj, uos_mbfs_stat);
  625. #endif // MICROPY_MBFS