Skip to content

Commit afd8b39

Browse files
committed
extensions/mtkflash: Mediatek mtk-flash direct flashing after build
- similar to `rkdevflash`, but for Mediatek devices - also simpler; lk.bin & fip.img are produced by image build and directly used - requires Rust+Cargo, so add those to hostdeps - since this is a core extension, those will be included in all Docker images too - which was bound to happen anyway since Rust in Linux Kernel is no longer an experiment - extensions/mtkflash: sha1-based bin path, use fork & add `--no-erase-boot1` - example invocation: - `BOARD=radxa-nio-12l BRANCH=collabora RELEASE=trixie EXT=ufs,mtkflash MTKFLASH_TTYACM_DEVICE=1`
1 parent a25496e commit afd8b39

1 file changed

Lines changed: 145 additions & 0 deletions

File tree

extensions/mtkflash.sh

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
#
2+
# SPDX-License-Identifier: GPL-2.0
3+
# Copyright (c) 2026 Ricardo Pardini <ricardo@pardini.net>
4+
# This file is a part of the Armbian Build Framework https://github.com/armbian/build/
5+
#
6+
7+
# This adds the required host-side dependencies, clones and builds mtk-flash.
8+
# mtk-flash is a rust application, so the rust toolchain is required.
9+
# When build is done, it flashes the device with the produced lk.bin/fip.img and disk image.
10+
# Mediatek devices show up as ttyACMx devices when in flashing mode.
11+
# Some common UART devices and modems might _also_ use ttyACMx.
12+
# If you can predict which ttyACMx to use for flashing, pass it via eg MTKFLASH_TTYACM_DEVICE=1
13+
# otherwise MTKFLASH_TTYACM_DEVICE=0 is used by default.
14+
15+
function add_host_dependencies__mtkflash() {
16+
display_alert "Preparing mtkflash host-side dependencies" "${EXTENSION}" "info"
17+
declare -g EXTRA_BUILD_DEPS="${EXTRA_BUILD_DEPS} rustc cargo build-essential" # @TODO: convert to array later
18+
}
19+
20+
function extension_finish_config__900_mtkflash() {
21+
display_alert "Preparing mtkflash extension" "${EXTENSION}" "info"
22+
23+
#declare -g -r MTKFLASH_GIT_REPO="${MTKFLASH_GIT_REPO:-"${GITHUB_SOURCE}/grinn-global/mtk-flash.git"}"
24+
#declare -g -r MTKFLASH_GIT_COMMIT="${MTKFLASH_GIT_COMMIT:-"2aa9ce8a0398a5ea67180df9178b0725de9ce259"}"
25+
# TODO: Back to upstream when https://github.com/grinn-global/mtk-flash/pull/2 is merged
26+
declare -g -r MTKFLASH_GIT_REPO="${MTKFLASH_GIT_REPO:-"${GITHUB_SOURCE}/rpardini/mtk-flash.git"}"
27+
declare -g -r MTKFLASH_GIT_COMMIT="${MTKFLASH_GIT_COMMIT:-"b29e79e841513d46b45717a1f6334bc48ae8abd3"}"
28+
29+
declare -g -r mtkflash_dir="${SRC}/cache/sources/mtk-flash"
30+
declare -g -r mtkflash_bin_path="${mtkflash_dir}/target/release/mtk-flash-${MTKFLASH_GIT_COMMIT}"
31+
32+
# if under docker, exit_with_error; we can't get at the USB needed.
33+
if [[ "${ARMBIAN_RUNNING_IN_CONTAINER}" == "yes" ]]; then
34+
exit_with_error "mtkflash: running under Docker is not supported. it requires direct access to the host USB devices."
35+
fi
36+
}
37+
38+
function host_dependencies_ready__mtkflash() {
39+
display_alert "Preparing mtkflash for usage" "${EXTENSION}" "info"
40+
41+
if [[ ! -f "${mtkflash_bin_path}" ]]; then
42+
display_alert "mtkflash not found, building it" "${EXTENSION}" "info"
43+
build_mtkflash
44+
fi
45+
46+
check_mtkflash # logs the version of mtkflash
47+
}
48+
49+
# post_build_image_write hooks are called in logging context, which is not so great for interactive flashing tools.
50+
# we probably should introduce a different hook that runs outside logging context for such tools.
51+
function post_build_image_write__mtkflash() {
52+
: "${built_image_file:?built_image_file is not set}" # check built_image_file is set
53+
display_alert "Starting mtk-flash flashing process" "${EXTENSION} :: ${built_image_file}" "info"
54+
55+
# Check the built image file exists
56+
if [[ ! -f "${built_image_file}" ]]; then
57+
exit_with_error "mtkflash: Built image file not found: '${built_image_file}'"
58+
fi
59+
60+
# We also need the lk.bin and fip.img file. Obtain their names by replacing the image file extension '.img' with '.lk.bin' and '.fip.img'
61+
declare lk_bin_file="${built_image_file%.img}.lk.bin"
62+
declare fip_img_file="${built_image_file%.img}.fip.img"
63+
if [[ ! -f "${lk_bin_file}" ]]; then
64+
exit_with_error "mtkflash: LK binary file not found: '${lk_bin_file}'"
65+
fi
66+
if [[ ! -f "${fip_img_file}" ]]; then
67+
exit_with_error "mtkflash: FIP image file not found: '${fip_img_file}'"
68+
fi
69+
70+
# Handle the ttyACMx device selection via MTKFLASH_TTYACM_DEVICE environment variable.
71+
# Default to 0 if not set; user can override it.
72+
# If user-set MTKFLASH_TTYACM_DEVICE begins with a slash, use it as full device path.
73+
# Otherwise assume it's a number and append to /dev/ttyACM.
74+
declare mtkflash_device_path="/dev/ttyACM0"
75+
if [[ -n "${MTKFLASH_TTYACM_DEVICE:-""}" ]]; then
76+
if [[ "${MTKFLASH_TTYACM_DEVICE}" == /* ]]; then
77+
mtkflash_device_path="${MTKFLASH_TTYACM_DEVICE}"
78+
else
79+
mtkflash_device_path="/dev/ttyACM${MTKFLASH_TTYACM_DEVICE}"
80+
fi
81+
fi
82+
83+
declare -a mtkflash_cmd_args=()
84+
85+
display_alert "mtk-flash flashing disk image file" "${built_image_file}" "info"
86+
mtkflash_cmd_args+=("--img" "${built_image_file}")
87+
display_alert "mtk-flash flashing LK binary file" "${lk_bin_file}" "info"
88+
mtkflash_cmd_args+=("--da" "${lk_bin_file}")
89+
display_alert "mtk-flash flashing FIP image file" "${fip_img_file}" "info"
90+
mtkflash_cmd_args+=("--fip" "${fip_img_file}")
91+
92+
display_alert "mtk-flash using ttyACM device" "${mtkflash_device_path} (customize with MTKFLASH_TTYACM_DEVICE if needed)" "info"
93+
mtkflash_cmd_args+=("--dev" "${mtkflash_device_path}")
94+
95+
mtkflash_cmd_args+=("--no-erase-boot1") # always preserve mmc0boot1 - used for u-boot environment storage
96+
97+
display_alert "mtk-flash command line" "${mtkflash_bin_path} ${mtkflash_cmd_args[*]@Q}" "debug"
98+
99+
# Since I hit this before: if the device exists at this stage, it's very likely a conflict with some modem or UART.
100+
# The device should only appear when user puts the board in flashing mode, not before.
101+
# If the mtkflash_device_path exists, and is a character device, warn the user to use MTKFLASH_TTYACM_DEVICE.
102+
if [[ -c "${mtkflash_device_path}" ]]; then
103+
display_alert "Warning: mtkflash device '${mtkflash_device_path}' already exists before you were asked to set the board to flashing mode." "${EXTENSION}" "warn"
104+
display_alert "This may indicate a conflict with some modem or UART device using a ttyACMx device." "${EXTENSION}" "warn"
105+
display_alert "Ensure you have the correct device path set via MTKFLASH_TTYACM_DEVICE environment variable." "${EXTENSION}" "warn"
106+
else
107+
display_alert "mtkflash device '${mtkflash_device_path}' (correctly) does not exist - hopefully it will show up when board is put in flashing mode." "${EXTENSION}" "info"
108+
fi
109+
110+
# let user know to put the device in flashing mode
111+
display_alert "NOW! is the time to put the Mediatek board in flashing mode" "${EXTENSION}" "ext"
112+
113+
# finally run mtk-flash with the accumulated arguments
114+
"${mtkflash_bin_path}" "${mtkflash_cmd_args[@]}"
115+
}
116+
117+
function build_mtkflash() {
118+
# Clone mtkflash
119+
fetch_from_repo "${MTKFLASH_GIT_REPO}" "mtk-flash" "commit:${MTKFLASH_GIT_COMMIT}"
120+
121+
# Build mtkflash
122+
pushd "${mtkflash_dir}" &> /dev/null || exit_with_error "Fail to cd to mtkflash: ${mtkflash_dir}"
123+
124+
run_host_command_logged pipetty cargo build --release
125+
126+
# If build succeeded, rename the bin with the commit SHA1 for later up-to-date checks
127+
run_host_command_logged pipetty ls -la "${mtkflash_dir}/target/release/mtk-flash"
128+
run_host_command_logged mv -v "${mtkflash_dir}/target/release/mtk-flash" "${mtkflash_bin_path}"
129+
run_host_command_logged pipetty ls -la "${mtkflash_bin_path}"
130+
131+
popd &> /dev/null || exit_with_error "Fail to cd back to armbian-build"
132+
}
133+
134+
function check_mtkflash() {
135+
declare mtkflash_version="undetermined"
136+
mtkflash_version="$("${mtkflash_bin_path}" --version)"
137+
display_alert "mtkflash version" "${EXTENSION} :: '${mtkflash_version}'" ""
138+
139+
# as a courtesy to the user, install a symlink into /usr/local/bin, so mtk-flash can be called by itself as well
140+
# either way never fail due to this
141+
if [[ -f /usr/local/bin/mtk-flash ]]; then
142+
run_host_command_logged rm -f /usr/local/bin/mtk-flash || true
143+
fi
144+
run_host_command_logged sudo ln -sf "${mtkflash_bin_path}" /usr/local/bin/mtk-flash || true
145+
}

0 commit comments

Comments
 (0)