| 1 | // v run tests/busybox/boot.v INSTALLED_ROOT VINIX_ISO NEW_OUTPUT_DIRECTORY [MODE] |
| 2 | // For ARM provide AARCH64_EFI_CODE and AARCH64_EFI_VARS, and CROSS_COMPILE for a cross GCC. |
| 3 | module main |
| 4 | |
| 5 | import crypto.sha256 |
| 6 | import os |
| 7 | |
| 8 | fn run(argv []string) ! { |
| 9 | result := os.exec(argv) |
| 10 | if result.exit_code != 0 { |
| 11 | return error('${argv}: exit ${result.exit_code}\n${result.output}') |
| 12 | } |
| 13 | } |
| 14 | |
| 15 | fn boot_image(root string, iso string, output string, mode string) ! { |
| 16 | shell := mode == '--shell' |
| 17 | arm := mode in ['--aarch64-musl', '--aarch64-userland'] |
| 18 | userland := mode in ['--userland', '--aarch64-userland'] |
| 19 | musl := mode in ['--musl', '--aarch64-musl'] |
| 20 | machine := if arm { 'aarch64' } else { 'x86_64' } |
| 21 | loader := '/lib/ld-musl-${machine}.so.1' |
| 22 | if root == '/' || output == root || output.starts_with(root + '/') { |
| 23 | return error('place the output outside a separate BusyBox package root') |
| 24 | } |
| 25 | digest := sha256.sum(os.read_bytes(os.join_path(root, 'bin/busybox'))!).hex() |
| 26 | helper := if userland { |
| 27 | 'userland-check.sh' |
| 28 | } else if shell { |
| 29 | 'serial-line' |
| 30 | } else { |
| 31 | 'busybox-probe' |
| 32 | } |
| 33 | if os.exists(os.join_path(root, 'etc/inittab')) || os.exists(os.join_path(root, helper)) { |
| 34 | return error('use a clean package root without a test helper or inittab') |
| 35 | } |
| 36 | os.mkdir(output)! |
| 37 | boot := os.join_path(output, 'image/boot') |
| 38 | additions := os.join_path(output, 'fixture') |
| 39 | os.mkdir_all(boot)! |
| 40 | os.mkdir_all(os.join_path(additions, 'etc'))! |
| 41 | inittab := if shell { |
| 42 | "com1::respawn:/bin/sh -c '" + r'while printf "vinix> " && line=$(/serial-line); do eval "$line"; done' + "'" |
| 43 | } else if userland { |
| 44 | console := if arm { '/dev/console' } else { '/dev/com1' } |
| 45 | "::sysinit:/bin/sh -c 'exec /bin/sh /userland-check.sh >${console} 2>&1'" |
| 46 | } else { |
| 47 | '::sysinit:/busybox-probe' |
| 48 | } |
| 49 | os.write_file(os.join_path(additions, 'etc/inittab'), inittab + '\n')! |
| 50 | mut loader_digest := '' |
| 51 | if userland { |
| 52 | os.cp(os.join_path(os.dir(@FILE), '..', 'userland', 'check.sh'), os.join_path(additions, helper))! |
| 53 | } else { |
| 54 | source := if shell { 'serial-line.v' } else { 'guest.v' } |
| 55 | mut compiler := [@VEXE, '-prod', '-gc', 'none'] |
| 56 | if musl { |
| 57 | loader_digest = sha256.sum(os.read_bytes(os.join_path(root, loader.trim_left('/')))!).hex() |
| 58 | specs := os.join_path(root, 'usr/lib/musl-gcc-sysroot.specs') |
| 59 | target_cc := if arm { os.getenv('CROSS_COMPILE') + 'gcc' } else { 'gcc' } |
| 60 | if arm { compiler << ['-os', 'linux', '-arch', 'arm64'] } |
| 61 | compiler << ['-musl', '-d', 'packaged_musl', '-cc', target_cc, '-cflags', |
| 62 | '--sysroot=' + os.quoted_path(root) + ' -specs=' + os.quoted_path(specs)] |
| 63 | } else { |
| 64 | compiler << ['-cc', 'musl-gcc', '-cflags', '-static'] |
| 65 | } |
| 66 | run([...compiler, '-o', os.join_path(additions, helper), os.join_path(os.dir(@FILE), source)])! |
| 67 | } |
| 68 | boot_files := if arm { |
| 69 | ['boot/limine-uefi-cd.bin', 'EFI/BOOT/BOOTAA64.EFI'] |
| 70 | } else { |
| 71 | ['boot/limine-bios.sys', 'boot/limine-bios-cd.bin'] |
| 72 | } |
| 73 | run(['bsdtar', '-xf', iso, '-C', os.dir(boot), 'boot/vinix', ...boot_files])! |
| 74 | os.write_file(os.join_path(boot, 'limine.conf'), 'timeout: 0\nserial: yes\n/BusyBox\n' + |
| 75 | ' protocol: limine\n kernel_path: boot():/boot/vinix\n resolution: 1024x768x32\n' + |
| 76 | ' kaslr: no\n module_path: boot():/boot/initramfs.tar.gz\n cmdline: vinix.disk=live\n')! |
| 77 | run(['tar', '--format=ustar', '--owner=0', '--group=0', '-czf', os.join_path(boot, |
| 78 | 'initramfs.tar.gz'), '-C', root, '.', '-C', additions, './etc', './' + helper])! |
| 79 | test_iso := os.join_path(output, if shell { 'busybox-shell.iso' } else { 'busybox.iso' }) |
| 80 | cd_boot := if arm { |
| 81 | ['-e', 'boot/limine-uefi-cd.bin', '-no-emul-boot'] |
| 82 | } else { |
| 83 | ['-b', 'boot/limine-bios-cd.bin', '-no-emul-boot', '-boot-load-size', '4', '-boot-info-table'] |
| 84 | } |
| 85 | run(['xorriso', '-as', 'mkisofs', '-R', '-r', ...cd_boot, '-o', test_iso, os.dir(boot)])! |
| 86 | if shell { |
| 87 | println('Interactive serial shell image: ${test_iso}') |
| 88 | return |
| 89 | } |
| 90 | log := os.join_path(output, 'serial.log') |
| 91 | mut emulator := if arm { |
| 92 | code := os.getenv('AARCH64_EFI_CODE') |
| 93 | vars := os.join_path(output, 'vars.fd') |
| 94 | os.cp(os.getenv('AARCH64_EFI_VARS'), vars)! |
| 95 | ['qemu-system-aarch64', '-machine', 'virt', '-device', 'ramfb', '-drive', |
| 96 | 'if=pflash,format=raw,readonly=on,file=' + code, '-drive', |
| 97 | 'if=pflash,format=raw,file=' + vars, '-device', 'virtio-scsi-pci,id=scsi0', '-drive', |
| 98 | 'if=none,id=cd0,media=cdrom,readonly=on,file=' + test_iso, '-device', |
| 99 | 'scsi-cd,drive=cd0,bus=scsi0.0,bootindex=1'] |
| 100 | } else { |
| 101 | ['qemu-system-x86_64', '-machine', 'q35', '-vga', 'std', '-cdrom', test_iso, '-boot', 'd'] |
| 102 | } |
| 103 | memory := if userland { |
| 104 | '512' |
| 105 | } else if arm { |
| 106 | '384' |
| 107 | } else { |
| 108 | '1024' |
| 109 | } |
| 110 | emulator << ['-accel', 'tcg', '-cpu', 'max', '-m', memory, '-smp', '2', '-nic', 'none', '-display', |
| 111 | 'none', '-monitor', 'none', '-serial', 'file:' + log, '-no-reboot'] |
| 112 | run(['timeout', '120s', ...emulator])! |
| 113 | lines := os.read_lines(log)!.map(it.trim_space()) |
| 114 | if userland { |
| 115 | if 'VPAC USERLAND GUEST PASS' !in lines { |
| 116 | return error('userland guest verification failed; see ${log}') |
| 117 | } |
| 118 | println('Vinix ${machine} userland checks passed. See ${log}') |
| 119 | return |
| 120 | } |
| 121 | if '${digest} /bin/busybox' !in lines || 'VPAC BUSYBOX GUEST PASS' !in lines { |
| 122 | return error('guest verification failed; see ${log}') |
| 123 | } |
| 124 | if musl && '${loader_digest} ${loader}' !in lines { |
| 125 | return error('guest musl differs from the packaged loader; see ${log}') |
| 126 | } |
| 127 | println('Vinix booted the installed BusyBox ${digest}. See ${log}') |
| 128 | } |
| 129 | |
| 130 | fn main() { |
| 131 | mode := if os.args.len == 5 { os.args[4] } else { '' } |
| 132 | if os.args.len != 4 && mode !in ['--shell', '--musl', '--aarch64-musl', '--userland', |
| 133 | '--aarch64-userland'] { |
| 134 | eprintln('usage: boot.v INSTALLED_ROOT VINIX_ISO NEW_OUTPUT_DIRECTORY [--shell|--musl|--aarch64-musl|--userland|--aarch64-userland]') |
| 135 | exit(2) |
| 136 | } |
| 137 | output := os.abs_path(os.args[3]) |
| 138 | new_dir := os.join_path(os.real_path(os.dir(output)), os.file_name(output)) |
| 139 | boot_image(os.real_path(os.args[1]), os.abs_path(os.args[2]), new_dir, mode) or { |
| 140 | eprintln(err) |
| 141 | exit(1) |
| 142 | } |
| 143 | } |
| 144 | |