应用层:(使用mtd ioctl)
0.获取mtd基本信息
struct mtd_info_user {
unsigned char type; //flash类型
unsigned int flags;
unsigned int size; //分区大小
unsigned int erasesize; //块大小
unsigned int writesize; //页数据大小
unsigned int oobsize; //OOB大小
unsigned int ecctype; //ECC类型
unsigned int eccsize; //ECC大小
};
fd = open("/dev/mtd", O_RDWR);
ioctl(fd, MEMGETINFO, &mtd_info);
1.读页数据
2.写页数据
unsigned char pagedate[2048] = {...};
write(fd, pagedate, mtd_info.writesize);
3.读OOB
struct mtd_oob_buf {
unsigned int start;
unsigned int length;
unsigned char *ptr;
};
unsigned char oobbuf2[64];
OOB_INFO.start = page_num * (2048);
OOB_INFO.ptr = oobbuf2;
ioctl(fd, MEMREADOOB, &OOB_INFO);
4.写OOB
unsigned char oobbuf[64] = {...};
OOB_INFO.start = page_num * (2048);
OOB_INFO.ptr = oobbuf;
ioctl(fd, MEMWRITEOOB, &OOB_INFO);
1 #include <stdlib.h> 2 #include <unistd.h> 3 #include <fcntl.h> 4 #include <sys/stat.h> 5 #include <sys/ioctl.h> 6 #include <stdio.h> 7 #include <string.h> 8 #include <getopt.h> 9 #include <errno.h> 10 11 struct mtd_info_user { 12 unsigned char type; 13 unsigned int flags; 14 unsigned int size; // Total size of the MTD 15 unsigned int erasesize; 16 unsigned int writesize; // Size of OOB blocks (e.g. 512) 17 unsigned int oobsize; // Amount of OOB data per block (e.g. 16) 18 unsigned int ecctype; 19 unsigned int eccsize; 20 }; 21 struct mtd_oob_buf { 22 unsigned int start; 23 unsigned int length; 24 unsigned char *ptr; 25 }; 26 typedef long long __kernel_loff_t; 27 #define SLEEP_MILLI(milliseconds) usleep(1000*(milliseconds)) /* sellp milliseconds */ 28 #define CORE_UPG_ONCE_SLP (100) /* 一次写入FLASH SLEEP时间 MS */ 29 30 #define MEMGETINFO _IOR('M' , 1 , struct mtd_info_user) 31 #define MEMGETBADBLOCK _IOW('M' , 11, __kernel_loff_t) 32 #define MEMWRITEOOB _IOWR('M', 3 , struct mtd_oob_buf) 33 #define MEMREADOOB _IOWR('M', 4 , struct mtd_oob_buf) 34 35 static unsigned int next_good_eraseblock(int fd, struct mtd_info_user *meminfo, 36 unsigned block_offset) 37 { 38 loff_t offs; 39 40 while (1) 41 { 42 if (block_offset >= meminfo->size) 43 { 44 printf("next_good_eraseblock:not enough space in MTD device "); 45 return block_offset; 46 } 47 48 offs = block_offset; 49 if (ioctl(fd, MEMGETBADBLOCK, &offs) == 0) 50 { 51 return block_offset; 52 } 53 54 printf("Skipping bad block at 0x%08x ", block_offset); 55 block_offset += meminfo->erasesize; 56 } 57 } 58 int main(void) 59 { 60 int fd = -1; 61 int ret = 0; 62 int i = 0,k = 0; 63 64 int writesize = 0; 65 unsigned int erasesize = 0; 66 unsigned int blockSize = 0; 67 int wrt_cnt = 0; 68 int wrt_left = 0; 69 unsigned int size = 0; 70 unsigned int offset = 0; 71 72 struct mtd_info_user mtd_info; //MTD信息 73 struct mtd_oob_buf OOB_INFO ; //OOB操作时用 74 unsigned char oobbuf2[64]; //读取OOB时用的缓存 75 //一页(2048Byte)有效的yaffs2信息头 76 unsigned char pagedate[2048] = { 77 0x59,0x46,0x53,0x53,0x21,0x56,0x31,0x30,0x10,0x06,0x00,0x00,0x02,0x06,0x23,0x07,//Y F S S ! V 1 0 DLE ACK NUL NUL STX ACK # BEL 78 0x00,0x08,0x00,0x00,0x02,0x00,0x00,0x00,0x7e,0x00,0x0a,0x79,0x61,0x66,0x66,0x73,//NUL BS NUL NUL STX NUL NUL NUL ~ NUL LF y a f f s 79 0x32,0x20,0x66,0x69,0x6c,0x65,0x20,0x73,0x79,0x73,0x74,0x65,0x6d,0x3a,0x20,0x32,//2 (space) f i l e (space) s y s t e m : (space) 2 80 0x4b,0x20,0x34,0x62,0x69,0x74,0x20,0x0a,0x6d,0x6b,0x79,0x61,0x66,0x66,0x73,0x32,//K (space) 4 b i t (space) . m j y a f f s 2 81 0x69,0x6d,0x61,0x67,0x65,0x20,0x76,0x65,0x72,0x73,0x69,0x6f,0x6e,0x3a,0x20,0x4c,//i m a g e (space) v e r s i o n : (space) L 82 0x69,0x6e,0x75,0x78,0x2d,0x32,0x2e,0x36,0x2e,0x33,0x35,0x20,0x4e,0x61,0x6e,0x64,//i n u x - 2 . 6 . 3 5 (space) N a n d 83 0x20,0x43,0x6f,0x6e,0x74,0x72,0x6f,0x6c,0x6c,0x65,0x72,0x20,0x56,0x36,0x31,0x30,//(space) C o n t r o l l e r (space) V 6 1 0 84 0x2c,0x20,0x62,0x75,0x69,0x6c,0x64,0x20,0x74,0x69,0x6d,0x65,0x3a,0x20,0x57,0x65,//, (space) b u i l d (space) t i m e : (space) W e 85 0x64,0x20,0x4d,0x61,0x79,0x20,0x32,0x34,0x20,0x31,0x37,0x3a,0x32,0x30,0x3a,0x33,//d (space) M a y (space) 2 4 (space) 1 7 : 2 0 : 3 86 0x34,0x20,0x32,0x30,0x31,0x37,0x0a,0x2e,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//4 (space) 2 0 1 7 NUL NUL NUL NUL NUL NUL NUL NUL NUL NUL 87 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 88 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 89 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 90 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 91 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 92 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 93 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 94 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 95 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 96 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 97 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 98 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 99 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 100 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 101 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 102 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 103 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 104 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 105 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 106 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 107 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 108 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 109 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 110 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 111 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 112 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 113 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 114 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 115 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 116 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 117 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 118 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 119 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 120 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 121 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 122 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 123 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 124 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 125 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 126 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 127 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 128 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 129 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 130 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 131 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 132 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 133 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 134 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 135 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 136 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 137 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 138 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 139 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 140 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 141 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 142 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 143 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 144 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 145 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 146 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 147 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 148 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 149 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 150 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 151 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 152 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 153 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 154 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 155 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 156 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 157 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 158 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 159 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 160 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 161 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 162 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 163 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 164 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 165 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 166 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 167 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 168 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 169 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 170 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 171 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 172 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 173 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 174 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 175 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 176 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 177 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 178 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 179 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 180 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 181 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 182 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 183 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 184 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 185 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 186 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 187 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 188 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 189 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 190 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 191 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 192 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 193 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 194 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 195 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 196 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 197 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 198 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 199 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 200 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 201 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 202 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 203 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 204 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 205 }; 206 //一页(2048Byte)对应的有效的yaffs2 ecc值 207 unsigned char oobbuf[64] = { 208 0xff,0xff,0x00,0x10,0x00,0x00,0x01,0x01, 209 0x00,0x00,0x00,0x04,0x00,0x00,0x99,0x00, 210 0x00,0x00,0x3c,0x00,0x00,0x00,0x09,0x00, 211 0x00,0x00,0x09,0x00,0x00,0x00,0xff,0xff, 212 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, 213 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, 214 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, 215 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, 216 }; 217 //1.打开设备 218 fd = open("/dev/mtd4", O_RDWR); 219 if (fd < 0) 220 { 221 printf("nand_write:open "); 222 ret = -1; 223 return ret; 224 } 225 //2.获取存储信息 226 if (ioctl(fd, MEMGETINFO, &mtd_info) < 0) 227 { 228 printf("nand_write:MEMGETINFO "); 229 ret = -1; 230 return ret; 231 } 232 offset = 0 & ~(mtd_info.erasesize - 1);//数据在mtd中的偏移 233 writesize = mtd_info.writesize;//页大小 234 blockSize = mtd_info.erasesize;//块大小 235 size = mtd_info.size;//16*1024*1024 实例大小为16M 236 237 erasesize = mtd_info.erasesize;//擦除大小 238 wrt_cnt = size / writesize;//分区页数 239 wrt_left = size % writesize;//分区最后一页字节数 240 memset(oobbuf, 0xff, sizeof(oobbuf));//写OOB时缓存 241 memset(oobbuf2, 0xff, sizeof(oobbuf2));//读OOB时缓存 242 OOB_INFO.start = 0;//OOB页起始地址 243 OOB_INFO.length = mtd_info.oobsize;//OOB大小 244 /* 245 * 读所有页OOB 246 */ 247 for (i = 0; i < wrt_cnt; i++)//页数 248 { 249 if (!(offset % blockSize)) 250 { 251 offset = next_good_eraseblock(fd, &mtd_info, offset); 252 if (offset >= mtd_info.size) 253 { 254 printf("nand_write:MTD is full "); 255 ret = -1; 256 return ret; 257 } 258 } 259 OOB_INFO.start = offset; 260 OOB_INFO.ptr = oobbuf; 261 262 ioctl(fd, MEMREADOOB, &OOB_INFO); 263 for(k = 0; k < mtd_info.oobsize; k++) 264 printf("OOB_INFO[%d] = %#x ", k,*(OOB_INFO.ptr+k)); 265 offset += writesize; 266 if (0 == i % 25) // 每写100KB数据后暂停100ms,防止升级线程占用太多CPU 资源 267 { 268 // 每写100KB数据后暂停100ms,防止升级线程占用太多CPU 资源 269 SLEEP_MILLI(CORE_UPG_ONCE_SLP); 270 } 271 } 272 if (wrt_left > 0) 273 { 274 if (!(offset % blockSize)) 275 { 276 offset = next_good_eraseblock(fd, &mtd_info, offset); 277 if (offset >= mtd_info.size) 278 { 279 printf("nand_write:MTD is full "); 280 ret = -1; 281 return ret; 282 } 283 } 284 OOB_INFO.start = offset; 285 OOB_INFO.ptr = oobbuf; 286 ioctl(fd, MEMREADOOB, &OOB_INFO); 287 for(k = 0; k < mtd_info.oobsize; k++) 288 printf("OOB_INFO[%d] = %#x ", k,*(OOB_INFO.ptr+k)); 289 offset += writesize; 290 } 291 292 write(fd, pagedate, mtd_info.writesize); 293 294 /* 295 * 写指定页OOB 296 */ 297 offset = 0 * (2048); 298 if (!(offset % blockSize)) 299 { 300 offset = next_good_eraseblock(fd, &mtd_info, offset); 301 if (offset >= mtd_info.size) 302 { 303 printf("nand_write:MTD is full "); 304 ret = -1; 305 return ret; 306 } 307 } 308 OOB_INFO.start = offset; 309 OOB_INFO.ptr = oobbuf; 310 ioctl(fd, MEMWRITEOOB, &OOB_INFO); 311 312 /* 313 * 读指定页OOB 314 */ 315 OOB_INFO.start = 0 * (2048); 316 OOB_INFO.ptr = oobbuf2; 317 ioctl(fd, MEMREADOOB, &OOB_INFO); 318 for(k = 0; k < mtd_info.oobsize; k++) 319 printf("OOB_INFO[%d] = %#x ", k,*(OOB_INFO.ptr+k)); 320 return 0; 321 }