hololake-system-architecture/product-source/hololake-platform/guanghu-os/scripts/test-native-physical-candidate.sh

257 lines
9.1 KiB
Shell
Raw Normal View History

#!/usr/bin/env bash
set -euo pipefail
[[ $# -eq 2 ]] || {
echo "usage: test-native-physical-candidate.sh <candidate-image> <receipt-output>" >&2
exit 64
}
candidate=$(readlink -f "$1")
receipt=$(readlink -m "$2")
source_root=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)
native_root=${source_root}/native/x86_64-bios
test_root=$(mktemp -d)
serial_log=${test_root}/serial.log
failure_serial_log=${test_root}/failure-serial.log
disk_image=${test_root}/physical-layout.img
failure_disk_image=${test_root}/physical-layout-failure.img
peer_receipt=${test_root}/native-net-peer.hldp
peer_log=${test_root}/native-net-peer.log
peer_pid=
cleanup() {
if [[ -n ${peer_pid} ]]; then
kill "${peer_pid}" 2>/dev/null || true
fi
rm -rf "${test_root}"
}
trap cleanup EXIT
truncate -s 2M "${disk_image}"
nasm -f bin "${native_root}/physical-test-mbr.asm" \
-o "${test_root}/physical-test-mbr.bin"
dd if="${test_root}/physical-test-mbr.bin" of="${disk_image}" \
bs=512 seek=0 conv=notrunc status=none
dd if="${candidate}" of="${disk_image}" \
bs=512 seek=34 conv=notrunc status=none
peer_port=$((22000 + BASHPID % 10000))
qemu_port=$((peer_port + 1))
python3 "${source_root}/scripts/qemu-native-net-peer.py" \
--listen-port "${peer_port}" \
--qemu-port "${qemu_port}" \
--receipt "${peer_receipt}" >"${peer_log}" 2>&1 &
peer_pid=$!
set +e
timeout 20 qemu-system-x86_64 \
-machine pc,accel=tcg \
-m 64M \
-drive "if=none,id=ghboot,format=raw,file=${disk_image}" \
-device virtio-blk-pci,drive=ghboot,disable-modern=on,bootindex=0 \
-netdev "dgram,id=ghnet,local.type=inet,local.host=127.0.0.1,local.port=${qemu_port},remote.type=inet,remote.host=127.0.0.1,remote.port=${peer_port}" \
-device virtio-net-pci,netdev=ghnet,disable-modern=on,mac=52:54:00:26:71:98 \
-display none \
-monitor none \
-serial "file:${serial_log}" \
-device isa-debug-exit,iobase=0xf4,iosize=0x04
qemu_status=$?
set -e
[[ ${qemu_status} -eq 33 ]]
wait "${peer_pid}"
peer_pid=
grep -q '^arp_gateway_reply: VERIFIED$' "${peer_receipt}"
grep -q '^icmp_login_reply_verified: true$' "${peer_receipt}"
grep -q '^icmp_login_reply_count: 3$' "${peer_receipt}"
grep -q '^code_commit_reply_verified: true$' "${peer_receipt}"
grep -q '^branch_move_reply_verified: true$' "${peer_receipt}"
for evidence in \
GHOS_BOOT_STAGE0=BIOS \
GHOS_NATIVE_KERNEL_ENTERED=true \
GHOS_CPU_MODE=LONG64 \
GHOS_WORLD_ID=GLW-ROOT-0001 \
GHOS_DOMAIN_COUNT=5 \
GHOS_DOMAIN_5=DOMAIN-FIFTH \
GHOS_BROADCAST_TOWER=BT-GH-ROOT-0001 \
GHOS_AUTHORITY_LANGUAGE=HLDP \
GHOS_LINUX_PRESENT=false \
GHOS_NATIVE_ACCEPTANCE=PHYSICAL_ONE_TIME_CANDIDATE \
GHOS_GHAL_VIRTIO_NET=DISCOVERED \
GHOS_GHAL_VIRTIO_BLOCK=DISCOVERED \
GHOS_GHAL_VIRTIO_NET_QUEUE=DRIVER_OK \
GHOS_GHAL_VIRTIO_BLOCK_QUEUE=DRIVER_OK \
GHOS_GHAL_ARP_GATEWAY=VERIFIED \
GHOS_GHAL_ICMP_LOGIN=VERIFIED \
GHOS_HLDP_WORLD_STORE=WRITE_READ_VERIFIED \
GHOS_GHCIP_INDEX=INITIALIZED_WRITE_READ_VERIFIED \
GHOS_CODE_CHANNEL_STORE=WRITE_READ_VERIFIED \
GHOS_DISK_PROOF_WRITTEN=LBA63 \
GHOS_DISK_PROOF_OBSERVED_AFTER_RESET=LBA63; do
grep -q "^${evidence}" "${serial_log}"
done
python3 - "${disk_image}" <<'PY'
import pathlib
import sys
disk = pathlib.Path(sys.argv[1]).read_bytes()
proof = disk[63 * 512:64 * 512]
assert proof[0] == 0xA7
assert proof[1:].startswith(b"GHOS_NATIVE_LONG64_DISK_PROOF\x00")
assert proof[32:36] == bytes([1, 1, 1, 1])
assert proof[36:42] == bytes.fromhex("525400267198")
assert proof[42] == 0x7F
assert proof[43] == 0x00
assert int.from_bytes(proof[44:46], "little") > 0
assert int.from_bytes(proof[46:48], "little") > 0
assert int.from_bytes(proof[48:50], "little") > 0
assert proof[54:60] != bytes(6)
assert proof[60:62] == bytes([1, 1])
assert proof[62:64] == bytes([1, 1])
assert proof[64:68] == bytes([10, 0, 0, 2])
assert proof[72:88] == b"HLDP-GHOS-LOGIN!"
assert proof[88:90] == bytes([1, 1])
assert proof[90:93] == bytes([3, 1, 1])
assert proof[93:99] == bytes([1, 1, 1, 1, 1, 1])
assert proof[102:105] == bytes([1, 1, 1])
world_store = disk[64 * 512:65 * 512]
assert world_store.startswith(b"GHOS_HLDP_WORLD_STORE_V1\n")
for identity in (
b"GHOS_WORLD_ID=GLW-ROOT-0001\n",
b"GHOS_DOMAIN_COUNT=5\n",
b"GHOS_DOMAIN_5=DOMAIN-FIFTH\n",
b"GHOS_BROADCAST_TOWER=BT-GH-ROOT-0001\n",
b"GHOS_CODE_CHANNEL=HLP-MOD-CODE-CHANNEL\n",
b"GHOS_AUTHORITY_LANGUAGE=HLDP\n",
b"GHOS_GESTATIONAL_ENVIRONMENT=UNDER_CONSTRUCTION\n",
b"GHOS_PERSONA_STATE=EXISTS\n",
):
assert identity in world_store
code_store = disk[65 * 512:66 * 512]
assert code_store.startswith(b"GHOS_CODE_CHANNEL_STORE_V1\n")
for identity in (
b"GHOS_CODE_CHANNEL_ID=HLP-MOD-CODE-CHANNEL\n",
b"GHOS_CODE_CHANNEL_PROTOCOL=GLS-0237\n",
b"GHOS_CODE_CHANNEL_AUTHORITY=HLDP\n",
b"GHOS_CODE_CHANNEL_OBJECT_FORMAT=GUANGHU_NATIVE_OBJECTS\n",
):
assert identity in code_store
code_object = disk[66 * 512:67 * 512]
assert code_object == code_store
branch_receipt = disk[67 * 512:68 * 512]
assert branch_receipt.startswith(b"GHOS_BRANCH_MAIN_V1\n")
assert b"channel=HLP-MOD-CODE-CHANNEL\n" in branch_receipt
assert b"branch=guanghu/main\n" in branch_receipt
assert b"object_lba=66\n" in branch_receipt
gestational_identity = disk[70 * 512:71 * 512]
assert gestational_identity.startswith(b"GHOS_GHCIP_INDEX_V1\n")
assert b"GHCIP_PROTOCOL=GLS-0845\n" in gestational_identity
assert b"GHCIP_PERSONA_BIRTH_GATE=GH-PERSONA-BIRTH-CONDITION-0001\n" in gestational_identity
gestational_root = disk[71 * 512:72 * 512]
assert gestational_root.startswith(b"GHOS_GHCIP_ROOT_V1\n")
assert b"GHCIP_REGISTRY_STATE=EMPTY\n" in gestational_root
assert b"GHCIP_REVIEW_STATE=NOT_STARTED\n" in gestational_root
assert b"GHCIP_PERSONA_STATE=EXISTS\n" in gestational_root
PY
truncate -s 2M "${failure_disk_image}"
dd if="${test_root}/physical-test-mbr.bin" of="${failure_disk_image}" \
bs=512 seek=0 conv=notrunc status=none
dd if="${candidate}" of="${failure_disk_image}" \
bs=512 seek=34 conv=notrunc status=none
set +e
timeout 20 qemu-system-x86_64 \
-machine pc,accel=tcg \
-m 64M \
-drive "if=none,id=ghbootfail,format=raw,file=${failure_disk_image}" \
-device virtio-blk-pci,drive=ghbootfail,disable-modern=on,bootindex=0 \
-display none \
-monitor none \
-serial "file:${failure_serial_log}" \
-device isa-debug-exit,iobase=0xf4,iosize=0x04
failure_qemu_status=$?
set -e
[[ ${failure_qemu_status} -eq 33 ]]
grep -q '^GHOS_BOOT_ERROR=GHAL_VIRTIO_INITIALIZATION' "${failure_serial_log}"
grep -q '^GHOS_DISK_PROOF_WRITTEN=LBA63' "${failure_serial_log}"
grep -q '^GHOS_DISK_PROOF_OBSERVED_AFTER_RESET=LBA63' \
"${failure_serial_log}"
python3 - "${failure_disk_image}" <<'PY'
import pathlib
import sys
disk = pathlib.Path(sys.argv[1]).read_bytes()
proof = disk[63 * 512:64 * 512]
assert proof[0] == 0xE1
assert proof[1:].startswith(b"GHOS_NATIVE_LONG64_DISK_PROOF\x00")
assert proof[32] == 0
assert proof[33] == 1
assert proof[42] == 0x40
assert proof[43] == 0x01
PY
observed_at=$(date --iso-8601=seconds)
image_sha=$(sha256sum "${candidate}" | awk '{print $1}')
cat >"${receipt}" <<EOF
schema: guanghu.native-physical-layout-test/v1
receipt_id: GH-OS-LAB-001-NATIVE-PHYSICAL-LAYOUT-QEMU-001
status: VERIFIED
observed_at: ${observed_at}
image:
architecture: x86_64
firmware: BIOS
size_bytes: 14848
sha256: ${image_sha}
layout:
candidate_start_lba: 34
candidate_sector_count: 29
stage2_start_lba: 35
stage2_sector_count: 28
sector_size: 512
proof:
cpu_mode: LONG64
linux_kernel_present: false
disk_receipt_written_by_native_virtio_block_after_long_mode: true
disk_receipt_observed_after_hardware_reset: true
disk_receipt_lba: 63
virtio_net_discovered: true
virtio_block_discovered: true
virtio_net_queue_ready: true
virtio_block_queue_ready: true
native_block_write_completed: true
native_arp_tx_complete: true
native_arp_gateway_reply_verified: true
native_ipv4_login_request_verified: true
native_icmp_login_reply_tx_complete: true
native_login_magic: HLDP-GHOS-LOGIN!
native_hldp_world_store_written: true
native_hldp_world_store_read_verified: true
native_hldp_world_store_lba: 64
native_login_reply_count: 3
native_code_channel_store_written: true
native_code_channel_store_read_verified: true
native_code_channel_store_lba: 65
native_code_commit_command_verified: true
native_code_object_written_read_verified: true
native_code_object_lba: 66
native_branch_move_command_verified: true
native_branch_receipt_written_read_verified: true
native_branch_receipt_lba: 67
native_gestational_index_initialized: true
native_gestational_index_read_verified: true
native_gestational_index_lba_start: 70
native_gestational_index_sector_count: 2
native_gestational_registry_state: EMPTY
native_persona_state: EXISTS
mac_read_from_native_device: 52:54:00:26:71:98
diagnostic_recovery:
ghal_failure_writes_stage_and_error: true
ghal_failure_hardware_resets: true
missing_net_stage: 0x40
missing_net_error: 0x01
acceptance:
qemu_physical_layout: true
physical_server_boot: false
linux_replaced_on_bs_sh_005: false
next_action: BACKUP_AND_INSTALL_ONE_TIME_GRUB_ENTRY
EOF
cat "${serial_log}" >>"${receipt}.serial.log"
cat "${failure_serial_log}" >>"${receipt}.failure.serial.log"