NAND dump: bit errors and ECC
A full dump was taken from a router's NAND chip: nand.bin. It consists of 64 pages; each page is 512 bytes of data + 16 bytes of OOB (528 bytes in total). Bit errors have accumulated in worn cells, so the zlib stream cannot be decompressed directly.
OOB structure: [0..2] = the ECC of the first 256 bytes of data, [3..5] = that of the second 256 bytes; [6]=0xFF; [7] = page type (0x43 'C' = configuration, 0x42 loader/other, 0xFF empty); [8..9] = generation (u16 LE); [10..11] = page order in the chain (from 0); [12..13] = number of pages in the chain; [14..15] = 0xFFFF. OOB[6..15] is not protected by ECC (it has no errors).
ECC (Linux nand_ecc.c, Hamming, 256 bytes -> 3 bytes): row parities rp0..rp15: rp(2k+x) (k=0..7, x=0/1) is the XOR of the bytes whose byte index i has bit k equal to x (for each byte, the XOR of its 8 bits). Column parities cp0..cp5: cp(2k+x) (k=0..2) is the XOR of all bits (across all bytes) whose bit position b (0..7) has bit k equal to x. Stored bytes: b0 = ~(rp7..rp0), b1 = ~(rp15..rp8) (rp0 / rp8 — the least significant bit), b2 = ((~cp5..cp0) << 2) | 3 (cp0 — the least significant bit). Each half (256 bytes) has at most one bit error: it may be in the data or in the ECC byte itself.
Configuration chain: 'C' pages of the same generation are joined by sequence number: a u32 LE length + a zlib stream (the rest is 0xFF). At start-up the device takes the highest generation among the complete chains (those that have all their pages). Find the value of activation_key= in the configuration. Flag: BAYROQ{NAND_ECC_...}. (Note: the last 16 characters of the flag are lowercase hexadecimal digits.)