From f182a11f3362017a6c669871414a9bb448ee050d Mon Sep 17 00:00:00 2001 From: Marcin Bukat Date: Mon, 2 Sep 2013 12:35:47 +0200 Subject: rk27utils: Add nandextract utility This quick and dirty utility allows to extract nand bootloader from raw 1st nand block dump. I post it mainly to somewhat document how BCH error correction engine of the rk27xx works. Change-Id: I37ca91add7d372e3576d2722afc946d0f08971a9 --- utils/rk27utils/nandextract/nandextract.c | 235 ++++++++++++++++++++++++++++++ 1 file changed, 235 insertions(+) create mode 100644 utils/rk27utils/nandextract/nandextract.c (limited to 'utils/rk27utils/nandextract/nandextract.c') diff --git a/utils/rk27utils/nandextract/nandextract.c b/utils/rk27utils/nandextract/nandextract.c new file mode 100644 index 0000000000..c9b41d26e3 --- /dev/null +++ b/utils/rk27utils/nandextract/nandextract.c @@ -0,0 +1,235 @@ +#include +#include +#include +#include +#include +#include "libbch.h" + +#define SECTOR_DATA_SIZE 512 +#define SECTOR_META_SIZE 3 +#define SECTOR_ECC_SIZE 13 +#define SECTOR_SIZE (SECTOR_DATA_SIZE + SECTOR_META_SIZE + SECTOR_ECC_SIZE) + +/* scramble mode */ +enum { + CONTINOUS_ENC, /* scramble whole block at once */ + PAGE_ENC /* nand bootloader is scrambled in 0x200 chunks */ +}; + +static uint8_t reverse_bits(uint8_t b) +{ + return (((b & 0x80) >> 7)| + ((b & 0x40) >> 5)| + ((b & 0x20) >> 3)| + ((b & 0x10) >> 1)| + ((b & 0x08) << 1)| + ((b & 0x04) << 3)| + ((b & 0x02) << 5)| + ((b & 0x01) << 7)); +} + +static int libbch_decode_sec(struct bch_control *bch, uint8_t *inbuf, uint8_t *outbuf) +{ + unsigned int errloc[8]; + static const uint8_t mask[13] = { + 0x4e, 0x8c, 0x9d, 0x52, + 0x2d, 0x6c, 0x7c, 0xcb, + 0xc3, 0x12, 0x14, 0x19, + 0x37, + }; + + int i, err_num = 0; + + /* ecc masking polynomial */ + for (i=0; i