aboutsummaryrefslogtreecommitdiffhomepage
path: root/platform_tools/barelinux/bin/arm64_download
blob: 257d464be173aa94a398fab68299e11263b4f5e9 (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
#!/bin/sh

# Copyright 2014 Google Inc.
#
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.


usage() {
    cat >&2 <<EOF
arm64_download - this script downloads the Linaro's ARMv8 Aarch64
toolchain and minimal embedded Linux system as well as ARM's
foundation model.  The required files are mirrored on Google Cloud.

If the files are already located in the working directory, the
download can be skipped if the checksums match.

The script then starts a emulated Arm64 Linux system in the
background.  After the boot is complete, you can SSH into the system
at port 8022 via user@localhost.  The SSH key will be downloaded into
the working directery as well.

Requires gsutil, xz, tar, and gunzip.

Usage:
  $0 WORKING_DIRECTORY
  ssh-add WORKING_DIRECTORY/key
  ...wait...
  ssh -p 8022 user@localhost
EOF
    return 1
}

try() {
    # print an error on nonzero return code
    "$@"
    local ret=$?
    if [ $ret != 0 ] ; then
        echo "'$@' failed and returned ${ret}." >&2
        return $ret
    fi
}

download_necessary_software_to_dir() (
    cd "$1"
    local location="chromium-skia-gm/arm64env"
    try gsutil cp "gs://${location}/md5sum.txt" . || return
    if md5sum -c --quiet "md5sum.txt"; then
        return 0
    fi
    try gsutil cp "gs://${location}/*" . || return
)

install_compiler() {
    local working_dir="$1"
    local toolchain="gcc-linaro-aarch64-linux-gnu-4.8-2013.12_linux"
    (
        try cd "$working_dir" || return
        try test -f "${toolchain}.tar.xz" || return
        try xz --decompress --stdout < "${toolchain}.tar.xz" | \
            try tar xf - || return
    )
    local dir="${working_dir}/${toolchain}"
    try test -d "$dir" || return
    try test -x "${dir}/bin/aarch64-linux-gnu-gcc" || return
    try test -x "${dir}/bin/aarch64-linux-gnu-g++" || return
}

install_runtime() {
    local working_dir="$1"

    local firmware='img-foundation.axf'
    local rootfs='vexpress64-openembedded_lamp-armv8-gcc-4.8_20131215-557'
    local compressed_rootfs="${rootfs}.img.CLEAN_AND_CONFIGURED.xz"
    local compressed_foundation_model='FM000-KT-00035-r0p8-52rel06.tgz'
    local keyfile='CLEAN_AND_CONFIGURED_key'

    try cp "${working_dir}/$firmware" "${working_dir}/firmware" || return

    try xz --decompress --stdout \
        < "${working_dir}/${compressed_rootfs}" \
        > "${working_dir}/rootfs" || return
    try test -f "${working_dir}/rootfs" || return

    (
        try cd "$working_dir" || return
        try test -f "$compressed_foundation_model" || return
        try gunzip -c "$compressed_foundation_model" | try tar xf - || return
        try test -d "Foundation_v8pkg" || return  #  Assert.
    )

    try cp "${working_dir}/${keyfile}" "${working_dir}/key" || return
    chmod 'go=' "${working_dir}/key"
}

start_arm64_image() {
    local working_dir="$1"
    local foundation_dir="${working_dir}/Foundation_v8pkg"
    local foundation="${foundation_dir}/models/Linux64_GCC-4.1/Foundation_v8"
    local firmware="${working_dir}/firmware"
    local rootfs="${working_dir}/rootfs"

    try test -d "$foundation_dir" || return
    try test -x "$foundation" || return
    try test -f "$firmware" || return
    try test -f "$rootfs" || return

    for PID in $(ps -o 'pid=' -C 'Foundation_v8') ; do
        kill $PID
    done

    DISPLAY='' nohup \
    "$foundation" \
        --image="${firmware}" \
        --cores=4 \
        --block-device="${rootfs}" \
        --network="nat" \
        --network-nat-subnet="192.168.31.0/24" \
        --network-nat-ports="8022=22" \
        > /dev/null 2>&1 &
    echo 'Waiting for foundation model to boot...'
    while ! ssh -i "${working_dir}/key" \
        -o NoHostAuthenticationForLocalhost=yes \
        -p 8022 user@localhost true 2> /dev/null; do
        sleep 5
    done
    echo 'Listening to SSH on port 8022.'
}

arm64_download() {
    local working_directory="$1"
    try mkdir -p "$working_directory" || return

    try download_necessary_software_to_dir "$working_directory" || return

    try install_compiler "$working_directory" || return

    try install_runtime "$working_directory" || return

    try start_arm64_image "$working_directory" || return
}

for command in gsutil xz tar md5sum gunzip; do
    try command -v "$command" > /dev/null || usage || exit
done

if [ -z "$1" ] ; then
    usage || exit
fi
try arm64_download "$1"