1 // SPDX-License-Identifier: GPL-2.0+
3 * (c) Copyright 2016 by VRT Technology
8 * Based on FAT environment driver
9 * (c) Copyright 2011 by Tigris Elektronik GmbH
14 * and EXT4 filesystem implementation
15 * (C) Copyright 2011 - 2012 Samsung Electronics
16 * EXT4 filesystem implementation in Uboot by
26 #include <env_internal.h>
27 #include <linux/stddef.h>
35 DECLARE_GLOBAL_DATA_PTR;
37 __weak const char *env_ext4_get_intf(void)
39 return (const char *)CONFIG_ENV_EXT4_INTERFACE;
42 __weak const char *env_ext4_get_dev_part(void)
45 static char *part_str;
48 part_str = CONFIG_ENV_EXT4_DEVICE_AND_PART;
49 if (!strcmp(CONFIG_ENV_EXT4_INTERFACE, "mmc") && part_str[0] == ':') {
50 part_str = "0" CONFIG_ENV_EXT4_DEVICE_AND_PART;
51 part_str[0] += mmc_get_env_dev();
57 return (const char *)CONFIG_ENV_EXT4_DEVICE_AND_PART;
61 static int env_ext4_save_buffer(env_t *env_new)
63 struct blk_desc *dev_desc = NULL;
64 struct disk_partition info;
67 const char *ifname = env_ext4_get_intf();
68 const char *dev_and_part = env_ext4_get_dev_part();
70 part = blk_get_device_part_str(ifname, dev_and_part,
75 dev = dev_desc->devnum;
76 ext4fs_set_blk_dev(dev_desc, &info);
78 if (!ext4fs_mount(info.size)) {
79 printf("\n** Unable to use %s %s for saveenv **\n",
80 ifname, dev_and_part);
84 err = ext4fs_write(CONFIG_ENV_EXT4_FILE, (void *)env_new,
85 sizeof(env_t), FILETYPE_REG);
89 printf("\n** Unable to write \"%s\" from %s%d:%d **\n",
90 CONFIG_ENV_EXT4_FILE, ifname, dev, part);
97 static int env_ext4_save(void)
102 err = env_export(&env_new);
106 err = env_ext4_save_buffer(&env_new);
110 gd->env_valid = ENV_VALID;
116 static int env_ext4_erase(void)
121 memset(&env_new, 0, sizeof(env_t));
123 err = env_ext4_save_buffer(&env_new);
127 gd->env_valid = ENV_INVALID;
133 static int env_ext4_load(void)
135 ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
136 struct blk_desc *dev_desc = NULL;
137 struct disk_partition info;
141 const char *ifname = env_ext4_get_intf();
142 const char *dev_and_part = env_ext4_get_dev_part();
145 if (!strcmp(ifname, "mmc"))
146 mmc_initialize(NULL);
149 part = blk_get_device_part_str(ifname, dev_and_part,
150 &dev_desc, &info, 1);
152 goto err_env_relocate;
154 dev = dev_desc->devnum;
155 ext4fs_set_blk_dev(dev_desc, &info);
157 if (!ext4fs_mount(info.size)) {
158 printf("\n** Unable to use %s %s for loading the env **\n",
159 ifname, dev_and_part);
160 goto err_env_relocate;
163 err = ext4_read_file(CONFIG_ENV_EXT4_FILE, buf, 0, CONFIG_ENV_SIZE,
168 printf("\n** Unable to read \"%s\" from %s%d:%d **\n",
169 CONFIG_ENV_EXT4_FILE, ifname, dev, part);
170 goto err_env_relocate;
173 err = env_import(buf, 1, H_EXTERNAL);
175 gd->env_valid = ENV_VALID;
180 env_set_default(NULL, 0);
185 U_BOOT_ENV_LOCATION(ext4) = {
186 .location = ENVL_EXT4,
188 .load = env_ext4_load,
189 .save = ENV_SAVE_PTR(env_ext4_save),
190 .erase = CONFIG_IS_ENABLED(CMD_ERASEENV) ? env_ext4_erase :