nand_ecc.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * calculate ecc code for nand flash
  3. *
  4. * Copyright (C) 2008 yajin <yajin@vm-kernel.org>
  5. * Copyright (C) 2009 Felix Fietkau <nbd@nbd.name>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 or
  10. * (at your option) version 3 of the License.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  20. * MA 02111-1307 USA
  21. */
  22. #include <sys/types.h>
  23. #include <sys/stat.h>
  24. #include <unistd.h>
  25. #include <stdlib.h>
  26. #include <stdint.h>
  27. #include <fcntl.h>
  28. #include <stdio.h>
  29. #define DEF_NAND_PAGE_SIZE 2048
  30. #define DEF_NAND_OOB_SIZE 64
  31. #define DEF_NAND_ECC_OFFSET 0x28
  32. static int page_size = DEF_NAND_PAGE_SIZE;
  33. static int oob_size = DEF_NAND_OOB_SIZE;
  34. static int ecc_offset = DEF_NAND_ECC_OFFSET;
  35. /*
  36. * Pre-calculated 256-way 1 byte column parity
  37. */
  38. static const uint8_t nand_ecc_precalc_table[] = {
  39. 0x00, 0x55, 0x56, 0x03, 0x59, 0x0c, 0x0f, 0x5a, 0x5a, 0x0f, 0x0c, 0x59, 0x03, 0x56, 0x55, 0x00,
  40. 0x65, 0x30, 0x33, 0x66, 0x3c, 0x69, 0x6a, 0x3f, 0x3f, 0x6a, 0x69, 0x3c, 0x66, 0x33, 0x30, 0x65,
  41. 0x66, 0x33, 0x30, 0x65, 0x3f, 0x6a, 0x69, 0x3c, 0x3c, 0x69, 0x6a, 0x3f, 0x65, 0x30, 0x33, 0x66,
  42. 0x03, 0x56, 0x55, 0x00, 0x5a, 0x0f, 0x0c, 0x59, 0x59, 0x0c, 0x0f, 0x5a, 0x00, 0x55, 0x56, 0x03,
  43. 0x69, 0x3c, 0x3f, 0x6a, 0x30, 0x65, 0x66, 0x33, 0x33, 0x66, 0x65, 0x30, 0x6a, 0x3f, 0x3c, 0x69,
  44. 0x0c, 0x59, 0x5a, 0x0f, 0x55, 0x00, 0x03, 0x56, 0x56, 0x03, 0x00, 0x55, 0x0f, 0x5a, 0x59, 0x0c,
  45. 0x0f, 0x5a, 0x59, 0x0c, 0x56, 0x03, 0x00, 0x55, 0x55, 0x00, 0x03, 0x56, 0x0c, 0x59, 0x5a, 0x0f,
  46. 0x6a, 0x3f, 0x3c, 0x69, 0x33, 0x66, 0x65, 0x30, 0x30, 0x65, 0x66, 0x33, 0x69, 0x3c, 0x3f, 0x6a,
  47. 0x6a, 0x3f, 0x3c, 0x69, 0x33, 0x66, 0x65, 0x30, 0x30, 0x65, 0x66, 0x33, 0x69, 0x3c, 0x3f, 0x6a,
  48. 0x0f, 0x5a, 0x59, 0x0c, 0x56, 0x03, 0x00, 0x55, 0x55, 0x00, 0x03, 0x56, 0x0c, 0x59, 0x5a, 0x0f,
  49. 0x0c, 0x59, 0x5a, 0x0f, 0x55, 0x00, 0x03, 0x56, 0x56, 0x03, 0x00, 0x55, 0x0f, 0x5a, 0x59, 0x0c,
  50. 0x69, 0x3c, 0x3f, 0x6a, 0x30, 0x65, 0x66, 0x33, 0x33, 0x66, 0x65, 0x30, 0x6a, 0x3f, 0x3c, 0x69,
  51. 0x03, 0x56, 0x55, 0x00, 0x5a, 0x0f, 0x0c, 0x59, 0x59, 0x0c, 0x0f, 0x5a, 0x00, 0x55, 0x56, 0x03,
  52. 0x66, 0x33, 0x30, 0x65, 0x3f, 0x6a, 0x69, 0x3c, 0x3c, 0x69, 0x6a, 0x3f, 0x65, 0x30, 0x33, 0x66,
  53. 0x65, 0x30, 0x33, 0x66, 0x3c, 0x69, 0x6a, 0x3f, 0x3f, 0x6a, 0x69, 0x3c, 0x66, 0x33, 0x30, 0x65,
  54. 0x00, 0x55, 0x56, 0x03, 0x59, 0x0c, 0x0f, 0x5a, 0x5a, 0x0f, 0x0c, 0x59, 0x03, 0x56, 0x55, 0x00
  55. };
  56. /**
  57. * nand_calculate_ecc - [NAND Interface] Calculate 3-byte ECC for 256-byte block
  58. * @dat: raw data
  59. * @ecc_code: buffer for ECC
  60. */
  61. int nand_calculate_ecc(const uint8_t *dat,
  62. uint8_t *ecc_code)
  63. {
  64. uint8_t idx, reg1, reg2, reg3, tmp1, tmp2;
  65. int i;
  66. /* Initialize variables */
  67. reg1 = reg2 = reg3 = 0;
  68. /* Build up column parity */
  69. for(i = 0; i < 256; i++) {
  70. /* Get CP0 - CP5 from table */
  71. idx = nand_ecc_precalc_table[*dat++];
  72. reg1 ^= (idx & 0x3f);
  73. /* All bit XOR = 1 ? */
  74. if (idx & 0x40) {
  75. reg3 ^= (uint8_t) i;
  76. reg2 ^= ~((uint8_t) i);
  77. }
  78. }
  79. /* Create non-inverted ECC code from line parity */
  80. tmp1 = (reg3 & 0x80) >> 0; /* B7 -> B7 */
  81. tmp1 |= (reg2 & 0x80) >> 1; /* B7 -> B6 */
  82. tmp1 |= (reg3 & 0x40) >> 1; /* B6 -> B5 */
  83. tmp1 |= (reg2 & 0x40) >> 2; /* B6 -> B4 */
  84. tmp1 |= (reg3 & 0x20) >> 2; /* B5 -> B3 */
  85. tmp1 |= (reg2 & 0x20) >> 3; /* B5 -> B2 */
  86. tmp1 |= (reg3 & 0x10) >> 3; /* B4 -> B1 */
  87. tmp1 |= (reg2 & 0x10) >> 4; /* B4 -> B0 */
  88. tmp2 = (reg3 & 0x08) << 4; /* B3 -> B7 */
  89. tmp2 |= (reg2 & 0x08) << 3; /* B3 -> B6 */
  90. tmp2 |= (reg3 & 0x04) << 3; /* B2 -> B5 */
  91. tmp2 |= (reg2 & 0x04) << 2; /* B2 -> B4 */
  92. tmp2 |= (reg3 & 0x02) << 2; /* B1 -> B3 */
  93. tmp2 |= (reg2 & 0x02) << 1; /* B1 -> B2 */
  94. tmp2 |= (reg3 & 0x01) << 1; /* B0 -> B1 */
  95. tmp2 |= (reg2 & 0x01) << 0; /* B7 -> B0 */
  96. /* Calculate final ECC code */
  97. #ifdef CONFIG_MTD_NAND_ECC_SMC
  98. ecc_code[0] = ~tmp2;
  99. ecc_code[1] = ~tmp1;
  100. #else
  101. ecc_code[0] = ~tmp1;
  102. ecc_code[1] = ~tmp2;
  103. #endif
  104. ecc_code[2] = ((~reg1) << 2) | 0x03;
  105. return 0;
  106. }
  107. /*
  108. * usage: bb-nandflash-ecc start_address size
  109. */
  110. void usage(const char *prog)
  111. {
  112. fprintf(stderr, "Usage: %s [options] <input> <output>\n"
  113. "Options:\n"
  114. " -p <pagesize> NAND page size (default: %d)\n"
  115. " -o <oobsize> NAND OOB size (default: %d)\n"
  116. " -e <offset> NAND ECC offset (default: %d)\n"
  117. "\n", prog, DEF_NAND_PAGE_SIZE, DEF_NAND_OOB_SIZE,
  118. DEF_NAND_ECC_OFFSET);
  119. exit(1);
  120. }
  121. /*start_address/size does not include oob
  122. */
  123. int main(int argc, char **argv)
  124. {
  125. uint8_t *page_data = NULL;
  126. uint8_t *ecc_data;
  127. int infd = -1, outfd = -1;
  128. int ret = 1;
  129. ssize_t bytes;
  130. int ch;
  131. while ((ch = getopt(argc, argv, "e:o:p:")) != -1) {
  132. switch(ch) {
  133. case 'p':
  134. page_size = strtoul(optarg, NULL, 0);
  135. break;
  136. case 'o':
  137. oob_size = strtoul(optarg, NULL, 0);
  138. break;
  139. case 'e':
  140. ecc_offset = strtoul(optarg, NULL, 0);
  141. break;
  142. default:
  143. usage(argv[0]);
  144. }
  145. }
  146. argc -= optind;
  147. if (argc < 2)
  148. usage(argv[0]);
  149. argv += optind;
  150. infd = open(argv[0], O_RDONLY, 0);
  151. if (infd < 0) {
  152. perror("open input file");
  153. goto out;
  154. }
  155. outfd = open(argv[1], O_WRONLY|O_CREAT|O_TRUNC, 0644);
  156. if (outfd < 0) {
  157. perror("open output file");
  158. goto out;
  159. }
  160. page_data = malloc(page_size + oob_size);
  161. while ((bytes = read(infd, page_data, page_size)) == page_size) {
  162. int j;
  163. ecc_data = page_data + page_size + ecc_offset;
  164. for (j = 0; j < page_size / 256; j++)
  165. {
  166. nand_calculate_ecc(page_data + j * 256, ecc_data);
  167. ecc_data += 3;
  168. }
  169. write(outfd, page_data, page_size + oob_size);
  170. }
  171. ret = 0;
  172. out:
  173. if (infd >= 0)
  174. close(infd);
  175. if (outfd >= 0)
  176. close(outfd);
  177. if (page_data)
  178. free(page_data);
  179. return ret;
  180. }