ucmd_disks.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302
  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;
  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. y = area.y - list_scroll.offset;
  559. r = (struct ngui_rect){ area.x, y, area.w, SECTION_H };
  560. ngui_section(&ui, r, "Overview", 0, 0);
  561. y += SECTION_H;
  562. r = (struct ngui_rect){ area.x, y, area.w, ROW_H };
  563. if (visible(r, area)) {
  564. snprintf(sub, sizeof(sub), "%d filesystem%s", nmounts,
  565. nmounts == 1 ? "" : "s");
  566. row.title = "All mounts";
  567. row.subtitle = sub;
  568. row.trailing = 0;
  569. row.bars = -1;
  570. row.selected = sel_disk < 0;
  571. if (ngui_list_row(&ui, ID_OVERVIEW, r, &row)) {
  572. sel_disk = -1;
  573. detail_scroll.offset = 0;
  574. ngui_invalidate(&ui);
  575. }
  576. }
  577. y += ROW_H;
  578. r = (struct ngui_rect){ area.x, y, area.w, SECTION_H };
  579. ngui_section(&ui, r, "Devices", 0, 0);
  580. y += SECTION_H;
  581. for (i = 0; i < ndisks; i++) {
  582. const struct disk *d = &disks[i];
  583. if (!listed(d))
  584. continue;
  585. r = (struct ngui_rect){ area.x, y, area.w, ROW_H };
  586. y += ROW_H;
  587. if (!visible(r, area))
  588. continue;
  589. fmt_size(size, sizeof(size), d->bd.total_sectors);
  590. if (!d->bd.present)
  591. snprintf(sub, sizeof(sub), "%s, not present",
  592. kind_name(d->bd.kind));
  593. else
  594. snprintf(sub, sizeof(sub), "%s %s",
  595. kind_name(d->bd.kind),
  596. d->bd.kind == KIND_FLOPPY ? "" : size);
  597. row.title = d->bd.name;
  598. row.subtitle = sub;
  599. row.trailing = d->nebos ? "NebOS" : 0;
  600. row.bars = -1;
  601. row.selected = sel_disk == i;
  602. if (ngui_list_row(&ui, ID_DISK_BASE + i, r, &row) &&
  603. sel_disk != i) {
  604. sel_disk = i;
  605. sel_key[0] = '\0';
  606. detail_scroll.offset = 0;
  607. ngui_invalidate(&ui);
  608. }
  609. }
  610. ngui_scroll_end(&ui, area, &list_scroll);
  611. }
  612. /* The overview: one line per mount. */
  613. static int draw_overview(struct ngui_rect area, int y)
  614. {
  615. static const int col[] = { 0, 96, 156, 230, 330, 404 };
  616. static const char *hdr[] = { "Mount", "Type", "Size", "Label",
  617. "Device", "Mode" };
  618. char size[16];
  619. int i, j;
  620. ngui_text_px(&ui, area.x, y, "All mounts", 10, ui.theme.text, 18, 1, 0, 0);
  621. y += 32;
  622. for (j = 0; j < 6; j++)
  623. text_dim(area.x + col[j], y, hdr[j]);
  624. y += 22;
  625. ngui_fill_rect(&ui, (struct ngui_rect){ area.x, y - 4, area.w, 1 },
  626. ui.theme.border);
  627. for (i = 0; i < nmounts; i++) {
  628. const struct sys_mount *m = &mounts[i];
  629. struct ngui_rect r = { area.x, y, area.w, 26 };
  630. char name[20];
  631. if (visible(r, area)) {
  632. snprintf(name, sizeof(name), "/%s", m->name);
  633. fmt_size(size, sizeof(size), m->total_sectors);
  634. ngui_text(&ui, area.x + col[0], y + 4, name, ui.theme.text);
  635. ngui_text(&ui, area.x + col[1], y + 4, m->type, ui.theme.text);
  636. ngui_text(&ui, area.x + col[2], y + 4, size, ui.theme.text);
  637. ngui_text(&ui, area.x + col[3], y + 4, m->label, ui.theme.text);
  638. ngui_text(&ui, area.x + col[4], y + 4,
  639. m->dev[0] ? m->key : "RAM", ui.theme.text_dim);
  640. ngui_text(&ui, area.x + col[5], y + 4,
  641. m->ro ? "read-only" : "read-write",
  642. m->ro ? ui.theme.warning : ui.theme.text_dim);
  643. if (m->dev[0] && ngui_button(&ui, ID_MOUNT_BASE + i,
  644. (struct ngui_rect){ area.x + area.w - 60, y + 1,
  645. 60, 24 }, "Show")) {
  646. for (j = 0; j < ndisks; j++)
  647. if (!strcmp(disks[j].bd.name, m->dev))
  648. sel_disk = j;
  649. snprintf(sel_key, sizeof(sel_key), "%s", m->key);
  650. detail_scroll.offset = 0;
  651. ngui_invalidate(&ui);
  652. }
  653. }
  654. y += 28;
  655. }
  656. y += GAP;
  657. text_dim(area.x, y,
  658. "Select a device to format it, install NebOS or change how");
  659. y += 18;
  660. text_dim(area.x, y, "its volumes are mounted.");
  661. return y + 24;
  662. }
  663. static void load_editor(const struct part *p)
  664. {
  665. const struct sys_mount_policy *pol = policy_by_key(p->key);
  666. const struct sys_mount *m = mount_by_key(p->key);
  667. snprintf(edit_key, sizeof(edit_key), "%s", p->key);
  668. if (pol && pol->name[0])
  669. ngui_input_set(&name_in, pol->name);
  670. else
  671. ngui_input_set(&name_in, m ? m->name : p->key);
  672. edit_ro = pol && (pol->flags & SYS_MOUNT_RO);
  673. edit_auto = !(pol && (pol->flags & SYS_MOUNT_NOAUTO));
  674. }
  675. static int draw_mount_editor(struct ngui_rect area, int y, const struct part *p)
  676. {
  677. const struct sys_mount *m = mount_by_key(p->key);
  678. struct ngui_rect r, b;
  679. char line[96], name[16];
  680. unsigned int flags;
  681. if (strcmp(edit_key, p->key) != 0)
  682. load_editor(p);
  683. r = (struct ngui_rect){ area.x, y, area.w, SECTION_H };
  684. snprintf(line, sizeof(line), "Volume %s", p->key);
  685. ngui_section(&ui, r, line, 0, 0);
  686. y += SECTION_H;
  687. if (m)
  688. snprintf(line, sizeof(line), "Mounted at /%s, %s, %s", m->name,
  689. m->type, m->ro ? "read-only" : "read-write");
  690. else
  691. snprintf(line, sizeof(line), "Not mounted");
  692. ngui_text(&ui, area.x, y + 2, line, m ? ui.theme.success : ui.theme.text_dim);
  693. y += 24;
  694. if (is_data_volume(p)) {
  695. text_dim(area.x, y,
  696. "This is the NebOS data volume (/data). It holds the");
  697. y += 18;
  698. text_dim(area.x, y,
  699. "saved settings, so it is always mounted read-write.");
  700. y += 26;
  701. if (m && ngui_button(&ui, ID_OPEN,
  702. (struct ngui_rect){ area.x, y, 130, 28 },
  703. "Open in Files"))
  704. goto open_files;
  705. return y + 38;
  706. }
  707. text_dim(area.x, y + 6, "Mount name");
  708. r = (struct ngui_rect){ area.x + 110, y, 180, 28 };
  709. if (visible(r, area))
  710. ngui_text_input(&ui, ID_NAME, r, &name_in);
  711. y += 34;
  712. r = (struct ngui_rect){ area.x, y, 290, 26 };
  713. if (visible(r, area) &&
  714. ngui_menu_item(&ui, ID_AUTO, r, "Mount automatically", 0, edit_auto))
  715. edit_auto = !edit_auto;
  716. y += 28;
  717. r = (struct ngui_rect){ area.x, y, 290, 26 };
  718. if (visible(r, area) &&
  719. ngui_menu_item(&ui, ID_RO, r, "Read-only", 0, edit_ro))
  720. edit_ro = !edit_ro;
  721. y += 34;
  722. ngui_input_value(&name_in, name, sizeof(name));
  723. flags = (edit_ro ? SYS_MOUNT_RO : 0) | (edit_auto ? 0 : SYS_MOUNT_NOAUTO);
  724. b = (struct ngui_rect){ area.x, y, 90, 28 };
  725. if (visible(b, area) && ngui_button(&ui, ID_APPLY, b, "Apply"))
  726. apply_policy(p->key, name, flags);
  727. b.x += 98;
  728. b.w = 100;
  729. if (visible(b, area) &&
  730. ngui_button(&ui, ID_MOUNT_TOGGLE, b, m ? "Unmount" : "Mount")) {
  731. if (m)
  732. apply_policy(p->key, name, flags | SYS_MOUNT_NOAUTO);
  733. else
  734. apply_policy(p->key, name, flags & ~SYS_MOUNT_NOAUTO);
  735. }
  736. b.x += 108;
  737. b.w = 90;
  738. if (visible(b, area) && ngui_button(&ui, ID_RESET, b, "Defaults"))
  739. apply_policy(p->key, "", 0);
  740. b.x += 98;
  741. b.w = 130;
  742. if (m && visible(b, area) && ngui_button(&ui, ID_OPEN, b, "Open in Files"))
  743. goto open_files;
  744. y += 38;
  745. text_dim(area.x, y,
  746. "Settings apply at once and are saved to /data/MOUNTS.CFG,");
  747. y += 18;
  748. text_dim(area.x, y, "so the boot scan uses them too.");
  749. return y + 26;
  750. open_files:
  751. {
  752. char cwd[24];
  753. snprintf(cwd, sizeof(cwd), "/%s", m->name);
  754. if (u_spawn("/ramfs/bin/files", "", 0, cwd) < 0)
  755. set_status("Could not start files", 1);
  756. }
  757. return y + 38;
  758. }
  759. static int draw_disk(struct ngui_rect area, int y, struct disk *d)
  760. {
  761. struct ngui_row row;
  762. struct ngui_rect r, b;
  763. char line[128], size[16], title[48], sub[80];
  764. const struct part *sel = 0;
  765. int i;
  766. snprintf(title, sizeof(title), "%s", d->bd.name);
  767. ngui_text_px(&ui, area.x, y, title, (unsigned int)strlen(title),
  768. ui.theme.text, 18, 1, 0, 0);
  769. y += 30;
  770. fmt_size(size, sizeof(size), d->bd.total_sectors);
  771. if (d->bd.kind == KIND_FLOPPY)
  772. snprintf(line, sizeof(line), "%s drive", kind_name(d->bd.kind));
  773. else
  774. snprintf(line, sizeof(line), "%s%s%s, %s (%u sectors)",
  775. kind_name(d->bd.kind), d->bd.model[0] ? " " : "",
  776. d->bd.model, size, d->bd.total_sectors);
  777. text_dim(area.x, y, line);
  778. y += 20;
  779. if (!d->bd.present) {
  780. ngui_text(&ui, area.x, y, "Not present", ui.theme.warning);
  781. return y + 30;
  782. }
  783. if (d->nebos) {
  784. ngui_text(&ui, area.x, y, d->has_data ?
  785. "NebOS installed, with a /data partition" :
  786. "NebOS installed", ui.theme.success);
  787. y += 20;
  788. }
  789. y += GAP;
  790. r = (struct ngui_rect){ area.x, y, area.w, SECTION_H };
  791. ngui_section(&ui, r, d->mbr ? "Partitions" : "Volume", 0, 0);
  792. y += SECTION_H;
  793. if (d->bd.kind != KIND_FLOPPY && !d->read_ok) {
  794. ngui_text(&ui, area.x, y + 4, "The device could not be read",
  795. ui.theme.error);
  796. y += 30;
  797. } else if (!d->nparts) {
  798. text_dim(area.x, y + 4,
  799. "No partition table and no FAT filesystem");
  800. y += 30;
  801. }
  802. for (i = 0; i < d->nparts; i++) {
  803. const struct part *p = &d->parts[i];
  804. const struct sys_mount *m = mount_by_key(p->key);
  805. const struct sys_mount_policy *pol = policy_by_key(p->key);
  806. const char *what;
  807. char trail[24];
  808. int selectable = p->fat && !p->nebos_boot;
  809. r = (struct ngui_rect){ area.x, y, area.w, ROW_H };
  810. y += ROW_H;
  811. if (selectable && !sel_key[0])
  812. snprintf(sel_key, sizeof(sel_key), "%s", p->key);
  813. if (selectable && !strcmp(sel_key, p->key))
  814. sel = p;
  815. if (!visible(r, area))
  816. continue;
  817. if (p->nebos_boot)
  818. what = "NebOS boot + kernel";
  819. else if (p->fat)
  820. what = p->fs;
  821. else
  822. what = type_name(p->type);
  823. fmt_size(size, sizeof(size), p->sectors);
  824. if (p->slot >= 0)
  825. snprintf(title, sizeof(title), "%d %s%s%s", p->slot + 1,
  826. what, p->label[0] ? " " : "", p->label);
  827. else
  828. /* A floppy is never read here; its mount has the label. */
  829. snprintf(title, sizeof(title), "%s%s%s",
  830. d->bd.kind == KIND_FLOPPY ? "Floppy disk" : what,
  831. (p->label[0] || (m && m->label[0])) ? " " : "",
  832. p->label[0] ? p->label : (m ? m->label : ""));
  833. if (p->slot >= 0)
  834. snprintf(sub, sizeof(sub), "%s, type 0x%02X, LBA %u%s",
  835. size, p->type, p->start,
  836. p->active ? ", active" : "");
  837. else
  838. snprintf(sub, sizeof(sub), "%s, whole device",
  839. d->bd.kind == KIND_FLOPPY ? "1.4 MB" : size);
  840. if (m)
  841. snprintf(trail, sizeof(trail), "/%s%s", m->name,
  842. m->ro ? " (ro)" : "");
  843. else if (pol && (pol->flags & SYS_MOUNT_NOAUTO))
  844. snprintf(trail, sizeof(trail), "unmounted");
  845. else
  846. snprintf(trail, sizeof(trail), "%s",
  847. selectable ? "not mounted" : "");
  848. row.title = title;
  849. row.subtitle = sub;
  850. row.trailing = trail[0] ? trail : 0;
  851. row.bars = -1;
  852. row.selected = sel == p;
  853. if (ngui_list_row(&ui, ID_PART_BASE + d->index * 4 +
  854. (unsigned int)(p->slot < 0 ? 0 : p->slot),
  855. r, &row) && selectable) {
  856. snprintf(sel_key, sizeof(sel_key), "%s", p->key);
  857. ngui_invalidate(&ui);
  858. }
  859. }
  860. y += GAP;
  861. if (sel)
  862. y = draw_mount_editor(area, y, sel) + GAP;
  863. r = (struct ngui_rect){ area.x, y, area.w, SECTION_H };
  864. ngui_section(&ui, r, "Device", 0, 0);
  865. y += SECTION_H;
  866. b = (struct ngui_rect){ area.x, y, 120, 28 };
  867. if (visible(b, area) && ngui_button(&ui, ID_FORMAT, b, "Format...")) {
  868. dialog = DLG_FORMAT;
  869. dlg_disk = sel_disk;
  870. ngui_input_set(&size_in, "");
  871. ngui_set_focus(&ui, ID_DLG_SIZE);
  872. }
  873. b.x += 128;
  874. b.w = 150;
  875. if (d->bd.kind != KIND_FLOPPY && visible(b, area) &&
  876. ngui_button(&ui, ID_INSTALL, b, "Install NebOS...")) {
  877. dialog = DLG_INSTALL;
  878. dlg_disk = sel_disk;
  879. }
  880. y += 36;
  881. text_dim(area.x, y, d->bd.kind == KIND_FLOPPY ?
  882. "Format writes a fresh FAT12 filesystem." :
  883. "Format writes one FAT partition (MBR) over the whole disk.");
  884. return y + 26;
  885. }
  886. static void draw_details(struct ngui_rect area)
  887. {
  888. int y, top;
  889. ngui_inset(&area, 14);
  890. ngui_scroll_begin(&ui, area, &detail_scroll);
  891. ngui_scroll_wheel(&ui, area, &detail_scroll);
  892. if (!ui.focus)
  893. ngui_scroll_keys(&ui, &detail_scroll);
  894. top = area.y - detail_scroll.offset;
  895. if (sel_disk < 0)
  896. y = draw_overview(area, top);
  897. else
  898. y = draw_disk(area, top, &disks[sel_disk]);
  899. detail_scroll.content_h = y - top;
  900. ngui_scroll_end(&ui, area, &detail_scroll);
  901. }
  902. /* ---- dialogs ---- */
  903. static struct ngui_rect dialog_rect(int w, int h)
  904. {
  905. struct ngui_rect r;
  906. r.w = w < ui.w - 20 ? w : ui.w - 20;
  907. r.h = h < ui.h - 20 ? h : ui.h - 20;
  908. r.x = (ui.w - r.w) / 2;
  909. r.y = (ui.h - r.h) / 2;
  910. ngui_fill_round_rect(&ui, (struct ngui_rect){ r.x - 1, r.y - 1,
  911. r.w + 2, r.h + 2 }, 9, 9, 9, 9,
  912. ui.theme.border);
  913. ngui_fill_round_rect(&ui, r, 8, 8, 8, 8, ui.theme.panel);
  914. return r;
  915. }
  916. static void dialog_title(struct ngui_rect *r, const char *s)
  917. {
  918. struct ngui_rect t = ngui_cut_top(r, 34);
  919. ngui_text_px(&ui, t.x, t.y + 6, s, (unsigned int)strlen(s),
  920. ui.theme.text, 16, 1, 0, 0);
  921. }
  922. static void dialog_line(struct ngui_rect *r, const char *s, unsigned short c)
  923. {
  924. struct ngui_rect t = ngui_cut_top(r, 20);
  925. ngui_text_ellipsis(&ui, t, t.x, t.y, s, c);
  926. }
  927. static int key_pressed(int key)
  928. {
  929. if (ngui_key_peek(&ui) == key) {
  930. ngui_key(&ui);
  931. return 1;
  932. }
  933. return 0;
  934. }
  935. static void draw_format_dialog(void)
  936. {
  937. const struct disk *d = &disks[dlg_disk];
  938. struct ngui_rect r = dialog_rect(460, 260), b;
  939. char line[96], size[16], arg[48], val[24];
  940. int floppy = d->bd.kind == KIND_FLOPPY, go = 0, i;
  941. ngui_inset(&r, 18);
  942. snprintf(line, sizeof(line), "Format %s", d->bd.name);
  943. dialog_title(&r, line);
  944. fmt_size(size, sizeof(size), d->bd.total_sectors);
  945. snprintf(line, sizeof(line), "%s%s%s, %s", kind_name(d->bd.kind),
  946. d->bd.model[0] ? " " : "", d->bd.model,
  947. floppy ? "1.4 MB" : size);
  948. dialog_line(&r, line, ui.theme.text_dim);
  949. dialog_line(&r, "Every file and partition on this device is erased.",
  950. ui.theme.warning);
  951. if (d->nebos)
  952. dialog_line(&r, "This removes the NebOS installation as well.",
  953. ui.theme.warning);
  954. ngui_cut_top(&r, 8);
  955. if (!floppy) {
  956. b = ngui_cut_top(&r, 30);
  957. text_dim(b.x, b.y + 6, "Size");
  958. b.x += 60;
  959. b.w = 140;
  960. size_in.placeholder = "whole disk";
  961. if (ngui_text_input(&ui, ID_DLG_SIZE, b, &size_in) &
  962. NGUI_INPUT_SUBMIT)
  963. go = 1;
  964. text_dim(b.x + b.w + 10, b.y + 6, "e.g. 100mb, 2gb");
  965. }
  966. b = ngui_cut_bottom(&r, 30);
  967. b.x = r.x + r.w - 110;
  968. b.w = 110;
  969. if (ngui_button(&ui, ID_DLG_OK, b, "Erase disk"))
  970. go = 1;
  971. b.x -= 100;
  972. b.w = 92;
  973. if (ngui_button(&ui, ID_DLG_CANCEL, b, "Cancel") ||
  974. key_pressed(NGUI_KEY_ESC)) {
  975. dialog = DLG_NONE;
  976. return;
  977. }
  978. if (!go)
  979. return;
  980. ngui_input_value(&size_in, val, sizeof(val));
  981. for (i = 0; val[i]; i++)
  982. if (val[i] == ' ' || (val[i] >= 'A' && val[i] <= 'Z'))
  983. val[i] = val[i] == ' ' ? '\0' : (char)(val[i] + 32);
  984. snprintf(arg, sizeof(arg), "-y %s%s%s", d->bd.name,
  985. (!floppy && val[0]) ? " " : "", floppy ? "" : val);
  986. snprintf(line, sizeof(line), "Formatting %s", d->bd.name);
  987. ngui_set_focus(&ui, 0);
  988. start_job("format", arg, line);
  989. }
  990. static void draw_install_dialog(void)
  991. {
  992. const struct disk *d = &disks[dlg_disk];
  993. struct ngui_rect r = dialog_rect(520, d->nebos ? 290 : 250), b;
  994. char line[96], arg[48], size[16];
  995. int mode = 0;
  996. ngui_inset(&r, 18);
  997. snprintf(line, sizeof(line), "Install NebOS on %s", d->bd.name);
  998. dialog_title(&r, line);
  999. fmt_size(size, sizeof(size), d->bd.total_sectors);
  1000. snprintf(line, sizeof(line), "%s%s%s, %s", kind_name(d->bd.kind),
  1001. d->bd.model[0] ? " " : "", d->bd.model, size);
  1002. dialog_line(&r, line, ui.theme.text_dim);
  1003. if (d->nebos) {
  1004. dialog_line(&r, "NebOS is already installed here.", ui.theme.text);
  1005. dialog_line(&r, "Update writes the running kernel and rootfs to "
  1006. "partitions 1-2.", ui.theme.text_dim);
  1007. dialog_line(&r, "Reinstall clones the live image (partitions 1-3).",
  1008. ui.theme.text_dim);
  1009. dialog_line(&r, "Both keep the /data partition.",
  1010. ui.theme.success);
  1011. } else {
  1012. dialog_line(&r, "Clones the live NebOS image (boot, rootfs, EFI) and",
  1013. ui.theme.text_dim);
  1014. dialog_line(&r, "creates a /data partition on the rest of the disk.",
  1015. ui.theme.text_dim);
  1016. dialog_line(&r, "Everything else on this device is erased.",
  1017. ui.theme.warning);
  1018. }
  1019. b = ngui_cut_bottom(&r, 30);
  1020. b.x = r.x + r.w - 110;
  1021. b.w = 110;
  1022. if (ngui_button(&ui, ID_DLG_OK, b, d->nebos ? "Update" : "Install"))
  1023. mode = d->nebos ? 2 : 1;
  1024. if (d->nebos) {
  1025. b.x -= 118;
  1026. b.w = 110;
  1027. if (ngui_button(&ui, ID_DLG_OK2, b, "Reinstall"))
  1028. mode = 1;
  1029. }
  1030. b.x -= 100;
  1031. b.w = 92;
  1032. if (ngui_button(&ui, ID_DLG_CANCEL, b, "Cancel") ||
  1033. key_pressed(NGUI_KEY_ESC)) {
  1034. dialog = DLG_NONE;
  1035. return;
  1036. }
  1037. if (!mode)
  1038. return;
  1039. snprintf(arg, sizeof(arg), "-y %s%s", d->bd.name,
  1040. mode == 2 ? " self" : "");
  1041. snprintf(line, sizeof(line), "%s NebOS on %s",
  1042. mode == 2 ? "Updating" : "Installing", d->bd.name);
  1043. start_job("install", arg, line);
  1044. }
  1045. static void draw_job_dialog(void)
  1046. {
  1047. struct ngui_rect r = dialog_rect(600, 380), b, log;
  1048. int lh = 16, lines, n, i, start;
  1049. const char *p;
  1050. ngui_inset(&r, 18);
  1051. dialog_title(&r, job_title);
  1052. if (!job_done)
  1053. dialog_line(&r, "Running... keep the devices connected.",
  1054. ui.theme.warning);
  1055. else
  1056. dialog_line(&r, job_ok ? "Finished." : "Failed; see the output below.",
  1057. job_ok ? ui.theme.success : ui.theme.error);
  1058. ngui_cut_top(&r, 6);
  1059. b = ngui_cut_bottom(&r, 30);
  1060. ngui_cut_bottom(&r, 10);
  1061. log = r;
  1062. ngui_fill_rect(&ui, log, ui.theme.bg);
  1063. ngui_inset(&log, 6);
  1064. ngui_push_clip(&ui, log);
  1065. /* Show the newest lines that fit. */
  1066. lines = log.h / lh;
  1067. n = 0;
  1068. for (p = job_log; *p; p++)
  1069. if (*p == '\n')
  1070. n++;
  1071. if (job_len && job_log[job_len - 1] != '\n')
  1072. n++;
  1073. start = n > lines ? n - lines : 0;
  1074. p = job_log;
  1075. for (i = 0; *p && i < start; p++)
  1076. if (*p == '\n')
  1077. i++;
  1078. for (i = 0; *p && i < lines; i++) {
  1079. const char *e = strchr(p, '\n');
  1080. unsigned int len = e ? (unsigned int)(e - p) : (unsigned int)strlen(p);
  1081. ngui_text_px(&ui, log.x, log.y + i * lh, p, len, ui.theme.text,
  1082. 13, 0, 0, 1);
  1083. p += len;
  1084. if (*p == '\n')
  1085. p++;
  1086. }
  1087. ngui_pop_clip(&ui);
  1088. if (job_done) {
  1089. b.x = r.x + r.w - 92;
  1090. b.w = 92;
  1091. if (ngui_button(&ui, ID_DLG_CLOSE, b, "Close") ||
  1092. key_pressed(NGUI_KEY_ESC) || key_pressed('\n'))
  1093. dialog = DLG_NONE;
  1094. }
  1095. }
  1096. static void draw(void)
  1097. {
  1098. struct ngui_rect all = { 0, 0, ui.w, ui.h }, body, list;
  1099. struct input_save save;
  1100. int muted = dialog != DLG_NONE;
  1101. if (muted)
  1102. input_mute(&save);
  1103. ngui_fill_rect(&ui, all, ui.theme.bg);
  1104. body = all;
  1105. draw_toolbar(ngui_cut_top(&body, TOOLBAR_H));
  1106. list = ngui_cut_left(&body, LIST_W);
  1107. draw_device_list(list);
  1108. ngui_fill_rect(&ui, ngui_cut_left(&body, 1), ui.theme.border);
  1109. draw_details(body);
  1110. if (dialog == DLG_NONE)
  1111. return;
  1112. if (muted)
  1113. input_restore(&save);
  1114. if (dialog == DLG_FORMAT)
  1115. draw_format_dialog();
  1116. else if (dialog == DLG_INSTALL)
  1117. draw_install_dialog();
  1118. else if (dialog == DLG_JOB)
  1119. draw_job_dialog();
  1120. }
  1121. void _start(const char *arg)
  1122. {
  1123. struct win_info wi;
  1124. unsigned int cw, ch;
  1125. int quit = 0;
  1126. (void)arg;
  1127. if (u_win_create(800, 560, "Disks", &wi) != 0) {
  1128. u_puts("disks: no graphics window\n");
  1129. u_exit(1);
  1130. }
  1131. cw = wi.width;
  1132. ch = wi.height;
  1133. ngui_init(&ui, (unsigned short *)wi.fb, (int)cw, (int)ch);
  1134. ngui_font_attach(&ui);
  1135. name_in.placeholder = "mount name";
  1136. load_all();
  1137. for (;;) {
  1138. struct win_info cur;
  1139. if (u_win_info(&cur) == 0 &&
  1140. (cur.width != cw || cur.height != ch) &&
  1141. u_win_ack_resize() == 0) {
  1142. cw = cur.width;
  1143. ch = cur.height;
  1144. ui.px = (unsigned short *)cur.fb;
  1145. ui.w = (int)cw;
  1146. ui.h = (int)ch;
  1147. ngui_invalidate(&ui);
  1148. }
  1149. job_poll();
  1150. if (status_until && (int)(u_ticks_ms() - status_until) >= 0) {
  1151. status[0] = '\0';
  1152. status_until = 0;
  1153. ngui_invalidate(&ui);
  1154. }
  1155. if (!ngui_begin(&ui)) {
  1156. u_sleep_ms(dialog == DLG_JOB ? 20 : 10);
  1157. continue;
  1158. }
  1159. draw();
  1160. if (dialog == DLG_NONE && !ui.focus && key_pressed(NGUI_KEY_ESC))
  1161. quit = 1;
  1162. ngui_end(&ui);
  1163. if (quit)
  1164. break;
  1165. if (rescan_pending) {
  1166. /* The frame above showed "Scanning..."; the rescan
  1167. * blocks this window while it probes the disks. */
  1168. struct sys_storage_info si;
  1169. rescan_pending = 0;
  1170. u_syscall1(SYS_STORAGE_INFO, (int)&si);
  1171. load_all();
  1172. set_status("Devices rescanned", 0);
  1173. }
  1174. u_sleep_ms(10);
  1175. }
  1176. u_win_close();
  1177. u_exit(0);
  1178. }