]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
fd1000b9 SL |
2 | /* |
3 | * (c) Copyright 2016 by VRT Technology | |
4 | * | |
5 | * Author: | |
6 | * Stuart Longland <[email protected]> | |
7 | * | |
8 | * Based on FAT environment driver | |
9 | * (c) Copyright 2011 by Tigris Elektronik GmbH | |
10 | * | |
11 | * Author: | |
12 | * Maximilian Schwerin <[email protected]> | |
13 | * | |
14 | * and EXT4 filesystem implementation | |
15 | * (C) Copyright 2011 - 2012 Samsung Electronics | |
16 | * EXT4 filesystem implementation in Uboot by | |
17 | * Uma Shankar <[email protected]> | |
18 | * Manjunatha C Achar <[email protected]> | |
fd1000b9 SL |
19 | */ |
20 | ||
21 | #include <common.h> | |
e6f6f9e6 | 22 | #include <part.h> |
fd1000b9 SL |
23 | |
24 | #include <command.h> | |
0ac7d722 | 25 | #include <env.h> |
f3998fdc | 26 | #include <env_internal.h> |
fd1000b9 SL |
27 | #include <linux/stddef.h> |
28 | #include <malloc.h> | |
bd62e241 | 29 | #include <memalign.h> |
fd1000b9 SL |
30 | #include <search.h> |
31 | #include <errno.h> | |
32 | #include <ext4fs.h> | |
33 | #include <mmc.h> | |
401d1c4f | 34 | #include <asm/global_data.h> |
fd1000b9 | 35 | |
286fee50 PD |
36 | DECLARE_GLOBAL_DATA_PTR; |
37 | ||
d319edca PC |
38 | __weak const char *env_ext4_get_intf(void) |
39 | { | |
40 | return (const char *)CONFIG_ENV_EXT4_INTERFACE; | |
41 | } | |
42 | ||
43 | __weak const char *env_ext4_get_dev_part(void) | |
44 | { | |
b0493bb7 DW |
45 | #ifdef CONFIG_MMC |
46 | static char *part_str; | |
47 | ||
48 | if (!part_str) { | |
49 | part_str = CONFIG_ENV_EXT4_DEVICE_AND_PART; | |
50 | if (!strcmp(CONFIG_ENV_EXT4_INTERFACE, "mmc") && part_str[0] == ':') { | |
51 | part_str = "0" CONFIG_ENV_EXT4_DEVICE_AND_PART; | |
52 | part_str[0] += mmc_get_env_dev(); | |
53 | } | |
54 | } | |
55 | ||
56 | return part_str; | |
57 | #else | |
d319edca | 58 | return (const char *)CONFIG_ENV_EXT4_DEVICE_AND_PART; |
b0493bb7 | 59 | #endif |
d319edca PC |
60 | } |
61 | ||
f6de047e | 62 | static int env_ext4_save_buffer(env_t *env_new) |
fd1000b9 | 63 | { |
bd62e241 | 64 | struct blk_desc *dev_desc = NULL; |
0528979f | 65 | struct disk_partition info; |
fd1000b9 SL |
66 | int dev, part; |
67 | int err; | |
d319edca PC |
68 | const char *ifname = env_ext4_get_intf(); |
69 | const char *dev_and_part = env_ext4_get_dev_part(); | |
fd1000b9 | 70 | |
d319edca | 71 | part = blk_get_device_part_str(ifname, dev_and_part, |
1087a794 | 72 | &dev_desc, &info, 1); |
fd1000b9 SL |
73 | if (part < 0) |
74 | return 1; | |
75 | ||
bd62e241 | 76 | dev = dev_desc->devnum; |
fd1000b9 SL |
77 | ext4fs_set_blk_dev(dev_desc, &info); |
78 | ||
79 | if (!ext4fs_mount(info.size)) { | |
80 | printf("\n** Unable to use %s %s for saveenv **\n", | |
d319edca | 81 | ifname, dev_and_part); |
fd1000b9 SL |
82 | return 1; |
83 | } | |
84 | ||
f6de047e | 85 | err = ext4fs_write(CONFIG_ENV_EXT4_FILE, (void *)env_new, |
5efc0686 | 86 | sizeof(env_t), FILETYPE_REG); |
fd1000b9 SL |
87 | ext4fs_close(); |
88 | ||
89 | if (err == -1) { | |
90 | printf("\n** Unable to write \"%s\" from %s%d:%d **\n", | |
d319edca | 91 | CONFIG_ENV_EXT4_FILE, ifname, dev, part); |
fd1000b9 SL |
92 | return 1; |
93 | } | |
94 | ||
f6de047e PD |
95 | return 0; |
96 | } | |
97 | ||
98 | static int env_ext4_save(void) | |
99 | { | |
100 | env_t env_new; | |
101 | int err; | |
102 | ||
103 | err = env_export(&env_new); | |
104 | if (err) | |
105 | return err; | |
106 | ||
107 | err = env_ext4_save_buffer(&env_new); | |
108 | if (err) | |
109 | return err; | |
110 | ||
111 | gd->env_valid = ENV_VALID; | |
fd1000b9 | 112 | puts("done\n"); |
f6de047e | 113 | |
fd1000b9 SL |
114 | return 0; |
115 | } | |
fd1000b9 | 116 | |
0718f743 PD |
117 | static int env_ext4_erase(void) |
118 | { | |
119 | env_t env_new; | |
120 | int err; | |
121 | ||
122 | memset(&env_new, 0, sizeof(env_t)); | |
123 | ||
124 | err = env_ext4_save_buffer(&env_new); | |
125 | if (err) | |
126 | return err; | |
127 | ||
128 | gd->env_valid = ENV_INVALID; | |
129 | puts("done\n"); | |
130 | ||
131 | return 0; | |
132 | } | |
133 | ||
c5951991 | 134 | static int env_ext4_load(void) |
fd1000b9 SL |
135 | { |
136 | ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE); | |
bd62e241 | 137 | struct blk_desc *dev_desc = NULL; |
0528979f | 138 | struct disk_partition info; |
fd1000b9 SL |
139 | int dev, part; |
140 | int err; | |
bd62e241 | 141 | loff_t off; |
d319edca PC |
142 | const char *ifname = env_ext4_get_intf(); |
143 | const char *dev_and_part = env_ext4_get_dev_part(); | |
fd1000b9 | 144 | |
95058fbb | 145 | #ifdef CONFIG_MMC |
d319edca | 146 | if (!strcmp(ifname, "mmc")) |
26862b4a | 147 | mmc_initialize(NULL); |
95058fbb | 148 | #endif |
26862b4a | 149 | |
d319edca | 150 | part = blk_get_device_part_str(ifname, dev_and_part, |
1087a794 | 151 | &dev_desc, &info, 1); |
fd1000b9 SL |
152 | if (part < 0) |
153 | goto err_env_relocate; | |
154 | ||
bd62e241 | 155 | dev = dev_desc->devnum; |
fd1000b9 SL |
156 | ext4fs_set_blk_dev(dev_desc, &info); |
157 | ||
158 | if (!ext4fs_mount(info.size)) { | |
159 | printf("\n** Unable to use %s %s for loading the env **\n", | |
d319edca | 160 | ifname, dev_and_part); |
fd1000b9 SL |
161 | goto err_env_relocate; |
162 | } | |
163 | ||
1087a794 JRO |
164 | err = ext4_read_file(CONFIG_ENV_EXT4_FILE, buf, 0, CONFIG_ENV_SIZE, |
165 | &off); | |
fd1000b9 SL |
166 | ext4fs_close(); |
167 | ||
168 | if (err == -1) { | |
169 | printf("\n** Unable to read \"%s\" from %s%d:%d **\n", | |
d319edca | 170 | CONFIG_ENV_EXT4_FILE, ifname, dev, part); |
fd1000b9 SL |
171 | goto err_env_relocate; |
172 | } | |
173 | ||
890feeca | 174 | err = env_import(buf, 1, H_EXTERNAL); |
286fee50 PD |
175 | if (!err) |
176 | gd->env_valid = ENV_VALID; | |
177 | ||
178 | return err; | |
fd1000b9 SL |
179 | |
180 | err_env_relocate: | |
0ac7d722 | 181 | env_set_default(NULL, 0); |
c5951991 SG |
182 | |
183 | return -EIO; | |
fd1000b9 | 184 | } |
4415f1d1 SG |
185 | |
186 | U_BOOT_ENV_LOCATION(ext4) = { | |
187 | .location = ENVL_EXT4, | |
ac358beb | 188 | ENV_NAME("EXT4") |
e5bce247 | 189 | .load = env_ext4_load, |
1df96a7e | 190 | .save = ENV_SAVE_PTR(env_ext4_save), |
1af031ac | 191 | .erase = ENV_ERASE_PTR(env_ext4_erase), |
4415f1d1 | 192 | }; |