Skip to content

Instantly share code, notes, and snippets.

@vvv279
Forked from zvldz/fw_mgl03_update.md
Created May 10, 2021 12:02
Show Gist options
  • Save vvv279/f00d5d51e4bb099e183c6866014595e0 to your computer and use it in GitHub Desktop.
Save vvv279/f00d5d51e4bb099e183c6866014595e0 to your computer and use it in GitHub Desktop.
Updating mgl03 gateway firmware from telnet

Updating mgl03 gateway firmware via telnet

Telnet must be opened on the gateway (via custom component from @AlexxIT or php-miio/python-miio). You need telnet client like putty or other. You can find out IP of the gateway in MiHome or on your router. Login - "admin", no password.

The easy way

Go to telnet session on gateway and run commands:

main command

curl -s -k -L -o /tmp/update.sh https://gist.github.com/zvldz/b40b4873e3c4c1a64ac536e8ce5dbdad/raw/mgl03_update.sh && sh /tmp/update.sh

If there is no curl in firmware, command will run with error:

-sh: curl: not found

To download curl, run following commands:

wget -O /tmp/wget http://pkg.musl.cc/wget/mipsel-linux-musln32/bin/wget && chmod +x /tmp/wget
/tmp/wget -O /tmp/curl http://mipsel.vacuumz.info/files/curl && chmod +x /tmp/curl && rm -rf /tmp/wget
export PATH="$PATH:/tmp"

The 'pkg.musl.cc' site may not work. Then there will be an error:

Resolving pkg.musl.cc (pkg.musl.cc)... 104.232.42.245
Connecting to pkg.musl.cc (pkg.musl.cc)|104.232.42.245|:80... failed: Connection refused.

In this case, try command:

printf 'GET /files/curl HTTP/1.1\r\nHost: mipsel-ssl.vacuumz.info\r\nUser-Agent: Wget/1.20.3\r\nConnection: close\r\n\r\n' | openssl s_client -quiet -tls1_1 -connect mipsel-ssl.vacuumz.info:443 -servername mipsel-ssl.vacuumz.info | sed '/alt-svc.*/d' | tail -n +19 > /tmp/curl && chmod +x /tmp/curl
export PATH="$PATH:/tmp"

or

wget http://pkg.simple-ha.ru/mipsel/curl -O /tmp/curl && chmod +x /tmp/curl
export PATH="$PATH:/tmp"

Then run first command again.

You will need to select firmware version.

If you are using the XiaomiGateway3 component. For recommended firmware, see https://github.com/AlexxIT/XiaomiGateway3/wiki.

If you see something like in screenshot, all is ok - you have updated gateway. If you used putty, window will close after rebooting gateway. Make sure there are no errors.

In case of major changes between versions of updated firmware, you will most likely need to reset gateway.

You don't need to read any more.

The hard way (way of the warrior)

Turning on ftp

Via custom_component from @AlexxIT

Go to "Developer Tools/SERVICES" in Home Assistant.

And run service:

Service: remote.send_command
Entity: remote.0x680ae2fffe266ed5_pair (for example)

Service Data (YAML, optional):
entity_id: remote.0x680ae2fffe266ed5_pair
command: ftp

Manual mode

To start the ftp-server you need to log into gateway via telnet and execute the commands:

curl -k -o /data/busybox https://busybox.net/downloads/binaries/1.21.1/busybox-mipsel && chmod +x /data/busybox
/data/busybox tcpsvd -vE 0.0.0.0 21 /data/busybox ftpd -w &

Copying files via ftp to gateway

Download modified firmware from firmware folder.

If you are using the XiaomiGateway3 component. For recommended firmware, see https://github.com/AlexxIT/XiaomiGateway3/wiki.

For example mgl03_1.4.7_0065_mod20201211.zip.

Unzip archive mgl03_1.4.7_0065_mod20201211.zip.

You need ftp client like "FileZilla/WinSCP/Total Commander" etc.

Copy files linux_1.4.7_0065.bin and root_1.4.7_0065_mod20201211.bin to gateway folder /tmp.

Copy file full_ble_1.4.7_0065.gbl with filename full.gbl to /data/firmware (directory /data/firmware needs to be created). In firmware 1.4.6 use /data path.

Starting update

Go back to telnet session on gateway and run commands:

fw_update /tmp/linux_1.4.7_0065.bin
fw_update /tmp/root_1.4.7_0065_mod20201211.bin
reboot

After first reboot, gateway will reboot again to update ble firmware. If you want to make sure if ble firmware has been updated, check for /data/firmware/full.gbl file, it should not exist. If not, you can update the BLE firmware manually.

run_ble_dfu.sh /dev/ttyS1 /data/firmware/full.gbl 123 1

All copied files will be deleted automatically.

In case of major changes between versions of updated firmware, you will most likely need to reset gateway.

#!/bin/sh
clean_exit () {
if [ -f /data/update.zip ]; then
echo
echo "* Cleaning temporary files"
rm -vrf /data/update.zip
exit
fi
}
trap clean_exit EXIT SIGINT SIGTERM SIGHUP
echo "* Getting firmware list"
FW_URI_LIST=$(curl -s -k -o- "https://api.github.com/repos/serrj-sv/lumi.gateway.mgl03/git/trees/main?recursive=1" | grep custom | grep mod | grep zip | sort | cut -f4 -d'"')
if [ -z "$FW_URI_LIST" ]; then
echo "! Cannot detect uri for firmware"
exit 2
fi
while : ; do
COUNT=0
echo
echo "For recommended firmware, see https://github.com/AlexxIT/XiaomiGateway3/wiki"
echo "Available firmware:"
for FW_URI in $FW_URI_LIST; do
COUNT=$(expr $COUNT + 1)
echo -n "[${COUNT}] "
echo $FW_URI | cut -d'/' -f4
done
echo -n "Please choose firmware: "
read CHOICE
for NUM in $(seq 1 $COUNT); do
if [ $CHOICE -eq $NUM ]; then
break 2
fi
done
echo "! Wrong choice"
done
FW_URI=$(echo $FW_URI_LIST | cut -d' ' -f$CHOICE)
FW_URL="https://raw.githubusercontent.com/serrj-sv/lumi.gateway.mgl03/main/${FW_URI}"
CONTENT_LENGTH=$(curl -s -I -L -k $FW_URL | grep Content-Length | cut -f2 -d' ' | tr -d "\n\r")
if [ -z "$CONTENT_LENGTH" ] || [ $CONTENT_LENGTH -lt 1024 ]; then
echo "! Cannot get Content-Length for firmware file"
echo "! Check network connection"
exit 3
fi
echo "* Downloading ..."
curl -L -k -o /data/update.zip $FW_URL
FW_SIZE=$(wc -c /data/update.zip | cut -f1 -d' ')
echo
echo "* Content-Length: $CONTENT_LENGTH"
echo "* Firmware size: $FW_SIZE"
if [ "$CONTENT_LENGTH" != "$FW_SIZE" ]; then
echo "! Incorrect firmware size"
exit 4
fi
echo "* Firmware size is correct"
echo "* Unpacking ..."
rm -rf /tmp/*.bin /tmp/*.gbl
unzip -o /data/update.zip -d /tmp/
if [ $? -ne 0 ]; then
echo "! Error when unpacking firmware"
exit 5
fi
echo
echo "* Flashing BLE firmware"
BLE_VER=$(grep -oe '1\.[23]\..' /tmp/full*gbl | sed 's/\.//g')
if [ -z $BLE_VER ]; then
echo "! BLE firmware version is not detected. Use 125."
BLE_VER=123
fi
run_ble_dfu.sh /dev/ttyS1 /tmp/full*gbl $BLE_VER 1
echo
echo "* Flashing kernel"
fw_update /tmp/linux_*
echo
echo "* Flashing root"
fw_update /tmp/root_*
echo
echo "*** Congratulations ***"
echo "Gateway will restart in 10 seconds"
sleep 10
rm -vrf /data/update.zip
reboot
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment