install.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104
  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. static int ask_continue(blk_dev_t *target, int have_data)
  382. {
  383. int k;
  384. if (have_data) {
  385. io_putstr("install: this replaces the OS on ");
  386. io_putstr(target->name);
  387. io_putstr("; files on /data are kept. Continue? (y/n) ");
  388. } else {
  389. io_putstr("install: this destroys all data on ");
  390. io_putstr(target->name);
  391. io_putstr("? (y/n) ");
  392. }
  393. vga_fb_flush();
  394. /* The confirmation loop yields so the kworker can poll the USB HID
  395. * keyboard. */
  396. io_set_raw_keyboard(1);
  397. for (;;) {
  398. k = io_poll_key_with_hw();
  399. if (k == 'y' || k == 'Y') {
  400. io_set_raw_keyboard(0);
  401. io_putchar('\n');
  402. return 1;
  403. }
  404. if (k == 'n' || k == 'N') {
  405. io_set_raw_keyboard(0);
  406. io_putstr("aborted\n");
  407. return 0;
  408. }
  409. process_sleep_ms(10);
  410. }
  411. }
  412. /* Write `bytes` from p at lba, padding the final partial sector with
  413. * zeros. Chunks release the BKL like the rest of the installer. */
  414. static int write_region(blk_dev_t *d, unsigned int lba,
  415. const unsigned char *p, unsigned int bytes,
  416. const char *what)
  417. {
  418. unsigned int full = bytes / 512u, off = 0, n;
  419. while (off < full) {
  420. n = full - off;
  421. if (n > INSTALL_CHUNK)
  422. n = INSTALL_CHUNK;
  423. if (write_chunk(d, lba + off, p + off * 512u, n) != 0) {
  424. install_fail(what, lba + off);
  425. return -1;
  426. }
  427. off += n;
  428. }
  429. if (bytes % 512u) {
  430. unsigned int rem = bytes % 512u, i;
  431. for (i = 0; i < rem; i++)
  432. copy_buf[i] = p[full * 512u + i];
  433. for (; i < 512u; i++)
  434. copy_buf[i] = 0;
  435. if (write_chunk(d, lba + full, copy_buf, 1) != 0) {
  436. install_fail(what, lba + full);
  437. return -1;
  438. }
  439. }
  440. return 0;
  441. }
  442. static int verify_region(blk_dev_t *d, unsigned int lba,
  443. const unsigned char *p, unsigned int bytes,
  444. const char *what)
  445. {
  446. unsigned int off = 0, sum = sum_bytes(0, p, bytes), vsum = 0, n;
  447. while (off + 512u <= bytes) {
  448. n = (bytes - off) / 512u;
  449. if (n > INSTALL_CHUNK)
  450. n = INSTALL_CHUNK;
  451. if (read_chunk(d, lba + off / 512u, copy_buf, n) != 0) {
  452. install_fail("target read-back failed",
  453. lba + off / 512u);
  454. return -1;
  455. }
  456. vsum = sum_bytes(vsum, copy_buf, n * 512u);
  457. off += n * 512u;
  458. }
  459. if (off < bytes) {
  460. if (read_chunk(d, lba + off / 512u, copy_buf, 1) != 0) {
  461. install_fail("target read-back failed",
  462. lba + off / 512u);
  463. return -1;
  464. }
  465. vsum = sum_bytes(vsum, copy_buf, bytes - off);
  466. }
  467. if (vsum != sum) {
  468. install_fail(what, 0);
  469. return -1;
  470. }
  471. return 0;
  472. }
  473. /* Shared P1/P2 writer for `install self` and the network install path. */
  474. static int write_os_image(blk_dev_t *d, const void *kernel,
  475. unsigned int ksize, const struct rootfs_hdr *hdr,
  476. const void *pack, unsigned int psize,
  477. int ensure_data)
  478. {
  479. unsigned char boot[512], mbr[512], data_entry[MBR_PART_SIZE];
  480. unsigned int used = 0, ksectors;
  481. int has_esp = 0, created = 0;
  482. if (!d || !d->present || d->kind == BLK_FLOPPY || !kernel || !ksize ||
  483. !hdr || !pack || !psize)
  484. return -1;
  485. if (hdr->version != ROOTFS_VERSION || hdr->pack_bytes != psize) {
  486. io_putstr("install: bad rootfs header\n");
  487. return -1;
  488. }
  489. if (hdr->flags & ~ROOTFS_FLAG_GZIP) {
  490. io_putstr("install: unsupported rootfs flags\n");
  491. return -1;
  492. }
  493. ksectors = (ksize + 511u) / 512u;
  494. if (ksectors == 0 ||
  495. ksectors > ROOTFS_LBA - NEBOS_KERNEL_LBA) {
  496. io_putstr("install: kernel does not fit partition 1\n");
  497. return -1;
  498. }
  499. if (psize + ROOTFS_HDR_SIZE > ROOTFS_SECTORS * 512u) {
  500. io_putstr("install: rootfs does not fit partition 2\n");
  501. return -1;
  502. }
  503. if (!check_nebos_image(d, &used, &has_esp, mbr)) {
  504. io_putstr("install: target is not an installed NebOS disk\n");
  505. io_putstr("install: run `install <target> <source>` from USB first\n");
  506. return -1;
  507. }
  508. if (blk_read(d, NEBOS_BOOT_LBA, boot, 1) != 0) {
  509. io_putstr("install: cannot read the target boot sector\n");
  510. return -1;
  511. }
  512. if (format_busy && process_is_alive(format_owner_pid)) {
  513. io_putstr("install: another format/install is in progress\n");
  514. return -1;
  515. }
  516. format_owner_pid = current_proc()->pid;
  517. format_busy = 1;
  518. /* Prepare the data partition before touching P1/P2: a format failure
  519. * then leaves the old OS bootable. */
  520. if (ensure_data &&
  521. !ensure_data_partition(d, mbr, data_entry, &created))
  522. goto fail;
  523. io_putstr("install: writing rootfs...\n");
  524. if (write_chunk(d, ROOTFS_LBA, hdr, 1) != 0 ||
  525. write_region(d, ROOTFS_LBA + 1u, (const unsigned char *)pack,
  526. psize, "rootfs write failed") != 0)
  527. goto fail;
  528. io_putstr("install: writing kernel...\n");
  529. if (write_region(d, NEBOS_KERNEL_LBA, (const unsigned char *)kernel,
  530. ksize, "kernel write failed") != 0)
  531. goto fail;
  532. /* The installed boot sector carries only the NebOS header fields. */
  533. zero_bytes(boot + 0x0B, 0x3E - 0x0B);
  534. put16(boot + KERNEL_SEC_OFF, ksectors);
  535. if (write_chunk(d, NEBOS_BOOT_LBA, boot, 1) != 0) {
  536. install_fail("boot sector write failed", NEBOS_BOOT_LBA);
  537. goto fail;
  538. }
  539. if (ensure_data && created) {
  540. copy_bytes(mbr + MBR_PART_OFF + 3 * MBR_PART_SIZE,
  541. data_entry, MBR_PART_SIZE);
  542. if (write_chunk(d, 0, mbr, 1) != 0) {
  543. install_fail("MBR write failed", 0);
  544. goto fail;
  545. }
  546. }
  547. {
  548. int fr;
  549. bkl_pause();
  550. fr = blk_flush(d);
  551. bkl_resume();
  552. if (fr != 0)
  553. io_putstr("install: cache flush failed; verifying anyway\n");
  554. }
  555. io_putstr("install: verifying...\n");
  556. if (verify_region(d, ROOTFS_LBA, (const unsigned char *)hdr,
  557. ROOTFS_HDR_SIZE, "rootfs header verify failed") != 0 ||
  558. verify_region(d, ROOTFS_LBA + 1u, (const unsigned char *)pack,
  559. psize, "rootfs verify failed") != 0 ||
  560. verify_region(d, NEBOS_KERNEL_LBA, (const unsigned char *)kernel,
  561. ksize, "kernel verify failed") != 0)
  562. goto fail;
  563. if (read_chunk(d, NEBOS_BOOT_LBA, copy_buf, 1) != 0) {
  564. install_fail("boot sector read-back failed", NEBOS_BOOT_LBA);
  565. goto fail;
  566. }
  567. {
  568. unsigned int i;
  569. for (i = 0; i < 512u; i++) {
  570. if (copy_buf[i] != boot[i]) {
  571. install_fail("boot sector verify failed",
  572. NEBOS_BOOT_LBA);
  573. goto fail;
  574. }
  575. }
  576. }
  577. if (created) {
  578. if (read_chunk(d, 0, copy_buf, 1) != 0) {
  579. install_fail("MBR read-back failed", 0);
  580. goto fail;
  581. }
  582. {
  583. unsigned int i;
  584. for (i = 0; i < 512u; i++) {
  585. if (copy_buf[i] != mbr[i]) {
  586. install_fail("MBR verify failed", 0);
  587. goto fail;
  588. }
  589. }
  590. }
  591. }
  592. if (created)
  593. vfs_remount_device(d);
  594. format_busy = 0;
  595. io_putstr("install: verified; ");
  596. put_size(used);
  597. io_putstr(" image written to ");
  598. io_putstr(d->name);
  599. if (ensure_data)
  600. io_putstr(", /data kept");
  601. io_putchar('\n');
  602. serial_puts(SERIAL_COM1, "install: image written\n");
  603. return 0;
  604. fail:
  605. format_busy = 0;
  606. io_putstr("install: image update failed\n");
  607. serial_puts(SERIAL_COM1, "install: image update failed\n");
  608. return -1;
  609. }
  610. static int install_self(const char *target_name)
  611. {
  612. struct vfs_file kf;
  613. struct vfs_mount *km = 0;
  614. const void *pack = 0;
  615. unsigned int psize = 0;
  616. int gz = 0, r;
  617. struct rootfs_hdr hdr;
  618. blk_dev_t *target;
  619. unsigned char mbr[512];
  620. int have_data = 0;
  621. if (vfs_load_file("/ramfs/install/kernel.bin", &kf, &km) != 0) {
  622. io_putstr("install: /ramfs/install/kernel.bin is missing\n");
  623. io_putstr("install: the floppy/lite pack has no running kernel image\n");
  624. return -1;
  625. }
  626. if (!ramfs_boot_image(&pack, &psize, &gz) || !pack || !psize) {
  627. io_putstr("install: no boot-time rootfs image is retained\n");
  628. vfs_release_file(km, &kf);
  629. return -1;
  630. }
  631. if (!gz) {
  632. io_putstr("install: the running pack was replaced at runtime (raw)\n");
  633. io_putstr("install: use `make net-install` to persist it\n");
  634. vfs_release_file(km, &kf);
  635. return -1;
  636. }
  637. if (psize + ROOTFS_HDR_SIZE > ROOTFS_SECTORS * 512u) {
  638. io_putstr("install: running rootfs is too large for partition 2\n");
  639. vfs_release_file(km, &kf);
  640. return -1;
  641. }
  642. target = resolve_target(target_name);
  643. if (!target) {
  644. vfs_release_file(km, &kf);
  645. return -1;
  646. }
  647. if (!target->present || target->kind == BLK_FLOPPY ||
  648. target->total_sectors == 0) {
  649. io_putstr("install: target is not usable\n");
  650. vfs_release_file(km, &kf);
  651. return -1;
  652. }
  653. if (!check_nebos_image(target, 0, 0, mbr)) {
  654. io_putstr("install: target is not an installed NebOS disk\n");
  655. io_putstr("install: run `install <target> <source>` from USB first\n");
  656. vfs_release_file(km, &kf);
  657. return -1;
  658. }
  659. have_data = detect_data_partition(target, mbr, 0);
  660. io_putstr("install: source running image, kernel ");
  661. put_size((kf.size + 511u) / 512u);
  662. io_putstr(", rootfs ");
  663. put_size((psize + 511u) / 512u);
  664. io_putstr(gz ? " gzip" : " raw");
  665. io_putchar('\n');
  666. io_putstr("install: target ");
  667. io_putstr(target->name);
  668. if (target->model[0]) {
  669. io_putstr(" (");
  670. io_putstr(target->model);
  671. io_putchar(')');
  672. }
  673. io_putchar('\n');
  674. if (!ask_continue(target, have_data)) {
  675. vfs_release_file(km, &kf);
  676. return -1;
  677. }
  678. zero_bytes((unsigned char *)&hdr, sizeof(hdr));
  679. copy_bytes((unsigned char *)hdr.magic, (const unsigned char *)ROOTFS_MAGIC,
  680. 7);
  681. hdr.version = ROOTFS_VERSION;
  682. hdr.pack_bytes = psize;
  683. hdr.pack_csum = sum_bytes(0, (const unsigned char *)pack, psize);
  684. hdr.flags = ROOTFS_FLAG_GZIP;
  685. r = write_os_image(target, kf.data, kf.size, &hdr, pack, psize, 1);
  686. vfs_release_file(km, &kf);
  687. if (r == 0) {
  688. io_putstr("install: done; /data kept or created.\n");
  689. serial_puts(SERIAL_COM1, "install: self done\n");
  690. }
  691. return r;
  692. }
  693. int install_ram_image(const char *target, const void *kernel,
  694. unsigned int ksize, const void *rootfs,
  695. unsigned int rsize)
  696. {
  697. const struct rootfs_hdr *hdr;
  698. const unsigned char *pack;
  699. unsigned int psize;
  700. blk_dev_t *d;
  701. if (!rootfs || rsize < ROOTFS_HDR_SIZE) {
  702. io_putstr("install: rootfs image too short\n");
  703. return -1;
  704. }
  705. hdr = (const struct rootfs_hdr *)rootfs;
  706. if (hdr->magic[0] != 'N' || hdr->magic[1] != 'B' ||
  707. hdr->magic[2] != 'R' || hdr->magic[3] != 'O' ||
  708. hdr->magic[4] != 'O' || hdr->magic[5] != 'T' ||
  709. hdr->magic[6] != '1' || hdr->version != ROOTFS_VERSION) {
  710. io_putstr("install: bad rootfs image\n");
  711. return -1;
  712. }
  713. psize = hdr->pack_bytes;
  714. if (!psize || psize > rsize - ROOTFS_HDR_SIZE ||
  715. !(hdr->flags & ROOTFS_FLAG_GZIP)) {
  716. io_putstr("install: bad rootfs length or encoding\n");
  717. return -1;
  718. }
  719. d = resolve_target(target);
  720. if (!d) {
  721. io_putstr("install: no network install target\n");
  722. return -1;
  723. }
  724. pack = (const unsigned char *)rootfs + ROOTFS_HDR_SIZE;
  725. io_putstr("install: net update to ");
  726. io_putstr(d->name);
  727. io_putstr(" (kernel ");
  728. put_size((ksize + 511u) / 512u);
  729. io_putstr(", rootfs ");
  730. put_size((psize + 511u) / 512u);
  731. io_putstr(")\n");
  732. return write_os_image(d, kernel, ksize, hdr, pack, psize, 0);
  733. }
  734. void program_install_main(const char *arg)
  735. {
  736. char target_name[INSTALL_NAME_MAX], source_name[INSTALL_NAME_MAX];
  737. char source_dev[INSTALL_NAME_MAX], target_dev[INSTALL_NAME_MAX];
  738. blk_dev_t *target, *source;
  739. blk_dev_t src_copy, dst_copy;
  740. unsigned char *image = 0;
  741. unsigned char target_mbr[512], data_entry[MBR_PART_SIZE];
  742. unsigned int image_pages = 0;
  743. int have_copies = 0;
  744. unsigned int used, i, j, lba, sum, vsum;
  745. int has_esp = 0, explicit_source = 0;
  746. int data_created = 0;
  747. if (format_busy && process_is_alive(format_owner_pid)) {
  748. io_putstr("install: another format/install is in progress\n");
  749. return;
  750. }
  751. format_busy = 0;
  752. if (!arg) {
  753. io_putstr("install: usage install <target> [source|self]\n");
  754. list_devices();
  755. return;
  756. }
  757. /* Target = first word, optional source = second word. */
  758. i = 0;
  759. while (arg[i] == ' ')
  760. i++;
  761. j = 0;
  762. while (arg[i] && arg[i] != ' ' && j < sizeof(target_name) - 1)
  763. target_name[j++] = arg[i++];
  764. target_name[j] = '\0';
  765. while (arg[i] == ' ')
  766. i++;
  767. j = 0;
  768. while (arg[i] && arg[i] != ' ' && j < sizeof(source_name) - 1)
  769. source_name[j++] = arg[i++];
  770. source_name[j] = '\0';
  771. explicit_source = (source_name[0] != '\0');
  772. while (arg[i] == ' ')
  773. i++;
  774. if (!target_name[0] || arg[i]) {
  775. io_putstr("install: usage install <target> [source|self]\n");
  776. return;
  777. }
  778. if (str_eq(source_name, "self")) {
  779. install_self(target_name);
  780. return;
  781. }
  782. target = resolve_target(target_name);
  783. if (!target)
  784. return;
  785. if (target->kind == BLK_FLOPPY) {
  786. io_putstr("install: a floppy is too small for the image\n");
  787. return;
  788. }
  789. if (!target->present || target->total_sectors == 0) {
  790. io_putstr("install: target ");
  791. io_putstr(target->name);
  792. io_putstr(" is not present\n");
  793. return;
  794. }
  795. for (i = 0; i + 1 < sizeof(target_dev) && target->name[i]; i++)
  796. target_dev[i] = target->name[i];
  797. target_dev[i] = '\0';
  798. source = find_source(target, explicit_source ? source_name : 0, &used);
  799. if (!source) {
  800. if (!explicit_source) {
  801. io_putstr("install: no NebOS install source found\n");
  802. io_putstr("install: the USB stick must be visible as a block device\n");
  803. list_devices();
  804. }
  805. return;
  806. }
  807. /* Recompute the flags for the chosen source (find_source only
  808. * returned the used length). */
  809. check_nebos_image(source, &used, &has_esp, 0);
  810. if (used > target->total_sectors) {
  811. io_putstr("install: source image (");
  812. put_size(used);
  813. io_putstr(") does not fit on ");
  814. io_putstr(target->name);
  815. io_putstr(" (");
  816. put_size(target->total_sectors);
  817. io_putstr(")\n");
  818. return;
  819. }
  820. io_putstr("install: source ");
  821. io_putstr(source->name);
  822. if (source->model[0]) {
  823. io_putstr(" (");
  824. io_putstr(source->model);
  825. io_putchar(')');
  826. }
  827. io_putstr(", ");
  828. put_size(used);
  829. io_putstr(", ");
  830. io_putstr(has_esp ? "BIOS + UEFI" : "BIOS only");
  831. io_putchar('\n');
  832. io_putstr("install: target ");
  833. io_putstr(target->name);
  834. if (target->model[0]) {
  835. io_putstr(" (");
  836. io_putstr(target->model);
  837. io_putchar(')');
  838. }
  839. io_putstr(", ");
  840. put_size(target->total_sectors);
  841. io_putchar('\n');
  842. for (i = 0; i + 1 < sizeof(source_dev) && source->name[i]; i++)
  843. source_dev[i] = source->name[i];
  844. source_dev[i] = '\0';
  845. {
  846. int have_data = 0;
  847. if (blk_read(target, 0, target_mbr, 1) == 0)
  848. have_data = detect_data_partition(target, target_mbr, 0);
  849. if (have_data)
  850. io_putstr("install: /data partition found; keeping it\n");
  851. if (!ask_continue(target, have_data))
  852. return;
  853. }
  854. /* The prompt yielded; another command may have rescanned the block
  855. * devices or started a format. Re-find both by name and take a
  856. * private copy of the source MBR. */
  857. if (format_busy && process_is_alive(format_owner_pid)) {
  858. io_putstr("install: another format/install is in progress\n");
  859. return;
  860. }
  861. format_owner_pid = current_proc()->pid;
  862. format_busy = 1;
  863. target = blk_find(target_dev);
  864. source = blk_find(source_dev);
  865. if (!target || !source || source == target ||
  866. !target->present || !source->present) {
  867. io_putstr("install: device set changed; aborted\n");
  868. goto fail;
  869. }
  870. src_copy = *source;
  871. dst_copy = *target;
  872. have_copies = 1;
  873. if (used > dst_copy.total_sectors) {
  874. io_putstr("install: source no longer fits; aborted\n");
  875. goto fail;
  876. }
  877. if (!check_nebos_image(&src_copy, &used, &has_esp, sector0)) {
  878. io_putstr("install: source changed; aborted\n");
  879. goto fail;
  880. }
  881. if (used > dst_copy.total_sectors) {
  882. io_putstr("install: source no longer fits; aborted\n");
  883. goto fail;
  884. }
  885. serial_puts(SERIAL_COM1, "install: ");
  886. serial_puts(SERIAL_COM1, src_copy.name);
  887. serial_puts(SERIAL_COM1, " -> ");
  888. serial_puts(SERIAL_COM1, dst_copy.name);
  889. serial_puts(SERIAL_COM1, ", ");
  890. serial_putdec(SERIAL_COM1, used);
  891. serial_puts(SERIAL_COM1, " sectors\n");
  892. /* Read the whole image into RAM first. Interleaving USB source reads
  893. * with the slow eMMC target writes lets the USB3 link go idle between
  894. * chunks; some sticks then fail the next READ(10) (the eMMC target is
  895. * still written from RAM afterwards). */
  896. image_pages = (used * 512u + 4095u) / 4096u;
  897. image = (unsigned char *)mm_alloc_pages(image_pages);
  898. if (!image) {
  899. io_putstr("install: out of memory for the source image\n");
  900. goto fail;
  901. }
  902. for (i = 0; i < sizeof(sector0); i++)
  903. image[i] = sector0[i];
  904. /* LBA 1..62 are the unused zero gap (partition 1 starts at 63). */
  905. for (i = sizeof(sector0); i < NEBOS_BOOT_LBA * 512u; i++)
  906. image[i] = 0;
  907. sum = 0;
  908. io_putstr("install: reading source");
  909. vga_fb_flush();
  910. /* Keep the BKL here: the USB source must stay active across all
  911. * chunks. The target writes below release it per chunk. */
  912. for (lba = NEBOS_BOOT_LBA; lba < used; lba += INSTALL_CHUNK) {
  913. unsigned int n = used - lba;
  914. if (n > INSTALL_CHUNK)
  915. n = INSTALL_CHUNK;
  916. if (blk_read(&src_copy, lba, image + lba * 512u, n) != 0) {
  917. install_fail("source read failed", lba);
  918. goto fail;
  919. }
  920. sum = sum_bytes(sum, image + lba * 512u, n * 512u);
  921. if (lba >= INSTALL_DOT_LBA && lba % INSTALL_DOT_LBA < INSTALL_CHUNK)
  922. io_putchar('.');
  923. }
  924. io_putchar('\n');
  925. /* The data partition is prepared before the target is touched. It is
  926. * outside the cloned P1..P3 range; an existing one is left untouched. */
  927. if (blk_read(&dst_copy, 0, target_mbr, 1) != 0)
  928. zero_bytes(target_mbr, sizeof(target_mbr));
  929. if (!ensure_data_partition(&dst_copy, target_mbr, data_entry,
  930. &data_created))
  931. goto fail;
  932. copy_bytes(image, sector0, 512);
  933. copy_bytes(image + MBR_PART_OFF + 3 * MBR_PART_SIZE, data_entry,
  934. MBR_PART_SIZE);
  935. /* Include the final MBR (with the merged data entry) and the zero gap
  936. * in the checksum. `sum` currently covers BOOT_LBA..used. */
  937. sum = sum_bytes(0, image, NEBOS_BOOT_LBA * 512u) + sum;
  938. io_putstr("install: writing target");
  939. for (lba = 1; lba < used; lba += INSTALL_CHUNK) {
  940. unsigned int n = used - lba;
  941. if (n > INSTALL_CHUNK)
  942. n = INSTALL_CHUNK;
  943. if (write_chunk(&dst_copy, lba, image + lba * 512u, n) != 0) {
  944. install_fail("target write failed", lba);
  945. goto fail;
  946. }
  947. if (lba >= INSTALL_DOT_LBA && lba % INSTALL_DOT_LBA < INSTALL_CHUNK)
  948. io_putchar('.');
  949. }
  950. /* MBR last: an interrupted copy is not bootable. */
  951. if (write_chunk(&dst_copy, 0, image, 1) != 0) {
  952. install_fail("target MBR write failed", 0);
  953. goto fail;
  954. }
  955. io_putchar('\n');
  956. {
  957. int fr;
  958. bkl_pause();
  959. fr = blk_flush(&dst_copy);
  960. bkl_resume();
  961. if (fr != 0)
  962. io_putstr("install: cache flush failed; verifying anyway\n");
  963. }
  964. io_putstr("install: verifying...\n");
  965. vsum = 0;
  966. for (lba = 0; lba < used; lba += INSTALL_CHUNK) {
  967. unsigned int n = used - lba;
  968. if (n > INSTALL_CHUNK)
  969. n = INSTALL_CHUNK;
  970. if (read_chunk(&dst_copy, lba, copy_buf, n) != 0) {
  971. install_fail("target read-back failed", lba);
  972. goto fail;
  973. }
  974. vsum = sum_bytes(vsum, copy_buf, n * 512u);
  975. }
  976. if (vsum != sum) {
  977. install_fail("verification mismatch", 0);
  978. goto fail;
  979. }
  980. io_putstr("install: verified ");
  981. format_putdec(used);
  982. io_putstr(" sectors\n");
  983. free_pages(image, image_pages);
  984. image = 0;
  985. vfs_remount_device(&dst_copy);
  986. format_busy = 0;
  987. io_putstr("install: done; remove the install medium and reboot.\n");
  988. io_putstr(data_created ? "install: created /data on partition 4.\n"
  989. : "install: kept the existing /data partition.\n");
  990. serial_puts(SERIAL_COM1, "install: done\n");
  991. return;
  992. fail:
  993. if (image)
  994. free_pages(image, image_pages);
  995. if (have_copies)
  996. vfs_remount_device(&dst_copy);
  997. format_busy = 0;
  998. io_putstr("install: target is incomplete; do not boot it\n");
  999. serial_puts(SERIAL_COM1, "install: failed\n");
  1000. }