ucmd_disks.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308
  1. /* disks: the ngui storage manager.
  2. *
  3. * Lists every block device (SYS_BLKDEV) with its MBR partitions, read as raw
  4. * sectors through SYS_BLK_READ, and every mounted filesystem. A FAT volume
  5. * can be mounted/unmounted, renamed and made read-only (SYS_MOUNT_CTL; the
  6. * policies are saved to /data/MOUNTS.CFG and applied again by the boot
  7. * scan). A device can be formatted or have NebOS installed on it: those run
  8. * the privileged `format -y` / `install -y` kernel commands on a private tty
  9. * and show their output, after this window asked for confirmation. The
  10. * `storage` command remains the text view. */
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include "ngui.h"
  14. #include "ucmd_lib.h"
  15. #define MAX_DISKS 16
  16. #define MAX_MOUNTS 24 /* VFS_MAX_MOUNTS */
  17. #define MAX_VOLS 5
  18. #define TOOLBAR_H 40
  19. #define LIST_W 230
  20. #define SECTION_H 28
  21. #define ROW_H 42
  22. #define GAP 10
  23. #define KIND_FLOPPY 0
  24. enum {
  25. ID_RESCAN = 1,
  26. ID_OVERVIEW,
  27. ID_FORMAT,
  28. ID_INSTALL,
  29. ID_MOUNT_TOGGLE,
  30. ID_RO,
  31. ID_AUTO,
  32. ID_NAME,
  33. ID_APPLY,
  34. ID_RESET,
  35. ID_OPEN,
  36. ID_DLG_SIZE,
  37. ID_DLG_CANCEL,
  38. ID_DLG_OK,
  39. ID_DLG_OK2,
  40. ID_DLG_CLOSE,
  41. ID_DISK_BASE = 100,
  42. ID_PART_BASE = 200,
  43. ID_MOUNT_BASE = 300,
  44. };
  45. /* One MBR partition (or the whole device for a floppy/superfloppy). */
  46. struct part {
  47. int slot; /* MBR slot 0..3, -1 = whole device */
  48. unsigned char type, active;
  49. unsigned int start, sectors;
  50. int fat; /* has a FAT boot sector */
  51. int nebos_boot; /* the NebOS boot sector + kernel */
  52. char fs[8];
  53. char label[12];
  54. char key[16]; /* default mount name */
  55. };
  56. struct disk {
  57. struct sys_blkdev bd;
  58. unsigned int index; /* SYS_BLKDEV / SYS_BLK_READ index */
  59. int read_ok; /* sector 0 was readable */
  60. int mbr; /* a valid partition table */
  61. int nparts;
  62. struct part parts[4];
  63. int nebos; /* boot partition + rootfs partition */
  64. int has_data; /* a NEBOSDATA partition */
  65. };
  66. enum dialog { DLG_NONE, DLG_FORMAT, DLG_INSTALL, DLG_JOB };
  67. static struct ngui ui;
  68. static struct disk disks[MAX_DISKS];
  69. static int ndisks;
  70. static struct sys_mount mounts[MAX_MOUNTS];
  71. static int nmounts;
  72. static struct sys_mount_policy pols[SYS_MOUNT_POLICY_MAX];
  73. static int npols;
  74. static int sel_disk = -1; /* -1 = mount overview */
  75. static char sel_key[16]; /* selected volume */
  76. static char edit_key[16]; /* volume the editor was loaded for */
  77. static struct ngui_input name_in;
  78. static int edit_ro, edit_auto;
  79. static struct ngui_scroll detail_scroll, list_scroll;
  80. static char status[128];
  81. static int status_err;
  82. static unsigned int status_until;
  83. static enum dialog dialog;
  84. static int rescan_pending;
  85. static struct ngui_input size_in;
  86. static int dlg_disk;
  87. /* The running format/install. */
  88. static int job_tty = -1, job_pid = -1;
  89. static int job_done, job_ok;
  90. static char job_title[64];
  91. static char job_log[4096];
  92. static unsigned int job_len;
  93. /* ---- helpers ---- */
  94. static void set_status(const char *msg, int err)
  95. {
  96. snprintf(status, sizeof(status), "%s", msg);
  97. status_err = err;
  98. status_until = u_ticks_ms() + 8000;
  99. ngui_invalidate(&ui);
  100. }
  101. static unsigned int rd32(const unsigned char *p)
  102. {
  103. return p[0] | (p[1] << 8) | (p[2] << 16) | ((unsigned int)p[3] << 24);
  104. }
  105. static void fmt_size(char *out, unsigned int n, unsigned int sectors)
  106. {
  107. if (sectors >= 2u * 1024u * 1024u)
  108. snprintf(out, n, "%u.%u GB", sectors >> 21,
  109. ((sectors & 0x1FFFFFu) * 10u) >> 21);
  110. else if (sectors >= 2048u)
  111. snprintf(out, n, "%u.%u MB", sectors >> 11,
  112. ((sectors & 0x7FFu) * 10u) >> 11);
  113. else
  114. snprintf(out, n, "%u KB", sectors / 2u);
  115. }
  116. static const char *kind_name(unsigned int kind)
  117. {
  118. static const char *names[] = { "Floppy", "IDE", "SATA", "eMMC", "USB" };
  119. return kind < 5 ? names[kind] : "Disk";
  120. }
  121. static const char *type_name(unsigned char t)
  122. {
  123. switch (t) {
  124. case 0x01: return "FAT12";
  125. case 0x04: case 0x06: case 0x0E: return "FAT16";
  126. case 0x0B: case 0x0C: return "FAT32";
  127. case 0x05: case 0x0F: return "Extended";
  128. case 0x07: return "NTFS/exFAT";
  129. case 0x7F: return "NebOS rootfs";
  130. case 0x82: return "Linux swap";
  131. case 0x83: return "Linux";
  132. case 0xEE: return "GPT";
  133. case 0xEF: return "EFI System";
  134. default: return "Unknown";
  135. }
  136. }
  137. /* Copy a space-padded BPB field and trim it. */
  138. static void bpb_str(char *out, const unsigned char *p, unsigned int n)
  139. {
  140. unsigned int i;
  141. for (i = 0; i < n; i++)
  142. out[i] = (p[i] >= 0x20 && p[i] < 0x7F) ? (char)p[i] : ' ';
  143. while (i > 0 && out[i - 1] == ' ')
  144. i--;
  145. out[i] = '\0';
  146. }
  147. /* Classify a partition's (or a superfloppy's) first sector. */
  148. static void probe_boot_sector(struct part *p, const unsigned char *s)
  149. {
  150. unsigned int bps = s[11] | (s[12] << 8);
  151. p->fat = 0;
  152. p->nebos_boot = 0;
  153. p->fs[0] = p->label[0] = '\0';
  154. if (s[510] != 0x55 || s[511] != 0xAA)
  155. return;
  156. if (s[3] == 'N' && s[4] == 'E' && s[5] == 'B' && s[6] == 'O' &&
  157. s[7] == 'S') {
  158. p->nebos_boot = 1;
  159. snprintf(p->fs, sizeof(p->fs), "NebOS");
  160. return;
  161. }
  162. if (bps != 512 || (s[0] != 0xEB && s[0] != 0xE9) || s[13] == 0)
  163. return;
  164. p->fat = 1;
  165. if ((s[0x16] | (s[0x17] << 8)) == 0) { /* FAT32 BPB */
  166. snprintf(p->fs, sizeof(p->fs), "FAT32");
  167. bpb_str(p->label, s + 0x47, 11);
  168. } else {
  169. bpb_str(p->fs, s + 0x36, 5);
  170. if (strncmp(p->fs, "FAT", 3) != 0)
  171. snprintf(p->fs, sizeof(p->fs), "FAT");
  172. bpb_str(p->label, s + 0x2B, 11);
  173. }
  174. if (!strcmp(p->label, "NO NAME"))
  175. p->label[0] = '\0';
  176. }
  177. static int blk_read(unsigned int index, unsigned int lba, unsigned char *buf)
  178. {
  179. return u_syscall3(SYS_BLK_READ, (int)index, (int)lba, (int)buf);
  180. }
  181. /* Read the partition table and the first sector of every partition. A
  182. * floppy is never read here: without a disk the controller retries for
  183. * seconds, and it has no partitions anyway. */
  184. static void probe_disk(struct disk *d)
  185. {
  186. static unsigned char s[512];
  187. unsigned int i;
  188. d->read_ok = d->mbr = d->nparts = d->nebos = d->has_data = 0;
  189. if (!d->bd.present)
  190. return;
  191. if (d->bd.kind == KIND_FLOPPY) {
  192. struct part *p = &d->parts[d->nparts++];
  193. memset(p, 0, sizeof(*p));
  194. p->slot = -1;
  195. p->sectors = d->bd.total_sectors;
  196. p->fat = 1;
  197. snprintf(p->key, sizeof(p->key), "%s", d->bd.name);
  198. return;
  199. }
  200. if (blk_read(d->index, 0, s) != 0)
  201. return;
  202. d->read_ok = 1;
  203. if (s[510] == 0x55 && s[511] == 0xAA) {
  204. int ok = 1, n = 0;
  205. for (i = 0; i < 4; i++) {
  206. const unsigned char *e = s + 0x1BE + i * 16;
  207. unsigned int start = rd32(e + 8), cnt = rd32(e + 12);
  208. if (e[4] == 0)
  209. continue;
  210. if ((e[0] != 0 && e[0] != 0x80) || start == 0 ||
  211. cnt == 0 || start > d->bd.total_sectors ||
  212. cnt > d->bd.total_sectors - start)
  213. ok = 0;
  214. n++;
  215. }
  216. d->mbr = ok && n > 0;
  217. }
  218. if (!d->mbr) {
  219. struct part *p = &d->parts[0];
  220. memset(p, 0, sizeof(*p));
  221. p->slot = -1;
  222. p->sectors = d->bd.total_sectors;
  223. probe_boot_sector(p, s);
  224. snprintf(p->key, sizeof(p->key), "%s", d->bd.name);
  225. if (p->fat)
  226. d->nparts = 1;
  227. return;
  228. }
  229. for (i = 0; i < 4; i++) {
  230. const unsigned char *e = s + 0x1BE + i * 16;
  231. struct part *p;
  232. if (e[4] == 0)
  233. continue;
  234. p = &d->parts[d->nparts++];
  235. memset(p, 0, sizeof(*p));
  236. p->slot = (int)i;
  237. p->type = e[4];
  238. p->active = e[0] == 0x80;
  239. p->start = rd32(e + 8);
  240. p->sectors = rd32(e + 12);
  241. snprintf(p->key, sizeof(p->key), "%sp%u", d->bd.name, i + 1);
  242. }
  243. for (i = 0; i < (unsigned int)d->nparts; i++) {
  244. struct part *p = &d->parts[i];
  245. if (p->type == 0x05 || p->type == 0x0F || p->type == 0xEE ||
  246. p->type == 0x7F)
  247. continue;
  248. if (blk_read(d->index, p->start, s) == 0)
  249. probe_boot_sector(p, s);
  250. if (p->nebos_boot && p->slot == 0)
  251. d->nebos |= 1;
  252. if (p->fat && !strcmp(p->label, "NEBOSDATA"))
  253. d->has_data = 1;
  254. }
  255. for (i = 0; i < (unsigned int)d->nparts; i++)
  256. if (d->parts[i].type == 0x7F)
  257. d->nebos |= 2;
  258. d->nebos = d->nebos == 3;
  259. }
  260. static void load_mounts(void)
  261. {
  262. int i;
  263. nmounts = 0;
  264. for (i = 0; i < MAX_MOUNTS; i++) {
  265. if (u_syscall2(SYS_LIST_MOUNTS, i, (int)&mounts[nmounts]) != 1)
  266. break;
  267. nmounts++;
  268. }
  269. npols = 0;
  270. for (i = 0; i < SYS_MOUNT_POLICY_MAX; i++) {
  271. if (u_syscall3(SYS_MOUNT_CTL, SYS_MOUNT_POLICY_GET, i,
  272. (int)&pols[npols]) != 1)
  273. break;
  274. npols++;
  275. }
  276. }
  277. /* USB INQUIRY strings are space-padded vendor/product fields. */
  278. static void squeeze(char *s)
  279. {
  280. char *o = s;
  281. const char *p;
  282. for (p = s; *p; p++)
  283. if (*p != ' ' || (o > s && o[-1] != ' '))
  284. *o++ = *p;
  285. if (o > s && o[-1] == ' ')
  286. o--;
  287. *o = '\0';
  288. }
  289. static void load_all(void)
  290. {
  291. int i;
  292. ndisks = 0;
  293. for (i = 0; i < MAX_DISKS; i++) {
  294. struct disk *d = &disks[ndisks];
  295. if (u_syscall2(SYS_BLKDEV, i, (int)&d->bd) != 1)
  296. break;
  297. d->index = (unsigned int)i;
  298. squeeze(d->bd.model);
  299. probe_disk(d);
  300. ndisks++;
  301. }
  302. if (sel_disk >= ndisks)
  303. sel_disk = -1;
  304. load_mounts();
  305. edit_key[0] = '\0'; /* reload the editor from the new state */
  306. }
  307. static const struct sys_mount *mount_by_key(const char *key)
  308. {
  309. int i;
  310. for (i = 0; i < nmounts; i++)
  311. if (mounts[i].dev[0] && !strcmp(mounts[i].key, key))
  312. return &mounts[i];
  313. return 0;
  314. }
  315. static const struct sys_mount *mount_by_label(const char *label)
  316. {
  317. int i;
  318. for (i = 0; i < nmounts; i++)
  319. if (mounts[i].dev[0] && !strcmp(mounts[i].label, label))
  320. return &mounts[i];
  321. return 0;
  322. }
  323. static const struct sys_mount_policy *policy_by_key(const char *key)
  324. {
  325. int i;
  326. for (i = 0; i < npols; i++)
  327. if (!strcmp(pols[i].key, key))
  328. return &pols[i];
  329. return 0;
  330. }
  331. /* The volume carrying the saved policies: never edited from here, since an
  332. * unmounted, renamed or read-only /data could not store the change. */
  333. static int is_data_volume(const struct part *p)
  334. {
  335. const struct sys_mount *m = mount_by_key(p->key);
  336. return !strcmp(p->label, "NEBOSDATA") ||
  337. (m && !strcmp(m->name, "data"));
  338. }
  339. /* ---- mount policies ---- */
  340. static int save_policies(void)
  341. {
  342. static struct {
  343. char magic[8];
  344. struct sys_mount_policy p[SYS_MOUNT_POLICY_MAX];
  345. } cfg;
  346. const struct sys_mount *data = mount_by_label("NEBOSDATA");
  347. char path[48];
  348. if (!data)
  349. return 0;
  350. memcpy(cfg.magic, SYS_MOUNT_CFG_MAGIC, 8);
  351. memcpy(cfg.p, pols, sizeof(pols[0]) * (unsigned int)npols);
  352. snprintf(path, sizeof(path), "/%s/MOUNTS.CFG", data->name);
  353. return u_syscall3(SYS_WRITE_FILE, (int)path, (int)&cfg,
  354. 8 + (int)sizeof(pols[0]) * npols) == 0 ? 1 : -1;
  355. }
  356. static int name_valid(const char *s)
  357. {
  358. unsigned int i;
  359. for (i = 0; s[i]; i++)
  360. if (i >= 15 || !((s[i] >= 'a' && s[i] <= 'z') ||
  361. (s[i] >= '0' && s[i] <= '9') ||
  362. s[i] == '_' || s[i] == '-'))
  363. return 0;
  364. return 1;
  365. }
  366. static void apply_policy(const char *key, const char *name, unsigned int flags)
  367. {
  368. struct sys_mount_policy p;
  369. const struct sys_mount *cur = mount_by_key(key);
  370. char msg[96];
  371. int i, saved;
  372. if (!name_valid(name)) {
  373. set_status("Mount name: up to 15 of a-z, 0-9, _ and -", 1);
  374. return;
  375. }
  376. if (!strcmp(name, "ramfs") || !strcmp(name, "share") ||
  377. !strcmp(name, "data")) {
  378. set_status("That mount name is reserved", 1);
  379. return;
  380. }
  381. for (i = 0; name[0] && i < nmounts; i++)
  382. if (!strcmp(mounts[i].name, name) && &mounts[i] != cur) {
  383. set_status("Another volume is mounted with that name", 1);
  384. return;
  385. }
  386. memset(&p, 0, sizeof(p));
  387. snprintf(p.key, sizeof(p.key), "%s", key);
  388. if (strcmp(name, key) != 0)
  389. snprintf(p.name, sizeof(p.name), "%s", name);
  390. p.flags = flags;
  391. if (u_syscall3(SYS_MOUNT_CTL, SYS_MOUNT_POLICY_SET, (int)&p, 0) != 0) {
  392. set_status("The kernel refused the mount settings", 1);
  393. return;
  394. }
  395. load_mounts();
  396. edit_key[0] = '\0';
  397. saved = save_policies();
  398. cur = mount_by_key(key);
  399. if (flags & SYS_MOUNT_NOAUTO)
  400. snprintf(msg, sizeof(msg), "%s is unmounted", key);
  401. else if (cur)
  402. snprintf(msg, sizeof(msg), "%s is mounted at /%s%s", key,
  403. cur->name, cur->ro ? " (read-only)" : "");
  404. else
  405. snprintf(msg, sizeof(msg), "%s has no readable filesystem", key);
  406. if (saved == 0)
  407. snprintf(msg + strlen(msg), sizeof(msg) - strlen(msg), "; not saved (no /data)");
  408. else if (saved < 0)
  409. snprintf(msg + strlen(msg), sizeof(msg) - strlen(msg), "; saving failed");
  410. set_status(msg, !cur && !(flags & SYS_MOUNT_NOAUTO));
  411. /* Applied, but only until the next boot: a warning, not an error. */
  412. if (saved == 0 && (cur || (flags & SYS_MOUNT_NOAUTO)))
  413. status_err = 2;
  414. }
  415. /* ---- format / install jobs ---- */
  416. static void start_job(const char *cmd, const char *arg, const char *title)
  417. {
  418. if (job_tty < 0)
  419. job_tty = u_tty_create();
  420. if (job_tty < 0) {
  421. set_status("No terminal for the command", 1);
  422. return;
  423. }
  424. job_len = 0;
  425. job_log[0] = '\0';
  426. job_done = job_ok = 0;
  427. snprintf(job_title, sizeof(job_title), "%s", title);
  428. job_pid = u_spawn_kcmd(cmd, arg, job_tty);
  429. if (job_pid < 0) {
  430. set_status("Could not start the command", 1);
  431. dialog = DLG_NONE;
  432. return;
  433. }
  434. dialog = DLG_JOB;
  435. ngui_invalidate(&ui);
  436. }
  437. /* The log is drawn in a 13 px monospace face; longer lines are wrapped
  438. * here so the dialog never clips them. */
  439. #define JOB_LOG_COLS 66
  440. static void job_append(const char *s, int n)
  441. {
  442. static unsigned int col;
  443. int i;
  444. for (i = 0; i < n; i++) {
  445. if (s[i] == '\r')
  446. continue;
  447. if (s[i] != '\n' && col >= JOB_LOG_COLS) {
  448. job_append("\n", 1);
  449. }
  450. col = s[i] == '\n' ? 0 : col + 1;
  451. if (job_len + 1 >= sizeof(job_log)) {
  452. /* Keep the newest half of the log. */
  453. unsigned int keep = sizeof(job_log) / 2;
  454. memmove(job_log, job_log + job_len - keep, keep);
  455. job_len = keep;
  456. }
  457. job_log[job_len++] = s[i];
  458. }
  459. job_log[job_len] = '\0';
  460. }
  461. static void job_poll(void)
  462. {
  463. char buf[256];
  464. int n, any = 0;
  465. if (job_tty < 0)
  466. return;
  467. while ((n = u_tty_read(job_tty, buf, sizeof(buf))) > 0) {
  468. if (dialog == DLG_JOB && !job_done)
  469. job_append(buf, n);
  470. any = 1;
  471. }
  472. if (dialog == DLG_JOB && !job_done && job_pid > 0 &&
  473. !u_alive((unsigned int)job_pid)) {
  474. while ((n = u_tty_read(job_tty, buf, sizeof(buf))) > 0)
  475. job_append(buf, n);
  476. job_done = 1;
  477. job_pid = -1;
  478. job_ok = strstr(job_log, "done") && !strstr(job_log, "failed") &&
  479. !strstr(job_log, "aborted");
  480. load_all();
  481. any = 1;
  482. }
  483. if (any)
  484. ngui_invalidate(&ui);
  485. }
  486. /* ---- drawing ---- */
  487. /* Widgets drawn under a dialog must not react: hide the pointer edges. */
  488. struct input_save {
  489. unsigned int pressed, released;
  490. int inside, scroll_y;
  491. };
  492. static void input_mute(struct input_save *s)
  493. {
  494. s->pressed = ui.pressed;
  495. s->released = ui.released;
  496. s->inside = ui.inside;
  497. s->scroll_y = ui.scroll_y;
  498. ui.pressed = ui.released = 0;
  499. ui.inside = 0;
  500. ui.scroll_y = 0;
  501. }
  502. static void input_restore(const struct input_save *s)
  503. {
  504. ui.pressed = s->pressed;
  505. ui.released = s->released;
  506. ui.inside = s->inside;
  507. ui.scroll_y = s->scroll_y;
  508. }
  509. static int visible(struct ngui_rect r, struct ngui_rect area)
  510. {
  511. return r.y + r.h > area.y && r.y < area.y + area.h;
  512. }
  513. static void text_dim(int x, int y, const char *s)
  514. {
  515. ngui_text(&ui, x, y, s, ui.theme.text_dim);
  516. }
  517. static void draw_toolbar(struct ngui_rect r)
  518. {
  519. struct ngui_rect b;
  520. unsigned short c;
  521. ngui_fill_rect(&ui, r, ui.theme.panel);
  522. ngui_inset(&r, 6);
  523. b = ngui_cut_left(&r, 92);
  524. if (ngui_button(&ui, ID_RESCAN, b, "Rescan")) {
  525. set_status("Scanning devices...", 0);
  526. rescan_pending = 1;
  527. }
  528. ngui_cut_left(&r, 12);
  529. if (status[0]) {
  530. c = status_err == 2 ? ui.theme.warning :
  531. status_err ? ui.theme.error : ui.theme.success;
  532. ngui_text_ellipsis(&ui, r, r.x, r.y + (r.h - 16) / 2, status, c);
  533. } else {
  534. ngui_text_ellipsis(&ui, r, r.x, r.y + (r.h - 16) / 2,
  535. "Storage devices, volumes and mounts",
  536. ui.theme.text_dim);
  537. }
  538. }
  539. /* Empty IDE channels and absent eMMC/USB slots are registered as devices
  540. * but have nothing to show; a floppy drive is listed without media too. */
  541. static int listed(const struct disk *d)
  542. {
  543. return d->bd.present || d->bd.kind == KIND_FLOPPY;
  544. }
  545. static void draw_device_list(struct ngui_rect area)
  546. {
  547. struct ngui_row row;
  548. struct ngui_rect r, col;
  549. char sub[64], size[16];
  550. int i, y;
  551. ngui_fill_rect(&ui, area, ui.theme.panel);
  552. ngui_inset(&area, 6);
  553. for (i = 0, y = 0; i < ndisks; i++)
  554. y += listed(&disks[i]);
  555. list_scroll.content_h = (y + 1) * ROW_H + 2 * SECTION_H;
  556. ngui_scroll_begin(&ui, area, &list_scroll);
  557. ngui_scroll_wheel(&ui, area, &list_scroll);
  558. col = area;
  559. y = area.y - list_scroll.offset;
  560. r = (struct ngui_rect){ col.x, y, col.w, SECTION_H };
  561. ngui_section(&ui, r, "Overview", 0, 0);
  562. y += SECTION_H;
  563. r = (struct ngui_rect){ col.x, y, col.w, ROW_H };
  564. if (visible(r, area)) {
  565. snprintf(sub, sizeof(sub), "%d filesystem%s", nmounts,
  566. nmounts == 1 ? "" : "s");
  567. row.title = "All mounts";
  568. row.subtitle = sub;
  569. row.trailing = 0;
  570. row.bars = -1;
  571. row.selected = sel_disk < 0;
  572. if (ngui_list_row(&ui, ID_OVERVIEW, r, &row)) {
  573. sel_disk = -1;
  574. detail_scroll.offset = 0;
  575. ngui_invalidate(&ui);
  576. }
  577. }
  578. y += ROW_H;
  579. r = (struct ngui_rect){ col.x, y, col.w, SECTION_H };
  580. ngui_section(&ui, r, "Devices", 0, 0);
  581. y += SECTION_H;
  582. for (i = 0; i < ndisks; i++) {
  583. const struct disk *d = &disks[i];
  584. if (!listed(d))
  585. continue;
  586. r = (struct ngui_rect){ col.x, y, col.w, ROW_H };
  587. y += ROW_H;
  588. if (!visible(r, area))
  589. continue;
  590. fmt_size(size, sizeof(size), d->bd.total_sectors);
  591. if (!d->bd.present)
  592. snprintf(sub, sizeof(sub), "%s, not present",
  593. kind_name(d->bd.kind));
  594. else
  595. snprintf(sub, sizeof(sub), "%s %s",
  596. kind_name(d->bd.kind),
  597. d->bd.kind == KIND_FLOPPY ? "" : size);
  598. row.title = d->bd.name;
  599. row.subtitle = sub;
  600. row.trailing = d->nebos ? "NebOS" : 0;
  601. row.bars = -1;
  602. row.selected = sel_disk == i;
  603. if (ngui_list_row(&ui, ID_DISK_BASE + i, r, &row) &&
  604. sel_disk != i) {
  605. sel_disk = i;
  606. sel_key[0] = '\0';
  607. detail_scroll.offset = 0;
  608. ngui_invalidate(&ui);
  609. }
  610. }
  611. ngui_scroll_end(&ui, area, &list_scroll);
  612. }
  613. /* The overview: one line per mount. */
  614. static int draw_overview(struct ngui_rect area, int y)
  615. {
  616. static const int col[] = { 0, 96, 156, 230, 330, 404 };
  617. static const char *hdr[] = { "Mount", "Type", "Size", "Label",
  618. "Device", "Mode" };
  619. char size[16];
  620. int i, j;
  621. ngui_text_px(&ui, area.x, y, "All mounts", 10, ui.theme.text, 18, 1, 0, 0);
  622. y += 32;
  623. for (j = 0; j < 6; j++)
  624. text_dim(area.x + col[j], y, hdr[j]);
  625. y += 22;
  626. ngui_fill_rect(&ui, (struct ngui_rect){ area.x, y - 4, area.w, 1 },
  627. ui.theme.border);
  628. for (i = 0; i < nmounts; i++) {
  629. const struct sys_mount *m = &mounts[i];
  630. struct ngui_rect r = { area.x, y, area.w, 26 };
  631. char name[20];
  632. if (visible(r, area)) {
  633. snprintf(name, sizeof(name), "/%s", m->name);
  634. fmt_size(size, sizeof(size), m->total_sectors);
  635. ngui_text(&ui, area.x + col[0], y + 4, name, ui.theme.text);
  636. ngui_text(&ui, area.x + col[1], y + 4, m->type, ui.theme.text);
  637. ngui_text(&ui, area.x + col[2], y + 4, size, ui.theme.text);
  638. ngui_text(&ui, area.x + col[3], y + 4, m->label, ui.theme.text);
  639. ngui_text(&ui, area.x + col[4], y + 4,
  640. m->dev[0] ? m->key : "RAM", ui.theme.text_dim);
  641. ngui_text(&ui, area.x + col[5], y + 4,
  642. m->ro ? "read-only" : "read-write",
  643. m->ro ? ui.theme.warning : ui.theme.text_dim);
  644. if (m->dev[0] && ngui_button(&ui, ID_MOUNT_BASE + i,
  645. (struct ngui_rect){ area.x + area.w - 60, y + 1,
  646. 60, 24 }, "Show")) {
  647. for (j = 0; j < ndisks; j++)
  648. if (!strcmp(disks[j].bd.name, m->dev))
  649. sel_disk = j;
  650. snprintf(sel_key, sizeof(sel_key), "%s", m->key);
  651. detail_scroll.offset = 0;
  652. ngui_invalidate(&ui);
  653. }
  654. }
  655. y += 28;
  656. }
  657. y += GAP;
  658. text_dim(area.x, y,
  659. "Select a device to format it, install NebOS or change how");
  660. y += 18;
  661. text_dim(area.x, y, "its volumes are mounted.");
  662. return y + 24;
  663. }
  664. static void load_editor(const struct part *p)
  665. {
  666. const struct sys_mount_policy *pol = policy_by_key(p->key);
  667. const struct sys_mount *m = mount_by_key(p->key);
  668. snprintf(edit_key, sizeof(edit_key), "%s", p->key);
  669. if (pol && pol->name[0])
  670. ngui_input_set(&name_in, pol->name);
  671. else
  672. ngui_input_set(&name_in, m ? m->name : p->key);
  673. edit_ro = pol && (pol->flags & SYS_MOUNT_RO);
  674. edit_auto = !(pol && (pol->flags & SYS_MOUNT_NOAUTO));
  675. }
  676. static int draw_mount_editor(struct ngui_rect area, int y, const struct part *p)
  677. {
  678. const struct sys_mount *m = mount_by_key(p->key);
  679. struct ngui_rect r, b;
  680. char line[96], name[16];
  681. unsigned int flags;
  682. if (strcmp(edit_key, p->key) != 0)
  683. load_editor(p);
  684. r = (struct ngui_rect){ area.x, y, area.w, SECTION_H };
  685. snprintf(line, sizeof(line), "Volume %s", p->key);
  686. ngui_section(&ui, r, line, 0, 0);
  687. y += SECTION_H;
  688. if (m)
  689. snprintf(line, sizeof(line), "Mounted at /%s, %s, %s", m->name,
  690. m->type, m->ro ? "read-only" : "read-write");
  691. else
  692. snprintf(line, sizeof(line), "Not mounted");
  693. ngui_text(&ui, area.x, y + 2, line, m ? ui.theme.success : ui.theme.text_dim);
  694. y += 24;
  695. if (is_data_volume(p)) {
  696. text_dim(area.x, y,
  697. "This is the NebOS data volume (/data). It holds the");
  698. y += 18;
  699. text_dim(area.x, y,
  700. "saved settings, so it is always mounted read-write.");
  701. y += 26;
  702. if (m && ngui_button(&ui, ID_OPEN,
  703. (struct ngui_rect){ area.x, y, 130, 28 },
  704. "Open in Files"))
  705. goto open_files;
  706. return y + 38;
  707. }
  708. text_dim(area.x, y + 6, "Mount name");
  709. r = (struct ngui_rect){ area.x + 110, y, 180, 28 };
  710. if (visible(r, area))
  711. ngui_text_input(&ui, ID_NAME, r, &name_in);
  712. y += 34;
  713. r = (struct ngui_rect){ area.x, y, 290, 26 };
  714. if (visible(r, area) &&
  715. ngui_menu_item(&ui, ID_AUTO, r, "Mount automatically", 0, edit_auto))
  716. edit_auto = !edit_auto;
  717. y += 28;
  718. r = (struct ngui_rect){ area.x, y, 290, 26 };
  719. if (visible(r, area) &&
  720. ngui_menu_item(&ui, ID_RO, r, "Read-only", 0, edit_ro))
  721. edit_ro = !edit_ro;
  722. y += 34;
  723. ngui_input_value(&name_in, name, sizeof(name));
  724. flags = (edit_ro ? SYS_MOUNT_RO : 0) | (edit_auto ? 0 : SYS_MOUNT_NOAUTO);
  725. b = (struct ngui_rect){ area.x, y, 90, 28 };
  726. if (visible(b, area) && ngui_button(&ui, ID_APPLY, b, "Apply"))
  727. apply_policy(p->key, name, flags);
  728. b.x += 98;
  729. b.w = 100;
  730. if (visible(b, area) &&
  731. ngui_button(&ui, ID_MOUNT_TOGGLE, b, m ? "Unmount" : "Mount")) {
  732. if (m)
  733. apply_policy(p->key, name, flags | SYS_MOUNT_NOAUTO);
  734. else
  735. apply_policy(p->key, name, flags & ~SYS_MOUNT_NOAUTO);
  736. }
  737. b.x += 108;
  738. b.w = 90;
  739. if (visible(b, area) && ngui_button(&ui, ID_RESET, b, "Defaults"))
  740. apply_policy(p->key, "", 0);
  741. b.x += 98;
  742. b.w = 130;
  743. if (m && visible(b, area) && ngui_button(&ui, ID_OPEN, b, "Open in Files"))
  744. goto open_files;
  745. y += 38;
  746. text_dim(area.x, y,
  747. "Settings apply at once and are saved to /data/MOUNTS.CFG,");
  748. y += 18;
  749. text_dim(area.x, y, "so the boot scan uses them too.");
  750. return y + 26;
  751. open_files:
  752. {
  753. char cwd[24];
  754. snprintf(cwd, sizeof(cwd), "/%s", m->name);
  755. if (u_spawn_detached("/ramfs/bin/files", "", 0, cwd) < 0)
  756. set_status("Could not start files", 1);
  757. }
  758. return y + 38;
  759. }
  760. static int draw_disk(struct ngui_rect area, int y, struct disk *d)
  761. {
  762. struct ngui_row row;
  763. struct ngui_rect r, b;
  764. char line[128], size[16], title[48], sub[80];
  765. const struct part *sel = 0;
  766. int i;
  767. snprintf(title, sizeof(title), "%s", d->bd.name);
  768. ngui_text_px(&ui, area.x, y, title, (unsigned int)strlen(title),
  769. ui.theme.text, 18, 1, 0, 0);
  770. y += 30;
  771. fmt_size(size, sizeof(size), d->bd.total_sectors);
  772. if (d->bd.kind == KIND_FLOPPY)
  773. snprintf(line, sizeof(line), "%s drive", kind_name(d->bd.kind));
  774. else
  775. snprintf(line, sizeof(line), "%s%s%s, %s (%u sectors)",
  776. kind_name(d->bd.kind), d->bd.model[0] ? " " : "",
  777. d->bd.model, size, d->bd.total_sectors);
  778. text_dim(area.x, y, line);
  779. y += 20;
  780. if (!d->bd.present) {
  781. ngui_text(&ui, area.x, y, "Not present", ui.theme.warning);
  782. return y + 30;
  783. }
  784. if (d->nebos) {
  785. ngui_text(&ui, area.x, y, d->has_data ?
  786. "NebOS installed, with a /data partition" :
  787. "NebOS installed", ui.theme.success);
  788. y += 20;
  789. }
  790. y += GAP;
  791. r = (struct ngui_rect){ area.x, y, area.w, SECTION_H };
  792. ngui_section(&ui, r, d->mbr ? "Partitions" : "Volume", 0, 0);
  793. y += SECTION_H;
  794. if (d->bd.kind != KIND_FLOPPY && !d->read_ok) {
  795. ngui_text(&ui, area.x, y + 4, "The device could not be read",
  796. ui.theme.error);
  797. y += 30;
  798. } else if (!d->nparts) {
  799. text_dim(area.x, y + 4,
  800. "No partition table and no FAT filesystem");
  801. y += 30;
  802. }
  803. for (i = 0; i < d->nparts; i++) {
  804. const struct part *p = &d->parts[i];
  805. const struct sys_mount *m = mount_by_key(p->key);
  806. const struct sys_mount_policy *pol = policy_by_key(p->key);
  807. const char *what;
  808. char trail[24];
  809. int selectable = p->fat && !p->nebos_boot;
  810. r = (struct ngui_rect){ area.x, y, area.w, ROW_H };
  811. y += ROW_H;
  812. if (selectable && !sel_key[0])
  813. snprintf(sel_key, sizeof(sel_key), "%s", p->key);
  814. if (selectable && !strcmp(sel_key, p->key))
  815. sel = p;
  816. if (!visible(r, area))
  817. continue;
  818. if (p->nebos_boot)
  819. what = "NebOS boot + kernel";
  820. else if (p->fat)
  821. what = p->fs;
  822. else
  823. what = type_name(p->type);
  824. fmt_size(size, sizeof(size), p->sectors);
  825. if (p->slot >= 0)
  826. snprintf(title, sizeof(title), "%d %s%s%s", p->slot + 1,
  827. what, p->label[0] ? " " : "", p->label);
  828. else
  829. /* A floppy is never read here; its mount has the label. */
  830. snprintf(title, sizeof(title), "%s%s%s",
  831. d->bd.kind == KIND_FLOPPY ? "Floppy disk" : what,
  832. (p->label[0] || (m && m->label[0])) ? " " : "",
  833. p->label[0] ? p->label : (m ? m->label : ""));
  834. if (p->slot >= 0)
  835. snprintf(sub, sizeof(sub), "%s, type 0x%02X, LBA %u%s",
  836. size, p->type, p->start,
  837. p->active ? ", active" : "");
  838. else
  839. snprintf(sub, sizeof(sub), "%s, whole device",
  840. d->bd.kind == KIND_FLOPPY ? "1.4 MB" : size);
  841. if (m)
  842. snprintf(trail, sizeof(trail), "/%s%s", m->name,
  843. m->ro ? " (ro)" : "");
  844. else if (pol && (pol->flags & SYS_MOUNT_NOAUTO))
  845. snprintf(trail, sizeof(trail), "unmounted");
  846. else
  847. snprintf(trail, sizeof(trail), "%s",
  848. selectable ? "not mounted" : "");
  849. row.title = title;
  850. row.subtitle = sub;
  851. row.trailing = trail[0] ? trail : 0;
  852. row.bars = -1;
  853. row.selected = sel == p;
  854. if (ngui_list_row(&ui, ID_PART_BASE + d->index * 4 +
  855. (unsigned int)(p->slot < 0 ? 0 : p->slot),
  856. r, &row) && selectable) {
  857. snprintf(sel_key, sizeof(sel_key), "%s", p->key);
  858. ngui_invalidate(&ui);
  859. }
  860. }
  861. y += GAP;
  862. if (sel)
  863. y = draw_mount_editor(area, y, sel) + GAP;
  864. r = (struct ngui_rect){ area.x, y, area.w, SECTION_H };
  865. ngui_section(&ui, r, "Device", 0, 0);
  866. y += SECTION_H;
  867. b = (struct ngui_rect){ area.x, y, 120, 28 };
  868. if (visible(b, area) && ngui_button(&ui, ID_FORMAT, b, "Format...")) {
  869. dialog = DLG_FORMAT;
  870. dlg_disk = sel_disk;
  871. ngui_input_set(&size_in, "");
  872. ngui_set_focus(&ui, ID_DLG_SIZE);
  873. }
  874. b.x += 128;
  875. b.w = 150;
  876. if (d->bd.kind != KIND_FLOPPY && visible(b, area) &&
  877. ngui_button(&ui, ID_INSTALL, b, "Install NebOS...")) {
  878. dialog = DLG_INSTALL;
  879. dlg_disk = sel_disk;
  880. }
  881. y += 36;
  882. text_dim(area.x, y, d->bd.kind == KIND_FLOPPY ?
  883. "Format writes a fresh FAT12 filesystem." :
  884. "Format writes one FAT partition (MBR) over the whole disk.");
  885. return y + 26;
  886. }
  887. static void draw_details(struct ngui_rect area)
  888. {
  889. struct ngui_rect col;
  890. int y, top;
  891. ngui_inset(&area, 14);
  892. ngui_scroll_begin(&ui, area, &detail_scroll);
  893. ngui_scroll_wheel(&ui, area, &detail_scroll);
  894. if (!ui.focus)
  895. ngui_scroll_keys(&ui, &detail_scroll);
  896. col = area;
  897. top = area.y - detail_scroll.offset;
  898. if (sel_disk < 0)
  899. y = draw_overview(col, top);
  900. else
  901. y = draw_disk(col, top, &disks[sel_disk]);
  902. detail_scroll.content_h = y - top;
  903. ngui_scroll_end(&ui, area, &detail_scroll);
  904. }
  905. /* ---- dialogs ---- */
  906. static struct ngui_rect dialog_rect(int w, int h)
  907. {
  908. struct ngui_rect r;
  909. r.w = w < ui.w - 20 ? w : ui.w - 20;
  910. r.h = h < ui.h - 20 ? h : ui.h - 20;
  911. r.x = (ui.w - r.w) / 2;
  912. r.y = (ui.h - r.h) / 2;
  913. ngui_fill_round_rect(&ui, (struct ngui_rect){ r.x - 1, r.y - 1,
  914. r.w + 2, r.h + 2 },
  915. NGUI_RADIUS + 1, NGUI_RADIUS + 1,
  916. NGUI_RADIUS + 1, NGUI_RADIUS + 1,
  917. ui.theme.border);
  918. ngui_fill_round_rect(&ui, r, NGUI_RADIUS, NGUI_RADIUS,
  919. NGUI_RADIUS, NGUI_RADIUS, ui.theme.panel);
  920. return r;
  921. }
  922. static void dialog_title(struct ngui_rect *r, const char *s)
  923. {
  924. struct ngui_rect t = ngui_cut_top(r, 34);
  925. ngui_text_px(&ui, t.x, t.y + 6, s, (unsigned int)strlen(s),
  926. ui.theme.text, 16, 1, 0, 0);
  927. }
  928. static void dialog_line(struct ngui_rect *r, const char *s, unsigned short c)
  929. {
  930. struct ngui_rect t = ngui_cut_top(r, 20);
  931. ngui_text_ellipsis(&ui, t, t.x, t.y, s, c);
  932. }
  933. static int key_pressed(int key)
  934. {
  935. if (ngui_key_peek(&ui) == key) {
  936. ngui_key(&ui);
  937. return 1;
  938. }
  939. return 0;
  940. }
  941. static void draw_format_dialog(void)
  942. {
  943. const struct disk *d = &disks[dlg_disk];
  944. struct ngui_rect r = dialog_rect(460, 260), b;
  945. char line[96], size[16], arg[48], val[24];
  946. int floppy = d->bd.kind == KIND_FLOPPY, go = 0, i;
  947. ngui_inset(&r, 18);
  948. snprintf(line, sizeof(line), "Format %s", d->bd.name);
  949. dialog_title(&r, line);
  950. fmt_size(size, sizeof(size), d->bd.total_sectors);
  951. snprintf(line, sizeof(line), "%s%s%s, %s", kind_name(d->bd.kind),
  952. d->bd.model[0] ? " " : "", d->bd.model,
  953. floppy ? "1.4 MB" : size);
  954. dialog_line(&r, line, ui.theme.text_dim);
  955. dialog_line(&r, "Every file and partition on this device is erased.",
  956. ui.theme.warning);
  957. if (d->nebos)
  958. dialog_line(&r, "This removes the NebOS installation as well.",
  959. ui.theme.warning);
  960. ngui_cut_top(&r, 8);
  961. if (!floppy) {
  962. b = ngui_cut_top(&r, 30);
  963. text_dim(b.x, b.y + 6, "Size");
  964. b.x += 60;
  965. b.w = 140;
  966. size_in.placeholder = "whole disk";
  967. if (ngui_text_input(&ui, ID_DLG_SIZE, b, &size_in) &
  968. NGUI_INPUT_SUBMIT)
  969. go = 1;
  970. text_dim(b.x + b.w + 10, b.y + 6, "e.g. 100mb, 2gb");
  971. }
  972. b = ngui_cut_bottom(&r, 30);
  973. b.x = r.x + r.w - 110;
  974. b.w = 110;
  975. if (ngui_button(&ui, ID_DLG_OK, b, "Erase disk"))
  976. go = 1;
  977. b.x -= 100;
  978. b.w = 92;
  979. if (ngui_button(&ui, ID_DLG_CANCEL, b, "Cancel") ||
  980. key_pressed(NGUI_KEY_ESC)) {
  981. dialog = DLG_NONE;
  982. return;
  983. }
  984. if (!go)
  985. return;
  986. ngui_input_value(&size_in, val, sizeof(val));
  987. for (i = 0; val[i]; i++)
  988. if (val[i] == ' ' || (val[i] >= 'A' && val[i] <= 'Z'))
  989. val[i] = val[i] == ' ' ? '\0' : (char)(val[i] + 32);
  990. snprintf(arg, sizeof(arg), "-y %s%s%s", d->bd.name,
  991. (!floppy && val[0]) ? " " : "", floppy ? "" : val);
  992. snprintf(line, sizeof(line), "Formatting %s", d->bd.name);
  993. ngui_set_focus(&ui, 0);
  994. start_job("format", arg, line);
  995. }
  996. static void draw_install_dialog(void)
  997. {
  998. const struct disk *d = &disks[dlg_disk];
  999. struct ngui_rect r = dialog_rect(520, d->nebos ? 290 : 250), b;
  1000. char line[96], arg[48], size[16];
  1001. int mode = 0;
  1002. ngui_inset(&r, 18);
  1003. snprintf(line, sizeof(line), "Install NebOS on %s", d->bd.name);
  1004. dialog_title(&r, line);
  1005. fmt_size(size, sizeof(size), d->bd.total_sectors);
  1006. snprintf(line, sizeof(line), "%s%s%s, %s", kind_name(d->bd.kind),
  1007. d->bd.model[0] ? " " : "", d->bd.model, size);
  1008. dialog_line(&r, line, ui.theme.text_dim);
  1009. if (d->nebos) {
  1010. dialog_line(&r, "NebOS is already installed here.", ui.theme.text);
  1011. dialog_line(&r, "Update writes the running kernel and rootfs to "
  1012. "partitions 1-2.", ui.theme.text_dim);
  1013. dialog_line(&r, "Reinstall clones the live image (partitions 1-3).",
  1014. ui.theme.text_dim);
  1015. dialog_line(&r, "Both keep the /data partition.",
  1016. ui.theme.success);
  1017. } else {
  1018. dialog_line(&r, "Clones the live NebOS image (boot, rootfs, EFI) and",
  1019. ui.theme.text_dim);
  1020. dialog_line(&r, "creates a /data partition on the rest of the disk.",
  1021. ui.theme.text_dim);
  1022. dialog_line(&r, "Everything else on this device is erased.",
  1023. ui.theme.warning);
  1024. }
  1025. b = ngui_cut_bottom(&r, 30);
  1026. b.x = r.x + r.w - 110;
  1027. b.w = 110;
  1028. if (ngui_button(&ui, ID_DLG_OK, b, d->nebos ? "Update" : "Install"))
  1029. mode = d->nebos ? 2 : 1;
  1030. if (d->nebos) {
  1031. b.x -= 118;
  1032. b.w = 110;
  1033. if (ngui_button(&ui, ID_DLG_OK2, b, "Reinstall"))
  1034. mode = 1;
  1035. }
  1036. b.x -= 100;
  1037. b.w = 92;
  1038. if (ngui_button(&ui, ID_DLG_CANCEL, b, "Cancel") ||
  1039. key_pressed(NGUI_KEY_ESC)) {
  1040. dialog = DLG_NONE;
  1041. return;
  1042. }
  1043. if (!mode)
  1044. return;
  1045. snprintf(arg, sizeof(arg), "-y %s%s", d->bd.name,
  1046. mode == 2 ? " self" : "");
  1047. snprintf(line, sizeof(line), "%s NebOS on %s",
  1048. mode == 2 ? "Updating" : "Installing", d->bd.name);
  1049. start_job("install", arg, line);
  1050. }
  1051. static void draw_job_dialog(void)
  1052. {
  1053. struct ngui_rect r = dialog_rect(600, 380), b, log;
  1054. int lh = 16, lines, n, i, start;
  1055. const char *p;
  1056. ngui_inset(&r, 18);
  1057. dialog_title(&r, job_title);
  1058. if (!job_done)
  1059. dialog_line(&r, "Running... keep the devices connected.",
  1060. ui.theme.warning);
  1061. else
  1062. dialog_line(&r, job_ok ? "Finished." : "Failed; see the output below.",
  1063. job_ok ? ui.theme.success : ui.theme.error);
  1064. ngui_cut_top(&r, 6);
  1065. b = ngui_cut_bottom(&r, 30);
  1066. ngui_cut_bottom(&r, 10);
  1067. log = r;
  1068. ngui_fill_rect(&ui, log, ui.theme.bg);
  1069. ngui_inset(&log, 6);
  1070. ngui_push_clip(&ui, log);
  1071. /* Show the newest lines that fit. */
  1072. lines = log.h / lh;
  1073. n = 0;
  1074. for (p = job_log; *p; p++)
  1075. if (*p == '\n')
  1076. n++;
  1077. if (job_len && job_log[job_len - 1] != '\n')
  1078. n++;
  1079. start = n > lines ? n - lines : 0;
  1080. p = job_log;
  1081. for (i = 0; *p && i < start; p++)
  1082. if (*p == '\n')
  1083. i++;
  1084. for (i = 0; *p && i < lines; i++) {
  1085. const char *e = strchr(p, '\n');
  1086. unsigned int len = e ? (unsigned int)(e - p) : (unsigned int)strlen(p);
  1087. ngui_text_px(&ui, log.x, log.y + i * lh, p, len, ui.theme.text,
  1088. 13, 0, 0, 1);
  1089. p += len;
  1090. if (*p == '\n')
  1091. p++;
  1092. }
  1093. ngui_pop_clip(&ui);
  1094. if (job_done) {
  1095. b.x = r.x + r.w - 92;
  1096. b.w = 92;
  1097. if (ngui_button(&ui, ID_DLG_CLOSE, b, "Close") ||
  1098. key_pressed(NGUI_KEY_ESC) || key_pressed('\n'))
  1099. dialog = DLG_NONE;
  1100. }
  1101. }
  1102. static void draw(void)
  1103. {
  1104. struct ngui_rect all = { 0, 0, ui.w, ui.h }, body, list;
  1105. struct input_save save;
  1106. int muted = dialog != DLG_NONE;
  1107. if (muted)
  1108. input_mute(&save);
  1109. ngui_fill_rect(&ui, all, ui.theme.bg);
  1110. body = all;
  1111. draw_toolbar(ngui_cut_top(&body, TOOLBAR_H));
  1112. list = ngui_cut_left(&body, LIST_W);
  1113. draw_device_list(list);
  1114. ngui_fill_rect(&ui, ngui_cut_left(&body, 1), ui.theme.border);
  1115. draw_details(body);
  1116. if (dialog == DLG_NONE)
  1117. return;
  1118. if (muted)
  1119. input_restore(&save);
  1120. if (dialog == DLG_FORMAT)
  1121. draw_format_dialog();
  1122. else if (dialog == DLG_INSTALL)
  1123. draw_install_dialog();
  1124. else if (dialog == DLG_JOB)
  1125. draw_job_dialog();
  1126. }
  1127. void _start(const char *arg)
  1128. {
  1129. struct win_info wi;
  1130. unsigned int cw, ch;
  1131. int quit = 0;
  1132. (void)arg;
  1133. if (u_win_create(800, 560, "Disks", &wi) != 0) {
  1134. u_puts("disks: no graphics window\n");
  1135. u_exit(1);
  1136. }
  1137. cw = wi.width;
  1138. ch = wi.height;
  1139. ngui_init(&ui, (unsigned short *)wi.fb, (int)cw, (int)ch);
  1140. ngui_font_attach(&ui);
  1141. name_in.placeholder = "mount name";
  1142. load_all();
  1143. for (;;) {
  1144. struct win_info cur;
  1145. if (u_win_info(&cur) == 0 &&
  1146. (cur.width != cw || cur.height != ch) &&
  1147. u_win_ack_resize() == 0) {
  1148. cw = cur.width;
  1149. ch = cur.height;
  1150. ui.px = (unsigned short *)cur.fb;
  1151. ui.w = (int)cw;
  1152. ui.h = (int)ch;
  1153. ngui_invalidate(&ui);
  1154. }
  1155. job_poll();
  1156. if (status_until && (int)(u_ticks_ms() - status_until) >= 0) {
  1157. status[0] = '\0';
  1158. status_until = 0;
  1159. ngui_invalidate(&ui);
  1160. }
  1161. if (!ngui_begin(&ui)) {
  1162. u_sleep_ms(dialog == DLG_JOB ? 20 : 10);
  1163. continue;
  1164. }
  1165. draw();
  1166. if (dialog == DLG_NONE && !ui.focus && key_pressed(NGUI_KEY_ESC))
  1167. quit = 1;
  1168. ngui_end(&ui);
  1169. if (quit)
  1170. break;
  1171. if (rescan_pending) {
  1172. /* The frame above showed "Scanning..."; the rescan
  1173. * blocks this window while it probes the disks. */
  1174. struct sys_storage_info si;
  1175. rescan_pending = 0;
  1176. u_syscall1(SYS_STORAGE_INFO, (int)&si);
  1177. load_all();
  1178. set_status("Devices rescanned", 0);
  1179. }
  1180. u_sleep_ms(10);
  1181. }
  1182. u_win_close();
  1183. u_exit(0);
  1184. }