|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Extension: jethub-burn |
| 3 | +# Automatically converts Armbian .img into burn image after build |
| 4 | + |
| 5 | +# Download and prepare tools |
| 6 | +function bootstrap_tools() { |
| 7 | + local repo_url="https://github.com/jethome-iot/jethome-tools.git" |
| 8 | + local ref="commit:87be932dceb6135c99dfc5a105a6345eff954f2c" |
| 9 | + local name="jethub-burn" |
| 10 | + local base="${SRC}/cache/sources/${name}" |
| 11 | + |
| 12 | + display_alert "jethub-burn" "Fetching jethome-tools (${ref})..." "info" |
| 13 | + fetch_from_repo "${repo_url}" "${name}" "${ref}" |
| 14 | + |
| 15 | + local packer_path |
| 16 | + packer_path="$(find "${base}" -maxdepth 12 -type f -path '*/tools/aml_image_v2_packer_new' -print -quit)" |
| 17 | + [[ -n "${packer_path}" ]] || exit_with_error "aml_image_v2_packer_new not found under ${base}" |
| 18 | + |
| 19 | + declare -g TOOLS_DIR="$(dirname "$(dirname "${packer_path}")")" |
| 20 | + declare -g PACKER="${packer_path}" |
| 21 | + declare -g BINS_DIR="${TOOLS_DIR}/bins" |
| 22 | + declare -g DTS_DIR="${TOOLS_DIR}/dts" |
| 23 | + declare -g DTBTOOLS_DIR="${TOOLS_DIR}/dtbtools" |
| 24 | + declare -g IMAGE_CFG="${BINS_DIR}/image.armbian.cfg" |
| 25 | + |
| 26 | + display_alert "jethub-burn" "Tools ready (packer: ${PACKER})" "ok" |
| 27 | +} |
| 28 | + |
| 29 | +# Build burn image |
| 30 | +function make_burn__run() { |
| 31 | + local -r input_img="$1" |
| 32 | + local -r board="$2" |
| 33 | + local -r dts_name="$3" |
| 34 | + local -r bins_subdir="$4" |
| 35 | + local -r uboot_bin="$5" |
| 36 | + |
| 37 | + local -r bins="${BINS_DIR}/${bins_subdir}" |
| 38 | + local tmpdir; tmpdir="$(mktemp -d)" |
| 39 | + trap 'rm -rf "$tmpdir"' RETURN |
| 40 | + |
| 41 | + local -r OUT_IMG="${DESTIMG}/${version}.burn.img" |
| 42 | + |
| 43 | + display_alert "make_burn" "Building burn image for ${board}" "info" |
| 44 | + |
| 45 | + # build _aml_dtb.PARTITION |
| 46 | + mkdir -p "$tmpdir/dts" |
| 47 | + cp "$DTS_DIR/$dts_name" "$tmpdir/dts/$dts_name" |
| 48 | + cp "$DTS_DIR/partition_arm.dtsi" "$tmpdir/dts/partition_arm.dtsi" |
| 49 | + sed -i 's#partition\.dtsi#partition_arm.dtsi#g' "$tmpdir/dts/$dts_name" |
| 50 | + |
| 51 | + cpp -nostdinc -I "$DTS_DIR" -I "$DTS_DIR/include" -undef -x assembler-with-cpp "$tmpdir/dts/$dts_name" "$tmpdir/${dts_name}.pre" |
| 52 | + dtc -I dts -O dtb -p 0x1000 -qqq "$tmpdir/${dts_name}.pre" -o "$tmpdir/board.dtb" |
| 53 | + |
| 54 | + cc -O2 -o "$tmpdir/dtbTool" "$DTBTOOLS_DIR/dtbTool.c" |
| 55 | + "$tmpdir/dtbTool" -o "$tmpdir/_aml_dtb.PARTITION" "$tmpdir" |
| 56 | + display_alert "make_burn" "_aml_dtb.PARTITION built" "info" |
| 57 | + |
| 58 | + display_alert "make_burn" "Extracting partitions (losetup -P)..." "info" |
| 59 | + local loopdev |
| 60 | + loopdev="$(losetup --find --show -P "$input_img")" || exit_with_error "losetup failed for $input_img" |
| 61 | + trap 'losetup -d "$loopdev" 2>/dev/null || true; rm -rf "$tmpdir"' RETURN |
| 62 | + |
| 63 | + local i=1 found=0 |
| 64 | + for p in $(ls -1 "${loopdev}"p* 2>/dev/null | sort -V); do |
| 65 | + found=1 |
| 66 | + display_alert "make_burn" "Copying $(basename "$p") → part-$i.img" "info" |
| 67 | + dd if="$p" of="$tmpdir/part-$i.img" bs=4M status=none || exit_with_error "dd failed for $p" |
| 68 | + i=$((i+1)) |
| 69 | + done |
| 70 | + [[ $found -eq 1 ]] || exit_with_error "No partitions detected on $input_img" |
| 71 | + |
| 72 | + cp "$bins/platform.conf" "$tmpdir/" |
| 73 | + cp "$bins/DDR.USB" "$tmpdir/" |
| 74 | + cp "$bins/UBOOT.USB" "$tmpdir/" |
| 75 | + cp "$IMAGE_CFG" "$tmpdir/image.cfg" |
| 76 | + cp "$uboot_bin" "$tmpdir/u-boot.bin" |
| 77 | + |
| 78 | + display_alert "make_burn" "Packing burn image..." "info" |
| 79 | + "$PACKER" -r "$tmpdir/image.cfg" "$tmpdir" "$OUT_IMG" || exit_with_error "Image pack FAILED" |
| 80 | + |
| 81 | + [[ -f "$OUT_IMG" ]] || exit_with_error "Burn image not produced" |
| 82 | + display_alert "make_burn" "Burn image created: $(basename "$OUT_IMG")" "ok" |
| 83 | +} |
| 84 | + |
| 85 | +function post_build_image__900_jethub_burn() { |
| 86 | + [[ -z $version ]] && exit_with_error "version is not set" |
| 87 | + |
| 88 | + local -r original_image_file="${DESTIMG}/${version}.img" |
| 89 | + [[ -f "$original_image_file" ]] || exit_with_error "Original image not found: $original_image_file" |
| 90 | + |
| 91 | + local dts_name bins_subdir |
| 92 | + case "${BOARD}" in |
| 93 | + jethubj80) dts_name="meson-gxl-s905w-jethome-jethub-j80.dts"; bins_subdir="j80" ;; |
| 94 | + jethubj100) dts_name="meson-axg-jethome-jethub-j100.dts"; bins_subdir="j100" ;; |
| 95 | + jethubj200) dts_name="meson-sm1-jethome-jethub-j200.dts"; bins_subdir="j200" ;; |
| 96 | + *) exit_with_error "Unsupported board: ${BOARD} (supported: j80, j100, j200)";; |
| 97 | + esac |
| 98 | + |
| 99 | + display_alert "Converting image to Amlogic burn format" "jethub-burn :: ${BOARD}" "info" |
| 100 | + |
| 101 | + bootstrap_tools |
| 102 | + |
| 103 | + local -r debs_dir="${SRC}/output/debs" |
| 104 | + local uboot_deb uboot_bin |
| 105 | + uboot_deb=$(find "${debs_dir}" -maxdepth 1 -type f -name "linux-u-boot-${BOARD}-*.deb" | sort -V | tail -n1) |
| 106 | + [[ -n "${uboot_deb}" ]] || exit_with_error "u-boot deb not found for ${BOARD}" |
| 107 | + local tmp_dir; tmp_dir="$(mktemp -d)" |
| 108 | + trap 'rm -rf "${tmp_dir}"' RETURN |
| 109 | + mkdir -p "${tmp_dir}/deb-uboot" |
| 110 | + dpkg -x "${uboot_deb}" "${tmp_dir}/deb-uboot" |
| 111 | + uboot_bin=$(find "${tmp_dir}/deb-uboot/usr/lib" -type f -name "u-boot.nosd.bin" | head -n1) |
| 112 | + [[ -n "${uboot_bin}" ]] || exit_with_error "u-boot.nosd.bin not found in deb" |
| 113 | + |
| 114 | + make_burn__run "${original_image_file}" "${BOARD}" "${dts_name}" "${bins_subdir}" "${uboot_bin}" |
| 115 | + |
| 116 | + display_alert "jethub-burn" "Burn image prepared (pre-checksum stage)" "ok" |
| 117 | +} |
0 commit comments