-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbuild-chip.sh
executable file
·297 lines (239 loc) · 9.1 KB
/
build-chip.sh
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
296
297
#!/bin/bash
set -eo pipefail
readonly PENYU_CHROOT_INSTALL_VERSION="0.6.0"
readonly PENYU_VERSION="latest-stable"
readonly LATEST_BUILDROOT_URL="http://opensource.nextthing.co/chip/buildroot/stable/latest"
readonly GITHUB_REPO="orcinustools/penyu"
readonly GITHUB_LOGIN_USERNAME="anak10thn"
# secure readonly GITHUB_ACCESS_TOKEN
die () {
printf '\033[1;31mERROR:\033[0m %s\n' "$@" >&2 # bold red
exit 1
}
einfo () {
printf '\n\033[1;36m> %s\033[0m\n' "$@" >&2 # bold cyan
}
ewarn () {
printf '\033[1;33m> %s\033[0m\n' "$@" >&2 # bold yellow
}
install_apt_dependencies () {
cp /etc/apt/sources.list /etc/apt/sources.list.ori
cat << EOF > /etc/apt/sources.list
deb http://kebo.pens.ac.id/ubuntu/ zesty main restricted
deb http://kebo.pens.ac.id/ubuntu/ zesty-updates main restricted
deb http://kebo.pens.ac.id/ubuntu/ zesty universe
deb http://kebo.pens.ac.id/ubuntu/ zesty-updates universe
deb http://kebo.pens.ac.id/ubuntu/ zesty multiverse
deb http://kebo.pens.ac.id/ubuntu/ zesty-updates multiverse
deb http://kebo.pens.ac.id/ubuntu/ zesty-backports main restricted universe multiverse
deb http://kebo.pens.ac.id/ubuntu/ zesty-security main restricted
deb http://kebo.pens.ac.id/ubuntu/ zesty-security universe
deb http://kebo.pens.ac.id/ubuntu/ zesty-security multiverse
EOF
apt-get update
apt-get install -y git liblzo2-dev python-lzo mtd-utils python-setuptools wget
}
install_ubi_reader () {
local temp_dir
temp_dir=$(mktemp -d -p /tmp ubi_reader.XXXXXX)
git clone https://github.com/jrspruitt/ubi_reader "${temp_dir}"
pushd "${temp_dir}"
python setup.py install
popd
rm --recursive "${temp_dir}"
}
install_penyu_chroot_install () {
local version="${1}"
wget --quiet --output-document /usr/local/bin/penyu-chroot-install "https://raw.githubusercontent.com/alpinelinux/alpine-chroot-install/v${version}/alpine-chroot-install"
chmod +x /usr/local/bin/penyu-chroot-install
}
get_latest_buildroot () {
local latest_buildroot_url="${1}"
local buildroot_dir="${2}"
local _latest_buildroot # _ because else conflict with latest_buildroot in main scope
_latest_buildroot=$(wget --quiet -O- "${latest_buildroot_url}")
eval "${3}=\"${_latest_buildroot}\""
local buildroot_rootfs_url
buildroot_rootfs_url="${_latest_buildroot}/images/rootfs.ubi"
local temp_dir
temp_dir=$(mktemp -d -p /tmp buildroot.XXXXXX)
# download buildroot
wget --quiet --output-document "${temp_dir}/rootfs.ubi" "${buildroot_rootfs_url}"
# extract ubi
pushd "${temp_dir}"
mkdir extracted
pushd extracted
ubireader_extract_files "../rootfs.ubi"
pushd ubifs-root
pushd "$(find . -maxdepth 1 ! -path .|head -n 1)"
pushd rootfs
cp --archive ./. "${buildroot_dir}"
popd
popd
popd
popd
popd
rm --recursive "${temp_dir}"
}
prepare_penyu () {
local penyu_version="${1}"
local penyu_dir="${2}"
CHROOT_KEEP_VARS="" ALPINE_PACKAGES="wpa_supplicant wireless-tools bkeymaps tzdata nano" penyu-chroot-install -d "${penyu_dir}" -a armhf -b "${penyu_version}"
"${penyu_dir}/enter-chroot" -u root <<-EOF
set -e
# Needed services
rc-update add devfs sysinit
rc-update add dmesg sysinit
rc-update add mdev sysinit
rc-update add modules boot
rc-update add sysctl boot
rc-update add hostname boot
rc-update add bootmisc boot
rc-update add syslog boot
rc-update add wpa_supplicant boot # needed, otherwise does not connect after reboot
rc-update add mount-ro shutdown
rc-update add killprocs shutdown
rc-update add savecache shutdown
# Allow root login with no password.
passwd root -d
# Allow root login from serial.
echo ttyS0 >> /etc/securetty
echo ttyGS0 >> /etc/securetty
# Make sure the USB virtual serial device is available.
echo g_serial >> /etc/modules
# Make sure wireless networking is available.
echo 8723bs >> /etc/modules
# These enable the USB virtual serial device, and the standard serial
# pins to both be used as TTYs
echo ttyS0::respawn:/sbin/getty -L ttyS0 115200 vt102 >> /etc/inittab
echo ttyGS0::respawn:/sbin/getty -L ttyGS0 115200 vt102 >> /etc/inittab
EOF
umount -l "${penyu_dir}/proc"
umount -l "${penyu_dir}/sys"
umount -l "${penyu_dir}/dev"
umount -l "${penyu_dir}$(pwd)"
rm "${penyu_dir}/usr/bin/qemu-arm-static"
rm "${penyu_dir}/etc/resolv.conf"
rm "${penyu_dir}/enter-chroot"
}
prepare_rootfs () {
local buildroot_dir="${1}"
local penyu_dir="${2}"
local ubi_dest="${3}"
local temp_dir
temp_dir=$(mktemp -d -p /tmp ubi.XXXXXX)
cp --archive "${buildroot_dir}/boot/." "${penyu_dir}/boot"
cp --archive "${buildroot_dir}/lib/modules/." "${penyu_dir}/lib/modules"
pushd "${temp_dir}"
cat <<EOF >ubinize.cfg
[ubifs]
mode=ubi
vol_id=0
vol_type=dynamic
vol_name=rootfs
vol_alignment=1
vol_flags=autoresize
image=rootfs.ubifs
EOF
mkfs.ubifs -d "${penyu_dir}" -o rootfs.ubifs -e 0x1f8000 -c 2000 -m 0x4000 -x lzo
ubinize -o "${ubi_dest}" -m 0x4000 -p 0x200000 -s 16384 ubinize.cfg
popd
rm --recursive "${temp_dir}"
}
make_penyu_release () {
local chip_build_dir="${1}"
local latest_buildroot="${2}"
local tar_dest="${3}"
wget --quiet --output-document "${chip_build_dir}/penyu/images/sun5i-r8-chip.dtb" "${latest_buildroot}/images/sun5i-r8-chip.dtb"
wget --quiet --output-document "${chip_build_dir}/penyu/images/sunxi-spl.bin" "${latest_buildroot}/images/sunxi-spl.bin"
wget --quiet --output-document "${chip_build_dir}/penyu/images/sunxi-spl-with-ecc.bin" "${latest_buildroot}/images/sunxi-spl-with-ecc.bin"
wget --quiet --output-document "${chip_build_dir}/penyu/images/uboot-env.bin" "${latest_buildroot}/images/uboot-env.bin"
wget --quiet --output-document "${chip_build_dir}/penyu/images/zImage" "${latest_buildroot}/images/zImage"
wget --quiet --output-document "${chip_build_dir}/penyu/images/u-boot-dtb.bin" "${latest_buildroot}/images/u-boot-dtb.bin"
pushd "${chip_build_dir}"
tar -zcv -C "${chip_build_dir}" -f "${tar_dest}" penyu
popd
}
gather_rootfs_versions () {
local buildroot_dir="${1}"
local penyu_dir="${2}"
# shellcheck source=/dev/null
source "${buildroot_dir}/etc/os-release"
eval "${3}=\"${VERSION_ID}\""
# shellcheck source=/dev/null
source "${penyu_dir}/etc/os-release"
eval "${4}=\"${VERSION_ID}\""
eval "${5}=\"${PRETTY_NAME}\""
}
release_github () {
local repo="${1}"
local username="${2}"
local access_token="${3}"
local tag_name="${4}"
local release_name="${5}"
local release_body="${6}"
local tar_location="${7}"
local release_json
release_json=$(printf '{"tag_name": "%s","target_commitish": "master","name": "%s","body": "%s","draft": false,"prerelease": false}' "${tag_name}" "${release_name}" "${release_body}")
local github_release_id
github_release_id=$(curl -u "${username}:${access_token}" --data "${release_json}" -v --silent "https://api.github.com/repos/${repo}/releases" 2>&1 | sed -ne 's/^ "id": \(.*\),$/\1/p')
curl -u "${username}:${access_token}" -X POST -H "Content-Type: application/gzip" --data-binary "@${tar_location}" "https://uploads.github.com/repos/${repo}/releases/${github_release_id}/assets?name=${tag_name}.tar.gz"
}
main () {
local working_dir
working_dir=$(mktemp -d -p /tmp chip-penyu.XXXXXX)
local buildroot_dir="${working_dir}/buildroot"
mkdir -p "${buildroot_dir}"
local penyu_dir="${working_dir}/penyu"
mkdir -p "${penyu_dir}"
local chip_build_dir="${working_dir}/chip-build"
mkdir -p "${chip_build_dir}/penyu/images"
einfo "Installing dependencies..."
install_apt_dependencies
einfo "Installing ubi_reader..."
install_ubi_reader
einfo "Installing penyu-chroot-install..."
install_penyu_chroot_install "${PENYU_CHROOT_INSTALL_VERSION}"
#####
# Get the latest base buildroot image
#####
einfo "Getting latest buildroot..."
local latest_buildroot=""
get_latest_buildroot "${LATEST_BUILDROOT_URL}" "${buildroot_dir}" "latest_buildroot"
#####
# Get and set-up Penyu
#####
einfo "Getting and setting-up Penyu..."
prepare_penyu "${PENYU_VERSION}" "${penyu_dir}"
#####
# Prepare rootfs
#####
einfo "Preparing rootfs..."
prepare_rootfs "${buildroot_dir}" "${penyu_dir}" "${chip_build_dir}/penyu/images/rootfs.ubi"
#####
# Make Penyu release
#####
einfo "Making Penyu release..."
local temp_tar
temp_tar=$(mktemp -p /tmp tar.XXXXXX)
make_penyu_release "${chip_build_dir}" "${latest_buildroot}" "${temp_tar}"
einfo "Gathering rootfs versions..."
local buildroot_version_id=""
local penyu_version_id=""
local penyu_pretty_name=""
gather_rootfs_versions "${buildroot_dir}" "${penyu_dir}" "buildroot_version_id" "penyu_version_id" "penyu_pretty_name"
#####
# Create GitHub release
#####
# einfo "Releasing on GitHub..."
# release_github "${GITHUB_REPO}" "${GITHUB_LOGIN_USERNAME}" "${GITHUB_ACCESS_TOKEN}" \
# "penyu-${penyu_version_id}_buildroot-${buildroot_version_id}_$(date +%s)" \
# "${penyu_pretty_name} with Buildroot ${buildroot_version_id} built on $(date +%Y-%m-%d)" \
# "Daily build." \
# "${temp_tar}"
mkdir -p /root/iso
cp "${temp_tar}" /root/iso/penyu-${penyu_version_id}-chip-armv7.tar.gz
einfo "Done!"
}
main
# tar zxvf penyu*.tar.gz && sudo BUILDROOT_OUTPUT_DIR=penyu/ ./chip-fel-flash.sh