vfs.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  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 "ramfs.h"
  8. vfs_mount_t vfs_mounts[VFS_MAX_MOUNTS];
  9. unsigned int vfs_mount_count;
  10. static void put_dec(unsigned int n)
  11. {
  12. char buf[12];
  13. unsigned int i = sizeof(buf) - 1;
  14. buf[i] = '\0';
  15. if (n == 0) {
  16. io_putchar('0');
  17. return;
  18. }
  19. while (n && i > 0) {
  20. buf[--i] = (char)('0' + (n % 10));
  21. n /= 10;
  22. }
  23. io_putstr(buf + i);
  24. }
  25. /* Print a size given in 512-byte sectors. Uses shifts (not bytes) so a
  26. * >4 GB filesystem/disk does not overflow 32-bit arithmetic. */
  27. static void put_size_sectors(unsigned int sectors)
  28. {
  29. if (sectors >= 2u * 1024u * 1024u) { /* >= 1 GiB */
  30. unsigned int gb = sectors >> 21;
  31. unsigned int tenths = ((sectors & 0x1FFFFFu) * 10u) >> 21;
  32. put_dec(gb);
  33. if (tenths) {
  34. io_putchar('.');
  35. put_dec(tenths);
  36. }
  37. io_putstr(" GB");
  38. } else if (sectors >= 2048u) { /* >= 1 MiB */
  39. unsigned int mb = sectors >> 11;
  40. unsigned int tenths = ((sectors & 0x7FFu) * 10u) >> 11;
  41. put_dec(mb);
  42. if (tenths) {
  43. io_putchar('.');
  44. put_dec(tenths);
  45. }
  46. io_putstr(" MB");
  47. } else {
  48. put_dec(sectors / 2u);
  49. io_putstr(" KB");
  50. }
  51. }
  52. static const char *fat_name(fat_type_t t)
  53. {
  54. switch (t) {
  55. case FAT12: return "FAT12";
  56. case FAT16: return "FAT16";
  57. case FAT32: return "FAT32";
  58. default: return "FAT";
  59. }
  60. }
  61. static void mem_copy(void *dst, const void *src, unsigned int n)
  62. {
  63. unsigned char *d = (unsigned char *)dst;
  64. const unsigned char *s = (const unsigned char *)src;
  65. while (n--)
  66. *d++ = *s++;
  67. }
  68. static void copy_bounded(char *dst, const char *src, unsigned int size)
  69. {
  70. unsigned int i = 0;
  71. while (i + 1 < size && src[i]) {
  72. dst[i] = src[i];
  73. i++;
  74. }
  75. dst[i] = '\0';
  76. }
  77. /* ---- FAT implementation ---- */
  78. /* One load buffer for the shell: only one command is loaded at a time and the
  79. * ELF loader has copied the segments out before we return. */
  80. static unsigned char fat_load_buf[VFS_FILE_MAX];
  81. static int fat_op_read_file(struct vfs_mount *m, const char *path,
  82. void *buf, unsigned int max)
  83. {
  84. unsigned int dir;
  85. char name[12];
  86. int r = fat_resolve_path(&m->fs, path, &dir, name);
  87. if (r == -2)
  88. return VFS_EIO;
  89. if (r != 0)
  90. return VFS_ENOENT;
  91. r = fat_read_file_in(&m->fs, dir, name, buf, max);
  92. if (r == -2)
  93. return VFS_EIO;
  94. if (r < 0)
  95. return VFS_ENOENT;
  96. return r;
  97. }
  98. static int fat_op_stat(struct vfs_mount *m, const char *path,
  99. unsigned int *size, int *is_dir)
  100. {
  101. int r = fat_stat(&m->fs, path, size, is_dir);
  102. if (r == -2)
  103. return VFS_EIO;
  104. if (r != 0)
  105. return VFS_ENOENT;
  106. return 0;
  107. }
  108. static int fat_op_load_file(struct vfs_mount *m, const char *path,
  109. struct vfs_file *out)
  110. {
  111. unsigned int size = 0;
  112. int is_dir = 0, r;
  113. r = fat_op_stat(m, path, &size, &is_dir);
  114. if (r != 0)
  115. return r;
  116. if (is_dir || size == 0 || size > sizeof(fat_load_buf))
  117. return VFS_EINVAL;
  118. r = fat_op_read_file(m, path, fat_load_buf, sizeof(fat_load_buf));
  119. if (r < 0)
  120. return r;
  121. out->data = fat_load_buf;
  122. out->size = (unsigned int)r;
  123. return 0;
  124. }
  125. static void fat_op_release_file(struct vfs_mount *m, struct vfs_file *f)
  126. {
  127. (void)m;
  128. (void)f;
  129. }
  130. static int fat_op_write_file(struct vfs_mount *m, const char *path,
  131. const void *buf, unsigned int len)
  132. {
  133. unsigned int dir;
  134. char name[12];
  135. int r = fat_resolve_path(&m->fs, path, &dir, name);
  136. if (r == -2)
  137. return VFS_EIO;
  138. if (r != 0)
  139. return VFS_EINVAL;
  140. r = fat_write_file_in(&m->fs, dir, name, buf, len);
  141. if (r == -2)
  142. return VFS_EIO;
  143. if (r != 0)
  144. return VFS_EINVAL;
  145. return 0;
  146. }
  147. static int fat_op_unlink(struct vfs_mount *m, const char *path)
  148. {
  149. unsigned int dir;
  150. char name[12];
  151. int r = fat_resolve_path(&m->fs, path, &dir, name);
  152. if (r == -2)
  153. return VFS_EIO;
  154. if (r != 0)
  155. return VFS_ENOENT;
  156. r = fat_delete_file_in(&m->fs, dir, name);
  157. if (r == -2)
  158. return VFS_EIO;
  159. if (r != 0)
  160. return VFS_ENOENT;
  161. return 0;
  162. }
  163. static int fat_op_mkdir(struct vfs_mount *m, const char *path)
  164. {
  165. unsigned int dir;
  166. char name[12];
  167. int r = fat_resolve_path(&m->fs, path, &dir, name);
  168. if (r == -2)
  169. return VFS_EIO;
  170. if (r != 0)
  171. return VFS_EINVAL;
  172. r = fat_mkdir(&m->fs, dir, name);
  173. if (r == -2)
  174. return VFS_EIO;
  175. if (r != 0)
  176. return VFS_EINVAL; /* the FAT layer printed its own reason */
  177. return 0;
  178. }
  179. static int fat_op_rmdir(struct vfs_mount *m, const char *path)
  180. {
  181. unsigned int dir;
  182. char name[12];
  183. int r = fat_resolve_path(&m->fs, path, &dir, name);
  184. if (r == -2)
  185. return VFS_EIO;
  186. if (r != 0)
  187. return VFS_EINVAL;
  188. r = fat_rmdir(&m->fs, dir, name);
  189. if (r == -2)
  190. return VFS_EIO;
  191. if (r != 0)
  192. return VFS_EINVAL;
  193. return 0;
  194. }
  195. /* Index-based listing: fat_list_dir walks from the start each call and we
  196. * count past `index` entries. Directories are small. */
  197. static unsigned int fat_list_index;
  198. static struct vfs_dirent *fat_list_out;
  199. static int fat_list_done;
  200. static unsigned int fat_list_seen;
  201. static void fat_list_cb(const char *name, int is_dir, unsigned int size)
  202. {
  203. unsigned int i;
  204. if (fat_list_done || fat_list_seen < fat_list_index) {
  205. fat_list_seen++;
  206. return;
  207. }
  208. for (i = 0; i < 11; i++)
  209. fat_list_out->name[i] = name[i] ? name[i] : ' ';
  210. fat_list_out->name[11] = '\0';
  211. fat_list_out->size = size;
  212. fat_list_out->is_dir = (unsigned int)is_dir;
  213. fat_list_done = 1;
  214. fat_list_seen++;
  215. }
  216. static int fat_op_list_dir(struct vfs_mount *m, const char *path,
  217. unsigned int index, struct vfs_dirent *out)
  218. {
  219. unsigned int dir;
  220. int r = fat_resolve_dir(&m->fs, path, &dir);
  221. if (r == -2)
  222. return VFS_EIO;
  223. if (r != 0)
  224. return VFS_ENOENT; /* not found (or not a directory) */
  225. fat_list_index = index;
  226. fat_list_seen = 0;
  227. fat_list_out = out;
  228. fat_list_done = 0;
  229. r = fat_list_dir(&m->fs, dir, fat_list_cb);
  230. if (r == -2)
  231. return VFS_EIO;
  232. return fat_list_done ? 1 : 0;
  233. }
  234. static const struct fs_ops fat_ops = {
  235. .raw_83 = 1,
  236. .read_file = fat_op_read_file,
  237. .load_file = fat_op_load_file,
  238. .release_file = fat_op_release_file,
  239. .stat = fat_op_stat,
  240. .write_file = fat_op_write_file,
  241. .unlink = fat_op_unlink,
  242. .mkdir = fat_op_mkdir,
  243. .rmdir = fat_op_rmdir,
  244. .list_dir = fat_op_list_dir,
  245. };
  246. /* ---- ramfs implementation ---- */
  247. static int ramfs_op_read_file(struct vfs_mount *m, const char *path,
  248. void *buf, unsigned int max)
  249. {
  250. const void *data;
  251. unsigned int size = 0, n;
  252. (void)m;
  253. data = ramfs_lookup(path, &size);
  254. if (!data)
  255. return VFS_ENOENT;
  256. n = size > max ? max : size;
  257. mem_copy(buf, data, n);
  258. return (int)n;
  259. }
  260. static int ramfs_op_load_file(struct vfs_mount *m, const char *path,
  261. struct vfs_file *out)
  262. {
  263. const void *data;
  264. unsigned int size = 0;
  265. (void)m;
  266. data = ramfs_lookup(path, &size);
  267. if (!data)
  268. return VFS_ENOENT;
  269. out->data = data;
  270. out->size = size;
  271. return 0;
  272. }
  273. static void ramfs_op_release_file(struct vfs_mount *m, struct vfs_file *f)
  274. {
  275. (void)m;
  276. (void)f;
  277. }
  278. static int ramfs_op_stat(struct vfs_mount *m, const char *path,
  279. unsigned int *size, int *is_dir)
  280. {
  281. const void *data;
  282. unsigned int sz = 0;
  283. struct ramfs_dirent e;
  284. (void)m;
  285. if (path[0] == '\0') {
  286. if (size)
  287. *size = 0;
  288. if (is_dir)
  289. *is_dir = 1;
  290. return 0;
  291. }
  292. data = ramfs_lookup(path, &sz);
  293. if (data) {
  294. if (size)
  295. *size = sz;
  296. if (is_dir)
  297. *is_dir = 0;
  298. return 0;
  299. }
  300. if (ramfs_list(path, 0, &e) == 1) {
  301. if (size)
  302. *size = 0;
  303. if (is_dir)
  304. *is_dir = 1;
  305. return 0;
  306. }
  307. return VFS_ENOENT;
  308. }
  309. static int ramfs_op_ro(struct vfs_mount *m, const char *path)
  310. {
  311. (void)m;
  312. (void)path;
  313. return VFS_EROFS;
  314. }
  315. static int ramfs_op_write_file(struct vfs_mount *m, const char *path,
  316. const void *buf, unsigned int len)
  317. {
  318. (void)buf;
  319. (void)len;
  320. return ramfs_op_ro(m, path);
  321. }
  322. static int ramfs_op_unlink(struct vfs_mount *m, const char *path)
  323. {
  324. return ramfs_op_ro(m, path);
  325. }
  326. static int ramfs_op_mkdir(struct vfs_mount *m, const char *path)
  327. {
  328. return ramfs_op_ro(m, path);
  329. }
  330. static int ramfs_op_rmdir(struct vfs_mount *m, const char *path)
  331. {
  332. return ramfs_op_ro(m, path);
  333. }
  334. static int ramfs_op_list_dir(struct vfs_mount *m, const char *path,
  335. unsigned int index, struct vfs_dirent *out)
  336. {
  337. struct ramfs_dirent e;
  338. (void)m;
  339. if (ramfs_list(path, index, &e) != 1) {
  340. if (index == 0 && path[0] != '\0') {
  341. unsigned int size = 0;
  342. /* Nothing matched: report a missing path instead of an
  343. * empty directory (the pack has no empty directories --
  344. * one exists exactly as a prefix of file paths), and a
  345. * file as "not a directory". */
  346. if (ramfs_lookup(path, &size))
  347. return VFS_EINVAL;
  348. return VFS_ENOENT;
  349. }
  350. return 0; /* end of directory */
  351. }
  352. copy_bounded(out->name, e.name, sizeof(out->name));
  353. out->size = e.size;
  354. out->is_dir = e.is_dir;
  355. return 1;
  356. }
  357. static const struct fs_ops ramfs_ops = {
  358. .raw_83 = 0,
  359. .read_file = ramfs_op_read_file,
  360. .load_file = ramfs_op_load_file,
  361. .release_file = ramfs_op_release_file,
  362. .stat = ramfs_op_stat,
  363. .write_file = ramfs_op_write_file,
  364. .unlink = ramfs_op_unlink,
  365. .mkdir = ramfs_op_mkdir,
  366. .rmdir = ramfs_op_rmdir,
  367. .list_dir = ramfs_op_list_dir,
  368. };
  369. /* ---- mounts ---- */
  370. static vfs_mount_t *mount_slot(const char *name)
  371. {
  372. unsigned int i;
  373. if (vfs_mount_count >= VFS_MAX_MOUNTS)
  374. return 0;
  375. {
  376. vfs_mount_t *m = &vfs_mounts[vfs_mount_count];
  377. for (i = 0; name[i] && i < sizeof(m->name) - 1; i++)
  378. m->name[i] = name[i];
  379. m->name[i] = '\0';
  380. m->mounted = 1;
  381. return m;
  382. }
  383. }
  384. static void mount_one(blk_dev_t *dev, unsigned int part_lba, const char *name)
  385. {
  386. fat_fs_t fs;
  387. vfs_mount_t *m;
  388. if (fat_mount(dev, part_lba, &fs) != 0)
  389. return;
  390. m = mount_slot(name);
  391. if (!m)
  392. return;
  393. m->ops = &fat_ops;
  394. m->fs = fs;
  395. m->ro = 0;
  396. m->total_sectors = fs.total_sectors;
  397. copy_bounded(m->type, fat_name(fs.type), sizeof(m->type));
  398. copy_bounded(m->label, fs.label, sizeof(m->label));
  399. vfs_mount_count++;
  400. }
  401. void vfs_mount_ramfs(void)
  402. {
  403. vfs_mount_t *m;
  404. if (ramfs_count() == 0)
  405. return;
  406. m = mount_slot("ramfs");
  407. if (!m)
  408. return;
  409. m->ops = &ramfs_ops;
  410. m->ro = 1;
  411. m->total_sectors = (ramfs_pack_size() + 511u) / 512u;
  412. copy_bounded(m->type, "ramfs", sizeof(m->type));
  413. m->label[0] = '\0';
  414. vfs_mount_count++;
  415. }
  416. void vfs_refresh_ramfs(void)
  417. {
  418. vfs_mount_t *m = vfs_find("ramfs");
  419. if (m)
  420. m->total_sectors = (ramfs_pack_size() + 511u) / 512u;
  421. }
  422. static void part_name(char *out, const char *base, unsigned int idx)
  423. {
  424. unsigned int i = 0;
  425. while (base[i] && i < 13) {
  426. out[i] = base[i];
  427. i++;
  428. }
  429. out[i++] = 'p';
  430. out[i++] = (char)('0' + idx + 1);
  431. out[i] = '\0';
  432. }
  433. void vfs_mount_all(void)
  434. {
  435. static unsigned char buf[512];
  436. unsigned int i;
  437. vfs_mount_count = 0;
  438. blk_scan();
  439. for (i = 0; i < blk_dev_count; i++) {
  440. blk_dev_t *d = &blk_devs[i];
  441. if (d->kind != BLK_FLOPPY && !d->present)
  442. continue;
  443. if (d->kind == BLK_FLOPPY) {
  444. mount_one(d, 0, d->name);
  445. continue;
  446. }
  447. /* IDE/SATA: MBR -> FAT partitions, else superfloppy. */
  448. {
  449. mbr_part_t parts[4];
  450. int n, j;
  451. int any_fat = 0;
  452. if (blk_read(d, 0, buf, 1) != 0)
  453. continue;
  454. n = mbr_parse(buf, parts);
  455. for (j = 0; j < n; j++) {
  456. char pname[16];
  457. if (!parts[j].is_fat || parts[j].start_lba == 0 ||
  458. parts[j].sectors == 0)
  459. continue;
  460. any_fat = 1;
  461. part_name(pname, d->name, parts[j].index);
  462. mount_one(d, parts[j].start_lba, pname);
  463. }
  464. if (!any_fat)
  465. mount_one(d, 0, d->name);
  466. }
  467. }
  468. }
  469. vfs_mount_t *vfs_find(const char *name)
  470. {
  471. unsigned int i;
  472. for (i = 0; i < vfs_mount_count; i++) {
  473. unsigned int j = 0;
  474. while (vfs_mounts[i].name[j] && name[j] &&
  475. vfs_mounts[i].name[j] == name[j])
  476. j++;
  477. if (vfs_mounts[i].name[j] == '\0' && name[j] == '\0')
  478. return &vfs_mounts[i];
  479. }
  480. return 0;
  481. }
  482. void vfs_list_root(void)
  483. {
  484. unsigned int i;
  485. for (i = 0; i < vfs_mount_count; i++) {
  486. vfs_mount_t *m = &vfs_mounts[i];
  487. io_putstr(m->name);
  488. io_putstr(" ");
  489. io_putstr(m->type);
  490. io_putchar(' ');
  491. put_size_sectors(m->total_sectors);
  492. if (m->label[0]) {
  493. io_putstr(" \"");
  494. io_putstr(m->label);
  495. io_putchar('"');
  496. }
  497. if (m->ro)
  498. io_putstr(" (ro)");
  499. io_putchar('\n');
  500. }
  501. }
  502. int vfs_resolve(const char *path, struct vfs_mount **m_out,
  503. char *rel_path, unsigned int rel_size)
  504. {
  505. unsigned int i = 0, j = 0, o = 0;
  506. char name[16];
  507. vfs_mount_t *m;
  508. if (!path || path[0] != '/' || rel_size == 0)
  509. return -1;
  510. i = 1;
  511. while (path[i] && path[i] != '/' && j < sizeof(name) - 1)
  512. name[j++] = path[i++];
  513. name[j] = '\0';
  514. if (j == 0)
  515. return -1; /* path is just "/" */
  516. m = vfs_find(name);
  517. if (!m)
  518. return -1;
  519. /* Copy the remainder as a normalized relative path: skip slashes, drop
  520. * "." components and never leave a trailing one. */
  521. while (path[i]) {
  522. unsigned int clen = 0;
  523. const char *comp;
  524. while (path[i] == '/')
  525. i++;
  526. if (!path[i])
  527. break;
  528. comp = path + i;
  529. while (path[i] && path[i] != '/') {
  530. i++;
  531. clen++;
  532. }
  533. if (clen == 1 && comp[0] == '.')
  534. continue;
  535. if (o + clen + 2 > rel_size)
  536. return -1;
  537. if (o > 0)
  538. rel_path[o++] = '/';
  539. {
  540. unsigned int k;
  541. for (k = 0; k < clen; k++)
  542. rel_path[o++] = comp[k];
  543. }
  544. }
  545. rel_path[o] = '\0';
  546. *m_out = m;
  547. return 0;
  548. }
  549. int vfs_load_file(const char *path, struct vfs_file *out,
  550. struct vfs_mount **m_out)
  551. {
  552. struct vfs_mount *m;
  553. char rel[VFS_PATH_MAX];
  554. int r;
  555. if (vfs_resolve(path, &m, rel, sizeof(rel)) != 0)
  556. return VFS_ENOENT;
  557. r = m->ops->load_file(m, rel, out);
  558. if (r == 0 && m_out)
  559. *m_out = m;
  560. return r;
  561. }
  562. void vfs_release_file(struct vfs_mount *m, struct vfs_file *f)
  563. {
  564. if (m && m->ops->release_file)
  565. m->ops->release_file(m, f);
  566. }
  567. int vfs_stat(const char *path, unsigned int *size, int *is_dir)
  568. {
  569. struct vfs_mount *m;
  570. char rel[VFS_PATH_MAX];
  571. if (vfs_resolve(path, &m, rel, sizeof(rel)) != 0)
  572. return VFS_ENOENT;
  573. return m->ops->stat(m, rel, size, is_dir);
  574. }
  575. int vfs_list_dir(const char *path, unsigned int index, struct vfs_dirent *out)
  576. {
  577. struct vfs_mount *m;
  578. char rel[VFS_PATH_MAX];
  579. if (vfs_resolve(path, &m, rel, sizeof(rel)) != 0)
  580. return VFS_ENOENT;
  581. return m->ops->list_dir(m, rel, index, out);
  582. }
  583. int vfs_join_path(const char *cwd, const char *arg, char *out, unsigned int out_size)
  584. {
  585. char buf[VFS_PATH_MAX * 2 + 2];
  586. unsigned int bi = 0;
  587. unsigned int len;
  588. const char *p;
  589. /* Build the combined (un-normalized) path. */
  590. if (arg[0] == '/') {
  591. p = arg;
  592. } else {
  593. p = cwd;
  594. while (*p && bi < sizeof(buf) - 1)
  595. buf[bi++] = *p++;
  596. if (bi > 0 && buf[bi - 1] != '/') {
  597. if (bi < sizeof(buf) - 1)
  598. buf[bi++] = '/';
  599. }
  600. p = arg;
  601. }
  602. while (*p && bi < sizeof(buf) - 1)
  603. buf[bi++] = *p++;
  604. buf[bi] = '\0';
  605. if (out_size < 2)
  606. return -1;
  607. /* Normalize into out (always begins with "/"). */
  608. out[0] = '/';
  609. out[1] = '\0';
  610. len = 1;
  611. p = buf;
  612. while (*p) {
  613. unsigned int clen = 0;
  614. const char *comp;
  615. while (*p == '/')
  616. p++;
  617. if (!*p)
  618. break;
  619. comp = p;
  620. while (*p && *p != '/') {
  621. p++;
  622. clen++;
  623. }
  624. if (clen == 1 && comp[0] == '.')
  625. continue;
  626. if (clen == 2 && comp[0] == '.' && comp[1] == '.') {
  627. /* Pop the previous component (may leave "/"). */
  628. while (len > 1 && out[len - 1] != '/')
  629. len--;
  630. if (len > 1)
  631. len--;
  632. out[len] = '\0';
  633. continue;
  634. }
  635. /* Append "/component". */
  636. if (len + 1 + clen + 1 > out_size)
  637. return -1;
  638. if (len > 1)
  639. out[len++] = '/';
  640. {
  641. unsigned int k;
  642. for (k = 0; k < clen; k++)
  643. out[len++] = comp[k];
  644. }
  645. out[len] = '\0';
  646. }
  647. return 0;
  648. }
  649. int vfs_device_known(const char *path)
  650. {
  651. unsigned int i = 0, j = 0;
  652. char name[16];
  653. if (!path || path[0] != '/')
  654. return 0;
  655. i = 1;
  656. while (path[i] && path[i] != '/' && j < sizeof(name) - 1)
  657. name[j++] = path[i++];
  658. name[j] = '\0';
  659. if (j == 0)
  660. return 0;
  661. return blk_find(name) != 0;
  662. }