Topic: TP-Link WR703N Debrick with Arduino UNO

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.

Re: TP-Link WR703N Debrick with Arduino UNO

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

Re: TP-Link WR703N Debrick with Arduino UNO

Nice idea ! Thanks for sharing !

4 (edited by pierotofy 2013-02-09 01:18:29)

Re: TP-Link WR703N Debrick with Arduino UNO

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.

Re: TP-Link WR703N Debrick with Arduino UNO

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.

Re: TP-Link WR703N Debrick with Arduino UNO

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