Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Generic Reed Solomon encoder / decoder library
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright 2002, Phil Karn, KA9Q
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * May be used under the terms of the GNU General Public License (GPL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * Adaption to the kernel by Thomas Gleixner (tglx@linutronix.de)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  * Generic data width independent code which is included by the wrappers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) 	struct rs_codec *rs = rsc->codec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 	int deg_lambda, el, deg_omega;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) 	int i, j, r, k, pad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 	int nn = rs->nn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 	int nroots = rs->nroots;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 	int fcr = rs->fcr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 	int prim = rs->prim;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 	int iprim = rs->iprim;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 	uint16_t *alpha_to = rs->alpha_to;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 	uint16_t *index_of = rs->index_of;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 	uint16_t u, q, tmp, num1, num2, den, discr_r, syn_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 	int count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	int num_corrected;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	uint16_t msk = (uint16_t) rs->nn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	 * The decoder buffers are in the rs control struct. They are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	 * arrays sized [nroots + 1]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	uint16_t *lambda = rsc->buffers + RS_DECODE_LAMBDA * (nroots + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	uint16_t *syn = rsc->buffers + RS_DECODE_SYN * (nroots + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	uint16_t *b = rsc->buffers + RS_DECODE_B * (nroots + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	uint16_t *t = rsc->buffers + RS_DECODE_T * (nroots + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	uint16_t *omega = rsc->buffers + RS_DECODE_OMEGA * (nroots + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	uint16_t *root = rsc->buffers + RS_DECODE_ROOT * (nroots + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	uint16_t *reg = rsc->buffers + RS_DECODE_REG * (nroots + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	uint16_t *loc = rsc->buffers + RS_DECODE_LOC * (nroots + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	/* Check length parameter for validity */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	pad = nn - nroots - len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	BUG_ON(pad < 0 || pad >= nn - nroots);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	/* Does the caller provide the syndrome ? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	if (s != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 		for (i = 0; i < nroots; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 			/* The syndrome is in index form,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 			 * so nn represents zero
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 			if (s[i] != nn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 				goto decode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 		/* syndrome is zero, no errors to correct  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	/* form the syndromes; i.e., evaluate data(x) at roots of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	 * g(x) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	for (i = 0; i < nroots; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 		syn[i] = (((uint16_t) data[0]) ^ invmsk) & msk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	for (j = 1; j < len; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 		for (i = 0; i < nroots; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 			if (syn[i] == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 				syn[i] = (((uint16_t) data[j]) ^
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 					  invmsk) & msk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 				syn[i] = ((((uint16_t) data[j]) ^
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 					   invmsk) & msk) ^
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 					alpha_to[rs_modnn(rs, index_of[syn[i]] +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 						       (fcr + i) * prim)];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	for (j = 0; j < nroots; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		for (i = 0; i < nroots; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 			if (syn[i] == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 				syn[i] = ((uint16_t) par[j]) & msk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 				syn[i] = (((uint16_t) par[j]) & msk) ^
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 					alpha_to[rs_modnn(rs, index_of[syn[i]] +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 						       (fcr+i)*prim)];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	s = syn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	/* Convert syndromes to index form, checking for nonzero condition */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	syn_error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	for (i = 0; i < nroots; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 		syn_error |= s[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 		s[i] = index_of[s[i]];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	if (!syn_error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 		/* if syndrome is zero, data[] is a codeword and there are no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		 * errors to correct. So return data[] unmodified
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)  decode:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	memset(&lambda[1], 0, nroots * sizeof(lambda[0]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	lambda[0] = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	if (no_eras > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 		/* Init lambda to be the erasure locator polynomial */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		lambda[1] = alpha_to[rs_modnn(rs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 					prim * (nn - 1 - (eras_pos[0] + pad)))];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 		for (i = 1; i < no_eras; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 			u = rs_modnn(rs, prim * (nn - 1 - (eras_pos[i] + pad)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 			for (j = i + 1; j > 0; j--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 				tmp = index_of[lambda[j - 1]];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 				if (tmp != nn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 					lambda[j] ^=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 						alpha_to[rs_modnn(rs, u + tmp)];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	for (i = 0; i < nroots + 1; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		b[i] = index_of[lambda[i]];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	 * Begin Berlekamp-Massey algorithm to determine error+erasure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	 * locator polynomial
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	r = no_eras;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	el = no_eras;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	while (++r <= nroots) {	/* r is the step number */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 		/* Compute discrepancy at the r-th step in poly-form */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		discr_r = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		for (i = 0; i < r; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 			if ((lambda[i] != 0) && (s[r - i - 1] != nn)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 				discr_r ^=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 					alpha_to[rs_modnn(rs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 							  index_of[lambda[i]] +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 							  s[r - i - 1])];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 		discr_r = index_of[discr_r];	/* Index form */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 		if (discr_r == nn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 			/* 2 lines below: B(x) <-- x*B(x) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 			memmove (&b[1], b, nroots * sizeof (b[0]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 			b[0] = nn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 			/* 7 lines below: T(x) <-- lambda(x)-discr_r*x*b(x) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 			t[0] = lambda[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 			for (i = 0; i < nroots; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 				if (b[i] != nn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 					t[i + 1] = lambda[i + 1] ^
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 						alpha_to[rs_modnn(rs, discr_r +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 								  b[i])];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 				} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 					t[i + 1] = lambda[i + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 			if (2 * el <= r + no_eras - 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 				el = r + no_eras - el;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 				/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 				 * 2 lines below: B(x) <-- inv(discr_r) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 				 * lambda(x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 				 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 				for (i = 0; i <= nroots; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 					b[i] = (lambda[i] == 0) ? nn :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 						rs_modnn(rs, index_of[lambda[i]]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 							 - discr_r + nn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 				/* 2 lines below: B(x) <-- x*B(x) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 				memmove(&b[1], b, nroots * sizeof(b[0]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 				b[0] = nn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 			memcpy(lambda, t, (nroots + 1) * sizeof(t[0]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	/* Convert lambda to index form and compute deg(lambda(x)) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	deg_lambda = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	for (i = 0; i < nroots + 1; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 		lambda[i] = index_of[lambda[i]];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 		if (lambda[i] != nn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 			deg_lambda = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	if (deg_lambda == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 		 * deg(lambda) is zero even though the syndrome is non-zero
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 		 * => uncorrectable error detected
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 		return -EBADMSG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	/* Find roots of error+erasure locator polynomial by Chien search */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	memcpy(&reg[1], &lambda[1], nroots * sizeof(reg[0]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	count = 0;		/* Number of roots of lambda(x) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	for (i = 1, k = iprim - 1; i <= nn; i++, k = rs_modnn(rs, k + iprim)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 		q = 1;		/* lambda[0] is always 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 		for (j = deg_lambda; j > 0; j--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 			if (reg[j] != nn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 				reg[j] = rs_modnn(rs, reg[j] + j);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 				q ^= alpha_to[reg[j]];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 		if (q != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 			continue;	/* Not a root */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 		if (k < pad) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 			/* Impossible error location. Uncorrectable error. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 			return -EBADMSG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 		/* store root (index-form) and error location number */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 		root[count] = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 		loc[count] = k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 		/* If we've already found max possible roots,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 		 * abort the search to save time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 		if (++count == deg_lambda)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	if (deg_lambda != count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 		 * deg(lambda) unequal to number of roots => uncorrectable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 		 * error detected
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 		return -EBADMSG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	 * Compute err+eras evaluator poly omega(x) = s(x)*lambda(x) (modulo
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	 * x**nroots). in index form. Also find deg(omega).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	deg_omega = deg_lambda - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	for (i = 0; i <= deg_omega; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 		tmp = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 		for (j = i; j >= 0; j--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 			if ((s[i - j] != nn) && (lambda[j] != nn))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 				tmp ^=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 				    alpha_to[rs_modnn(rs, s[i - j] + lambda[j])];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 		omega[i] = index_of[tmp];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	 * Compute error values in poly-form. num1 = omega(inv(X(l))), num2 =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	 * inv(X(l))**(fcr-1) and den = lambda_pr(inv(X(l))) all in poly-form
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	 * Note: we reuse the buffer for b to store the correction pattern
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	num_corrected = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	for (j = count - 1; j >= 0; j--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 		num1 = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 		for (i = deg_omega; i >= 0; i--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 			if (omega[i] != nn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 				num1 ^= alpha_to[rs_modnn(rs, omega[i] +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 							i * root[j])];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 		if (num1 == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 			/* Nothing to correct at this position */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 			b[j] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 		num2 = alpha_to[rs_modnn(rs, root[j] * (fcr - 1) + nn)];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 		den = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 		/* lambda[i+1] for i even is the formal derivative
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 		 * lambda_pr of lambda[i] */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 		for (i = min(deg_lambda, nroots - 1) & ~1; i >= 0; i -= 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 			if (lambda[i + 1] != nn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 				den ^= alpha_to[rs_modnn(rs, lambda[i + 1] +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 						       i * root[j])];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 		b[j] = alpha_to[rs_modnn(rs, index_of[num1] +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 					       index_of[num2] +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 					       nn - index_of[den])];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 		num_corrected++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	 * We compute the syndrome of the 'error' and check that it matches
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	 * the syndrome of the received word
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	for (i = 0; i < nroots; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 		tmp = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 		for (j = 0; j < count; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 			if (b[j] == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 			k = (fcr + i) * prim * (nn-loc[j]-1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 			tmp ^= alpha_to[rs_modnn(rs, index_of[b[j]] + k)];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 		if (tmp != alpha_to[s[i]])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 			return -EBADMSG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	 * Store the error correction pattern, if a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	 * correction buffer is available
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	if (corr && eras_pos) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 		j = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 		for (i = 0; i < count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 			if (b[i]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 				corr[j] = b[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 				eras_pos[j++] = loc[i] - pad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	} else if (data && par) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 		/* Apply error to data and parity */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 		for (i = 0; i < count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 			if (loc[i] < (nn - nroots))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 				data[loc[i] - pad] ^= b[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 				par[loc[i] - pad - len] ^= b[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	return  num_corrected;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) }