boot1
boot1 is the second stage of the Starlet boot chain. boot0 loads it into SRAM, decrypts and verifies it and jumps to 0xFFF00000. boot1 then finds boot2 in NAND, checks its signature chain, decrypts and hashes it into MEM1 and jumps to it.
boot1 is a 94 KiB (0x17800 byte) image. It starts executing through the 0xFFF00000 alias of SRAM but its pointers to its own data and to some of its own functions use the 0x0D400000 alias. Both aliases refer to the same memory. The memory map shows the mappings.
Entry
Execution starts at start (0xFFF00000) in ARM state. The entry code prepares the caches and the stack, calls the loader and then jumps to the address the loader returns. Its call to memset32 (0xFFF0005C) writes nothing because both ends of the range are 0x0D404320. Bit 12 (I) of the CP15 control register enables the instruction cache, as described in boot2 kernel entry. boot1 keeps the cache enabled only while the loader runs:
void start(void)
{
invalidate_icache(); // MCR p15, 0, r0, c7, c5, 0
invalidate_dcache(); // MCR p15, 0, r0, c7, c6, 0
cp15_control |= 1 << 12; // enable the instruction cache
memset32(bss_start, bss_end, 0, 4); // the range is empty in boot1
sp = 0x0D40EFFC;
void* entry = load_boot2(); // Thumb code
cp15_control &= ~(1 << 12); // disable the instruction cache
goto *entry; // MOV PC, R1, stays in ARM state
}
Loader
load_boot2 (0xFFF00494 in boot1b and 0xFFF004F4 in boot1d) does all the work. It writes progress code 0x11 to bits 23:16 of 0x0D8000E0, the same GPIO field that boot0 uses for its error codes. It then writes 0x47 to HW_SRNPROT (0x0D800060) if the upper four bits of the byte at 0x0D800217 are zero. Otherwise it writes 0x07.
After that, boot1 makes up to two attempts to load boot2. The first attempt searches NAND blocks 1 to 7 in ascending order. If it fails, boot1 waits for the NAND, AES and SHA-1 engines to finish and tries again, this time searching from block 7 down to block 1. If the second attempt also fails, boot1 shows the error code forever.
void* load_boot2(void)
{
int error;
debug_out(0x11);
HW_SRNPROT = (read8(0x0D800217) >> 4) == 0 ? 0x47 : 0x07;
for (int attempt = 1; attempt <= 2; attempt++) {
int first = attempt == 1 ? 1 : 7;
int step = attempt == 1 ? +1 : -1;
if (attempt == 2)
wait_until_idle(NAND, AES, SHA1);
read_block_map(&map);
error = load(first, step, &map, &content);
if (error == 0) {
write32(content + 0x0C, 1);
debug_out(0x22);
return content + read32(content);
}
}
for (;;) {
debug_out(error);
delay(1000000);
debug_out(~error);
delay(1000000);
}
}
debug_out(code) (0xFFF0007C) writes the low byte of code to bits 23:16 of 0x0D8000E0. The final error loop alternates the error code with its complement and calls delay (0xFFF03CD4 in boot1b and 0xFFF031AC in boot1d) after each write.
Finding boot2
Block map
Before each attempt, boot1 looks for a block map. It reads the last page (page 63) of every block from 1 to 7. Each of these pages can hold three copies of a 76 byte map, at offsets 0x00, 0x4C and 0x98:
| Offset | Size | Contents |
|---|---|---|
0x00 |
8 | Magic, an 8 byte constant stored in boot1 |
0x08 |
4 | Generation number, big-endian |
0x0C |
64 | One byte per block number, 1 means skip block |
A page only provides a map if at least two of its three copies are identical over all 76 bytes. boot1 uses copy 0 when its magic matches and it is identical to copy 1 or copy 2. Otherwise it uses copy 1 when its magic matches and it is identical to copy 2. If neither applies, the page provides no map. Out of all accepted copies in blocks 1 to 7, the one with the highest generation wins.
Choosing a block
If the winning generation is greater than 1, boot1 trusts the map and skips every block whose byte is 1. If there is no usable map, it checks the blocks directly instead: it reads pages 0 and 1 of the block and skips the block if the first spare byte of either page is not 0xFF.
The first block that is not skipped, in the direction of the current attempt, holds the start of boot2. If no such block exists between 1 and 7, the attempt fails with error -4.
Reading pages
boot1 reads a page by programming the NAND engine with these writes:
0x0D010008=0,0x0D01000C= page number,0x0D010000=0x9F000000.- Wait until bit 31 of
0x0D010000reads as 0. 0x0D010010= data buffer address,0x0D010014= spare buffer address,0x0D010000=0x8030B840.
The function returns right away. boot1 waits for bit 31 of 0x0D010000 to clear before it uses the page and corrects every header and content page with ecc_correct (0xFFF00254 in boot1b and 0xFFF00288 in boot1d).
boot2 layout
Page 0 of the boot2 block starts with a 32 byte header. All values are big-endian:
| Offset | Contents |
|---|---|
0x00 |
Header size, must be 0x20 |
0x04 |
Offset of the encrypted content from the start of the block |
0x08 |
Size of the certificate chain |
0x0C |
Size of the ticket |
0x10 |
Size of the TMD |
The content offset must not be larger than 0x20000 and must be a multiple of 64. Otherwise boot1 fails with error -5. boot1d also requires a 0xA00 byte certificate chain, a 0x2A4 byte ticket and a 0x208 byte TMD. boot1b accepts the three sizes from the header without these equality checks.
boot1 reads pages 0 up to content_offset / 0x800 of the block into MEM1 at 0x00028000. The certificate chain follows the header, the ticket follows the certificate chain and the TMD follows the ticket:
0x00028000 header (0x20 bytes)
+ 0x20 certificate chain
+ 0x20 + certs ticket
+ 0x20 + certs + ticket TMD
Signature checks
Certificate chain
boot1 checks five signed blobs. The checks run in this order. Offsets are relative to the start of the certificate chain:
| Blob | Size | Expected issuer | Key |
|---|---|---|---|
Certificate at 0x000 |
0x400 |
Root |
Root key stored in boot1 (RSA-4096) |
Certificate at 0x400 |
0x300 |
Root- + name of the first certificate |
Modulus at 0x2C8, exponent at 0x3C8 |
| TMD | TMD size | Root- + first name + - + second name |
Modulus at 0x5C8, exponent at 0x6C8 |
Certificate at 0x700 |
0x300 |
Root- + name of the first certificate |
Modulus at 0x2C8, exponent at 0x3C8 |
| Ticket | 0x2A4 |
Root- + first name + - + third name |
Modulus at 0x8C8, exponent at 0x9C8 |
The certificate names are stored at 0x284, 0x584 and 0x884. Before a name is used, boot1 checks its first two characters. They must be CA, CP and XS respectively or the attempt fails with error -9.
Vulnerable verifier
boot1b checks each blob with verify_signed (0xFFF003A4). The function copies the blob to MEM1 at 0x000AC000 with mem_copy (0xFFF03ABC) and compares its 64 byte issuer field with the expected issuer string using memcmp (0xFFF039D4). It then hashes everything from the issuer field to the end of the blob with SHA-1 and checks an RSA signature over that hash:
int verify_signed(const u8* blob, const char* issuer, const u8* modulus,
const u8* exponent, int kind, u32 size)
{
u8* copy = (u8*)0x000AC000;
u32 body = kind == 1 ? 0x240 : 0x140; // kind 1: RSA-4096, kinds 2-4: RSA-2048
u32 key_len = kind == 1 ? 0x200 : 0x100;
mem_copy(copy, blob, size);
if (memcmp(copy + body, issuer, 64) != 0)
return -8;
u8 hash[20] = SHA1(copy + body, size - body);
u8 m[key_len] = RSA_Public(copy + 4, modulus, exponent); // m = sig^e mod n, big-endian
if (strncmp(m + key_len - 20, hash, 20) != 0)
return -8;
return 0;
}
The signature starts at offset 4 of the blob. The 32 bit value at offset 0 is not looked at. The public exponent is stored as 4 big-endian bytes. boot1 drops any trailing zero bytes before using it.
The boot1b check has two flaws:
- Only the last 20 bytes of
mare compared with the hash. The padding in front of them is ignored. - The comparison uses
strncmp(0xFFF03C74), which stops at the first position where both strings contain a zero byte. If the first byte of the hash is0x00and the matching byte ofmis also0x00, the check passes no matter what the remaining 19 bytes are.
Fixed verifier
boot1d checks each blob with verify_signed (0xFFF003D8). The blob must start from 0x00028000 through 0x00048000 and its size must not exceed 0x3FFF. The function copies it to 0x000AC000 with mem_copy (0xFFF028CC) then checks the issuer with memcmp (0xFFF02ED0) and hashes the signed body. A failed issuer or RSA check returns error -8.
rsa_verify (0xFFF034D8) checks the complete EMSA-PKCS1-v1_5 encoded message. It requires the initial 0x00 0x01, every 0xFF padding byte, the 0x00 separator, the SHA-1 DigestInfo encoding and all 20 digest bytes. The accepted message is:
00 01 FF ... FF 00 30 21 30 09 06 05 2B 0E 03 02 1A 05 00 04 14 <20 byte SHA-1>
The first root certificate uses a 512 byte encoded message. The remaining four blobs use 256 byte encoded messages. A zero signature decodes to zero and fails the initial bytes, padding and digest checks.
Decryption and hashing
Once all signatures pass, boot1 decrypts the content key, checks the TMD and loads the content:
- It decrypts the 16 bytes at offset
0x1BFof the ticket with AES-128-CBC. The key is a constant stored in boot1. The IV is the 8 bytes at offset0x18Cof the TMD followed by 8 zero bytes. This decryption happens in place at MEM10x000B0000and boot1 clears that buffer afterwards. - It checks that the two big-endian words at offsets
0x18Cand0x190of the TMD are both1. Otherwise the attempt fails with error-11. - It checks that the byte at offset
0x18Bof the TMD is at least version byte 0 of the SEEPROM version record. If it is smaller or if no valid record can be read, the attempt fails with error-10. - It takes the content size from the word at offset
0x1F0of the TMD and the expected SHA-1 hash from offset0x1F4and builds the content IV from the 2 bytes at offset0x1E8followed by 14 zero bytes.
The content then streams from NAND into MEM1. The page that contains the content offset is loaded to 0x00028000, so the content itself starts at 0x00028000 + (content_offset & 0x7FF). Later pages follow in order. When boot1 reaches the end of a block, it continues with the next block in the direction of the current attempt, skipping blocks the same way as before. The content size is rounded up to a multiple of 16 bytes and the spare buffers are placed right behind the content. If round_up(0x00028800 + (content_offset & 0x7FF) + padded_size, 128) + 256 is larger than 0x7FFFF or if boot1 runs out of blocks, the attempt fails with error -6.
The pages move through a three-stage pipeline. While the NAND engine reads one page, the previous page is corrected and decrypted in place and the page before that is hashed. All of the content is decrypted as one AES-128-CBC stream with the decrypted content key. The first AES command written to 0x0D020000 is 0x98000000 | (length / 16 - 1) and every following one is 0x98001000 | (length / 16 - 1). The SHA-1 hash covers exactly the content size rather than the padding to 16 bytes and uses standard SHA-1 padding. If the hash does not match the TMD, the attempt fails with error -7.
SHA-1
The SHA-1 engine only processes whole 64 byte blocks, so boot1 adds the padding itself. To finish a hash, it builds the last one or two blocks in MEM1 at 0x000A8000: the leftover bytes, a 0x80 byte, zeros and the message length in bits. Only the low 32 bits of the length are written. The upper 32 bits are always zero.
ECC correction
ecc_correct checks a 2048 byte page in four 512 byte chunks. For each chunk it compares the ECC word stored in the spare area at 0x30 + 4 * i with the word the NAND engine calculated at 0x40 + 4 * i:
int ecc_correct(u8* data, u8* spare)
{
for (int i = 0; i < 4; i++) {
u32 stored = read32(spare + 0x30 + 4 * i);
u32 computed = read32(spare + 0x40 + 4 * i);
if (stored == computed)
continue;
u32 diff = bswap32(stored) ^ bswap32(computed);
if ((diff & (diff - 1)) == 0)
continue; // only one bit differs: the error is in the ECC itself
if (((diff & 0xFFF) ^ (diff >> 16)) != 0xFFF)
return -1; // the data cannot be corrected
u32 byte = (diff >> 19) & 0x1FF;
u32 bit = (diff >> 8) & 7;
data[i * 512 + byte] ^= 1 << bit;
}
return 0;
}
Engine handshake
Before every AES or SHA-1 command and after every NAND read or AES command, boot1 runs the ahb_mem_flush (0xFFF01744) handshake. It writes a mask to the 16 bit register at 0x0D8B4228 and then reads the 16 bit register at 0x0D8B422A until it returns the same mask, calling udelay(1) (0xFFF024DC) between reads. When the register matches or after 1,000,000 failed reads, boot1 writes 0 back to 0x0D8B4228.
| When | Mask |
|---|---|
| Before a SHA-1 or AES command | 1 |
| After a NAND read | 8 |
| After an AES command | 2 |
If the register never matches, boot1 prints ERROR: ahbMemFlush(0x%x) did not ack! and carries on. The message goes out through ARM semihosting, never had to deal with this so take it with a grain of salt pls: a Thumb SVC 0xAB with R0 = 4 (SYS_WRITE0) and R1 pointing at the text. boot1 sends the text in pieces of up to 15 characters.
Synchronization after a register read
Each acknowledgement poll goes through memreg_read_sync(0x22A) (0xFFF01724). This helper reads the 16 bit register at 0x0D8B422A, calls ahb_sync_dev(0) (0xFFF014EC) and then returns the value it read before that call.
ahb_sync_dev(0) works on bit 15 of the 32 bit register at 0x0D8B0008.
| Register | Bit | Use by boot1 |
|---|---|---|
0x0D8B0008 |
15 | Skips the device 0 sequence when set. Otherwise pulsed as shown below. |
If bit 15 already reads as one, the call returns without writing the register. Otherwise boot1 makes four 32 bit writes based on the value it read: one with bit 15 clear, followed by three with bit 15 set. The other bits keep their values from that original read.
Status wait before device synchronization
Before it submits SHA-1 input, boot1 calls ahb_sync(5) (0xFFF01800), which runs ahb_sync_dev(5) and then ahb_sync_dev(0). Devices 2 through 10 share one branch of ahb_sync_dev. Each selects mask 1 << (device - 2) in the 32 bit register at 0x0D8B0008, so device 5 selects bit 3 (0x00000008). If the selected bit already reads as one, the call returns immediately.
Otherwise boot1 polls the low four bits of HW_BOOT0 (0x0D80018C) while it changes HW_SPARE0 (0x0D800188) and several other control registers. It reads 0x0D8B0008 through mc_read (0xFFF014A0):
| Register | Bits | Use by boot1 |
|---|---|---|
HW_SPARE0 (0x0D800188) |
25 | Set before the second wait. Cleared during cleanup. |
HW_SPARE0 (0x0D800188) |
16 | Set on every pass of the first wait while HW_BOOT0[3:0] is 9. Cleared afterwards. |
HW_BOOT0 |
3:0 |
Polled until it leaves 9, then polled again after setup until it reaches 9. |
0x0D800124 |
10:6 |
Set to 10 during setup and to 3 during cleanup. Bit 10 is clear in both values. |
0x0D800134 |
10 | Set before the second wait. Cleared during cleanup. |
0x0D800100 through 0x0D800120, every 4 bytes |
10 | Set in each register after the second wait. Cleared during cleanup. |
0x0D8B0008 |
8:0 |
Devices 2 through 10 select bits 0 through 8. A set bit skips the sequence. |
0x0D800214 |
7:4 |
Tested by mc_read to decide whether it sets and later restores 0x0D800130. |
0x0D800130 |
10 | Set by mc_read before four discarded reads of 0x0D800138 when that path is taken. |
All accesses in the sequence are 32 bits at Starlet physical addresses. set32 and clear32 each read the register fresh and write it back with only the given bits changed. replace32 reads the register, clears the mask and inserts the given value:
void ahb_sync_dev(u32 device) // devices 2 through 10
{
u32 mask = 1u << (device - 2);
if ((mc_read(8) & mask) != 0)
return;
while ((read32(HW_BOOT0) & 0xF) == 9)
set32(HW_SPARE0, 0x00010000);
clear32(HW_SPARE0, 0x00010000);
set32(HW_SPARE0, 0x02000000);
replace32(0x0D800124, 0x000007C0, 0x00000280);
set32(0x0D800134, 0x00000400);
while ((read32(HW_BOOT0) & 0xF) != 9) {}
for (u32 address = 0x0D800100; address <= 0x0D800120; address += 4)
set32(address, 0x00000400);
write32(0x0D8B0008, mc_read(8) & ~mask);
write32(0x0D8B0008, mc_read(8) | mask);
clear32(0x0D800134, 0x00000400);
for (u32 address = 0x0D800100; address <= 0x0D800120; address += 4)
clear32(address, 0x00000400);
clear32(HW_SPARE0, 0x02000000);
replace32(0x0D800124, 0x000007C0, 0x000000C0);
}
The first loop waits for HW_BOOT0[3:0] to leave 9 and the second waits for it to reach 9. Both compare against 9 for every device in this branch. Neither loop has a timeout or calls udelay. boot1 never writes HW_BOOT0 during the sequence. The two writes to 0x0D8B0008 happen only after the second wait. The SHA-1 command is issued after ahb_sync returns.
Both writes to 0x0D800124 clear bit 10. The setup write puts 10 into bits 9:6 and the cleanup write puts 3 there. Cleanup clears the bits that setup set instead of restoring their entry values. It returns without polling HW_BOOT0 again.
mc_read(offset) reads the word at 0x0D8B0000 | offset. If bits 7:4 of 0x0D800214 read as zero on entry, it first saves 0x0D800130, sets bit 10 there and reads 0x0D800138 four times, discarding the values. After reading the requested word, it reads 0x0D800214 twice. If bits 7:4 of the second read are zero, it writes the saved value back to 0x0D800130. When the entry test found a nonzero field, nothing was saved and this restore writes zero.
udelay
udelay(n) busy-waits on the counter at 0x0D800010. It converts n to a number of counter ticks by adding shifted copies of n:
u32 ticks = scale
? n + (n >> 1) + (n >> 2) + (n >> 3) + (n >> 6) + (n >> 8) + (n >> 9) + (n >> 10) + (n >> 12)
: n + (n >> 2) + (n >> 6);
if (ticks < 2)
ticks = 2;
scale (0x0D404314) is a word in boot1's data that holds 1. The wait handles the counter wrapping around.
SEEPROM
boot1 accesses SEEPROM through GPIO. It calls delay(125) before and after raising the clock for each bit.
SEEPROM version record
The SEEPROM holds two copies of a 10 byte version record, at byte offsets 0x48 and 0x52. All values are big-endian:
| Offset | Size | Contents |
|---|---|---|
0x00 |
1 | Version byte 0, the lowest allowed value for TMD byte 0x18B |
0x04 |
4 | Counter |
0x08 |
2 | Checksum: the sum of the four 16 bit words at 0x00-0x07, truncated to 16 bits |
A copy is valid when its checksum matches. Among the valid copies, boot1 uses the one with the highest counter. If neither copy is valid, the version check fails.
Errors
When both attempts fail, the GPIO debug field alternates between the low byte of the error code and its complement.
| Code | Shown | Meaning |
|---|---|---|
-1 |
0xFF / 0x00 |
A page has an error that ECC cannot correct |
-4 |
0xFC / 0x03 |
No usable block between 1 and 7 |
-5 |
0xFB / 0x04 |
The header is invalid |
-6 |
0xFA / 0x05 |
The content is too large or boot1 ran out of blocks |
-7 |
0xF9 / 0x06 |
The content hash does not match the TMD |
-8 |
0xF8 / 0x07 |
An issuer does not match or a signature check failed |
-9 |
0xF7 / 0x08 |
A certificate name does not start with CA, CP or XS |
-10 |
0xF6 / 0x09 |
The TMD version is too old or the SEEPROM record is invalid |
-11 |
0xF5 / 0x0A |
The TMD title ID is not 00000001-00000001 |