| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550 |
- /* install: clone the live NebOS image onto a storage device.
- *
- * The source is the running install medium as a block device. A USB stick
- * needs the USB mass-storage driver (usb0); an internal image can be read
- * from IDE/SATA/eMMC directly. The installer copies the image's used
- * sectors (MBR through the end of the last partition), writes the target's
- * MBR last and verifies the copy with a checksum. The cloned image keeps
- * both boot paths: the MBR/legacy chain and (when present) the ESP/UEFI
- * chain.
- *
- * Usage: install <target> [source]
- */
- #include "blkdev.h"
- #include "fatformat.h"
- #include "io.h"
- #include "mm.h"
- #include "process.h"
- #include "sched.h"
- #include "serial.h"
- #include "vga.h"
- #include "vfs.h"
- #define INSTALL_NAME_MAX 16
- #define INSTALL_CHUNK 32u /* 16 KiB copy buffer */
- #define INSTALL_DOT_LBA 2048u /* one progress dot per MiB */
- #define MBR_PART_OFF 0x1BE
- #define MBR_PART_SIZE 16
- #define BOOT_LBA 63u
- #define ROOTFS_LBA 1088u
- #define PACK_LBA_OFF 0x42
- #define KERNEL_LBA_OFF 0x40
- #define KERNEL_SEC_OFF 0x3E
- #define ESP_TYPE 0xEFu
- static unsigned char sector0[512];
- static unsigned char copy_buf[INSTALL_CHUNK * 512];
- /* The copy and verify release the BKL around each chunk, like the format
- * raw-sector loop, so the WM and terminals keep running. format_busy keeps
- * disk-backed filesystem syscalls out; the device descriptors are copied
- * before the loop so a mount rescan cannot invalidate them. */
- static int read_chunk(blk_dev_t *d, unsigned int lba, void *buf,
- unsigned int n)
- {
- int r;
- bkl_pause();
- r = blk_read(d, lba, buf, n);
- bkl_resume();
- return r;
- }
- static int write_chunk(blk_dev_t *d, unsigned int lba, const void *buf,
- unsigned int n)
- {
- int r;
- bkl_pause();
- r = blk_write(d, lba, buf, n);
- bkl_resume();
- return r;
- }
- static void free_pages(unsigned char *p, unsigned int pages)
- {
- unsigned int i;
- if (!p)
- return;
- for (i = 0; i < pages; i++)
- mm_free_page((unsigned int)p + i * 4096u);
- }
- static unsigned int get16(const unsigned char *p)
- {
- return (unsigned int)p[0] | ((unsigned int)p[1] << 8);
- }
- static unsigned int get32(const unsigned char *p)
- {
- return (unsigned int)p[0] | ((unsigned int)p[1] << 8) |
- ((unsigned int)p[2] << 16) | ((unsigned int)p[3] << 24);
- }
- static void list_devices(void)
- {
- unsigned int i;
- io_putstr("devices:");
- for (i = 0; i < blk_dev_count; i++) {
- io_putchar(' ');
- io_putstr(blk_devs[i].name);
- }
- io_putchar('\n');
- }
- static void put_size(unsigned int sectors)
- {
- if (sectors % 2048u == 0) {
- format_putdec(sectors / 2048u);
- io_putstr(" MiB");
- } else {
- format_putdec(sectors);
- io_putstr(" sectors");
- }
- }
- /* Validate the NebOS image signature and return its used sector count.
- * The image layout is the one the Makefile builds: an MBR whose first
- * partition starts at LBA 63 and holds the boot sector at 63 + the kernel
- * at 64, and a boot sector whose pack_lba points at the rootfs image.
- * `mbr_out` (optional) receives the MBR read during validation. */
- static int check_nebos_image(blk_dev_t *d, unsigned int *used, int *has_esp,
- unsigned char *mbr_out)
- {
- unsigned char mbr_local[512], boot[512];
- unsigned char *mbr = mbr_out ? mbr_out : mbr_local;
- unsigned int i, max_end = 0;
- int esp = 0;
- if (!d || !d->present || d->kind == BLK_FLOPPY)
- return 0;
- if (d->total_sectors < ROOTFS_LBA + 512u)
- return 0;
- if (blk_read(d, 0, mbr, 1) != 0)
- return 0;
- if (mbr[510] != 0x55 || mbr[511] != 0xAA)
- return 0;
- /* Partition 1: active, starts at LBA 63, holds boot + kernel. */
- {
- const unsigned char *e = mbr + MBR_PART_OFF;
- if (e[0] != 0x80 || e[4] != 0x06 ||
- get32(e + 8) != BOOT_LBA || get32(e + 12) < 1024u)
- return 0;
- }
- if (blk_read(d, BOOT_LBA, boot, 1) != 0)
- return 0;
- if (boot[510] != 0x55 || boot[511] != 0xAA)
- return 0;
- if (boot[3] != 'N' || boot[4] != 'E' || boot[5] != 'B' ||
- boot[6] != 'O' || boot[7] != 'S')
- return 0;
- if (get32(boot + PACK_LBA_OFF) != ROOTFS_LBA)
- return 0;
- if (get16(boot + KERNEL_LBA_OFF) != BOOT_LBA + 1u)
- return 0;
- if (get16(boot + KERNEL_SEC_OFF) == 0)
- return 0;
- /* Used length: the end of the highest non-empty partition. Require
- * the rootfs area to be covered, and reject an entry that claims more
- * sectors than the source has. */
- for (i = 0; i < 4; i++) {
- const unsigned char *e = mbr + MBR_PART_OFF + i * MBR_PART_SIZE;
- unsigned int start, count, end;
- if (e[4] == 0xEF)
- esp = 1;
- if (e[4] == 0)
- continue;
- start = get32(e + 8);
- count = get32(e + 12);
- if (!count || start > d->total_sectors ||
- count > d->total_sectors - start)
- return 0;
- end = start + count;
- if (end > max_end)
- max_end = end;
- }
- if (max_end < ROOTFS_LBA + 512u)
- return 0;
- if (used)
- *used = max_end;
- if (has_esp)
- *has_esp = esp;
- return 1;
- }
- static blk_dev_t *find_source(blk_dev_t *target, const char *name,
- unsigned int *used)
- {
- blk_dev_t *usb = 0, *other = 0;
- unsigned int usb_used = 0, other_used = 0, u;
- int esp;
- if (name) {
- blk_dev_t *d = blk_find(name);
- if (!d || !d->present) {
- io_putstr("install: source '");
- io_putstr(name);
- io_putstr("' is not present\n");
- return 0;
- }
- if (d == target) {
- io_putstr("install: source and target are the same device\n");
- return 0;
- }
- if (!check_nebos_image(d, &u, &esp, 0)) {
- io_putstr("install: '");
- io_putstr(name);
- io_putstr("' does not contain a NebOS image\n");
- return 0;
- }
- *used = u;
- return d;
- }
- for (u = 0; u < blk_dev_count; u++) {
- blk_dev_t *d = &blk_devs[u];
- unsigned int n;
- if (d == target || !d->present || d->kind == BLK_FLOPPY)
- continue;
- if (!check_nebos_image(d, &n, &esp, 0))
- continue;
- if (d->kind == BLK_USB) {
- if (!usb) {
- usb = d;
- usb_used = n;
- }
- } else if (!other) {
- other = d;
- other_used = n;
- }
- }
- if (usb) {
- *used = usb_used;
- return usb;
- }
- if (other) {
- *used = other_used;
- return other;
- }
- return 0;
- }
- static unsigned int sum_bytes(unsigned int sum, const unsigned char *p,
- unsigned int n)
- {
- while (n--)
- sum += *p++;
- return sum;
- }
- static void install_fail(const char *msg, unsigned int lba)
- {
- io_putstr("install: ");
- io_putstr(msg);
- if (lba) {
- io_putstr(" at lba ");
- format_putdec(lba);
- }
- io_putchar('\n');
- serial_puts(SERIAL_COM1, "install: failed at lba ");
- serial_putdec(SERIAL_COM1, lba);
- serial_puts(SERIAL_COM1, "\n");
- }
- void program_install_main(const char *arg)
- {
- char target_name[INSTALL_NAME_MAX], source_name[INSTALL_NAME_MAX];
- char source_dev[INSTALL_NAME_MAX];
- blk_dev_t *target, *source;
- blk_dev_t src_copy, dst_copy;
- unsigned char *image = 0;
- unsigned int image_pages = 0;
- int have_copies = 0;
- unsigned int used, i, j, lba, sum, vsum;
- int has_esp = 0, explicit_source = 0;
- int k;
- if (format_busy && process_is_alive(format_owner_pid)) {
- io_putstr("install: another format/install is in progress\n");
- return;
- }
- format_busy = 0;
- if (!arg) {
- io_putstr("install: usage install <target> [source]\n");
- list_devices();
- return;
- }
- /* Target = first word, optional source = second word. */
- i = 0;
- while (arg[i] == ' ')
- i++;
- j = 0;
- while (arg[i] && arg[i] != ' ' && j < sizeof(target_name) - 1)
- target_name[j++] = arg[i++];
- target_name[j] = '\0';
- while (arg[i] == ' ')
- i++;
- j = 0;
- while (arg[i] && arg[i] != ' ' && j < sizeof(source_name) - 1)
- source_name[j++] = arg[i++];
- source_name[j] = '\0';
- explicit_source = (source_name[0] != '\0');
- while (arg[i] == ' ')
- i++;
- if (!target_name[0] || arg[i]) {
- io_putstr("install: usage install <target> [source]\n");
- return;
- }
- target = blk_find(target_name);
- if (!target) {
- io_putstr("install: unknown target '");
- io_putstr(target_name);
- io_putstr("'\n");
- list_devices();
- return;
- }
- if (target->kind == BLK_FLOPPY) {
- io_putstr("install: a floppy is too small for the image\n");
- return;
- }
- if (!target->present || target->total_sectors == 0) {
- io_putstr("install: target ");
- io_putstr(target->name);
- io_putstr(" is not present\n");
- return;
- }
- source = find_source(target, explicit_source ? source_name : 0, &used);
- if (!source) {
- if (!explicit_source) {
- io_putstr("install: no NebOS install source found\n");
- io_putstr("install: the USB stick must be visible as a block device\n");
- list_devices();
- }
- return;
- }
- /* Recompute the flags for the chosen source (find_source only
- * returned the used length). */
- check_nebos_image(source, &used, &has_esp, 0);
- if (used > target->total_sectors) {
- io_putstr("install: source image (");
- put_size(used);
- io_putstr(") does not fit on ");
- io_putstr(target->name);
- io_putstr(" (");
- put_size(target->total_sectors);
- io_putstr(")\n");
- return;
- }
- io_putstr("install: source ");
- io_putstr(source->name);
- if (source->model[0]) {
- io_putstr(" (");
- io_putstr(source->model);
- io_putchar(')');
- }
- io_putstr(", ");
- put_size(used);
- io_putstr(", ");
- io_putstr(has_esp ? "BIOS + UEFI" : "BIOS only");
- io_putchar('\n');
- io_putstr("install: target ");
- io_putstr(target->name);
- if (target->model[0]) {
- io_putstr(" (");
- io_putstr(target->model);
- io_putchar(')');
- }
- io_putstr(", ");
- put_size(target->total_sectors);
- io_putchar('\n');
- for (i = 0; i + 1 < sizeof(source_dev) && source->name[i]; i++)
- source_dev[i] = source->name[i];
- source_dev[i] = '\0';
- io_putstr("install: this destroys all data on ");
- io_putstr(target->name);
- io_putstr("? (y/n) ");
- vga_fb_flush();
- /* The confirmation loop yields so the kworker can poll the USB HID
- * keyboard. The copy then releases the BKL per chunk; do not run
- * lsusb (a bus rescan) while an install is in progress. */
- io_set_raw_keyboard(1);
- for (;;) {
- k = io_poll_key_with_hw();
- if (k == 'y' || k == 'Y')
- break;
- if (k == 'n' || k == 'N') {
- io_set_raw_keyboard(0);
- io_putstr("aborted\n");
- return;
- }
- process_sleep_ms(10);
- }
- io_set_raw_keyboard(0);
- io_putchar('\n');
- /* The prompt yielded; another command may have rescanned the block
- * devices or started a format. Re-find both by name and take a
- * private copy of the source MBR. */
- if (format_busy && process_is_alive(format_owner_pid)) {
- io_putstr("install: another format/install is in progress\n");
- return;
- }
- format_owner_pid = current_proc()->pid;
- format_busy = 1;
- target = blk_find(target_name);
- source = blk_find(source_dev);
- if (!target || !source || source == target ||
- !target->present || !source->present) {
- io_putstr("install: device set changed; aborted\n");
- goto fail;
- }
- src_copy = *source;
- dst_copy = *target;
- have_copies = 1;
- if (used > dst_copy.total_sectors) {
- io_putstr("install: source no longer fits; aborted\n");
- goto fail;
- }
- if (!check_nebos_image(&src_copy, &used, &has_esp, sector0)) {
- io_putstr("install: source changed; aborted\n");
- goto fail;
- }
- if (used > dst_copy.total_sectors) {
- io_putstr("install: source no longer fits; aborted\n");
- goto fail;
- }
- serial_puts(SERIAL_COM1, "install: ");
- serial_puts(SERIAL_COM1, src_copy.name);
- serial_puts(SERIAL_COM1, " -> ");
- serial_puts(SERIAL_COM1, dst_copy.name);
- serial_puts(SERIAL_COM1, ", ");
- serial_putdec(SERIAL_COM1, used);
- serial_puts(SERIAL_COM1, " sectors\n");
- /* Read the whole image into RAM first. Interleaving USB source reads
- * with the slow eMMC target writes lets the USB3 link go idle between
- * chunks; some sticks then fail the next READ(10) (the eMMC target is
- * still written from RAM afterwards). */
- image_pages = (used * 512u + 4095u) / 4096u;
- image = (unsigned char *)mm_alloc_pages(image_pages);
- if (!image) {
- io_putstr("install: out of memory for the source image\n");
- goto fail;
- }
- for (i = 0; i < sizeof(sector0); i++)
- image[i] = sector0[i];
- /* LBA 1..62 are the unused zero gap (partition 1 starts at 63). */
- for (i = sizeof(sector0); i < BOOT_LBA * 512u; i++)
- image[i] = 0;
- sum = sum_bytes(0, sector0, sizeof(sector0));
- io_putstr("install: reading source");
- vga_fb_flush();
- /* Keep the BKL here: the USB source must stay active across all
- * chunks. The target writes below release it per chunk. */
- for (lba = BOOT_LBA; lba < used; lba += INSTALL_CHUNK) {
- unsigned int n = used - lba;
- if (n > INSTALL_CHUNK)
- n = INSTALL_CHUNK;
- if (blk_read(&src_copy, lba, image + lba * 512u, n) != 0) {
- install_fail("source read failed", lba);
- goto fail;
- }
- sum = sum_bytes(sum, image + lba * 512u, n * 512u);
- if (lba >= INSTALL_DOT_LBA && lba % INSTALL_DOT_LBA < INSTALL_CHUNK)
- io_putchar('.');
- }
- io_putchar('\n');
- io_putstr("install: writing target");
- for (lba = 1; lba < used; lba += INSTALL_CHUNK) {
- unsigned int n = used - lba;
- if (n > INSTALL_CHUNK)
- n = INSTALL_CHUNK;
- if (write_chunk(&dst_copy, lba, image + lba * 512u, n) != 0) {
- install_fail("target write failed", lba);
- goto fail;
- }
- if (lba >= INSTALL_DOT_LBA && lba % INSTALL_DOT_LBA < INSTALL_CHUNK)
- io_putchar('.');
- }
- /* MBR last: an interrupted copy is not bootable. */
- if (write_chunk(&dst_copy, 0, image, 1) != 0) {
- install_fail("target MBR write failed", 0);
- goto fail;
- }
- io_putchar('\n');
- {
- int fr;
- bkl_pause();
- fr = blk_flush(&dst_copy);
- bkl_resume();
- if (fr != 0)
- io_putstr("install: cache flush failed; verifying anyway\n");
- }
- io_putstr("install: verifying...\n");
- vsum = 0;
- for (lba = 0; lba < used; lba += INSTALL_CHUNK) {
- unsigned int n = used - lba;
- if (n > INSTALL_CHUNK)
- n = INSTALL_CHUNK;
- if (read_chunk(&dst_copy, lba, copy_buf, n) != 0) {
- install_fail("target read-back failed", lba);
- goto fail;
- }
- vsum = sum_bytes(vsum, copy_buf, n * 512u);
- }
- if (vsum != sum) {
- install_fail("verification mismatch", 0);
- goto fail;
- }
- io_putstr("install: verified ");
- format_putdec(used);
- io_putstr(" sectors\n");
- free_pages(image, image_pages);
- image = 0;
- vfs_remount_device(&dst_copy);
- format_busy = 0;
- io_putstr("install: done; remove the install medium and reboot.\n");
- serial_puts(SERIAL_COM1, "install: done\n");
- return;
- fail:
- if (image)
- free_pages(image, image_pages);
- if (have_copies)
- vfs_remount_device(&dst_copy);
- format_busy = 0;
- io_putstr("install: target is incomplete; do not boot it\n");
- serial_puts(SERIAL_COM1, "install: failed\n");
- }
|