diff options
Diffstat (limited to 'utils/rk27utils/nandextract/libbch.h')
-rw-r--r-- | utils/rk27utils/nandextract/libbch.h | 113 |
1 files changed, 113 insertions, 0 deletions
diff --git a/utils/rk27utils/nandextract/libbch.h b/utils/rk27utils/nandextract/libbch.h new file mode 100644 index 0000000000..b4c779e815 --- /dev/null +++ b/utils/rk27utils/nandextract/libbch.h | |||
@@ -0,0 +1,113 @@ | |||
1 | /* | ||
2 | * Generic binary BCH encoding/decoding library | ||
3 | * | ||
4 | * This program is free software; you can redistribute it and/or modify it | ||
5 | * under the terms of the GNU General Public License version 2 as published by | ||
6 | * the Free Software Foundation. | ||
7 | * | ||
8 | * This program is distributed in the hope that it will be useful, but WITHOUT | ||
9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | ||
11 | * more details. | ||
12 | * | ||
13 | * You should have received a copy of the GNU General Public License along with | ||
14 | * this program; if not, write to the Free Software Foundation, Inc., 51 | ||
15 | * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | ||
16 | * | ||
17 | * Copyright © 2011 Parrot S.A. | ||
18 | * | ||
19 | * Author: Ivan Djelic <ivan.djelic@parrot.com> | ||
20 | * | ||
21 | * Description: | ||
22 | * | ||
23 | * This library provides runtime configurable encoding/decoding of binary | ||
24 | * Bose-Chaudhuri-Hocquenghem (BCH) codes. | ||
25 | */ | ||
26 | #ifndef _BCH_H | ||
27 | #define _BCH_H | ||
28 | |||
29 | #include <stdint.h> | ||
30 | |||
31 | #if defined(CONFIG_BCH_CONST_PARAMS) | ||
32 | #define GF_M(_p) (CONFIG_BCH_CONST_M) | ||
33 | #define GF_T(_p) (CONFIG_BCH_CONST_T) | ||
34 | #define GF_N(_p) ((1 << (CONFIG_BCH_CONST_M))-1) | ||
35 | #else | ||
36 | #define GF_M(_p) ((_p)->m) | ||
37 | #define GF_T(_p) ((_p)->t) | ||
38 | #define GF_N(_p) ((_p)->n) | ||
39 | #endif | ||
40 | |||
41 | #define BCH_ECC_WORDS(_p) DIV_ROUND_UP(GF_M(_p)*GF_T(_p), 32) | ||
42 | #define BCH_ECC_BYTES(_p) DIV_ROUND_UP(GF_M(_p)*GF_T(_p), 8) | ||
43 | |||
44 | #ifndef dbg | ||
45 | #define dbg(_fmt, args...) do {} while (0) | ||
46 | #endif | ||
47 | |||
48 | /* | ||
49 | * represent a polynomial over GF(2^m) | ||
50 | */ | ||
51 | struct gf_poly { | ||
52 | unsigned int deg; /* polynomial degree */ | ||
53 | unsigned int c[0]; /* polynomial terms */ | ||
54 | }; | ||
55 | |||
56 | /* given its degree, compute a polynomial size in bytes */ | ||
57 | #define GF_POLY_SZ(_d) (sizeof(struct gf_poly)+((_d)+1)*sizeof(unsigned int)) | ||
58 | |||
59 | /* polynomial of degree 1 */ | ||
60 | struct gf_poly_deg1 { | ||
61 | struct gf_poly poly; | ||
62 | unsigned int c[2]; | ||
63 | }; | ||
64 | |||
65 | /** | ||
66 | * struct bch_control - BCH control structure | ||
67 | * @m: Galois field order | ||
68 | * @n: maximum codeword size in bits (= 2^m-1) | ||
69 | * @t: error correction capability in bits | ||
70 | * @ecc_bits: ecc exact size in bits, i.e. generator polynomial degree (<=m*t) | ||
71 | * @ecc_bytes: ecc max size (m*t bits) in bytes | ||
72 | * @a_pow_tab: Galois field GF(2^m) exponentiation lookup table | ||
73 | * @a_log_tab: Galois field GF(2^m) log lookup table | ||
74 | * @mod8_tab: remainder generator polynomial lookup tables | ||
75 | * @ecc_buf: ecc parity words buffer | ||
76 | * @ecc_buf2: ecc parity words buffer | ||
77 | * @xi_tab: GF(2^m) base for solving degree 2 polynomial roots | ||
78 | * @syn: syndrome buffer | ||
79 | * @cache: log-based polynomial representation buffer | ||
80 | * @elp: error locator polynomial | ||
81 | * @poly_2t: temporary polynomials of degree 2t | ||
82 | */ | ||
83 | struct bch_control { | ||
84 | unsigned int m; | ||
85 | unsigned int n; | ||
86 | unsigned int t; | ||
87 | unsigned int ecc_bits; | ||
88 | unsigned int ecc_bytes; | ||
89 | /* private: */ | ||
90 | uint16_t *a_pow_tab; | ||
91 | uint16_t *a_log_tab; | ||
92 | uint32_t *mod8_tab; | ||
93 | uint32_t *ecc_buf; | ||
94 | uint32_t *ecc_buf2; | ||
95 | unsigned int *xi_tab; | ||
96 | unsigned int *syn; | ||
97 | int *cache; | ||
98 | struct gf_poly *elp; | ||
99 | struct gf_poly *poly_2t[4]; | ||
100 | }; | ||
101 | |||
102 | struct bch_control *init_bch(int m, int t, unsigned int prim_poly); | ||
103 | |||
104 | void free_bch(struct bch_control *bch); | ||
105 | |||
106 | void encode_bch(struct bch_control *bch, const uint8_t *data, | ||
107 | unsigned int len, uint8_t *ecc); | ||
108 | |||
109 | int decode_bch(struct bch_control *bch, const uint8_t *data, unsigned int len, | ||
110 | const uint8_t *recv_ecc, const uint8_t *calc_ecc, | ||
111 | const unsigned int *syn, unsigned int *errloc); | ||
112 | |||
113 | #endif /* _BCH_H */ | ||