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 unsigned char data[512]; 19 static unsigned char error_data[512]; 20 21 static int nand_ecc_test(const size_t size) 22 { 23 unsigned char code[3]; 24 unsigned char error_code[3]; 25 char testname[30]; 26 27 BUG_ON(sizeof(data) < size); 28 29 sprintf(testname, "nand-ecc-%zu", size); 30 31 get_random_bytes(data, size); 32 33 memcpy(error_data, data, size); 34 inject_single_bit_error(error_data, size); 35 36 __nand_calculate_ecc(data, size, code); 37 __nand_calculate_ecc(error_data, size, error_code); 38 __nand_correct_data(error_data, code, error_code, size); 39 40 if (!memcmp(data, error_data, size)) { 41 printk(KERN_INFO "mtd_nandecctest: ok - %s\n", testname); 42 return 0; 43 } 44 45 printk(KERN_ERR "mtd_nandecctest: not ok - %s\n", testname); 46 47 printk(KERN_DEBUG "hexdump of data:\n"); 48 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 16, 4, 49 data, size, false); 50 printk(KERN_DEBUG "hexdump of error data:\n"); 51 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 16, 4, 52 error_data, size, false); 53 54 return -EINVAL; 55 } 56 57 #else 58 59 static int nand_ecc_test(const size_t size) 60 { 61 return 0; 62 } 63 64 #endif 65 66 static int __init ecc_test_init(void) 67 { 68 int err; 69 70 err = nand_ecc_test(256); 71 if (err) 72 return err; 73 74 return nand_ecc_test(512); 75 } 76 77 static void __exit ecc_test_exit(void) 78 { 79 } 80 81 module_init(ecc_test_init); 82 module_exit(ecc_test_exit); 83 84 MODULE_DESCRIPTION("NAND ECC function test module"); 85 MODULE_AUTHOR("Akinobu Mita"); 86 MODULE_LICENSE("GPL"); 87