vfs.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263
  1. /* Virtual filesystem: mount every device with a valid FAT filesystem under `/`,
  2. * and the command pack (ramfs) at /ramfs. File operations go through the
  3. * fs_ops vtable so sys_fs.c and the shell stay filesystem-agnostic. */
  4. #include "vfs.h"
  5. #include "fatdetect.h"
  6. #include "io.h"
  7. #include "kmalloc.h"
  8. #include "process.h"
  9. #include "sched.h"
  10. #include "ramfs.h"
  11. #include "rootfs.h"
  12. #include "serial.h"
  13. #include "share.h"
  14. #include "syscall.h"
  15. #include "timer.h"
  16. #include "usbmsc.h"
  17. vfs_mount_t vfs_mounts[VFS_MAX_MOUNTS];
  18. unsigned int vfs_mount_count;
  19. /* Set while vfs_mount_all() rebuilds the mount table. File syscalls wait on
  20. * this (sys_fs.c) so they never resolve a path in a half-rebuilt table or use
  21. * the FAT sector buffer while the scan is reading disks. The owner (a
  22. * `storage` syscall) is exempt. */
  23. volatile int vfs_scan_busy;
  24. volatile unsigned int vfs_scan_owner_pid;
  25. static void put_dec(unsigned int n)
  26. {
  27. char buf[12];
  28. unsigned int i = sizeof(buf) - 1;
  29. buf[i] = '\0';
  30. if (n == 0) {
  31. io_putchar('0');
  32. return;
  33. }
  34. while (n && i > 0) {
  35. buf[--i] = (char)('0' + (n % 10));
  36. n /= 10;
  37. }
  38. io_putstr(buf + i);
  39. }
  40. /* Print a size given in 512-byte sectors. Uses shifts (not bytes) so a
  41. * >4 GB filesystem/disk does not overflow 32-bit arithmetic. */
  42. static void put_size_sectors(unsigned int sectors)
  43. {
  44. if (sectors >= 2u * 1024u * 1024u) { /* >= 1 GiB */
  45. unsigned int gb = sectors >> 21;
  46. unsigned int tenths = ((sectors & 0x1FFFFFu) * 10u) >> 21;
  47. put_dec(gb);
  48. if (tenths) {
  49. io_putchar('.');
  50. put_dec(tenths);
  51. }
  52. io_putstr(" GB");
  53. } else if (sectors >= 2048u) { /* >= 1 MiB */
  54. unsigned int mb = sectors >> 11;
  55. unsigned int tenths = ((sectors & 0x7FFu) * 10u) >> 11;
  56. put_dec(mb);
  57. if (tenths) {
  58. io_putchar('.');
  59. put_dec(tenths);
  60. }
  61. io_putstr(" MB");
  62. } else {
  63. put_dec(sectors / 2u);
  64. io_putstr(" KB");
  65. }
  66. }
  67. static const char *fat_name(fat_type_t t)
  68. {
  69. switch (t) {
  70. case FAT12: return "FAT12";
  71. case FAT16: return "FAT16";
  72. case FAT32: return "FAT32";
  73. default: return "FAT";
  74. }
  75. }
  76. static void mem_copy(void *dst, const void *src, unsigned int n)
  77. {
  78. unsigned char *d = (unsigned char *)dst;
  79. const unsigned char *s = (const unsigned char *)src;
  80. while (n--)
  81. *d++ = *s++;
  82. }
  83. static void copy_bounded(char *dst, const char *src, unsigned int size)
  84. {
  85. unsigned int i = 0;
  86. while (i + 1 < size && src[i]) {
  87. dst[i] = src[i];
  88. i++;
  89. }
  90. dst[i] = '\0';
  91. }
  92. /* ---- FAT implementation ---- */
  93. static void fat_cache_clear(void);
  94. static int fat_op_read_file(struct vfs_mount *m, const char *path,
  95. void *buf, unsigned int max)
  96. {
  97. unsigned int dir;
  98. char name[12];
  99. int r = fat_resolve_path(&m->fs, path, &dir, name);
  100. if (r == -2)
  101. return VFS_EIO;
  102. if (r != 0)
  103. return VFS_ENOENT;
  104. r = fat_read_file_in(&m->fs, dir, name, buf, max);
  105. if (r == -2)
  106. return VFS_EIO;
  107. if (r < 0)
  108. return VFS_ENOENT;
  109. return r;
  110. }
  111. static int fat_op_stat(struct vfs_mount *m, const char *path,
  112. unsigned int *size, int *is_dir)
  113. {
  114. int r = fat_stat(&m->fs, path, size, is_dir);
  115. if (r == -2)
  116. return VFS_EIO;
  117. if (r != 0)
  118. return VFS_ENOENT;
  119. return 0;
  120. }
  121. static int fat_op_load_file(struct vfs_mount *m, const char *path,
  122. struct vfs_file *out)
  123. {
  124. unsigned int size = 0;
  125. int is_dir = 0, r;
  126. void *buf;
  127. r = fat_op_stat(m, path, &size, &is_dir);
  128. if (r != 0)
  129. return r;
  130. if (is_dir || size == 0 || size > VFS_FILE_MAX)
  131. return VFS_EINVAL;
  132. /* One command is loaded at a time; the ELF loader has copied the
  133. * segments out before release_file() runs, so a heap buffer is safe. */
  134. buf = kmalloc(size);
  135. if (!buf)
  136. return VFS_EINVAL;
  137. r = fat_op_read_file(m, path, buf, size);
  138. if (r < 0) {
  139. kfree(buf);
  140. return r;
  141. }
  142. out->data = buf;
  143. out->size = (unsigned int)r;
  144. return 0;
  145. }
  146. static void fat_op_release_file(struct vfs_mount *m, struct vfs_file *f)
  147. {
  148. (void)m;
  149. kfree((void *)f->data);
  150. f->data = 0;
  151. f->size = 0;
  152. }
  153. static int fat_op_write_file(struct vfs_mount *m, const char *path,
  154. const void *buf, unsigned int len)
  155. {
  156. fat_cache_clear();
  157. unsigned int dir;
  158. char name[12];
  159. int r = fat_resolve_path(&m->fs, path, &dir, name);
  160. if (r == -2)
  161. return VFS_EIO;
  162. if (r != 0)
  163. return VFS_EINVAL;
  164. r = fat_write_file_in(&m->fs, dir, name, buf, len);
  165. if (r == -2)
  166. return VFS_EIO;
  167. if (r != 0)
  168. return VFS_EINVAL;
  169. return 0;
  170. }
  171. static int fat_op_unlink(struct vfs_mount *m, const char *path)
  172. {
  173. fat_cache_clear();
  174. unsigned int dir;
  175. char name[12];
  176. int r = fat_resolve_path(&m->fs, path, &dir, name);
  177. if (r == -2)
  178. return VFS_EIO;
  179. if (r != 0)
  180. return VFS_ENOENT;
  181. r = fat_delete_file_in(&m->fs, dir, name);
  182. if (r == -2)
  183. return VFS_EIO;
  184. if (r != 0)
  185. return VFS_ENOENT;
  186. return 0;
  187. }
  188. static int fat_op_mkdir(struct vfs_mount *m, const char *path)
  189. {
  190. fat_cache_clear();
  191. unsigned int dir;
  192. char name[12];
  193. int r = fat_resolve_path(&m->fs, path, &dir, name);
  194. if (r == -2)
  195. return VFS_EIO;
  196. if (r != 0)
  197. return VFS_EINVAL;
  198. r = fat_mkdir(&m->fs, dir, name);
  199. if (r == -2)
  200. return VFS_EIO;
  201. if (r != 0)
  202. return VFS_EINVAL; /* the FAT layer printed its own reason */
  203. return 0;
  204. }
  205. static int fat_op_rmdir(struct vfs_mount *m, const char *path)
  206. {
  207. fat_cache_clear();
  208. unsigned int dir;
  209. char name[12];
  210. int r = fat_resolve_path(&m->fs, path, &dir, name);
  211. if (r == -2)
  212. return VFS_EIO;
  213. if (r != 0)
  214. return VFS_EINVAL;
  215. r = fat_rmdir(&m->fs, dir, name);
  216. if (r == -2)
  217. return VFS_EIO;
  218. if (r != 0)
  219. return VFS_EINVAL;
  220. return 0;
  221. }
  222. /* Index-based listing used to re-walk the FAT directory for every index, and
  223. * the WebDAV server asks for 0..N-1 in one PROPFIND. Cache the first 24
  224. * entries of the last directory so a listing is one directory walk; a write
  225. * invalidates it. Larger directories fall back to the old walk past 24. */
  226. #define FAT_LIST_CACHE 24
  227. static struct vfs_dirent fat_cache[FAT_LIST_CACHE];
  228. static unsigned int fat_cache_count;
  229. static int fat_cache_valid;
  230. static int fat_cache_truncated;
  231. static struct vfs_mount *fat_cache_mount;
  232. static char fat_cache_path[VFS_PATH_MAX];
  233. static void fat_cache_clear(void)
  234. {
  235. fat_cache_valid = 0;
  236. }
  237. static int fat_path_eq(const char *a, const char *b)
  238. {
  239. unsigned int i = 0;
  240. while (a[i] && b[i] && a[i] == b[i])
  241. i++;
  242. return a[i] == '\0' && b[i] == '\0';
  243. }
  244. static void fat_cache_cb(const char *name, int is_dir, unsigned int size)
  245. {
  246. struct vfs_dirent *d;
  247. unsigned int i;
  248. if (fat_cache_count >= FAT_LIST_CACHE) {
  249. fat_cache_truncated = 1;
  250. return;
  251. }
  252. d = &fat_cache[fat_cache_count++];
  253. for (i = 0; i < 11; i++)
  254. d->name[i] = name[i] ? name[i] : ' ';
  255. d->name[11] = '\0';
  256. d->size = size;
  257. d->is_dir = (unsigned int)is_dir;
  258. d->raw_83 = 1;
  259. }
  260. /* Old behaviour: one directory walk, counting to `index`. */
  261. static unsigned int fat_list_index;
  262. static struct vfs_dirent *fat_list_out;
  263. static int fat_list_done;
  264. static unsigned int fat_list_seen;
  265. static void fat_list_cb(const char *name, int is_dir, unsigned int size)
  266. {
  267. unsigned int i;
  268. if (fat_list_done || fat_list_seen < fat_list_index) {
  269. fat_list_seen++;
  270. return;
  271. }
  272. for (i = 0; i < 11; i++)
  273. fat_list_out->name[i] = name[i] ? name[i] : ' ';
  274. fat_list_out->name[11] = '\0';
  275. fat_list_out->size = size;
  276. fat_list_out->is_dir = (unsigned int)is_dir;
  277. fat_list_out->raw_83 = 1;
  278. fat_list_done = 1;
  279. fat_list_seen++;
  280. }
  281. static int fat_list_walk(struct vfs_mount *m, const char *path,
  282. unsigned int index, struct vfs_dirent *out)
  283. {
  284. unsigned int dir;
  285. int r = fat_resolve_dir(&m->fs, path, &dir);
  286. if (r == -2)
  287. return VFS_EIO;
  288. if (r != 0)
  289. return VFS_ENOENT;
  290. fat_list_index = index;
  291. fat_list_seen = 0;
  292. fat_list_out = out;
  293. fat_list_done = 0;
  294. r = fat_list_dir(&m->fs, dir, fat_list_cb);
  295. if (r == -2)
  296. return VFS_EIO;
  297. return fat_list_done ? 1 : 0;
  298. }
  299. static int fat_op_list_dir(struct vfs_mount *m, const char *path,
  300. unsigned int index, struct vfs_dirent *out)
  301. {
  302. int same = fat_cache_valid && fat_cache_mount == m &&
  303. fat_path_eq(fat_cache_path, path);
  304. if (same) {
  305. if (index < fat_cache_count) {
  306. *out = fat_cache[index];
  307. return 1;
  308. }
  309. if (!fat_cache_truncated)
  310. return 0;
  311. /* Beyond the cached prefix: use the old walk. */
  312. return fat_list_walk(m, path, index, out);
  313. }
  314. {
  315. unsigned int dir;
  316. int r = fat_resolve_dir(&m->fs, path, &dir);
  317. if (r == -2)
  318. return VFS_EIO;
  319. if (r != 0)
  320. return VFS_ENOENT; /* not found / not a directory */
  321. fat_cache_count = 0;
  322. fat_cache_truncated = 0;
  323. fat_cache_mount = m;
  324. copy_bounded(fat_cache_path, path, sizeof(fat_cache_path));
  325. fat_cache_valid = 0;
  326. r = fat_list_dir(&m->fs, dir, fat_cache_cb);
  327. if (r == -2)
  328. return VFS_EIO;
  329. fat_cache_valid = 1;
  330. }
  331. if (index < fat_cache_count) {
  332. *out = fat_cache[index];
  333. return 1;
  334. }
  335. return 0;
  336. }
  337. static const struct fs_ops fat_ops = {
  338. .raw_83 = 1,
  339. .read_file = fat_op_read_file,
  340. .load_file = fat_op_load_file,
  341. .release_file = fat_op_release_file,
  342. .stat = fat_op_stat,
  343. .write_file = fat_op_write_file,
  344. .unlink = fat_op_unlink,
  345. .mkdir = fat_op_mkdir,
  346. .rmdir = fat_op_rmdir,
  347. .list_dir = fat_op_list_dir,
  348. };
  349. /* 1 for a block-device-backed mount (the FAT driver). A destructive format
  350. * rewrites such a mount; RAM-backed mounts stay usable while it runs. */
  351. int vfs_mount_is_disk(const vfs_mount_t *m)
  352. {
  353. return m && m->ops == &fat_ops;
  354. }
  355. void vfs_scan_poll(unsigned int t0)
  356. {
  357. if (vfs_scan_busy && vfs_scan_owner_pid && get_ticks_ms() - t0 >= 2)
  358. vfs_scan_relax(1);
  359. }
  360. int vfs_scan_relax(unsigned int ms)
  361. {
  362. struct process *p;
  363. if (!vfs_scan_busy || !vfs_scan_owner_pid)
  364. return 0;
  365. p = current_proc();
  366. if (!p || p->pid != vfs_scan_owner_pid || this_cpu()->preempt_count)
  367. return 0;
  368. {
  369. unsigned int flags;
  370. __asm__ volatile ("pushfl; popl %0" : "=r"(flags));
  371. if (!(flags & 0x200u))
  372. return 0; /* interrupts off: cannot sleep */
  373. }
  374. process_sleep_ms(ms ? ms : 1);
  375. return 1;
  376. }
  377. /* ---- ramfs implementation ---- */
  378. static int ramfs_op_read_file(struct vfs_mount *m, const char *path,
  379. void *buf, unsigned int max)
  380. {
  381. const void *data;
  382. unsigned int size = 0, n;
  383. (void)m;
  384. data = ramfs_lookup(path, &size);
  385. if (!data)
  386. return VFS_ENOENT;
  387. n = size > max ? max : size;
  388. mem_copy(buf, data, n);
  389. return (int)n;
  390. }
  391. static int ramfs_op_load_file(struct vfs_mount *m, const char *path,
  392. struct vfs_file *out)
  393. {
  394. const void *data;
  395. unsigned int size = 0;
  396. (void)m;
  397. data = ramfs_lookup(path, &size);
  398. if (!data)
  399. return VFS_ENOENT;
  400. out->data = data;
  401. out->size = size;
  402. return 0;
  403. }
  404. static void ramfs_op_release_file(struct vfs_mount *m, struct vfs_file *f)
  405. {
  406. (void)m;
  407. (void)f;
  408. }
  409. static int ramfs_op_stat(struct vfs_mount *m, const char *path,
  410. unsigned int *size, int *is_dir)
  411. {
  412. const void *data;
  413. unsigned int sz = 0;
  414. struct ramfs_dirent e;
  415. (void)m;
  416. if (path[0] == '\0') {
  417. if (size)
  418. *size = 0;
  419. if (is_dir)
  420. *is_dir = 1;
  421. return 0;
  422. }
  423. data = ramfs_lookup(path, &sz);
  424. if (data) {
  425. if (size)
  426. *size = sz;
  427. if (is_dir)
  428. *is_dir = 0;
  429. return 0;
  430. }
  431. if (ramfs_list(path, 0, &e) == 1) {
  432. if (size)
  433. *size = 0;
  434. if (is_dir)
  435. *is_dir = 1;
  436. return 0;
  437. }
  438. return VFS_ENOENT;
  439. }
  440. static int ramfs_op_ro(struct vfs_mount *m, const char *path)
  441. {
  442. (void)m;
  443. (void)path;
  444. return VFS_EROFS;
  445. }
  446. static int ramfs_op_write_file(struct vfs_mount *m, const char *path,
  447. const void *buf, unsigned int len)
  448. {
  449. (void)buf;
  450. (void)len;
  451. return ramfs_op_ro(m, path);
  452. }
  453. static int ramfs_op_unlink(struct vfs_mount *m, const char *path)
  454. {
  455. return ramfs_op_ro(m, path);
  456. }
  457. static int ramfs_op_mkdir(struct vfs_mount *m, const char *path)
  458. {
  459. return ramfs_op_ro(m, path);
  460. }
  461. static int ramfs_op_rmdir(struct vfs_mount *m, const char *path)
  462. {
  463. return ramfs_op_ro(m, path);
  464. }
  465. static int ramfs_op_list_dir(struct vfs_mount *m, const char *path,
  466. unsigned int index, struct vfs_dirent *out)
  467. {
  468. struct ramfs_dirent e;
  469. (void)m;
  470. if (ramfs_list(path, index, &e) != 1) {
  471. if (index == 0 && path[0] != '\0') {
  472. unsigned int size = 0;
  473. /* Nothing matched: report a missing path instead of an
  474. * empty directory (the pack has no empty directories --
  475. * one exists exactly as a prefix of file paths), and a
  476. * file as "not a directory". */
  477. if (ramfs_lookup(path, &size))
  478. return VFS_EINVAL;
  479. return VFS_ENOENT;
  480. }
  481. return 0; /* end of directory */
  482. }
  483. copy_bounded(out->name, e.name, sizeof(out->name));
  484. out->size = e.size;
  485. out->is_dir = e.is_dir;
  486. out->raw_83 = 0;
  487. return 1;
  488. }
  489. static const struct fs_ops ramfs_ops = {
  490. .raw_83 = 0,
  491. .read_file = ramfs_op_read_file,
  492. .load_file = ramfs_op_load_file,
  493. .release_file = ramfs_op_release_file,
  494. .stat = ramfs_op_stat,
  495. .write_file = ramfs_op_write_file,
  496. .unlink = ramfs_op_unlink,
  497. .mkdir = ramfs_op_mkdir,
  498. .rmdir = ramfs_op_rmdir,
  499. .list_dir = ramfs_op_list_dir,
  500. };
  501. int vfs_mount_is_ram(const vfs_mount_t *m)
  502. {
  503. return m && (m->ops == &ramfs_ops || m->ops == &share_ops);
  504. }
  505. /* ---- mounts ---- */
  506. static vfs_mount_t *mount_slot(const char *name, const char *dev)
  507. {
  508. unsigned int i;
  509. if (vfs_mount_count >= VFS_MAX_MOUNTS)
  510. return 0;
  511. {
  512. vfs_mount_t *m = &vfs_mounts[vfs_mount_count];
  513. for (i = 0; name[i] && i < sizeof(m->name) - 1; i++)
  514. m->name[i] = name[i];
  515. m->name[i] = '\0';
  516. m->dev[0] = '\0';
  517. if (dev) {
  518. for (i = 0; dev[i] && i < sizeof(m->dev) - 1; i++)
  519. m->dev[i] = dev[i];
  520. m->dev[i] = '\0';
  521. }
  522. m->key[0] = '\0';
  523. m->mounted = 1;
  524. return m;
  525. }
  526. }
  527. static int name_eq(const char *a, const char *b)
  528. {
  529. unsigned int i = 0;
  530. while (a[i] && b[i] && a[i] == b[i])
  531. i++;
  532. return a[i] == '\0' && b[i] == '\0';
  533. }
  534. static unsigned int mbr_dword(const unsigned char *p)
  535. {
  536. return (unsigned int)p[0] | ((unsigned int)p[1] << 8) |
  537. ((unsigned int)p[2] << 16) | ((unsigned int)p[3] << 24);
  538. }
  539. /* The NebOS MBR (boot/mbr.S) has a fixed layout: an active, FAT16-looking
  540. * P1 at LBA 63 with 1024 sectors (boot sector + kernel) and P2 at LBA 1088
  541. * (the rootfs partition, or the dummy entry that keeps the AT3IONT-I BIOS
  542. * from classifying the stick as USB-ZIP). Recognising that layout lets the
  543. * boot stick skip the P1 content read, which is one USB round trip on every
  544. * boot. Anything that does not match exactly still goes through the
  545. * content check below. */
  546. static int mbr_is_nebos_layout(const unsigned char *mbr)
  547. {
  548. const unsigned char *p1 = mbr + 0x1BE;
  549. const unsigned char *p2 = p1 + 16;
  550. if (mbr[0] != 0xEB || mbr[1] != 0x42 || mbr[2] != 0x90)
  551. return 0;
  552. if (p1[0] != 0x80 || p1[4] != 0x06)
  553. return 0;
  554. if (mbr_dword(p1 + 8) != 63 || mbr_dword(p1 + 12) != 1024)
  555. return 0;
  556. return mbr_dword(p2 + 8) == 1088;
  557. }
  558. /* The P1 boot sector keeps only the NebOS header fields; a real FAT volume
  559. * never carries these values. Older installed media still has the original
  560. * FAT-looking BPB (before boot.S was changed), so skip it explicitly instead
  561. * of mounting the raw kernel partition as a fake filesystem.
  562. * Returns 1 = NebOS boot partition, 0 = not, -1 = the sector could not be
  563. * read (skip it either way; a failed probe must not turn into a mount). */
  564. static int is_nebos_boot_partition(blk_dev_t *d, unsigned int start_lba)
  565. {
  566. unsigned char s[512];
  567. unsigned int i;
  568. if (blk_read(d, start_lba, s, 1) != 0)
  569. return -1;
  570. if (s[510] != 0x55 || s[511] != 0xAA)
  571. return 0;
  572. if (s[3] != 'N' || s[4] != 'E' || s[5] != 'B' || s[6] != 'O' ||
  573. s[7] != 'S')
  574. return 0;
  575. if ((s[0x40] | (s[0x41] << 8)) != NEBOS_KERNEL_LBA)
  576. return 0;
  577. i = s[0x42] | (s[0x43] << 8) | (s[0x44] << 16) |
  578. ((unsigned int)s[0x45] << 24);
  579. return i == ROOTFS_LBA ? 1 : 0;
  580. }
  581. /* ---- mount policies ---- */
  582. static struct sys_mount_policy vfs_policies[SYS_MOUNT_POLICY_MAX];
  583. static unsigned int vfs_policy_count;
  584. static struct sys_mount_policy *policy_find(const char *key)
  585. {
  586. unsigned int i;
  587. for (i = 0; i < vfs_policy_count; i++)
  588. if (name_eq(vfs_policies[i].key, key))
  589. return &vfs_policies[i];
  590. return 0;
  591. }
  592. /* A mount name or key: 1..15 of [a-z0-9_-]. */
  593. static int policy_name_ok(const char *s)
  594. {
  595. unsigned int i;
  596. for (i = 0; s[i]; i++) {
  597. char c = s[i];
  598. if (i >= 15 || !((c >= 'a' && c <= 'z') ||
  599. (c >= '0' && c <= '9') || c == '_' || c == '-'))
  600. return 0;
  601. }
  602. return i > 0;
  603. }
  604. /* The block device a policy key names: "<dev>" or "<dev>p<1-4>". */
  605. static blk_dev_t *policy_device(const char *key)
  606. {
  607. unsigned int i, n;
  608. for (i = 0; i < blk_dev_count; i++) {
  609. const char *d = blk_devs[i].name;
  610. for (n = 0; d[n] && key[n] == d[n]; n++)
  611. ;
  612. if (d[n])
  613. continue;
  614. if (key[n] == '\0' ||
  615. (key[n] == 'p' && key[n + 1] >= '1' && key[n + 1] <= '4' &&
  616. key[n + 2] == '\0'))
  617. return &blk_devs[i];
  618. }
  619. return 0;
  620. }
  621. static void remount(blk_dev_t *d);
  622. int vfs_policy_set(const struct sys_mount_policy *p)
  623. {
  624. struct sys_mount_policy *e;
  625. blk_dev_t *d;
  626. if (!policy_name_ok(p->key) || p->name[sizeof(p->name) - 1] ||
  627. (p->name[0] && (!policy_name_ok(p->name) ||
  628. name_eq(p->name, "ramfs") ||
  629. name_eq(p->name, "share"))) ||
  630. (p->flags & ~(SYS_MOUNT_NOAUTO | SYS_MOUNT_RO)))
  631. return -1;
  632. d = policy_device(p->key);
  633. if (!d)
  634. return -1;
  635. e = policy_find(p->key);
  636. if (!p->name[0] && !p->flags) {
  637. if (e)
  638. *e = vfs_policies[--vfs_policy_count];
  639. } else {
  640. if (!e) {
  641. if (vfs_policy_count >= SYS_MOUNT_POLICY_MAX)
  642. return -1;
  643. e = &vfs_policies[vfs_policy_count++];
  644. }
  645. *e = *p;
  646. }
  647. remount(d);
  648. return 0;
  649. }
  650. int vfs_policy_get(unsigned int index, struct sys_mount_policy *out)
  651. {
  652. if (index >= vfs_policy_count)
  653. return 0;
  654. *out = vfs_policies[index];
  655. return 1;
  656. }
  657. static void mount_one(blk_dev_t *dev, unsigned int part_lba, const char *name)
  658. {
  659. fat_fs_t fs;
  660. vfs_mount_t *m;
  661. const struct sys_mount_policy *pol = policy_find(name);
  662. const char *use_name = name;
  663. int r;
  664. if (pol && (pol->flags & SYS_MOUNT_NOAUTO))
  665. return;
  666. fat_cache_clear();
  667. r = fat_mount(dev, part_lba, &fs);
  668. if (r != 0) {
  669. serial_puts(SERIAL_COM1, "vfs: ");
  670. serial_puts(SERIAL_COM1, name);
  671. serial_puts(SERIAL_COM1, " no FAT\n");
  672. return;
  673. }
  674. /* The labelled data partition has a stable path independent of the
  675. * device name. Only the first NEBOSDATA volume becomes /data. */
  676. if (name_eq(fs.label, "NEBOSDATA") && !vfs_find("data"))
  677. use_name = "data";
  678. /* A policy name wins unless another volume already holds it. */
  679. if (pol && pol->name[0] && !vfs_find(pol->name))
  680. use_name = pol->name;
  681. m = mount_slot(use_name, dev->name);
  682. if (!m)
  683. return;
  684. copy_bounded(m->key, name, sizeof(m->key));
  685. m->ops = &fat_ops;
  686. m->fs = fs;
  687. m->ro = (pol && (pol->flags & SYS_MOUNT_RO)) ? 1 : 0;
  688. m->total_sectors = fs.total_sectors;
  689. copy_bounded(m->type, fat_name(fs.type), sizeof(m->type));
  690. copy_bounded(m->label, fs.label, sizeof(m->label));
  691. vfs_mount_count++;
  692. serial_puts(SERIAL_COM1, "vfs: ");
  693. serial_puts(SERIAL_COM1, use_name);
  694. serial_puts(SERIAL_COM1, " ");
  695. serial_puts(SERIAL_COM1, fat_name(fs.type));
  696. serial_puts(SERIAL_COM1, "\n");
  697. }
  698. void vfs_mount_ramfs(void)
  699. {
  700. vfs_mount_t *m;
  701. /* Mount even when the pack is empty: the runtime `cmds-dev` path can
  702. * install a pack later and vfs_refresh_ramfs() then updates the mount. */
  703. m = mount_slot("ramfs", 0);
  704. if (!m)
  705. return;
  706. m->ops = &ramfs_ops;
  707. m->ro = 1;
  708. m->total_sectors = (ramfs_pack_size() + 511u) / 512u;
  709. copy_bounded(m->type, "ramfs", sizeof(m->type));
  710. m->label[0] = '\0';
  711. vfs_mount_count++;
  712. }
  713. void vfs_refresh_ramfs(void)
  714. {
  715. vfs_mount_t *m = vfs_find("ramfs");
  716. if (m)
  717. m->total_sectors = (ramfs_pack_size() + 511u) / 512u;
  718. }
  719. /* Writable RAM store for network uploads. Unlike the pack ramfs it is not a
  720. * block device and its contents survive vfs_mount_all() refreshes. */
  721. void vfs_mount_share(void)
  722. {
  723. vfs_mount_t *m;
  724. if (share_init() != 0)
  725. return;
  726. m = mount_slot("share", 0);
  727. if (!m)
  728. return;
  729. m->ops = &share_ops;
  730. m->ro = 0;
  731. m->total_sectors = SHARE_MAX_BYTES / 512u;
  732. copy_bounded(m->type, "share", sizeof(m->type));
  733. m->label[0] = '\0';
  734. vfs_mount_count++;
  735. }
  736. static void part_name(char *out, const char *base, unsigned int idx)
  737. {
  738. unsigned int i = 0;
  739. while (base[i] && i < 13) {
  740. out[i] = base[i];
  741. i++;
  742. }
  743. out[i++] = 'p';
  744. out[i++] = (char)('0' + idx + 1);
  745. out[i] = '\0';
  746. }
  747. static void mount_device(blk_dev_t *d, unsigned char *buf)
  748. {
  749. mbr_part_t parts[4];
  750. fat_info_t fi;
  751. int n, j, neb_mbr;
  752. int any_fat = 0;
  753. if (!d->present)
  754. return;
  755. if (d->kind == BLK_FLOPPY) {
  756. mount_one(d, 0, d->name);
  757. return;
  758. }
  759. /* IDE/SATA/USB: MBR -> FAT partitions, else superfloppy. */
  760. if (blk_read(d, 0, buf, 1) != 0)
  761. return;
  762. n = mbr_parse(buf, parts);
  763. neb_mbr = mbr_is_nebos_layout(buf);
  764. for (j = 0; j < n; j++) {
  765. char pname[16];
  766. int neb;
  767. if (!parts[j].is_fat || parts[j].start_lba == 0 ||
  768. parts[j].sectors == 0)
  769. continue;
  770. /* The NebOS boot stick's P1 is the raw boot sector + kernel,
  771. * not a filesystem; the layout check above avoids a USB read
  772. * of it on every boot, the content check is the fallback. */
  773. if (neb_mbr && parts[j].index == 0)
  774. continue;
  775. neb = is_nebos_boot_partition(d, parts[j].start_lba);
  776. if (neb != 0) /* NebOS or unreadable: not FAT */
  777. continue;
  778. any_fat = 1;
  779. part_name(pname, d->name, parts[j].index);
  780. mount_one(d, parts[j].start_lba, pname);
  781. }
  782. /* A valid MBR whose partitions are all non-FAT (the NebOS boot stick)
  783. * is not a superfloppy; only probe one when the sector has no MBR
  784. * entries, or still looks like a FAT boot sector with stray partition
  785. * bytes (a formatted superfloppy). */
  786. if (!any_fat && (n == 0 || fat_detect(buf, &fi) == 0))
  787. mount_one(d, 0, d->name);
  788. }
  789. /* Mounts remember their source device, so /data can be named independently
  790. * of <device>pN and still be found by a remount. */
  791. static int mount_belongs_to(const vfs_mount_t *m, const char *dev)
  792. {
  793. return m->dev[0] != '\0' && name_eq(m->dev, dev);
  794. }
  795. static void unmount_device(const char *dev)
  796. {
  797. unsigned int i, j;
  798. for (i = 0; i < vfs_mount_count; ) {
  799. if (mount_belongs_to(&vfs_mounts[i], dev)) {
  800. for (j = i + 1; j < vfs_mount_count; j++)
  801. vfs_mounts[j - 1] = vfs_mounts[j];
  802. vfs_mount_count--;
  803. } else {
  804. i++;
  805. }
  806. }
  807. }
  808. void vfs_mount_all(int skip_usb)
  809. {
  810. static unsigned char buf[512];
  811. unsigned int i;
  812. /* Publish the scan before touching the table: a ring-3 file syscall
  813. * on another CPU waits for it instead of resolving a path halfway
  814. * through the rebuild. At boot current_proc() is idle (pid 0). */
  815. vfs_scan_owner_pid = current_proc() ? current_proc()->pid : 0;
  816. vfs_scan_busy = 1;
  817. vfs_mount_count = 0;
  818. /* Non-block filesystems first. The WM and terminal live on /ramfs,
  819. * so they stay usable while the (possibly slow) disk scan runs. */
  820. vfs_mount_ramfs();
  821. vfs_mount_share();
  822. blk_scan();
  823. if (!skip_usb) {
  824. /* A user-initiated `storage` rescan gets the full USB recovery
  825. * path, including the long first-read deadline. */
  826. usbmsc_set_probe_only(0);
  827. usbmsc_begin_full_scan();
  828. }
  829. for (i = 0; i < blk_dev_count; i++) {
  830. unsigned int t0;
  831. if (skip_usb && blk_devs[i].kind == BLK_USB) {
  832. serial_puts(SERIAL_COM1, "vfs: ");
  833. serial_puts(SERIAL_COM1, blk_devs[i].name);
  834. serial_puts(SERIAL_COM1, " deferred\n");
  835. continue;
  836. }
  837. t0 = get_ticks_ms();
  838. mount_device(&blk_devs[i], buf);
  839. serial_puts(SERIAL_COM1, "vfs: ");
  840. serial_puts(SERIAL_COM1, blk_devs[i].name);
  841. serial_puts(SERIAL_COM1, " mount ");
  842. serial_putdec(SERIAL_COM1, get_ticks_ms() - t0);
  843. serial_puts(SERIAL_COM1, " ms\n");
  844. }
  845. serial_puts(SERIAL_COM1, "vfs: ");
  846. serial_putdec(SERIAL_COM1, vfs_mount_count);
  847. serial_puts(SERIAL_COM1, " mount(s)\n");
  848. vfs_scan_busy = 0;
  849. }
  850. static int device_is_mounted(const char *dev)
  851. {
  852. unsigned int i;
  853. for (i = 0; i < vfs_mount_count; i++)
  854. if (mount_belongs_to(&vfs_mounts[i], dev))
  855. return 1;
  856. return 0;
  857. }
  858. /* Mount the USB disks the boot scan deferred. A card reader or a stick
  859. * can NAK its first media read for several seconds while it initialises, so
  860. * this runs after the bootscan window has closed: the desktop, /ramfs,
  861. * /share and the network are already usable while it waits. */
  862. void vfs_mount_usb(void)
  863. {
  864. static unsigned char buf[512];
  865. unsigned int i;
  866. int any = 0;
  867. vfs_scan_owner_pid = current_proc() ? current_proc()->pid : 0;
  868. vfs_scan_busy = 1;
  869. usbmsc_set_probe_only(1);
  870. for (i = 0; i < blk_dev_count; i++) {
  871. blk_dev_t *d = &blk_devs[i];
  872. unsigned int t0;
  873. if (d->kind != BLK_USB || !d->present ||
  874. device_is_mounted(d->name))
  875. continue;
  876. t0 = get_ticks_ms();
  877. mount_device(d, buf);
  878. serial_puts(SERIAL_COM1, "vfs: ");
  879. serial_puts(SERIAL_COM1, d->name);
  880. serial_puts(SERIAL_COM1, " mount ");
  881. serial_putdec(SERIAL_COM1, get_ticks_ms() - t0);
  882. serial_puts(SERIAL_COM1, " ms\n");
  883. any = 1;
  884. }
  885. usbmsc_set_probe_only(0);
  886. if (any)
  887. serial_puts(SERIAL_COM1, "vfs: usb storage scanned\n");
  888. vfs_scan_busy = 0;
  889. }
  890. static void remount(blk_dev_t *d)
  891. {
  892. unsigned char buf[512];
  893. fat_cache_clear();
  894. unmount_device(d->name);
  895. mount_device(d, buf);
  896. }
  897. void vfs_remount_device(blk_dev_t *d)
  898. {
  899. if (!d)
  900. return;
  901. remount(d);
  902. if (!device_is_mounted(d->name)) {
  903. io_putstr("vfs: remount of ");
  904. io_putstr(d->name);
  905. io_putstr(" found no filesystem\n");
  906. }
  907. }
  908. vfs_mount_t *vfs_find(const char *name)
  909. {
  910. unsigned int i;
  911. for (i = 0; i < vfs_mount_count; i++) {
  912. unsigned int j = 0;
  913. while (vfs_mounts[i].name[j] && name[j] &&
  914. vfs_mounts[i].name[j] == name[j])
  915. j++;
  916. if (vfs_mounts[i].name[j] == '\0' && name[j] == '\0')
  917. return &vfs_mounts[i];
  918. }
  919. return 0;
  920. }
  921. void vfs_list_root(void)
  922. {
  923. unsigned int i;
  924. for (i = 0; i < vfs_mount_count; i++) {
  925. vfs_mount_t *m = &vfs_mounts[i];
  926. io_putstr(m->name);
  927. io_putstr(" ");
  928. io_putstr(m->type);
  929. io_putchar(' ');
  930. put_size_sectors(m->total_sectors);
  931. if (m->label[0]) {
  932. io_putstr(" \"");
  933. io_putstr(m->label);
  934. io_putchar('"');
  935. }
  936. if (m->ro)
  937. io_putstr(" (ro)");
  938. io_putchar('\n');
  939. }
  940. }
  941. int vfs_resolve(const char *path, struct vfs_mount **m_out,
  942. char *rel_path, unsigned int rel_size)
  943. {
  944. unsigned int i = 0, j = 0, o = 0;
  945. char name[16];
  946. vfs_mount_t *m;
  947. if (!path || path[0] != '/' || rel_size == 0)
  948. return -1;
  949. i = 1;
  950. while (path[i] && path[i] != '/' && j < sizeof(name) - 1)
  951. name[j++] = path[i++];
  952. name[j] = '\0';
  953. if (j == 0)
  954. return -1; /* path is just "/" */
  955. m = vfs_find(name);
  956. if (!m)
  957. return -1;
  958. /* Copy the remainder as a normalized relative path: skip slashes, drop
  959. * "." components and never leave a trailing one. */
  960. while (path[i]) {
  961. unsigned int clen = 0;
  962. const char *comp;
  963. while (path[i] == '/')
  964. i++;
  965. if (!path[i])
  966. break;
  967. comp = path + i;
  968. while (path[i] && path[i] != '/') {
  969. i++;
  970. clen++;
  971. }
  972. if (clen == 1 && comp[0] == '.')
  973. continue;
  974. if (o + clen + 2 > rel_size)
  975. return -1;
  976. if (o > 0)
  977. rel_path[o++] = '/';
  978. {
  979. unsigned int k;
  980. for (k = 0; k < clen; k++)
  981. rel_path[o++] = comp[k];
  982. }
  983. }
  984. rel_path[o] = '\0';
  985. *m_out = m;
  986. return 0;
  987. }
  988. int vfs_load_file(const char *path, struct vfs_file *out,
  989. struct vfs_mount **m_out)
  990. {
  991. struct vfs_mount *m;
  992. char rel[VFS_PATH_MAX];
  993. int r;
  994. if (vfs_resolve(path, &m, rel, sizeof(rel)) != 0)
  995. return VFS_ENOENT;
  996. r = m->ops->load_file(m, rel, out);
  997. if (r == 0 && m_out)
  998. *m_out = m;
  999. return r;
  1000. }
  1001. void vfs_release_file(struct vfs_mount *m, struct vfs_file *f)
  1002. {
  1003. if (m && m->ops->release_file)
  1004. m->ops->release_file(m, f);
  1005. }
  1006. int vfs_stat(const char *path, unsigned int *size, int *is_dir)
  1007. {
  1008. struct vfs_mount *m;
  1009. char rel[VFS_PATH_MAX];
  1010. if (vfs_resolve(path, &m, rel, sizeof(rel)) != 0)
  1011. return VFS_ENOENT;
  1012. return m->ops->stat(m, rel, size, is_dir);
  1013. }
  1014. int vfs_list_dir(const char *path, unsigned int index, struct vfs_dirent *out)
  1015. {
  1016. struct vfs_mount *m;
  1017. char rel[VFS_PATH_MAX];
  1018. if (vfs_resolve(path, &m, rel, sizeof(rel)) != 0)
  1019. return VFS_ENOENT;
  1020. return m->ops->list_dir(m, rel, index, out);
  1021. }
  1022. int vfs_join_path(const char *cwd, const char *arg, char *out, unsigned int out_size)
  1023. {
  1024. char buf[VFS_PATH_MAX * 2 + 2];
  1025. unsigned int bi = 0;
  1026. unsigned int len;
  1027. const char *p;
  1028. /* Build the combined (un-normalized) path. */
  1029. if (arg[0] == '/') {
  1030. p = arg;
  1031. } else {
  1032. p = cwd;
  1033. while (*p && bi < sizeof(buf) - 1)
  1034. buf[bi++] = *p++;
  1035. if (bi > 0 && buf[bi - 1] != '/') {
  1036. if (bi < sizeof(buf) - 1)
  1037. buf[bi++] = '/';
  1038. }
  1039. p = arg;
  1040. }
  1041. while (*p && bi < sizeof(buf) - 1)
  1042. buf[bi++] = *p++;
  1043. buf[bi] = '\0';
  1044. if (out_size < 2)
  1045. return -1;
  1046. /* Normalize into out (always begins with "/"). */
  1047. out[0] = '/';
  1048. out[1] = '\0';
  1049. len = 1;
  1050. p = buf;
  1051. while (*p) {
  1052. unsigned int clen = 0;
  1053. const char *comp;
  1054. while (*p == '/')
  1055. p++;
  1056. if (!*p)
  1057. break;
  1058. comp = p;
  1059. while (*p && *p != '/') {
  1060. p++;
  1061. clen++;
  1062. }
  1063. if (clen == 1 && comp[0] == '.')
  1064. continue;
  1065. if (clen == 2 && comp[0] == '.' && comp[1] == '.') {
  1066. /* Pop the previous component (may leave "/"). */
  1067. while (len > 1 && out[len - 1] != '/')
  1068. len--;
  1069. if (len > 1)
  1070. len--;
  1071. out[len] = '\0';
  1072. continue;
  1073. }
  1074. /* Append "/component". */
  1075. if (len + 1 + clen + 1 > out_size)
  1076. return -1;
  1077. if (len > 1)
  1078. out[len++] = '/';
  1079. {
  1080. unsigned int k;
  1081. for (k = 0; k < clen; k++)
  1082. out[len++] = comp[k];
  1083. }
  1084. out[len] = '\0';
  1085. }
  1086. return 0;
  1087. }
  1088. int vfs_device_known(const char *path)
  1089. {
  1090. unsigned int i = 0, j = 0;
  1091. char name[16];
  1092. if (!path || path[0] != '/')
  1093. return 0;
  1094. i = 1;
  1095. while (path[i] && path[i] != '/' && j < sizeof(name) - 1)
  1096. name[j++] = path[i++];
  1097. name[j] = '\0';
  1098. if (j == 0)
  1099. return 0;
  1100. return blk_find(name) != 0;
  1101. }