文件路径:busybox-1.20.2/miscutils/nandwrite.c
MTD 相关信息结构体
struct mtd_info_user {
__u8 type; // MTD 设备类型
__u32 flags; // MTD设备属性标志
__u32 size; // mtd设备的大小
__u32 erasesize; // MTD设备的擦除单元大小,对于 NandFlash来说就是 Block的大小
__u32 writesize; // MTD设备的读写单元大小,对于 NandFlash来说就是page 的大小
__u32 oobsize; // oob区域大小
__u64 padding; // 有效的oob区域大小
};
假如要将 mtd2 拷贝到 mtd3 分区中,使用的命令是:nandwrite /dev/mtd3 /dev/mtd2
int nandwrite_main(int argc UNUSED_PARAM, char **argv)
{
/* Buffer for OOB data */
unsigned char *oobbuf;
unsigned opts;
int fd;
ssize_t cnt;
unsigned mtdoffset, meminfo_writesize, blockstart, limit;
unsigned end_addr = ~0;
struct mtd_info_user meminfo;
struct mtd_oob_buf oob;
unsigned char *filebuf;
const char *opt_s = "0", *opt_f = "-", *opt_l;
if (IS_NANDDUMP) { // 从命令行获取参数
opt_complementary = "=1";
opts = getopt32(argv, "os:bf:l:", &opt_s, &opt_f, &opt_l);
} else { /* nandwrite */
opt_complementary = "-1:?2";
opts = getopt32(argv, "ps:", &opt_s);
}
argv += optind;
if (IS_NANDWRITE && argv[1]) // argv[1]为 /dev/mtd2
opt_f = argv[1];
if (!LONE_DASH(opt_f)) { // 判断输入的参数是否为文件,根据命令为文件添加权限
int tmp_fd = xopen(opt_f,
IS_NANDDUMP ? O_WRONLY | O_TRUNC | O_CREAT : O_RDONLY
);
xmove_fd(tmp_fd, IS_NANDDUMP ? STDOUT_FILENO : STDIN_FILENO); // 将文件内容放在标准输出或者标准输入中。
}
fd = xopen(argv[0], O_RDWR); // 打开文件,argv[0]为/dev/mtd3
xioctl(fd, MEMGETINFO, &meminfo); // 获取内存信息
mtdoffset = xstrtou(opt_s, 0); // 获取mtd偏移量,默认为0
if (IS_NANDDUMP && (opts & OPT_l)) {
unsigned length = xstrtou(opt_l, 0);
if (length < meminfo.size - mtdoffset)
end_addr = mtdoffset + length;
}
/* Pull it into a CPU register (hopefully) - smaller code that way */
meminfo_writesize = meminfo.writesize; // 获取每次写入内存的大小(一般为页大小)
if (mtdoffset & (meminfo_writesize - 1)) // 判断写入的地址是否页对齐
bb_error_msg_and_die("start address is not page aligned");
filebuf = xmalloc(meminfo_writesize); // 根据每次写入的大小分配buf和oob内存
oobbuf = xmalloc(meminfo.oobsize);
oob.start = 0; // 开始地址
oob.length = meminfo.oobsize; // oob大小
oob.ptr = oobbuf; // oob值
blockstart = mtdoffset & ~(meminfo.erasesize - 1); // 获得块起始地址
if (blockstart != mtdoffset) {
unsigned tmp;
/* mtdoffset is in the middle of an erase block, verify that
* this block is OK. Advance mtdoffset only if this block is
* bad.
*/
tmp = next_good_eraseblock(fd, &meminfo, blockstart);
if (tmp != blockstart) {
/* bad block(s), advance mtdoffset */
if (IS_NANDDUMP & !(opts & OPT_b)) {
int bad_len = MIN(tmp, end_addr) - mtdoffset;
dump_bad(&meminfo, bad_len, !(opts & OPT_o));
}
mtdoffset = tmp;
}
}
cnt = -1;
limit = MIN(meminfo.size, end_addr); // 获取写入总空间,meminfo.size为mtd总空间大小
while (mtdoffset < limit) { // 循环往mtd写入数值,直到超出mtd总空间大小
int input_fd = IS_NANDWRITE ? STDIN_FILENO : fd; // 若为IS_NANDWRITE指令,将输入input_fd指向标准输入,输出output_fd指向文件
int output_fd = IS_NANDWRITE ? fd : STDOUT_FILENO;
blockstart = mtdoffset & ~(meminfo.erasesize - 1); // 获得块起始地址
if (blockstart == mtdoffset) { // 若是对齐的,开始检测坏块
/* starting a new eraseblock */
mtdoffset = next_good_eraseblock(fd, &meminfo, blockstart); // 检测坏块,若检测到跳过,具体实现见下面一个函数
if (IS_NANDWRITE)
printf("Writing at 0x%08x\n", mtdoffset);
else if (mtdoffset > blockstart) {
int bad_len = MIN(mtdoffset, limit) - blockstart;
dump_bad(&meminfo, bad_len, !(opts & OPT_o));
}
if (mtdoffset >= limit) // 偏移量超出mtd总大小跳出
break;
}
xlseek(fd, mtdoffset, SEEK_SET); // 将读写位置移到文件开头
/* get some more data from input */
cnt = full_read(input_fd, filebuf, meminfo_writesize); // 从获取标准输入中获取数据,大小为内存写入的大小,数据保存在filebuf
if (cnt == 0) {
/* even with -p, we do not pad past the end of input
* (-p only zero-pads last incomplete page)
*/
break;
}
if (cnt < meminfo_writesize) { // 从标准输出中获取到数据的大小 若小于 写入的标准大小
if (IS_NANDDUMP)
bb_error_msg_and_die("short read");
if (!(opts & OPT_p))
bb_error_msg_and_die("input size is not rounded up to page size, "
"use -p to zero pad");
/* zero pad to end of write block */
memset(filebuf + cnt, 0, meminfo_writesize - cnt); // 在数据后面填充0
}
xwrite(output_fd, filebuf, meminfo_writesize); // 将filebuf数据拷贝到 mtd中
if (IS_NANDDUMP && !(opts & OPT_o)) {
/* Dump OOB data */
oob.start = mtdoffset;
xioctl(fd, MEMREADOOB, &oob);
xwrite(output_fd, oobbuf, meminfo.oobsize);
}
mtdoffset += meminfo_writesize; // 指向下个要写入的地址
if (cnt < meminfo_writesize) // 若本次获取数据的量小于写入的大小,则跳出
break;
}
if (IS_NANDWRITE && cnt != 0) { // 填满了整个MTD,但是我们在输入时达到EOF了吗
/* We filled entire MTD, but did we reach EOF on input? */
if (full_read(STDIN_FILENO, filebuf, meminfo_writesize) != 0) {
/* no */
bb_error_msg_and_die("not enough space in MTD device");
}
}
if (ENABLE_FEATURE_CLEAN_UP) {
free(filebuf);
close(fd);
}
return EXIT_SUCCESS;
}
标准输出输出定义
#define STDIN_FILENO 0 /* Standard input. */
#define STDOUT_FILENO 1 /* Standard output. */
#define STDERR_FILENO 2 /* Standard error output. */
检测坏块实现
static unsigned next_good_eraseblock(int fd, struct mtd_info_user *meminfo,
unsigned block_offset)
{
while (1) { // 循环检测,便于跳过坏块
loff_t offs;
if (block_offset >= meminfo->size) { // 1、传入的块偏移量大于等于mtd总大小
if (IS_NANDWRITE)
bb_error_msg_and_die("not enough space in MTD device");
return block_offset; /* let the caller exit */ // 返回
}
offs = block_offset;
if (xioctl(fd, MEMGETBADBLOCK, &offs) == 0) // 2、判断是否为坏块
return block_offset; // 若不是返回地址
/* ioctl returned 1 => "bad block" */
if (IS_NANDWRITE) // 若是坏块跳过,并指向下一块的地址检测
printf("Skipping bad block at 0x%08x\n", block_offset);
block_offset += meminfo->erasesize;
}
}