/* disks: the ngui storage manager. * * Lists every block device (SYS_BLKDEV) with its MBR partitions, read as raw * sectors through SYS_BLK_READ, and every mounted filesystem. A FAT volume * can be mounted/unmounted, renamed and made read-only (SYS_MOUNT_CTL; the * policies are saved to /data/MOUNTS.CFG and applied again by the boot * scan). A device can be formatted or have NebOS installed on it: those run * the privileged `format -y` / `install -y` kernel commands on a private tty * and show their output, after this window asked for confirmation. The * `storage` command remains the text view. */ #include #include #include "ngui.h" #include "ucmd_lib.h" #define MAX_DISKS 16 #define MAX_MOUNTS 24 /* VFS_MAX_MOUNTS */ #define MAX_VOLS 5 #define TOOLBAR_H 40 #define LIST_W 230 #define SECTION_H 28 #define ROW_H 42 #define GAP 10 #define KIND_FLOPPY 0 enum { ID_RESCAN = 1, ID_OVERVIEW, ID_FORMAT, ID_INSTALL, ID_MOUNT_TOGGLE, ID_RO, ID_AUTO, ID_NAME, ID_APPLY, ID_RESET, ID_OPEN, ID_DLG_SIZE, ID_DLG_CANCEL, ID_DLG_OK, ID_DLG_OK2, ID_DLG_CLOSE, ID_DISK_BASE = 100, ID_PART_BASE = 200, ID_MOUNT_BASE = 300, }; /* One MBR partition (or the whole device for a floppy/superfloppy). */ struct part { int slot; /* MBR slot 0..3, -1 = whole device */ unsigned char type, active; unsigned int start, sectors; int fat; /* has a FAT boot sector */ int nebos_boot; /* the NebOS boot sector + kernel */ char fs[8]; char label[12]; char key[16]; /* default mount name */ }; struct disk { struct sys_blkdev bd; unsigned int index; /* SYS_BLKDEV / SYS_BLK_READ index */ int read_ok; /* sector 0 was readable */ int mbr; /* a valid partition table */ int nparts; struct part parts[4]; int nebos; /* boot partition + rootfs partition */ int has_data; /* a NEBOSDATA partition */ }; enum dialog { DLG_NONE, DLG_FORMAT, DLG_INSTALL, DLG_JOB }; static struct ngui ui; static struct disk disks[MAX_DISKS]; static int ndisks; static struct sys_mount mounts[MAX_MOUNTS]; static int nmounts; static struct sys_mount_policy pols[SYS_MOUNT_POLICY_MAX]; static int npols; static int sel_disk = -1; /* -1 = mount overview */ static char sel_key[16]; /* selected volume */ static char edit_key[16]; /* volume the editor was loaded for */ static struct ngui_input name_in; static int edit_ro, edit_auto; static struct ngui_scroll detail_scroll, list_scroll; static char status[128]; static int status_err; static unsigned int status_until; static enum dialog dialog; static int rescan_pending; static struct ngui_input size_in; static int dlg_disk; /* The running format/install. */ static int job_tty = -1, job_pid = -1; static int job_done, job_ok; static char job_title[64]; static char job_log[4096]; static unsigned int job_len; /* ---- helpers ---- */ static void set_status(const char *msg, int err) { snprintf(status, sizeof(status), "%s", msg); status_err = err; status_until = u_ticks_ms() + 8000; ngui_invalidate(&ui); } static unsigned int rd32(const unsigned char *p) { return p[0] | (p[1] << 8) | (p[2] << 16) | ((unsigned int)p[3] << 24); } static void fmt_size(char *out, unsigned int n, unsigned int sectors) { if (sectors >= 2u * 1024u * 1024u) snprintf(out, n, "%u.%u GB", sectors >> 21, ((sectors & 0x1FFFFFu) * 10u) >> 21); else if (sectors >= 2048u) snprintf(out, n, "%u.%u MB", sectors >> 11, ((sectors & 0x7FFu) * 10u) >> 11); else snprintf(out, n, "%u KB", sectors / 2u); } static const char *kind_name(unsigned int kind) { static const char *names[] = { "Floppy", "IDE", "SATA", "eMMC", "USB" }; return kind < 5 ? names[kind] : "Disk"; } static const char *type_name(unsigned char t) { switch (t) { case 0x01: return "FAT12"; case 0x04: case 0x06: case 0x0E: return "FAT16"; case 0x0B: case 0x0C: return "FAT32"; case 0x05: case 0x0F: return "Extended"; case 0x07: return "NTFS/exFAT"; case 0x7F: return "NebOS rootfs"; case 0x82: return "Linux swap"; case 0x83: return "Linux"; case 0xEE: return "GPT"; case 0xEF: return "EFI System"; default: return "Unknown"; } } /* Copy a space-padded BPB field and trim it. */ static void bpb_str(char *out, const unsigned char *p, unsigned int n) { unsigned int i; for (i = 0; i < n; i++) out[i] = (p[i] >= 0x20 && p[i] < 0x7F) ? (char)p[i] : ' '; while (i > 0 && out[i - 1] == ' ') i--; out[i] = '\0'; } /* Classify a partition's (or a superfloppy's) first sector. */ static void probe_boot_sector(struct part *p, const unsigned char *s) { unsigned int bps = s[11] | (s[12] << 8); p->fat = 0; p->nebos_boot = 0; p->fs[0] = p->label[0] = '\0'; if (s[510] != 0x55 || s[511] != 0xAA) return; if (s[3] == 'N' && s[4] == 'E' && s[5] == 'B' && s[6] == 'O' && s[7] == 'S') { p->nebos_boot = 1; snprintf(p->fs, sizeof(p->fs), "NebOS"); return; } if (bps != 512 || (s[0] != 0xEB && s[0] != 0xE9) || s[13] == 0) return; p->fat = 1; if ((s[0x16] | (s[0x17] << 8)) == 0) { /* FAT32 BPB */ snprintf(p->fs, sizeof(p->fs), "FAT32"); bpb_str(p->label, s + 0x47, 11); } else { bpb_str(p->fs, s + 0x36, 5); if (strncmp(p->fs, "FAT", 3) != 0) snprintf(p->fs, sizeof(p->fs), "FAT"); bpb_str(p->label, s + 0x2B, 11); } if (!strcmp(p->label, "NO NAME")) p->label[0] = '\0'; } static int blk_read(unsigned int index, unsigned int lba, unsigned char *buf) { return u_syscall3(SYS_BLK_READ, (int)index, (int)lba, (int)buf); } /* Read the partition table and the first sector of every partition. A * floppy is never read here: without a disk the controller retries for * seconds, and it has no partitions anyway. */ static void probe_disk(struct disk *d) { static unsigned char s[512]; unsigned int i; d->read_ok = d->mbr = d->nparts = d->nebos = d->has_data = 0; if (!d->bd.present) return; if (d->bd.kind == KIND_FLOPPY) { struct part *p = &d->parts[d->nparts++]; memset(p, 0, sizeof(*p)); p->slot = -1; p->sectors = d->bd.total_sectors; p->fat = 1; snprintf(p->key, sizeof(p->key), "%s", d->bd.name); return; } if (blk_read(d->index, 0, s) != 0) return; d->read_ok = 1; if (s[510] == 0x55 && s[511] == 0xAA) { int ok = 1, n = 0; for (i = 0; i < 4; i++) { const unsigned char *e = s + 0x1BE + i * 16; unsigned int start = rd32(e + 8), cnt = rd32(e + 12); if (e[4] == 0) continue; if ((e[0] != 0 && e[0] != 0x80) || start == 0 || cnt == 0 || start > d->bd.total_sectors || cnt > d->bd.total_sectors - start) ok = 0; n++; } d->mbr = ok && n > 0; } if (!d->mbr) { struct part *p = &d->parts[0]; memset(p, 0, sizeof(*p)); p->slot = -1; p->sectors = d->bd.total_sectors; probe_boot_sector(p, s); snprintf(p->key, sizeof(p->key), "%s", d->bd.name); if (p->fat) d->nparts = 1; return; } for (i = 0; i < 4; i++) { const unsigned char *e = s + 0x1BE + i * 16; struct part *p; if (e[4] == 0) continue; p = &d->parts[d->nparts++]; memset(p, 0, sizeof(*p)); p->slot = (int)i; p->type = e[4]; p->active = e[0] == 0x80; p->start = rd32(e + 8); p->sectors = rd32(e + 12); snprintf(p->key, sizeof(p->key), "%sp%u", d->bd.name, i + 1); } for (i = 0; i < (unsigned int)d->nparts; i++) { struct part *p = &d->parts[i]; if (p->type == 0x05 || p->type == 0x0F || p->type == 0xEE || p->type == 0x7F) continue; if (blk_read(d->index, p->start, s) == 0) probe_boot_sector(p, s); if (p->nebos_boot && p->slot == 0) d->nebos |= 1; if (p->fat && !strcmp(p->label, "NEBOSDATA")) d->has_data = 1; } for (i = 0; i < (unsigned int)d->nparts; i++) if (d->parts[i].type == 0x7F) d->nebos |= 2; d->nebos = d->nebos == 3; } static void load_mounts(void) { int i; nmounts = 0; for (i = 0; i < MAX_MOUNTS; i++) { if (u_syscall2(SYS_LIST_MOUNTS, i, (int)&mounts[nmounts]) != 1) break; nmounts++; } npols = 0; for (i = 0; i < SYS_MOUNT_POLICY_MAX; i++) { if (u_syscall3(SYS_MOUNT_CTL, SYS_MOUNT_POLICY_GET, i, (int)&pols[npols]) != 1) break; npols++; } } /* USB INQUIRY strings are space-padded vendor/product fields. */ static void squeeze(char *s) { char *o = s; const char *p; for (p = s; *p; p++) if (*p != ' ' || (o > s && o[-1] != ' ')) *o++ = *p; if (o > s && o[-1] == ' ') o--; *o = '\0'; } static void load_all(void) { int i; ndisks = 0; for (i = 0; i < MAX_DISKS; i++) { struct disk *d = &disks[ndisks]; if (u_syscall2(SYS_BLKDEV, i, (int)&d->bd) != 1) break; d->index = (unsigned int)i; squeeze(d->bd.model); probe_disk(d); ndisks++; } if (sel_disk >= ndisks) sel_disk = -1; load_mounts(); edit_key[0] = '\0'; /* reload the editor from the new state */ } static const struct sys_mount *mount_by_key(const char *key) { int i; for (i = 0; i < nmounts; i++) if (mounts[i].dev[0] && !strcmp(mounts[i].key, key)) return &mounts[i]; return 0; } static const struct sys_mount *mount_by_label(const char *label) { int i; for (i = 0; i < nmounts; i++) if (mounts[i].dev[0] && !strcmp(mounts[i].label, label)) return &mounts[i]; return 0; } static const struct sys_mount_policy *policy_by_key(const char *key) { int i; for (i = 0; i < npols; i++) if (!strcmp(pols[i].key, key)) return &pols[i]; return 0; } /* The volume carrying the saved policies: never edited from here, since an * unmounted, renamed or read-only /data could not store the change. */ static int is_data_volume(const struct part *p) { const struct sys_mount *m = mount_by_key(p->key); return !strcmp(p->label, "NEBOSDATA") || (m && !strcmp(m->name, "data")); } /* ---- mount policies ---- */ static int save_policies(void) { static struct { char magic[8]; struct sys_mount_policy p[SYS_MOUNT_POLICY_MAX]; } cfg; const struct sys_mount *data = mount_by_label("NEBOSDATA"); char path[48]; if (!data) return 0; memcpy(cfg.magic, SYS_MOUNT_CFG_MAGIC, 8); memcpy(cfg.p, pols, sizeof(pols[0]) * (unsigned int)npols); snprintf(path, sizeof(path), "/%s/MOUNTS.CFG", data->name); return u_syscall3(SYS_WRITE_FILE, (int)path, (int)&cfg, 8 + (int)sizeof(pols[0]) * npols) == 0 ? 1 : -1; } static int name_valid(const char *s) { unsigned int i; for (i = 0; s[i]; i++) if (i >= 15 || !((s[i] >= 'a' && s[i] <= 'z') || (s[i] >= '0' && s[i] <= '9') || s[i] == '_' || s[i] == '-')) return 0; return 1; } static void apply_policy(const char *key, const char *name, unsigned int flags) { struct sys_mount_policy p; const struct sys_mount *cur = mount_by_key(key); char msg[96]; int i, saved; if (!name_valid(name)) { set_status("Mount name: up to 15 of a-z, 0-9, _ and -", 1); return; } if (!strcmp(name, "ramfs") || !strcmp(name, "share") || !strcmp(name, "data")) { set_status("That mount name is reserved", 1); return; } for (i = 0; name[0] && i < nmounts; i++) if (!strcmp(mounts[i].name, name) && &mounts[i] != cur) { set_status("Another volume is mounted with that name", 1); return; } memset(&p, 0, sizeof(p)); snprintf(p.key, sizeof(p.key), "%s", key); if (strcmp(name, key) != 0) snprintf(p.name, sizeof(p.name), "%s", name); p.flags = flags; if (u_syscall3(SYS_MOUNT_CTL, SYS_MOUNT_POLICY_SET, (int)&p, 0) != 0) { set_status("The kernel refused the mount settings", 1); return; } load_mounts(); edit_key[0] = '\0'; saved = save_policies(); cur = mount_by_key(key); if (flags & SYS_MOUNT_NOAUTO) snprintf(msg, sizeof(msg), "%s is unmounted", key); else if (cur) snprintf(msg, sizeof(msg), "%s is mounted at /%s%s", key, cur->name, cur->ro ? " (read-only)" : ""); else snprintf(msg, sizeof(msg), "%s has no readable filesystem", key); if (saved == 0) snprintf(msg + strlen(msg), sizeof(msg) - strlen(msg), "; not saved (no /data)"); else if (saved < 0) snprintf(msg + strlen(msg), sizeof(msg) - strlen(msg), "; saving failed"); set_status(msg, !cur && !(flags & SYS_MOUNT_NOAUTO)); /* Applied, but only until the next boot: a warning, not an error. */ if (saved == 0 && (cur || (flags & SYS_MOUNT_NOAUTO))) status_err = 2; } /* ---- format / install jobs ---- */ static void start_job(const char *cmd, const char *arg, const char *title) { if (job_tty < 0) job_tty = u_tty_create(); if (job_tty < 0) { set_status("No terminal for the command", 1); return; } job_len = 0; job_log[0] = '\0'; job_done = job_ok = 0; snprintf(job_title, sizeof(job_title), "%s", title); job_pid = u_spawn_kcmd(cmd, arg, job_tty); if (job_pid < 0) { set_status("Could not start the command", 1); dialog = DLG_NONE; return; } dialog = DLG_JOB; ngui_invalidate(&ui); } /* The log is drawn in a 13 px monospace face; longer lines are wrapped * here so the dialog never clips them. */ #define JOB_LOG_COLS 66 static void job_append(const char *s, int n) { static unsigned int col; int i; for (i = 0; i < n; i++) { if (s[i] == '\r') continue; if (s[i] != '\n' && col >= JOB_LOG_COLS) { job_append("\n", 1); } col = s[i] == '\n' ? 0 : col + 1; if (job_len + 1 >= sizeof(job_log)) { /* Keep the newest half of the log. */ unsigned int keep = sizeof(job_log) / 2; memmove(job_log, job_log + job_len - keep, keep); job_len = keep; } job_log[job_len++] = s[i]; } job_log[job_len] = '\0'; } static void job_poll(void) { char buf[256]; int n, any = 0; if (job_tty < 0) return; while ((n = u_tty_read(job_tty, buf, sizeof(buf))) > 0) { if (dialog == DLG_JOB && !job_done) job_append(buf, n); any = 1; } if (dialog == DLG_JOB && !job_done && job_pid > 0 && !u_alive((unsigned int)job_pid)) { while ((n = u_tty_read(job_tty, buf, sizeof(buf))) > 0) job_append(buf, n); job_done = 1; job_pid = -1; job_ok = strstr(job_log, "done") && !strstr(job_log, "failed") && !strstr(job_log, "aborted"); load_all(); any = 1; } if (any) ngui_invalidate(&ui); } /* ---- drawing ---- */ /* Widgets drawn under a dialog must not react: hide the pointer edges. */ struct input_save { unsigned int pressed, released; int inside, scroll_y; }; static void input_mute(struct input_save *s) { s->pressed = ui.pressed; s->released = ui.released; s->inside = ui.inside; s->scroll_y = ui.scroll_y; ui.pressed = ui.released = 0; ui.inside = 0; ui.scroll_y = 0; } static void input_restore(const struct input_save *s) { ui.pressed = s->pressed; ui.released = s->released; ui.inside = s->inside; ui.scroll_y = s->scroll_y; } static int visible(struct ngui_rect r, struct ngui_rect area) { return r.y + r.h > area.y && r.y < area.y + area.h; } static void text_dim(int x, int y, const char *s) { ngui_text(&ui, x, y, s, ui.theme.text_dim); } static void draw_toolbar(struct ngui_rect r) { struct ngui_rect b; unsigned short c; ngui_fill_rect(&ui, r, ui.theme.panel); ngui_inset(&r, 6); b = ngui_cut_left(&r, 92); if (ngui_button(&ui, ID_RESCAN, b, "Rescan")) { set_status("Scanning devices...", 0); rescan_pending = 1; } ngui_cut_left(&r, 12); if (status[0]) { c = status_err == 2 ? ui.theme.warning : status_err ? ui.theme.error : ui.theme.success; ngui_text_ellipsis(&ui, r, r.x, r.y + (r.h - 16) / 2, status, c); } else { ngui_text_ellipsis(&ui, r, r.x, r.y + (r.h - 16) / 2, "Storage devices, volumes and mounts", ui.theme.text_dim); } } /* Empty IDE channels and absent eMMC/USB slots are registered as devices * but have nothing to show; a floppy drive is listed without media too. */ static int listed(const struct disk *d) { return d->bd.present || d->bd.kind == KIND_FLOPPY; } static void draw_device_list(struct ngui_rect area) { struct ngui_row row; struct ngui_rect r, col; char sub[64], size[16]; int i, y; ngui_fill_rect(&ui, area, ui.theme.panel); ngui_inset(&area, 6); for (i = 0, y = 0; i < ndisks; i++) y += listed(&disks[i]); list_scroll.content_h = (y + 1) * ROW_H + 2 * SECTION_H; ngui_scroll_begin(&ui, area, &list_scroll); ngui_scroll_wheel(&ui, area, &list_scroll); col = area; y = area.y - list_scroll.offset; r = (struct ngui_rect){ col.x, y, col.w, SECTION_H }; ngui_section(&ui, r, "Overview", 0, 0); y += SECTION_H; r = (struct ngui_rect){ col.x, y, col.w, ROW_H }; if (visible(r, area)) { snprintf(sub, sizeof(sub), "%d filesystem%s", nmounts, nmounts == 1 ? "" : "s"); row.title = "All mounts"; row.subtitle = sub; row.trailing = 0; row.bars = -1; row.selected = sel_disk < 0; if (ngui_list_row(&ui, ID_OVERVIEW, r, &row)) { sel_disk = -1; detail_scroll.offset = 0; ngui_invalidate(&ui); } } y += ROW_H; r = (struct ngui_rect){ col.x, y, col.w, SECTION_H }; ngui_section(&ui, r, "Devices", 0, 0); y += SECTION_H; for (i = 0; i < ndisks; i++) { const struct disk *d = &disks[i]; if (!listed(d)) continue; r = (struct ngui_rect){ col.x, y, col.w, ROW_H }; y += ROW_H; if (!visible(r, area)) continue; fmt_size(size, sizeof(size), d->bd.total_sectors); if (!d->bd.present) snprintf(sub, sizeof(sub), "%s, not present", kind_name(d->bd.kind)); else snprintf(sub, sizeof(sub), "%s %s", kind_name(d->bd.kind), d->bd.kind == KIND_FLOPPY ? "" : size); row.title = d->bd.name; row.subtitle = sub; row.trailing = d->nebos ? "NebOS" : 0; row.bars = -1; row.selected = sel_disk == i; if (ngui_list_row(&ui, ID_DISK_BASE + i, r, &row) && sel_disk != i) { sel_disk = i; sel_key[0] = '\0'; detail_scroll.offset = 0; ngui_invalidate(&ui); } } ngui_scroll_end(&ui, area, &list_scroll); } /* The overview: one line per mount. */ static int draw_overview(struct ngui_rect area, int y) { static const int col[] = { 0, 96, 156, 230, 330, 404 }; static const char *hdr[] = { "Mount", "Type", "Size", "Label", "Device", "Mode" }; char size[16]; int i, j; ngui_text_px(&ui, area.x, y, "All mounts", 10, ui.theme.text, 18, 1, 0, 0); y += 32; for (j = 0; j < 6; j++) text_dim(area.x + col[j], y, hdr[j]); y += 22; ngui_fill_rect(&ui, (struct ngui_rect){ area.x, y - 4, area.w, 1 }, ui.theme.border); for (i = 0; i < nmounts; i++) { const struct sys_mount *m = &mounts[i]; struct ngui_rect r = { area.x, y, area.w, 26 }; char name[20]; if (visible(r, area)) { snprintf(name, sizeof(name), "/%s", m->name); fmt_size(size, sizeof(size), m->total_sectors); ngui_text(&ui, area.x + col[0], y + 4, name, ui.theme.text); ngui_text(&ui, area.x + col[1], y + 4, m->type, ui.theme.text); ngui_text(&ui, area.x + col[2], y + 4, size, ui.theme.text); ngui_text(&ui, area.x + col[3], y + 4, m->label, ui.theme.text); ngui_text(&ui, area.x + col[4], y + 4, m->dev[0] ? m->key : "RAM", ui.theme.text_dim); ngui_text(&ui, area.x + col[5], y + 4, m->ro ? "read-only" : "read-write", m->ro ? ui.theme.warning : ui.theme.text_dim); if (m->dev[0] && ngui_button(&ui, ID_MOUNT_BASE + i, (struct ngui_rect){ area.x + area.w - 60, y + 1, 60, 24 }, "Show")) { for (j = 0; j < ndisks; j++) if (!strcmp(disks[j].bd.name, m->dev)) sel_disk = j; snprintf(sel_key, sizeof(sel_key), "%s", m->key); detail_scroll.offset = 0; ngui_invalidate(&ui); } } y += 28; } y += GAP; text_dim(area.x, y, "Select a device to format it, install NebOS or change how"); y += 18; text_dim(area.x, y, "its volumes are mounted."); return y + 24; } static void load_editor(const struct part *p) { const struct sys_mount_policy *pol = policy_by_key(p->key); const struct sys_mount *m = mount_by_key(p->key); snprintf(edit_key, sizeof(edit_key), "%s", p->key); if (pol && pol->name[0]) ngui_input_set(&name_in, pol->name); else ngui_input_set(&name_in, m ? m->name : p->key); edit_ro = pol && (pol->flags & SYS_MOUNT_RO); edit_auto = !(pol && (pol->flags & SYS_MOUNT_NOAUTO)); } static int draw_mount_editor(struct ngui_rect area, int y, const struct part *p) { const struct sys_mount *m = mount_by_key(p->key); struct ngui_rect r, b; char line[96], name[16]; unsigned int flags; if (strcmp(edit_key, p->key) != 0) load_editor(p); r = (struct ngui_rect){ area.x, y, area.w, SECTION_H }; snprintf(line, sizeof(line), "Volume %s", p->key); ngui_section(&ui, r, line, 0, 0); y += SECTION_H; if (m) snprintf(line, sizeof(line), "Mounted at /%s, %s, %s", m->name, m->type, m->ro ? "read-only" : "read-write"); else snprintf(line, sizeof(line), "Not mounted"); ngui_text(&ui, area.x, y + 2, line, m ? ui.theme.success : ui.theme.text_dim); y += 24; if (is_data_volume(p)) { text_dim(area.x, y, "This is the NebOS data volume (/data). It holds the"); y += 18; text_dim(area.x, y, "saved settings, so it is always mounted read-write."); y += 26; if (m && ngui_button(&ui, ID_OPEN, (struct ngui_rect){ area.x, y, 130, 28 }, "Open in Files")) goto open_files; return y + 38; } text_dim(area.x, y + 6, "Mount name"); r = (struct ngui_rect){ area.x + 110, y, 180, 28 }; if (visible(r, area)) ngui_text_input(&ui, ID_NAME, r, &name_in); y += 34; r = (struct ngui_rect){ area.x, y, 290, 26 }; if (visible(r, area) && ngui_menu_item(&ui, ID_AUTO, r, "Mount automatically", 0, edit_auto)) edit_auto = !edit_auto; y += 28; r = (struct ngui_rect){ area.x, y, 290, 26 }; if (visible(r, area) && ngui_menu_item(&ui, ID_RO, r, "Read-only", 0, edit_ro)) edit_ro = !edit_ro; y += 34; ngui_input_value(&name_in, name, sizeof(name)); flags = (edit_ro ? SYS_MOUNT_RO : 0) | (edit_auto ? 0 : SYS_MOUNT_NOAUTO); b = (struct ngui_rect){ area.x, y, 90, 28 }; if (visible(b, area) && ngui_button(&ui, ID_APPLY, b, "Apply")) apply_policy(p->key, name, flags); b.x += 98; b.w = 100; if (visible(b, area) && ngui_button(&ui, ID_MOUNT_TOGGLE, b, m ? "Unmount" : "Mount")) { if (m) apply_policy(p->key, name, flags | SYS_MOUNT_NOAUTO); else apply_policy(p->key, name, flags & ~SYS_MOUNT_NOAUTO); } b.x += 108; b.w = 90; if (visible(b, area) && ngui_button(&ui, ID_RESET, b, "Defaults")) apply_policy(p->key, "", 0); b.x += 98; b.w = 130; if (m && visible(b, area) && ngui_button(&ui, ID_OPEN, b, "Open in Files")) goto open_files; y += 38; text_dim(area.x, y, "Settings apply at once and are saved to /data/MOUNTS.CFG,"); y += 18; text_dim(area.x, y, "so the boot scan uses them too."); return y + 26; open_files: { char cwd[24]; snprintf(cwd, sizeof(cwd), "/%s", m->name); if (u_spawn_detached("/ramfs/bin/files", "", 0, cwd) < 0) set_status("Could not start files", 1); } return y + 38; } static int draw_disk(struct ngui_rect area, int y, struct disk *d) { struct ngui_row row; struct ngui_rect r, b; char line[128], size[16], title[48], sub[80]; const struct part *sel = 0; int i; snprintf(title, sizeof(title), "%s", d->bd.name); ngui_text_px(&ui, area.x, y, title, (unsigned int)strlen(title), ui.theme.text, 18, 1, 0, 0); y += 30; fmt_size(size, sizeof(size), d->bd.total_sectors); if (d->bd.kind == KIND_FLOPPY) snprintf(line, sizeof(line), "%s drive", kind_name(d->bd.kind)); else snprintf(line, sizeof(line), "%s%s%s, %s (%u sectors)", kind_name(d->bd.kind), d->bd.model[0] ? " " : "", d->bd.model, size, d->bd.total_sectors); text_dim(area.x, y, line); y += 20; if (!d->bd.present) { ngui_text(&ui, area.x, y, "Not present", ui.theme.warning); return y + 30; } if (d->nebos) { ngui_text(&ui, area.x, y, d->has_data ? "NebOS installed, with a /data partition" : "NebOS installed", ui.theme.success); y += 20; } y += GAP; r = (struct ngui_rect){ area.x, y, area.w, SECTION_H }; ngui_section(&ui, r, d->mbr ? "Partitions" : "Volume", 0, 0); y += SECTION_H; if (d->bd.kind != KIND_FLOPPY && !d->read_ok) { ngui_text(&ui, area.x, y + 4, "The device could not be read", ui.theme.error); y += 30; } else if (!d->nparts) { text_dim(area.x, y + 4, "No partition table and no FAT filesystem"); y += 30; } for (i = 0; i < d->nparts; i++) { const struct part *p = &d->parts[i]; const struct sys_mount *m = mount_by_key(p->key); const struct sys_mount_policy *pol = policy_by_key(p->key); const char *what; char trail[24]; int selectable = p->fat && !p->nebos_boot; r = (struct ngui_rect){ area.x, y, area.w, ROW_H }; y += ROW_H; if (selectable && !sel_key[0]) snprintf(sel_key, sizeof(sel_key), "%s", p->key); if (selectable && !strcmp(sel_key, p->key)) sel = p; if (!visible(r, area)) continue; if (p->nebos_boot) what = "NebOS boot + kernel"; else if (p->fat) what = p->fs; else what = type_name(p->type); fmt_size(size, sizeof(size), p->sectors); if (p->slot >= 0) snprintf(title, sizeof(title), "%d %s%s%s", p->slot + 1, what, p->label[0] ? " " : "", p->label); else /* A floppy is never read here; its mount has the label. */ snprintf(title, sizeof(title), "%s%s%s", d->bd.kind == KIND_FLOPPY ? "Floppy disk" : what, (p->label[0] || (m && m->label[0])) ? " " : "", p->label[0] ? p->label : (m ? m->label : "")); if (p->slot >= 0) snprintf(sub, sizeof(sub), "%s, type 0x%02X, LBA %u%s", size, p->type, p->start, p->active ? ", active" : ""); else snprintf(sub, sizeof(sub), "%s, whole device", d->bd.kind == KIND_FLOPPY ? "1.4 MB" : size); if (m) snprintf(trail, sizeof(trail), "/%s%s", m->name, m->ro ? " (ro)" : ""); else if (pol && (pol->flags & SYS_MOUNT_NOAUTO)) snprintf(trail, sizeof(trail), "unmounted"); else snprintf(trail, sizeof(trail), "%s", selectable ? "not mounted" : ""); row.title = title; row.subtitle = sub; row.trailing = trail[0] ? trail : 0; row.bars = -1; row.selected = sel == p; if (ngui_list_row(&ui, ID_PART_BASE + d->index * 4 + (unsigned int)(p->slot < 0 ? 0 : p->slot), r, &row) && selectable) { snprintf(sel_key, sizeof(sel_key), "%s", p->key); ngui_invalidate(&ui); } } y += GAP; if (sel) y = draw_mount_editor(area, y, sel) + GAP; r = (struct ngui_rect){ area.x, y, area.w, SECTION_H }; ngui_section(&ui, r, "Device", 0, 0); y += SECTION_H; b = (struct ngui_rect){ area.x, y, 120, 28 }; if (visible(b, area) && ngui_button(&ui, ID_FORMAT, b, "Format...")) { dialog = DLG_FORMAT; dlg_disk = sel_disk; ngui_input_set(&size_in, ""); ngui_set_focus(&ui, ID_DLG_SIZE); } b.x += 128; b.w = 150; if (d->bd.kind != KIND_FLOPPY && visible(b, area) && ngui_button(&ui, ID_INSTALL, b, "Install NebOS...")) { dialog = DLG_INSTALL; dlg_disk = sel_disk; } y += 36; text_dim(area.x, y, d->bd.kind == KIND_FLOPPY ? "Format writes a fresh FAT12 filesystem." : "Format writes one FAT partition (MBR) over the whole disk."); return y + 26; } static void draw_details(struct ngui_rect area) { struct ngui_rect col; int y, top; ngui_inset(&area, 14); ngui_scroll_begin(&ui, area, &detail_scroll); ngui_scroll_wheel(&ui, area, &detail_scroll); if (!ui.focus) ngui_scroll_keys(&ui, &detail_scroll); col = area; top = area.y - detail_scroll.offset; if (sel_disk < 0) y = draw_overview(col, top); else y = draw_disk(col, top, &disks[sel_disk]); detail_scroll.content_h = y - top; ngui_scroll_end(&ui, area, &detail_scroll); } /* ---- dialogs ---- */ static struct ngui_rect dialog_rect(int w, int h) { struct ngui_rect r; r.w = w < ui.w - 20 ? w : ui.w - 20; r.h = h < ui.h - 20 ? h : ui.h - 20; r.x = (ui.w - r.w) / 2; r.y = (ui.h - r.h) / 2; ngui_fill_round_rect(&ui, (struct ngui_rect){ r.x - 1, r.y - 1, r.w + 2, r.h + 2 }, NGUI_RADIUS + 1, NGUI_RADIUS + 1, NGUI_RADIUS + 1, NGUI_RADIUS + 1, ui.theme.border); ngui_fill_round_rect(&ui, r, NGUI_RADIUS, NGUI_RADIUS, NGUI_RADIUS, NGUI_RADIUS, ui.theme.panel); return r; } static void dialog_title(struct ngui_rect *r, const char *s) { struct ngui_rect t = ngui_cut_top(r, 34); ngui_text_px(&ui, t.x, t.y + 6, s, (unsigned int)strlen(s), ui.theme.text, 16, 1, 0, 0); } static void dialog_line(struct ngui_rect *r, const char *s, unsigned short c) { struct ngui_rect t = ngui_cut_top(r, 20); ngui_text_ellipsis(&ui, t, t.x, t.y, s, c); } static int key_pressed(int key) { if (ngui_key_peek(&ui) == key) { ngui_key(&ui); return 1; } return 0; } static void draw_format_dialog(void) { const struct disk *d = &disks[dlg_disk]; struct ngui_rect r = dialog_rect(460, 260), b; char line[96], size[16], arg[48], val[24]; int floppy = d->bd.kind == KIND_FLOPPY, go = 0, i; ngui_inset(&r, 18); snprintf(line, sizeof(line), "Format %s", d->bd.name); dialog_title(&r, line); fmt_size(size, sizeof(size), d->bd.total_sectors); snprintf(line, sizeof(line), "%s%s%s, %s", kind_name(d->bd.kind), d->bd.model[0] ? " " : "", d->bd.model, floppy ? "1.4 MB" : size); dialog_line(&r, line, ui.theme.text_dim); dialog_line(&r, "Every file and partition on this device is erased.", ui.theme.warning); if (d->nebos) dialog_line(&r, "This removes the NebOS installation as well.", ui.theme.warning); ngui_cut_top(&r, 8); if (!floppy) { b = ngui_cut_top(&r, 30); text_dim(b.x, b.y + 6, "Size"); b.x += 60; b.w = 140; size_in.placeholder = "whole disk"; if (ngui_text_input(&ui, ID_DLG_SIZE, b, &size_in) & NGUI_INPUT_SUBMIT) go = 1; text_dim(b.x + b.w + 10, b.y + 6, "e.g. 100mb, 2gb"); } b = ngui_cut_bottom(&r, 30); b.x = r.x + r.w - 110; b.w = 110; if (ngui_button(&ui, ID_DLG_OK, b, "Erase disk")) go = 1; b.x -= 100; b.w = 92; if (ngui_button(&ui, ID_DLG_CANCEL, b, "Cancel") || key_pressed(NGUI_KEY_ESC)) { dialog = DLG_NONE; return; } if (!go) return; ngui_input_value(&size_in, val, sizeof(val)); for (i = 0; val[i]; i++) if (val[i] == ' ' || (val[i] >= 'A' && val[i] <= 'Z')) val[i] = val[i] == ' ' ? '\0' : (char)(val[i] + 32); snprintf(arg, sizeof(arg), "-y %s%s%s", d->bd.name, (!floppy && val[0]) ? " " : "", floppy ? "" : val); snprintf(line, sizeof(line), "Formatting %s", d->bd.name); ngui_set_focus(&ui, 0); start_job("format", arg, line); } static void draw_install_dialog(void) { const struct disk *d = &disks[dlg_disk]; struct ngui_rect r = dialog_rect(520, d->nebos ? 290 : 250), b; char line[96], arg[48], size[16]; int mode = 0; ngui_inset(&r, 18); snprintf(line, sizeof(line), "Install NebOS on %s", d->bd.name); dialog_title(&r, line); fmt_size(size, sizeof(size), d->bd.total_sectors); snprintf(line, sizeof(line), "%s%s%s, %s", kind_name(d->bd.kind), d->bd.model[0] ? " " : "", d->bd.model, size); dialog_line(&r, line, ui.theme.text_dim); if (d->nebos) { dialog_line(&r, "NebOS is already installed here.", ui.theme.text); dialog_line(&r, "Update writes the running kernel and rootfs to " "partitions 1-2.", ui.theme.text_dim); dialog_line(&r, "Reinstall clones the live image (partitions 1-3).", ui.theme.text_dim); dialog_line(&r, "Both keep the /data partition.", ui.theme.success); } else { dialog_line(&r, "Clones the live NebOS image (boot, rootfs, EFI) and", ui.theme.text_dim); dialog_line(&r, "creates a /data partition on the rest of the disk.", ui.theme.text_dim); dialog_line(&r, "Everything else on this device is erased.", ui.theme.warning); } b = ngui_cut_bottom(&r, 30); b.x = r.x + r.w - 110; b.w = 110; if (ngui_button(&ui, ID_DLG_OK, b, d->nebos ? "Update" : "Install")) mode = d->nebos ? 2 : 1; if (d->nebos) { b.x -= 118; b.w = 110; if (ngui_button(&ui, ID_DLG_OK2, b, "Reinstall")) mode = 1; } b.x -= 100; b.w = 92; if (ngui_button(&ui, ID_DLG_CANCEL, b, "Cancel") || key_pressed(NGUI_KEY_ESC)) { dialog = DLG_NONE; return; } if (!mode) return; snprintf(arg, sizeof(arg), "-y %s%s", d->bd.name, mode == 2 ? " self" : ""); snprintf(line, sizeof(line), "%s NebOS on %s", mode == 2 ? "Updating" : "Installing", d->bd.name); start_job("install", arg, line); } static void draw_job_dialog(void) { struct ngui_rect r = dialog_rect(600, 380), b, log; int lh = 16, lines, n, i, start; const char *p; ngui_inset(&r, 18); dialog_title(&r, job_title); if (!job_done) dialog_line(&r, "Running... keep the devices connected.", ui.theme.warning); else dialog_line(&r, job_ok ? "Finished." : "Failed; see the output below.", job_ok ? ui.theme.success : ui.theme.error); ngui_cut_top(&r, 6); b = ngui_cut_bottom(&r, 30); ngui_cut_bottom(&r, 10); log = r; ngui_fill_rect(&ui, log, ui.theme.bg); ngui_inset(&log, 6); ngui_push_clip(&ui, log); /* Show the newest lines that fit. */ lines = log.h / lh; n = 0; for (p = job_log; *p; p++) if (*p == '\n') n++; if (job_len && job_log[job_len - 1] != '\n') n++; start = n > lines ? n - lines : 0; p = job_log; for (i = 0; *p && i < start; p++) if (*p == '\n') i++; for (i = 0; *p && i < lines; i++) { const char *e = strchr(p, '\n'); unsigned int len = e ? (unsigned int)(e - p) : (unsigned int)strlen(p); ngui_text_px(&ui, log.x, log.y + i * lh, p, len, ui.theme.text, 13, 0, 0, 1); p += len; if (*p == '\n') p++; } ngui_pop_clip(&ui); if (job_done) { b.x = r.x + r.w - 92; b.w = 92; if (ngui_button(&ui, ID_DLG_CLOSE, b, "Close") || key_pressed(NGUI_KEY_ESC) || key_pressed('\n')) dialog = DLG_NONE; } } static void draw(void) { struct ngui_rect all = { 0, 0, ui.w, ui.h }, body, list; struct input_save save; int muted = dialog != DLG_NONE; if (muted) input_mute(&save); ngui_fill_rect(&ui, all, ui.theme.bg); body = all; draw_toolbar(ngui_cut_top(&body, TOOLBAR_H)); list = ngui_cut_left(&body, LIST_W); draw_device_list(list); ngui_fill_rect(&ui, ngui_cut_left(&body, 1), ui.theme.border); draw_details(body); if (dialog == DLG_NONE) return; if (muted) input_restore(&save); if (dialog == DLG_FORMAT) draw_format_dialog(); else if (dialog == DLG_INSTALL) draw_install_dialog(); else if (dialog == DLG_JOB) draw_job_dialog(); } void _start(const char *arg) { struct win_info wi; unsigned int cw, ch; int quit = 0; (void)arg; if (u_win_create(800, 560, "Disks", &wi) != 0) { u_puts("disks: no graphics window\n"); u_exit(1); } cw = wi.width; ch = wi.height; ngui_init(&ui, (unsigned short *)wi.fb, (int)cw, (int)ch); ngui_font_attach(&ui); name_in.placeholder = "mount name"; load_all(); for (;;) { struct win_info cur; if (u_win_info(&cur) == 0 && (cur.width != cw || cur.height != ch) && u_win_ack_resize() == 0) { cw = cur.width; ch = cur.height; ui.px = (unsigned short *)cur.fb; ui.w = (int)cw; ui.h = (int)ch; ngui_invalidate(&ui); } job_poll(); if (status_until && (int)(u_ticks_ms() - status_until) >= 0) { status[0] = '\0'; status_until = 0; ngui_invalidate(&ui); } if (!ngui_begin(&ui)) { u_sleep_ms(dialog == DLG_JOB ? 20 : 10); continue; } draw(); if (dialog == DLG_NONE && !ui.focus && key_pressed(NGUI_KEY_ESC)) quit = 1; ngui_end(&ui); if (quit) break; if (rescan_pending) { /* The frame above showed "Scanning..."; the rescan * blocks this window while it probes the disks. */ struct sys_storage_info si; rescan_pending = 0; u_syscall1(SYS_STORAGE_INFO, (int)&si); load_all(); set_status("Devices rescanned", 0); } u_sleep_ms(10); } u_win_close(); u_exit(0); }