/* 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 "fatformat.h" #include "io.h" #include "process.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]; 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; 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 [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 [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 copy runs with the BKL held: a USB source transfer must not be * interrupted by a bus rescan. 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') 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; } if (used > target->total_sectors) { io_putstr("install: source no longer fits; aborted\n"); goto fail; } if (!check_nebos_image(source, &used, &has_esp, sector0)) { io_putstr("install: source changed; aborted\n"); goto fail; } if (used > target->total_sectors) { io_putstr("install: source no longer fits; aborted\n"); goto fail; } serial_puts(SERIAL_COM1, "install: "); serial_puts(SERIAL_COM1, source->name); serial_puts(SERIAL_COM1, " -> "); serial_puts(SERIAL_COM1, target->name); serial_puts(SERIAL_COM1, ", "); serial_putdec(SERIAL_COM1, used); serial_puts(SERIAL_COM1, " sectors\n"); io_putstr("install: copying"); vga_fb_flush(); sum = sum_bytes(0, sector0, sizeof(sector0)); for (lba = 1; lba < used; lba += INSTALL_CHUNK) { unsigned int n = used - lba; if (n > INSTALL_CHUNK) n = INSTALL_CHUNK; if (blk_read(source, lba, copy_buf, n) != 0) { install_fail("source read failed", lba); goto fail; } sum = sum_bytes(sum, copy_buf, n * 512u); if (blk_write(target, lba, copy_buf, 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 (blk_write(target, 0, sector0, 1) != 0) { install_fail("target MBR write failed", 0); goto fail; } io_putchar('\n'); { int fr = blk_flush(target); 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 (blk_read(target, 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"); vfs_remount_device(target); format_busy = 0; io_putstr("install: done; remove the install medium and reboot.\n"); serial_puts(SERIAL_COM1, "install: done\n"); return; fail: vfs_remount_device(target); format_busy = 0; io_putstr("install: target is incomplete; do not boot it\n"); serial_puts(SERIAL_COM1, "install: failed\n"); }