Ernie Firmware

= References =


 * 78K0R_User_Manual.pdf
 * RL78_ISA_Manual.pdf
 * IDA Proc module

= Architecture =

Read the reference manual for full information. Below are the most important facts.

Memory Space
The memory is laid out into  sized blocks. The bottom X blocks are flash memory that can be programmed. Some blocks are not programmable on Ernie. Most keys reside on the non-programmable blocks.

Registers and Devices
Registers are 8-bits and adjacent registers can be addressed as 16-bits (example: register AX can be addressed as A (top 8 bits) and X (bottom 8 bits)).

Additional registers are located near the top of the memory map. They are addressed like any other memory. Ernie uses  as extra scratch registers.

Device registers (documented in the reference manual) are located at  (SFR) and   (SFR2).

The UART serial controller used by Ernie is not documented in the reference manual but the register definitions are the same as the serial controllers that are documented. There appears to be a few other registers and interrupt vectors that differ from the documentation.

Memory access is done through segmented addressing on a 20-bit address space. CS contains the top 4 bits for code access. ES contains the top 4 bits for data access.

Calling Convention
Functions with one argument: AX

Functions with one pointer argument: AX = pointer, C = segment

Functions with multiple arguments: first argument same as above, then rest are in stack using PUSH instruction. Stack pointer moves downwards.

Often, at the start of a function, the AX and BC registers are PUSH onto the stack before the frame pointer is allocated. Then the arguments are referenced by addressing the bottom of the frame pointer.

= Firmware Basics =

Startup
Reset vector sets up environment and calls into the Worker Loop

Worker Loop
The Worker Loop is a FSM with 10 states (0-9). In each state, the Main function is called with the current state and the target state depending on flags that are set.

The starting state is either 0 or 7 depending on a global value derived earlier.

Flags are global variables in memory set by various sources: asynchronous interrupts, synchronous work in the current state, command handlers, etc. Not all states will handle all flags. Some flags can be handled in all states and some flags can be handled in only some states. Each flag is cleared after it is read. When multiple flags are set, the Main function will be called multiple times synchronously in a set order (certain flags have higher priority).

In states 3 and 4, after the Main Function is called (potentially multiple times depending on the flags set), the Command Handler is called.

Main Function
The Main Function is the starting point for most of the Syscon's tasks. It looks at the current state (a global variable) and the target state (its only argument) to determine what to do. If the task is successful, the target state will be written to the current state variable and the function call returns to the Worker Loop.

Command Handler
Two bytes are read from SDR00. This is the command header. If the header & 0x80 == 0, then there is no data payload in the command and the command handler is invoked. One more byte is read. If there is no payload, this is the checksum. If there is a payload, the rest of the packet is read and handled in a different command table. Finally, the checksum is checked.

Asynchronous Handler
A timer is set up at the start of the Worker Loop to count in   cycle intervals. Oddly, the timer interrupt is not configured but  is configured. Possibly the reference manual is not correct w.r.t Ernie because the interrupt handler for  does not appear to reference the serial interface at all. We will assume this interrupt is the timer's interrupt.

A table of software handlers are initialized to 0 at the start of the Worker Loop. Functions can register handlers in this table. Functions can also flag a handler to run at the next timer interrupt. During each timer interrupt, (with interrupt disabled) the firmware will iterate through the handlers and run any function that is flagged (and unflag it).

JIG Handler
When JIG is enabled, commands can be sent through an UART interface by an external agent. An UART RX interrupt will flag asynchronous handler. This handler is registered at the start of the Worker Loop to set flag  and return. In the Worker Loop, flag  can be handled by states 1, 3, 8, 9. In any of these states, the JIG Handler is called.

JIG packets follow the same format as the Command Handler packets (2 byte command id, optional length payload, 1 byte checksum) but the packets are ASCII encoded, so every raw byte is represented by two printable ASCII bytes.

= Kermit Bootrom JIG Mode =

Similar to Pandora battery on the PSP, the Vita also has a hidden manufacturing/recovery mode in the boot ROM. By convention, we call this "JIG mode". Once a handshake with Kermit boot ROM passes, the Vita will boot from an SD card in the gamecard slot instead of from eMMC. The payload must be signed by Sony specifically for this mode. There is no vulnerability that allows you to bypass the signature check.

To trigger JIG mode, we have to first enable JIG from the syscon and then do a handshake with Kermit.

JIG Enable

 * 1) At startup, the timer for the Ernie_Firmware is set up. When the interrupt is triggered at least 8 times, every time it is triggered, flag   is set.
 * 2) In states 0, 1, 3, 8, or 9, flag   is handled.
 * 3) The ADC is called on channel ANI6 (port P26). The voltage is passed to a lookup table that converts different voltage ranges to different ids. For example 1.562V - 1.622V corresponds to id 0.
 * 4) Repeat 1-3 and if the id value did not change, then we set a global variable to that value and set flag.
 * 5) In states 0, 1, 3, 8, or 9, flag   is handled.
 * 6) If the id from #3 is 0 (P26 is held at 1.562V - 1.622V for at least 32000 clock cycles), then then UART serial controller is configured. If bit 3 is set in Sysroot then a hello packet is written:.

Voltage Table
The conversion is done using the equation on 11.4.2 of the manual with Vref=1.8V.

Entering Handshake
Now that Ernie_Firmware is set up, you can perform the handshake with Kermit boot ROM.


 * 1) Send a JIG packet with Command ID 0x110. This is a packet whose payload is encrypted. TODO: document the packet format. Maybe similar to 0xD0/0xD2 on the normal interface?
 * 2) In the decrypted packet, the data triggers some command handler which sets flag
 * 3) Only state 1 handles flag.
 * 4) Main Function in state 1 is called with target state 9. It does some unknown tasks (maybe power on device?) and sets flag  . State is set to 9.
 * 5) Main Function in state 9 is called with target state 3 (due to flag  ).
 * 6) Ernie is ready to perform the handshake.

Handshake
The pin references for Kermit is from GPIO Registers. The pin references for Ernie is from the reference manual. TODO: figure out the physical mapping of the pins.


 * 1) Ernie set P15
 * 2) Ernie set P97
 * 3) Kermit poll for GPIO Port 4 high
 * 4) Kermit does some magic register writes (possibly switching SPI pins to JIG handshake interface)
 * 5) Kermit writes 8 bytes challenge to a F00D only register
 * 6) Kermit sets GPIO Port 3 high
 * 7) Ernie poll for P16 high
 * 8) Ernie clears P90
 * 9) Ernie receives a packet   where XX is 4 bytes of the challenge.
 * 10) Ernie sets P90
 * 11) Ernie clears P90
 * 12) Ernie receives a packet   where XX is 4 bytes of the challenge.
 * 13) Ernie sets P90
 * 14) Ernie does some endian swapping with the data.
 * 15) Ernie AES encrypts the challenge with the shared key with Kermit boot ROM.
 * 16) Ernie does some endian swapping with the data.
 * 17) Kermit polls for GPIO Port 4 high
 * 18) Ernie clears P90
 * 19) Ernie sends a packet   where XX is 4 bytes of the response.
 * 20) Ernie sets P90
 * 21) Ernie clears P90
 * 22) Ernie sends a packet   where XX is 4 bytes of the response.
 * 23) Ernie sets P90
 * 24) Kermit magically gets 8 bytes in a F00D only register
 * 25) Kermit sets GPIO Port 3 low
 * 26) Kermit AES encrypts the challenge with its own shared key and does a timing-safe memcmp with the response.
 * 27) Kermit makes sure to wipe the key and all relevant registers.

Hardware
In theory there needs to be 2 digital pins (UART) and 1 analog pin (JIG enable sense) accessible to the factory agent in order for JIG to work. On the Slim units, it is currently unknown where these pins might be. One theory is that there may be a USB serial hardware inside the TI charging chip. There is only weak evidence for this: the TI chip communicates with Syscon through an I2C interface and we see from pictures of the PCB that the USB data lanes go into and out of the TI chip on the Slim. This is different from the SN99057 on the phat units which routes the USB data lanes in parallel (for charge sensing).

On the phat units, the multiconnector port has UART exposed on pin 6 and 7. Additionally there are pins 11, 12, and 13 that are routed directly to Ernie (seen through PCB delayering).



We observe that without anything attached, the resistance between pin 11 and 12 is exactly 200Kohm. When the DevKit USB ethernet adapter is plugged in, the resistance between pin 11 and 12 is non-linear. One theory is that there is some sort of accessory sense based on these pins and the Ernie_Firmware.