nand_write_skip_bad函数解析

本文介绍了一种用于NAND闪存的写入方法,该方法能够绕过标记为坏块的部分,并继续将数据写入下一个可用块,确保即使在存在坏块的情况下也能完成数据写入。
/**
 * nand_write_skip_bad:
 *
 * Write image to NAND flash.
 * Blocks that are marked bad are skipped and the is written to the next
 * block instead as long as the image is short enough to fit even after
 * skipping the bad blocks.  Due to bad blocks we may not be able to
 * perform the requested write.  In the case where the write would
 * extend beyond the end of the NAND device, both length and actual (if
 * not NULL) are set to 0.  In the case where the write would extend
 * beyond the limit we are passed, length is set to 0 and actual is set
 * to the required length.
 *
 * @param nand  	NAND device
 * @param offset	offset in flash
 * @param length	buffer length
 * @param actual	set to size required to write length worth of
 *			buffer or 0 on error, if not NULL
 * @param lim		maximum size that actual may be in order to not
 *			exceed the buffer
 * @param buffer        buffer to read from
 * @param flags		flags modifying the behaviour of the write to NAND
 * @return		0 in case of success
 */
int nand_write_skip_bad(nand_info_t *nand, loff_t offset, size_t *length,
		size_t *actual, loff_t lim, u_char *buffer, int flags)
{
	int rval = 0, blocksize;
	size_t left_to_write = *length;
	size_t used_for_write = 0;
	u_char *p_buffer = buffer;
	int need_skip;

	if (actual)
		*actual = 0;

	blocksize = nand->erasesize;

	/*
	 * nand_write() handles unaligned, partial page writes.
	 *
	 * We allow length to be unaligned, for convenience in
	 * using the $filesize variable.
	 *
	 * However, starting at an unaligned offset makes the
	 * semantics of bad block skipping ambiguous (really,
	 * you should only start a block skipping access at a
	 * partition boundary).  So don't try to handle that.
	 */
	if ((offset & (nand->writesize - 1)) != 0) {	//-- 页不对齐,出错误返回
		printf("Attempt to write non page-aligned data\n");
		*length = 0;
		return -EINVAL;
	}

	need_skip = check_skip_len(nand, offset, *length, &used_for_write);//-- 计算含坏块后的写总长度,用used_for_write返回

	if (actual)
		*actual = used_for_write;

	if (need_skip < 0) {//-- 说明要写的区域超过的设备容量
		printf("Attempt to write outside the flash area\n");
		*length = 0;
		return -EINVAL;
	}

	if (used_for_write > lim) {
		puts("Size of write exceeds partition or device limit\n");
		*length = 0;
		return -EFBIG;
	}

	if (!need_skip && !(flags & WITH_DROP_FFS)) {//-- 没有坏块并且flag不是WITH_DROP_FFS参数,直接按length长度写入
		rval = nand_write(nand, offset, length, buffer);

		if ((flags & WITH_WR_VERIFY) && !rval)
			rval = nand_verify(nand, offset, *length, buffer);

		if (rval == 0)
			return 0;

		*length = 0;
		printf("NAND write to offset %llx failed %d\n",
			offset, rval);
		return rval;
	}

	while (left_to_write > 0) {
		size_t block_offset = offset & (nand->erasesize - 1);	//-- 获取块内偏移,例如一个page 2048个字节,对于第2个page来说,在该块内该值为4096
		size_t write_size, truncated_write_size;

		WATCHDOG_RESET();

		//-- offset & ~(nand->erasesize - 1)算出来的值是要写入的page所在的block的第一个page的第一个字节。比如一个block有64个page,一个page有2048个字节,如果要
		//-- 要写第10个block的第2个page,那么该值为10 * 64 * 2048.此处应该就是坏块标记,是按照该块的第一个page的第一个字节是否为0xff来判断是否为坏块。
		if (nand_block_isbad(nand, offset & ~(nand->erasesize - 1))) {//-- 跳过一个坏块
			printf("Skip bad block 0x%08llx\n",
				offset & ~(nand->erasesize - 1));
			offset += nand->erasesize - block_offset;
			continue;
		}

		if (left_to_write < (blocksize - block_offset))
			write_size = left_to_write;
		else
			write_size = blocksize - block_offset;

		truncated_write_size = write_size;
#ifdef CONFIG_CMD_NAND_TRIMFFS
		if (flags & WITH_DROP_FFS)
			truncated_write_size = drop_ffs(nand, p_buffer,
					&write_size);
#endif

		rval = nand_write(nand, offset, &truncated_write_size,
				p_buffer);

		if ((flags & WITH_WR_VERIFY) && !rval)
			rval = nand_verify(nand, offset,//-- 将写入的数据再读一遍验证写入是否正确
				truncated_write_size, p_buffer);

		offset += write_size;
		p_buffer += write_size;

		if (rval != 0) {
			printf("NAND write to offset %llx failed %d\n",
				offset, rval);
			*length -= left_to_write;
			return rval;
		}

		left_to_write -= write_size;
	}

	return 0;
}

#ifdef CMD_SAVEENV /* The legacy NAND code saved the environment in the first NAND device i.e., nand_dev_desc + 0. This is also the behaviour using the new NAND code. */ static int writeenv(size_t offset, u_char *buf) { size_t end = offset + CONFIG_ENV_RANGE; size_t amount_saved = 0; size_t blocksize, len; struct mtd_info *mtd; u_char *char_ptr; mtd = get_nand_dev_by_index(0); if (!mtd) return 1; blocksize = mtd->erasesize; len = min(blocksize, (size_t)CONFIG_ENV_SIZE); while (amount_saved < CONFIG_ENV_SIZE && offset < end) { if (nand_block_isbad(mtd, offset)) { offset += blocksize; } else { char_ptr = &buf[amount_saved]; if (nand_write(mtd, offset, &len, char_ptr)) return 1; offset += blocksize; amount_saved += len; } } if (amount_saved != CONFIG_ENV_SIZE) return 1; return 0; } struct nand_env_location { const char *name; const nand_erase_options_t erase_opts; }; static int erase_and_write_env(const struct nand_env_location *location, u_char *env_new) { struct mtd_info *mtd; int ret = 0; mtd = get_nand_dev_by_index(0); if (!mtd) return 1; printf("Erasing %s...\n", location->name); if (nand_erase_opts(mtd, &location->erase_opts)) return 1; printf("Writing to %s... ", location->name); ret = writeenv(location->erase_opts.offset, env_new); puts(ret ? "FAILED!\n" : "OK\n"); return ret; } static int env_nand_save(void) { int ret = 0; ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1); int env_idx = 0; static const struct nand_env_location location[] = { { .name = “NAND”, .erase_opts = { .length = CONFIG_ENV_RANGE, .offset = CONFIG_ENV_OFFSET, }, }, #ifdef CONFIG_ENV_OFFSET_REDUND { .name = “redundant NAND”, .erase_opts = { .length = CONFIG_ENV_RANGE, .offset = CONFIG_ENV_OFFSET_REDUND, }, }, #endif }; if (CONFIG_ENV_RANGE < CONFIG_ENV_SIZE) return 1; ret = env_export(env_new); if (ret) return ret; #ifdef CONFIG_ENV_OFFSET_REDUND env_idx = (gd->env_valid == ENV_VALID); #endif ret = erase_and_write_env(&location[env_idx], (u_char *)env_new); #ifdef CONFIG_ENV_OFFSET_REDUND if (!ret) { /* preset other copy for next write */ gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND; return ret; } env_idx = (env_idx + 1) & 1; ret = erase_and_write_env(&location[env_idx], (u_char *)env_new); if (!ret) printf("Warning: primary env write failed," " redundancy is lost!\n"); #endif return ret; } #endif /* CMD_SAVEENV */ #if defined(CONFIG_SPL_BUILD) static int readenv(size_t offset, u_char *buf) { return nand_spl_load_image(offset, CONFIG_ENV_SIZE, buf); } #else static int readenv(size_t offset, u_char *buf) { size_t end = offset + CONFIG_ENV_RANGE; size_t amount_loaded = 0; size_t blocksize, len; struct mtd_info *mtd; u_char *char_ptr; mtd = get_nand_dev_by_index(0); if (!mtd) return 1; blocksize = mtd->erasesize; len = min(blocksize, (size_t)CONFIG_ENV_SIZE); while (amount_loaded < CONFIG_ENV_SIZE && offset < end) { if (nand_block_isbad(mtd, offset)) { offset += blocksize; } else { char_ptr = &buf[amount_loaded]; if (nand_read_skip_bad(mtd, offset, &len, NULL, mtd->size, char_ptr)) return 1; offset += blocksize; amount_loaded += len; } } if (amount_loaded != CONFIG_ENV_SIZE) return 1; return 0; } #endif /* #if defined(CONFIG_SPL_BUILD) */ #ifdef CONFIG_ENV_OFFSET_OOB int get_nand_env_oob(struct mtd_info *mtd, unsigned long *result) { struct mtd_oob_ops ops; uint32_t oob_buf[ENV_OFFSET_SIZE / sizeof(uint32_t)]; int ret; ops.datbuf = NULL; ops.mode = MTD_OOB_AUTO; ops.ooboffs = 0; ops.ooblen = ENV_OFFSET_SIZE; ops.oobbuf = (void *)oob_buf; ret = mtd->read_oob(mtd, ENV_OFFSET_SIZE, &ops); if (ret) { printf("error reading OOB block 0\n"); return ret; } if (oob_buf[0] == ENV_OOB_MARKER) { *result = ovoid ob_buf[1] * mtd->erasesize; } else if (oob_buf[0] == ENV_OOB_MARKER_OLD) { *result = oob_buf[1]; } else { printf("No dynamic environment marker in OOB block 0\n"); return -ENOENT; } return 0; } #endif #ifdef CONFIG_ENV_OFFSET_REDUND static int env_nand_load(void) { #if defined(ENV_IS_EMBEDDED) return 0; #else int read1_fail, read2_fail; env_t *tmp_env1, *tmp_env2; int ret = 0; tmp_env1 = (env_t *)malloc(CONFIG_ENV_SIZE); tmp_env2 = (env_t *)malloc(CONFIG_ENV_SIZE); if (tmp_env1 == NULL || tmp_env2 == NULL) { puts("Can't allocate buffers for environment\n"); env_set_default("malloc() failed", 0); ret = -EIO; goto done; } read1_fail = readenv(CONFIG_ENV_OFFSET, (u_char *) tmp_env1); read2_fail = readenv(CONFIG_ENV_OFFSET_REDUND, (u_char *) tmp_env2); ret = env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2, read2_fail, H_EXTERNAL); done: free(tmp_env1); free(tmp_env2); return ret; #endif /* ! ENV_IS_EMBEDDED / } #else / ! CONFIG_ENV_OFFSET_REDUND / / The legacy NAND code saved the environment in the first NAND device i.e., nand_dev_desc + 0. This is also the behaviour using the new NAND code. */ static int env_nand_load(void) { #if !defined(ENV_IS_EMBEDDED) int ret; ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE); #if defined(CONFIG_ENV_OFFSET_OOB) struct mtd_info mtd = get_nand_dev_by_index(0); / * If unable to read environment offset from NAND OOB then fall through * to the normal environment reading code below */ if (mtd && !get_nand_env_oob(mtd, &nand_env_oob_offset)) { printf(“Found Environment offset in OOB…\n”); } else { env_set_default(“no env offset in OOB”, 0); return; } #endif ret = readenv(CONFIG_ENV_OFFSET, (u_char *)buf); if (ret) { env_set_default("readenv() failed", 0); return -EIO; } return env_import(buf, 1, H_EXTERNAL); #endif /* ! ENV_IS_EMBEDDED */ return 0; } #endif /* CONFIG_ENV_OFFSET_REDUND */ U_BOOT_ENV_LOCATION(nand) = { .location = ENVL_NAND, ENV_NAME(“NAND”) .load = env_nand_load, #if defined(CMD_SAVEENV) .save = env_save_ptr(env_nand_save), #endif .init = env_nand_init, }; 这段代码是没改过的boot源码,详细解释一下代码的功能
最新发布
09-03
当前我这边的env/nand.c中只有: struct nand_env_location { const char *name; const nand_erase_options_t erase_opts; }; static int erase_and_write_env(const struct nand_env_location *location, u_char *env_new) { struct mtd_info *mtd; int ret = 0; mtd = get_nand_dev_by_index(0); if (!mtd) return 1; printf("Erasing %s...\n", location->name); if (nand_erase_opts(mtd, &location->erase_opts)) return 1; printf("Writing to %s... ", location->name); ret = writeenv(location->erase_opts.offset, env_new); puts(ret ? "FAILED!\n" : "OK\n"); return ret; } static int env_nand_save(void) { int ret = 0; ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1); int env_idx = 0; static const struct nand_env_location location[] = { { .name = "NAND", .erase_opts = { .length = CONFIG_ENV_RANGE, .offset = CONFIG_ENV_OFFSET, }, }, #ifdef CONFIG_ENV_OFFSET_REDUND { .name = "redundant NAND", .erase_opts = { .length = CONFIG_ENV_RANGE, .offset = CONFIG_ENV_OFFSET_REDUND, }, }, #endif }; if (CONFIG_ENV_RANGE < CONFIG_ENV_SIZE) return 1; ret = env_export(env_new); if (ret) return ret; #ifdef CONFIG_ENV_OFFSET_REDUND env_idx = (gd->env_valid == ENV_VALID); #endif ret = erase_and_write_env(&location[env_idx], (u_char *)env_new); #ifdef CONFIG_ENV_OFFSET_REDUND if (!ret) { /* preset other copy for next write */ gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND; return ret; } env_idx = (env_idx + 1) & 1; ret = erase_and_write_env(&location[env_idx], (u_char *)env_new); if (!ret) printf("Warning: primary env write failed," " redundancy is lost!\n"); #endif return ret; } #endif /* CMD_SAVEENV */ #if defined(CONFIG_SPL_BUILD) static int readenv(size_t offset, u_char *buf) { return nand_spl_load_image(offset, CONFIG_ENV_SIZE, buf); } #else static int readenv(size_t offset, u_char *buf) { size_t end = offset + CONFIG_ENV_RANGE; size_t amount_loaded = 0; size_t blocksize, len; struct mtd_info *mtd; u_char *char_ptr; mtd = get_nand_dev_by_index(0); if (!mtd) return 1; blocksize = mtd->erasesize; len = min(blocksize, (size_t)CONFIG_ENV_SIZE); while (amount_loaded < CONFIG_ENV_SIZE && offset < end) { if (nand_block_isbad(mtd, offset)) { offset += blocksize; } else { char_ptr = &buf[amount_loaded]; if (nand_read_skip_bad(mtd, offset, &len, NULL, mtd->size, char_ptr)) return 1; offset += blocksize; amount_loaded += len; } } if (amount_loaded != CONFIG_ENV_SIZE) return 1; return 0; } #endif /* #if defined(CONFIG_SPL_BUILD) */ #ifdef CONFIG_ENV_OFFSET_OOB int get_nand_env_oob(struct mtd_info *mtd, unsigned long *result) { struct mtd_oob_ops ops; uint32_t oob_buf[ENV_OFFSET_SIZE / sizeof(uint32_t)]; int ret; ops.datbuf = NULL; ops.mode = MTD_OOB_AUTO; ops.ooboffs = 0; ops.ooblen = ENV_OFFSET_SIZE; ops.oobbuf = (void *)oob_buf; ret = mtd->read_oob(mtd, ENV_OFFSET_SIZE, &ops); if (ret) { printf("error reading OOB block 0\n"); return ret; } if (oob_buf[0] == ENV_OOB_MARKER) { *result = ovoid ob_buf[1] * mtd->erasesize; } else if (oob_buf[0] == ENV_OOB_MARKER_OLD) { *result = oob_buf[1]; } else { printf("No dynamic environment marker in OOB block 0\n"); return -ENOENT; } return 0; } #endif #ifdef CONFIG_ENV_OFFSET_REDUND static int env_nand_load(void) { #if defined(ENV_IS_EMBEDDED) return 0; #else int read1_fail, read2_fail; env_t *tmp_env1, *tmp_env2; int ret = 0; tmp_env1 = (env_t *)malloc(CONFIG_ENV_SIZE); tmp_env2 = (env_t *)malloc(CONFIG_ENV_SIZE); if (tmp_env1 == NULL || tmp_env2 == NULL) { puts("Can't allocate buffers for environment\n"); env_set_default("malloc() failed", 0); ret = -EIO; goto done; } read1_fail = readenv(CONFIG_ENV_OFFSET, (u_char *) tmp_env1); read2_fail = readenv(CONFIG_ENV_OFFSET_REDUND, (u_char *) tmp_env2); ret = env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2, read2_fail, H_EXTERNAL); done: free(tmp_env1); free(tmp_env2); return ret; #endif /* ! ENV_IS_EMBEDDED */ } #else /* ! CONFIG_ENV_OFFSET_REDUND */ /* * The legacy NAND code saved the environment in the first NAND * device i.e., nand_dev_desc + 0. This is also the behaviour using * the new NAND code. */ static int env_nand_load(void) { #if !defined(ENV_IS_EMBEDDED) int ret; ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE); #if defined(CONFIG_ENV_OFFSET_OOB) struct mtd_info *mtd = get_nand_dev_by_index(0); /* * If unable to read environment offset from NAND OOB then fall through * to the normal environment reading code below */ if (mtd && !get_nand_env_oob(mtd, &nand_env_oob_offset)) { printf("Found Environment offset in OOB..\n"); } else { env_set_default("no env offset in OOB", 0); return; } #endif ret = readenv(CONFIG_ENV_OFFSET, (u_char *)buf); if (ret) { env_set_default("readenv() failed", 0); return -EIO; } return env_import(buf, 1, H_EXTERNAL); #endif /* ! ENV_IS_EMBEDDED */ return 0; } #endif /* CONFIG_ENV_OFFSET_REDUND */ U_BOOT_ENV_LOCATION(nand) = { .location = ENVL_NAND, ENV_NAME("NAND") .load = env_nand_load, #if defined(CMD_SAVEENV) .save = env_save_ptr(env_nand_save), #endif .init = env_nand_init, };
09-03
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黑猫学长呀

有帮助到你就来打个赏呗

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值