uboot-mediatek: update to U-Boot 2024.01 release
[openwrt/openwrt.git] / package / boot / uboot-mediatek / patches / 100-11-env-add-support-for-NMBM-upper-MTD-layer.patch
1 From 240d98e6ad0aed3c11236aa40a60bbd6fe01fae5 Mon Sep 17 00:00:00 2001
2 From: Weijie Gao <weijie.gao@mediatek.com>
3 Date: Mon, 25 Jul 2022 10:50:46 +0800
4 Subject: [PATCH 45/71] env: add support for NMBM upper MTD layer
5
6 Add an env driver for NMBM upper MTD layer
7
8 Signed-off-by: Weijie Gao <weijie.gao@mediatek.com>
9 ---
10 cmd/nvedit.c | 3 +-
11 env/Kconfig | 19 ++++-
12 env/Makefile | 1 +
13 env/env.c | 3 +
14 env/nmbm.c | 155 +++++++++++++++++++++++++++++++++++++++++
15 include/env_internal.h | 1 +
16 tools/Makefile | 1 +
17 7 files changed, 180 insertions(+), 3 deletions(-)
18 create mode 100644 env/nmbm.c
19
20 --- a/env/Kconfig
21 +++ b/env/Kconfig
22 @@ -59,6 +59,7 @@ config ENV_IS_DEFAULT
23 def_bool y if !ENV_IS_IN_EEPROM && !ENV_IS_IN_EXT4 && \
24 !ENV_IS_IN_FAT && !ENV_IS_IN_FLASH && \
25 !ENV_IS_IN_MMC && !ENV_IS_IN_NAND && \
26 + !ENV_IS_IN_NMBM && \
27 !ENV_IS_IN_NVRAM && !ENV_IS_IN_ONENAND && \
28 !ENV_IS_IN_REMOTE && !ENV_IS_IN_SPI_FLASH && \
29 !ENV_IS_IN_UBI && !ENV_IS_IN_MTD
30 @@ -315,6 +316,21 @@ config ENV_RANGE
31 Specifying a range with more erase blocks than are needed to hold
32 CONFIG_ENV_SIZE allows bad blocks within the range to be avoided.
33
34 +config ENV_IS_IN_NMBM
35 + bool "Environment in a NMBM upper MTD layer"
36 + depends on !CHAIN_OF_TRUST
37 + depends on NMBM_MTD
38 + help
39 + Define this if you have a NMBM upper MTD which you want to use for
40 + the environment.
41 +
42 + - CONFIG_ENV_OFFSET:
43 + - CONFIG_ENV_SIZE:
44 +
45 + These two #defines specify the offset and size of the environment
46 + area within the first NAND device. CONFIG_ENV_OFFSET must be
47 + aligned to an erase block boundary.
48 +
49 config ENV_IS_IN_NVRAM
50 bool "Environment in a non-volatile RAM"
51 depends on !CHAIN_OF_TRUST
52 @@ -591,7 +607,7 @@ config ENV_MTD_NAME
53 config ENV_OFFSET
54 hex "Environment offset"
55 depends on ENV_IS_IN_EEPROM || ENV_IS_IN_MMC || ENV_IS_IN_NAND || \
56 - ENV_IS_IN_SPI_FLASH || ENV_IS_IN_MTD
57 + ENV_IS_IN_SPI_FLASH || ENV_IS_IN_NMBM || ENV_IS_IN_MTD
58 default 0x3f8000 if ARCH_ROCKCHIP && ENV_IS_IN_MMC
59 default 0x140000 if ARCH_ROCKCHIP && ENV_IS_IN_SPI_FLASH
60 default 0xF0000 if ARCH_SUNXI
61 --- a/env/Makefile
62 +++ b/env/Makefile
63 @@ -26,6 +26,7 @@ obj-$(CONFIG_$(SPL_TPL_)ENV_IS_IN_FAT) +
64 obj-$(CONFIG_$(SPL_TPL_)ENV_IS_IN_EXT4) += ext4.o
65 obj-$(CONFIG_$(SPL_TPL_)ENV_IS_IN_MTD) += mtd.o
66 obj-$(CONFIG_$(SPL_TPL_)ENV_IS_IN_NAND) += nand.o
67 +obj-$(CONFIG_$(SPL_TPL_)ENV_IS_IN_NMBM) += nmbm.o
68 obj-$(CONFIG_$(SPL_TPL_)ENV_IS_IN_SPI_FLASH) += sf.o
69 obj-$(CONFIG_$(SPL_TPL_)ENV_IS_IN_FLASH) += flash.o
70
71 --- a/env/env.c
72 +++ b/env/env.c
73 @@ -52,6 +52,9 @@ static enum env_location env_locations[]
74 #ifdef CONFIG_ENV_IS_IN_NAND
75 ENVL_NAND,
76 #endif
77 +#ifdef CONFIG_ENV_IS_IN_NMBM
78 + ENVL_NMBM,
79 +#endif
80 #ifdef CONFIG_ENV_IS_IN_NVRAM
81 ENVL_NVRAM,
82 #endif
83 --- /dev/null
84 +++ b/env/nmbm.c
85 @@ -0,0 +1,155 @@
86 +/* SPDX-License-Identifier: GPL-2.0 */
87 +/*
88 + * Copyright (C) 2020 MediaTek Inc. All Rights Reserved.
89 + *
90 + * Author: Weijie Gao <weijie.gao@mediatek.com>
91 + */
92 +
93 +#include <command.h>
94 +#include <env.h>
95 +#include <env_internal.h>
96 +#include <errno.h>
97 +#include <linux/kernel.h>
98 +#include <linux/stddef.h>
99 +#include <linux/types.h>
100 +#include <malloc.h>
101 +#include <memalign.h>
102 +#include <search.h>
103 +
104 +#include <nmbm/nmbm-mtd.h>
105 +
106 +#if defined(CONFIG_CMD_SAVEENV) && defined(CONFIG_NMBM_MTD)
107 +#define CMD_SAVEENV
108 +#endif
109 +
110 +#if defined(ENV_IS_EMBEDDED)
111 +env_t *env_ptr = &environment;
112 +#else /* ! ENV_IS_EMBEDDED */
113 +env_t *env_ptr;
114 +#endif /* ENV_IS_EMBEDDED */
115 +
116 +DECLARE_GLOBAL_DATA_PTR;
117 +
118 +static int env_nmbm_init(void)
119 +{
120 +#if defined(ENV_IS_EMBEDDED)
121 + int crc1_ok = 0, crc2_ok = 0;
122 + env_t *tmp_env1;
123 +
124 + tmp_env1 = env_ptr;
125 + crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc;
126 +
127 + if (!crc1_ok && !crc2_ok) {
128 + gd->env_addr = 0;
129 + gd->env_valid = ENV_INVALID;
130 +
131 + return 0;
132 + } else if (crc1_ok && !crc2_ok) {
133 + gd->env_valid = ENV_VALID;
134 + }
135 +
136 + if (gd->env_valid == ENV_VALID)
137 + env_ptr = tmp_env1;
138 +
139 + gd->env_addr = (ulong)env_ptr->data;
140 +
141 +#else /* ENV_IS_EMBEDDED */
142 + gd->env_addr = (ulong)&default_environment[0];
143 + gd->env_valid = ENV_VALID;
144 +#endif /* ENV_IS_EMBEDDED */
145 +
146 + return 0;
147 +}
148 +
149 +#ifdef CMD_SAVEENV
150 +static int env_nmbm_save(void)
151 +{
152 + ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
153 + struct mtd_info *mtd;
154 + struct erase_info ei;
155 + int ret = 0;
156 +
157 + ret = env_export(env_new);
158 + if (ret)
159 + return ret;
160 +
161 + mtd = nmbm_mtd_get_upper_by_index(0);
162 + if (!mtd)
163 + return 1;
164 +
165 + printf("Erasing on NMBM...\n");
166 + memset(&ei, 0, sizeof(ei));
167 +
168 + ei.mtd = mtd;
169 + ei.addr = CONFIG_ENV_OFFSET;
170 + ei.len = CONFIG_ENV_SIZE;
171 +
172 + if (mtd_erase(mtd, &ei))
173 + return 1;
174 +
175 + printf("Writing on NMBM... ");
176 + ret = mtd_write(mtd, CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, NULL,
177 + (u_char *)env_new);
178 + puts(ret ? "FAILED!\n" : "OK\n");
179 +
180 + return !!ret;
181 +}
182 +#endif /* CMD_SAVEENV */
183 +
184 +static int readenv(size_t offset, u_char *buf)
185 +{
186 + struct mtd_info *mtd;
187 + struct mtd_oob_ops ops;
188 + int ret;
189 + size_t len = CONFIG_ENV_SIZE;
190 +
191 + mtd = nmbm_mtd_get_upper_by_index(0);
192 + if (!mtd)
193 + return 1;
194 +
195 + ops.mode = MTD_OPS_AUTO_OOB;
196 + ops.ooblen = 0;
197 + while(len > 0) {
198 + ops.datbuf = buf;
199 + ops.len = min(len, (size_t)mtd->writesize);
200 + ops.oobbuf = NULL;
201 +
202 + ret = mtd_read_oob(mtd, offset, &ops);
203 + if (ret)
204 + return 1;
205 +
206 + buf += mtd->writesize;
207 + len -= mtd->writesize;
208 + offset += mtd->writesize;
209 + }
210 +
211 + return 0;
212 +}
213 +
214 +static int env_nmbm_load(void)
215 +{
216 +#if !defined(ENV_IS_EMBEDDED)
217 + ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
218 + int ret;
219 +
220 + ret = readenv(CONFIG_ENV_OFFSET, (u_char *)buf);
221 + if (ret) {
222 + env_set_default("readenv() failed", 0);
223 + return -EIO;
224 + }
225 +
226 + return env_import(buf, 1, H_EXTERNAL);
227 +#endif /* ! ENV_IS_EMBEDDED */
228 +
229 + return 0;
230 +}
231 +
232 +U_BOOT_ENV_LOCATION(nmbm) = {
233 + .location = ENVL_NMBM,
234 + ENV_NAME("NMBM")
235 + .load = env_nmbm_load,
236 +#if defined(CMD_SAVEENV)
237 + .save = env_save_ptr(env_nmbm_save),
238 +#endif
239 + .init = env_nmbm_init,
240 +};
241 --- a/include/env_internal.h
242 +++ b/include/env_internal.h
243 @@ -111,6 +111,7 @@ enum env_location {
244 ENVL_MMC,
245 ENVL_MTD,
246 ENVL_NAND,
247 + ENVL_NMBM,
248 ENVL_NVRAM,
249 ENVL_ONENAND,
250 ENVL_REMOTE,
251 --- a/tools/Makefile
252 +++ b/tools/Makefile
253 @@ -39,6 +39,7 @@ ENVCRC-$(CONFIG_ENV_IS_IN_FLASH) = y
254 ENVCRC-$(CONFIG_ENV_IS_IN_ONENAND) = y
255 ENVCRC-$(CONFIG_ENV_IS_IN_MTD) = y
256 ENVCRC-$(CONFIG_ENV_IS_IN_NAND) = y
257 +ENVCRC-$(CONFIG_ENV_IS_IN_NMBM) = y
258 ENVCRC-$(CONFIG_ENV_IS_IN_NVRAM) = y
259 ENVCRC-$(CONFIG_ENV_IS_IN_SPI_FLASH) = y
260 BUILD_ENVCRC ?= $(ENVCRC-y)