/* 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 [source] */ #include "blkdev.h" #include "fatdetect.h" #include "fatformat.h" #include "install.h" #include "io.h" #include "mm.h" #include "process.h" #include "ramfs.h" #include "rootfs.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 DATA_MIN_SECTORS 2048u /* /data needs at least 1 MiB */ #define MBR_PART_OFF 0x1BE #define MBR_PART_SIZE 16 #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) != NEBOS_BOOT_LBA || get32(e + 12) < 1024u) return 0; } if (blk_read(d, NEBOS_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) != NEBOS_KERNEL_LBA) return 0; if (get16(boot + KERNEL_SEC_OFF) == 0) return 0; /* Used length: the end of the highest OS partition. Partition 4 is * the persistent data volume: it is not part of the cloned image and * may legitimately extend to the end of the disk. A source entry may * not cross into the data area, though. */ 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; if (start >= NEBOS_DATA_LBA) continue; end = start + count; if (end > NEBOS_DATA_LBA) return 0; 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"); } static void put16(unsigned char *p, unsigned int v) { p[0] = (unsigned char)v; p[1] = (unsigned char)(v >> 8); } static void put32(unsigned char *p, unsigned int v) { p[0] = (unsigned char)v; p[1] = (unsigned char)(v >> 8); p[2] = (unsigned char)(v >> 16); p[3] = (unsigned char)(v >> 24); } static void copy_bytes(unsigned char *dst, const unsigned char *src, unsigned int n) { unsigned int i; for (i = 0; i < n; i++) dst[i] = src[i]; } static void zero_bytes(unsigned char *dst, unsigned int n) { unsigned int i; for (i = 0; i < n; i++) dst[i] = 0; } static int str_eq(const char *a, const char *b) { unsigned int i = 0; while (a[i] && b[i] && a[i] == b[i]) i++; return a[i] == '\0' && b[i] == '\0'; } /* The persistent data volume is recognised by its fixed LBA and label. */ static int detect_data_partition(blk_dev_t *d, const unsigned char *mbr, unsigned char *entry) { const unsigned char *e = mbr + MBR_PART_OFF + 3 * MBR_PART_SIZE; unsigned int start, count; fat_fs_t fs; if (!mbr_is_fat_type(e[4])) return 0; start = get32(e + 8); count = get32(e + 12); if (start != NEBOS_DATA_LBA || count < DATA_MIN_SECTORS) return 0; if (start > d->total_sectors || count > d->total_sectors - start) return 0; if (fat_mount(d, start, &fs) != 0) return 0; if (!str_eq(fs.label, "NEBOSDATA")) return 0; if (entry) copy_bytes(entry, e, MBR_PART_SIZE); return 1; } /* Detect and, when absent, create the data partition. The caller owns the * target MBR buffer and writes it last. `created` says whether the entry is * new (and therefore whether the MBR must be rewritten). */ static int ensure_data_partition(blk_dev_t *d, const unsigned char *mbr, unsigned char *entry, int *created) { unsigned int sectors, type = 0; *created = 0; if (detect_data_partition(d, mbr, entry)) { io_putstr("install: /data partition present; keeping it\n"); return 1; } if (d->total_sectors <= NEBOS_DATA_LBA || d->total_sectors - NEBOS_DATA_LBA < DATA_MIN_SECTORS) { io_putstr("install: target is too small for a /data partition\n"); return 0; } sectors = d->total_sectors - NEBOS_DATA_LBA; io_putstr("install: creating /data partition ("); put_size(sectors); io_putstr(")...\n"); /* The installer's format path owns the disks through format_busy, and * the FAT loop releases the BKL per chunk. Dropping it entirely (as * `format` does) avoids the format_progress sleeps holding it. */ bkl_unlock(); if (fat_format_partition(d, NEBOS_DATA_LBA, sectors, "NEBOSDATA", &type) != 0) { bkl_lock(); io_putstr("install: /data format failed\n"); return 0; } bkl_lock(); zero_bytes(entry, MBR_PART_SIZE); entry[1] = 0xFE; entry[2] = 0xFF; entry[3] = 0xFF; entry[4] = (type == 32) ? 0x0C : ((type == 16) ? 0x0E : 0x01); entry[5] = 0xFE; entry[6] = 0xFF; entry[7] = 0xFF; put32(entry + 8, NEBOS_DATA_LBA); put32(entry + 12, sectors); *created = 1; io_putstr("install: /data FAT"); format_putdec(type); io_putstr(", "); put_size(sectors); io_putchar('\n'); return 1; } static blk_dev_t *resolve_target(const char *name) { blk_dev_t *found = 0; unsigned int i; if (!str_eq(name, "auto")) { found = blk_find(name); if (!found) { io_putstr("install: unknown target '"); io_putstr(name); io_putstr("'\n"); list_devices(); } return found; } for (i = 0; i < blk_dev_count; i++) { blk_dev_t *d = &blk_devs[i]; if (!d->present || d->kind == BLK_FLOPPY || d->kind == BLK_USB) continue; if (!check_nebos_image(d, 0, 0, 0)) continue; if (found) { io_putstr("install: auto found more than one NebOS disk; " "name the target explicitly\n"); return 0; } found = d; } if (!found) io_putstr("install: auto found no installed NebOS disk\n"); return found; } static int ask_continue(blk_dev_t *target, int have_data) { int k; if (have_data) { io_putstr("install: this replaces the OS on "); io_putstr(target->name); io_putstr("; files on /data are kept. Continue? (y/n) "); } else { 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. */ io_set_raw_keyboard(1); for (;;) { k = io_poll_key_with_hw(); if (k == 'y' || k == 'Y') { io_set_raw_keyboard(0); io_putchar('\n'); return 1; } if (k == 'n' || k == 'N') { io_set_raw_keyboard(0); io_putstr("aborted\n"); return 0; } process_sleep_ms(10); } } /* Write `bytes` from p at lba, padding the final partial sector with * zeros. Chunks release the BKL like the rest of the installer. */ static int write_region(blk_dev_t *d, unsigned int lba, const unsigned char *p, unsigned int bytes, const char *what) { unsigned int full = bytes / 512u, off = 0, n; while (off < full) { n = full - off; if (n > INSTALL_CHUNK) n = INSTALL_CHUNK; if (write_chunk(d, lba + off, p + off * 512u, n) != 0) { install_fail(what, lba + off); return -1; } off += n; } if (bytes % 512u) { unsigned int rem = bytes % 512u, i; for (i = 0; i < rem; i++) copy_buf[i] = p[full * 512u + i]; for (; i < 512u; i++) copy_buf[i] = 0; if (write_chunk(d, lba + full, copy_buf, 1) != 0) { install_fail(what, lba + full); return -1; } } return 0; } static int verify_region(blk_dev_t *d, unsigned int lba, const unsigned char *p, unsigned int bytes, const char *what) { unsigned int off = 0, sum = sum_bytes(0, p, bytes), vsum = 0, n; while (off + 512u <= bytes) { n = (bytes - off) / 512u; if (n > INSTALL_CHUNK) n = INSTALL_CHUNK; if (read_chunk(d, lba + off / 512u, copy_buf, n) != 0) { install_fail("target read-back failed", lba + off / 512u); return -1; } vsum = sum_bytes(vsum, copy_buf, n * 512u); off += n * 512u; } if (off < bytes) { if (read_chunk(d, lba + off / 512u, copy_buf, 1) != 0) { install_fail("target read-back failed", lba + off / 512u); return -1; } vsum = sum_bytes(vsum, copy_buf, bytes - off); } if (vsum != sum) { install_fail(what, 0); return -1; } return 0; } /* Shared P1/P2 writer for `install self` and the network install path. */ static int write_os_image(blk_dev_t *d, const void *kernel, unsigned int ksize, const struct rootfs_hdr *hdr, const void *pack, unsigned int psize, int ensure_data) { unsigned char boot[512], mbr[512], data_entry[MBR_PART_SIZE]; unsigned int used = 0, ksectors; int has_esp = 0, created = 0; if (!d || !d->present || d->kind == BLK_FLOPPY || !kernel || !ksize || !hdr || !pack || !psize) return -1; if (hdr->version != ROOTFS_VERSION || hdr->pack_bytes != psize) { io_putstr("install: bad rootfs header\n"); return -1; } if (hdr->flags & ~ROOTFS_FLAG_GZIP) { io_putstr("install: unsupported rootfs flags\n"); return -1; } ksectors = (ksize + 511u) / 512u; if (ksectors == 0 || ksectors > ROOTFS_LBA - NEBOS_KERNEL_LBA) { io_putstr("install: kernel does not fit partition 1\n"); return -1; } if (psize + ROOTFS_HDR_SIZE > ROOTFS_SECTORS * 512u) { io_putstr("install: rootfs does not fit partition 2\n"); return -1; } if (!check_nebos_image(d, &used, &has_esp, mbr)) { io_putstr("install: target is not an installed NebOS disk\n"); io_putstr("install: run `install ` from USB first\n"); return -1; } if (blk_read(d, NEBOS_BOOT_LBA, boot, 1) != 0) { io_putstr("install: cannot read the target boot sector\n"); return -1; } if (format_busy && process_is_alive(format_owner_pid)) { io_putstr("install: another format/install is in progress\n"); return -1; } format_owner_pid = current_proc()->pid; format_busy = 1; /* Prepare the data partition before touching P1/P2: a format failure * then leaves the old OS bootable. */ if (ensure_data && !ensure_data_partition(d, mbr, data_entry, &created)) goto fail; io_putstr("install: writing rootfs...\n"); if (write_chunk(d, ROOTFS_LBA, hdr, 1) != 0 || write_region(d, ROOTFS_LBA + 1u, (const unsigned char *)pack, psize, "rootfs write failed") != 0) goto fail; io_putstr("install: writing kernel...\n"); if (write_region(d, NEBOS_KERNEL_LBA, (const unsigned char *)kernel, ksize, "kernel write failed") != 0) goto fail; /* The installed boot sector carries only the NebOS header fields. */ zero_bytes(boot + 0x0B, 0x3E - 0x0B); put16(boot + KERNEL_SEC_OFF, ksectors); if (write_chunk(d, NEBOS_BOOT_LBA, boot, 1) != 0) { install_fail("boot sector write failed", NEBOS_BOOT_LBA); goto fail; } if (ensure_data && created) { copy_bytes(mbr + MBR_PART_OFF + 3 * MBR_PART_SIZE, data_entry, MBR_PART_SIZE); if (write_chunk(d, 0, mbr, 1) != 0) { install_fail("MBR write failed", 0); goto fail; } } { int fr; bkl_pause(); fr = blk_flush(d); bkl_resume(); if (fr != 0) io_putstr("install: cache flush failed; verifying anyway\n"); } io_putstr("install: verifying...\n"); if (verify_region(d, ROOTFS_LBA, (const unsigned char *)hdr, ROOTFS_HDR_SIZE, "rootfs header verify failed") != 0 || verify_region(d, ROOTFS_LBA + 1u, (const unsigned char *)pack, psize, "rootfs verify failed") != 0 || verify_region(d, NEBOS_KERNEL_LBA, (const unsigned char *)kernel, ksize, "kernel verify failed") != 0) goto fail; if (read_chunk(d, NEBOS_BOOT_LBA, copy_buf, 1) != 0) { install_fail("boot sector read-back failed", NEBOS_BOOT_LBA); goto fail; } { unsigned int i; for (i = 0; i < 512u; i++) { if (copy_buf[i] != boot[i]) { install_fail("boot sector verify failed", NEBOS_BOOT_LBA); goto fail; } } } if (created) { if (read_chunk(d, 0, copy_buf, 1) != 0) { install_fail("MBR read-back failed", 0); goto fail; } { unsigned int i; for (i = 0; i < 512u; i++) { if (copy_buf[i] != mbr[i]) { install_fail("MBR verify failed", 0); goto fail; } } } } if (created) vfs_remount_device(d); format_busy = 0; io_putstr("install: verified; "); put_size(used); io_putstr(" image written to "); io_putstr(d->name); if (ensure_data) io_putstr(", /data kept"); io_putchar('\n'); serial_puts(SERIAL_COM1, "install: image written\n"); return 0; fail: format_busy = 0; io_putstr("install: image update failed\n"); serial_puts(SERIAL_COM1, "install: image update failed\n"); return -1; } static int install_self(const char *target_name) { struct vfs_file kf; struct vfs_mount *km = 0; const void *pack = 0; unsigned int psize = 0; int gz = 0, r; struct rootfs_hdr hdr; blk_dev_t *target; unsigned char mbr[512]; int have_data = 0; if (vfs_load_file("/ramfs/install/kernel.bin", &kf, &km) != 0) { io_putstr("install: /ramfs/install/kernel.bin is missing\n"); io_putstr("install: the floppy/lite pack has no running kernel image\n"); return -1; } if (!ramfs_boot_image(&pack, &psize, &gz) || !pack || !psize) { io_putstr("install: no boot-time rootfs image is retained\n"); vfs_release_file(km, &kf); return -1; } if (!gz) { io_putstr("install: the running pack was replaced at runtime (raw)\n"); io_putstr("install: use `make net-install` to persist it\n"); vfs_release_file(km, &kf); return -1; } if (psize + ROOTFS_HDR_SIZE > ROOTFS_SECTORS * 512u) { io_putstr("install: running rootfs is too large for partition 2\n"); vfs_release_file(km, &kf); return -1; } target = resolve_target(target_name); if (!target) { vfs_release_file(km, &kf); return -1; } if (!target->present || target->kind == BLK_FLOPPY || target->total_sectors == 0) { io_putstr("install: target is not usable\n"); vfs_release_file(km, &kf); return -1; } if (!check_nebos_image(target, 0, 0, mbr)) { io_putstr("install: target is not an installed NebOS disk\n"); io_putstr("install: run `install ` from USB first\n"); vfs_release_file(km, &kf); return -1; } have_data = detect_data_partition(target, mbr, 0); io_putstr("install: source running image, kernel "); put_size((kf.size + 511u) / 512u); io_putstr(", rootfs "); put_size((psize + 511u) / 512u); io_putstr(gz ? " gzip" : " raw"); io_putchar('\n'); io_putstr("install: target "); io_putstr(target->name); if (target->model[0]) { io_putstr(" ("); io_putstr(target->model); io_putchar(')'); } io_putchar('\n'); if (!ask_continue(target, have_data)) { vfs_release_file(km, &kf); return -1; } zero_bytes((unsigned char *)&hdr, sizeof(hdr)); copy_bytes((unsigned char *)hdr.magic, (const unsigned char *)ROOTFS_MAGIC, 7); hdr.version = ROOTFS_VERSION; hdr.pack_bytes = psize; hdr.pack_csum = sum_bytes(0, (const unsigned char *)pack, psize); hdr.flags = ROOTFS_FLAG_GZIP; r = write_os_image(target, kf.data, kf.size, &hdr, pack, psize, 1); vfs_release_file(km, &kf); if (r == 0) { io_putstr("install: done; /data kept or created.\n"); serial_puts(SERIAL_COM1, "install: self done\n"); } return r; } int install_ram_image(const char *target, const void *kernel, unsigned int ksize, const void *rootfs, unsigned int rsize) { const struct rootfs_hdr *hdr; const unsigned char *pack; unsigned int psize; blk_dev_t *d; if (!rootfs || rsize < ROOTFS_HDR_SIZE) { io_putstr("install: rootfs image too short\n"); return -1; } hdr = (const struct rootfs_hdr *)rootfs; if (hdr->magic[0] != 'N' || hdr->magic[1] != 'B' || hdr->magic[2] != 'R' || hdr->magic[3] != 'O' || hdr->magic[4] != 'O' || hdr->magic[5] != 'T' || hdr->magic[6] != '1' || hdr->version != ROOTFS_VERSION) { io_putstr("install: bad rootfs image\n"); return -1; } psize = hdr->pack_bytes; if (!psize || psize > rsize - ROOTFS_HDR_SIZE || !(hdr->flags & ROOTFS_FLAG_GZIP)) { io_putstr("install: bad rootfs length or encoding\n"); return -1; } d = resolve_target(target); if (!d) { io_putstr("install: no network install target\n"); return -1; } pack = (const unsigned char *)rootfs + ROOTFS_HDR_SIZE; io_putstr("install: net update to "); io_putstr(d->name); io_putstr(" (kernel "); put_size((ksize + 511u) / 512u); io_putstr(", rootfs "); put_size((psize + 511u) / 512u); io_putstr(")\n"); return write_os_image(d, kernel, ksize, hdr, pack, psize, 0); } void program_install_main(const char *arg) { char target_name[INSTALL_NAME_MAX], source_name[INSTALL_NAME_MAX]; char source_dev[INSTALL_NAME_MAX], target_dev[INSTALL_NAME_MAX]; blk_dev_t *target, *source; blk_dev_t src_copy, dst_copy; unsigned char *image = 0; unsigned char target_mbr[512], data_entry[MBR_PART_SIZE]; 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 data_created = 0; 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 [source|self]\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 [source|self]\n"); return; } if (str_eq(source_name, "self")) { install_self(target_name); return; } target = resolve_target(target_name); if (!target) 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; } for (i = 0; i + 1 < sizeof(target_dev) && target->name[i]; i++) target_dev[i] = target->name[i]; target_dev[i] = '\0'; 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'; { int have_data = 0; if (blk_read(target, 0, target_mbr, 1) == 0) have_data = detect_data_partition(target, target_mbr, 0); if (have_data) io_putstr("install: /data partition found; keeping it\n"); if (!ask_continue(target, have_data)) return; } /* 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_dev); 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 < NEBOS_BOOT_LBA * 512u; i++) image[i] = 0; sum = 0; 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 = NEBOS_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'); /* The data partition is prepared before the target is touched. It is * outside the cloned P1..P3 range; an existing one is left untouched. */ if (blk_read(&dst_copy, 0, target_mbr, 1) != 0) zero_bytes(target_mbr, sizeof(target_mbr)); if (!ensure_data_partition(&dst_copy, target_mbr, data_entry, &data_created)) goto fail; copy_bytes(image, sector0, 512); copy_bytes(image + MBR_PART_OFF + 3 * MBR_PART_SIZE, data_entry, MBR_PART_SIZE); /* Include the final MBR (with the merged data entry) and the zero gap * in the checksum. `sum` currently covers BOOT_LBA..used. */ sum = sum_bytes(0, image, NEBOS_BOOT_LBA * 512u) + sum; 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"); io_putstr(data_created ? "install: created /data on partition 4.\n" : "install: kept the existing /data partition.\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"); }