install.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118
  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 "fatdetect.h"
  15. #include "fatformat.h"
  16. #include "install.h"
  17. #include "io.h"
  18. #include "mm.h"
  19. #include "process.h"
  20. #include "ramfs.h"
  21. #include "rootfs.h"
  22. #include "sched.h"
  23. #include "serial.h"
  24. #include "vga.h"
  25. #include "vfs.h"
  26. #define INSTALL_NAME_MAX 16
  27. #define INSTALL_CHUNK 32u /* 16 KiB copy buffer */
  28. #define INSTALL_DOT_LBA 2048u /* one progress dot per MiB */
  29. #define DATA_MIN_SECTORS 2048u /* /data needs at least 1 MiB */
  30. #define MBR_PART_OFF 0x1BE
  31. #define MBR_PART_SIZE 16
  32. #define PACK_LBA_OFF 0x42
  33. #define KERNEL_LBA_OFF 0x40
  34. #define KERNEL_SEC_OFF 0x3E
  35. #define ESP_TYPE 0xEFu
  36. static unsigned char sector0[512];
  37. static unsigned char copy_buf[INSTALL_CHUNK * 512];
  38. /* The copy and verify release the BKL around each chunk, like the format
  39. * raw-sector loop, so the WM and terminals keep running. format_busy keeps
  40. * disk-backed filesystem syscalls out; the device descriptors are copied
  41. * before the loop so a mount rescan cannot invalidate them. */
  42. static int read_chunk(blk_dev_t *d, unsigned int lba, void *buf,
  43. unsigned int n)
  44. {
  45. int r;
  46. bkl_pause();
  47. r = blk_read(d, lba, buf, n);
  48. bkl_resume();
  49. return r;
  50. }
  51. static int write_chunk(blk_dev_t *d, unsigned int lba, const void *buf,
  52. unsigned int n)
  53. {
  54. int r;
  55. bkl_pause();
  56. r = blk_write(d, lba, buf, n);
  57. bkl_resume();
  58. return r;
  59. }
  60. static void free_pages(unsigned char *p, unsigned int pages)
  61. {
  62. unsigned int i;
  63. if (!p)
  64. return;
  65. for (i = 0; i < pages; i++)
  66. mm_free_page((unsigned int)p + i * 4096u);
  67. }
  68. static unsigned int get16(const unsigned char *p)
  69. {
  70. return (unsigned int)p[0] | ((unsigned int)p[1] << 8);
  71. }
  72. static unsigned int get32(const unsigned char *p)
  73. {
  74. return (unsigned int)p[0] | ((unsigned int)p[1] << 8) |
  75. ((unsigned int)p[2] << 16) | ((unsigned int)p[3] << 24);
  76. }
  77. static void list_devices(void)
  78. {
  79. unsigned int i;
  80. io_putstr("devices:");
  81. for (i = 0; i < blk_dev_count; i++) {
  82. io_putchar(' ');
  83. io_putstr(blk_devs[i].name);
  84. }
  85. io_putchar('\n');
  86. }
  87. static void put_size(unsigned int sectors)
  88. {
  89. if (sectors % 2048u == 0) {
  90. format_putdec(sectors / 2048u);
  91. io_putstr(" MiB");
  92. } else {
  93. format_putdec(sectors);
  94. io_putstr(" sectors");
  95. }
  96. }
  97. /* Validate the NebOS image signature and return its used sector count.
  98. * The image layout is the one the Makefile builds: an MBR whose first
  99. * partition starts at LBA 63 and holds the boot sector at 63 + the kernel
  100. * at 64, and a boot sector whose pack_lba points at the rootfs image.
  101. * `mbr_out` (optional) receives the MBR read during validation. */
  102. static int check_nebos_image(blk_dev_t *d, unsigned int *used, int *has_esp,
  103. unsigned char *mbr_out)
  104. {
  105. unsigned char mbr_local[512], boot[512];
  106. unsigned char *mbr = mbr_out ? mbr_out : mbr_local;
  107. unsigned int i, max_end = 0;
  108. int esp = 0;
  109. if (!d || !d->present || d->kind == BLK_FLOPPY)
  110. return 0;
  111. if (d->total_sectors < ROOTFS_LBA + 512u)
  112. return 0;
  113. if (blk_read(d, 0, mbr, 1) != 0)
  114. return 0;
  115. if (mbr[510] != 0x55 || mbr[511] != 0xAA)
  116. return 0;
  117. /* Partition 1: active, starts at LBA 63, holds boot + kernel. */
  118. {
  119. const unsigned char *e = mbr + MBR_PART_OFF;
  120. if (e[0] != 0x80 || e[4] != 0x06 ||
  121. get32(e + 8) != NEBOS_BOOT_LBA ||
  122. get32(e + 12) < 1024u)
  123. return 0;
  124. }
  125. if (blk_read(d, NEBOS_BOOT_LBA, boot, 1) != 0)
  126. return 0;
  127. if (boot[510] != 0x55 || boot[511] != 0xAA)
  128. return 0;
  129. if (boot[3] != 'N' || boot[4] != 'E' || boot[5] != 'B' ||
  130. boot[6] != 'O' || boot[7] != 'S')
  131. return 0;
  132. if (get32(boot + PACK_LBA_OFF) != ROOTFS_LBA)
  133. return 0;
  134. if (get16(boot + KERNEL_LBA_OFF) != NEBOS_KERNEL_LBA)
  135. return 0;
  136. if (get16(boot + KERNEL_SEC_OFF) == 0)
  137. return 0;
  138. /* Used length: the end of the highest OS partition. Partition 4 is
  139. * the persistent data volume: it is not part of the cloned image and
  140. * may legitimately extend to the end of the disk. A source entry may
  141. * not cross into the data area, though. */
  142. for (i = 0; i < 4; i++) {
  143. const unsigned char *e = mbr + MBR_PART_OFF + i * MBR_PART_SIZE;
  144. unsigned int start, count, end;
  145. if (e[4] == 0xEF)
  146. esp = 1;
  147. if (e[4] == 0)
  148. continue;
  149. start = get32(e + 8);
  150. count = get32(e + 12);
  151. if (!count || start > d->total_sectors ||
  152. count > d->total_sectors - start)
  153. return 0;
  154. if (start >= NEBOS_DATA_LBA)
  155. continue;
  156. end = start + count;
  157. if (end > NEBOS_DATA_LBA)
  158. return 0;
  159. if (end > max_end)
  160. max_end = end;
  161. }
  162. if (max_end < ROOTFS_LBA + 512u)
  163. return 0;
  164. if (used)
  165. *used = max_end;
  166. if (has_esp)
  167. *has_esp = esp;
  168. return 1;
  169. }
  170. static blk_dev_t *find_source(blk_dev_t *target, const char *name,
  171. unsigned int *used)
  172. {
  173. blk_dev_t *usb = 0, *other = 0;
  174. unsigned int usb_used = 0, other_used = 0, u;
  175. int esp;
  176. if (name) {
  177. blk_dev_t *d = blk_find(name);
  178. if (!d || !d->present) {
  179. io_putstr("install: source '");
  180. io_putstr(name);
  181. io_putstr("' is not present\n");
  182. return 0;
  183. }
  184. if (d == target) {
  185. io_putstr("install: source and target are the same device\n");
  186. return 0;
  187. }
  188. if (!check_nebos_image(d, &u, &esp, 0)) {
  189. io_putstr("install: '");
  190. io_putstr(name);
  191. io_putstr("' does not contain a NebOS image\n");
  192. return 0;
  193. }
  194. *used = u;
  195. return d;
  196. }
  197. for (u = 0; u < blk_dev_count; u++) {
  198. blk_dev_t *d = &blk_devs[u];
  199. unsigned int n;
  200. if (d == target || !d->present || d->kind == BLK_FLOPPY)
  201. continue;
  202. if (!check_nebos_image(d, &n, &esp, 0))
  203. continue;
  204. if (d->kind == BLK_USB) {
  205. if (!usb) {
  206. usb = d;
  207. usb_used = n;
  208. }
  209. } else if (!other) {
  210. other = d;
  211. other_used = n;
  212. }
  213. }
  214. if (usb) {
  215. *used = usb_used;
  216. return usb;
  217. }
  218. if (other) {
  219. *used = other_used;
  220. return other;
  221. }
  222. return 0;
  223. }
  224. static unsigned int sum_bytes(unsigned int sum, const unsigned char *p,
  225. unsigned int n)
  226. {
  227. while (n--)
  228. sum += *p++;
  229. return sum;
  230. }
  231. static void install_fail(const char *msg, unsigned int lba)
  232. {
  233. io_putstr("install: ");
  234. io_putstr(msg);
  235. if (lba) {
  236. io_putstr(" at lba ");
  237. format_putdec(lba);
  238. }
  239. io_putchar('\n');
  240. serial_puts(SERIAL_COM1, "install: failed at lba ");
  241. serial_putdec(SERIAL_COM1, lba);
  242. serial_puts(SERIAL_COM1, "\n");
  243. }
  244. static void put16(unsigned char *p, unsigned int v)
  245. {
  246. p[0] = (unsigned char)v;
  247. p[1] = (unsigned char)(v >> 8);
  248. }
  249. static void put32(unsigned char *p, unsigned int v)
  250. {
  251. p[0] = (unsigned char)v;
  252. p[1] = (unsigned char)(v >> 8);
  253. p[2] = (unsigned char)(v >> 16);
  254. p[3] = (unsigned char)(v >> 24);
  255. }
  256. static void copy_bytes(unsigned char *dst, const unsigned char *src,
  257. unsigned int n)
  258. {
  259. unsigned int i;
  260. for (i = 0; i < n; i++)
  261. dst[i] = src[i];
  262. }
  263. static void zero_bytes(unsigned char *dst, unsigned int n)
  264. {
  265. unsigned int i;
  266. for (i = 0; i < n; i++)
  267. dst[i] = 0;
  268. }
  269. static int str_eq(const char *a, const char *b)
  270. {
  271. unsigned int i = 0;
  272. while (a[i] && b[i] && a[i] == b[i])
  273. i++;
  274. return a[i] == '\0' && b[i] == '\0';
  275. }
  276. /* The persistent data volume is recognised by its fixed LBA and label. */
  277. static int detect_data_partition(blk_dev_t *d, const unsigned char *mbr,
  278. unsigned char *entry)
  279. {
  280. const unsigned char *e = mbr + MBR_PART_OFF + 3 * MBR_PART_SIZE;
  281. unsigned int start, count;
  282. fat_fs_t fs;
  283. if (!mbr_is_fat_type(e[4]))
  284. return 0;
  285. start = get32(e + 8);
  286. count = get32(e + 12);
  287. if (start != NEBOS_DATA_LBA || count < DATA_MIN_SECTORS)
  288. return 0;
  289. if (start > d->total_sectors || count > d->total_sectors - start)
  290. return 0;
  291. if (fat_mount(d, start, &fs) != 0)
  292. return 0;
  293. if (!str_eq(fs.label, "NEBOSDATA"))
  294. return 0;
  295. if (entry)
  296. copy_bytes(entry, e, MBR_PART_SIZE);
  297. return 1;
  298. }
  299. /* Detect and, when absent, create the data partition. The caller owns the
  300. * target MBR buffer and writes it last. `created` says whether the entry is
  301. * new (and therefore whether the MBR must be rewritten). */
  302. static int ensure_data_partition(blk_dev_t *d, const unsigned char *mbr,
  303. unsigned char *entry, int *created)
  304. {
  305. unsigned int sectors, type = 0;
  306. *created = 0;
  307. if (detect_data_partition(d, mbr, entry)) {
  308. io_putstr("install: /data partition present; keeping it\n");
  309. return 1;
  310. }
  311. if (d->total_sectors <= NEBOS_DATA_LBA ||
  312. d->total_sectors - NEBOS_DATA_LBA < DATA_MIN_SECTORS) {
  313. io_putstr("install: target is too small for a /data partition\n");
  314. return 0;
  315. }
  316. sectors = d->total_sectors - NEBOS_DATA_LBA;
  317. io_putstr("install: creating /data partition (");
  318. put_size(sectors);
  319. io_putstr(")...\n");
  320. /* The installer's format path owns the disks through format_busy, and
  321. * the FAT loop releases the BKL per chunk. Dropping it entirely (as
  322. * `format` does) avoids the format_progress sleeps holding it. */
  323. bkl_unlock();
  324. if (fat_format_partition(d, NEBOS_DATA_LBA, sectors, "NEBOSDATA",
  325. &type) != 0) {
  326. bkl_lock();
  327. io_putstr("install: /data format failed\n");
  328. return 0;
  329. }
  330. bkl_lock();
  331. zero_bytes(entry, MBR_PART_SIZE);
  332. entry[1] = 0xFE;
  333. entry[2] = 0xFF;
  334. entry[3] = 0xFF;
  335. entry[4] = (type == 32) ? 0x0C : ((type == 16) ? 0x0E : 0x01);
  336. entry[5] = 0xFE;
  337. entry[6] = 0xFF;
  338. entry[7] = 0xFF;
  339. put32(entry + 8, NEBOS_DATA_LBA);
  340. put32(entry + 12, sectors);
  341. *created = 1;
  342. io_putstr("install: /data FAT");
  343. format_putdec(type);
  344. io_putstr(", ");
  345. put_size(sectors);
  346. io_putchar('\n');
  347. return 1;
  348. }
  349. static blk_dev_t *resolve_target(const char *name)
  350. {
  351. blk_dev_t *found = 0;
  352. unsigned int i;
  353. if (!str_eq(name, "auto")) {
  354. found = blk_find(name);
  355. if (!found) {
  356. io_putstr("install: unknown target '");
  357. io_putstr(name);
  358. io_putstr("'\n");
  359. list_devices();
  360. }
  361. return found;
  362. }
  363. for (i = 0; i < blk_dev_count; i++) {
  364. blk_dev_t *d = &blk_devs[i];
  365. if (!d->present || d->kind == BLK_FLOPPY ||
  366. d->kind == BLK_USB)
  367. continue;
  368. if (!check_nebos_image(d, 0, 0, 0))
  369. continue;
  370. if (found) {
  371. io_putstr("install: auto found more than one NebOS disk; "
  372. "name the target explicitly\n");
  373. return 0;
  374. }
  375. found = d;
  376. }
  377. if (!found)
  378. io_putstr("install: auto found no installed NebOS disk\n");
  379. return found;
  380. }
  381. /* Set by `install -y ...` (the `disks` app confirms in its own dialog). */
  382. static int install_yes;
  383. static int ask_continue(blk_dev_t *target, int have_data)
  384. {
  385. int k;
  386. if (have_data) {
  387. io_putstr("install: this replaces the OS on ");
  388. io_putstr(target->name);
  389. io_putstr("; files on /data are kept. Continue? (y/n) ");
  390. } else {
  391. io_putstr("install: this destroys all data on ");
  392. io_putstr(target->name);
  393. io_putstr("? (y/n) ");
  394. }
  395. if (install_yes) {
  396. io_putstr("y\n");
  397. return 1;
  398. }
  399. vga_fb_flush();
  400. /* The confirmation loop yields so the kworker can poll the USB HID
  401. * keyboard. */
  402. io_set_raw_keyboard(1);
  403. for (;;) {
  404. k = io_poll_key_with_hw();
  405. if (k == 'y' || k == 'Y') {
  406. io_set_raw_keyboard(0);
  407. io_putchar('\n');
  408. return 1;
  409. }
  410. if (k == 'n' || k == 'N') {
  411. io_set_raw_keyboard(0);
  412. io_putstr("aborted\n");
  413. return 0;
  414. }
  415. process_sleep_ms(10);
  416. }
  417. }
  418. /* Write `bytes` from p at lba, padding the final partial sector with
  419. * zeros. Chunks release the BKL like the rest of the installer. */
  420. static int write_region(blk_dev_t *d, unsigned int lba,
  421. const unsigned char *p, unsigned int bytes,
  422. const char *what)
  423. {
  424. unsigned int full = bytes / 512u, off = 0, n;
  425. while (off < full) {
  426. n = full - off;
  427. if (n > INSTALL_CHUNK)
  428. n = INSTALL_CHUNK;
  429. if (write_chunk(d, lba + off, p + off * 512u, n) != 0) {
  430. install_fail(what, lba + off);
  431. return -1;
  432. }
  433. off += n;
  434. }
  435. if (bytes % 512u) {
  436. unsigned int rem = bytes % 512u, i;
  437. for (i = 0; i < rem; i++)
  438. copy_buf[i] = p[full * 512u + i];
  439. for (; i < 512u; i++)
  440. copy_buf[i] = 0;
  441. if (write_chunk(d, lba + full, copy_buf, 1) != 0) {
  442. install_fail(what, lba + full);
  443. return -1;
  444. }
  445. }
  446. return 0;
  447. }
  448. static int verify_region(blk_dev_t *d, unsigned int lba,
  449. const unsigned char *p, unsigned int bytes,
  450. const char *what)
  451. {
  452. unsigned int off = 0, sum = sum_bytes(0, p, bytes), vsum = 0, n;
  453. while (off + 512u <= bytes) {
  454. n = (bytes - off) / 512u;
  455. if (n > INSTALL_CHUNK)
  456. n = INSTALL_CHUNK;
  457. if (read_chunk(d, lba + off / 512u, copy_buf, n) != 0) {
  458. install_fail("target read-back failed",
  459. lba + off / 512u);
  460. return -1;
  461. }
  462. vsum = sum_bytes(vsum, copy_buf, n * 512u);
  463. off += n * 512u;
  464. }
  465. if (off < bytes) {
  466. if (read_chunk(d, lba + off / 512u, copy_buf, 1) != 0) {
  467. install_fail("target read-back failed",
  468. lba + off / 512u);
  469. return -1;
  470. }
  471. vsum = sum_bytes(vsum, copy_buf, bytes - off);
  472. }
  473. if (vsum != sum) {
  474. install_fail(what, 0);
  475. return -1;
  476. }
  477. return 0;
  478. }
  479. /* Shared P1/P2 writer for `install self` and the network install path. */
  480. static int write_os_image(blk_dev_t *d, const void *kernel,
  481. unsigned int ksize, const struct rootfs_hdr *hdr,
  482. const void *pack, unsigned int psize,
  483. int ensure_data)
  484. {
  485. unsigned char boot[512], mbr[512], data_entry[MBR_PART_SIZE];
  486. unsigned int used = 0, ksectors;
  487. int has_esp = 0, created = 0;
  488. if (!d || !d->present || d->kind == BLK_FLOPPY || !kernel || !ksize ||
  489. !hdr || !pack || !psize)
  490. return -1;
  491. if (hdr->version != ROOTFS_VERSION || hdr->pack_bytes != psize) {
  492. io_putstr("install: bad rootfs header\n");
  493. return -1;
  494. }
  495. if (hdr->flags & ~ROOTFS_FLAG_GZIP) {
  496. io_putstr("install: unsupported rootfs flags\n");
  497. return -1;
  498. }
  499. ksectors = (ksize + 511u) / 512u;
  500. if (ksectors == 0 ||
  501. ksectors > ROOTFS_LBA - NEBOS_KERNEL_LBA) {
  502. io_putstr("install: kernel does not fit partition 1\n");
  503. return -1;
  504. }
  505. if (psize + ROOTFS_HDR_SIZE > ROOTFS_SECTORS * 512u) {
  506. io_putstr("install: rootfs does not fit partition 2\n");
  507. return -1;
  508. }
  509. if (!check_nebos_image(d, &used, &has_esp, mbr)) {
  510. io_putstr("install: target is not an installed NebOS disk\n");
  511. io_putstr("install: run `install <target> <source>` from USB first\n");
  512. return -1;
  513. }
  514. if (blk_read(d, NEBOS_BOOT_LBA, boot, 1) != 0) {
  515. io_putstr("install: cannot read the target boot sector\n");
  516. return -1;
  517. }
  518. if (format_busy && process_is_alive(format_owner_pid)) {
  519. io_putstr("install: another format/install is in progress\n");
  520. return -1;
  521. }
  522. format_owner_pid = current_proc()->pid;
  523. format_busy = 1;
  524. /* Prepare the data partition before touching P1/P2: a format failure
  525. * then leaves the old OS bootable. */
  526. if (ensure_data &&
  527. !ensure_data_partition(d, mbr, data_entry, &created))
  528. goto fail;
  529. io_putstr("install: writing rootfs...\n");
  530. if (write_chunk(d, ROOTFS_LBA, hdr, 1) != 0 ||
  531. write_region(d, ROOTFS_LBA + 1u, (const unsigned char *)pack,
  532. psize, "rootfs write failed") != 0)
  533. goto fail;
  534. io_putstr("install: writing kernel...\n");
  535. if (write_region(d, NEBOS_KERNEL_LBA, (const unsigned char *)kernel,
  536. ksize, "kernel write failed") != 0)
  537. goto fail;
  538. /* The installed boot sector carries only the NebOS header fields. */
  539. zero_bytes(boot + 0x0B, 0x3E - 0x0B);
  540. put16(boot + KERNEL_SEC_OFF, ksectors);
  541. if (write_chunk(d, NEBOS_BOOT_LBA, boot, 1) != 0) {
  542. install_fail("boot sector write failed", NEBOS_BOOT_LBA);
  543. goto fail;
  544. }
  545. if (ensure_data && created) {
  546. copy_bytes(mbr + MBR_PART_OFF + 3 * MBR_PART_SIZE,
  547. data_entry, MBR_PART_SIZE);
  548. if (write_chunk(d, 0, mbr, 1) != 0) {
  549. install_fail("MBR write failed", 0);
  550. goto fail;
  551. }
  552. }
  553. {
  554. int fr;
  555. bkl_pause();
  556. fr = blk_flush(d);
  557. bkl_resume();
  558. if (fr != 0)
  559. io_putstr("install: cache flush failed; verifying anyway\n");
  560. }
  561. io_putstr("install: verifying...\n");
  562. if (verify_region(d, ROOTFS_LBA, (const unsigned char *)hdr,
  563. ROOTFS_HDR_SIZE, "rootfs header verify failed") != 0 ||
  564. verify_region(d, ROOTFS_LBA + 1u, (const unsigned char *)pack,
  565. psize, "rootfs verify failed") != 0 ||
  566. verify_region(d, NEBOS_KERNEL_LBA, (const unsigned char *)kernel,
  567. ksize, "kernel verify failed") != 0)
  568. goto fail;
  569. if (read_chunk(d, NEBOS_BOOT_LBA, copy_buf, 1) != 0) {
  570. install_fail("boot sector read-back failed", NEBOS_BOOT_LBA);
  571. goto fail;
  572. }
  573. {
  574. unsigned int i;
  575. for (i = 0; i < 512u; i++) {
  576. if (copy_buf[i] != boot[i]) {
  577. install_fail("boot sector verify failed",
  578. NEBOS_BOOT_LBA);
  579. goto fail;
  580. }
  581. }
  582. }
  583. if (created) {
  584. if (read_chunk(d, 0, copy_buf, 1) != 0) {
  585. install_fail("MBR read-back failed", 0);
  586. goto fail;
  587. }
  588. {
  589. unsigned int i;
  590. for (i = 0; i < 512u; i++) {
  591. if (copy_buf[i] != mbr[i]) {
  592. install_fail("MBR verify failed", 0);
  593. goto fail;
  594. }
  595. }
  596. }
  597. }
  598. if (created)
  599. vfs_remount_device(d);
  600. format_busy = 0;
  601. io_putstr("install: verified; ");
  602. put_size(used);
  603. io_putstr(" image written to ");
  604. io_putstr(d->name);
  605. if (ensure_data)
  606. io_putstr(", /data kept");
  607. io_putchar('\n');
  608. serial_puts(SERIAL_COM1, "install: image written\n");
  609. return 0;
  610. fail:
  611. format_busy = 0;
  612. io_putstr("install: image update failed\n");
  613. serial_puts(SERIAL_COM1, "install: image update failed\n");
  614. return -1;
  615. }
  616. static int install_self(const char *target_name)
  617. {
  618. struct vfs_file kf;
  619. struct vfs_mount *km = 0;
  620. const void *pack = 0;
  621. unsigned int psize = 0;
  622. int gz = 0, r;
  623. struct rootfs_hdr hdr;
  624. blk_dev_t *target;
  625. unsigned char mbr[512];
  626. int have_data = 0;
  627. if (vfs_load_file("/ramfs/install/kernel.bin", &kf, &km) != 0) {
  628. io_putstr("install: /ramfs/install/kernel.bin is missing\n");
  629. io_putstr("install: the floppy/lite pack has no running kernel image\n");
  630. return -1;
  631. }
  632. if (!ramfs_boot_image(&pack, &psize, &gz) || !pack || !psize) {
  633. io_putstr("install: no boot-time rootfs image is retained\n");
  634. vfs_release_file(km, &kf);
  635. return -1;
  636. }
  637. if (!gz) {
  638. io_putstr("install: the running pack was replaced at runtime (raw)\n");
  639. io_putstr("install: use `make net-install` to persist it\n");
  640. vfs_release_file(km, &kf);
  641. return -1;
  642. }
  643. if (psize + ROOTFS_HDR_SIZE > ROOTFS_SECTORS * 512u) {
  644. io_putstr("install: running rootfs is too large for partition 2\n");
  645. vfs_release_file(km, &kf);
  646. return -1;
  647. }
  648. target = resolve_target(target_name);
  649. if (!target) {
  650. vfs_release_file(km, &kf);
  651. return -1;
  652. }
  653. if (!target->present || target->kind == BLK_FLOPPY ||
  654. target->total_sectors == 0) {
  655. io_putstr("install: target is not usable\n");
  656. vfs_release_file(km, &kf);
  657. return -1;
  658. }
  659. if (!check_nebos_image(target, 0, 0, mbr)) {
  660. io_putstr("install: target is not an installed NebOS disk\n");
  661. io_putstr("install: run `install <target> <source>` from USB first\n");
  662. vfs_release_file(km, &kf);
  663. return -1;
  664. }
  665. have_data = detect_data_partition(target, mbr, 0);
  666. io_putstr("install: source running image, kernel ");
  667. put_size((kf.size + 511u) / 512u);
  668. io_putstr(", rootfs ");
  669. put_size((psize + 511u) / 512u);
  670. io_putstr(gz ? " gzip" : " raw");
  671. io_putchar('\n');
  672. io_putstr("install: target ");
  673. io_putstr(target->name);
  674. if (target->model[0]) {
  675. io_putstr(" (");
  676. io_putstr(target->model);
  677. io_putchar(')');
  678. }
  679. io_putchar('\n');
  680. if (!ask_continue(target, have_data)) {
  681. vfs_release_file(km, &kf);
  682. return -1;
  683. }
  684. zero_bytes((unsigned char *)&hdr, sizeof(hdr));
  685. copy_bytes((unsigned char *)hdr.magic, (const unsigned char *)ROOTFS_MAGIC,
  686. 7);
  687. hdr.version = ROOTFS_VERSION;
  688. hdr.pack_bytes = psize;
  689. hdr.pack_csum = sum_bytes(0, (const unsigned char *)pack, psize);
  690. hdr.flags = ROOTFS_FLAG_GZIP;
  691. r = write_os_image(target, kf.data, kf.size, &hdr, pack, psize, 1);
  692. vfs_release_file(km, &kf);
  693. if (r == 0) {
  694. io_putstr("install: done; /data kept or created.\n");
  695. serial_puts(SERIAL_COM1, "install: self done\n");
  696. }
  697. return r;
  698. }
  699. int install_ram_image(const char *target, const void *kernel,
  700. unsigned int ksize, const void *rootfs,
  701. unsigned int rsize)
  702. {
  703. const struct rootfs_hdr *hdr;
  704. const unsigned char *pack;
  705. unsigned int psize;
  706. blk_dev_t *d;
  707. if (!rootfs || rsize < ROOTFS_HDR_SIZE) {
  708. io_putstr("install: rootfs image too short\n");
  709. return -1;
  710. }
  711. hdr = (const struct rootfs_hdr *)rootfs;
  712. if (hdr->magic[0] != 'N' || hdr->magic[1] != 'B' ||
  713. hdr->magic[2] != 'R' || hdr->magic[3] != 'O' ||
  714. hdr->magic[4] != 'O' || hdr->magic[5] != 'T' ||
  715. hdr->magic[6] != '1' || hdr->version != ROOTFS_VERSION) {
  716. io_putstr("install: bad rootfs image\n");
  717. return -1;
  718. }
  719. psize = hdr->pack_bytes;
  720. if (!psize || psize > rsize - ROOTFS_HDR_SIZE ||
  721. !(hdr->flags & ROOTFS_FLAG_GZIP)) {
  722. io_putstr("install: bad rootfs length or encoding\n");
  723. return -1;
  724. }
  725. d = resolve_target(target);
  726. if (!d) {
  727. io_putstr("install: no network install target\n");
  728. return -1;
  729. }
  730. pack = (const unsigned char *)rootfs + ROOTFS_HDR_SIZE;
  731. io_putstr("install: net update to ");
  732. io_putstr(d->name);
  733. io_putstr(" (kernel ");
  734. put_size((ksize + 511u) / 512u);
  735. io_putstr(", rootfs ");
  736. put_size((psize + 511u) / 512u);
  737. io_putstr(")\n");
  738. return write_os_image(d, kernel, ksize, hdr, pack, psize, 0);
  739. }
  740. void program_install_main(const char *arg)
  741. {
  742. char target_name[INSTALL_NAME_MAX], source_name[INSTALL_NAME_MAX];
  743. char source_dev[INSTALL_NAME_MAX], target_dev[INSTALL_NAME_MAX];
  744. blk_dev_t *target, *source;
  745. blk_dev_t src_copy, dst_copy;
  746. unsigned char *image = 0;
  747. unsigned char target_mbr[512], data_entry[MBR_PART_SIZE];
  748. unsigned int image_pages = 0;
  749. int have_copies = 0;
  750. unsigned int used, i, j, lba, sum, vsum;
  751. int has_esp = 0, explicit_source = 0;
  752. int data_created = 0;
  753. if (format_busy && process_is_alive(format_owner_pid)) {
  754. io_putstr("install: another format/install is in progress\n");
  755. return;
  756. }
  757. format_busy = 0;
  758. if (!arg) {
  759. io_putstr("install: usage install <target> [source|self]\n");
  760. list_devices();
  761. return;
  762. }
  763. /* Target = first word, optional source = second word. */
  764. i = 0;
  765. while (arg[i] == ' ')
  766. i++;
  767. install_yes = 0;
  768. if (arg[i] == '-' && arg[i + 1] == 'y' && arg[i + 2] == ' ') {
  769. install_yes = 1;
  770. i += 3;
  771. while (arg[i] == ' ')
  772. i++;
  773. }
  774. j = 0;
  775. while (arg[i] && arg[i] != ' ' && j < sizeof(target_name) - 1)
  776. target_name[j++] = arg[i++];
  777. target_name[j] = '\0';
  778. while (arg[i] == ' ')
  779. i++;
  780. j = 0;
  781. while (arg[i] && arg[i] != ' ' && j < sizeof(source_name) - 1)
  782. source_name[j++] = arg[i++];
  783. source_name[j] = '\0';
  784. explicit_source = (source_name[0] != '\0');
  785. while (arg[i] == ' ')
  786. i++;
  787. if (!target_name[0] || arg[i]) {
  788. io_putstr("install: usage install <target> [source|self]\n");
  789. return;
  790. }
  791. if (str_eq(source_name, "self")) {
  792. install_self(target_name);
  793. return;
  794. }
  795. target = resolve_target(target_name);
  796. if (!target)
  797. return;
  798. if (target->kind == BLK_FLOPPY) {
  799. io_putstr("install: a floppy is too small for the image\n");
  800. return;
  801. }
  802. if (!target->present || target->total_sectors == 0) {
  803. io_putstr("install: target ");
  804. io_putstr(target->name);
  805. io_putstr(" is not present\n");
  806. return;
  807. }
  808. for (i = 0; i + 1 < sizeof(target_dev) && target->name[i]; i++)
  809. target_dev[i] = target->name[i];
  810. target_dev[i] = '\0';
  811. source = find_source(target, explicit_source ? source_name : 0, &used);
  812. if (!source) {
  813. if (!explicit_source) {
  814. io_putstr("install: no NebOS install source found\n");
  815. io_putstr("install: the USB stick must be visible as a block device\n");
  816. list_devices();
  817. }
  818. return;
  819. }
  820. /* Recompute the flags for the chosen source (find_source only
  821. * returned the used length). */
  822. check_nebos_image(source, &used, &has_esp, 0);
  823. if (used > target->total_sectors) {
  824. io_putstr("install: source image (");
  825. put_size(used);
  826. io_putstr(") does not fit on ");
  827. io_putstr(target->name);
  828. io_putstr(" (");
  829. put_size(target->total_sectors);
  830. io_putstr(")\n");
  831. return;
  832. }
  833. io_putstr("install: source ");
  834. io_putstr(source->name);
  835. if (source->model[0]) {
  836. io_putstr(" (");
  837. io_putstr(source->model);
  838. io_putchar(')');
  839. }
  840. io_putstr(", ");
  841. put_size(used);
  842. io_putstr(", ");
  843. io_putstr(has_esp ? "BIOS + UEFI" : "BIOS only");
  844. io_putchar('\n');
  845. io_putstr("install: target ");
  846. io_putstr(target->name);
  847. if (target->model[0]) {
  848. io_putstr(" (");
  849. io_putstr(target->model);
  850. io_putchar(')');
  851. }
  852. io_putstr(", ");
  853. put_size(target->total_sectors);
  854. io_putchar('\n');
  855. for (i = 0; i + 1 < sizeof(source_dev) && source->name[i]; i++)
  856. source_dev[i] = source->name[i];
  857. source_dev[i] = '\0';
  858. {
  859. int have_data = 0;
  860. if (blk_read(target, 0, target_mbr, 1) == 0)
  861. have_data = detect_data_partition(target, target_mbr, 0);
  862. if (have_data)
  863. io_putstr("install: /data partition found; keeping it\n");
  864. if (!ask_continue(target, have_data))
  865. return;
  866. }
  867. /* The prompt yielded; another command may have rescanned the block
  868. * devices or started a format. Re-find both by name and take a
  869. * private copy of the source MBR. */
  870. if (format_busy && process_is_alive(format_owner_pid)) {
  871. io_putstr("install: another format/install is in progress\n");
  872. return;
  873. }
  874. format_owner_pid = current_proc()->pid;
  875. format_busy = 1;
  876. target = blk_find(target_dev);
  877. source = blk_find(source_dev);
  878. if (!target || !source || source == target ||
  879. !target->present || !source->present) {
  880. io_putstr("install: device set changed; aborted\n");
  881. goto fail;
  882. }
  883. src_copy = *source;
  884. dst_copy = *target;
  885. have_copies = 1;
  886. if (used > dst_copy.total_sectors) {
  887. io_putstr("install: source no longer fits; aborted\n");
  888. goto fail;
  889. }
  890. if (!check_nebos_image(&src_copy, &used, &has_esp, sector0)) {
  891. io_putstr("install: source changed; aborted\n");
  892. goto fail;
  893. }
  894. if (used > dst_copy.total_sectors) {
  895. io_putstr("install: source no longer fits; aborted\n");
  896. goto fail;
  897. }
  898. serial_puts(SERIAL_COM1, "install: ");
  899. serial_puts(SERIAL_COM1, src_copy.name);
  900. serial_puts(SERIAL_COM1, " -> ");
  901. serial_puts(SERIAL_COM1, dst_copy.name);
  902. serial_puts(SERIAL_COM1, ", ");
  903. serial_putdec(SERIAL_COM1, used);
  904. serial_puts(SERIAL_COM1, " sectors\n");
  905. /* Read the whole image into RAM first. Interleaving USB source reads
  906. * with the slow eMMC target writes lets the USB3 link go idle between
  907. * chunks; some sticks then fail the next READ(10) (the eMMC target is
  908. * still written from RAM afterwards). */
  909. image_pages = (used * 512u + 4095u) / 4096u;
  910. image = (unsigned char *)mm_alloc_pages(image_pages);
  911. if (!image) {
  912. io_putstr("install: out of memory for the source image\n");
  913. goto fail;
  914. }
  915. for (i = 0; i < sizeof(sector0); i++)
  916. image[i] = sector0[i];
  917. /* LBA 1..62 are the unused zero gap (partition 1 starts at 63). */
  918. for (i = sizeof(sector0); i < NEBOS_BOOT_LBA * 512u; i++)
  919. image[i] = 0;
  920. sum = 0;
  921. io_putstr("install: reading source");
  922. vga_fb_flush();
  923. /* Keep the BKL here: the USB source must stay active across all
  924. * chunks. The target writes below release it per chunk. */
  925. for (lba = NEBOS_BOOT_LBA; lba < used; lba += INSTALL_CHUNK) {
  926. unsigned int n = used - lba;
  927. if (n > INSTALL_CHUNK)
  928. n = INSTALL_CHUNK;
  929. if (blk_read(&src_copy, lba, image + lba * 512u, n) != 0) {
  930. install_fail("source read failed", lba);
  931. goto fail;
  932. }
  933. sum = sum_bytes(sum, image + lba * 512u, n * 512u);
  934. if (lba >= INSTALL_DOT_LBA && lba % INSTALL_DOT_LBA < INSTALL_CHUNK)
  935. io_putchar('.');
  936. }
  937. io_putchar('\n');
  938. /* The data partition is prepared before the target is touched. It is
  939. * outside the cloned P1..P3 range; an existing one is left untouched. */
  940. if (blk_read(&dst_copy, 0, target_mbr, 1) != 0)
  941. zero_bytes(target_mbr, sizeof(target_mbr));
  942. if (!ensure_data_partition(&dst_copy, target_mbr, data_entry,
  943. &data_created))
  944. goto fail;
  945. copy_bytes(image, sector0, 512);
  946. copy_bytes(image + MBR_PART_OFF + 3 * MBR_PART_SIZE, data_entry,
  947. MBR_PART_SIZE);
  948. /* Include the final MBR (with the merged data entry) and the zero gap
  949. * in the checksum. `sum` currently covers BOOT_LBA..used. */
  950. sum = sum_bytes(0, image, NEBOS_BOOT_LBA * 512u) + sum;
  951. io_putstr("install: writing target");
  952. for (lba = 1; lba < used; lba += INSTALL_CHUNK) {
  953. unsigned int n = used - lba;
  954. if (n > INSTALL_CHUNK)
  955. n = INSTALL_CHUNK;
  956. if (write_chunk(&dst_copy, lba, image + lba * 512u, n) != 0) {
  957. install_fail("target write failed", lba);
  958. goto fail;
  959. }
  960. if (lba >= INSTALL_DOT_LBA && lba % INSTALL_DOT_LBA < INSTALL_CHUNK)
  961. io_putchar('.');
  962. }
  963. /* MBR last: an interrupted copy is not bootable. */
  964. if (write_chunk(&dst_copy, 0, image, 1) != 0) {
  965. install_fail("target MBR write failed", 0);
  966. goto fail;
  967. }
  968. io_putchar('\n');
  969. {
  970. int fr;
  971. bkl_pause();
  972. fr = blk_flush(&dst_copy);
  973. bkl_resume();
  974. if (fr != 0)
  975. io_putstr("install: cache flush failed; verifying anyway\n");
  976. }
  977. io_putstr("install: verifying...\n");
  978. vsum = 0;
  979. for (lba = 0; lba < used; lba += INSTALL_CHUNK) {
  980. unsigned int n = used - lba;
  981. if (n > INSTALL_CHUNK)
  982. n = INSTALL_CHUNK;
  983. if (read_chunk(&dst_copy, lba, copy_buf, n) != 0) {
  984. install_fail("target read-back failed", lba);
  985. goto fail;
  986. }
  987. vsum = sum_bytes(vsum, copy_buf, n * 512u);
  988. }
  989. if (vsum != sum) {
  990. install_fail("verification mismatch", 0);
  991. goto fail;
  992. }
  993. io_putstr("install: verified ");
  994. format_putdec(used);
  995. io_putstr(" sectors\n");
  996. free_pages(image, image_pages);
  997. image = 0;
  998. vfs_remount_device(&dst_copy);
  999. format_busy = 0;
  1000. io_putstr("install: done; remove the install medium and reboot.\n");
  1001. io_putstr(data_created ? "install: created /data on partition 4.\n"
  1002. : "install: kept the existing /data partition.\n");
  1003. serial_puts(SERIAL_COM1, "install: done\n");
  1004. return;
  1005. fail:
  1006. if (image)
  1007. free_pages(image, image_pages);
  1008. if (have_copies)
  1009. vfs_remount_device(&dst_copy);
  1010. format_busy = 0;
  1011. io_putstr("install: target is incomplete; do not boot it\n");
  1012. serial_puts(SERIAL_COM1, "install: failed\n");
  1013. }