summaryrefslogtreecommitdiff
path: root/build_android.sh
blob: e67efa3558920d1b4e7d562a19ff6b1c9dbdbec5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#!/bin/bash

export ARCH=arm
export CROSS_COMPILE="ccache arm-linux-gnueabihf-"

PROJECT_PATH=$(dirname $(readlink -f "$0"))
ANDROID_PATH="/opt/android"
ANDROID_IMAGE_PATH="${ANDROID_PATH}/out/target/product/linaro_$ARCH"
LINUX=$PROJECT_PATH/linux
LINUX_BOOT=$LINUX/arch/arm/boot

CPU_CORES=$(nproc --ignore=1)

IMX6_DEVBOARDS=$(find $LINUX_BOOT -name "imx6q*.dts" | sort | sed 's=.*/==' | sed 's/\.dts//' | tr '\r\n' ' ' | sed 's/.*/\0\nrdu2/')
UBOOT_DEVBOARDS="imx6q-sabrelite imx6q-sabresd imx6qp-sabresd"
DEVBOARD=""

DO_BUILD_KERNEL=0
DO_BUILD_AOSP=0
DO_FLASH=0

print_devboards()
{
    echo "Valid boards:"
    for board in $IMX6_DEVBOARDS; do
        echo "    $board"
    done
}

print_help()
{
    if [ ! -z ${1+x} ]; then
        echo "$1"
    fi

    echo ""
    echo "$ ./build_android.sh [option]"
    echo ""
    echo "Options:"
    echo "-d [devboard]    -- Set the imx6q board type (required)"
    echo ""
    echo "Actions:"
    echo "-k               -- Build kernel"
    echo "-a               -- Build AOSP"
    echo "-b [device]      -- Build everything and create bootable image on device"
    echo ""

    if [ ! -z ${2+x} ]; then
        echo "$2"
    fi

    exit 1
}

print_header()
{
    STRLEN=${#1}
    (( LINELEN = STRLEN + 14 ))
    echo ""
    head -c $LINELEN < /dev/zero | tr '\0' '-'
    echo ""
    echo "---    $1    ---"
    head -c $LINELEN < /dev/zero | tr '\0' '-'
    echo ""
}

mmc_is_valid()
{
    if [ "${1:-}" ]; then
        BLK_DEV=$1
    fi

    if ! [[ -a "${BLK_DEV}" ]]; then
        return 1
    fi

    DEV_PARTITION=$(lsblk -prb -o NAME,SIZE,LABEL $BLK_DEV | grep -i "boot" | cut -d' ' -f1)
    if [ -z "$DEV_PARTITION" ]; then
        return 1
    fi

    return 0
}

devboard_is_rdu2()
{
    case "$1" in
        *rdu2*)
            return 0
            ;;
        *RDU2*)
            return 0
            ;;
    esac

    return 1
}

devboard_is_valid()
{
    devboard_is_rdu2 "$1" && return 0

    for board in $IMX6_DEVBOARDS; do
        if [[ $board = $1 ]]; then
            return 0
        fi
    done

    return 1
}

parse_args()
{
    while getopts ":kab:d:" opt;
    do
    case "$opt" in
        k)
            DO_BUILD_KERNEL=1
            ;;
        a)
            DO_BUILD_AOSP=1
            ;;
        b)
            DO_BUILD_KERNEL=1
            DO_BUILD_AOSP=1
            DO_FLASH=1
            mmc_is_valid $OPTARG || print_help "Invalid MMC supplied!"
            BLK_DEV=$OPTARG
            ;;
        d)
            devboard_is_valid $OPTARG || print_help "Invalid ($OPTARG) devboard supplied!" "$(print_devboards)"
            DEVBOARD=$OPTARG
            devboard_is_rdu2 $DEVBOARD && DEVBOARD=rdu2
            ;;
        *)
            echo "*)"
            print_help "Invalid argument -$opt $OPTARG"
            ;;
        esac
    done
    shift $((OPTIND-1))

    [ -n "$DEVBOARD" ] || print_help "No devboard supplied!"

    [ $DO_BUILD_KERNEL -eq 1 ] || [ $DO_BUILD_AOSP -eq 1 ] || [ $DO_FLASH -eq 1 ] || print_help "No action specified!"

    [ $DO_BUILD_KERNEL -eq 1 ] && echo "requested to build kernel..."
    [ $DO_BUILD_AOSP -eq 1 ]   && echo "requested to build AOSP..."
    [ $DO_FLASH -eq 1 ] && echo "requested to flash..."
}

flash_uboot()
{
    mmc_is_valid || return
    print_header "Copying u-boot boot.scr"

    TMPDIR=$(mktemp -d)
    ME=$(whoami)

    BOOT_SCR_FILENAME="boot.scr"
    if [[ $DEVBOARD = "imx6q-sabrelite" ]]; then
        BOOT_SCR_FILENAME="6x_bootscript"
    fi

    sudo umount $DEV_PARTITION &>/dev/null
    sudo mount $DEV_PARTITION $TMPDIR
    cd $PROJECT_PATH
    sudo mkimage -A arm -O linux -T script -C none -n boot.scr -d uboot_$DEVBOARD.scr $TMPDIR/$BOOT_SCR_FILENAME
    sync
    sudo umount $TMPDIR
    rmdir $TMPDIR
}

flash_kernel_mmc()
{
    mmc_is_valid || return
    print_header "Copying zImage & dtb to mmc"

    TMPDIR=$(mktemp -d)
    ME=$(whoami)

    sudo umount $DEV_PARTITION &>/dev/null
    sudo mount $DEV_PARTITION $TMPDIR
    sudo cp $LINUX_BOOT/zImage $TMPDIR/android_zImage
    
    if devboard_is_rdu2 $DEVBOARD; then
        sudo cp $LINUX_BOOT/dts/imx6q-zii-rdu2.dtb $TMPDIR/
        sudo cp $LINUX_BOOT/dts/imx6qp-zii-rdu2.dtb $TMPDIR/
    else
        sudo cp $LINUX_BOOT/dts/$DEVBOARD.dtb $TMPDIR/
    fi
    sync
    sudo umount $TMPDIR
    rmdir $TMPDIR
}

flash_kernel()
{
    TIMESTAMP="${PROJECT_PATH}/flash_kernel.timestamp"

    if [ ! -f "${TIMESTAMP}" ] || \
       [ ${LINUX_BOOT}/Image.gz          -nt ${TIMESTAMP} ] || \
       [ ${LINUX_BOOT}/dts/$DEVBOARD.dtb -nt ${TIMESTAMP} ]
    then
        # Do things on new kernel build
        echo
    fi

    flash_kernel_mmc

    touch ${TIMESTAMP}
}

build_kernel()
{
    cd $PROJECT_PATH/linux && \
    make imx_v6_v7_defconfig android-base.config android-recommended.config && \
    scripts/kconfig/merge_config.sh -m .config $PROJECT_PATH/config_sw_sync && \
    scripts/kconfig/merge_config.sh -m .config $PROJECT_PATH/config_$DEVBOARD && \
    make -j$CPU_CORES olddefconfig zImage dtbs
}

flash_android_mmc()
{
    mmc_is_valid || return
    print_header "Copying Android to mmc"

    TMPDIR=$(mktemp -d)
    ME=$(whoami)

    sudo umount $DEV_PARTITION &>/dev/null
    sudo mount $DEV_PARTITION $TMPDIR
    sudo mkimage -A arm -O linux -T ramdisk -C gzip -d ${ANDROID_IMAGE_PATH}/ramdisk.img $TMPDIR/android_ramdisk.img.gz
    sync
    sudo umount $TMPDIR
    rmdir $TMPDIR

    bash -c "
        cd ${ANDROID_PATH} && \\
        source build/envsetup.sh && \\
        lunch linaro_arm-userdebug && \\
        device/linaro/generic/tools/sd-resize-and-flash -b ${BLK_DEV} -d out/target/product/linaro_arm/"
}

flash_android()
{
    TIMESTAMP="${PROJECT_PATH}/flash_android.timestamp"

    if [ ! -f "${TIMESTAMP}" ] || \
       [ ${ANDROID_IMAGE_PATH}/cache.img    -nt ${TIMESTAMP} ] || \
       [ ${ANDROID_IMAGE_PATH}/ramdisk.img  -nt ${TIMESTAMP} ] || \
       [ ${ANDROID_IMAGE_PATH}/system.img   -nt ${TIMESTAMP} ] || \
       [ ${ANDROID_IMAGE_PATH}/userdata.img -nt ${TIMESTAMP} ]
    then
        # Do things on new android build
        echo
    fi

    flash_android_mmc

    touch ${TIMESTAMP}
}

build_android()
{
    bash -c "
        cd ${ANDROID_PATH} && \\
        source build/envsetup.sh && \\
        lunch linaro_arm-userdebug && \\
        make -j${CPU_CORES}"
}

do_flash_uboot()
{
    for board in $UBOOT_DEVBOARDS; do
        if [[ $board = $DEVBOARD ]]; then
            return 0
        fi
    done

    return 1
}

parse_args $@
cd $PROJECT_PATH

[ $DO_BUILD_KERNEL -eq 0 ] || build_kernel  || exit 1
[ $DO_BUILD_AOSP   -eq 0 ] || build_android || exit 1

[ $DO_FLASH -eq 1 ] && {
    flash_kernel && flash_android || exit 1
    do_flash_uboot && flash_uboot || exit 1
}

exit 0