OpenWrt Forum Archive

Topic: TP-Link WR703N Debrick with Arduino UNO

The content of this topic has been archived on 25 Apr 2018. There are no obvious gaps in this topic, but there may still be some posts missing at the end.

Greetings OpenWrt community!

I recently flashed OpenWrt on a TP-Link WR703N and got locked out (Ethernet was not working and WiFi is disabled by default). I soldered the wires to get serial access, but I did not want to purchase a USB serial adapter (could have, but decided not to). I wanted to see if I could use an Arduino UNO I had laying around to debrick my device, so I came up with this small sketch.

/*
  Arduino OpenWRT Serial Debrick Sketch
  Piero Toffanin - 2013
  
  Usage:
    Power off the TP-Link
    Connect TP_OUT --> Arduino RX Pin
    Connect TP_IN --> Arduino TX Pin
    (Optional) Connect 1000 Ohm resistor in series with an Led --> Pin 13
    Setup TFTP server with IP 192.168.1.100/24
    Place your image in the root TFTP folder and rename it "openwrt.bin"
    Connect Ethernet cable from TP-Link to TFTP server
    Upload Sketch to Arduino
    Power up the TP-Link
    LED will turn on during the de-bricking process
    Wait ~1 minute
    LED will flash 10 times when process is over
    Remove connections
    Reboot the TP-Link by turning it off and on
    Buy me a beer
  
    MIT License
  Copyright © 2013

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the “Software”), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:
  
  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.
  
  THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  THE SOFTWARE.
*/


#define BUFZSIZE 800
#define DEBUGPIN 13 // Connect a resistor + LED to this pin to see debug activity

char buffer[BUFZSIZE];
int c;
boolean tpl;
int command;
boolean done;
void setup()  
{
  pinMode(DEBUGPIN, OUTPUT);
  digitalWrite(DEBUGPIN, LOW);
  
  Serial.begin(115200);
  int i;
  for (int i = 0; i < BUFZSIZE; i++) buffer[i] = 0;
  tpl = false;
  done = false;
  command = 0;
}

void loop() // run over and over
{
  start:
  if (!done && Serial.available() > 0){
    char ch = Serial.read();
    if (ch == '#') goto start;
    buffer[c] = ch;
    if (!tpl && c >= 6 &&
        buffer[c] == 's' && 
        buffer[c-1] == 'd' && 
        buffer[c-2] == 'n' && 
        buffer[c-3] == 'o' && 
        buffer[c-4] == 'c' && 
        buffer[c-5] == 'e' && 
        buffer[c-6] == 's'){
        digitalWrite(DEBUGPIN, HIGH);
        delay(400);
        Serial.print("tpl\n");
        digitalWrite(DEBUGPIN, LOW);
        tpl = true;
        c = 0;
     }else if (tpl){
        if (c >= 6 &&
            buffer[c] == '>' && 
            buffer[c-1] == 't' && 
            buffer[c-2] == 'e' && 
            buffer[c-3] == 'n' && 
            buffer[c-4] == 'r' && 
            buffer[c-5] == 'o' &&
            buffer[c-6] == 'h'){
              digitalWrite(DEBUGPIN, HIGH);
              delay(8000); 
              if (command == 0){
                Serial.print("setenv ipaddr 192.168.1.101\n");
              }else if (command == 1){
                Serial.print("setenv serverip 192.168.1.100\n");  
              }else if (command == 2){
                Serial.print("tftpboot 0x81000000 openwrt.bin\n");
              }else if (command == 3){
                Serial.print("erase 0x9f020000 +0x3c0000\n");     
              }else if (command == 4){
                Serial.print("cp.b 0x81000000 0x9f020000 0x3c0000\n");
              }else if (command == 5){
                
                //Serial.print("boot.m 9f020000\n");
                //Serial.print("reset\n");
                
                done = true;
                int j;
                for (j = 0; j < 10; j++){
                  digitalWrite(DEBUGPIN, LOW);
                  delay(500);
                  digitalWrite(DEBUGPIN, HIGH);
                  delay(500);
                }
  
                digitalWrite(DEBUGPIN, LOW);

/* Uncomment to print out buffer at the end (useful for debugging)                
                for (j = 0; j < BUFZSIZE; j++){
                   Serial.print(buffer[j]);
                }
*/
                // Stop
                while(true){
                   delay(100);
                }
            } 
            
            command++;
        }
            
     }
     
     if (c++ > BUFZSIZE) c = 0;
  } 
}

The wiring is trivial and although Arduino's logic levels are different than the TP-Link it seems to work quite nicely. It basically waits for the proper U-boot message, sends the "tpl" command to enter the console and then sends the recovery commands.

I thought I'd share.

Neat. If you remove the atmel chip (assuming they are in sockets on the uno) you can use the board as a usb-serial converter, but your solution adds value.
Do you want to add it to the WiKi:
http://wiki.openwrt.org/doc/howto/generic.debrick

Nice idea ! Thanks for sharing !

I tried to remove the Atmega chip before I attempted to write this sketch to see if I could use it as a usb-serial converter, but for some reason I didn't get anything on the serial console. Not sure if the logic levels have anything to do with this.

I added a note about this method in the Wiki.

(Last edited by pierotofy on 9 Feb 2013, 01:18)

I know this is a old thread, but since its linked in a wiki and is considered current...

Firstly thanks for the sketch it will be handy.

Secondly, if you wish to use your Arduino as a straight serial to TTL device, jump RESET pin to GND this will isolate the Atmel from its I/O pins and let the Arduino act as a simple usb to serial. Useful in Putty or any other serial terminal.

You deserve that beer, this unbricked my router too.

As a side note, I recommend that connection between tftp server and router to be done using a switch, not directly. At least in Windows 7, it took some time for the interface of the PC to be brought up, so it is better to be always up, no matter what is the state of the Ethernet interface of the router.

Anyway, the idea is brilliant and saved me a lot of time.

Merry Christmas! smile

Hi, sorry for reactivate of this thread but i have a doubt,

In my case, i bricked my TL-WDR3600 (N600) Samknows Version by flashing it with the wrong firmware.. Now i'm hopping to unbrick it with terminal access.
So, i'm guessing if i have to buy a USB to TTL cable or i use my Arduino to do it!
Is this code compatible with my Arduino Duemilanove and my TP-link router model?

Best Regards,

(Last edited by tiagoermida on 29 Sep 2014, 14:48)

The discussion might have continued from here.