1 From efc3e6f5d29f87a433b42f15a0b87e04b7cd498d Mon Sep 17 00:00:00 2001
2 From: Weijie Gao <weijie.gao@mediatek.com>
3 Date: Wed, 3 Mar 2021 10:11:32 +0800
4 Subject: [PATCH 38/71] env: add support for generic MTD device
6 Add an env driver for generic MTD device.
8 Signed-off-by: Weijie Gao <weijie.gao@mediatek.com>
11 env/Kconfig | 37 +++++-
14 env/mtd.c | 256 +++++++++++++++++++++++++++++++++++++++++
15 include/env_internal.h | 1 +
17 7 files changed, 299 insertions(+), 3 deletions(-)
18 create mode 100644 env/mtd.c
22 @@ -61,7 +61,7 @@ config ENV_IS_DEFAULT
23 !ENV_IS_IN_MMC && !ENV_IS_IN_NAND && \
24 !ENV_IS_IN_NVRAM && !ENV_IS_IN_ONENAND && \
25 !ENV_IS_IN_REMOTE && !ENV_IS_IN_SPI_FLASH && \
27 + !ENV_IS_IN_UBI && !ENV_IS_IN_MTD
31 @@ -254,6 +254,27 @@ config ENV_IS_IN_MMC
32 offset: "u-boot,mmc-env-offset", "u-boot,mmc-env-offset-redundant".
33 CONFIG_ENV_OFFSET and CONFIG_ENV_OFFSET_REDUND are not used.
36 + bool "Environment in a MTD device"
37 + depends on !CHAIN_OF_TRUST
40 + Define this if you have a MTD device which you want to use for
43 + - CONFIG_ENV_MTD_NAME:
44 + - CONFIG_ENV_OFFSET:
47 + These three #defines specify the MTD device where the environment
48 + is stored, offset and size of the environment area within the MTD
49 + device. CONFIG_ENV_OFFSET must be aligned to an erase block boundary.
51 + - CONFIG_ENV_SIZE_REDUND:
53 + This #define specify the maximum size allowed for read/write/erase
54 + with skipped bad blocks starting from ENV_OFFSET.
57 bool "Environment in a NAND device"
58 depends on !CHAIN_OF_TRUST
59 @@ -561,10 +582,16 @@ config ENV_ADDR_REDUND
60 Offset from the start of the device (or partition) of the redundant
64 + string "Name of the MTD device storing the environment"
65 + depends on ENV_IS_IN_MTD
67 + Name of the MTD device that stores the environment
70 hex "Environment offset"
71 depends on ENV_IS_IN_EEPROM || ENV_IS_IN_MMC || ENV_IS_IN_NAND || \
73 + ENV_IS_IN_SPI_FLASH || ENV_IS_IN_MTD
74 default 0x3f8000 if ARCH_ROCKCHIP && ENV_IS_IN_MMC
75 default 0x140000 if ARCH_ROCKCHIP && ENV_IS_IN_SPI_FLASH
76 default 0xF0000 if ARCH_SUNXI
77 @@ -622,6 +649,12 @@ config ENV_SECT_SIZE
79 Size of the sector containing the environment.
81 +config ENV_SIZE_REDUND
82 + hex "Redundant environment size"
83 + depends on ENV_IS_IN_MTD
85 + The maximum size allowed for read/write/erase with skipped bad blocks.
88 string "UBI partition name"
89 depends on ENV_IS_IN_UBI
92 @@ -24,6 +24,7 @@ obj-$(CONFIG_$(SPL_TPL_)ENV_IS_NOWHERE)
93 obj-$(CONFIG_$(SPL_TPL_)ENV_IS_IN_MMC) += mmc.o
94 obj-$(CONFIG_$(SPL_TPL_)ENV_IS_IN_FAT) += fat.o
95 obj-$(CONFIG_$(SPL_TPL_)ENV_IS_IN_EXT4) += ext4.o
96 +obj-$(CONFIG_$(SPL_TPL_)ENV_IS_IN_MTD) += mtd.o
97 obj-$(CONFIG_$(SPL_TPL_)ENV_IS_IN_NAND) += nand.o
98 obj-$(CONFIG_$(SPL_TPL_)ENV_IS_IN_SPI_FLASH) += sf.o
99 obj-$(CONFIG_$(SPL_TPL_)ENV_IS_IN_FLASH) += flash.o
102 @@ -46,6 +46,9 @@ static enum env_location env_locations[]
103 #ifdef CONFIG_ENV_IS_IN_MMC
106 +#ifdef CONFIG_ENV_IS_IN_MTD
109 #ifdef CONFIG_ENV_IS_IN_NAND
115 +/* SPDX-License-Identifier: GPL-2.0 */
117 + * Copyright (C) 2021 MediaTek Inc. All Rights Reserved.
119 + * Author: Weijie Gao <weijie.gao@mediatek.com>
122 +#include <command.h>
124 +#include <env_internal.h>
126 +#include <linux/kernel.h>
127 +#include <linux/stddef.h>
128 +#include <linux/types.h>
129 +#include <linux/mtd/mtd.h>
131 +#include <memalign.h>
135 +#if CONFIG_ENV_SIZE_REDUND < CONFIG_ENV_SIZE
136 +#undef CONFIG_ENV_SIZE_REDUND
137 +#define CONFIG_ENV_SIZE_REDUND CONFIG_ENV_SIZE
140 +#if defined(ENV_IS_EMBEDDED)
141 +env_t *env_ptr = &environment;
142 +#else /* ! ENV_IS_EMBEDDED */
144 +#endif /* ENV_IS_EMBEDDED */
146 +DECLARE_GLOBAL_DATA_PTR;
148 +static int env_mtd_init(void)
150 +#if defined(ENV_IS_EMBEDDED)
151 + int crc1_ok = 0, crc2_ok = 0;
154 + tmp_env1 = env_ptr;
155 + crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc;
157 + if (!crc1_ok && !crc2_ok) {
159 + gd->env_valid = ENV_INVALID;
162 + } else if (crc1_ok && !crc2_ok) {
163 + gd->env_valid = ENV_VALID;
166 + if (gd->env_valid == ENV_VALID)
167 + env_ptr = tmp_env1;
169 + gd->env_addr = (ulong)env_ptr->data;
171 +#else /* ENV_IS_EMBEDDED */
172 + gd->env_addr = (ulong)&default_environment[0];
173 + gd->env_valid = ENV_VALID;
174 +#endif /* ENV_IS_EMBEDDED */
179 +static struct mtd_info *env_mtd_get_dev(void)
181 + struct mtd_info *mtd;
183 + mtd_probe_devices();
185 + mtd = get_mtd_device_nm(CONFIG_ENV_MTD_NAME);
186 + if (IS_ERR(mtd) || !mtd) {
187 + printf("MTD device '%s' not found\n", CONFIG_ENV_MTD_NAME);
194 +static inline bool mtd_addr_is_block_aligned(struct mtd_info *mtd, u64 addr)
196 + return (addr & mtd->erasesize_mask) == 0;
199 +static int mtd_io_skip_bad(struct mtd_info *mtd, bool read, loff_t offset,
200 + size_t length, size_t redund, u8 *buffer)
202 + struct mtd_oob_ops io_op = {};
203 + size_t remaining = length;
207 + io_op.mode = MTD_OPS_PLACE_OOB;
208 + io_op.len = mtd->writesize;
209 + io_op.datbuf = (void *)buffer;
211 + /* Search for the first good block after the given offset */
213 + end = (off + redund) | (mtd->erasesize - 1);
214 + while (mtd_block_isbad(mtd, off) && off < end)
215 + off += mtd->erasesize;
217 + /* Reached end position */
221 + /* Loop over the pages to do the actual read/write */
222 + while (remaining) {
223 + /* Skip the block if it is bad */
224 + if (mtd_addr_is_block_aligned(mtd, off) &&
225 + mtd_block_isbad(mtd, off)) {
226 + off += mtd->erasesize;
231 + ret = mtd_read_oob(mtd, off, &io_op);
233 + ret = mtd_write_oob(mtd, off, &io_op);
236 + printf("Failure while %s at offset 0x%llx\n",
237 + read ? "reading" : "writing", off);
241 + off += io_op.retlen;
242 + remaining -= io_op.retlen;
243 + io_op.datbuf += io_op.retlen;
244 + io_op.oobbuf += io_op.oobretlen;
246 + /* Reached end position */
254 +#ifdef CONFIG_CMD_SAVEENV
255 +static int mtd_erase_skip_bad(struct mtd_info *mtd, loff_t offset,
256 + size_t length, size_t redund)
258 + struct erase_info erase_op = {};
259 + loff_t end = (offset + redund) | (mtd->erasesize - 1);
262 + erase_op.mtd = mtd;
263 + erase_op.addr = offset;
264 + erase_op.len = length;
266 + while (erase_op.len) {
267 + ret = mtd_erase(mtd, &erase_op);
269 + /* Abort if its not a bad block error */
273 + printf("Skipping bad block at 0x%08llx\n", erase_op.fail_addr);
275 + /* Skip bad block and continue behind it */
276 + erase_op.len -= erase_op.fail_addr - erase_op.addr;
277 + erase_op.len -= mtd->erasesize;
278 + erase_op.addr = erase_op.fail_addr + mtd->erasesize;
280 + /* Reached end position */
281 + if (erase_op.addr >= end)
288 +static int env_mtd_save(void)
290 + ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
291 + struct mtd_info *mtd;
294 + ret = env_export(env_new);
298 + mtd = env_mtd_get_dev();
302 + printf("Erasing on MTD device '%s'... ", mtd->name);
304 + ret = mtd_erase_skip_bad(mtd, CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE,
305 + CONFIG_ENV_SIZE_REDUND);
307 + puts(ret ? "FAILED\n" : "OK\n");
310 + put_mtd_device(mtd);
314 + printf("Writing to MTD device '%s'... ", mtd->name);
316 + ret = mtd_io_skip_bad(mtd, false, CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE,
317 + CONFIG_ENV_SIZE_REDUND, (u8 *)env_new);
319 + puts(ret ? "FAILED\n" : "OK\n");
321 + put_mtd_device(mtd);
325 +#endif /* CONFIG_CMD_SAVEENV */
327 +static int readenv(size_t offset, u_char *buf)
329 + struct mtd_info *mtd;
332 + mtd = env_mtd_get_dev();
336 + ret = mtd_io_skip_bad(mtd, true, offset, CONFIG_ENV_SIZE,
337 + CONFIG_ENV_SIZE_REDUND, buf);
339 + put_mtd_device(mtd);
344 +static int env_mtd_load(void)
346 +#if !defined(ENV_IS_EMBEDDED)
347 + ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
350 + ret = readenv(CONFIG_ENV_OFFSET, (u_char *)buf);
352 + env_set_default("readenv() failed", 0);
356 + return env_import(buf, 1, H_EXTERNAL);
357 +#endif /* ! ENV_IS_EMBEDDED */
362 +U_BOOT_ENV_LOCATION(mtd) = {
363 + .location = ENVL_MTD,
365 + .load = env_mtd_load,
366 +#if defined(CONFIG_CMD_SAVEENV)
367 + .save = env_save_ptr(env_mtd_save),
369 + .init = env_mtd_init,
371 --- a/include/env_internal.h
372 +++ b/include/env_internal.h
373 @@ -109,6 +109,7 @@ enum env_location {
383 @@ -37,6 +37,7 @@ subdir-$(HOST_TOOLS_ALL) += gdb
384 ENVCRC-$(CONFIG_ENV_IS_IN_EEPROM) = y
385 ENVCRC-$(CONFIG_ENV_IS_IN_FLASH) = y
386 ENVCRC-$(CONFIG_ENV_IS_IN_ONENAND) = y
387 +ENVCRC-$(CONFIG_ENV_IS_IN_MTD) = y
388 ENVCRC-$(CONFIG_ENV_IS_IN_NAND) = y
389 ENVCRC-$(CONFIG_ENV_IS_IN_NVRAM) = y
390 ENVCRC-$(CONFIG_ENV_IS_IN_SPI_FLASH) = y