efuse_bch_8.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #include <linux/string.h>
  2. #include <linux/efuse.h>
  3. static inline int cm(int p, int x)
  4. {
  5. int i, tmp;
  6. tmp = x;
  7. for (i=0; i<p; i++) {
  8. tmp <<= 1;
  9. if (tmp&(1<<8)) tmp ^= 0x11d;
  10. }
  11. return tmp;
  12. }
  13. static inline int gm(int x, int y)
  14. {
  15. int i, tmp;
  16. tmp = 0;
  17. for (i=0; i<8; i++) {
  18. if (y&(1<<i)) tmp ^= cm(i, x);
  19. }
  20. return tmp;
  21. }
  22. static void bch_enc(int c[255], int n, int t)
  23. {
  24. int i, j, r, gate;
  25. int b[20], g[20];
  26. char gs1[] = "101110001";
  27. char gs2[] = "11000110111101101";
  28. memset(g, 0, 20*sizeof(int));
  29. memset(b, 0, 20*sizeof(int));
  30. r = t*8;
  31. for (i=0; i<r+1; i++) {
  32. g[i] = (t==1 ? gs1[i] : gs2[i]) - '0';
  33. }
  34. for (i=0; i<n; i++) {
  35. c[i] = i<n-r ? c[i] : b[r-1];
  36. gate = i<n-r ? c[i]^b[r-1] : 0;
  37. for (j=r-1; j>=0; j--)
  38. b[j] = gate*g[j] ^ (j==0?0:b[j-1]);
  39. }
  40. }
  41. static int bch_dec(int c[255], int n, int t)
  42. {
  43. int i, j, tmp;
  44. int s[4], a[6], b[6], temp[6], deg_a, deg_b;
  45. int sig[5], deg_sig;
  46. int errloc[4], errcnt;
  47. memset(s, 0, 4*sizeof(int));
  48. for (j=0; j<n; j++)
  49. for (i=0; i<t*2; i++)
  50. s[i] = cm(i+1, s[i]) ^ (c[j]&1);
  51. memset(a, 0, 6*sizeof(int));
  52. memset(b, 0, 6*sizeof(int));
  53. a[0] = 1;
  54. deg_a = t*2;
  55. for (i=0; i<t*2; i++) b[i] = s[t*2-1-i];
  56. deg_b = t*2-1;
  57. a[t*2+1] = 1;
  58. b[t*2+1] = 0;
  59. while (deg_b >= t) {
  60. if (b[0] == 0) {
  61. memmove(b, b+1, 5*sizeof(int));
  62. b[5] = 0;
  63. deg_b--;
  64. }
  65. else {
  66. for (i=t*2+1; i>deg_a; i--)
  67. b[i] = gm(b[i], b[0]) ^ gm(a[i], a[0]);
  68. for (i=deg_a; i>deg_b; i--) {
  69. b[i] = gm(b[i], b[0]);
  70. a[i] = gm(a[i], b[0]);
  71. }
  72. for (; i>0; i--)
  73. a[i] = gm(a[i], b[0]) ^ gm(b[i], a[0]);
  74. memmove(a, a+1, 5*sizeof(int));
  75. a[5] = 0;
  76. deg_a--;
  77. if (deg_a < deg_b) {
  78. memcpy(temp, a, 6*sizeof(int));
  79. memcpy(a, b, 6*sizeof(int));
  80. memcpy(b, temp, 6*sizeof(int));
  81. tmp = deg_a;
  82. deg_a = deg_b;
  83. deg_b = tmp;
  84. }
  85. }
  86. }
  87. deg_sig = t*2 - deg_a;
  88. memcpy(sig, a+deg_a+1, (deg_sig+1)*sizeof(int));
  89. errcnt = 0;
  90. for (j=0; j<255; j++) {
  91. tmp = 0;
  92. for (i=0; i<=deg_sig; i++) {
  93. sig[i] = cm(i, sig[i]);
  94. tmp ^= sig[i];
  95. }
  96. if (tmp == 0) {
  97. errloc[errcnt] = j - (255-n);
  98. if (errloc[errcnt] >= 0)
  99. errcnt++;
  100. }
  101. }
  102. if (errcnt<deg_sig) {
  103. return -1;
  104. }
  105. for (i=0; i<errcnt; i++) {
  106. c[errloc[i]] ^= 1;
  107. __D("fix error at %4d\n", errloc[i]);
  108. }
  109. return errcnt;
  110. }
  111. void efuse_bch_enc(const char *ibuf, int isize, char *obuf, int reverse)
  112. {
  113. int i, j;
  114. int tmp;
  115. //int errnum, errbit;
  116. char info;
  117. int c[255];
  118. int t = BCH_T;
  119. int n = isize*8 + t*8;
  120. for (i = 0; i < isize; ++i) {
  121. info = ibuf[i];
  122. if(reverse)
  123. info = ~info;
  124. for (j = 0; j < 8; ++j) {
  125. c[i*8 + j] = info >> (7 - j)&1;
  126. }
  127. }
  128. bch_enc(c, n, t);
  129. #ifdef __ADDERR
  130. /* add error */
  131. errnum = t;
  132. for ( i = 0; i < errnum; ++i) {
  133. errbit = rand()%n;
  134. c[errbit] ^= 1;
  135. __D("add error #%d at %d\n", i, errbit );
  136. }
  137. #endif
  138. for (i = 0; i < n/8; ++i) {
  139. tmp = 0;
  140. for (j = 0; j < 8; ++j) {
  141. tmp += c[i*8 + j]<<(7-j);
  142. }
  143. if(reverse)
  144. obuf[i] = ~tmp;
  145. else
  146. obuf[i] = tmp;
  147. }
  148. }
  149. void efuse_bch_dec(const char *ibuf, int isize, char *obuf, int reverse)
  150. {
  151. int i, j;
  152. int tmp;
  153. char info;
  154. int c[255];
  155. int t = BCH_T;
  156. int n = isize*8;
  157. for (i = 0; i < isize; ++i) {
  158. info = ibuf[i];
  159. if(reverse)
  160. info = ~info;
  161. for (j = 0; j < 8; ++j) {
  162. c[i*8 + j] = info >> (7 - j)&1;
  163. }
  164. }
  165. bch_dec(c, n, t);
  166. for (i = 0; i < (n/8 - t); ++i) {
  167. tmp = 0;
  168. for (j = 0; j < 8; ++j) {
  169. tmp += c[i*8 + j]<<(7-j);
  170. }
  171. if(reverse)
  172. obuf[i] = ~tmp;
  173. else
  174. obuf[i] = tmp;
  175. }
  176. }