1#!/bin/sh
2
3set -eu
4
5function print_help() {
6    script_name=$(basename $0)
7    echo NAME
8    echo '\t'$script_name: Assemble an OpenBMC eMMC image that can be booted under QEMU
9    echo
10    echo SYNOPSYS
11    echo '\t'$script_name '<TARGET>' '<BUILDDIR>' '[IMAGESIZE]'
12    echo
13    echo DESCRIPTION
14    echo '\t''TARGET':'\t\t'The name of the target machine, used to locate the required images.
15    echo '\t''BUILDDIR':'\t'The path to the OpenBMC build directory.
16    echo '\t''IMAGESIZE':'\t'The output image size, may be specified with units. Defaults to 16G.
17    echo
18    echo EXAMPLE:
19    echo '\t'$script_name 'p10bmc' '~/src/openbmc/openbmc/build/p10bmc'
20}
21
22function detect_xzdec() {
23    if command -v xz >/dev/null
24    then
25        echo xz -dc
26    elif command -v xzdec >/dev/null
27    then
28        echo xzdec
29    fi
30}
31
32if [ $# -eq 0 ]
33then
34    print_help
35    exit 1
36fi
37
38if [ "$1" = "-h" -o "$1" = "--help" ]
39then
40    print_help
41    exit 0
42fi
43
44if [ $# -lt 2 ]
45then
46    print_help
47    exit 1
48fi
49
50
51XZDEC=$(detect_xzdec)
52if [ -z "$XZDEC" ]; then
53    echo >&2 "XZ decompress is not available (xz or xzdec).  Stopping."
54    exit 1
55fi
56
57set -x
58
59target="$1"
60build_dir="$2"
61image_size=${3:-16G}
62
63fw_dir=${build_dir}/tmp/deploy/images/${target}
64wicxz="${fw_dir}/obmc-phosphor-image-${target}.wic.xz"
65mmc="mmc-${target}.img"
66
67dd of=$mmc if=/dev/zero bs=1M count=128
68dd of=$mmc if=${fw_dir}/u-boot-spl.bin conv=notrunc
69dd of=$mmc if=${fw_dir}/u-boot.bin conv=notrunc bs=1K seek=64
70${XZDEC} $wicxz | dd of=$mmc conv=notrunc bs=1M seek=2
71truncate --size ${image_size} $mmc
72
73set +x
74
75echo
76echo For an AST2600-based machine, invoke QEMU with the following parameters:
77echo
78echo '\t'-drive file=$(realpath ${mmc}),if=sd,format=raw,index=2
79