8836cc6633cc0fbc44c4d0c4e17092937e9e59d4
[oweals/u-boot_mod.git] / README.md
1 U-Boot 1.1.4 modification for routers
2 ==========
3
4 Table of contents
5 -----------------
6
7 [README w jÄ™zyku polskim/README in Polish language](https://github.com/pepe2k/u-boot_mod/blob/master/READMEPL.md)
8
9 - [Introduction](#introduction)
10 - [Supported devices](#supported-devices)
11 - [Known issues](#known-issues)
12 - [Modifications, changes](#modifications-changes)
13         - [Web server](#web-server)
14         - [Network Console](#network-console)
15         - [Other](#other)
16         - [Supported FLASH chips](#supported-flash-chips)
17 - [How to install it?](#how-to-install-it)
18         - [Cautions, backups](#cautions-backups)
19         - [Using external programmer](#using-external-programmer)
20         - [Using UART, U-Boot console and TFTP server](#using-uart-u-boot-console-and-tftp-server)
21                 - [Important notice!](#important-notice)
22                 - [Step by step instruction](#step-by-step-instruction)
23         - [Using OpenWrt](#using-openwrt)
24         - [Using DD-WRT](#using-dd-wrt)
25 - [How to use it?](#how-to-use-it)
26 - [How to compile the code?](#how-to-compile-the-code)
27 - [FAQ](#faq)
28 - [License, outdated sources etc.](#license-outdated-sources-etc)
29 - [Credits](#credits)
30
31 Introduction
32 ------------
33
34 In short, this project is a deep modification of **U-Boot 1.1.4** sources, mostly from **TP-Link**, but some code fragments were taken also from **D-Link**.
35
36 You can download original sources from the following pages:
37
38 - [TP-Link GPL Code Center](http://www.tp-link.com/en/support/gpl/ "TP-Link GPL Code Center")
39 - [D-Link GPL Source Code Support](http://tsd.dlink.com.tw/GPL.asp "D-Link GPL Source Code Support")
40
41 The concept for this project came from another U-Boot modification, dedicated to a small and very popular TP-Link router - model **TL-WR703N**, which includes web fail safe mode: **[wr703n-uboot-with-web-failsafe](http://code.google.com/p/wr703n-uboot-with-web-failsafe/)**. I was using it and decided to make my own version, which could have some improvements, additional capabilities, support for different models and work with all modern web browsers.
42
43 First version of this modification was introduced on **OpenWrt** forum in [this thread](https://forum.openwrt.org/viewtopic.php?id=43237), at the end of March 2013 and was dedicated only for TP-Link routers with **Atheros AR9331** SoC. Now, it supports also models from different manufacturers, devices with **Atheros AR934x** (like **TP-Link TL-WDR3600**, **TL-WDR43x0**, **TL-WR841N/D v8**, **TL-WA830RE v2**) and other (in the near future **Qualcomm Atheros QCA955x**) are under development.
44
45 You can find some information about previous versions of this project also on my [blog](http://www.tech-blog.pl), in [this article](http://www.tech-blog.pl/2013/03/29/zmodyfikowany-u-boot-dla-routerow-tp-link-z-atheros-ar9331-z-trybem-aktualizacji-oprogramowania-przez-www-i-konsola-sieciowa-netconsole/). It is in Polish, but [Google Translator](http://translate.google.com/translate?hl=pl&sl=pl&tl=en&u=http%3A%2F%2Fwww.tech-blog.pl%2F2013%2F03%2F29%2Fzmodyfikowany-u-boot-dla-routerow-tp-link-z-atheros-ar9331-z-trybem-aktualizacji-oprogramowania-przez-www-i-konsola-sieciowa-netconsole%2F&sandbox=1) will help you to understand it.
46
47 If you like this project, you may [buy me a beer](https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=FN3XW36YHSY2S&lc=US&item_name=For%20a%20great%20job%21&currency_code=USD&bn=PP%2dDonationsBF%3abtn_donate_LG%2egif%3aNonHosted)!
48
49 Supported devices
50 -----------------
51
52 Currently supported devices:
53
54 - **Atheros AR9331**:
55   - 8devices Carambola 2 (for version with development board, [photos in my gallery](http://galeria.tech-blog.pl/8devices_Carambola_2/))
56   - TP-Link TL-MR3020 v1 ([photos in my gallery](http://galeria.tech-blog.pl/TPLINK_TL-MR3020/))
57   - TP-Link TL-MR3040 v1 and v2
58   - TP-Link TL-WR703N v1, ([photos in my gallery](http://galeria.tech-blog.pl/TPLINK_TL-WR703N/))
59   - TP-Link TL-WR720N v3 (version for Chinese market)
60   - TP-Link TL-WR710N v1 (version for European market, [photos in my gallery](http://galeria.tech-blog.pl/TP-Link_TL-WR710N-EU/))
61   - TP-Link TL-MR10U v1 ([photos in my gallery](http://galeria.tech-blog.pl/TP-Link_TL-MR10U/))
62   - TP-Link TL-MR13U v1
63   - TP-Link TL-WR740N v4 (and similar, like TL-WR741ND v4)
64   - TP-Link TL-MR3220 v2
65   - GS-Oolite/Elink EL-M150 module with dev board ([photos in my gallery](http://galeria.tech-blog.pl/Elink_EL-M150_Development-Board/))
66   - Dragino 2 (MS14)
67
68 - **Atheros AR1311 (similar to AR9331)**
69   - D-Link DIR-505 H/W ver. A1 ([photos in my gallery](http://galeria.tech-blog.pl/D-Link_DIR-505/))
70
71 - **Atheros AR9341**:
72   - TP-Link TL-MR3420 v2
73   - TP-Link TL-WR841N/D v8
74   - TP-Link TL-WA830RE v2
75   - TP-Link TL-WA801ND v2
76
77 - **Atheros AR9344**:
78   - TP-Link TL-WDR3600 v1
79   - TP-Link TL-WDR43x0 v1
80   - TP-Link TL-WDR3500 v1
81
82 I tested this modification on most of these devices, with OpenWrt and OFW firmware. If you are not sure about the version of your device, please contact with me **before** you try to make an upgrade. Changing bootloader to a wrong version will probably damage your router and you will need special hardware to fix it, so please, **be very careful**.
83
84 More information about supported devices:
85
86 | Model | SoC | FLASH | RAM | U-Boot image | U-Boot env |
87 |:--- | :--- | ---: | ---: | ---: | ---: |
88 | [8devices Carambola 2](http://8devices.com/carambola-2) | AR9331 | 16 MiB | 64 MiB DDR2 | 256 KiB | 64 KiB, R/W |
89 | [TP-Link TL-MR3020 v1](http://wiki.openwrt.org/toh/tp-link/tl-mr3020) | AR9331 | 4 MiB | 32 MiB DDR1 | 64 KiB, LZMA | RO |
90 | [TP-Link TL-MR3040 v1/2](http://wiki.openwrt.org/toh/tp-link/tl-mr3040) | AR9331 | 4 MiB | 32 MiB DDR1 | 64 KiB, LZMA | RO |
91 | [TP-Link TL-WR703N](http://wiki.openwrt.org/toh/tp-link/tl-wr703n) | AR9331 | 4 MiB | 32 MiB DDR1 | 64 KiB, LZMA | RO |
92 | [TP-Link TL-WR720N v3](http://wiki.openwrt.org/toh/tp-link/tl-wr720n) | AR9331 | 4 MiB | 32 MiB DDR1 | 64 KiB, LZMA | RO |
93 | [TP-Link TL-WR710N v1](http://wiki.openwrt.org/toh/tp-link/tl-wr710n) | AR9331 | 8 MiB | 32 MiB DDR1 | 64 KiB, LZMA | RO |
94 | [TP-Link TL-MR10U v1](http://wiki.openwrt.org/toh/tp-link/tl-mr10u) | AR9331 | 4 MiB | 32 MiB DDR1 | 64 KiB, LZMA | RO |
95 | [TP-Link TL-MR13U v1](http://wiki.openwrt.org/toh/tp-link/tl-mr13u) | AR9331 | 4 MiB | 32 MiB DDR1 | 64 KiB, LZMA | RO |
96 | [TP-Link TL-WR740N v4](http://wiki.openwrt.org/toh/tp-link/tl-wr740n) | AR9331 | 4 MiB | 32 MiB DDR1 | 64 KiB, LZMA | RO |
97 | [TP-Link TL-MR3220 v2](http://wiki.openwrt.org/toh/tp-link/tl-mr3420) | AR9331 | 4 MiB | 32 MiB DDR1 | 64 KiB, LZMA | RO |
98 | GS-Oolite/Elink EL-M150 module | AR9331 | 4/8/16 MiB | 64 MiB DDR2 | 64 KiB, LZMA | RO |
99 | [Dragino 2 (MS14)](http://wiki.openwrt.org/toh/dragino/ms14) | AR9331 | 16 MiB | 64 MiB DDR1 | 192 KiB | R/W |
100 | [TP-Link TL-MR3420 v2](http://wikidevi.com/wiki/TP-LINK_TL-MR3420_v2) | AR9341 | 4 MiB | 32 MiB DDR1 | 64 KiB, LZMA | RO |
101 | [TP-Link TL-WR841N/D v8](http://wiki.openwrt.org/toh/tp-link/tl-wr841nd) | AR9341 | 4 MiB | 32 MiB DDR1 | 64 KiB, LZMA | RO |
102 | [TP-Link TL-WA830RE v2](http://wikidevi.com/wiki/TP-LINK_TL-WA830RE_v2) | AR9341 | 4 MiB | 32 MiB DDR1 | 64 KiB, LZMA | RO |
103 | [TP-Link TL-WA801ND v2](http://wikidevi.com/wiki/TP-LINK_TL-WA801ND_v2) | AR9341 | 4 MiB | 32 MiB DDR1 | 64 KiB, LZMA | RO |
104 | [TP-Link TL-WDR3600 v1](http://wiki.openwrt.org/toh/tp-link/tl-wdr3600) | AR9344 | 8 MiB | 128 MiB DDR2 | 64 KiB, LZMA | RO |
105 | [TP-Link TL-WDR43x0 v1](http://wiki.openwrt.org/toh/tp-link/tl-wdr4300) | AR9344 | 8 MiB | 128 MiB DDR2 | 64 KiB, LZMA | RO |
106 | [TP-Link TL-WDR3500 v1](http://wiki.openwrt.org/toh/tp-link/tl-wdr3500) | AR9344 | 8 MiB | 128 MiB DDR2 | 64 KiB, LZMA | RO |
107 | [D-Link DIR-505 H/W ver. A1](http://wiki.openwrt.org/toh/d-link/dir-505) | AR1311 | 8 MiB | 64 MiB DDR2 | 64 KiB, LZMA | RO |
108
109 *(LZMA) - U-Boot binary image is compressed with LZMA.*  
110 *(R/W) - environment exists in separate FLASH block which allows you to save it and keep after power down.*
111 *(RO) - environment is read only, you can change and add new variables only during runtime.*
112
113 Known issues
114 ------------
115
116 Current release is not loading kernel from some versions of TP-Link's official firmware. If you want to use the so-called OFW in any of supported TP-Link's router, do not use this modification. I am working on a solution for this issue.
117
118 Modifications, changes
119 ----------------------
120
121 ### Web server
122
123 The most important change is an inclusion of a web server, based on **[uIP 0.9 TCP/IP stack](http://www.gaisler.com/doc/net/uip-0.9/doc/html/main.html)**. It allows to upgrade **firmware**, **U-Boot** and **ART** (Atheros Radio Test) images, directly from your web browser, without need to access serial console and running a TFTP server. You can find similar firmware recovery mode, also based on uIP 0.9 TCP/IP stack, in **D-Link** routers.
124
125 Web server contains 7 pages:
126
127 1. index.html (allows to upgrade firmware image, screenshot below)
128 2. uboot.html (allows to upgrade U-Boot image)
129 3. art.html (allows to upgrade ART image)
130 4. flashing.html
131 5. 404.html
132 6. fail.html
133 7. style.css
134
135 ![](http://www.tech-blog.pl/wordpress/wp-content/uploads/2013/08/uboot_mod_firmware_upgrade.jpg)
136
137 ### Network Console
138
139 Second, very useful modification is a network console (it is a part of original U-Boot sources, but none of the manufacturers included it). It allows you to communicate with U-Boot console over the Ethernet, using UDP protocol (default UDP port: 6666, router IP: 192.168.1.1).
140
141 ![](http://www.tech-blog.pl/wordpress/wp-content/uploads/2013/04/u-boot_mod_for_tp-link_with_ar9331_netconsole.jpg)
142
143 ### Other
144
145 Moreover:
146
147 - Faster boot up
148 - Unnecessary information from boot up sequence were removed
149 - FLASH chip is automatically recognized (using JEDEC ID)
150 - Ethernet MAC is set from FLASH (no more "No valid address in FLASH. Using fixed address")
151 - Automatic kernel booting can be interrupted using any key
152 - Press and hold reset button to run:
153   - Web server (min. 3 seconds)
154   - U-Boot serial console (min. 5 seconds)
155   - U-Boot network console (min. 7 seconds)
156 - Additional commands (in comparison to the default version; availability depends on router model):
157   -  httpd
158   -  printmac
159   -  setmac
160   -  printmodel
161   -  printpin
162   -  startnc
163   -  startsc
164   -  eraseenv
165   -  ping
166   -  dhcp
167   -  sntp
168   -  iminfo
169 - Overclocking and underclocking possibilities (for now, only routers with AR9331)
170
171 ### Supported FLASH chips
172
173 FLASH type detection may be very useful for people who has exchanged the FLASH chip in their routers. You will not need to recompile U-Boot sources, to have access to overall FLASH space in U-Boot console.
174
175 If you use FLASH type which is not listed below, this version of U-Boot will use default size for your router and, in most supported models, updating the ART image will not be available.
176
177 Currently supported FLASH types:
178
179 **4 MiB**:
180
181 - Spansion S25FL032P (4 MiB, JEDEC ID: 01 0215)*
182 - Atmel AT25DF321 (4 MiB, JEDEC ID: 1F 4700)
183 - EON EN25Q32 (4 MiB, JEDEC ID: 1C 3016)*
184 - EON EN25F32 (4 MiB, JEDEC ID: 1C 3116)*
185 - Micron M25P32 (4 MiB, JEDEC ID: 20 2016)
186 - Windbond W25Q32 (4 MiB, JEDEC ID: EF 4016)
187 - Macronix MX25L320 (4 MiB, JEDEC ID: C2 2016)
188
189 **8 MiB**:
190
191 - Spansion S25FL064P (8 MiB, JEDEC ID: 01 0216)
192 - Atmel AT25DF641 (8 MiB, JEDEC ID: 1F 4800)
193 - EON EN25Q64 (8 MiB, JEDEC ID: 1C 3017)*
194 - Micron M25P64 (8 MiB, JEDEC ID: 20 2017)
195 - Windbond W25Q64 (8 MiB, JEDEC ID: EF 4017)*
196 - Macronix MX25L64 (8 MiB, JEDEC ID: C2 2017, C2 2617)
197
198 **16 MiB**:
199
200 - Winbond W25Q128 (16 MB, JEDEC ID: EF 4018)*
201 - Macronix MX25L128 (16 MB, JEDEC ID: C2 2018, C2 2618)
202 - Spansion S25FL127S (16 MB, JEDEC ID: 01 2018)*
203
204 (*) tested
205
206 If you want to use other type, please contact with me or make changes in the code, test them and send a pull request or a patch.
207
208 How to install it?
209 ------------------
210
211 ### Cautions, backups
212
213 **You do so at your own risk!**   
214 **If you make any mistake or something goes wrong during upgrade, in worst case, your router will not boot again!**
215
216 It is a good practice to backup your original U-Boot image/partition (especially for TP-Link devices) **before** you make any changes. For example, using OpenWrt (TP-Link TL-WR703N with 16 MiB FLASH):
217
218 ```
219 cat /proc/mtd
220 ```
221
222 This command will show you all **MTD** (Memory Technology Device) partitions:
223
224 ```
225 dev:    size   erasesize  name
226 mtd0: 00020000 00010000 "u-boot"
227 mtd1: 000eeb70 00010000 "kernel"
228 mtd2: 00ee1490 00010000 "rootfs"
229 mtd3: 00c60000 00010000 "rootfs_data"
230 mtd4: 00010000 00010000 "art"
231 mtd5: 00fd0000 00010000 "firmware"
232 ```
233
234 As you can see, `u-boot` partition size is **0x20000** (128 KiB) and my image for this model has size of **0x10000** (64 KiB) - it is a very important difference! You should remember about this if you want to use `mtd` utility, to change U-Boot.
235
236 To backup `u-boot` partition in RAM, run:
237
238 ```
239 cat /dev/mtd0 > /tmp/uboot_backup.bin
240 ```
241
242 And then connect to your router using `SCP protocol` and download from `/tmp` the `uboot_backup.bin` file.
243
244 ### Using external programmer
245
246 If you have an external FLASH programmer (all supported devices have **SPI NOR FLASH** chips), you probably know how to use it. Download package with prebuilt images or compile the code, choose right file for your device and put it on FLASH at the beginning (offset `0x00000`). Remember to first erase block(s) - with high probability, if you use some kind of automatic mode, the programmer will do it for you.
247
248 All prebuilt images are padded with 0xFF, so their size will always be a **multiple of 64 KiB block** and they will not be bigger than the original versions. For example, **TP-Link** uses only first **64 KiB** block to store compressed U-Boot image (in most of their modern devices). In the second 64 KiB block they store additional information like MAC address, model number and WPS pin number.
249
250 On the other hand, U-Boot image in **Carambola 2** from **8devices** may have up to **256 KiB** (4x 64 KiB block), they use uncompressed version and environment stored in FLASH. Immediately after the Carambola 2 U-Boot partition is an area which contains U-Boot environment variables (1x 64 KiB block), called `u-boot-env`:
251
252 ```
253 dev:    size   erasesize  name
254 mtd0: 00040000 00010000 "u-boot"
255 mtd1: 00010000 00010000 "u-boot-env"
256 mtd2: 00f90000 00010000 "firmware"
257 mtd3: 00e80000 00010000 "rootfs"
258 mtd4: 00cc0000 00010000 "rootfs_data"
259 mtd5: 00010000 00010000 "nvram"
260 mtd6: 00010000 00010000 "art"
261 ```
262
263 ### Using UART, U-Boot console and TFTP server
264
265 It is probably the most common method to change firmware in case of any problems. Main disadvantage of this approach is the need to connect with device using a serial port (this does not apply to Carambola 2 with development board, which already has a built-in USB-UART adapter, based on FTDI FT232RQ).
266
267 #### Important notice!
268
269 All these devices have an UART interface integrated inside the SoC, which operates at TTL 3.3 V (in fact, GPIO pins can work at this voltage, but their real range is < 3 V)!
270
271 Please, **do not** connect any RS232 +/- 12 V cable or any adapter without logic level converter, because it may damage your device. It would be the best if you use any USB to UART adapter with integrated 3.3 V logic level converter. And please, remember that **you should connect only RX, TX and GND signals**. **DO NOT** connect together 3.3 V signals from router and from adapter if you do not know what are you doing, because you may burn out your adapter and/or router! Connect the adapter using USB port in your PC and router with original power supply.
272
273 For a long time I have been using without any problems a small and very cheap (about 1-2 USD) **CP2102** based adapter. Go to [Serial Console article in OpenWrt Wiki](http://wiki.openwrt.org/doc/hardware/port.serial) for more, detailed information.
274
275 #### Step by step instructions
276
277 1. Install and configure any **TFTP server** on your PC (on Windows, you can use [TFTP32](http://tftpd32.jounin.net)).
278
279 2. Set a fixed IP address on your PC (in this tutorial we will use **192.168.1.2** for the PC and **192.168.1.1** for the router) and connect it to the router, using RJ45 network cable (in most case you will need to use one of the available LAN ports, but WAN port should also work).
280
281 3. Connect USB to UART adapter to the router and start any application to communicate with it, like [PuTTY](http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html).   
282 Configure adapter to use the following settings:
283   * Baud rate: 115200
284   * Data bits: 8
285   * Parity control: none
286   * Stop bits: 1
287   * Handshaking: none
288 4. Power on the router, wait for a line like one of the following and interrupt the process of loading a kernel:
289
290   `Autobooting in 1 seconds` (for most **TP-Link** routers, you should enter `tpl` at this point)   
291   `Hit ESC key to stop autoboot:  1` (for **8devices Carambola 2**, use `ESC` key)   
292   `Hit any key to stop autoboot:  1` (for **D-Link DIR-505**, use any key)
293
294 5. Set `ipaddr` and `serverip` environment variables:
295
296   ```
297   hornet> setenv ipaddr 192.168.1.1
298   hornet> setenv serverip 192.168.1.2
299   ```
300 6. Check the changes:
301
302   ```
303   hornet> printenv ipaddr
304   ipaddr=192.168.1.1
305   hornet> printenv serverip
306   serverip=192.168.1.2
307   ```
308
309 7. Download and store in RAM proper image for your router, using `tftpboot` command in U-Boot console (in this example, for **TP-Link TL-MR3020**):
310
311   ```
312   tftpboot 0x80800000 uboot_for_tp-link_tl-mr3020.bin
313
314   eth1 link down
315   Using eth0 device
316   TFTP from server 192.168.1.2; our IP address is 192.168.1.1
317   Filename 'uboot_for_tp-link_tl-mr3020.bin'.
318   Load address: 0x80800000
319   Loading: #############
320   done
321   Bytes transferred = 65536 (10000 hex)
322   hornet>
323   ```
324
325 8. Next step is very risky! You are going to delete existing U-Boot image from FLASH in your device and copy from RAM the new one. If something goes wrong (for example, a power failure), your router, without bootloader, will not boot again!
326
327   You should also note the size of downloaded image. For supported **TP-Link** and **D-Link** routers it will be always **0x10000** (64 KiB), but for Carambola 2 image size is different: **0x40000** (256 KiB). In all cases, the start address of FLASH is **0x9F000000** and for RAM: **0x80000000** (as you may noticed, I did not use start address of RAM to store image and you should follow this approach).
328
329   Please, do not make any mistake with offsets and sizes during next steps!
330
331 9. Erase appropriate FLASH space for new U-Boot image (this command will remove default U-Boot image!):
332
333   ```
334   hornet> erase 0x9F000000 +0x10000   
335
336   First 0x0 last 0x0 sector size 0x10000
337   0
338   Erased 1 sectors
339   ```
340
341 10. Now your router does not have U-Boot, so do not wait and copy to FLASH the new one, stored earlier in RAM:
342
343   ```
344   hornet> cp.b 0x80800000 0x9F000000 0x10000   
345
346   Copy to Flash... write addr: 9f000000
347   done
348   ```
349
350 11. If you want, you can check content of the newly written FLASH and compare it to the image on your PC (or better also do such a "legit memory content" comparison prior to writing!), using `md` command in U-Boot console, which prints indicated memory area (press only ENTER after first execution of this command to move further in memory):
351
352   ```
353   hornet> md 0x9F000000
354
355   9f000000: 100000ff 00000000 100000fd 00000000    ................
356   9f000010: 10000222 00000000 10000220 00000000    ..."....... ....
357   9f000020: 1000021e 00000000 1000021c 00000000    ................
358   9f000030: 1000021a 00000000 10000218 00000000    ................
359   9f000040: 10000216 00000000 10000214 00000000    ................
360   9f000050: 10000212 00000000 10000210 00000000    ................
361   9f000060: 1000020e 00000000 1000020c 00000000    ................
362   9f000070: 1000020a 00000000 10000208 00000000    ................
363   9f000080: 10000206 00000000 10000204 00000000    ................
364   9f000090: 10000202 00000000 10000200 00000000    ................
365   9f0000a0: 100001fe 00000000 100001fc 00000000    ................
366   9f0000b0: 100001fa 00000000 100001f8 00000000    ................
367   9f0000c0: 100001f6 00000000 100001f4 00000000    ................
368   9f0000d0: 100001f2 00000000 100001f0 00000000    ................
369   9f0000e0: 100001ee 00000000 100001ec 00000000    ................
370   9f0000f0: 100001ea 00000000 100001e8 00000000    ................
371   ```
372
373 12. If you are sure that everything went OK, you may reset the board:
374
375   ```
376   hornet> reset
377   ```
378
379 ### Using OpenWrt
380
381 [TODO]
382
383 ### Using DD-WRT
384
385 #### Step by step instructions
386
387 Tutorial untested and taken from [here](http://zhujunsan.net/index.php/2013/08/install-web-failsafe-u-boot-for-wr703n/).
388
389 1. Copy the following, and save to a shell script on your local computer (e.g. makeu.sh)
390
391     ```bash
392     #! /bin/sh
393     # high chance need have a change ...
394     
395     UBOOT_NAME=wr703n_tuboot_test_2012_06_06.bin
396     MD5SUM_SHOULD_BE="623dc0bba6fab68c22e5fb2f329d7d09"
397     
398     # need check the md5sum, any one byte in bootloader shoud right ...
399     CURRENT_MD5SUM_VAL=$( md5sum $UBOOT_NAME |awk '{print $1 }' )
400     echo "$UBOOT_NAME md5sum : $CURRENT_MD5SUM_VAL"
401
402     if [ $MD5SUM_SHOULD_BE = $CURRENT_MD5SUM_VAL ]; then
403         echo "$UBOOT_NAME md5sum check pass"
404     else
405         echo "###############$UBOOT_NAME md5sum check fail###############"
406         exit
407     fi
408     
409     RAW_UBOOT_LEN=`wc -c $UBOOT_NAME | awk '{print $1 }'`
410     NEED_PAD_LEN=$((0x1fc00-$RAW_UBOOT_LEN))
411     
412     # Generate a file used as pad ...
413     dd if=/dev/zero of=pad.bin bs=1 count=$NEED_PAD_LEN
414     cat $UBOOT_NAME pad.bin >tuboot_0x1fc00.bin
415     
416     echo "Backup some config first, just like MAC address ..."
417     dd if=/dev/mtd0 of=./config.bin bs=1 skip=$((0x1fc00))
418     cat ./tuboot_0x1fc00.bin ./config.bin >uboot_0x20000.bin
419     ```
420
421 2. Edit the lines 3 and 4 to the name of the file you will flash, and its MD5 sum. e.g.:
422
423     ```bash
424     ..
425     UBOOT_NAME=uboot_for_tl-wr740n_v4.bin
426     MD5SUM_SHOULD_BE="0c09e1d87724dc3e45872e8641b28916"
427     ..
428     ```
429
430     **IMPORTANT**: If you're editing the file in Windows, it's best to use Notepad++. Before you save **make sure** to **Edit -> EOL Conversion -> UNIX/OSX Format**. Otherwise, your script will not run, and will return an Error 2(it stops seeing the "then" on line 8, because the EOL character breaks it).
431  
432     You can grab the bin file's sum by running **md5sum uboot_for_tl-wr740n_v4.bin** under Linux, or use a tool like [HashTab for Windows](http://implbits.com/HashTab/HashTabWindows.aspx).
433
434 3. Once you have your shell script, and your bin file, place them in a folder(in this example, we'll call it **yourfolder**), and SCP the folder into /tmp on the router (for Windows, you can use [WinSCP](http://winscp.net/eng/index.php)).
435
436 4. SSH into the router, and run the following first, to make a backup of your current U-Boot partition:
437
438     ```bash
439     cd /tmp/yourfolder
440     cat /dev/mtd0 > ./uboot_backup.bin
441     ```
442     
443     SCP that file back to your computer for safe keeping.
444
445 5. Fix your script's permissions by invoking:
446
447     ```bash
448     chmod 777 /tmp/yourfolder/makeu.sh
449     ```
450
451 6. Run the builder script by invoking:
452
453     ```bash
454     ./makeu.sh
455     ```
456     
457     You must get output similar to:
458
459     ```ShellSession
460     root@superhornet:/tmp/superhornet# chmod 777 makeu.sh
461     root@superhornet:/tmp/superhornet# ./makeu.sh
462     uboot_for_tl-wr740n_v4.bin md5sum : 0c09e1d87724dc3e45872e8641b28916
463     uboot_for_tl-wr740n_v4.bin md5sum check pass
464     64512+0 records in
465     64512+0 records out
466     Backup some config first,just like MAC address ...
467     1024+0 records in
468     1024+0 records out
469     root@superhornet:/tmp/superhornet#
470     ```
471
472     This will output several files into yourfolder. The idea is that this takes part of mtd0 from address 0x1fc00 onwards, and then writes out the 64KB U-boot bin, followed by some padding, and the the information from 0x1fc00 to build a full 128KB U-boot that also contains your router's MAC Address. This is required, in order to make a file that's safely flashable. The one you're interested in will be **uboot_0x20000.bin**.
473
474 7. Explore your folder, to make sure the file you're about to flash (uboot_0x200000.bin) has the correct non-zero length:
475     ```bash
476     ls -l
477     ```
478
479     Example output:
480     
481     ```ShellSession
482     root@superhornet:/tmp/superhornet# ls -l
483     -rw-r--r--    1 root     root          1024 May 19 14:06 config.bin
484     -rwxrwxrwx    1 root     root           876 May 18 16:03 makeu.sh
485     -rw-r--r--    1 root     root         64512 May 19 14:06 pad.bin
486     -rw-r--r--    1 root     root        130048 May 19 14:06 tuboot_0x1fc00.bin
487     -rw-r--r--    1 root     root        131072 May 19 14:06 uboot_0x20000.bin
488     -rw-r--r--    1 root     root        131072 May 18 17:45 uboot_backup.bin
489     -rw-r--r--    1 root     root         65536 Jun 19  2013 uboot_for_tl-wr740n_v4.bin
490     root@superhornet:/tmp/superhornet#
491     ```
492
493 8. Check the name of your mtd0 partition:
494
495     ```bash
496     cat /proc/mtd
497     ```
498
499     e.g.:
500
501     ```ShellSession
502     root@superhornet:/tmp/superhornet# cat /proc/mtd
503     dev:    size   erasesize  name
504     mtd0: 00020000 00010000 "RedBoot"
505     mtd1: 003c0000 00010000 "linux"
506     mtd2: 002c0000 00010000 "rootfs"
507     mtd3: 00010000 00010000 "ddwrt"
508     mtd4: 00010000 00010000 "nvram"
509     mtd5: 00010000 00010000 "board_config"
510     mtd6: 00400000 00010000 "fullflash"
511     mtd7: 00020000 00010000 "fullboot"
512     root@superhornet:/tmp/superhornet#
513     ```
514     
515     The one you want is mtd0. In some devices it may be labeled "u-boot", instead of "RedBoot". As long as the size reads 00020000(128KB), you are ok.
516
517 9. (Point of no return step!) Flash the created image onto your mtd0. Invoke:
518
519     ```bash
520     mtd write uboot_0x20000.bin RedBoot
521     ```
522
523     In the above RedBoot is the name of the partition - it may be different between devices. This was tested successfully on TP-LINK TL-WR740 v4.23 board with a **EON EN25Q32 (4 MiB, JEDEC ID: 1C 3016)** memory chip.
524
525     If an error is displayed at any point **IMMEDIATELY** revert the changes you made by restoring the backup you took earlier:
526     ```bash
527     mtd write uboot_backup.bin RedBoot
528     ```
529     
530     If flashing completes with no errors, you may issue a **reboot** to reboot your router. It should start as normal.
531   
532 **Warning**: if the U-Boot partition is broken and the device is restarted, it becomes a superbrick (especially true for devices like tlwr740 that don't have JTAG).
533
534 How to use it?
535 --------------
536
537 [TODO]
538
539 How to compile the code?
540 ------------------------
541
542 You can use one of the free toolchains:
543
544 - [Sourcery CodeBench Lite Edition for MIPS GNU/Linux](https://sourcery.mentor.com/GNUToolchain/subscription3130?lite=MIPS),
545 - [OpenWrt Toolchain for AR71xx MIPS](http://downloads.openwrt.org/attitude_adjustment/12.09/ar71xx/generic/OpenWrt-Toolchain-ar71xx-for-mips_r2-gcc-4.6-linaro_uClibc-0.9.33.2.tar.bz2),
546 - [ELDK (Embedded Linux Development Kit)](ftp://ftp.denx.de/pub/eldk/),
547 - or any others...
548
549 I am using **Sourcery CodeBench Lite Edition for MIPS GNU/Linux** on **Ubuntu 12.04 LTS** (32-bit, virtual machine) and all released binary images were/will be built using this set.
550
551 All you need to do, after choosing a toolchain, is to modify [Makefile](Makefile) - change or remove `export MAKECMD` and if needed add `export PATH`. For example, to use OpenWrt Toolchain instead of Sourcery CodeBench Lite, download it and extract into `toolchain` folder, inside the top dir and change first lines in [Makefile](Makefile):
552
553 ```
554 export BUILD_TOPDIR=$(PWD)
555 export STAGING_DIR=$(BUILD_TOPDIR)/tmp
556
557 export MAKECMD=make --silent ARCH=mips CROSS_COMPILE=mips-openwrt-linux-uclibc-
558 export PATH:=$(BUILD_TOPDIR)/toolchain/bin/:$(PATH)
559 ```
560
561 To build image, run `make model` inside top dir, for example, command:
562
563 ```
564 make tplink_wr703n
565 ```
566
567 will start building U-Boot image for **TP-Link TL-WR703N**.
568
569 FAQ
570 ---
571
572 #### 1. My device is not supported, but has the same hardware as one in the list, can I use this modification?
573
574 *It could be dangerous! I know that a lot of routers uses the same hardware - for example, TP-Link has a battery powered routers set, which contains: TL-MR10U, TL-MR11U (TL-MR3040 in Europe) TL-MR12U and TL-MR13U. All of them has the same platform: Atheros AR9331 with 32 MiB of DDR RAM and 4 MiB of SPI NOR FLASH. But, there may exist a slight difference, like GPIO pin number for reset button or LED(s), that may cause problems.*
575
576 *You can try, but remember that you are doing this only at your own risk!*
577
578 #### 2. I want to overclock my router, how can I do this?
579
580 *Currently, this option is available only for routers with Atheros AR9331 (please, look at [ap121.h](u-boot/include/configs/ap121.h) file which contains all information about PLL register configuration and an untypical clocks for CPU, RAM and AHB). What more, you will need to compile the code yourself, because I will not publish images with non-default clocks.*
581
582 *And again, remember that you are doing this only at your own risk!*
583
584 #### 3. Do you test all prebuilt images before you publish them?
585
586 *No, because I do not have all supported devices, only few of them. But, I make tests for every supported SoC types.*
587
588 #### 4. I would like you to add support for device X.
589
590 *You can do it yourself and send me a pull request or a patch. If you do not want to, or do not know how to do it, please contact with me directly.*
591
592 #### 5. My device does not boot after upgrade!
593
594 *I told you... bootloader, in this case U-Boot, is the most important piece of code inside your device. It is responsible for hardware initialization and booting an OS (kernel in this case), i.e. it's the bridge head for delegating to / flashing kernel and rootfs images. So, if during the upgrade something went wrong, your device will not boot any more. The only way to recover from such a situation in a mild way is via a JTAG adapter connection. In case of a lack of JTAG connection, you would even need to remove the FLASH chip, load proper image using an external programmer and solder it back.*
595
596 License, outdated sources etc.
597 ------------------------------
598
599 **[U-Boot](http://www.denx.de/wiki/U-Boot/WebHome "U-Boot")** project is Free Software, licensed under version 2 of the **GNU General Public License**. All information about license, contributors etc., are included with sources, inside *u-boot* folder.
600
601 You should know, that most routers, especially those based on Atheros SoCs, uses very old versions of U-Boot (1.1.4 is from 2005/2006). So, *these sources are definitely outdated* (do not even try to merge them with official release), but it was easier for me to modify them, than move TP-Link/Atheros changes to the current version. Moreover, lot of unnecessary code fragments and source files were removed for ease of understanding the code.
602
603 Credits
604 -------
605
606 - Thanks to *pupie* from OpenWrt forum for his great help
607 - Thanks for all donators and for users who contributed in code development