/* Virtual filesystem: mount every device with a valid FAT filesystem under `/`, * and the command pack (ramfs) at /ramfs. File operations go through the * fs_ops vtable so sys_fs.c and the shell stay filesystem-agnostic. */ #include "vfs.h" #include "fatdetect.h" #include "io.h" #include "kmalloc.h" #include "process.h" #include "sched.h" #include "ramfs.h" #include "rootfs.h" #include "serial.h" #include "share.h" #include "syscall.h" #include "timer.h" #include "usbmsc.h" vfs_mount_t vfs_mounts[VFS_MAX_MOUNTS]; unsigned int vfs_mount_count; /* Set while vfs_mount_all() rebuilds the mount table. File syscalls wait on * this (sys_fs.c) so they never resolve a path in a half-rebuilt table or use * the FAT sector buffer while the scan is reading disks. The owner (a * `storage` syscall) is exempt. */ volatile int vfs_scan_busy; volatile unsigned int vfs_scan_owner_pid; static void put_dec(unsigned int n) { char buf[12]; unsigned int i = sizeof(buf) - 1; buf[i] = '\0'; if (n == 0) { io_putchar('0'); return; } while (n && i > 0) { buf[--i] = (char)('0' + (n % 10)); n /= 10; } io_putstr(buf + i); } /* Print a size given in 512-byte sectors. Uses shifts (not bytes) so a * >4 GB filesystem/disk does not overflow 32-bit arithmetic. */ static void put_size_sectors(unsigned int sectors) { if (sectors >= 2u * 1024u * 1024u) { /* >= 1 GiB */ unsigned int gb = sectors >> 21; unsigned int tenths = ((sectors & 0x1FFFFFu) * 10u) >> 21; put_dec(gb); if (tenths) { io_putchar('.'); put_dec(tenths); } io_putstr(" GB"); } else if (sectors >= 2048u) { /* >= 1 MiB */ unsigned int mb = sectors >> 11; unsigned int tenths = ((sectors & 0x7FFu) * 10u) >> 11; put_dec(mb); if (tenths) { io_putchar('.'); put_dec(tenths); } io_putstr(" MB"); } else { put_dec(sectors / 2u); io_putstr(" KB"); } } static const char *fat_name(fat_type_t t) { switch (t) { case FAT12: return "FAT12"; case FAT16: return "FAT16"; case FAT32: return "FAT32"; default: return "FAT"; } } static void mem_copy(void *dst, const void *src, unsigned int n) { unsigned char *d = (unsigned char *)dst; const unsigned char *s = (const unsigned char *)src; while (n--) *d++ = *s++; } static void copy_bounded(char *dst, const char *src, unsigned int size) { unsigned int i = 0; while (i + 1 < size && src[i]) { dst[i] = src[i]; i++; } dst[i] = '\0'; } /* ---- FAT implementation ---- */ static void fat_cache_clear(void); static int fat_op_read_file(struct vfs_mount *m, const char *path, void *buf, unsigned int max) { unsigned int dir; char name[12]; int r = fat_resolve_path(&m->fs, path, &dir, name); if (r == -2) return VFS_EIO; if (r != 0) return VFS_ENOENT; r = fat_read_file_in(&m->fs, dir, name, buf, max); if (r == -2) return VFS_EIO; if (r < 0) return VFS_ENOENT; return r; } static int fat_op_stat(struct vfs_mount *m, const char *path, unsigned int *size, int *is_dir) { int r = fat_stat(&m->fs, path, size, is_dir); if (r == -2) return VFS_EIO; if (r != 0) return VFS_ENOENT; return 0; } static int fat_op_load_file(struct vfs_mount *m, const char *path, struct vfs_file *out) { unsigned int size = 0; int is_dir = 0, r; void *buf; r = fat_op_stat(m, path, &size, &is_dir); if (r != 0) return r; if (is_dir || size == 0 || size > VFS_FILE_MAX) return VFS_EINVAL; /* One command is loaded at a time; the ELF loader has copied the * segments out before release_file() runs, so a heap buffer is safe. */ buf = kmalloc(size); if (!buf) return VFS_EINVAL; r = fat_op_read_file(m, path, buf, size); if (r < 0) { kfree(buf); return r; } out->data = buf; out->size = (unsigned int)r; return 0; } static void fat_op_release_file(struct vfs_mount *m, struct vfs_file *f) { (void)m; kfree((void *)f->data); f->data = 0; f->size = 0; } static int fat_op_write_file(struct vfs_mount *m, const char *path, const void *buf, unsigned int len) { fat_cache_clear(); unsigned int dir; char name[12]; int r = fat_resolve_path(&m->fs, path, &dir, name); if (r == -2) return VFS_EIO; if (r != 0) return VFS_EINVAL; r = fat_write_file_in(&m->fs, dir, name, buf, len); if (r == -2) return VFS_EIO; if (r != 0) return VFS_EINVAL; return 0; } static int fat_op_unlink(struct vfs_mount *m, const char *path) { fat_cache_clear(); unsigned int dir; char name[12]; int r = fat_resolve_path(&m->fs, path, &dir, name); if (r == -2) return VFS_EIO; if (r != 0) return VFS_ENOENT; r = fat_delete_file_in(&m->fs, dir, name); if (r == -2) return VFS_EIO; if (r != 0) return VFS_ENOENT; return 0; } static int fat_op_mkdir(struct vfs_mount *m, const char *path) { fat_cache_clear(); unsigned int dir; char name[12]; int r = fat_resolve_path(&m->fs, path, &dir, name); if (r == -2) return VFS_EIO; if (r != 0) return VFS_EINVAL; r = fat_mkdir(&m->fs, dir, name); if (r == -2) return VFS_EIO; if (r != 0) return VFS_EINVAL; /* the FAT layer printed its own reason */ return 0; } static int fat_op_rmdir(struct vfs_mount *m, const char *path) { fat_cache_clear(); unsigned int dir; char name[12]; int r = fat_resolve_path(&m->fs, path, &dir, name); if (r == -2) return VFS_EIO; if (r != 0) return VFS_EINVAL; r = fat_rmdir(&m->fs, dir, name); if (r == -2) return VFS_EIO; if (r != 0) return VFS_EINVAL; return 0; } /* Index-based listing used to re-walk the FAT directory for every index, and * the WebDAV server asks for 0..N-1 in one PROPFIND. Cache the first 24 * entries of the last directory so a listing is one directory walk; a write * invalidates it. Larger directories fall back to the old walk past 24. */ #define FAT_LIST_CACHE 24 static struct vfs_dirent fat_cache[FAT_LIST_CACHE]; static unsigned int fat_cache_count; static int fat_cache_valid; static int fat_cache_truncated; static struct vfs_mount *fat_cache_mount; static char fat_cache_path[VFS_PATH_MAX]; static void fat_cache_clear(void) { fat_cache_valid = 0; } static int fat_path_eq(const char *a, const char *b) { unsigned int i = 0; while (a[i] && b[i] && a[i] == b[i]) i++; return a[i] == '\0' && b[i] == '\0'; } static void fat_cache_cb(const char *name, int is_dir, unsigned int size) { struct vfs_dirent *d; unsigned int i; if (fat_cache_count >= FAT_LIST_CACHE) { fat_cache_truncated = 1; return; } d = &fat_cache[fat_cache_count++]; for (i = 0; i < 11; i++) d->name[i] = name[i] ? name[i] : ' '; d->name[11] = '\0'; d->size = size; d->is_dir = (unsigned int)is_dir; d->raw_83 = 1; } /* Old behaviour: one directory walk, counting to `index`. */ static unsigned int fat_list_index; static struct vfs_dirent *fat_list_out; static int fat_list_done; static unsigned int fat_list_seen; static void fat_list_cb(const char *name, int is_dir, unsigned int size) { unsigned int i; if (fat_list_done || fat_list_seen < fat_list_index) { fat_list_seen++; return; } for (i = 0; i < 11; i++) fat_list_out->name[i] = name[i] ? name[i] : ' '; fat_list_out->name[11] = '\0'; fat_list_out->size = size; fat_list_out->is_dir = (unsigned int)is_dir; fat_list_out->raw_83 = 1; fat_list_done = 1; fat_list_seen++; } static int fat_list_walk(struct vfs_mount *m, const char *path, unsigned int index, struct vfs_dirent *out) { unsigned int dir; int r = fat_resolve_dir(&m->fs, path, &dir); if (r == -2) return VFS_EIO; if (r != 0) return VFS_ENOENT; fat_list_index = index; fat_list_seen = 0; fat_list_out = out; fat_list_done = 0; r = fat_list_dir(&m->fs, dir, fat_list_cb); if (r == -2) return VFS_EIO; return fat_list_done ? 1 : 0; } static int fat_op_list_dir(struct vfs_mount *m, const char *path, unsigned int index, struct vfs_dirent *out) { int same = fat_cache_valid && fat_cache_mount == m && fat_path_eq(fat_cache_path, path); if (same) { if (index < fat_cache_count) { *out = fat_cache[index]; return 1; } if (!fat_cache_truncated) return 0; /* Beyond the cached prefix: use the old walk. */ return fat_list_walk(m, path, index, out); } { unsigned int dir; int r = fat_resolve_dir(&m->fs, path, &dir); if (r == -2) return VFS_EIO; if (r != 0) return VFS_ENOENT; /* not found / not a directory */ fat_cache_count = 0; fat_cache_truncated = 0; fat_cache_mount = m; copy_bounded(fat_cache_path, path, sizeof(fat_cache_path)); fat_cache_valid = 0; r = fat_list_dir(&m->fs, dir, fat_cache_cb); if (r == -2) return VFS_EIO; fat_cache_valid = 1; } if (index < fat_cache_count) { *out = fat_cache[index]; return 1; } return 0; } static const struct fs_ops fat_ops = { .raw_83 = 1, .read_file = fat_op_read_file, .load_file = fat_op_load_file, .release_file = fat_op_release_file, .stat = fat_op_stat, .write_file = fat_op_write_file, .unlink = fat_op_unlink, .mkdir = fat_op_mkdir, .rmdir = fat_op_rmdir, .list_dir = fat_op_list_dir, }; /* 1 for a block-device-backed mount (the FAT driver). A destructive format * rewrites such a mount; RAM-backed mounts stay usable while it runs. */ int vfs_mount_is_disk(const vfs_mount_t *m) { return m && m->ops == &fat_ops; } void vfs_scan_poll(unsigned int t0) { if (vfs_scan_busy && vfs_scan_owner_pid && get_ticks_ms() - t0 >= 2) vfs_scan_relax(1); } int vfs_scan_relax(unsigned int ms) { struct process *p; if (!vfs_scan_busy || !vfs_scan_owner_pid) return 0; p = current_proc(); if (!p || p->pid != vfs_scan_owner_pid || this_cpu()->preempt_count) return 0; { unsigned int flags; __asm__ volatile ("pushfl; popl %0" : "=r"(flags)); if (!(flags & 0x200u)) return 0; /* interrupts off: cannot sleep */ } process_sleep_ms(ms ? ms : 1); return 1; } /* ---- ramfs implementation ---- */ static int ramfs_op_read_file(struct vfs_mount *m, const char *path, void *buf, unsigned int max) { const void *data; unsigned int size = 0, n; (void)m; data = ramfs_lookup(path, &size); if (!data) return VFS_ENOENT; n = size > max ? max : size; mem_copy(buf, data, n); return (int)n; } static int ramfs_op_load_file(struct vfs_mount *m, const char *path, struct vfs_file *out) { const void *data; unsigned int size = 0; (void)m; data = ramfs_lookup(path, &size); if (!data) return VFS_ENOENT; out->data = data; out->size = size; return 0; } static void ramfs_op_release_file(struct vfs_mount *m, struct vfs_file *f) { (void)m; (void)f; } static int ramfs_op_stat(struct vfs_mount *m, const char *path, unsigned int *size, int *is_dir) { const void *data; unsigned int sz = 0; struct ramfs_dirent e; (void)m; if (path[0] == '\0') { if (size) *size = 0; if (is_dir) *is_dir = 1; return 0; } data = ramfs_lookup(path, &sz); if (data) { if (size) *size = sz; if (is_dir) *is_dir = 0; return 0; } if (ramfs_list(path, 0, &e) == 1) { if (size) *size = 0; if (is_dir) *is_dir = 1; return 0; } return VFS_ENOENT; } static int ramfs_op_ro(struct vfs_mount *m, const char *path) { (void)m; (void)path; return VFS_EROFS; } static int ramfs_op_write_file(struct vfs_mount *m, const char *path, const void *buf, unsigned int len) { (void)buf; (void)len; return ramfs_op_ro(m, path); } static int ramfs_op_unlink(struct vfs_mount *m, const char *path) { return ramfs_op_ro(m, path); } static int ramfs_op_mkdir(struct vfs_mount *m, const char *path) { return ramfs_op_ro(m, path); } static int ramfs_op_rmdir(struct vfs_mount *m, const char *path) { return ramfs_op_ro(m, path); } static int ramfs_op_list_dir(struct vfs_mount *m, const char *path, unsigned int index, struct vfs_dirent *out) { struct ramfs_dirent e; (void)m; if (ramfs_list(path, index, &e) != 1) { if (index == 0 && path[0] != '\0') { unsigned int size = 0; /* Nothing matched: report a missing path instead of an * empty directory (the pack has no empty directories -- * one exists exactly as a prefix of file paths), and a * file as "not a directory". */ if (ramfs_lookup(path, &size)) return VFS_EINVAL; return VFS_ENOENT; } return 0; /* end of directory */ } copy_bounded(out->name, e.name, sizeof(out->name)); out->size = e.size; out->is_dir = e.is_dir; out->raw_83 = 0; return 1; } static const struct fs_ops ramfs_ops = { .raw_83 = 0, .read_file = ramfs_op_read_file, .load_file = ramfs_op_load_file, .release_file = ramfs_op_release_file, .stat = ramfs_op_stat, .write_file = ramfs_op_write_file, .unlink = ramfs_op_unlink, .mkdir = ramfs_op_mkdir, .rmdir = ramfs_op_rmdir, .list_dir = ramfs_op_list_dir, }; int vfs_mount_is_ram(const vfs_mount_t *m) { return m && (m->ops == &ramfs_ops || m->ops == &share_ops); } /* ---- mounts ---- */ static vfs_mount_t *mount_slot(const char *name, const char *dev) { unsigned int i; if (vfs_mount_count >= VFS_MAX_MOUNTS) return 0; { vfs_mount_t *m = &vfs_mounts[vfs_mount_count]; for (i = 0; name[i] && i < sizeof(m->name) - 1; i++) m->name[i] = name[i]; m->name[i] = '\0'; m->dev[0] = '\0'; if (dev) { for (i = 0; dev[i] && i < sizeof(m->dev) - 1; i++) m->dev[i] = dev[i]; m->dev[i] = '\0'; } m->key[0] = '\0'; m->mounted = 1; return m; } } static int name_eq(const char *a, const char *b) { unsigned int i = 0; while (a[i] && b[i] && a[i] == b[i]) i++; return a[i] == '\0' && b[i] == '\0'; } static unsigned int mbr_dword(const unsigned char *p) { return (unsigned int)p[0] | ((unsigned int)p[1] << 8) | ((unsigned int)p[2] << 16) | ((unsigned int)p[3] << 24); } /* The NebOS MBR (boot/mbr.S) has a fixed layout: an active, FAT16-looking * P1 at LBA 63 with 1024 sectors (boot sector + kernel) and P2 at LBA 1088 * (the rootfs partition, or the dummy entry that keeps the AT3IONT-I BIOS * from classifying the stick as USB-ZIP). Recognising that layout lets the * boot stick skip the P1 content read, which is one USB round trip on every * boot. Anything that does not match exactly still goes through the * content check below. */ static int mbr_is_nebos_layout(const unsigned char *mbr) { const unsigned char *p1 = mbr + 0x1BE; const unsigned char *p2 = p1 + 16; if (mbr[0] != 0xEB || mbr[1] != 0x42 || mbr[2] != 0x90) return 0; if (p1[0] != 0x80 || p1[4] != 0x06) return 0; if (mbr_dword(p1 + 8) != 63 || mbr_dword(p1 + 12) != 1024) return 0; return mbr_dword(p2 + 8) == 1088; } /* The P1 boot sector keeps only the NebOS header fields; a real FAT volume * never carries these values. Older installed media still has the original * FAT-looking BPB (before boot.S was changed), so skip it explicitly instead * of mounting the raw kernel partition as a fake filesystem. * Returns 1 = NebOS boot partition, 0 = not, -1 = the sector could not be * read (skip it either way; a failed probe must not turn into a mount). */ static int is_nebos_boot_partition(blk_dev_t *d, unsigned int start_lba) { unsigned char s[512]; unsigned int i; if (blk_read(d, start_lba, s, 1) != 0) return -1; if (s[510] != 0x55 || s[511] != 0xAA) return 0; if (s[3] != 'N' || s[4] != 'E' || s[5] != 'B' || s[6] != 'O' || s[7] != 'S') return 0; if ((s[0x40] | (s[0x41] << 8)) != NEBOS_KERNEL_LBA) return 0; i = s[0x42] | (s[0x43] << 8) | (s[0x44] << 16) | ((unsigned int)s[0x45] << 24); return i == ROOTFS_LBA ? 1 : 0; } /* ---- mount policies ---- */ static struct sys_mount_policy vfs_policies[SYS_MOUNT_POLICY_MAX]; static unsigned int vfs_policy_count; static struct sys_mount_policy *policy_find(const char *key) { unsigned int i; for (i = 0; i < vfs_policy_count; i++) if (name_eq(vfs_policies[i].key, key)) return &vfs_policies[i]; return 0; } /* A mount name or key: 1..15 of [a-z0-9_-]. */ static int policy_name_ok(const char *s) { unsigned int i; for (i = 0; s[i]; i++) { char c = s[i]; if (i >= 15 || !((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '_' || c == '-')) return 0; } return i > 0; } /* The block device a policy key names: "" or "p<1-4>". */ static blk_dev_t *policy_device(const char *key) { unsigned int i, n; for (i = 0; i < blk_dev_count; i++) { const char *d = blk_devs[i].name; for (n = 0; d[n] && key[n] == d[n]; n++) ; if (d[n]) continue; if (key[n] == '\0' || (key[n] == 'p' && key[n + 1] >= '1' && key[n + 1] <= '4' && key[n + 2] == '\0')) return &blk_devs[i]; } return 0; } static void remount(blk_dev_t *d); int vfs_policy_set(const struct sys_mount_policy *p) { struct sys_mount_policy *e; blk_dev_t *d; if (!policy_name_ok(p->key) || p->name[sizeof(p->name) - 1] || (p->name[0] && (!policy_name_ok(p->name) || name_eq(p->name, "ramfs") || name_eq(p->name, "share"))) || (p->flags & ~(SYS_MOUNT_NOAUTO | SYS_MOUNT_RO))) return -1; d = policy_device(p->key); if (!d) return -1; e = policy_find(p->key); if (!p->name[0] && !p->flags) { if (e) *e = vfs_policies[--vfs_policy_count]; } else { if (!e) { if (vfs_policy_count >= SYS_MOUNT_POLICY_MAX) return -1; e = &vfs_policies[vfs_policy_count++]; } *e = *p; } remount(d); return 0; } int vfs_policy_get(unsigned int index, struct sys_mount_policy *out) { if (index >= vfs_policy_count) return 0; *out = vfs_policies[index]; return 1; } static void mount_one(blk_dev_t *dev, unsigned int part_lba, const char *name) { fat_fs_t fs; vfs_mount_t *m; const struct sys_mount_policy *pol = policy_find(name); const char *use_name = name; int r; if (pol && (pol->flags & SYS_MOUNT_NOAUTO)) return; fat_cache_clear(); r = fat_mount(dev, part_lba, &fs); if (r != 0) { serial_puts(SERIAL_COM1, "vfs: "); serial_puts(SERIAL_COM1, name); serial_puts(SERIAL_COM1, " no FAT\n"); return; } /* The labelled data partition has a stable path independent of the * device name. Only the first NEBOSDATA volume becomes /data. */ if (name_eq(fs.label, "NEBOSDATA") && !vfs_find("data")) use_name = "data"; /* A policy name wins unless another volume already holds it. */ if (pol && pol->name[0] && !vfs_find(pol->name)) use_name = pol->name; m = mount_slot(use_name, dev->name); if (!m) return; copy_bounded(m->key, name, sizeof(m->key)); m->ops = &fat_ops; m->fs = fs; m->ro = (pol && (pol->flags & SYS_MOUNT_RO)) ? 1 : 0; m->total_sectors = fs.total_sectors; copy_bounded(m->type, fat_name(fs.type), sizeof(m->type)); copy_bounded(m->label, fs.label, sizeof(m->label)); vfs_mount_count++; serial_puts(SERIAL_COM1, "vfs: "); serial_puts(SERIAL_COM1, use_name); serial_puts(SERIAL_COM1, " "); serial_puts(SERIAL_COM1, fat_name(fs.type)); serial_puts(SERIAL_COM1, "\n"); } void vfs_mount_ramfs(void) { vfs_mount_t *m; /* Mount even when the pack is empty: the runtime `cmds-dev` path can * install a pack later and vfs_refresh_ramfs() then updates the mount. */ m = mount_slot("ramfs", 0); if (!m) return; m->ops = &ramfs_ops; m->ro = 1; m->total_sectors = (ramfs_pack_size() + 511u) / 512u; copy_bounded(m->type, "ramfs", sizeof(m->type)); m->label[0] = '\0'; vfs_mount_count++; } void vfs_refresh_ramfs(void) { vfs_mount_t *m = vfs_find("ramfs"); if (m) m->total_sectors = (ramfs_pack_size() + 511u) / 512u; } /* Writable RAM store for network uploads. Unlike the pack ramfs it is not a * block device and its contents survive vfs_mount_all() refreshes. */ void vfs_mount_share(void) { vfs_mount_t *m; if (share_init() != 0) return; m = mount_slot("share", 0); if (!m) return; m->ops = &share_ops; m->ro = 0; m->total_sectors = SHARE_MAX_BYTES / 512u; copy_bounded(m->type, "share", sizeof(m->type)); m->label[0] = '\0'; vfs_mount_count++; } static void part_name(char *out, const char *base, unsigned int idx) { unsigned int i = 0; while (base[i] && i < 13) { out[i] = base[i]; i++; } out[i++] = 'p'; out[i++] = (char)('0' + idx + 1); out[i] = '\0'; } static void mount_device(blk_dev_t *d, unsigned char *buf) { mbr_part_t parts[4]; fat_info_t fi; int n, j, neb_mbr; int any_fat = 0; if (!d->present) return; if (d->kind == BLK_FLOPPY) { mount_one(d, 0, d->name); return; } /* IDE/SATA/USB: MBR -> FAT partitions, else superfloppy. */ if (blk_read(d, 0, buf, 1) != 0) return; n = mbr_parse(buf, parts); neb_mbr = mbr_is_nebos_layout(buf); for (j = 0; j < n; j++) { char pname[16]; int neb; if (!parts[j].is_fat || parts[j].start_lba == 0 || parts[j].sectors == 0) continue; /* The NebOS boot stick's P1 is the raw boot sector + kernel, * not a filesystem; the layout check above avoids a USB read * of it on every boot, the content check is the fallback. */ if (neb_mbr && parts[j].index == 0) continue; neb = is_nebos_boot_partition(d, parts[j].start_lba); if (neb != 0) /* NebOS or unreadable: not FAT */ continue; any_fat = 1; part_name(pname, d->name, parts[j].index); mount_one(d, parts[j].start_lba, pname); } /* A valid MBR whose partitions are all non-FAT (the NebOS boot stick) * is not a superfloppy; only probe one when the sector has no MBR * entries, or still looks like a FAT boot sector with stray partition * bytes (a formatted superfloppy). */ if (!any_fat && (n == 0 || fat_detect(buf, &fi) == 0)) mount_one(d, 0, d->name); } /* Mounts remember their source device, so /data can be named independently * of pN and still be found by a remount. */ static int mount_belongs_to(const vfs_mount_t *m, const char *dev) { return m->dev[0] != '\0' && name_eq(m->dev, dev); } static void unmount_device(const char *dev) { unsigned int i, j; for (i = 0; i < vfs_mount_count; ) { if (mount_belongs_to(&vfs_mounts[i], dev)) { for (j = i + 1; j < vfs_mount_count; j++) vfs_mounts[j - 1] = vfs_mounts[j]; vfs_mount_count--; } else { i++; } } } void vfs_mount_all(int skip_usb) { static unsigned char buf[512]; unsigned int i; /* Publish the scan before touching the table: a ring-3 file syscall * on another CPU waits for it instead of resolving a path halfway * through the rebuild. At boot current_proc() is idle (pid 0). */ vfs_scan_owner_pid = current_proc() ? current_proc()->pid : 0; vfs_scan_busy = 1; vfs_mount_count = 0; /* Non-block filesystems first. The WM and terminal live on /ramfs, * so they stay usable while the (possibly slow) disk scan runs. */ vfs_mount_ramfs(); vfs_mount_share(); blk_scan(); if (!skip_usb) { /* A user-initiated `storage` rescan gets the full USB recovery * path, including the long first-read deadline. */ usbmsc_set_probe_only(0); usbmsc_begin_full_scan(); } for (i = 0; i < blk_dev_count; i++) { unsigned int t0; if (skip_usb && blk_devs[i].kind == BLK_USB) { serial_puts(SERIAL_COM1, "vfs: "); serial_puts(SERIAL_COM1, blk_devs[i].name); serial_puts(SERIAL_COM1, " deferred\n"); continue; } t0 = get_ticks_ms(); mount_device(&blk_devs[i], buf); serial_puts(SERIAL_COM1, "vfs: "); serial_puts(SERIAL_COM1, blk_devs[i].name); serial_puts(SERIAL_COM1, " mount "); serial_putdec(SERIAL_COM1, get_ticks_ms() - t0); serial_puts(SERIAL_COM1, " ms\n"); } serial_puts(SERIAL_COM1, "vfs: "); serial_putdec(SERIAL_COM1, vfs_mount_count); serial_puts(SERIAL_COM1, " mount(s)\n"); vfs_scan_busy = 0; } static int device_is_mounted(const char *dev) { unsigned int i; for (i = 0; i < vfs_mount_count; i++) if (mount_belongs_to(&vfs_mounts[i], dev)) return 1; return 0; } /* Mount the USB disks the boot scan deferred. A card reader or a stick * can NAK its first media read for several seconds while it initialises, so * this runs after the bootscan window has closed: the desktop, /ramfs, * /share and the network are already usable while it waits. */ void vfs_mount_usb(void) { static unsigned char buf[512]; unsigned int i; int any = 0; vfs_scan_owner_pid = current_proc() ? current_proc()->pid : 0; vfs_scan_busy = 1; usbmsc_set_probe_only(1); for (i = 0; i < blk_dev_count; i++) { blk_dev_t *d = &blk_devs[i]; unsigned int t0; if (d->kind != BLK_USB || !d->present || device_is_mounted(d->name)) continue; t0 = get_ticks_ms(); mount_device(d, buf); serial_puts(SERIAL_COM1, "vfs: "); serial_puts(SERIAL_COM1, d->name); serial_puts(SERIAL_COM1, " mount "); serial_putdec(SERIAL_COM1, get_ticks_ms() - t0); serial_puts(SERIAL_COM1, " ms\n"); any = 1; } usbmsc_set_probe_only(0); if (any) serial_puts(SERIAL_COM1, "vfs: usb storage scanned\n"); vfs_scan_busy = 0; } static void remount(blk_dev_t *d) { unsigned char buf[512]; fat_cache_clear(); unmount_device(d->name); mount_device(d, buf); } void vfs_remount_device(blk_dev_t *d) { if (!d) return; remount(d); if (!device_is_mounted(d->name)) { io_putstr("vfs: remount of "); io_putstr(d->name); io_putstr(" found no filesystem\n"); } } vfs_mount_t *vfs_find(const char *name) { unsigned int i; for (i = 0; i < vfs_mount_count; i++) { unsigned int j = 0; while (vfs_mounts[i].name[j] && name[j] && vfs_mounts[i].name[j] == name[j]) j++; if (vfs_mounts[i].name[j] == '\0' && name[j] == '\0') return &vfs_mounts[i]; } return 0; } void vfs_list_root(void) { unsigned int i; for (i = 0; i < vfs_mount_count; i++) { vfs_mount_t *m = &vfs_mounts[i]; io_putstr(m->name); io_putstr(" "); io_putstr(m->type); io_putchar(' '); put_size_sectors(m->total_sectors); if (m->label[0]) { io_putstr(" \""); io_putstr(m->label); io_putchar('"'); } if (m->ro) io_putstr(" (ro)"); io_putchar('\n'); } } int vfs_resolve(const char *path, struct vfs_mount **m_out, char *rel_path, unsigned int rel_size) { unsigned int i = 0, j = 0, o = 0; char name[16]; vfs_mount_t *m; if (!path || path[0] != '/' || rel_size == 0) return -1; i = 1; while (path[i] && path[i] != '/' && j < sizeof(name) - 1) name[j++] = path[i++]; name[j] = '\0'; if (j == 0) return -1; /* path is just "/" */ m = vfs_find(name); if (!m) return -1; /* Copy the remainder as a normalized relative path: skip slashes, drop * "." components and never leave a trailing one. */ while (path[i]) { unsigned int clen = 0; const char *comp; while (path[i] == '/') i++; if (!path[i]) break; comp = path + i; while (path[i] && path[i] != '/') { i++; clen++; } if (clen == 1 && comp[0] == '.') continue; if (o + clen + 2 > rel_size) return -1; if (o > 0) rel_path[o++] = '/'; { unsigned int k; for (k = 0; k < clen; k++) rel_path[o++] = comp[k]; } } rel_path[o] = '\0'; *m_out = m; return 0; } int vfs_load_file(const char *path, struct vfs_file *out, struct vfs_mount **m_out) { struct vfs_mount *m; char rel[VFS_PATH_MAX]; int r; if (vfs_resolve(path, &m, rel, sizeof(rel)) != 0) return VFS_ENOENT; r = m->ops->load_file(m, rel, out); if (r == 0 && m_out) *m_out = m; return r; } void vfs_release_file(struct vfs_mount *m, struct vfs_file *f) { if (m && m->ops->release_file) m->ops->release_file(m, f); } int vfs_stat(const char *path, unsigned int *size, int *is_dir) { struct vfs_mount *m; char rel[VFS_PATH_MAX]; if (vfs_resolve(path, &m, rel, sizeof(rel)) != 0) return VFS_ENOENT; return m->ops->stat(m, rel, size, is_dir); } int vfs_list_dir(const char *path, unsigned int index, struct vfs_dirent *out) { struct vfs_mount *m; char rel[VFS_PATH_MAX]; if (vfs_resolve(path, &m, rel, sizeof(rel)) != 0) return VFS_ENOENT; return m->ops->list_dir(m, rel, index, out); } int vfs_join_path(const char *cwd, const char *arg, char *out, unsigned int out_size) { char buf[VFS_PATH_MAX * 2 + 2]; unsigned int bi = 0; unsigned int len; const char *p; /* Build the combined (un-normalized) path. */ if (arg[0] == '/') { p = arg; } else { p = cwd; while (*p && bi < sizeof(buf) - 1) buf[bi++] = *p++; if (bi > 0 && buf[bi - 1] != '/') { if (bi < sizeof(buf) - 1) buf[bi++] = '/'; } p = arg; } while (*p && bi < sizeof(buf) - 1) buf[bi++] = *p++; buf[bi] = '\0'; if (out_size < 2) return -1; /* Normalize into out (always begins with "/"). */ out[0] = '/'; out[1] = '\0'; len = 1; p = buf; while (*p) { unsigned int clen = 0; const char *comp; while (*p == '/') p++; if (!*p) break; comp = p; while (*p && *p != '/') { p++; clen++; } if (clen == 1 && comp[0] == '.') continue; if (clen == 2 && comp[0] == '.' && comp[1] == '.') { /* Pop the previous component (may leave "/"). */ while (len > 1 && out[len - 1] != '/') len--; if (len > 1) len--; out[len] = '\0'; continue; } /* Append "/component". */ if (len + 1 + clen + 1 > out_size) return -1; if (len > 1) out[len++] = '/'; { unsigned int k; for (k = 0; k < clen; k++) out[len++] = comp[k]; } out[len] = '\0'; } return 0; } int vfs_device_known(const char *path) { unsigned int i = 0, j = 0; char name[16]; if (!path || path[0] != '/') return 0; i = 1; while (path[i] && path[i] != '/' && j < sizeof(name) - 1) name[j++] = path[i++]; name[j] = '\0'; if (j == 0) return 0; return blk_find(name) != 0; }