11800ed4dSAndrew Jeffery#!/bin/sh
21800ed4dSAndrew Jeffery
31800ed4dSAndrew Jefferyset -eu
41800ed4dSAndrew Jeffery
5*56acf685Snitroglycerineprint_help() {
61800ed4dSAndrew Jeffery    script_name=$(basename $0)
71800ed4dSAndrew Jeffery    echo NAME
81800ed4dSAndrew Jeffery    echo '\t'$script_name: Assemble an OpenBMC eMMC image that can be booted under QEMU
91800ed4dSAndrew Jeffery    echo
101800ed4dSAndrew Jeffery    echo SYNOPSYS
111800ed4dSAndrew Jeffery    echo '\t'$script_name '<TARGET>' '<BUILDDIR>' '[IMAGESIZE]'
121800ed4dSAndrew Jeffery    echo
131800ed4dSAndrew Jeffery    echo DESCRIPTION
141800ed4dSAndrew Jeffery    echo '\t''TARGET':'\t\t'The name of the target machine, used to locate the required images.
151800ed4dSAndrew Jeffery    echo '\t''BUILDDIR':'\t'The path to the OpenBMC build directory.
161800ed4dSAndrew Jeffery    echo '\t''IMAGESIZE':'\t'The output image size, may be specified with units. Defaults to 16G.
171800ed4dSAndrew Jeffery    echo
181800ed4dSAndrew Jeffery    echo EXAMPLE:
191800ed4dSAndrew Jeffery    echo '\t'$script_name 'p10bmc' '~/src/openbmc/openbmc/build/p10bmc'
201800ed4dSAndrew Jeffery}
211800ed4dSAndrew Jeffery
22*56acf685Snitroglycerinedetect_xzdec() {
239949cc7cSJoseph Reynolds    if command -v xz >/dev/null
249949cc7cSJoseph Reynolds    then
259949cc7cSJoseph Reynolds        echo xz -dc
269949cc7cSJoseph Reynolds    elif command -v xzdec >/dev/null
279949cc7cSJoseph Reynolds    then
289949cc7cSJoseph Reynolds        echo xzdec
299949cc7cSJoseph Reynolds    fi
309949cc7cSJoseph Reynolds}
319949cc7cSJoseph Reynolds
321800ed4dSAndrew Jefferyif [ $# -eq 0 ]
331800ed4dSAndrew Jefferythen
341800ed4dSAndrew Jeffery    print_help
351800ed4dSAndrew Jeffery    exit 1
361800ed4dSAndrew Jefferyfi
371800ed4dSAndrew Jeffery
381800ed4dSAndrew Jefferyif [ "$1" = "-h" -o "$1" = "--help" ]
391800ed4dSAndrew Jefferythen
401800ed4dSAndrew Jeffery    print_help
411800ed4dSAndrew Jeffery    exit 0
421800ed4dSAndrew Jefferyfi
431800ed4dSAndrew Jeffery
441800ed4dSAndrew Jefferyif [ $# -lt 2 ]
451800ed4dSAndrew Jefferythen
461800ed4dSAndrew Jeffery    print_help
471800ed4dSAndrew Jeffery    exit 1
481800ed4dSAndrew Jefferyfi
491800ed4dSAndrew Jeffery
509949cc7cSJoseph Reynolds
519949cc7cSJoseph ReynoldsXZDEC=$(detect_xzdec)
529949cc7cSJoseph Reynoldsif [ -z "$XZDEC" ]; then
539949cc7cSJoseph Reynolds    echo >&2 "XZ decompress is not available (xz or xzdec).  Stopping."
549949cc7cSJoseph Reynolds    exit 1
559949cc7cSJoseph Reynoldsfi
565ec6208bSKonstantin Aladyshev
571800ed4dSAndrew Jefferyset -x
581800ed4dSAndrew Jeffery
591800ed4dSAndrew Jefferytarget="$1"
601800ed4dSAndrew Jefferybuild_dir="$2"
611800ed4dSAndrew Jefferyimage_size=${3:-16G}
621800ed4dSAndrew Jeffery
631800ed4dSAndrew Jefferyfw_dir=${build_dir}/tmp/deploy/images/${target}
641800ed4dSAndrew Jefferywicxz="${fw_dir}/obmc-phosphor-image-${target}.wic.xz"
651800ed4dSAndrew Jefferymmc="mmc-${target}.img"
661800ed4dSAndrew Jeffery
671800ed4dSAndrew Jefferydd of=$mmc if=/dev/zero bs=1M count=128
681800ed4dSAndrew Jefferydd of=$mmc if=${fw_dir}/u-boot-spl.bin conv=notrunc
691800ed4dSAndrew Jefferydd of=$mmc if=${fw_dir}/u-boot.bin conv=notrunc bs=1K seek=64
709949cc7cSJoseph Reynolds${XZDEC} $wicxz | dd of=$mmc conv=notrunc bs=1M seek=2
711800ed4dSAndrew Jefferytruncate --size ${image_size} $mmc
721800ed4dSAndrew Jeffery
731800ed4dSAndrew Jefferyset +x
741800ed4dSAndrew Jeffery
751800ed4dSAndrew Jefferyecho
761800ed4dSAndrew Jefferyecho For an AST2600-based machine, invoke QEMU with the following parameters:
771800ed4dSAndrew Jefferyecho
781800ed4dSAndrew Jefferyecho '\t'-drive file=$(realpath ${mmc}),if=sd,format=raw,index=2
79