2
0

100-11-env-add-support-for-NMBM-upper-MTD-layer.patch 7.2 KB

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