ucmd_disks.c 35 KB

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