Files
makronix/mobile/tools/mkbootimg.sh
T
lotherkandClaude Sonnet 5.5 73c9de51a6 licence: the CNCL line in every file Makronix wrote (tools, docs, www, mobile, top level)
consmirror.c: the BSD notice is replaced (Makronix's own file).  Files with
changes of other sessions pending are left for their commit.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-14 11:50:26 +00:00

120 lines
3.5 KiB
Bash
Executable File

#!/bin/sh
# License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
#
# Pack an Android boot image, header version 2, in POSIX sh.
#
# No mkbootimg binary: the one pmbootstrap ships is musl-linked and does not
# run on the host, and tools/ is POSIX sh (AGENTS.md). The header layout is
# AOSP's boot_img_hdr_v2; the id field is a SHA-1 over kernel, ramdisk, second,
# recovery dtbo and dtb as AOSP's mkbootimg computes it. For hotdogb the
# result is byte for byte the image that boots on the phone (docs/BOOT.md).
#
# Usage: mkbootimg.sh DEVICEDIR KERNEL INITRAMFS OUT
set -eu
. "$(dirname "$0")/lib.sh"
[ $# -eq 4 ] || { echo "usage: $0 DEVICEDIR KERNEL INITRAMFS OUT" >&2; exit 2; }
load_device "$1"
kernel=$2 ramdisk=$3 out=$4
[ "$BOOT_HEADER_VERSION" = 2 ] || die "only boot header v2 is implemented, device wants $BOOT_HEADER_VERSION"
# the DTB: built in the kernel tree (KERNEL_DTB, below arch/<arch>/boot/dts,
# next to the Image) or a binary in the device directory (DTB)
if [ -n "${KERNEL_DTB:-}" ]; then
dtb=$(dirname "$kernel")/dts/$KERNEL_DTB
else
dtb=${DTB:-}
fi
[ -f "$dtb" ] || die "no DTB ${dtb:-(neither KERNEL_DTB nor DTB set)}"
cmdline=$(cat "$CMDLINE")
# the root file system is named by its UUID (device.conf, tools/mkrootimg.sh):
# root=UUID= for our initramfs (base/initramfs/init), pmos_root_uuid= for the
# pmOS one, which still boots the same root when INITRAMFS names it
[ -z "${ROOT_UUID:-}" ] ||
cmdline="$cmdline root=UUID=$ROOT_UUID pmos_root_uuid=$ROOT_UUID"
# one-off arguments for a test image (makronix.debug, initcall_blacklist=...)
[ -z "${EXTRA_CMDLINE:-}" ] || cmdline="$cmdline $EXTRA_CMDLINE"
[ ${#cmdline} -lt 512 ] || die "cmdline too long"
# le VALUE BYTES: little-endian integer on stdout
le() {
_v=$(($1)) _n=$2
while [ "$_n" -gt 0 ]; do
printf "\\$(printf %03o $((_v & 255)))"
_v=$((_v >> 8))
_n=$((_n - 1))
done
}
# zeros N: N NUL bytes on stdout
zeros() {
[ "$1" -le 0 ] || head -c "$1" /dev/zero
}
size() {
wc -c < "$1" | tr -d ' '
}
ksz=$(size "$kernel") rsz=$(size "$ramdisk") dsz=$(size "$dtb")
# hex SHA-1 of the AOSP id input
idsum() {
{
cat "$kernel"; le "$ksz" 4
cat "$ramdisk"; le "$rsz" 4
le 0 4 # second
le 0 4 # recovery dtbo
le "$dsz" 4; cat "$dtb"
} | sha1sum | cut -d' ' -f1
}
hex2bin() {
_h=$1
while [ -n "$_h" ]; do
printf "\\$(printf %03o 0x$(printf %s "$_h" | cut -c1-2))"
_h=$(printf %s "$_h" | cut -c3-)
done
}
tmp=$out.tmp
{
printf 'ANDROID!'
le "$ksz" 4; le "$BOOT_KERNEL_ADDR" 4
le "$rsz" 4; le "$BOOT_RAMDISK_ADDR" 4
le 0 4; le "$BOOT_SECOND_ADDR" 4
le "$BOOT_TAGS_ADDR" 4
le "$BOOT_PAGESIZE" 4
le 2 4 # header_version
le 0 4 # os_version
zeros 16 # name
printf '%s' "$cmdline"; zeros $((512 - ${#cmdline}))
hex2bin "$(idsum)"; zeros 12 # id: 20 bytes of SHA-1, padded to 32
zeros 1024 # extra_cmdline
le 0 4; le 0 8 # recovery dtbo size, offset
le 1660 4 # header_size
le "$dsz" 4; le "$BOOT_DTB_ADDR" 8
} > "$tmp"
# the header occupies exactly one page
hs=$(size "$tmp")
zeros $((BOOT_PAGESIZE - hs)) >> "$tmp"
pad() { # append FILE, padded to a page
cat "$1" >> "$tmp"
_r=$(( (BOOT_PAGESIZE - $(size "$1") % BOOT_PAGESIZE) % BOOT_PAGESIZE ))
zeros "$_r" >> "$tmp"
}
pad "$kernel"; pad "$ramdisk"; pad "$dtb"
# partition-sized image, if the device wants one
if [ -n "${BOOT_PAD:-}" ]; then
cur=$(size "$tmp")
[ "$cur" -le "$BOOT_PAD" ] || { rm -f "$tmp"; die "image larger than BOOT_PAD"; }
zeros $((BOOT_PAD - cur)) >> "$tmp"
fi
mv "$tmp" "$out"
echo "$out: $(size "$out") bytes, kernel $ksz, ramdisk $rsz, dtb $dsz"