1 // SPDX-License-Identifier: GPL-2.0+
3 * (c) Copyright 2016 by VRT Technology
6 * Stuart Longland <stuartl@vrt.com.au>
8 * Based on FAT environment driver
9 * (c) Copyright 2011 by Tigris Elektronik GmbH
12 * Maximilian Schwerin <mvs@tigris.de>
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>
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)
44 return (const char *)CONFIG_ENV_EXT4_DEVICE_AND_PART;
47 static int env_ext4_save_buffer(env_t *env_new)
49 struct blk_desc *dev_desc = NULL;
50 struct disk_partition info;
53 const char *ifname = env_ext4_get_intf();
54 const char *dev_and_part = env_ext4_get_dev_part();
56 part = blk_get_device_part_str(ifname, dev_and_part,
61 dev = dev_desc->devnum;
62 ext4fs_set_blk_dev(dev_desc, &info);
64 if (!ext4fs_mount(info.size)) {
65 printf("\n** Unable to use %s %s for saveenv **\n",
66 ifname, dev_and_part);
70 err = ext4fs_write(CONFIG_ENV_EXT4_FILE, (void *)env_new,
71 sizeof(env_t), FILETYPE_REG);
75 printf("\n** Unable to write \"%s\" from %s%d:%d **\n",
76 CONFIG_ENV_EXT4_FILE, ifname, dev, part);
83 static int env_ext4_save(void)
88 err = env_export(&env_new);
92 err = env_ext4_save_buffer(&env_new);
96 gd->env_valid = ENV_VALID;
102 static int env_ext4_erase(void)
107 memset(&env_new, 0, sizeof(env_t));
109 err = env_ext4_save_buffer(&env_new);
113 gd->env_valid = ENV_INVALID;
119 static int env_ext4_load(void)
121 ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
122 struct blk_desc *dev_desc = NULL;
123 struct disk_partition info;
127 const char *ifname = env_ext4_get_intf();
128 const char *dev_and_part = env_ext4_get_dev_part();
131 if (!strcmp(ifname, "mmc"))
132 mmc_initialize(NULL);
135 part = blk_get_device_part_str(ifname, dev_and_part,
136 &dev_desc, &info, 1);
138 goto err_env_relocate;
140 dev = dev_desc->devnum;
141 ext4fs_set_blk_dev(dev_desc, &info);
143 if (!ext4fs_mount(info.size)) {
144 printf("\n** Unable to use %s %s for loading the env **\n",
145 ifname, dev_and_part);
146 goto err_env_relocate;
149 err = ext4_read_file(CONFIG_ENV_EXT4_FILE, buf, 0, CONFIG_ENV_SIZE,
154 printf("\n** Unable to read \"%s\" from %s%d:%d **\n",
155 CONFIG_ENV_EXT4_FILE, ifname, dev, part);
156 goto err_env_relocate;
159 err = env_import(buf, 1, H_EXTERNAL);
161 gd->env_valid = ENV_VALID;
166 env_set_default(NULL, 0);
171 U_BOOT_ENV_LOCATION(ext4) = {
172 .location = ENVL_EXT4,
174 .load = env_ext4_load,
175 .save = ENV_SAVE_PTR(env_ext4_save),
176 .erase = CONFIG_IS_ENABLED(CMD_ERASEENV) ? env_ext4_erase :