1 // SPDX-License-Identifier: GPL-2.0+
3 * (c) Copyright 2011 by Tigris Elektronik GmbH
12 #include <env_internal.h>
20 #include <asm/cache.h>
21 #include <linux/stddef.h>
23 #ifdef CONFIG_SPL_BUILD
25 # if !defined(CONFIG_TARGET_AM335X_EVM) || defined(CONFIG_SPL_OS_BOOT)
32 __weak int mmc_get_env_dev(void)
34 #ifdef CONFIG_SYS_MMC_ENV_DEV
35 return CONFIG_SYS_MMC_ENV_DEV;
41 static char *env_fat_device_and_part(void)
44 static char *part_str;
47 part_str = CONFIG_ENV_FAT_DEVICE_AND_PART;
48 if (!strcmp(CONFIG_ENV_FAT_INTERFACE, "mmc") && part_str[0] == ':') {
49 part_str = "0" CONFIG_ENV_FAT_DEVICE_AND_PART;
50 part_str[0] += mmc_get_env_dev();
56 return CONFIG_ENV_FAT_DEVICE_AND_PART;
60 static int env_fat_save(void)
62 env_t __aligned(ARCH_DMA_MINALIGN) env_new;
63 struct blk_desc *dev_desc = NULL;
64 struct disk_partition info;
69 err = env_export(&env_new);
73 part = blk_get_device_part_str(CONFIG_ENV_FAT_INTERFACE,
74 env_fat_device_and_part(),
79 dev = dev_desc->devnum;
80 if (fat_set_blk_dev(dev_desc, &info) != 0) {
82 * This printf is embedded in the messages from env_save that
83 * will calling it. The missing \n is intentional.
85 printf("Unable to use %s %d:%d... ",
86 CONFIG_ENV_FAT_INTERFACE, dev, part);
90 err = file_fat_write(CONFIG_ENV_FAT_FILE, (void *)&env_new, 0, sizeof(env_t),
94 * This printf is embedded in the messages from env_save that
95 * will calling it. The missing \n is intentional.
97 printf("Unable to write \"%s\" from %s%d:%d... ",
98 CONFIG_ENV_FAT_FILE, CONFIG_ENV_FAT_INTERFACE, dev, part);
106 static int env_fat_load(void)
108 ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
109 struct blk_desc *dev_desc = NULL;
110 struct disk_partition info;
115 if (!strcmp(CONFIG_ENV_FAT_INTERFACE, "mmc"))
116 mmc_initialize(NULL);
119 part = blk_get_device_part_str(CONFIG_ENV_FAT_INTERFACE,
120 env_fat_device_and_part(),
121 &dev_desc, &info, 1);
123 goto err_env_relocate;
125 dev = dev_desc->devnum;
126 if (fat_set_blk_dev(dev_desc, &info) != 0) {
128 * This printf is embedded in the messages from env_save that
129 * will calling it. The missing \n is intentional.
131 printf("Unable to use %s %d:%d... ",
132 CONFIG_ENV_FAT_INTERFACE, dev, part);
133 goto err_env_relocate;
136 err = file_fat_read(CONFIG_ENV_FAT_FILE, buf, CONFIG_ENV_SIZE);
139 * This printf is embedded in the messages from env_save that
140 * will calling it. The missing \n is intentional.
142 printf("Unable to read \"%s\" from %s%d:%d... ",
143 CONFIG_ENV_FAT_FILE, CONFIG_ENV_FAT_INTERFACE, dev, part);
144 goto err_env_relocate;
147 return env_import(buf, 1);
150 env_set_default(NULL, 0);
156 U_BOOT_ENV_LOCATION(fat) = {
157 .location = ENVL_FAT,
160 .load = env_fat_load,
162 .save = ENV_SAVE_PTR(env_fat_save),