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

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