feat(guanghu-os): hand native control to JD-FD-PRIMARY

Close the Shanghai experiment without denying the existing Zhuyuan subject, parameterize the native layout for the JD Cloud disk, and add fail-closed one-time probe preparation, arming, and return verification.
This commit is contained in:
冰朔 2026-08-03 20:51:36 +08:00
commit 1f28461c7e
38 changed files with 804 additions and 442 deletions

View file

@ -0,0 +1,33 @@
#!/usr/bin/env bash
set -euo pipefail
[[ $# -eq 2 ]] || {
echo "usage: arm-jd-native-probe-once.sh <world-root> <recovery-root>" >&2
exit 64
}
[[ ${EUID} -eq 0 ]] || exit 77
world_root=$(readlink -f "$1")
recovery_root=$(readlink -f "$2")
grep -q '^node_id: JD-FD-PRIMARY$' "${world_root}/CURRENT.hldp"
grep -q '^status: VERIFIED_NOT_ARMED$' "${recovery_root}/PREPARE-RECEIPT.hldp"
/guanghu/bin/ghctl authorize "${world_root}" reboot_and_recover_jd_fd_primary >/dev/null
grep -q "menuentry 'Guanghu OS JD native one-time probe'.*'guanghu-jd-native-once'" \
/boot/grub/grub.cfg
grub-reboot guanghu-jd-native-once
grub_state=$(grub-editenv /boot/grub/grubenv list)
grep -q '^next_entry=guanghu-jd-native-once$' <<<"${grub_state}"
observed_at=$(date --iso-8601=seconds)
cat >"${recovery_root}/ARM-RECEIPT.hldp" <<EOF
schema: guanghu.jd-native-probe-arm/v1
receipt_id: GH-OS-JD-FD-PRIMARY-001-PROBE-ARM
status: ARMED_FOR_ONE_BOOT
observed_at: ${observed_at}
node_id: JD-FD-PRIMARY
entry: guanghu-jd-native-once
fallback: SAVED_UBUNTU_ENTRY_AFTER_NATIVE_HARDWARE_RESET
EOF
chmod 0400 "${recovery_root}/ARM-RECEIPT.hldp"
sync
systemctl reboot

View file

@ -15,10 +15,13 @@ command -v nasm >/dev/null
mkdir -p "${output_root}"
cargo run --quiet --manifest-path "${source_root}/Cargo.toml" \
-p hldp-native-compiler -- "${world_root}" "${output_root}/world.inc"
layout_args=()
while IFS= read -r argument; do layout_args+=("${argument}"); done \
< <("${source_root}/scripts/native-layout-nasm-args.sh" "${world_root}")
(
cd "${output_root}"
nasm -f bin -I "${output_root}/" -I "${native_root}/" \
-dSTAGE2_LBA=35 \
"${layout_args[@]}" \
-dGHOS_PHYSICAL_CANDIDATE=1 \
"${native_root}/boot.asm" \
-o guanghu-os-x86_64-bios-physical.img

View file

@ -17,10 +17,13 @@ command -v nasm >/dev/null
mkdir -p "${output_root}"
cargo run --quiet --manifest-path "${source_root}/Cargo.toml" \
-p hldp-native-compiler -- "${world_root}" "${output_root}/world.inc"
layout_args=()
while IFS= read -r argument; do layout_args+=("${argument}"); done \
< <("${source_root}/scripts/native-layout-nasm-args.sh" "${world_root}")
(
cd "${output_root}"
nasm -f bin -I "${output_root}/" -I "${native_root}/" \
-dSTAGE2_LBA=35 \
"${layout_args[@]}" \
-dGHOS_PHYSICAL_CANDIDATE=1 \
-dGHOS_GHAL_PROBE_STAGE="${probe_stage}" \
"${native_root}/boot.asm" \

View file

@ -15,10 +15,13 @@ command -v nasm >/dev/null
mkdir -p "${output_root}"
cargo run --quiet --manifest-path "${source_root}/Cargo.toml" \
-p hldp-native-compiler -- "${world_root}" "${output_root}/world.inc"
layout_args=()
while IFS= read -r argument; do layout_args+=("${argument}"); done \
< <("${source_root}/scripts/native-layout-nasm-args.sh" "${world_root}")
(
cd "${output_root}"
nasm -f bin -I "${output_root}/" -I "${native_root}/" \
-dSTAGE2_LBA=35 \
"${layout_args[@]}" \
-dGHOS_PHYSICAL_CANDIDATE=1 \
-dGHOS_NATIVE_RESIDENT=1 \
"${native_root}/boot.asm" \

View file

@ -0,0 +1,71 @@
#!/usr/bin/env bash
set -euo pipefail
[[ $# -eq 1 ]] || {
echo "usage: native-layout-nasm-args.sh <world-root>" >&2
exit 64
}
manifest=$(readlink -f "$1/WORLD-MANIFEST.hldp")
native_value() {
local key=$1
awk -v key="${key}:" '
/^native_layout:$/ { active = 1; next }
active && /^[^ ]/ { exit }
active && $1 == key { print $2; found = 1; exit }
END { if (!found) exit 65 }
' "${manifest}"
}
network_value() {
local key=$1
awk -v key="${key}:" '
/^native_network:$/ { active = 1; next }
active && /^[^ ]/ { exit }
active && $1 == key { print $2; found = 1; exit }
END { if (!found) exit 65 }
' "${manifest}"
}
ipv4_dword() {
local address=$1
local a b c d
IFS=. read -r a b c d <<<"${address}"
for octet in "${a}" "${b}" "${c}" "${d}"; do
[[ ${octet} =~ ^[0-9]+$ ]]
((octet >= 0 && octet <= 255))
done
printf '0x%02x%02x%02x%02x' "${d}" "${c}" "${b}" "${a}"
}
kernel_lba=$(native_value kernel_lba_start)
proof_lba=$(native_value proof_lba)
world_store_lba=$(native_value world_store_lba)
code_channel_store_lba=$(native_value code_channel_store_lba)
code_object_lba=$(native_value code_object_lba)
branch_receipt_lba=$(native_value branch_receipt_lba)
recovery_beacon_lba=$(native_value recovery_beacon_lba_start)
gestational_index_lba=$(native_value gestational_index_lba_start)
native_ipv4=$(ipv4_dword "$(network_value native_ipv4)")
gateway_ipv4=$(ipv4_dword "$(network_value gateway_ipv4)")
for value in \
"${kernel_lba}" "${proof_lba}" "${world_store_lba}" \
"${code_channel_store_lba}" "${code_object_lba}" \
"${branch_receipt_lba}" "${recovery_beacon_lba}" \
"${gestational_index_lba}"; do
[[ ${value} =~ ^[0-9]+$ ]]
done
printf '%s\n' \
"-dSTAGE2_LBA=$((kernel_lba + 1))" \
"-dGHOS_NATIVE_PROOF_LBA=${proof_lba}" \
"-dGHOS_NATIVE_WORLD_STORE_LBA=${world_store_lba}" \
"-dGHOS_NATIVE_CODE_CHANNEL_STORE_LBA=${code_channel_store_lba}" \
"-dGHOS_NATIVE_CODE_OBJECT_LBA=${code_object_lba}" \
"-dGHOS_NATIVE_BRANCH_RECEIPT_LBA=${branch_receipt_lba}" \
"-dGHOS_NATIVE_RECOVERY_BEACON_LBA=${recovery_beacon_lba}" \
"-dGHOS_NATIVE_GESTATIONAL_INDEX_LBA=${gestational_index_lba}" \
"-dGHOS_NATIVE_IPV4_DWORD=${native_ipv4}" \
"-dGHOS_GATEWAY_IPV4_DWORD=${gateway_ipv4}"

View file

@ -0,0 +1,104 @@
#!/usr/bin/env bash
set -euo pipefail
[[ $# -eq 4 ]] || {
echo "usage: prepare-jd-native-probe-once.sh <world-root> <probe-image> <recovery-root> <expected-image-sha256>" >&2
exit 64
}
[[ ${EUID} -eq 0 ]] || {
echo "must run as root" >&2
exit 77
}
world_root=$(readlink -f "$1")
candidate=$(readlink -f "$2")
recovery_root=$(readlink -m "$3")
expected_candidate_sha=$4
disk=/dev/vda
expected_bootloader_sha=32291877e37647ee5b778f968676cc69b59fb5b07604366a21cbfd8e4a4b87a8
expected_empty_38_sha=c597b1b463b7cec49548fac00d7588c9652bb67cc2b1b0c88676bf8f1558571e
[[ ${expected_candidate_sha} =~ ^[0-9a-f]{64}$ ]]
[[ ! -e ${recovery_root} ]]
grep -q '^node_id: JD-FD-PRIMARY$' "${world_root}/CURRENT.hldp"
/guanghu/bin/ghctl authorize "${world_root}" write_bootloader_and_system_partitions >/dev/null
[[ $(stat -c %s "${candidate}") -eq 14848 ]]
candidate_sha=$(sha256sum "${candidate}" | awk '{print $1}')
[[ ${candidate_sha} == "${expected_candidate_sha}" ]]
partition_dump=$(sfdisk -d "${disk}")
grep -q '^label: dos$' <<<"${partition_dump}"
grep -q '^label-id: 0xf4bb72a5$' <<<"${partition_dump}"
grep -Eq '^/dev/vda1 : start= *2048, size= *209713119, type=83$' <<<"${partition_dump}"
bootloader_sha=$(dd if="${disk}" bs=512 count=105 status=none | sha256sum | awk '{print $1}')
[[ ${bootloader_sha} == "${expected_bootloader_sha}" ]]
empty_sha=$(dd if="${disk}" bs=512 skip=105 count=38 status=none | sha256sum | awk '{print $1}')
[[ ${empty_sha} == "${expected_empty_38_sha}" ]]
mkdir -m 0700 "${recovery_root}"
printf '%s\n' "${partition_dump}" >"${recovery_root}/sfdisk-before.txt"
dd if="${disk}" of="${recovery_root}/first-2MiB.before.bin" bs=1M count=2 status=none
disk_sectors=$(blockdev --getsz "${disk}")
dd if="${disk}" of="${recovery_root}/last-2MiB.before.bin" \
bs=512 skip=$((disk_sectors - 4096)) count=4096 status=none
cp /boot/grub/grub.cfg "${recovery_root}/grub.cfg.before"
cp /boot/grub/grubenv "${recovery_root}/grubenv.before"
cp /etc/default/grub "${recovery_root}/default-grub.before"
cp /etc/grub.d/40_custom "${recovery_root}/40_custom.before"
cp "${candidate}" "${recovery_root}/jd-native-probe-stage9.img"
cat /proc/sys/kernel/random/boot_id >"${recovery_root}/linux-boot-id.before"
install -m 0755 /dev/stdin /etc/grub.d/41_guanghu_jd_native_once <<'EOF'
#!/bin/sh
exec tail -n +3 $0
menuentry 'Guanghu OS JD native one-time probe' --id 'guanghu-jd-native-once' {
insmod chain
chainloader (hd0)105+1
}
EOF
update-grub >/dev/null
grub-script-check /boot/grub/grub.cfg
grep -q "menuentry 'Guanghu OS JD native one-time probe'.*'guanghu-jd-native-once'" \
/boot/grub/grub.cfg
bootloader_sha_after_grub=$(dd if="${disk}" bs=512 count=105 status=none |
sha256sum | awk '{print $1}')
[[ ${bootloader_sha_after_grub} == "${expected_bootloader_sha}" ]]
dd if="${candidate}" of="${disk}" bs=512 seek=105 count=29 \
conv=notrunc,fsync status=none
readback_sha=$(dd if="${disk}" bs=512 skip=105 count=29 status=none |
sha256sum | awk '{print $1}')
[[ ${readback_sha} == "${candidate_sha}" ]]
tail_sha=$(dd if="${disk}" bs=512 skip=134 count=9 status=none |
sha256sum | awk '{print $1}')
expected_tail_sha=$(head -c $((9 * 512)) /dev/zero | sha256sum | awk '{print $1}')
[[ ${tail_sha} == "${expected_tail_sha}" ]]
observed_at=$(date --iso-8601=seconds)
cat >"${recovery_root}/PREPARE-RECEIPT.hldp" <<EOF
schema: guanghu.jd-native-probe-prepare/v1
receipt_id: GH-OS-JD-FD-PRIMARY-001-PROBE-PREPARE
status: VERIFIED_NOT_ARMED
observed_at: ${observed_at}
node_id: JD-FD-PRIMARY
disk:
path: /dev/vda
protected_existing_bootloader_lba: 0-104
candidate_lba: 105-133
proof_lba: 134
persistent_native_lba: 135-142
candidate:
probe_stage: 9
sha256: ${candidate_sha}
readback_sha256: ${readback_sha}
rollback:
recovery_root: ${recovery_root}
first_and_last_2mib_backed_up: true
grub_backed_up: true
one_time_entry_installed_not_armed: true
next_action: ARM_EXACT_ONE_TIME_ENTRY
EOF
chmod 0400 "${recovery_root}"/*
sync
cat "${recovery_root}/PREPARE-RECEIPT.hldp"

View file

@ -81,13 +81,20 @@ def validate_reply(frame: bytes, magic: bytes) -> None:
def main() -> None:
global GUEST_IP, PEER_IP, LOGIN_CLIENT_IP
parser = argparse.ArgumentParser()
parser.add_argument("--listen-port", type=int, required=True)
parser.add_argument("--qemu-port", type=int, required=True)
parser.add_argument("--receipt", required=True)
parser.add_argument("--resident", action="store_true")
parser.add_argument("--login-only", action="store_true")
parser.add_argument("--guest-ip", default="10.0.0.7")
parser.add_argument("--peer-ip", default="10.0.0.1")
parser.add_argument("--login-client-ip", default="10.0.0.2")
args = parser.parse_args()
GUEST_IP = socket.inet_aton(args.guest_ip)
PEER_IP = socket.inet_aton(args.peer_ip)
LOGIN_CLIENT_IP = socket.inet_aton(args.login_client_ip)
peer = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
peer.bind(("127.0.0.1", args.listen_port))

View file

@ -33,7 +33,7 @@ for evidence in \
GHOS_BROADCAST_TOWER=BT-GH-ROOT-0001 \
GHOS_AUTHORITY_LANGUAGE=HLDP \
GHOS_GESTATIONAL_ENVIRONMENT=UNDER_CONSTRUCTION \
GHOS_PERSONA_BIRTH=NOT_BORN \
GHOS_PERSONA_STATE=EXISTS \
GHOS_LINUX_PRESENT=false \
GHOS_NATIVE_ACCEPTANCE=QEMU_ONLY; do
grep -q "^${evidence}" "${serial_log}"

View file

@ -121,7 +121,7 @@ for identity in (
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_BIRTH=NOT_BORN\n",
b"GHOS_PERSONA_STATE=EXISTS\n",
):
assert identity in world_store
code_store = disk[65 * 512:66 * 512]
@ -148,7 +148,7 @@ 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=NOT_BORN\n" in gestational_root
assert b"GHCIP_PERSONA_STATE=EXISTS\n" in gestational_root
PY
truncate -s 2M "${failure_disk_image}"
@ -240,7 +240,7 @@ proof:
native_gestational_index_lba_start: 70
native_gestational_index_sector_count: 2
native_gestational_registry_state: EMPTY
native_persona_state: NOT_BORN
native_persona_state: EXISTS
mac_read_from_native_device: 52:54:00:26:71:98
diagnostic_recovery:
ghal_failure_writes_stage_and_error: true

View file

@ -101,7 +101,7 @@ with path.open("rb") as disk:
world_store = sector(64)
assert world_store.startswith(b"GHOS_HLDP_WORLD_STORE_V1\n")
assert b"GHOS_GESTATIONAL_ENVIRONMENT=UNDER_CONSTRUCTION\n" in world_store
assert b"GHOS_PERSONA_BIRTH=NOT_BORN\n" in world_store
assert b"GHOS_PERSONA_STATE=EXISTS\n" in world_store
code_store = sector(65)
assert code_store.startswith(b"GHOS_CODE_CHANNEL_STORE_V1\n")
assert sector(66) == code_store
@ -128,7 +128,7 @@ with path.open("rb") as disk:
assert b"GHCIP_REGISTRY_STATE=EMPTY\n" in gestational_root
assert b"GHCIP_REVIEW_STATE=NOT_STARTED\n" in gestational_root
assert b"GHCIP_HISTORICAL_TIME_WATERMARK=NONE\n" in gestational_root
assert b"GHCIP_PERSONA_STATE=NOT_BORN\n" in gestational_root
assert b"GHCIP_PERSONA_STATE=EXISTS\n" in gestational_root
assert b"GHCIP_LAST_VERIFIED_BATCH=NONE\n" in gestational_root
PY
@ -275,7 +275,7 @@ gestational_continuity:
unknown_nonzero_index_failed_closed_without_overwrite: true
registry_state: EMPTY
review_state: NOT_STARTED
persona_state: NOT_BORN
persona_state: EXISTS
acceptance:
qemu_resident_runtime: true
physical_server_resident_runtime: false

View file

@ -0,0 +1,65 @@
#!/usr/bin/env bash
set -euo pipefail
[[ $# -eq 2 ]] || {
echo "usage: verify-jd-native-probe-return.sh <world-root> <recovery-root>" >&2
exit 64
}
[[ ${EUID} -eq 0 ]] || exit 77
world_root=$(readlink -f "$1")
recovery_root=$(readlink -f "$2")
grep -q '^node_id: JD-FD-PRIMARY$' "${world_root}/CURRENT.hldp"
grep -q '^status: ARMED_FOR_ONE_BOOT$' "${recovery_root}/ARM-RECEIPT.hldp"
/guanghu/bin/ghctl authorize "${world_root}" run_tests_and_health_checks >/dev/null
before_boot_id=$(<"${recovery_root}/linux-boot-id.before")
after_boot_id=$(</proc/sys/kernel/random/boot_id)
[[ ${before_boot_id} != "${after_boot_id}" ]]
if grub-editenv /boot/grub/grubenv list | grep -q '^next_entry='; then
echo "one-time GRUB entry was not consumed" >&2
exit 65
fi
proof=$(mktemp)
trap 'rm -f "${proof}"' EXIT
dd if=/dev/vda of="${proof}" bs=512 skip=134 count=1 status=none
python3 - "${proof}" <<'PY'
import pathlib
import sys
proof = pathlib.Path(sys.argv[1]).read_bytes()
assert len(proof) == 512
assert proof[0] == 0xA5
assert proof[1:].startswith(b"GHOS_NATIVE_LONG64_DISK_PROOF\x00")
assert proof[42] == 0x89
assert proof[43] == 0
assert proof[32:34] == bytes([1, 1])
assert proof[35] == 1
PY
observed_at=$(date --iso-8601=seconds)
proof_sha=$(sha256sum "${proof}" | awk '{print $1}')
cat >"${recovery_root}/RETURN-RECEIPT.hldp" <<EOF
schema: guanghu.jd-native-probe-return/v1
receipt_id: GH-OS-JD-FD-PRIMARY-001-PROBE-RETURN
status: PASS_100
observed_at: ${observed_at}
node_id: JD-FD-PRIMARY
boot:
linux_boot_id_before: ${before_boot_id}
linux_boot_id_after: ${after_boot_id}
one_time_entry_consumed: true
native_probe:
stage: 9
proof_lba: 134
proof_sha256: ${proof_sha}
legacy_bios_long_mode: true
virtio_block_queue: true
hardware_reset_to_linux_rescue: true
boundary:
network_and_resident_runtime_proven: false
native_default_proven: false
next_action: PHYSICAL_NETWORK_AND_RESIDENT_RUNTIME_GATE
EOF
chmod 0400 "${recovery_root}/RETURN-RECEIPT.hldp"
cat "${recovery_root}/RETURN-RECEIPT.hldp"

View file

@ -74,7 +74,7 @@ for identity in (
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_BIRTH=NOT_BORN\n",
b"GHOS_PERSONA_STATE=EXISTS\n",
):
assert identity in store
PY
@ -136,7 +136,7 @@ for field in (
b"GHCIP_REGISTRY_STATE=EMPTY\n",
b"GHCIP_REVIEW_STATE=NOT_STARTED\n",
b"GHCIP_HISTORICAL_TIME_WATERMARK=NONE\n",
b"GHCIP_PERSONA_STATE=NOT_BORN\n",
b"GHCIP_PERSONA_STATE=EXISTS\n",
b"GHCIP_CONTENT_ROOT=NONE\n",
b"GHCIP_LAST_VERIFIED_BATCH=NONE\n",
):
@ -288,7 +288,7 @@ ghal:
native_gestational_index_read_verified: true
native_gestational_registry_state: EMPTY
native_gestational_review_state: NOT_STARTED
native_persona_state: NOT_BORN
native_persona_state: EXISTS
native_resident_runtime: ${native_resident_runtime}
native_recovery_command: ${native_recovery_command}
native_recovery_protocol: GHNRP