text
stringlengths
0
1.99k
A while back, @hyprdude and I were doing some reconnaissance on the router.
hyperdude found that the GPL tarball [1] published by TP-Link was fully
loaded, and included the modified Linux kernel and Das U-Boot sources used
on the device.
This discovery sparked an idea of creating a custom Qemu board for this
particular chipset, which will help us understand the initial MMIO regions
that the bootloader writes and reads to when it's first powered on (e.g.
making an LED blink different colors.) A custom board will also give us
the ability to debug the kernel and kernel modules, because the pins for
E-JTAG were not working.
After looking at the bootloader's source code, it was obvious why the pins
were not working. The following code is executed upon startup, which
disables the E-JTAG ports on the device via multiplexing.
```
[board956x.c]
#define GPIO_FUNC 0x1804006c
/* set non-JTag */
li t0, GPIO_FUNC
lw t1, 0(t0) li t2, (1<<1) /* we useGPIO14/GPIO15, so disable JTAG*/
or t1, t1, t2
sw t1, 0(t0)
```
By looking at the code, we can note that the MMIO address 0x1804006c is the
`GPIO_FUNC` register. This register may be responsible for GPIO input
multiplexing, but without a datasheet it's all just guesses from prior
experiences.
Luckily, there was a datasheet posted on a Github repo [2] for the QCA9563
chip, which specifies that `bit 1` at address `0x1804006c` is for disabling
JTAG. Since we have the source code that actually compiles, we can simply
modify Das U-Boot and enable JTAG. But, the goal is to achieve kernel
debugging without touching the hardware, even though it should be possible
to access E-JTAG before the above ASM statements are executed.
--[ 2 - Looking into Das U-Boot
While looking for more hints about the MMIO regions, I decided to analyze
the modified source code for the bootloader within the GPL tarball.
If the following keywords are defined:
- `CONFIG_AUTOBOOT_KEYED`
- `CONFIG_BOOTDELAY`
- `CONFIG_AUTOBOOT_STOP_STR` or `CONFIG_AUTOBOOT_STOP_STR2`
Then the string defined in `CONFIG_AUTOBOOT_STOP_STR*` needs to be sent
to the console before the countdown defined in CONFIG_BOOTDELAY reaches
zero. (this reminds me of the game NFL Blitz where you can press in a code
before the match begins)
For the WR940Nv6, the string `tpl` is defined and needs to be sent within
1 second after the `CONFIG_AUTOBOOT_PROMPT` is displayed. Doing this
manually has a low success rate, but using python to spam the string `tpl`
over and over again via a serial adapter has a very high success rate!
This drops us into a Das U-boot shell which gives us read and write access
to physical memory via the `md` and `mw` commands. Awesome!
The code for the `md` and `mw` commands can be found in
`/ap151/boot/u-boot/common/cmd_mem.c` within the GPL tarball for the
WR940Nv6.
--[ 3 - Initial Testing
To make sure that the newly discovered Das U-Boot shell can actually read
and write to physical memory I decided to write to address `0x18040008`
which corresponds to the `GPIO_OUT` register. This address is marked as
"read-only" in the datasheet, so I looked at the Das U-Boot code to find
any hints to help me confirm this is true. Within the `led.S` file the
address `0x18040008` is labeled as `GPIO_OUT` which lines up with the
datasheet, but then they write the value `0xc000` to it with a comment
that says that the LED will turn orange.
The value `0xc000` has bits 14 and 15 set, which could mean that GPIO
output ports 14 and 15 are "ON" which turns on the LED, but why is it
orange? Well, the LED is a three pin multi-colored LED with two different
colors, red (but it looks orange irl) and blue. By providing power to one
of the pins, we can enable the red (orange) LED. Since this code is made
to support different versions of the WR940N (which all have different LED
configurations) they set both GPIO 14 and 15 to ON, but only one pin is
needed to make the red LED turn on, so the red pin is connected to either
GPIO pin 14 or 15. Through trial and error it was found that GPIO pin 14
on the WR940Nv6 is the red LED and pin 19 is the blue LED!
There's a statement within `led.S` that says to turn all of the "WAN" LEDs
blue via `~((1<<3) | (1<<14) | (1<<4) | (1<<5) | (1<<6) | (1<<7))`, and
through trial and error it was discovered that GPIO pin 19 turns on the
blue LED and setting pins 14 and 19 will make the LED turn purple! Even
though this test seems a bit silly, it verifies that the Das U-boot shell
can write to MMIO regions and they actually work! The following diagram
shows how the test was conducted:
||====[UART (Das U-Boot Shell)]