linux MTD子系统框架

1. MTD:Memory Technology Device,内存技术设备

MTD(memory technology device内存技术设备)是用于访问memory设备(ROM、flash)的Linux的子系统。MTD的主要目的是为了使新的memory设备的驱动更加简单,为此它在硬件和上层之间提供了一个抽象的接口。MTD的所有源代码在/drivers/mtd子目录下。CFI接口的MTD设备分为四层(从设备节点直到底层硬件驱动),这四层从上到下依次是:设备节点、MTD设备层、MTD原始设备层和硬件驱动层
在这里插入图片描述
在这里插入图片描述
2. MTD分块设备和字符设备. 在/dev/目录下,可以看到类似mtd× / mtdblock× 文件
在这里插入图片描述
3. 打开 “/proc/mtd” 文件即可解析出MTD分区信息,从这里可以知道MTD数目,SIZE,擦写块大小及DEV NAME。
可看出每个mtd设备有两个:一个是RW设备,另一个的RO,一般使用不带ro后缀的设备
00c00000

  1. 实现硬写nand 和读取nand(主要是操作mtd字符设备). 相关代码实现如下:
    无论读或者写nand,check有效块的时候,一定要加上退出判断条件,不能check整个块。 特别是系统第一次read nand数据的时候,由于nand中没有数据,会一直轮训check所有的块,就拿mtd9来说,对应的size为0x10000000,擦除块为0x20000, 如果一直轮询查询,大概要循环2048次,会导致系统启动异常。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <mtd/mtd-user.h>
#ifdef NAND_DATA_DEBUG_DUMP
    #define NAND_DUMP(format, arg...) printf(format, ##arg)
#else
    #define NAND_DUMP(format, ...)
#endif
#define  NAND_PARAM_MTD   "/dev/mtd9"
int32_t write_wifi_pwd_into_nand(uint32_t addr, void *param, int32_t param_len)
{
	int32_t i = 0;
	int32_t n = 0;
	int32_t ret = 0;
	int32_t fd = -1;
	struct mtd_info_user meminfo;
	erase_info_t eraseinfo;
	uint32_t block_start = 0;
	uint32_t mtd_offset = addr;  //mtd9 size 10000000
	loff_t offs;
	int32_t block_align = 1;
	int32_t bad_block_flag = 0;
	int32_t write_done = 0;
	int32_t write_len = param_len + 2;
	uint16_t crc16 = 0;
	unsigned char write_buf[2*1024] = {0};
	unsigned char read_buf[2*1024] = {0};
	uint32_t block_skipped = 0;

	/*fill data into buf*/
	memcpy(write_buf, (char *)param, param_len);
	/*add crc check bytes into buf*/
	crc16 = do_ccitt_crc16((uint8_t *)param, param_len);
	write_buf[param_len] = (crc16 >> 8) & 0xFF;
	write_buf[param_len+1] = crc16 & 0xFF;
	printf("write to nand:\n");
	for(i=0;i<write_len;++i)
	{
		printf("%02X ", write_buf[i]);
	}
	printf("\n");

	fd = open(NAND_PARAM_MTD, O_RDWR);
	if(fd < 0)
	{
		printf("[%s]ERROR: open mtd device failed\n", __FUNCTION__);
		return -1;
	}

	if(ioctl(fd, MEMGETINFO, &meminfo) != 0)
	{
		printf("[%s]ERROR: MEMGETINFO failed\n", __FUNCTION__);
		close(fd);
		return -1;
	}
	printf("[%s]INFO: Nand Flash erase_size=%d, write_size=%d\n", __FUNCTION__, meminfo.erasesize, meminfo.writesize);

	/* Write out the Page data */
	while(!write_done && (mtd_offset+write_len < meminfo.size))
	{
		//while(block_start != (mtd_offset & (~meminfo.erasesize + 1)))  //block alignment
		{
			block_start = mtd_offset & (~meminfo.erasesize + 1);
			offs = block_start;
			bad_block_flag = 0;
			/* Check all the blocks in an erase block for bad blocks */
			do
			{
				if ((ret = ioctl(fd, MEMGETBADBLOCK, &offs)) < 0)
				{
					printf("[%s]ERROR: MEMGETBADBLOCK failed\n", __FUNCTION__);
					close(fd);
					return -1;
				}
				if (1 == ret) {
					bad_block_flag = 1;
					printf("[%s]INFO: Bad block at %x, %d block(s) "
								"from %x will be skipped\n", __FUNCTION__,
								(int32_t)offs, block_align, block_start);
				}

				if (bad_block_flag) {
					mtd_offset = block_start + meminfo.erasesize;
				}
				offs +=  meminfo.erasesize / block_align;
			} while (offs < block_start + meminfo.erasesize);
		}

		/*erase block*/
		eraseinfo.start = mtd_offset;
		eraseinfo.length = meminfo.erasesize;
		if(ioctl(fd, MEMERASE, &eraseinfo) < 0)
		{
			printf("[%s]ERROR: erase 1 block at %x failed\n", __FUNCTION__, eraseinfo.start);
			close(fd);
			return -1;
		}
		/*write data*/
		if(pwrite(fd, write_buf, meminfo.writesize, mtd_offset) != (int32_t)meminfo.writesize)
		{
			printf("[%s]ERROR: write out page data failed\n", __FUNCTION__);
			close(fd);
			return -1;
		}
		/*read out data to check if block is write bad*/
		if(pread(fd, read_buf, meminfo.writesize, mtd_offset) != (int32_t)meminfo.writesize)
		{
			printf("[%s]ERROR: read out page data failed\n", __FUNCTION__);
			close(fd);
			return -1;
		}
		n = 0;
		while((write_buf[n] == read_buf[n]) && (n < write_len))
		{
			n++;
		}
		if(n < write_len) //find write error, maybe it's a bad block, skip
		{
			offs = (loff_t)block_start;
			if(ioctl(fd, MEMSETBADBLOCK, &offs) < 0)
			{
				printf("[%s]ERROR: MEMSETBADBLOCK failed\n", __FUNCTION__);
				close(fd);
				return -1;
			}
			mtd_offset = block_start + meminfo.erasesize;
			block_skipped++;
			if(block_skipped > (0x080000/meminfo.erasesize - 1))
			{
			    printf("all blocks skipped: addr = %x\n", mtd_offset);
			    close(fd);
			    return -1;
			}
			printf("[%s]WARNING: data is not correct at block %x, skipped [%d : %d]\n", __FUNCTION__,
			       mtd_offset, block_skipped, 0x080000/meminfo.erasesize - 1);

		}
		else
		{
			write_done = 1;
		}
	}
	close(fd);
	if(!write_done)
	{
		printf("[%s]ERROR: fail to write data into nand, block is used up\n", __FUNCTION__);
		return -1;
	}

	return 0;
}
int32_t read_wifi_pwd_from_nand(uint32_t addr, void *param, int32_t param_len)
{
#if TEST_STATE
	int32_t i = 0;
#endif
	int32_t ret = 0;
	int32_t fd = -1;
	struct mtd_info_user meminfo;
	uint32_t block_start = 0;
	uint32_t mtd_offset = addr;  //mtd9 size 10000000
	loff_t offs;
	int32_t block_align = 1;
	int32_t bad_block_flag = 0;
	int32_t read_done = 0;
	int32_t read_len = param_len + 2;
	uint16_t crc16 = 0;
	unsigned char read_buf[2*1024] = {0};
	uint32_t block_skipped = 0;

	fd = open(NAND_PARAM_MTD, O_RDWR);
	if(fd < 0)
	{
		printf("[%s]ERROR: open mtd device failed\n", __FUNCTION__);
		return -1;
	}

	if(ioctl(fd, MEMGETINFO, &meminfo) != 0)
	{
		printf("[%s]ERROR: MEMGETINFO failed\n", __FUNCTION__);
		close(fd);
		return -1;
	}

	/* Read out the Page data */
	while(!read_done && (mtd_offset+read_len < meminfo.size))
	{
		//while(block_start != (mtd_offset & (~meminfo.erasesize + 1)))  //block alignment
		{
			block_start = mtd_offset & (~meminfo.erasesize + 1);
			offs = block_start;
			bad_block_flag = 0;
			/* Check all the blocks in an erase block for bad blocks */
			do
			{
				if ((ret = ioctl(fd, MEMGETBADBLOCK, &offs)) < 0)
				{
					printf("[%s]ERROR: MEMGETBADBLOCK failed\n", __FUNCTION__);
					close(fd);
					return -1;
				}
				if (1 == ret) {
					bad_block_flag = 1;
					printf("[%s]INFO: Bad block at %x, %d block(s) "
								"from %x will be skipped\n", __FUNCTION__,
								(int32_t)offs, block_align, block_start);
				}
				if (bad_block_flag) {
					mtd_offset = block_start + meminfo.erasesize;  //jump to next block
				}
				offs +=  meminfo.erasesize / block_align;
			} while (offs < block_start + meminfo.erasesize);
		}

		if (pread(fd, read_buf, meminfo.writesize, mtd_offset) != (int32_t)meminfo.writesize)
		{
			printf("[%s]ERROR: write out page data failed\n", __FUNCTION__);
			close(fd);
			return -1;
		}
		/*check crc*/
		crc16 = do_ccitt_crc16((uint8_t *)read_buf, param_len);
		if((((crc16 >> 8) & 0xFF) != read_buf[param_len]) || ((crc16 & 0xFF) != read_buf[param_len+1]))
		{
			printf("[%s]WARNING: read bad block at %x, try next block\n", __FUNCTION__, mtd_offset);
			mtd_offset += meminfo.erasesize;  //jump to next block
			block_skipped++;
			if(block_skipped > (0x080000/meminfo.erasesize - 1))
			{
			    printf("all blocks skipped: addr = %x\n", mtd_offset);
			    close(fd);
			    return -1;
			}
			printf("[%s]WARNING: data is not correct at block %x, skipped [%d : %d]\n", __FUNCTION__,
			                mtd_offset, block_skipped, 0x080000/meminfo.erasesize - 1);
		}
		else
		{
			read_done = 1;
			memcpy(param, read_buf, param_len);
#if TEST_STATE
			printf("[%s]INFO: read data from nand, block start at %x\n", __FUNCTION__, mtd_offset);
			for(i=0;i<param_len;++i)
			{
				printf("%02X ", ((char *)param)[i]);
			}
			printf("\n");
#endif
		}
	}
	close(fd);
	if(!read_done)
	{
		printf("[%s]ERROR: fail to read out data from nand, block is used up\n", __FUNCTION__);
		return -1;
	}

	return 0;
}

const unsigned char crc8_table[256] = {
	0x00, 0x5e, 0xbc, 0xe2, 0x61, 0x3f, 0xdd, 0x83,
	0xc2, 0x9c, 0x7e, 0x20, 0xa3, 0xfd, 0x1f, 0x41,
	0x9d, 0xc3, 0x21, 0x7f, 0xfc, 0xa2, 0x40, 0x1e,
	0x5f, 0x01, 0xe3, 0xbd, 0x3e, 0x60, 0x82, 0xdc,
	0x23, 0x7d, 0x9f, 0xc1, 0x42, 0x1c, 0xfe, 0xa0,
	0xe1, 0xbf, 0x5d, 0x03, 0x80, 0xde, 0x3c, 0x62,
	0xbe, 0xe0, 0x02, 0x5c, 0xdf, 0x81, 0x63, 0x3d,
	0x7c, 0x22, 0xc0, 0x9e, 0x1d, 0x43, 0xa1, 0xff,
	0x46, 0x18, 0xfa, 0xa4, 0x27, 0x79, 0x9b, 0xc5,
	0x84, 0xda, 0x38, 0x66, 0xe5, 0xbb, 0x59, 0x07,
	0xdb, 0x85, 0x67, 0x39, 0xba, 0xe4, 0x06, 0x58,
	0x19, 0x47, 0xa5, 0xfb, 0x78, 0x26, 0xc4, 0x9a,
	0x65, 0x3b, 0xd9, 0x87, 0x04, 0x5a, 0xb8, 0xe6,
	0xa7, 0xf9, 0x1b, 0x45, 0xc6, 0x98, 0x7a, 0x24,
	0xf8, 0xa6, 0x44, 0x1a, 0x99, 0xc7, 0x25, 0x7b,
	0x3a, 0x64, 0x86, 0xd8, 0x5b, 0x05, 0xe7, 0xb9,
	0x8c, 0xd2, 0x30, 0x6e, 0xed, 0xb3, 0x51, 0x0f,
	0x4e, 0x10, 0xf2, 0xac, 0x2f, 0x71, 0x93, 0xcd,
	0x11, 0x4f, 0xad, 0xf3, 0x70, 0x2e, 0xcc, 0x92,
	0xd3, 0x8d, 0x6f, 0x31, 0xb2, 0xec, 0x0e, 0x50,
	0xaf, 0xf1, 0x13, 0x4d, 0xce, 0x90, 0x72, 0x2c,
	0x6d, 0x33, 0xd1, 0x8f, 0x0c, 0x52, 0xb0, 0xee,
	0x32, 0x6c, 0x8e, 0xd0, 0x53, 0x0d, 0xef, 0xb1,
	0xf0, 0xae, 0x4c, 0x12, 0x91, 0xcf, 0x2d, 0x73,
	0xca, 0x94, 0x76, 0x28, 0xab, 0xf5, 0x17, 0x49,
	0x08, 0x56, 0xb4, 0xea, 0x69, 0x37, 0xd5, 0x8b,
	0x57, 0x09, 0xeb, 0xb5, 0x36, 0x68, 0x8a, 0xd4,
	0x95, 0xcb, 0x29, 0x77, 0xf4, 0xaa, 0x48, 0x16,
	0xe9, 0xb7, 0x55, 0x0b, 0x88, 0xd6, 0x34, 0x6a,
	0x2b, 0x75, 0x97, 0xc9, 0x4a, 0x14, 0xf6, 0xa8,
	0x74, 0x2a, 0xc8, 0x96, 0x15, 0x4b, 0xa9, 0xf7,
	0xb6, 0xe8, 0x0a, 0x54, 0xd7, 0x89, 0x6b, 0x35,
};

const uint16_t crc16_ccitt_table[256] = {
    0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7,
    0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF,
    0x1231, 0x0210, 0x3273, 0x2252, 0x52B5, 0x4294, 0x72F7, 0x62D6,
    0x9339, 0x8318, 0xB37B, 0xA35A, 0xD3BD, 0xC39C, 0xF3FF, 0xE3DE,
    0x2462, 0x3443, 0x0420, 0x1401, 0x64E6, 0x74C7, 0x44A4, 0x5485,
    0xA56A, 0xB54B, 0x8528, 0x9509, 0xE5EE, 0xF5CF, 0xC5AC, 0xD58D,
    0x3653, 0x2672, 0x1611, 0x0630, 0x76D7, 0x66F6, 0x5695, 0x46B4,
    0xB75B, 0xA77A, 0x9719, 0x8738, 0xF7DF, 0xE7FE, 0xD79D, 0xC7BC,
    0x48C4, 0x58E5, 0x6886, 0x78A7, 0x0840, 0x1861, 0x2802, 0x3823,
    0xC9CC, 0xD9ED, 0xE98E, 0xF9AF, 0x8948, 0x9969, 0xA90A, 0xB92B,
    0x5AF5, 0x4AD4, 0x7AB7, 0x6A96, 0x1A71, 0x0A50, 0x3A33, 0x2A12,
    0xDBFD, 0xCBDC, 0xFBBF, 0xEB9E, 0x9B79, 0x8B58, 0xBB3B, 0xAB1A,
    0x6CA6, 0x7C87, 0x4CE4, 0x5CC5, 0x2C22, 0x3C03, 0x0C60, 0x1C41,
    0xEDAE, 0xFD8F, 0xCDEC, 0xDDCD, 0xAD2A, 0xBD0B, 0x8D68, 0x9D49,
    0x7E97, 0x6EB6, 0x5ED5, 0x4EF4, 0x3E13, 0x2E32, 0x1E51, 0x0E70,
    0xFF9F, 0xEFBE, 0xDFDD, 0xCFFC, 0xBF1B, 0xAF3A, 0x9F59, 0x8F78,
    0x9188, 0x81A9, 0xB1CA, 0xA1EB, 0xD10C, 0xC12D, 0xF14E, 0xE16F,
    0x1080, 0x00A1, 0x30C2, 0x20E3, 0x5004, 0x4025, 0x7046, 0x6067,
    0x83B9, 0x9398, 0xA3FB, 0xB3DA, 0xC33D, 0xD31C, 0xE37F, 0xF35E,
    0x02B1, 0x1290, 0x22F3, 0x32D2, 0x4235, 0x5214, 0x6277, 0x7256,
    0xB5EA, 0xA5CB, 0x95A8, 0x8589, 0xF56E, 0xE54F, 0xD52C, 0xC50D,
    0x34E2, 0x24C3, 0x14A0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
    0xA7DB, 0xB7FA, 0x8799, 0x97B8, 0xE75F, 0xF77E, 0xC71D, 0xD73C,
    0x26D3, 0x36F2, 0x0691, 0x16B0, 0x6657, 0x7676, 0x4615, 0x5634,
    0xD94C, 0xC96D, 0xF90E, 0xE92F, 0x99C8, 0x89E9, 0xB98A, 0xA9AB,
    0x5844, 0x4865, 0x7806, 0x6827, 0x18C0, 0x08E1, 0x3882, 0x28A3,
    0xCB7D, 0xDB5C, 0xEB3F, 0xFB1E, 0x8BF9, 0x9BD8, 0xABBB, 0xBB9A,
    0x4A75, 0x5A54, 0x6A37, 0x7A16, 0x0AF1, 0x1AD0, 0x2AB3, 0x3A92,
    0xFD2E, 0xED0F, 0xDD6C, 0xCD4D, 0xBDAA, 0xAD8B, 0x9DE8, 0x8DC9,
    0x7C26, 0x6C07, 0x5C64, 0x4C45, 0x3CA2, 0x2C83, 0x1CE0, 0x0CC1,
    0xEF1F, 0xFF3E, 0xCF5D, 0xDF7C, 0xAF9B, 0xBFBA, 0x8FD9, 0x9FF8,
    0x6E17, 0x7E36, 0x4E55, 0x5E74, 0x2E93, 0x3EB2, 0x0ED1, 0x1EF0
};


uint8_t do_crc8(uint8_t *pdata, int32_t len)
{
	uint8_t crc8 = 0;

	for( ;len > 0;len--)
	{
	   crc8 = crc8_table[crc8^*pdata];
	   pdata++;
	}

	return crc8;
}

uint16_t do_ccitt_crc16(uint8_t *pdata, int32_t len)
{
    uint16_t crc_reg = 0;

    while (len--)
        crc_reg = crc16_ccitt_table[(crc_reg >> 8 ^ *pdata++) & 0xFF] ^ (crc_reg << 8);

    return crc_reg;
}

发布了93 篇原创文章 · 获赞 13 · 访问量 1万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览