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/mtd/nand_ecc.h> 8 9 #if defined(CONFIG_MTD_NAND) || defined(CONFIG_MTD_NAND_MODULE) 10 11 static void inject_single_bit_error(void *data, size_t size) 12 { 13 unsigned long offset = random32() % (size * BITS_PER_BYTE); 14 15 __change_bit(offset, data); 16 } 17 18 static void dump_data_ecc(void *error_data, void *error_ecc, void *correct_data, 19 void *correct_ecc, const size_t size) 20 { 21 pr_info("hexdump of error data:\n"); 22 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET, 16, 4, 23 error_data, size, false); 24 print_hex_dump(KERN_INFO, "hexdump of error ecc: ", 25 DUMP_PREFIX_NONE, 16, 1, error_ecc, 3, false); 26 27 pr_info("hexdump of correct data:\n"); 28 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET, 16, 4, 29 correct_data, size, false); 30 print_hex_dump(KERN_INFO, "hexdump of correct ecc: ", 31 DUMP_PREFIX_NONE, 16, 1, correct_ecc, 3, false); 32 } 33 34 static unsigned char correct_data[512]; 35 static unsigned char error_data[512]; 36 37 static int nand_ecc_test(const size_t size) 38 { 39 unsigned char correct_ecc[3]; 40 unsigned char error_ecc[3]; 41 char testname[30]; 42 43 BUG_ON(sizeof(correct_data) < size); 44 45 sprintf(testname, "nand-ecc-%zu", size); 46 47 get_random_bytes(correct_data, size); 48 49 memcpy(error_data, correct_data, size); 50 inject_single_bit_error(error_data, size); 51 52 __nand_calculate_ecc(correct_data, size, correct_ecc); 53 __nand_calculate_ecc(error_data, size, error_ecc); 54 __nand_correct_data(error_data, correct_ecc, error_ecc, size); 55 56 if (!memcmp(correct_data, error_data, size)) { 57 pr_info("mtd_nandecctest: ok - %s\n", testname); 58 return 0; 59 } 60 61 pr_err("mtd_nandecctest: not ok - %s\n", testname); 62 dump_data_ecc(error_data, error_ecc, correct_data, correct_ecc, size); 63 64 return -EINVAL; 65 } 66 67 #else 68 69 static int nand_ecc_test(const size_t size) 70 { 71 return 0; 72 } 73 74 #endif 75 76 static int __init ecc_test_init(void) 77 { 78 int err; 79 80 err = nand_ecc_test(256); 81 if (err) 82 return err; 83 84 return nand_ecc_test(512); 85 } 86 87 static void __exit ecc_test_exit(void) 88 { 89 } 90 91 module_init(ecc_test_init); 92 module_exit(ecc_test_exit); 93 94 MODULE_DESCRIPTION("NAND ECC function test module"); 95 MODULE_AUTHOR("Akinobu Mita"); 96 MODULE_LICENSE("GPL"); 97