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