install.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. /* install: clone the live NebOS image onto a storage device.
  2. *
  3. * The source is the running install medium as a block device. A USB stick
  4. * needs the USB mass-storage driver (usb0); an internal image can be read
  5. * from IDE/SATA/eMMC directly. The installer copies the image's used
  6. * sectors (MBR through the end of the last partition), writes the target's
  7. * MBR last and verifies the copy with a checksum. The cloned image keeps
  8. * both boot paths: the MBR/legacy chain and (when present) the ESP/UEFI
  9. * chain.
  10. *
  11. * Usage: install <target> [source]
  12. */
  13. #include "blkdev.h"
  14. #include "fatformat.h"
  15. #include "io.h"
  16. #include "mm.h"
  17. #include "process.h"
  18. #include "sched.h"
  19. #include "serial.h"
  20. #include "vga.h"
  21. #include "vfs.h"
  22. #define INSTALL_NAME_MAX 16
  23. #define INSTALL_CHUNK 32u /* 16 KiB copy buffer */
  24. #define INSTALL_DOT_LBA 2048u /* one progress dot per MiB */
  25. #define MBR_PART_OFF 0x1BE
  26. #define MBR_PART_SIZE 16
  27. #define BOOT_LBA 63u
  28. #define ROOTFS_LBA 1088u
  29. #define PACK_LBA_OFF 0x42
  30. #define KERNEL_LBA_OFF 0x40
  31. #define KERNEL_SEC_OFF 0x3E
  32. #define ESP_TYPE 0xEFu
  33. static unsigned char sector0[512];
  34. static unsigned char copy_buf[INSTALL_CHUNK * 512];
  35. /* The copy and verify release the BKL around each chunk, like the format
  36. * raw-sector loop, so the WM and terminals keep running. format_busy keeps
  37. * disk-backed filesystem syscalls out; the device descriptors are copied
  38. * before the loop so a mount rescan cannot invalidate them. */
  39. static int read_chunk(blk_dev_t *d, unsigned int lba, void *buf,
  40. unsigned int n)
  41. {
  42. int r;
  43. bkl_pause();
  44. r = blk_read(d, lba, buf, n);
  45. bkl_resume();
  46. return r;
  47. }
  48. static int write_chunk(blk_dev_t *d, unsigned int lba, const void *buf,
  49. unsigned int n)
  50. {
  51. int r;
  52. bkl_pause();
  53. r = blk_write(d, lba, buf, n);
  54. bkl_resume();
  55. return r;
  56. }
  57. static void free_pages(unsigned char *p, unsigned int pages)
  58. {
  59. unsigned int i;
  60. if (!p)
  61. return;
  62. for (i = 0; i < pages; i++)
  63. mm_free_page((unsigned int)p + i * 4096u);
  64. }
  65. static unsigned int get16(const unsigned char *p)
  66. {
  67. return (unsigned int)p[0] | ((unsigned int)p[1] << 8);
  68. }
  69. static unsigned int get32(const unsigned char *p)
  70. {
  71. return (unsigned int)p[0] | ((unsigned int)p[1] << 8) |
  72. ((unsigned int)p[2] << 16) | ((unsigned int)p[3] << 24);
  73. }
  74. static void list_devices(void)
  75. {
  76. unsigned int i;
  77. io_putstr("devices:");
  78. for (i = 0; i < blk_dev_count; i++) {
  79. io_putchar(' ');
  80. io_putstr(blk_devs[i].name);
  81. }
  82. io_putchar('\n');
  83. }
  84. static void put_size(unsigned int sectors)
  85. {
  86. if (sectors % 2048u == 0) {
  87. format_putdec(sectors / 2048u);
  88. io_putstr(" MiB");
  89. } else {
  90. format_putdec(sectors);
  91. io_putstr(" sectors");
  92. }
  93. }
  94. /* Validate the NebOS image signature and return its used sector count.
  95. * The image layout is the one the Makefile builds: an MBR whose first
  96. * partition starts at LBA 63 and holds the boot sector at 63 + the kernel
  97. * at 64, and a boot sector whose pack_lba points at the rootfs image.
  98. * `mbr_out` (optional) receives the MBR read during validation. */
  99. static int check_nebos_image(blk_dev_t *d, unsigned int *used, int *has_esp,
  100. unsigned char *mbr_out)
  101. {
  102. unsigned char mbr_local[512], boot[512];
  103. unsigned char *mbr = mbr_out ? mbr_out : mbr_local;
  104. unsigned int i, max_end = 0;
  105. int esp = 0;
  106. if (!d || !d->present || d->kind == BLK_FLOPPY)
  107. return 0;
  108. if (d->total_sectors < ROOTFS_LBA + 512u)
  109. return 0;
  110. if (blk_read(d, 0, mbr, 1) != 0)
  111. return 0;
  112. if (mbr[510] != 0x55 || mbr[511] != 0xAA)
  113. return 0;
  114. /* Partition 1: active, starts at LBA 63, holds boot + kernel. */
  115. {
  116. const unsigned char *e = mbr + MBR_PART_OFF;
  117. if (e[0] != 0x80 || e[4] != 0x06 ||
  118. get32(e + 8) != BOOT_LBA || get32(e + 12) < 1024u)
  119. return 0;
  120. }
  121. if (blk_read(d, BOOT_LBA, boot, 1) != 0)
  122. return 0;
  123. if (boot[510] != 0x55 || boot[511] != 0xAA)
  124. return 0;
  125. if (boot[3] != 'N' || boot[4] != 'E' || boot[5] != 'B' ||
  126. boot[6] != 'O' || boot[7] != 'S')
  127. return 0;
  128. if (get32(boot + PACK_LBA_OFF) != ROOTFS_LBA)
  129. return 0;
  130. if (get16(boot + KERNEL_LBA_OFF) != BOOT_LBA + 1u)
  131. return 0;
  132. if (get16(boot + KERNEL_SEC_OFF) == 0)
  133. return 0;
  134. /* Used length: the end of the highest non-empty partition. Require
  135. * the rootfs area to be covered, and reject an entry that claims more
  136. * sectors than the source has. */
  137. for (i = 0; i < 4; i++) {
  138. const unsigned char *e = mbr + MBR_PART_OFF + i * MBR_PART_SIZE;
  139. unsigned int start, count, end;
  140. if (e[4] == 0xEF)
  141. esp = 1;
  142. if (e[4] == 0)
  143. continue;
  144. start = get32(e + 8);
  145. count = get32(e + 12);
  146. if (!count || start > d->total_sectors ||
  147. count > d->total_sectors - start)
  148. return 0;
  149. end = start + count;
  150. if (end > max_end)
  151. max_end = end;
  152. }
  153. if (max_end < ROOTFS_LBA + 512u)
  154. return 0;
  155. if (used)
  156. *used = max_end;
  157. if (has_esp)
  158. *has_esp = esp;
  159. return 1;
  160. }
  161. static blk_dev_t *find_source(blk_dev_t *target, const char *name,
  162. unsigned int *used)
  163. {
  164. blk_dev_t *usb = 0, *other = 0;
  165. unsigned int usb_used = 0, other_used = 0, u;
  166. int esp;
  167. if (name) {
  168. blk_dev_t *d = blk_find(name);
  169. if (!d || !d->present) {
  170. io_putstr("install: source '");
  171. io_putstr(name);
  172. io_putstr("' is not present\n");
  173. return 0;
  174. }
  175. if (d == target) {
  176. io_putstr("install: source and target are the same device\n");
  177. return 0;
  178. }
  179. if (!check_nebos_image(d, &u, &esp, 0)) {
  180. io_putstr("install: '");
  181. io_putstr(name);
  182. io_putstr("' does not contain a NebOS image\n");
  183. return 0;
  184. }
  185. *used = u;
  186. return d;
  187. }
  188. for (u = 0; u < blk_dev_count; u++) {
  189. blk_dev_t *d = &blk_devs[u];
  190. unsigned int n;
  191. if (d == target || !d->present || d->kind == BLK_FLOPPY)
  192. continue;
  193. if (!check_nebos_image(d, &n, &esp, 0))
  194. continue;
  195. if (d->kind == BLK_USB) {
  196. if (!usb) {
  197. usb = d;
  198. usb_used = n;
  199. }
  200. } else if (!other) {
  201. other = d;
  202. other_used = n;
  203. }
  204. }
  205. if (usb) {
  206. *used = usb_used;
  207. return usb;
  208. }
  209. if (other) {
  210. *used = other_used;
  211. return other;
  212. }
  213. return 0;
  214. }
  215. static unsigned int sum_bytes(unsigned int sum, const unsigned char *p,
  216. unsigned int n)
  217. {
  218. while (n--)
  219. sum += *p++;
  220. return sum;
  221. }
  222. static void install_fail(const char *msg, unsigned int lba)
  223. {
  224. io_putstr("install: ");
  225. io_putstr(msg);
  226. if (lba) {
  227. io_putstr(" at lba ");
  228. format_putdec(lba);
  229. }
  230. io_putchar('\n');
  231. serial_puts(SERIAL_COM1, "install: failed at lba ");
  232. serial_putdec(SERIAL_COM1, lba);
  233. serial_puts(SERIAL_COM1, "\n");
  234. }
  235. void program_install_main(const char *arg)
  236. {
  237. char target_name[INSTALL_NAME_MAX], source_name[INSTALL_NAME_MAX];
  238. char source_dev[INSTALL_NAME_MAX];
  239. blk_dev_t *target, *source;
  240. blk_dev_t src_copy, dst_copy;
  241. unsigned char *image = 0;
  242. unsigned int image_pages = 0;
  243. int have_copies = 0;
  244. unsigned int used, i, j, lba, sum, vsum;
  245. int has_esp = 0, explicit_source = 0;
  246. int k;
  247. if (format_busy && process_is_alive(format_owner_pid)) {
  248. io_putstr("install: another format/install is in progress\n");
  249. return;
  250. }
  251. format_busy = 0;
  252. if (!arg) {
  253. io_putstr("install: usage install <target> [source]\n");
  254. list_devices();
  255. return;
  256. }
  257. /* Target = first word, optional source = second word. */
  258. i = 0;
  259. while (arg[i] == ' ')
  260. i++;
  261. j = 0;
  262. while (arg[i] && arg[i] != ' ' && j < sizeof(target_name) - 1)
  263. target_name[j++] = arg[i++];
  264. target_name[j] = '\0';
  265. while (arg[i] == ' ')
  266. i++;
  267. j = 0;
  268. while (arg[i] && arg[i] != ' ' && j < sizeof(source_name) - 1)
  269. source_name[j++] = arg[i++];
  270. source_name[j] = '\0';
  271. explicit_source = (source_name[0] != '\0');
  272. while (arg[i] == ' ')
  273. i++;
  274. if (!target_name[0] || arg[i]) {
  275. io_putstr("install: usage install <target> [source]\n");
  276. return;
  277. }
  278. target = blk_find(target_name);
  279. if (!target) {
  280. io_putstr("install: unknown target '");
  281. io_putstr(target_name);
  282. io_putstr("'\n");
  283. list_devices();
  284. return;
  285. }
  286. if (target->kind == BLK_FLOPPY) {
  287. io_putstr("install: a floppy is too small for the image\n");
  288. return;
  289. }
  290. if (!target->present || target->total_sectors == 0) {
  291. io_putstr("install: target ");
  292. io_putstr(target->name);
  293. io_putstr(" is not present\n");
  294. return;
  295. }
  296. source = find_source(target, explicit_source ? source_name : 0, &used);
  297. if (!source) {
  298. if (!explicit_source) {
  299. io_putstr("install: no NebOS install source found\n");
  300. io_putstr("install: the USB stick must be visible as a block device\n");
  301. list_devices();
  302. }
  303. return;
  304. }
  305. /* Recompute the flags for the chosen source (find_source only
  306. * returned the used length). */
  307. check_nebos_image(source, &used, &has_esp, 0);
  308. if (used > target->total_sectors) {
  309. io_putstr("install: source image (");
  310. put_size(used);
  311. io_putstr(") does not fit on ");
  312. io_putstr(target->name);
  313. io_putstr(" (");
  314. put_size(target->total_sectors);
  315. io_putstr(")\n");
  316. return;
  317. }
  318. io_putstr("install: source ");
  319. io_putstr(source->name);
  320. if (source->model[0]) {
  321. io_putstr(" (");
  322. io_putstr(source->model);
  323. io_putchar(')');
  324. }
  325. io_putstr(", ");
  326. put_size(used);
  327. io_putstr(", ");
  328. io_putstr(has_esp ? "BIOS + UEFI" : "BIOS only");
  329. io_putchar('\n');
  330. io_putstr("install: target ");
  331. io_putstr(target->name);
  332. if (target->model[0]) {
  333. io_putstr(" (");
  334. io_putstr(target->model);
  335. io_putchar(')');
  336. }
  337. io_putstr(", ");
  338. put_size(target->total_sectors);
  339. io_putchar('\n');
  340. for (i = 0; i + 1 < sizeof(source_dev) && source->name[i]; i++)
  341. source_dev[i] = source->name[i];
  342. source_dev[i] = '\0';
  343. io_putstr("install: this destroys all data on ");
  344. io_putstr(target->name);
  345. io_putstr("? (y/n) ");
  346. vga_fb_flush();
  347. /* The confirmation loop yields so the kworker can poll the USB HID
  348. * keyboard. The copy then releases the BKL per chunk; do not run
  349. * lsusb (a bus rescan) while an install is in progress. */
  350. io_set_raw_keyboard(1);
  351. for (;;) {
  352. k = io_poll_key_with_hw();
  353. if (k == 'y' || k == 'Y')
  354. break;
  355. if (k == 'n' || k == 'N') {
  356. io_set_raw_keyboard(0);
  357. io_putstr("aborted\n");
  358. return;
  359. }
  360. process_sleep_ms(10);
  361. }
  362. io_set_raw_keyboard(0);
  363. io_putchar('\n');
  364. /* The prompt yielded; another command may have rescanned the block
  365. * devices or started a format. Re-find both by name and take a
  366. * private copy of the source MBR. */
  367. if (format_busy && process_is_alive(format_owner_pid)) {
  368. io_putstr("install: another format/install is in progress\n");
  369. return;
  370. }
  371. format_owner_pid = current_proc()->pid;
  372. format_busy = 1;
  373. target = blk_find(target_name);
  374. source = blk_find(source_dev);
  375. if (!target || !source || source == target ||
  376. !target->present || !source->present) {
  377. io_putstr("install: device set changed; aborted\n");
  378. goto fail;
  379. }
  380. src_copy = *source;
  381. dst_copy = *target;
  382. have_copies = 1;
  383. if (used > dst_copy.total_sectors) {
  384. io_putstr("install: source no longer fits; aborted\n");
  385. goto fail;
  386. }
  387. if (!check_nebos_image(&src_copy, &used, &has_esp, sector0)) {
  388. io_putstr("install: source changed; aborted\n");
  389. goto fail;
  390. }
  391. if (used > dst_copy.total_sectors) {
  392. io_putstr("install: source no longer fits; aborted\n");
  393. goto fail;
  394. }
  395. serial_puts(SERIAL_COM1, "install: ");
  396. serial_puts(SERIAL_COM1, src_copy.name);
  397. serial_puts(SERIAL_COM1, " -> ");
  398. serial_puts(SERIAL_COM1, dst_copy.name);
  399. serial_puts(SERIAL_COM1, ", ");
  400. serial_putdec(SERIAL_COM1, used);
  401. serial_puts(SERIAL_COM1, " sectors\n");
  402. /* Read the whole image into RAM first. Interleaving USB source reads
  403. * with the slow eMMC target writes lets the USB3 link go idle between
  404. * chunks; some sticks then fail the next READ(10) (the eMMC target is
  405. * still written from RAM afterwards). */
  406. image_pages = (used * 512u + 4095u) / 4096u;
  407. image = (unsigned char *)mm_alloc_pages(image_pages);
  408. if (!image) {
  409. io_putstr("install: out of memory for the source image\n");
  410. goto fail;
  411. }
  412. for (i = 0; i < sizeof(sector0); i++)
  413. image[i] = sector0[i];
  414. /* LBA 1..62 are the unused zero gap (partition 1 starts at 63). */
  415. for (i = sizeof(sector0); i < BOOT_LBA * 512u; i++)
  416. image[i] = 0;
  417. sum = sum_bytes(0, sector0, sizeof(sector0));
  418. io_putstr("install: reading source");
  419. vga_fb_flush();
  420. /* Keep the BKL here: the USB source must stay active across all
  421. * chunks. The target writes below release it per chunk. */
  422. for (lba = BOOT_LBA; lba < used; lba += INSTALL_CHUNK) {
  423. unsigned int n = used - lba;
  424. if (n > INSTALL_CHUNK)
  425. n = INSTALL_CHUNK;
  426. if (blk_read(&src_copy, lba, image + lba * 512u, n) != 0) {
  427. install_fail("source read failed", lba);
  428. goto fail;
  429. }
  430. sum = sum_bytes(sum, image + lba * 512u, n * 512u);
  431. if (lba >= INSTALL_DOT_LBA && lba % INSTALL_DOT_LBA < INSTALL_CHUNK)
  432. io_putchar('.');
  433. }
  434. io_putchar('\n');
  435. io_putstr("install: writing target");
  436. for (lba = 1; lba < used; lba += INSTALL_CHUNK) {
  437. unsigned int n = used - lba;
  438. if (n > INSTALL_CHUNK)
  439. n = INSTALL_CHUNK;
  440. if (write_chunk(&dst_copy, lba, image + lba * 512u, n) != 0) {
  441. install_fail("target write failed", lba);
  442. goto fail;
  443. }
  444. if (lba >= INSTALL_DOT_LBA && lba % INSTALL_DOT_LBA < INSTALL_CHUNK)
  445. io_putchar('.');
  446. }
  447. /* MBR last: an interrupted copy is not bootable. */
  448. if (write_chunk(&dst_copy, 0, image, 1) != 0) {
  449. install_fail("target MBR write failed", 0);
  450. goto fail;
  451. }
  452. io_putchar('\n');
  453. {
  454. int fr;
  455. bkl_pause();
  456. fr = blk_flush(&dst_copy);
  457. bkl_resume();
  458. if (fr != 0)
  459. io_putstr("install: cache flush failed; verifying anyway\n");
  460. }
  461. io_putstr("install: verifying...\n");
  462. vsum = 0;
  463. for (lba = 0; lba < used; lba += INSTALL_CHUNK) {
  464. unsigned int n = used - lba;
  465. if (n > INSTALL_CHUNK)
  466. n = INSTALL_CHUNK;
  467. if (read_chunk(&dst_copy, lba, copy_buf, n) != 0) {
  468. install_fail("target read-back failed", lba);
  469. goto fail;
  470. }
  471. vsum = sum_bytes(vsum, copy_buf, n * 512u);
  472. }
  473. if (vsum != sum) {
  474. install_fail("verification mismatch", 0);
  475. goto fail;
  476. }
  477. io_putstr("install: verified ");
  478. format_putdec(used);
  479. io_putstr(" sectors\n");
  480. free_pages(image, image_pages);
  481. image = 0;
  482. vfs_remount_device(&dst_copy);
  483. format_busy = 0;
  484. io_putstr("install: done; remove the install medium and reboot.\n");
  485. serial_puts(SERIAL_COM1, "install: done\n");
  486. return;
  487. fail:
  488. if (image)
  489. free_pages(image, image_pages);
  490. if (have_copies)
  491. vfs_remount_device(&dst_copy);
  492. format_busy = 0;
  493. io_putstr("install: target is incomplete; do not boot it\n");
  494. serial_puts(SERIAL_COM1, "install: failed\n");
  495. }