Transform 'u8' to 'uint8_t' in src/flash
[openocd.git] / src / flash / nand_ecc_kw.c
index ecc7adc265c542f81b780a96e96c59e6e0236b4e..de73326676c3ead5da91e969cd3b232a8ac6f4dc 100644 (file)
-/*\r
- * Reed-Solomon ECC handling for the Marvell Kirkwood SOC\r
- * Copyright (C) 2009 Marvell Semiconductor, Inc.\r
- *\r
- * Authors: Lennert Buytenhek <buytenh@wantstofly.org>\r
- *          Nicolas Pitre <nico@cam.org>\r
- *\r
- * This file is free software; you can redistribute it and/or modify it\r
- * under the terms of the GNU General Public License as published by the\r
- * Free Software Foundation; either version 2 or (at your option) any\r
- * later version.\r
- *\r
- * This file is distributed in the hope that it will be useful, but WITHOUT\r
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
- * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License\r
- * for more details.\r
- */\r
-\r
-#ifdef HAVE_CONFIG_H\r
-#include "config.h"\r
-#endif\r
-\r
-#include <sys/types.h>\r
-#include "nand.h"\r
-\r
-\r
-/*****************************************************************************\r
- * Arithmetic in GF(2^10) ("F") modulo x^10 + x^3 + 1.\r
- *\r
- * For multiplication, a discrete log/exponent table is used, with\r
- * primitive element x (F is a primitive field, so x is primitive).\r
- */\r
-#define MODPOLY                0x409           /* x^10 + x^3 + 1 in binary */\r
-\r
-/*\r
- * Maps an integer a [0..1022] to a polynomial b = gf_exp[a] in\r
- * GF(2^10) mod x^10 + x^3 + 1 such that b = x ^ a.  There's two\r
- * identical copies of this array back-to-back so that we can save\r
- * the mod 1023 operation when doing a GF multiplication.\r
- */\r
-static uint16_t gf_exp[1023 + 1023];\r
-\r
-/*\r
- * Maps a polynomial b in GF(2^10) mod x^10 + x^3 + 1 to an index\r
- * a = gf_log[b] in [0..1022] such that b = x ^ a.\r
- */\r
-static uint16_t gf_log[1024];\r
-\r
-static void gf_build_log_exp_table(void)\r
-{\r
-       int i;\r
-       int p_i;\r
-\r
-       /*\r
-        * p_i = x ^ i\r
-        *\r
-        * Initialise to 1 for i = 0.\r
-        */\r
-       p_i = 1;\r
-\r
-       for (i = 0; i < 1023; i++) {\r
-               gf_exp[i] = p_i;\r
-               gf_exp[i + 1023] = p_i;\r
-               gf_log[p_i] = i;\r
-\r
-               /*\r
-                * p_i = p_i * x\r
-                */\r
-               p_i <<= 1;\r
-               if (p_i & (1 << 10))\r
-                       p_i ^= MODPOLY;\r
-       }\r
-}\r
-\r
-\r
-/*****************************************************************************\r
- * Reed-Solomon code\r
- *\r
- * This implements a (1023,1015) Reed-Solomon ECC code over GF(2^10)\r
- * mod x^10 + x^3 + 1, shortened to (520,512).  The ECC data consists\r
- * of 8 10-bit symbols, or 10 8-bit bytes.\r
- *\r
- * Given 512 bytes of data, computes 10 bytes of ECC.\r
- *\r
- * This is done by converting the 512 bytes to 512 10-bit symbols\r
- * (elements of F), interpreting those symbols as a polynomial in F[X]\r
- * by taking symbol 0 as the coefficient of X^8 and symbol 511 as the\r
- * coefficient of X^519, and calculating the residue of that polynomial\r
- * divided by the generator polynomial, which gives us the 8 ECC symbols\r
- * as the remainder.  Finally, we convert the 8 10-bit ECC symbols to 10\r
- * 8-bit bytes.\r
- *\r
- * The generator polynomial is hardcoded, as that is faster, but it\r
- * can be computed by taking the primitive element a = x (in F), and\r
- * constructing a polynomial in F[X] with roots a, a^2, a^3, ..., a^8\r
- * by multiplying the minimal polynomials for those roots (which are\r
- * just 'x - a^i' for each i).\r
- *\r
- * Note: due to unfortunate circumstances, the bootrom in the Kirkwood SOC\r
- * expects the ECC to be computed backward, i.e. from the last byte down\r
- * to the first one.\r
- */\r
-int nand_calculate_ecc_kw(struct nand_device_s *device, const u8 *data, u8 *ecc)\r
-{\r
-       unsigned int r7, r6, r5, r4, r3, r2, r1, r0;\r
-       int i;\r
-       static int tables_initialized = 0;\r
-\r
-       if (!tables_initialized) {\r
-               gf_build_log_exp_table();\r
-               tables_initialized = 1;\r
-       }\r
-\r
-       /*\r
-        * Load bytes 504..511 of the data into r.\r
-        */\r
-       r0 = data[504];\r
-       r1 = data[505];\r
-       r2 = data[506];\r
-       r3 = data[507];\r
-       r4 = data[508];\r
-       r5 = data[509];\r
-       r6 = data[510];\r
-       r7 = data[511];\r
-\r
-\r
-       /*\r
-        * Shift bytes 503..0 (in that order) into r0, followed\r
-        * by eight zero bytes, while reducing the polynomial by the\r
-        * generator polynomial in every step.\r
-        */\r
-       for (i = 503; i >= -8; i--) {\r
-               unsigned int d;\r
-\r
-               d = 0;\r
-               if (i >= 0)\r
-                       d = data[i];\r
-\r
-               if (r7) {\r
-                       u16 *t = gf_exp + gf_log[r7];\r
-\r
-                       r7 = r6 ^ t[0x21c];\r
-                       r6 = r5 ^ t[0x181];\r
-                       r5 = r4 ^ t[0x18e];\r
-                       r4 = r3 ^ t[0x25f];\r
-                       r3 = r2 ^ t[0x197];\r
-                       r2 = r1 ^ t[0x193];\r
-                       r1 = r0 ^ t[0x237];\r
-                       r0 = d  ^ t[0x024];\r
-               } else {\r
-                       r7 = r6;\r
-                       r6 = r5;\r
-                       r5 = r4;\r
-                       r4 = r3;\r
-                       r3 = r2;\r
-                       r2 = r1;\r
-                       r1 = r0;\r
-                       r0 = d;\r
-               }\r
-       }\r
-\r
-       ecc[0] = r0;\r
-       ecc[1] = (r0 >> 8) | (r1 << 2);\r
-       ecc[2] = (r1 >> 6) | (r2 << 4);\r
-       ecc[3] = (r2 >> 4) | (r3 << 6);\r
-       ecc[4] = (r3 >> 2);\r
-       ecc[5] = r4;\r
-       ecc[6] = (r4 >> 8) | (r5 << 2);\r
-       ecc[7] = (r5 >> 6) | (r6 << 4);\r
-       ecc[8] = (r6 >> 4) | (r7 << 6);\r
-       ecc[9] = (r7 >> 2);\r
-\r
-       return 0;\r
-}\r
+/*
+ * Reed-Solomon ECC handling for the Marvell Kirkwood SOC
+ * Copyright (C) 2009 Marvell Semiconductor, Inc.
+ *
+ * Authors: Lennert Buytenhek <buytenh@wantstofly.org>
+ *          Nicolas Pitre <nico@cam.org>
+ *
+ * This file is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2 or (at your option) any
+ * later version.
+ *
+ * This file is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * for more details.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <sys/types.h>
+#include "nand.h"
+
+
+/*****************************************************************************
+ * Arithmetic in GF(2^10) ("F") modulo x^10 + x^3 + 1.
+ *
+ * For multiplication, a discrete log/exponent table is used, with
+ * primitive element x (F is a primitive field, so x is primitive).
+ */
+#define MODPOLY                0x409           /* x^10 + x^3 + 1 in binary */
+
+/*
+ * Maps an integer a [0..1022] to a polynomial b = gf_exp[a] in
+ * GF(2^10) mod x^10 + x^3 + 1 such that b = x ^ a.  There's two
+ * identical copies of this array back-to-back so that we can save
+ * the mod 1023 operation when doing a GF multiplication.
+ */
+static uint16_t gf_exp[1023 + 1023];
+
+/*
+ * Maps a polynomial b in GF(2^10) mod x^10 + x^3 + 1 to an index
+ * a = gf_log[b] in [0..1022] such that b = x ^ a.
+ */
+static uint16_t gf_log[1024];
+
+static void gf_build_log_exp_table(void)
+{
+       int i;
+       int p_i;
+
+       /*
+        * p_i = x ^ i
+        *
+        * Initialise to 1 for i = 0.
+        */
+       p_i = 1;
+
+       for (i = 0; i < 1023; i++) {
+               gf_exp[i] = p_i;
+               gf_exp[i + 1023] = p_i;
+               gf_log[p_i] = i;
+
+               /*
+                * p_i = p_i * x
+                */
+               p_i <<= 1;
+               if (p_i & (1 << 10))
+                       p_i ^= MODPOLY;
+       }
+}
+
+
+/*****************************************************************************
+ * Reed-Solomon code
+ *
+ * This implements a (1023,1015) Reed-Solomon ECC code over GF(2^10)
+ * mod x^10 + x^3 + 1, shortened to (520,512).  The ECC data consists
+ * of 8 10-bit symbols, or 10 8-bit bytes.
+ *
+ * Given 512 bytes of data, computes 10 bytes of ECC.
+ *
+ * This is done by converting the 512 bytes to 512 10-bit symbols
+ * (elements of F), interpreting those symbols as a polynomial in F[X]
+ * by taking symbol 0 as the coefficient of X^8 and symbol 511 as the
+ * coefficient of X^519, and calculating the residue of that polynomial
+ * divided by the generator polynomial, which gives us the 8 ECC symbols
+ * as the remainder.  Finally, we convert the 8 10-bit ECC symbols to 10
+ * 8-bit bytes.
+ *
+ * The generator polynomial is hardcoded, as that is faster, but it
+ * can be computed by taking the primitive element a = x (in F), and
+ * constructing a polynomial in F[X] with roots a, a^2, a^3, ..., a^8
+ * by multiplying the minimal polynomials for those roots (which are
+ * just 'x - a^i' for each i).
+ *
+ * Note: due to unfortunate circumstances, the bootrom in the Kirkwood SOC
+ * expects the ECC to be computed backward, i.e. from the last byte down
+ * to the first one.
+ */
+int nand_calculate_ecc_kw(struct nand_device_s *device, const uint8_t *data, uint8_t *ecc)
+{
+       unsigned int r7, r6, r5, r4, r3, r2, r1, r0;
+       int i;
+       static int tables_initialized = 0;
+
+       if (!tables_initialized) {
+               gf_build_log_exp_table();
+               tables_initialized = 1;
+       }
+
+       /*
+        * Load bytes 504..511 of the data into r.
+        */
+       r0 = data[504];
+       r1 = data[505];
+       r2 = data[506];
+       r3 = data[507];
+       r4 = data[508];
+       r5 = data[509];
+       r6 = data[510];
+       r7 = data[511];
+
+
+       /*
+        * Shift bytes 503..0 (in that order) into r0, followed
+        * by eight zero bytes, while reducing the polynomial by the
+        * generator polynomial in every step.
+        */
+       for (i = 503; i >= -8; i--) {
+               unsigned int d;
+
+               d = 0;
+               if (i >= 0)
+                       d = data[i];
+
+               if (r7) {
+                       u16 *t = gf_exp + gf_log[r7];
+
+                       r7 = r6 ^ t[0x21c];
+                       r6 = r5 ^ t[0x181];
+                       r5 = r4 ^ t[0x18e];
+                       r4 = r3 ^ t[0x25f];
+                       r3 = r2 ^ t[0x197];
+                       r2 = r1 ^ t[0x193];
+                       r1 = r0 ^ t[0x237];
+                       r0 = d  ^ t[0x024];
+               } else {
+                       r7 = r6;
+                       r6 = r5;
+                       r5 = r4;
+                       r4 = r3;
+                       r3 = r2;
+                       r2 = r1;
+                       r1 = r0;
+                       r0 = d;
+               }
+       }
+
+       ecc[0] = r0;
+       ecc[1] = (r0 >> 8) | (r1 << 2);
+       ecc[2] = (r1 >> 6) | (r2 << 4);
+       ecc[3] = (r2 >> 4) | (r3 << 6);
+       ecc[4] = (r3 >> 2);
+       ecc[5] = r4;
+       ecc[6] = (r4 >> 8) | (r5 << 2);
+       ecc[7] = (r5 >> 6) | (r6 << 4);
+       ecc[8] = (r6 >> 4) | (r7 << 6);
+       ecc[9] = (r7 >> 2);
+
+       return 0;
+}

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)