/** * nand_bch_calculate_ecc - [NAND Interface] Calculate ECC for data block * @mtd: MTD block structure * @buf: input buffer with raw data * @code: output buffer with ECC */ int nand_bch_calculate_ecc(struct omap_nand_info *info, const unsigned char *buf, unsigned char *code) { const struct nand_chip *chip = &info->nand; int i, blocks = 0; unsigned char *ecc_ptr = code; unsigned char *data = buf; int ecc_bytes = 0; if (chip->ecc.size == 2048) blocks = 4; else blocks = 1; ecc_bytes = chip->ecc.bytes / blocks; for (i = 0; i < blocks; i++) { data = buf + i * 512; ecc_ptr = code + i * ecc_bytes; memset(ecc_ptr, 0, ecc_bytes); encode_bch(info->bch, data, 512, ecc_ptr); } return 0; }
/** ** compute and store the inverted ecc of an erased ecc block **/ void prepare_ecc_mask(struct omap_nand_info *info, const unsigned int eccsize, const unsigned int eccbytes) { int i; unsigned char erased_page[eccsize]; memset(erased_page, 0xff, eccsize); info->eccmask = malloc(eccbytes); memset(info->eccmask, 0, eccbytes); encode_bch(info->bch, erased_page, eccsize, info->eccmask); for (i = 0; i < eccbytes; i++) { info->eccmask[i] ^= 0xff; } }
void BCH::encode(int encoded_bits[BCH_DEFAULT_LENGTH], const _64bits orig_n) { assert(k == BCH_DEFAULT_K && length == BCH_DEFAULT_LENGTH); int orig_bits[BCH_DEFAULT_K]; toBitPattern(orig_bits,orig_n,k); encode_bch(encoded_bits,orig_bits); for (int i = 0; i < k; i++) encoded_bits[i + length - k] = orig_bits[i]; //printf("[x] encoded_bits: "); //printBitArray(encoded_bits, BCH_DEFAULT_LENGTH); //printf("\n"); }
main() { int i; read_p(); /* Read m */ generate_gf(); /* Construct the Galois Field GF(2**m) */ gen_poly(); /* Compute the generator polynomial of BCH code */ /* Randomly generate DATA */ seed = 131073; srandom(seed); for (i = 0; i < k; i++) data[i] = ( random() & 65536 ) >> 16; encode_bch(); /* encode data */ /* * recd[] are the coefficients of c(x) = x**(length-k)*data(x) + b(x) */ for (i = 0; i < length - k; i++) recd[i] = bb[i]; for (i = 0; i < k; i++) recd[i + length - k] = data[i]; printf("Code polynomial:\nc(x) = "); for (i = 0; i < length; i++) { printf("%1d", recd[i]); if (i && ((i % 50) == 0)) printf("\n"); } printf("\n"); printf("Enter the number of errors:\n"); scanf("%d", &numerr); /* CHANNEL errors */ printf("Enter error locations (integers between"); printf(" 0 and %d): ", length-1); /* * recd[] are the coefficients of r(x) = c(x) + e(x) */ for (i = 0; i < numerr; i++) scanf("%d", &errpos[i]); if (numerr) for (i = 0; i < numerr; i++) recd[errpos[i]] ^= 1; printf("r(x) = "); for (i = 0; i < length; i++) { printf("%1d", recd[i]); if (i && ((i % 50) == 0)) printf("\n"); } printf("\n"); decode_bch(); /* DECODE received codeword recv[] */ /* * print out original and decoded data */ printf("Results:\n"); printf("original data = "); for (i = 0; i < k; i++) { printf("%1d", data[i]); if (i && ((i % 50) == 0)) printf("\n"); } printf("\nrecovered data = "); for (i = length - k; i < length; i++) { printf("%1d", recd[i]); if ((i-length+k) && (((i-length+k) % 50) == 0)) printf("\n"); } printf("\n"); /* * DECODING ERRORS? we compare only the data portion */ for (i = length - k; i < length; i++) if (data[i - length + k] != recd[i]) decerror++; if (decerror) printf("There were %d decoding errors in message positions\n", decerror); else printf("Succesful decoding\n"); }