vfs.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /* Virtual filesystem: mount every device with a valid FAT filesystem under `/`. */
  2. #include "vfs.h"
  3. #include "fatdetect.h"
  4. #include "io.h"
  5. vfs_mount_t vfs_mounts[VFS_MAX_MOUNTS];
  6. unsigned int vfs_mount_count;
  7. static void put_dec(unsigned int n)
  8. {
  9. char buf[12];
  10. unsigned int i = sizeof(buf) - 1;
  11. buf[i] = '\0';
  12. if (n == 0) {
  13. io_putchar('0');
  14. return;
  15. }
  16. while (n && i > 0) {
  17. buf[--i] = (char)('0' + (n % 10));
  18. n /= 10;
  19. }
  20. io_putstr(buf + i);
  21. }
  22. /* Print a size given in 512-byte sectors. Uses shifts (not bytes) so a
  23. * >4 GB filesystem/disk does not overflow 32-bit arithmetic. */
  24. static void put_size_sectors(unsigned int sectors)
  25. {
  26. if (sectors >= 2u * 1024u * 1024u) { /* >= 1 GiB */
  27. unsigned int gb = sectors >> 21;
  28. unsigned int tenths = ((sectors & 0x1FFFFFu) * 10u) >> 21;
  29. put_dec(gb);
  30. if (tenths) {
  31. io_putchar('.');
  32. put_dec(tenths);
  33. }
  34. io_putstr(" GB");
  35. } else if (sectors >= 2048u) { /* >= 1 MiB */
  36. unsigned int mb = sectors >> 11;
  37. unsigned int tenths = ((sectors & 0x7FFu) * 10u) >> 11;
  38. put_dec(mb);
  39. if (tenths) {
  40. io_putchar('.');
  41. put_dec(tenths);
  42. }
  43. io_putstr(" MB");
  44. } else {
  45. put_dec(sectors / 2u);
  46. io_putstr(" KB");
  47. }
  48. }
  49. static const char *fat_name(fat_type_t t)
  50. {
  51. switch (t) {
  52. case FAT12: return "FAT12";
  53. case FAT16: return "FAT16";
  54. case FAT32: return "FAT32";
  55. default: return "FAT";
  56. }
  57. }
  58. static void mount_one(blk_dev_t *dev, unsigned int part_lba, const char *name)
  59. {
  60. fat_fs_t fs;
  61. unsigned int i;
  62. if (vfs_mount_count >= VFS_MAX_MOUNTS)
  63. return;
  64. if (fat_mount(dev, part_lba, &fs) != 0)
  65. return;
  66. for (i = 0; name[i] && i < sizeof(vfs_mounts[vfs_mount_count].name) - 1; i++)
  67. vfs_mounts[vfs_mount_count].name[i] = name[i];
  68. vfs_mounts[vfs_mount_count].name[i] = '\0';
  69. vfs_mounts[vfs_mount_count].fs = fs;
  70. vfs_mounts[vfs_mount_count].mounted = 1;
  71. vfs_mount_count++;
  72. }
  73. static void part_name(char *out, const char *base, unsigned int idx)
  74. {
  75. unsigned int i = 0;
  76. while (base[i] && i < 13) {
  77. out[i] = base[i];
  78. i++;
  79. }
  80. out[i++] = 'p';
  81. out[i++] = (char)('0' + idx + 1);
  82. out[i] = '\0';
  83. }
  84. void vfs_mount_all(void)
  85. {
  86. static unsigned char buf[512];
  87. unsigned int i;
  88. vfs_mount_count = 0;
  89. blk_scan();
  90. for (i = 0; i < blk_dev_count; i++) {
  91. blk_dev_t *d = &blk_devs[i];
  92. if (d->kind != BLK_FLOPPY && !d->present)
  93. continue;
  94. if (d->kind == BLK_FLOPPY) {
  95. mount_one(d, 0, d->name);
  96. continue;
  97. }
  98. /* IDE/SATA: MBR -> FAT partitions, else superfloppy. */
  99. {
  100. mbr_part_t parts[4];
  101. int n, j;
  102. int any_fat = 0;
  103. if (blk_read(d, 0, buf, 1) != 0)
  104. continue;
  105. n = mbr_parse(buf, parts);
  106. for (j = 0; j < n; j++) {
  107. char pname[16];
  108. if (!parts[j].is_fat || parts[j].start_lba == 0 ||
  109. parts[j].sectors == 0)
  110. continue;
  111. any_fat = 1;
  112. part_name(pname, d->name, parts[j].index);
  113. mount_one(d, parts[j].start_lba, pname);
  114. }
  115. if (!any_fat)
  116. mount_one(d, 0, d->name);
  117. }
  118. }
  119. }
  120. vfs_mount_t *vfs_find(const char *name)
  121. {
  122. unsigned int i;
  123. for (i = 0; i < vfs_mount_count; i++) {
  124. unsigned int j = 0;
  125. while (vfs_mounts[i].name[j] && name[j] &&
  126. vfs_mounts[i].name[j] == name[j])
  127. j++;
  128. if (vfs_mounts[i].name[j] == '\0' && name[j] == '\0')
  129. return &vfs_mounts[i];
  130. }
  131. return 0;
  132. }
  133. void vfs_list_root(void)
  134. {
  135. unsigned int i;
  136. for (i = 0; i < vfs_mount_count; i++) {
  137. vfs_mount_t *m = &vfs_mounts[i];
  138. fat_fs_t *fs = &m->fs;
  139. io_putstr(m->name);
  140. io_putstr(" ");
  141. io_putstr(fat_name(fs->type));
  142. io_putchar(' ');
  143. put_size_sectors(fs->total_sectors);
  144. if (fs->label[0]) {
  145. io_putstr(" \"");
  146. io_putstr(fs->label);
  147. io_putchar('"');
  148. }
  149. io_putchar('\n');
  150. }
  151. }
  152. int vfs_resolve(const char *path, fat_fs_t **fs, const char **fs_path)
  153. {
  154. unsigned int i = 0, j;
  155. char name[16];
  156. vfs_mount_t *m;
  157. if (!path || path[0] != '/')
  158. return -1;
  159. i = 1;
  160. j = 0;
  161. while (path[i] && path[i] != '/' && j < sizeof(name) - 1)
  162. name[j++] = path[i++];
  163. name[j] = '\0';
  164. if (j == 0)
  165. return -1; /* path is just "/" */
  166. m = vfs_find(name);
  167. if (!m)
  168. return -1;
  169. *fs = &m->fs;
  170. *fs_path = path + i;
  171. return 0;
  172. }
  173. int vfs_join_path(const char *cwd, const char *arg, char *out, unsigned int out_size)
  174. {
  175. char buf[VFS_PATH_MAX * 2 + 2];
  176. unsigned int bi = 0;
  177. unsigned int len;
  178. const char *p;
  179. /* Build the combined (un-normalized) path. */
  180. if (arg[0] == '/') {
  181. p = arg;
  182. } else {
  183. p = cwd;
  184. while (*p && bi < sizeof(buf) - 1)
  185. buf[bi++] = *p++;
  186. if (bi > 0 && buf[bi - 1] != '/') {
  187. if (bi < sizeof(buf) - 1)
  188. buf[bi++] = '/';
  189. }
  190. p = arg;
  191. }
  192. while (*p && bi < sizeof(buf) - 1)
  193. buf[bi++] = *p++;
  194. buf[bi] = '\0';
  195. if (out_size < 2)
  196. return -1;
  197. /* Normalize into out (always begins with "/"). */
  198. out[0] = '/';
  199. out[1] = '\0';
  200. len = 1;
  201. p = buf;
  202. while (*p) {
  203. unsigned int clen = 0;
  204. const char *comp;
  205. while (*p == '/')
  206. p++;
  207. if (!*p)
  208. break;
  209. comp = p;
  210. while (*p && *p != '/') {
  211. p++;
  212. clen++;
  213. }
  214. if (clen == 1 && comp[0] == '.')
  215. continue;
  216. if (clen == 2 && comp[0] == '.' && comp[1] == '.') {
  217. /* Pop the previous component (may leave "/"). */
  218. while (len > 1 && out[len - 1] != '/')
  219. len--;
  220. if (len > 1)
  221. len--;
  222. out[len] = '\0';
  223. continue;
  224. }
  225. /* Append "/component". */
  226. if (len + 1 + clen + 1 > out_size)
  227. return -1;
  228. if (len > 1)
  229. out[len++] = '/';
  230. {
  231. unsigned int k;
  232. for (k = 0; k < clen; k++)
  233. out[len++] = comp[k];
  234. }
  235. out[len] = '\0';
  236. }
  237. return 0;
  238. }
  239. int vfs_device_known(const char *path)
  240. {
  241. unsigned int i = 0, j = 0;
  242. char name[16];
  243. if (!path || path[0] != '/')
  244. return 0;
  245. i = 1;
  246. while (path[i] && path[i] != '/' && j < sizeof(name) - 1)
  247. name[j++] = path[i++];
  248. name[j] = '\0';
  249. if (j == 0)
  250. return 0;
  251. return blk_find(name) != 0;
  252. }