Skip to content

Instantly share code, notes, and snippets.

@ro99
Last active July 2, 2024 13:16
Show Gist options
  • Save ro99/77579b1d22fcde9ca72e0180b17bbc9d to your computer and use it in GitHub Desktop.
Save ro99/77579b1d22fcde9ca72e0180b17bbc9d to your computer and use it in GitHub Desktop.

For my reference in the future I will record here the steps that I had to follow in order to install Openwrt on the tp-link router EX220. This was done using a Windows 11 machine.

Disclaimer: Use this tutorial at your own risk. We are not responsible for any damage/harms done to your device using the information from this tutorial.

Steps to install OpenWRT on EX220 router

OEM installation using the serial + TFTP method

These are the steps provided at the official https://openwrt.org/toh/tp-link/ex220_v1 page:

1. Establish a serial connection.
2. Set up a TFTP server and copy both initramfs and sysupgrade images there.
3. Power on the device and press any key to interrupt normal boot.
4. Load the initramfs image using tftpboot.
5. Boot with bootm.
6. If you haven't done so already, back up all stock mtd partitions.
7. Copy the sysupgrade image to the router.
8. Flash OpenWrt through either LuCI or the sysupgrade command. Remember not to attempt saving settings.

Next we will detail each one of them.

Step 1 - Establish a serial connection.

For this we will need the following pieces of hardware:

  • USB to TTL adaptor (CP2102)
  • Ethernet cable
  • Female Female jump connectors (3x)
  • Header pins

Soldering

  1. Start by opening the router and soldering the header pins to the UART port
  2. Connect the 3 jumper connectors (3V3 - GND - RX - TX)
  3. In my case I had to bend the pins, otherwise it would not be possible to close the case. This is the result:

img1

Accessing the console

  1. Start by installing Putty
  2. Connect the CP2102 adapter to your PC and go to Device Manager. Check the COM ports and take note of it

image

  1. Open Putty and configure it using the correct serial line to connect to. In my case it is the COM4 (as show above). Also set the Speed (baud) to 115200, Data bits to 8, Stop bits to 1, Parity to None and Flow Control to None.
  2. (optional) Save the Session
  3. (optional) Make sure that your CP2102 adapter is working. You can do a loopback test by connecting the RXD to the TXD port, and writing anything in the Putty console (if it is working you will see the text that you are writing at the console, otherwise nothing will appear).

img4

  1. Connect the CP2102 adapter to your router (GND, RX and TX only). Do not connect the 3.3V pin from the router to your adapter.

img3

  1. Open the Putty console using the configurations from step 3
  2. Turn on the router. You will start to see (in the Putty console) a lot of (readable) textual information received from the router.
  3. Turn off the router.

With this we now confirm that the serial communication between the PC and the Router is working. We can proceed to the TFTP step.

Step 2 - Set up a TFTP server

  1. Download the tftpd64 program (the portable version)
  2. Download the initramfs image (check the link from the official page)
  3. (optional) Rename the image name to initramfs.bin
  4. Copy the image file to the root directory of tftp64
  5. Click on the program tftp64.exe and configure it by going into Settings. Go to GLOBAL tab and check only the TFTP Server checkbox. Go to the TFTP tab and configure like the image below:

image

  1. You can close the program after configuring it
  2. (optional) Deactivate the Wifi and turn off the firewall (not sure if this is necessary)
  3. Configure your Ethernet to use Manual IPv4 (instead of DHCP):
    • IP Address: the same from tftp64, in our case, 192.168.0.2
    • Mask: 255.255.255.0
    • Gateway: 192.168.0.1
    • DNS: any, I am using the 8.8.8.8
  4. Connect your router to your PC via the ethernet cable.
  5. Connect your router to your PC via USB (CP2102) adapter

img5

We are now ready to copy the initramfs file to the router.

Step 3 - Power on the device and press any key to interrupt normal boot

  1. Open a new Putty console (using the settings from step 3 of section Accessing the Console)
  2. Turn on the router and click any key on the Putty console. You need to be fast, otherwise the normal boot will not be interupted.
  3. If everything works, now you should be seeing a screen with the U-Boot Boot Menu.

img6

  1. Press DOWN to move and ENTER the option 0. U-Boot console

Step 4 & 5 - Load the initramfs image using tftpboot & Boot it with bootm

The initramfs image is like a OpenWRT "test driven" image that lives only in the RAM. That means that it will be erased as soon as you turn off the router. We will need to load this image on the RAM of our router (for backup reasons and to copy the sysupgrade.bin to the /tmp/ directory). For our case we are using the address 0x80010000. The reason to use this address is that this was the one used when we tried to load the image using the 5. Load Image option. Not sure if this is correct (it worked for us).

At the U-Boot console, enter the following commands:

setenv ipaddr 192.168.0.1
setenv serverip 192.168.0.2
tftpboot 0x80010000 initramfs.bin
bootm 0x80010000

Wait for a while. At some point no new text will appear at the console. At this point you can hit Enter.

BusyBox v1.36.1 (2024-02-04 00:12:51 UTC) built-in shell (ash)

  _______                     ________        __
 |       |.-----.-----.-----.|  |  |  |.----.|  |_
 |   -   ||  _  |  -__|     ||  |  |  ||   _||   _|
 |_______||   __|_____|__|__||________||__|  |____|
          |__| W I R E L E S S   F R E E D O M
 -----------------------------------------------------
 OpenWrt SNAPSHOT, r25002-6e8df06fc1
 -----------------------------------------------------
=== WARNING! =====================================
There is no root password defined on this device!
Use the "passwd" command to set up a new password
in order to prevent unauthorized SSH logins.
--------------------------------------------------

We can now proceed to the Backup.

Step 6 - Back up all stock mtd partitions (not mandatory, just recommended)

There are different ways to do this. The one that worked for us was by setting a SSH server on the windows PC and accessing it via Dropbear client on the router. We will not detail how to setup an SSH Server on Windows. But notice that it was important that the (a) server was configured password-less, (b) default WSL2 shell and (c) Server IP 192.168.1.2, Port 2222. An excelent guide by Adam Fields can be found here. Other resource used was the Microsoft Get started with OpenSSH for Windows page.

For me this was the trickiest part...

  1. You can close the tftpd64.exe program (we will no longer need TFTP).
  2. At your Windows PC, configure the Ethernet to Manual IPv4 with IP 192.168.1.2, Mask 255.255.255.0, Gateway 192.168.1.0, DNS 8.8.8.8. The motivation for this is that the router is with 192.168.1.1 IP address (you can confirm with the ifconfig command), and we need to be in the same net (also, IIRC, it is using SSH to listen for incoming connections at port 22, that is why we used port 2222 for the Windows SSH server).
  3. At the Openwrt "console" enter the follwing commands:
mkdir ~/.ssh
dropbearkey -t rsa -f ~/.ssh/id_dropbear
  1. Copy the generated public key to the file .ssh\authorized_keys on the Windows PC (you can do this by clicking with the mouse right buttom at the Putty taskbar and "Copy all to Clipboard").
  2. Restart the SSH service using a PowerShell console and the command: Restart-Service sshd
  3. Using Putty/OpenWRT, create a backup script at the tmp directory: cat << "EOF" > /tmp/backup.sh, and the enter:
#!/bin/sh

BACKUP_HOST="192.168.1.2"
BACKUP_USER="CHANGETOYOURWINDOWSUSERNAME"
echo "Backup host ${BACKUP_HOST}"
 
cat /proc/mtd | tail -n+2 | while read; do
  MTD_DEV=$(echo ${REPLY} | cut -f1 -d:)
  MTD_NAME=$(echo ${REPLY} | cut -f2 -d\")
  echo "Backing up ${MTD_DEV} (${MTD_NAME})"
  dd if=/dev/${MTD_DEV}ro | dbclient ${BACKUP_USER}@${BACKUP_HOST} -p 2222 "dd of=~/${MTD_DEV}_${MTD_NAME}.backup"
done
EOF
  1. Run the script: sh /tmp/backup.sh
  2. If everything works, now you will have your backup files at the WSL FS (hint, use explorer.exe . to copy the files outside the VM).
  3. (optional) Turn off the router. Disconnect the adaptor. Turn off your PC.

Step 7 - Copy the sysupgrade image to the router.

We now need to copy the sysupgrade image to the router (tmp directory). This time the router will be the server and the PC will be the client. For more info see "Troubleshooting, If you cant use 'wget'".

  1. Repeat Steps 1 - 5 (in case you turned off the router/PC)
  2. Use scp: scp openwrt-ramips-mt7621-tplink_ex220-v1-squashfs-sysupgrade.bin root@192.168.1.1:/tmp (Ensure you have set a non-null password for your device root account to properly use scp).

Step 8 - Flash OpenWrt through either LuCI or the sysupgrade command.

We will use the sysupgrade command.

  1. Execute command sysupgrade -v /tmp/firmware_image.bin. More info here, "Flash the new OpenWrt firmware" .
@cirotrc
Copy link

cirotrc commented Apr 19, 2024

Rodrigo faz um vídeo, tem uma galera com esse modem querendo trocar a firmware dele mas não consegue. eu por exemplo sou um. nesse passo a passo não consegui. valeu
pessoal da comu la do openwrt nao ajudou até baniram eu eu outro rapaz la....

@ro99
Copy link
Author

ro99 commented May 4, 2024

Oi Ciro, qual a parte que está dando problema para você? Usando a opção 5 (Load Image), também não funciona?

@rdlf4
Copy link

rdlf4 commented Jul 2, 2024

Uma dúvida, você definiu o endereço IP do computador para 192.168.0.2;
Daí definiu também as Variáveis de ambiente - 192.168.0.2 para servidor, que será o computador e 192.168.0.1 para o roteador, destinatário do arquivo bin.
Mas em seguida, você manda copiar o arquivo para a /tmp de 192.168.1.1 (?)
Não seria mandar copiar para 192.168.0.1?

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