Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Updating mgl03 gateway firmware from telnet

Manual has moved to https://github.com/zvldz/mgl03_fw/tree/main/firmware

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
@firegl99

This comment has been minimized.

Copy link

@firegl99 firegl99 commented Dec 24, 2020

Great job!
If I use telnet update from mgl03_1.4.7_0065_mod20201211 to mgl03_1.4.7_0115_mod20201222, do I need to do factory reset after the update?
reconnect all the iot is a hard job...

@zvldz

This comment has been minimized.

Copy link
Owner Author

@zvldz zvldz commented Dec 24, 2020

Yes, you need reset. Format some db has changed.

@zslibra

This comment has been minimized.

Copy link

@zslibra zslibra commented Dec 29, 2020

Very good job!
I try to flash the firmware twice, but /data/firmware/full.gbl file exists after flash completed reboot.
Does the ble firmware update fail?
Thanks
图片

@zvldz

This comment has been minimized.

Copy link
Owner Author

@zvldz zvldz commented Dec 29, 2020

Does the ble firmware update fail?

Sometimes it takes about 5 minutes.
The gateway does a firmware check at startup.
You can also try rebooting it.
There were times when firmware 1.4.6_0012 did not update.
I'll recheck if there's different path.

@zslibra

This comment has been minimized.

Copy link

@zslibra zslibra commented Dec 29, 2020

hi zvldz,
Yes, I flash the firmware 1.4.6_0012..

@zvldz

This comment has been minimized.

Copy link
Owner Author

@zvldz zvldz commented Dec 29, 2020

I flash the firmware 1.4.6_0012..

In fw 1.4.6_0012 full.gbl must be moved to /data and reboot gw.

@zslibra

This comment has been minimized.

Copy link

@zslibra zslibra commented Dec 29, 2020

I try to manual reboot the gw after 5 minutes, but /data/firmware/full.gbl file, still exist.

@zvldz

This comment has been minimized.

Copy link
Owner Author

@zvldz zvldz commented Dec 29, 2020

path to full.gbl on fw 1.4.6_0012
/data/full.gbl

@zslibra

This comment has been minimized.

Copy link

@zslibra zslibra commented Dec 30, 2020

I try to mv full.gbl from /data/firmware/ to /data then reboot gw (on fw 1.4.6_0012), after reboot the /data/full.gbl not exist.

@zvldz

This comment has been minimized.

Copy link
Owner Author

@zvldz zvldz commented Dec 30, 2020

/data/full.gbl not exist

so firmware has been updated

@zslibra

This comment has been minimized.

Copy link

@zslibra zslibra commented Dec 30, 2020

Oh yes, it may be more perfect to add BLE firmware path in firmware 1.4.6_0012 to mgl03_update.sh.
Thank you very much zvldz!

@Rai23nZ

This comment has been minimized.

Copy link

@Rai23nZ Rai23nZ commented Feb 28, 2021

Trying
wget -O /tmp/wget http://pkg.musl.cc/wget/mipsel-linux-musln32/bin/wget && chmod +x /tmp/wget
But get
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.

@wantsoftat

This comment has been minimized.

Copy link

@wantsoftat wantsoftat commented Feb 28, 2021

Trying
wget -O /tmp/wget http://pkg.musl.cc/wget/mipsel-linux-musln32/bin/wget && chmod +x /tmp/wget
But get
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.

similar error

@zvldz

This comment has been minimized.

Copy link
Owner Author

@zvldz zvldz commented Feb 28, 2021

Yes, the site is down.
I'll update the link later

@zvldz

This comment has been minimized.

Copy link
Owner Author

@zvldz zvldz commented Mar 1, 2021

similar error

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"

Then run first command again.

@RRozga

This comment has been minimized.

Copy link

@RRozga RRozga commented Mar 2, 2021

Then run first command again.

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

@kingwap99

This comment has been minimized.

Copy link

@kingwap99 kingwap99 commented Apr 28, 2021

run this command
wget -O /tmp/wget http://pkg.musl.cc/wget/mipsel-linux-musln32/bin/wget && chmod +x /tmp/wget

and got the error as below

Connecting to pkg.musl.cc (104.232.42.245:80)
wget: server returned error: HTTP/1.1 404 Not Found

@zvldz

This comment has been minimized.

Copy link
Owner Author

@zvldz zvldz commented Apr 28, 2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment