Apr 19, 2013

TP-Link WR703N Debrick with Arduino UNO

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. - source

0 comments:

Post a Comment