mtd_nandecctest.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #include <linux/kernel.h>
  2. #include <linux/module.h>
  3. #include <linux/list.h>
  4. #include <linux/random.h>
  5. #include <linux/string.h>
  6. #include <linux/bitops.h>
  7. #include <linux/jiffies.h>
  8. #include <linux/mtd/nand_ecc.h>
  9. #if defined(CONFIG_MTD_NAND) || defined(CONFIG_MTD_NAND_MODULE)
  10. static void inject_single_bit_error(void *data, size_t size)
  11. {
  12. unsigned long offset = random32() % (size * BITS_PER_BYTE);
  13. __change_bit(offset, data);
  14. }
  15. static unsigned char data[512];
  16. static unsigned char error_data[512];
  17. static int nand_ecc_test(const size_t size)
  18. {
  19. unsigned char code[3];
  20. unsigned char error_code[3];
  21. char testname[30];
  22. BUG_ON(sizeof(data) < size);
  23. sprintf(testname, "nand-ecc-%zu", size);
  24. get_random_bytes(data, size);
  25. memcpy(error_data, data, size);
  26. inject_single_bit_error(error_data, size);
  27. __nand_calculate_ecc(data, size, code);
  28. __nand_calculate_ecc(error_data, size, error_code);
  29. __nand_correct_data(error_data, code, error_code, size);
  30. if (!memcmp(data, error_data, size)) {
  31. printk(KERN_INFO "mtd_nandecctest: ok - %s\n", testname);
  32. return 0;
  33. }
  34. printk(KERN_ERR "mtd_nandecctest: not ok - %s\n", testname);
  35. printk(KERN_DEBUG "hexdump of data:\n");
  36. print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 16, 4,
  37. data, size, false);
  38. printk(KERN_DEBUG "hexdump of error data:\n");
  39. print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 16, 4,
  40. error_data, size, false);
  41. return -1;
  42. }
  43. #else
  44. static int nand_ecc_test(const size_t size)
  45. {
  46. return 0;
  47. }
  48. #endif
  49. static int __init ecc_test_init(void)
  50. {
  51. srandom32(jiffies);
  52. nand_ecc_test(256);
  53. nand_ecc_test(512);
  54. return 0;
  55. }
  56. static void __exit ecc_test_exit(void)
  57. {
  58. }
  59. module_init(ecc_test_init);
  60. module_exit(ecc_test_exit);
  61. MODULE_DESCRIPTION("NAND ECC function test module");
  62. MODULE_AUTHOR("Akinobu Mita");
  63. MODULE_LICENSE("GPL");