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 @@ -48,6 +48,7 @@ DECLARE_GLOBAL_DATA_PTR;
23 defined(CONFIG_ENV_IS_IN_MMC) || \
24 defined(CONFIG_ENV_IS_IN_FAT) || \
25 defined(CONFIG_ENV_IS_IN_EXT4) || \
26 + defined(CONFIG_ENV_IS_IN_MTD) || \
27 defined(CONFIG_ENV_IS_IN_NAND) || \
28 defined(CONFIG_ENV_IS_IN_NVRAM) || \
29 defined(CONFIG_ENV_IS_IN_ONENAND) || \
30 @@ -62,7 +63,7 @@ DECLARE_GLOBAL_DATA_PTR;
32 #if !defined(ENV_IS_IN_DEVICE) && \
33 !defined(CONFIG_ENV_IS_NOWHERE)
34 -# error Define one of CONFIG_ENV_IS_IN_{EEPROM|FLASH|MMC|FAT|EXT4|\
35 +# error Define one of CONFIG_ENV_IS_IN_{EEPROM|FLASH|MMC|FAT|EXT4|MTD|\
36 NAND|NVRAM|ONENAND|SATA|SPI_FLASH|REMOTE|UBI} or CONFIG_ENV_IS_NOWHERE
41 @@ -53,7 +53,7 @@ config ENV_IS_NOWHERE
42 !ENV_IS_IN_MMC && !ENV_IS_IN_NAND && \
43 !ENV_IS_IN_NVRAM && !ENV_IS_IN_ONENAND && \
44 !ENV_IS_IN_REMOTE && !ENV_IS_IN_SPI_FLASH && \
46 + !ENV_IS_IN_UBI && !ENV_IS_IN_MTD
48 Define this if you don't want to or can't have an environment stored
49 on a storage medium. In this case the environment will still exist
50 @@ -242,6 +242,27 @@ config ENV_IS_IN_MMC
51 This value is also in units of bytes, but must also be aligned to
52 an MMC sector boundary.
55 + bool "Environment in a MTD device"
56 + depends on !CHAIN_OF_TRUST
59 + Define this if you have a MTD device which you want to use for
62 + - CONFIG_ENV_MTD_NAME:
63 + - CONFIG_ENV_OFFSET:
66 + These three #defines specify the MTD device where the environment
67 + is stored, offset and size of the environment area within the MTD
68 + device. CONFIG_ENV_OFFSET must be aligned to an erase block boundary.
70 + - CONFIG_ENV_SIZE_REDUND:
72 + This #define specify the maximum size allowed for read/write/erase
73 + with skipped bad blocks starting from ENV_OFFSET.
76 bool "Environment in a NAND device"
77 depends on !CHAIN_OF_TRUST
78 @@ -549,10 +570,16 @@ config ENV_ADDR_REDUND
79 Offset from the start of the device (or partition) of the redundant
83 + string "Name of the MTD device storing the environment"
84 + depends on ENV_IS_IN_MTD
86 + Name of the MTD device that stores the environment
89 hex "Environment offset"
90 depends on ENV_IS_IN_EEPROM || ENV_IS_IN_MMC || ENV_IS_IN_NAND || \
92 + ENV_IS_IN_SPI_FLASH || ENV_IS_IN_MTD
93 default 0x3f8000 if ARCH_ROCKCHIP && ENV_IS_IN_MMC
94 default 0x140000 if ARCH_ROCKCHIP && ENV_IS_IN_SPI_FLASH
95 default 0xF0000 if ARCH_SUNXI
96 @@ -599,6 +626,12 @@ config ENV_SECT_SIZE
98 Size of the sector containing the environment.
100 +config ENV_SIZE_REDUND
101 + hex "Redundant environment size"
102 + depends on ENV_IS_IN_MTD
104 + The maximum size allowed for read/write/erase with skipped bad blocks.
107 string "UBI partition name"
108 depends on ENV_IS_IN_UBI
111 @@ -26,6 +26,7 @@ obj-$(CONFIG_$(SPL_TPL_)ENV_IS_NOWHERE)
112 obj-$(CONFIG_$(SPL_TPL_)ENV_IS_IN_MMC) += mmc.o
113 obj-$(CONFIG_$(SPL_TPL_)ENV_IS_IN_FAT) += fat.o
114 obj-$(CONFIG_$(SPL_TPL_)ENV_IS_IN_EXT4) += ext4.o
115 +obj-$(CONFIG_$(SPL_TPL_)ENV_IS_IN_MTD) += mtd.o
116 obj-$(CONFIG_$(SPL_TPL_)ENV_IS_IN_NAND) += nand.o
117 obj-$(CONFIG_$(SPL_TPL_)ENV_IS_IN_SPI_FLASH) += sf.o
118 obj-$(CONFIG_$(SPL_TPL_)ENV_IS_IN_FLASH) += flash.o
121 @@ -69,6 +69,9 @@ static enum env_location env_locations[]
122 #ifdef CONFIG_ENV_IS_IN_MMC
125 +#ifdef CONFIG_ENV_IS_IN_MTD
128 #ifdef CONFIG_ENV_IS_IN_NAND
134 +/* SPDX-License-Identifier: GPL-2.0 */
136 + * Copyright (C) 2021 MediaTek Inc. All Rights Reserved.
138 + * Author: Weijie Gao <weijie.gao@mediatek.com>
141 +#include <command.h>
143 +#include <env_internal.h>
145 +#include <linux/kernel.h>
146 +#include <linux/stddef.h>
147 +#include <linux/types.h>
148 +#include <linux/mtd/mtd.h>
150 +#include <memalign.h>
154 +#if CONFIG_ENV_SIZE_REDUND < CONFIG_ENV_SIZE
155 +#undef CONFIG_ENV_SIZE_REDUND
156 +#define CONFIG_ENV_SIZE_REDUND CONFIG_ENV_SIZE
159 +#if defined(ENV_IS_EMBEDDED)
160 +env_t *env_ptr = &environment;
161 +#else /* ! ENV_IS_EMBEDDED */
163 +#endif /* ENV_IS_EMBEDDED */
165 +DECLARE_GLOBAL_DATA_PTR;
167 +static int env_mtd_init(void)
169 +#if defined(ENV_IS_EMBEDDED)
170 + int crc1_ok = 0, crc2_ok = 0;
173 + tmp_env1 = env_ptr;
174 + crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc;
176 + if (!crc1_ok && !crc2_ok) {
178 + gd->env_valid = ENV_INVALID;
181 + } else if (crc1_ok && !crc2_ok) {
182 + gd->env_valid = ENV_VALID;
185 + if (gd->env_valid == ENV_VALID)
186 + env_ptr = tmp_env1;
188 + gd->env_addr = (ulong)env_ptr->data;
190 +#else /* ENV_IS_EMBEDDED */
191 + gd->env_addr = (ulong)&default_environment[0];
192 + gd->env_valid = ENV_VALID;
193 +#endif /* ENV_IS_EMBEDDED */
198 +static struct mtd_info *env_mtd_get_dev(void)
200 + struct mtd_info *mtd;
202 + mtd_probe_devices();
204 + mtd = get_mtd_device_nm(CONFIG_ENV_MTD_NAME);
205 + if (IS_ERR(mtd) || !mtd) {
206 + printf("MTD device '%s' not found\n", CONFIG_ENV_MTD_NAME);
213 +static inline bool mtd_addr_is_block_aligned(struct mtd_info *mtd, u64 addr)
215 + return (addr & mtd->erasesize_mask) == 0;
218 +static int mtd_io_skip_bad(struct mtd_info *mtd, bool read, loff_t offset,
219 + size_t length, size_t redund, u8 *buffer)
221 + struct mtd_oob_ops io_op = {};
222 + size_t remaining = length;
226 + io_op.mode = MTD_OPS_PLACE_OOB;
227 + io_op.len = mtd->writesize;
228 + io_op.datbuf = (void *)buffer;
230 + /* Search for the first good block after the given offset */
232 + end = (off + redund) | (mtd->erasesize - 1);
233 + while (mtd_block_isbad(mtd, off) && off < end)
234 + off += mtd->erasesize;
236 + /* Reached end position */
240 + /* Loop over the pages to do the actual read/write */
241 + while (remaining) {
242 + /* Skip the block if it is bad */
243 + if (mtd_addr_is_block_aligned(mtd, off) &&
244 + mtd_block_isbad(mtd, off)) {
245 + off += mtd->erasesize;
250 + ret = mtd_read_oob(mtd, off, &io_op);
252 + ret = mtd_write_oob(mtd, off, &io_op);
255 + printf("Failure while %s at offset 0x%llx\n",
256 + read ? "reading" : "writing", off);
260 + off += io_op.retlen;
261 + remaining -= io_op.retlen;
262 + io_op.datbuf += io_op.retlen;
263 + io_op.oobbuf += io_op.oobretlen;
265 + /* Reached end position */
273 +#ifdef CONFIG_CMD_SAVEENV
274 +static int mtd_erase_skip_bad(struct mtd_info *mtd, loff_t offset,
275 + size_t length, size_t redund)
277 + struct erase_info erase_op = {};
278 + loff_t end = (offset + redund) | (mtd->erasesize - 1);
281 + erase_op.mtd = mtd;
282 + erase_op.addr = offset;
283 + erase_op.len = length;
285 + while (erase_op.len) {
286 + ret = mtd_erase(mtd, &erase_op);
288 + /* Abort if its not a bad block error */
292 + printf("Skipping bad block at 0x%08llx\n", erase_op.fail_addr);
294 + /* Skip bad block and continue behind it */
295 + erase_op.len -= erase_op.fail_addr - erase_op.addr;
296 + erase_op.len -= mtd->erasesize;
297 + erase_op.addr = erase_op.fail_addr + mtd->erasesize;
299 + /* Reached end position */
300 + if (erase_op.addr >= end)
307 +static int env_mtd_save(void)
309 + ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
310 + struct mtd_info *mtd;
313 + ret = env_export(env_new);
317 + mtd = env_mtd_get_dev();
321 + printf("Erasing on MTD device '%s'... ", mtd->name);
323 + ret = mtd_erase_skip_bad(mtd, CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE,
324 + CONFIG_ENV_SIZE_REDUND);
326 + puts(ret ? "FAILED\n" : "OK\n");
329 + put_mtd_device(mtd);
333 + printf("Writing to MTD device '%s'... ", mtd->name);
335 + ret = mtd_io_skip_bad(mtd, false, CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE,
336 + CONFIG_ENV_SIZE_REDUND, (u8 *)env_new);
338 + puts(ret ? "FAILED\n" : "OK\n");
340 + put_mtd_device(mtd);
344 +#endif /* CONFIG_CMD_SAVEENV */
346 +static int readenv(size_t offset, u_char *buf)
348 + struct mtd_info *mtd;
351 + mtd = env_mtd_get_dev();
355 + ret = mtd_io_skip_bad(mtd, true, offset, CONFIG_ENV_SIZE,
356 + CONFIG_ENV_SIZE_REDUND, buf);
358 + put_mtd_device(mtd);
363 +static int env_mtd_load(void)
365 +#if !defined(ENV_IS_EMBEDDED)
366 + ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
369 + ret = readenv(CONFIG_ENV_OFFSET, (u_char *)buf);
371 + env_set_default("readenv() failed", 0);
375 + return env_import(buf, 1, H_EXTERNAL);
376 +#endif /* ! ENV_IS_EMBEDDED */
381 +U_BOOT_ENV_LOCATION(mtd) = {
382 + .location = ENVL_MTD,
384 + .load = env_mtd_load,
385 +#if defined(CONFIG_CMD_SAVEENV)
386 + .save = env_save_ptr(env_mtd_save),
388 + .init = env_mtd_init,
390 --- a/include/env_internal.h
391 +++ b/include/env_internal.h
392 @@ -130,6 +130,7 @@ enum env_location {
402 @@ -41,6 +41,7 @@ ENVCRC-$(CONFIG_ENV_IS_EMBEDDED) = y
403 ENVCRC-$(CONFIG_ENV_IS_IN_EEPROM) = y
404 ENVCRC-$(CONFIG_ENV_IS_IN_FLASH) = y
405 ENVCRC-$(CONFIG_ENV_IS_IN_ONENAND) = y
406 +ENVCRC-$(CONFIG_ENV_IS_IN_MTD) = y
407 ENVCRC-$(CONFIG_ENV_IS_IN_NAND) = y
408 ENVCRC-$(CONFIG_ENV_IS_IN_NVRAM) = y
409 ENVCRC-$(CONFIG_ENV_IS_IN_SPI_FLASH) = y