1 /*
2 * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 *
17 * Description: ufs.c
18 */
19
20 #include <common.h>
21 #include <command.h>
22 #include <env_internal.h>
23 #include <fdtdec.h>
24 #include <linux/stddef.h>
25 #include <malloc.h>
26 #include <memalign.h>
27 #include <ufs.h>
28 #include <part.h>
29 #include <search.h>
30 #include <errno.h>
31
32 DECLARE_GLOBAL_DATA_PTR;
33
34 #if !defined(CONFIG_ENV_OFFSET)
35 #define CONFIG_ENV_OFFSET 0
36 #endif
37
38 #define UFS_BLK_LEN 4096
39
ufs_offset(void)40 static inline s64 ufs_offset(void)
41 {
42 return CONFIG_ENV_OFFSET;
43 }
44
ufs_get_env_addr(u32 * env_addr)45 __weak int ufs_get_env_addr(u32 *env_addr)
46 {
47 s64 offset = ufs_offset();
48
49 *env_addr = offset;
50
51 return 0;
52 }
53
54 #if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_SPL_BUILD)
write_env(unsigned long size,unsigned long offset,const void * buffer)55 static int write_env(unsigned long size, unsigned long offset, const void *buffer)
56 {
57 uint start, cnt, n;
58
59 start = ALIGN(offset, UFS_BLK_LEN);
60 cnt = ALIGN(size, UFS_BLK_LEN);
61
62 n = ufs_write_storage((uint64_t)(uintptr_t)buffer, start, cnt);
63
64 return (n == 0) ? 0 : -1;
65 }
66
env_ufs_save(void)67 static int env_ufs_save(void)
68 {
69 ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
70 u32 offset;
71 int ret;
72
73 ufs_storage_init();
74
75 ret = env_export(env_new);
76 if (ret)
77 goto err;
78
79 if (ufs_get_env_addr(&offset)) {
80 ret = 1;
81 goto err;
82 }
83
84 printf("Writing to UFS... ");
85 if (write_env(CONFIG_ENV_SIZE, offset, (u_char *)env_new)) {
86 puts("failed\n");
87 ret = 1;
88 goto err;
89 }
90
91 ret = 0;
92
93 err:
94 return ret;
95 }
96 #endif /* CONFIG_CMD_SAVEENV && !CONFIG_SPL_BUILD */
97
read_env(unsigned long size,unsigned long offset,const void * buffer)98 static int read_env(unsigned long size, unsigned long offset, const void *buffer)
99 {
100 uint start, cnt, n;
101
102 start = ALIGN(offset, UFS_BLK_LEN);
103 cnt = ALIGN(size, UFS_BLK_LEN);
104
105 n = ufs_read_storage((uint64_t)(uintptr_t)buffer, start, cnt);
106
107 return (n == 0) ? 0 : -1;
108 }
109
env_ufs_load(void)110 static int env_ufs_load(void)
111 {
112 #if !defined(ENV_IS_EMBEDDED)
113 ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
114 u32 offset;
115 int ret;
116 char *errmsg = NULL;
117
118 ufs_storage_init();
119
120 if (ufs_get_env_addr(&offset)) {
121 ret = -EIO;
122 goto err;
123 }
124
125 if (read_env(CONFIG_ENV_SIZE, offset, buf)) {
126 errmsg = "!read failed";
127 ret = -EIO;
128 goto err;
129 }
130
131 ret = env_import(buf, 1);
132
133 err:
134 if (ret)
135 env_set_default(errmsg, 0);
136 #endif
137 return ret;
138 }
139
140 U_BOOT_ENV_LOCATION(ufs) = {
141 .location = ENVL_UFS,
142 ENV_NAME("UFS")
143 .load = env_ufs_load,
144 #ifndef CONFIG_SPL_BUILD
145 .save = env_save_ptr(env_ufs_save),
146 #endif
147 };
148