#!/bin/sh
# ============================================================
# c2h_getkey — initramfs C2H DMA key retrieval (zero driver)
#
# Reads the disk encryption key from FPGA board DDR via PCIe
# BAR0 C2H SGDMA engine. Uses only busybox devmem — no kernel
# driver required.
#
# Prerequisites:
#   1. Board already loaded XDMA bitstream (fpga_manager)
#   2. Board ARM wrote key to DDR at KEY_DDR_ADDR
#   3. Host kernel cmdline: memmap=8K\\$0x10000000
#      (reserves 8 KB at 0x10000000 for descriptor + key buffer)
#   4. busybox with devmem, sleep, xxd
#
# Reference: C2H验证结论报告.md, GRUB_C2H密钥方案.md §5
# ============================================================

# --- PCIe BAR0 (fixed by enumeration) ------------------------
BAR0=0xa2100000

# --- C2H engine registers (BAR0+0x1000 + SGDMA offset 0x4000)
CTRL=$((BAR0 + 0x1004))       # engine control
COMP_CNT=$((BAR0 + 0x1048))   # completed descriptor count
FD_LO=$((BAR0 + 0x5080))      # first_desc_lo
FD_HI=$((BAR0 + 0x5084))      # first_desc_hi
FD_ADJ=$((BAR0 + 0x5088))     # first_desc_adjacent

# --- Reserved host physical memory (memmap=8K$0x10000000) ---
DESC_PHYS=0x10000000           # descriptor (32 bytes)
KEY_BUF=0x10000100             # key buffer (256 bytes)

# --- Board DDR key address -----------------------------------
KEY_DDR_ADDR=0x1FF80000
KEY_LEN=32                     # minimum 16 (C2H alignment constraint)

# --- Stop C2H engine -----------------------------------------
busybox devmem $CTRL 32 0x0
sleep 0.1

# --- Build descriptor in host physical memory ----------------
# Descriptor format (32 bytes, XDMA v4.1):
#   +0x00  control  (DESC_MAGIC=0xAD4B0000 | STOP=0x1)
#   +0x04  transfer length in bytes
#   +0x08  src_addr_lo  (board DDR physical address)
#   +0x0C  src_addr_hi
#   +0x10  dst_addr_lo  (host physical buffer)
#   +0x14  dst_addr_hi
#   +0x18  next_desc_lo (0 = end of chain)
#   +0x1C  next_desc_hi
busybox devmem $((DESC_PHYS + 0x00)) 32 0xAD4B0001  # magic + STOP
busybox devmem $((DESC_PHYS + 0x04)) 32 $KEY_LEN
busybox devmem $((DESC_PHYS + 0x08)) 32 $KEY_DDR_ADDR
busybox devmem $((DESC_PHYS + 0x0C)) 32 0x0
busybox devmem $((DESC_PHYS + 0x10)) 32 $KEY_BUF
busybox devmem $((DESC_PHYS + 0x14)) 32 0x0
busybox devmem $((DESC_PHYS + 0x18)) 32 0x0
busybox devmem $((DESC_PHYS + 0x1C)) 32 0x0

# --- Start engine --------------------------------------------
# ctrl = RUN_STOP(bit0) + DESC_FETCH(bit1) +
#        all error enables (bits 9-13, 19-23) = 0x00F83E1F
busybox devmem $FD_LO 32 $DESC_PHYS
busybox devmem $FD_HI 32 0x0
busybox devmem $FD_ADJ 32 0x0
busybox devmem $CTRL 32 0x00F83E1F

# --- Poll completed descriptor count -------------------------
TIMEOUT=100
while [ "$(busybox devmem $COMP_CNT 32)" = "0x00000000" ]; do
    sleep 0.1
    TIMEOUT=$((TIMEOUT - 1))
    if [ $TIMEOUT -le 0 ]; then
        echo "ERROR: C2H DMA timeout"
        exit 1
    fi
done

# --- Read key from host buffer into file ---------------------
# Read KEY_LEN bytes word by word, convert from hex to binary.
# busybox devmem outputs hex like "0x12345678".
rm -f /tmp/disk_key
for i in $(seq 0 4 $((KEY_LEN - 4))); do
    busybox devmem $((KEY_BUF + i)) 32 | sed 's/^0x//' >> /tmp/disk_key_hex
done
xxd -r -p /tmp/disk_key_hex /tmp/disk_key
rm -f /tmp/disk_key_hex

# --- Unlock LUKS rootfs ---------------------------------------
# (Commented out for P0 validation — uncomment after key is
#  provisioned and rootfs is encrypted.)
# cryptsetup luksOpen /dev/sda2 root --key-file=/tmp/disk_key
# mount /dev/mapper/root /newroot
# rm -f /tmp/disk_key

echo "c2h_getkey: key read complete ($KEY_LEN bytes)"
exit 0
