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