boot2

Playing with the boostrap
git clone https://git.ryansepassi.com/git/boot2.git
Log | Files | Refs | README

boot-build-p1.sh (2880B)


      1 #!/bin/sh
      2 ## boot-build-p1.sh — in-container .P1/.M1 -> ELF.
      3 ##
      4 ## Pure transformation. Caller (the Makefile) ensures every fixed-path
      5 ## input below already exists. Only the variable per-call inputs (source,
      6 ## output binary) come in as args.
      7 ##
      8 ## Pipeline:
      9 ##   cat <P1/P1-$ARCH.M1> <src> -> /tmp/combined.M1
     10 ##   M0   /tmp/combined.M1            -> /tmp/prog.hex2
     11 ##   catm /tmp/elf.hex2 /tmp/prog.hex2 -> /tmp/linked.hex2
     12 ##   hex2-0 /tmp/linked.hex2          -> $OUT
     13 ##
     14 ## Stages through /tmp because the stage0 tools do one syscall per byte;
     15 ## virtiofs round-trips would dominate otherwise.
     16 ##
     17 ## Per-call intermediates (combined.M1, prog.hex2, linked.hex2) land at
     18 ## build/$ARCH/.work/<src-without-ext>/, mirroring the source path under
     19 ## the repo root (e.g. tests/P1/00-hello.P1 -> build/aarch64/.work/
     20 ## tests/P1/00-hello/). A one-line sidecar at <out>.workdir records
     21 ## that path so tooling (scripts/disasm-elf.sh) can find the artifacts
     22 ## from the binary alone.
     23 ##
     24 ## Env: ARCH=aarch64|amd64|riscv64
     25 ## Usage: boot-build-p1.sh <src> <out>
     26 
     27 set -eu
     28 
     29 # Per-stage tracing is always on. Stage0 tools (M0, hex2-0) print
     30 # nothing on success and almost nothing on failure, so we narrate which
     31 # step is running, snapshot intermediates to $WORK after each one, and
     32 # print a clear FAIL banner so the user knows where it died.
     33 ARCH_LBL=${ARCH:-?}
     34 CURRENT_STEP=
     35 trap 'rc=$?
     36 if [ "$rc" -ne 0 ] && [ -n "$CURRENT_STEP" ]; then
     37     echo "[p1 $ARCH_LBL] FAIL at: $CURRENT_STEP (exit $rc)" >&2
     38     if [ -n "${WORK:-}" ]; then
     39         echo "[p1 $ARCH_LBL] partial intermediates in $WORK" >&2
     40     fi
     41 fi' EXIT
     42 
     43 trace() {
     44     label=$1; path=$2
     45     sz=$(wc -c < "$path" 2>/dev/null || echo "?")
     46     printf '[p1 %s] %s (%s bytes) %s\n' "$ARCH_LBL" "$label" "$sz" "$path" >&2
     47 }
     48 
     49 step() {
     50     CURRENT_STEP=$1
     51     printf '[p1 %s] >> %s\n' "$ARCH_LBL" "$CURRENT_STEP" >&2
     52 }
     53 
     54 : "${ARCH:?ARCH must be set}"
     55 [ "$#" -eq 2 ] || { echo "usage: ARCH=<arch> $0 <src> <out>" >&2; exit 2; }
     56 
     57 SRC=$1
     58 OUT=$2
     59 
     60 TABLE=P1/P1-$ARCH.M1
     61 ELF_HDR=vendor/seed/$ARCH/ELF.hex2
     62 TOOLS=build/$ARCH/tools
     63 NAME=${SRC%.*}
     64 WORK=build/$ARCH/.work/$NAME
     65 mkdir -p "$WORK" "$(dirname "$OUT")"
     66 
     67 step "cat: P1 table + $SRC -> combined.M1"
     68 cat "$TABLE" "$SRC" > /tmp/combined.M1
     69 cp /tmp/combined.M1 "$WORK/combined.M1"
     70 trace "combined.M1" /tmp/combined.M1
     71 
     72 step "M0: combined.M1 -> prog.hex2"
     73 "$TOOLS/M0" /tmp/combined.M1 /tmp/prog.hex2
     74 cp /tmp/prog.hex2 "$WORK/prog.hex2"
     75 trace "prog.hex2" /tmp/prog.hex2
     76 
     77 step "catm: ELF header + prog.hex2 -> linked.hex2"
     78 cp "$ELF_HDR" /tmp/elf.hex2
     79 "$TOOLS/catm" /tmp/linked.hex2 /tmp/elf.hex2 /tmp/prog.hex2
     80 cp /tmp/linked.hex2 "$WORK/linked.hex2"
     81 trace "linked.hex2" /tmp/linked.hex2
     82 
     83 step "hex2-0: linked.hex2 -> $OUT"
     84 "$TOOLS/hex2-0" /tmp/linked.hex2 /tmp/prog.bin
     85 cp /tmp/prog.bin "$OUT"
     86 chmod 0700 "$OUT"
     87 trace "$OUT" "$OUT"
     88 
     89 printf '%s\n' "$WORK" > "$OUT.workdir"
     90 CURRENT_STEP=