MAOS.git
Publicgit clone https://git.maproj.org/MAOS.gitMAOS.git / installer / prepare-factory.sh
prepare-factory.sh225 lines · 10.4 KiBRaw
| 1 | #!/usr/bin/env bash |
| 2 | # |
| 3 | # prepare-factory.sh — turn a MAOS release image into what the web installer consumes: |
| 4 | # a manifest.json flash plan plus per-partition images, uploaded to R2. |
| 5 | # |
| 6 | # The web installer (ma.vayunmathur.com/os/install) fetches |
| 7 | # https://ota.maproj.org/factory/<device>/latest/manifest.json |
| 8 | # and flashes each step, streaming the referenced images from the same directory. |
| 9 | # |
| 10 | # ── Two input modes ──────────────────────────────────────────────────────────────────── |
| 11 | # 1. INSTALL ZIP (preferred, correct): <device>-install-<build>.zip |
| 12 | # Ships GrapheneOS's *verified* `script.txt` flash sequence plus the whole `super` |
| 13 | # already pre-split into `super_*.img` (sized to the device max-download-size, flashed in |
| 14 | # bootloader mode — the lowest-brick-risk path). When a `script.txt` is present we |
| 15 | # translate it verbatim (installer/script_to_manifest.py) into ordered manifest steps and |
| 16 | # copy the images AS-IS (no re-splitting). This is what phase_publish in build-maos.sh |
| 17 | # passes and what a real release should use. |
| 18 | # |
| 19 | # 2. FACTORY ZIP (legacy fallback): <device>-factory-<build>.zip (or any -P plan.json) |
| 20 | # No script.txt. We build a partition->image map and either follow an explicit -P plan or |
| 21 | # emit a BEST-EFFORT scaffold (prints a warning). Oversized images are split into sparse |
| 22 | # chunks with sparse_split.py. Treat the auto plan as a scaffold, not a guarantee, and |
| 23 | # validate on a device before publishing. See installer/README.md. |
| 24 | # ───────────────────────────────────────────────────────────────────────────────────────── |
| 25 | # |
| 26 | # Usage: |
| 27 | # installer/prepare-factory.sh -d cheetah -b 2026081000 -f <device>-install-<build>.zip \ |
| 28 | # [-P plan.json] [-m 104857600] [-O out/maos-factory] [-B maos] [--no-upload] |
| 29 | # |
| 30 | # R2 upload reuses the same env as build-maos.sh's publish phase: |
| 31 | # R2_ACCESS_KEY_ID, R2_SECRET_ACCESS_KEY, and R2_ACCOUNT_ID (or R2_ENDPOINT). |
| 32 | # |
| 33 | set -euo pipefail |
| 34 | |
| 35 | HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 36 | DEVICE="" ; BUILD="" ; FACTORY="" ; PLAN="" ; MAXB=$((100 * 1024 * 1024)) |
| 37 | OUTDIR="out/maos-factory" ; BUCKET="${R2_BUCKET:-maos}" ; UPLOAD=1 |
| 38 | |
| 39 | usage() { sed -n '2,36p' "$0" >&2; exit 2; } |
| 40 | |
| 41 | while [[ $# -gt 0 ]]; do |
| 42 | case "$1" in |
| 43 | -d) DEVICE="$2"; shift 2 ;; |
| 44 | -b) BUILD="$2"; shift 2 ;; |
| 45 | -f) FACTORY="$2"; shift 2 ;; |
| 46 | -P) PLAN="$2"; shift 2 ;; |
| 47 | -m) MAXB="$2"; shift 2 ;; |
| 48 | -O) OUTDIR="$2"; shift 2 ;; |
| 49 | -B) BUCKET="$2"; shift 2 ;; |
| 50 | --no-upload) UPLOAD=0; shift ;; |
| 51 | -h|--help) usage ;; |
| 52 | *) echo "unknown arg: $1" >&2; usage ;; |
| 53 | esac |
| 54 | done |
| 55 | |
| 56 | [[ -n "$DEVICE" && -n "$BUILD" && -n "$FACTORY" ]] || usage |
| 57 | [[ -f "$FACTORY" ]] || { echo "image zip not found: $FACTORY" >&2; exit 1; } |
| 58 | command -v python3 >/dev/null || { echo "python3 required" >&2; exit 1; } |
| 59 | |
| 60 | WORK="$(mktemp -d)" |
| 61 | trap 'rm -rf "$WORK"' EXIT |
| 62 | STAGE="$OUTDIR/$DEVICE/$BUILD" |
| 63 | mkdir -p "$STAGE" |
| 64 | MANIFEST="$STAGE/manifest.json" |
| 65 | |
| 66 | echo ">> unpacking $FACTORY" |
| 67 | unzip -q -o "$FACTORY" -d "$WORK" |
| 68 | |
| 69 | # Install-zip layout ships script.txt (GrapheneOS's verified flash sequence). Unless the |
| 70 | # caller forces an explicit -P plan, prefer translating that script verbatim. |
| 71 | SCRIPT_TXT="$(find "$WORK" -name script.txt | head -1 || true)" |
| 72 | |
| 73 | if [[ -z "$PLAN" && -n "$SCRIPT_TXT" ]]; then |
| 74 | # ---- Install-zip mode: translate script.txt, copy images as-is (no re-split) ---- |
| 75 | echo ">> install-zip detected (script.txt) — translating GrapheneOS flash sequence" |
| 76 | SRCDIR="$(dirname "$SCRIPT_TXT")" |
| 77 | python3 "$HERE/script_to_manifest.py" \ |
| 78 | --script "$SCRIPT_TXT" --device "$DEVICE" --build "$BUILD" --srcdir "$SRCDIR" \ |
| 79 | > "$MANIFEST" || { echo "script.txt translation failed" >&2; exit 1; } |
| 80 | |
| 81 | echo ">> copying referenced images as-is (super_*.img are already sized; no re-split)" |
| 82 | # Every image the manifest's flash steps reference, from wherever it sits in the zip. |
| 83 | while IFS= read -r img; do |
| 84 | [[ -n "$img" ]] || continue |
| 85 | src="$(find "$WORK" -name "$img" | head -1 || true)" |
| 86 | [[ -n "$src" ]] || { echo "manifest references missing image: $img" >&2; exit 1; } |
| 87 | cp "$src" "$STAGE/$img" |
| 88 | done < <(python3 -c 'import json,sys |
| 89 | m=json.load(open(sys.argv[1])) |
| 90 | for s in m["steps"]: |
| 91 | if s.get("action") == "flash": |
| 92 | print(s["image"])' "$MANIFEST") |
| 93 | else |
| 94 | # ---- Legacy factory-zip mode: partition map + (explicit or best-effort) plan ---- |
| 95 | # Pixel factory images nest the partition images inside image-<device>-<build>.zip. |
| 96 | INNER="$(find "$WORK" -name 'image-*.zip' | head -1 || true)" |
| 97 | if [[ -n "$INNER" ]]; then |
| 98 | unzip -q -o "$INNER" -d "$WORK/inner" |
| 99 | fi |
| 100 | |
| 101 | # Collect a partition -> image-file map. Each *.img's partition is its basename sans .img. |
| 102 | declare -A IMG |
| 103 | add_img() { # add_img <path> |
| 104 | local p; p="$(basename "$1")"; p="${p%.img}" |
| 105 | # Normalize bootloader-/radio-<...> to bare partition names. |
| 106 | case "$p" in |
| 107 | bootloader-*) p="bootloader" ;; |
| 108 | radio-*) p="radio" ;; |
| 109 | esac |
| 110 | IMG["$p"]="$1" |
| 111 | } |
| 112 | for f in "$WORK"/*.img "$WORK"/inner/*.img; do [[ -e "$f" ]] && add_img "$f"; done |
| 113 | |
| 114 | # Stage an image for a partition, splitting into sparse chunks if it exceeds MAXB. |
| 115 | # Emits one or more manifest flash-step JSON objects on stdout. |
| 116 | stage_flash() { # stage_flash <partition> <src-image> |
| 117 | local part="$1" src="$2" sz base steps="" |
| 118 | sz="$(stat -c '%s' "$src")" |
| 119 | base="$(basename "$src")" |
| 120 | if (( sz <= MAXB )); then |
| 121 | cp "$src" "$STAGE/$base" |
| 122 | printf '{"action":"flash","partition":"%s","image":"%s"}' "$part" "$base" |
| 123 | else |
| 124 | echo " splitting $base ($sz bytes) into sparse chunks <= $MAXB" >&2 |
| 125 | local prefix="$STAGE/${part}" |
| 126 | # sparse_split prints each output path (one per line). |
| 127 | local first=1 |
| 128 | while IFS= read -r chunk; do |
| 129 | [[ $first -eq 1 ]] || steps+="," |
| 130 | first=0 |
| 131 | steps+=$(printf '{"action":"flash","partition":"%s","image":"%s"}' \ |
| 132 | "$part" "$(basename "$chunk")") |
| 133 | done < <(python3 "$HERE/sparse_split.py" "$src" "$prefix" --max-bytes "$MAXB") |
| 134 | printf '%s' "$steps" |
| 135 | fi |
| 136 | } |
| 137 | |
| 138 | echo ">> building flash plan" |
| 139 | STEPS_JSON="" |
| 140 | append() { [[ -z "$STEPS_JSON" ]] || STEPS_JSON+=","; STEPS_JSON+="$1"; } |
| 141 | |
| 142 | if [[ -n "$PLAN" ]]; then |
| 143 | # Explicit plan: array of steps. `flash` steps name a partition + an image key that |
| 144 | # must exist in IMG (or a literal file under the factory dir). We still split big ones. |
| 145 | [[ -f "$PLAN" ]] || { echo "plan not found: $PLAN" >&2; exit 1; } |
| 146 | command -v jq >/dev/null || { echo "jq required to use -P plan.json" >&2; exit 1; } |
| 147 | while IFS= read -r line; do |
| 148 | action="$(jq -r '.action' <<<"$line")" |
| 149 | case "$action" in |
| 150 | flash) |
| 151 | part="$(jq -r '.partition' <<<"$line")" |
| 152 | key="$(jq -r '.image' <<<"$line")" |
| 153 | src="${IMG[$key]:-$WORK/$key}" |
| 154 | [[ -e "$src" ]] || src="$WORK/inner/$key" |
| 155 | [[ -e "$src" ]] || { echo "plan references missing image: $key" >&2; exit 1; } |
| 156 | append "$(stage_flash "$part" "$src")" |
| 157 | ;; |
| 158 | *) append "$line" ;; |
| 159 | esac |
| 160 | done < <(jq -c '.[]' "$PLAN") |
| 161 | else |
| 162 | echo " WARNING: no script.txt and no -P plan.json; generating a best-effort plan." >&2 |
| 163 | echo " VERIFY against the device's real flash-all before trusting this." >&2 |
| 164 | [[ -n "${IMG[bootloader]:-}" ]] && { append "$(stage_flash bootloader "${IMG[bootloader]}")"; append '{"action":"reboot-bootloader"}'; } |
| 165 | [[ -n "${IMG[radio]:-}" ]] && { append "$(stage_flash radio "${IMG[radio]}")"; append '{"action":"reboot-bootloader"}'; } |
| 166 | for part in "${!IMG[@]}"; do |
| 167 | case "$part" in bootloader|radio) continue ;; esac |
| 168 | append "$(stage_flash "$part" "${IMG[$part]}")" |
| 169 | done |
| 170 | append '{"action":"erase","partition":"userdata"}' |
| 171 | append '{"action":"reboot"}' |
| 172 | fi |
| 173 | |
| 174 | cat > "$MANIFEST" <<EOF |
| 175 | { |
| 176 | "device": "$DEVICE", |
| 177 | "build": "$BUILD", |
| 178 | "steps": [ $STEPS_JSON ] |
| 179 | } |
| 180 | EOF |
| 181 | fi |
| 182 | |
| 183 | echo ">> manifest: $MANIFEST" |
| 184 | echo " $(find "$STAGE" -type f | wc -l) file(s) staged in $STAGE" |
| 185 | |
| 186 | if [[ "$UPLOAD" -eq 0 ]]; then |
| 187 | echo ">> --no-upload set; skipping R2 upload" |
| 188 | exit 0 |
| 189 | fi |
| 190 | |
| 191 | # ---- Upload to R2 (mirrors build-maos.sh phase_publish) ---- |
| 192 | command -v aws >/dev/null || { echo "aws CLI (v2) required to upload" >&2; exit 1; } |
| 193 | : "${R2_ACCESS_KEY_ID:?set R2_ACCESS_KEY_ID}" |
| 194 | : "${R2_SECRET_ACCESS_KEY:?set R2_SECRET_ACCESS_KEY}" |
| 195 | ENDPOINT="${R2_ENDPOINT:-}" |
| 196 | if [[ -z "$ENDPOINT" ]]; then |
| 197 | : "${R2_ACCOUNT_ID:?set R2_ACCOUNT_ID or R2_ENDPOINT}" |
| 198 | ENDPOINT="https://${R2_ACCOUNT_ID}.r2.cloudflarestorage.com" |
| 199 | fi |
| 200 | export AWS_ACCESS_KEY_ID="$R2_ACCESS_KEY_ID" AWS_SECRET_ACCESS_KEY="$R2_SECRET_ACCESS_KEY" |
| 201 | export AWS_DEFAULT_REGION="auto" |
| 202 | export AWS_REQUEST_CHECKSUM_CALCULATION="when_required" |
| 203 | export AWS_RESPONSE_CHECKSUM_VALIDATION="when_required" |
| 204 | s3() { aws s3 --endpoint-url "$ENDPOINT" "$@"; } |
| 205 | |
| 206 | # Images first (immutable), manifest last (uncacheable) so the installer never sees a |
| 207 | # manifest pointing at a not-yet-uploaded image. Upload to the versioned path, then mirror |
| 208 | # to /latest/ which the installer reads by default. Everything except manifest.json is an |
| 209 | # image (.img/.simg/.bin) and is content-addressed + immutable. |
| 210 | for dest in "factory/$DEVICE/$BUILD" "factory/$DEVICE/latest"; do |
| 211 | echo ">> uploading images -> s3://$BUCKET/$dest/" |
| 212 | for img in "$STAGE"/*; do |
| 213 | [[ -e "$img" ]] || continue |
| 214 | [[ "$(basename "$img")" == "manifest.json" ]] && continue |
| 215 | s3 cp "$img" "s3://$BUCKET/$dest/$(basename "$img")" \ |
| 216 | --content-type "application/octet-stream" \ |
| 217 | --cache-control "public, max-age=31536000, immutable" --only-show-errors |
| 218 | done |
| 219 | echo ">> uploading manifest -> s3://$BUCKET/$dest/manifest.json" |
| 220 | s3 cp "$MANIFEST" "s3://$BUCKET/$dest/manifest.json" \ |
| 221 | --content-type "application/json" \ |
| 222 | --cache-control "no-store, must-revalidate" --only-show-errors |
| 223 | done |
| 224 | |
| 225 | echo ">> done. Installer will read https://ota.maproj.org/factory/$DEVICE/latest/manifest.json" |