1 // SPDX-License-Identifier: GPL-2.0+
3 * (c) Copyright 2011 by Tigris Elektronik GmbH
6 * Maximilian Schwerin <mvs@tigris.de>
12 #include <env_internal.h>
21 #include <asm/cache.h>
22 #include <asm/global_data.h>
23 #include <linux/stddef.h>
25 #ifdef CONFIG_SPL_BUILD
26 /* TODO(sjg@chromium.org): Figure out why this is needed */
27 # if !defined(CONFIG_TARGET_AM335X_EVM) || defined(CONFIG_SPL_OS_BOOT)
34 DECLARE_GLOBAL_DATA_PTR;
36 __weak const char *env_fat_get_intf(void)
38 return (const char *)CONFIG_ENV_FAT_INTERFACE;
41 __weak char *env_fat_get_dev_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;
65 const char *file = CONFIG_ENV_FAT_FILE;
69 const char *ifname = env_fat_get_intf();
70 const char *dev_and_part = env_fat_get_dev_part();
72 err = env_export(&env_new);
76 part = blk_get_device_part_str(ifname, dev_and_part,
81 dev = dev_desc->devnum;
82 if (fat_set_blk_dev(dev_desc, &info) != 0) {
84 * This printf is embedded in the messages from env_save that
85 * will calling it. The missing \n is intentional.
87 printf("Unable to use %s %d:%d...\n", ifname, dev, part);
91 #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
92 if (gd->env_valid == ENV_VALID)
93 file = CONFIG_ENV_FAT_FILE_REDUND;
96 err = file_fat_write(file, (void *)&env_new, 0, sizeof(env_t), &size);
99 * This printf is embedded in the messages from env_save that
100 * will calling it. The missing \n is intentional.
102 printf("Unable to write \"%s\" from %s%d:%d...\n", file, ifname, dev, part);
106 #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
107 gd->env_valid = (gd->env_valid == ENV_REDUND) ? ENV_VALID : ENV_REDUND;
114 static int env_fat_load(void)
116 ALLOC_CACHE_ALIGN_BUFFER(char, buf1, CONFIG_ENV_SIZE);
117 #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
118 ALLOC_CACHE_ALIGN_BUFFER(char, buf2, CONFIG_ENV_SIZE);
121 struct blk_desc *dev_desc = NULL;
122 struct disk_partition info;
125 const char *ifname = env_fat_get_intf();
126 const char *dev_and_part = env_fat_get_dev_part();
129 if (!strcmp(ifname, "mmc"))
130 mmc_initialize(NULL);
132 #ifndef CONFIG_SPL_BUILD
133 #if defined(CONFIG_AHCI) || defined(CONFIG_SCSI)
134 if (!strcmp(CONFIG_ENV_FAT_INTERFACE, "scsi"))
138 part = blk_get_device_part_str(ifname, dev_and_part,
139 &dev_desc, &info, 1);
141 goto err_env_relocate;
143 dev = dev_desc->devnum;
144 if (fat_set_blk_dev(dev_desc, &info) != 0) {
146 * This printf is embedded in the messages from env_save that
147 * will calling it. The missing \n is intentional.
149 printf("Unable to use %s %d:%d...\n", ifname, dev, part);
150 goto err_env_relocate;
153 err1 = file_fat_read(CONFIG_ENV_FAT_FILE, buf1, CONFIG_ENV_SIZE);
154 #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
155 err2 = file_fat_read(CONFIG_ENV_FAT_FILE_REDUND, buf2, CONFIG_ENV_SIZE);
157 err1 = (err1 >= 0) ? 0 : -1;
158 err2 = (err2 >= 0) ? 0 : -1;
159 return env_import_redund(buf1, err1, buf2, err2, H_EXTERNAL);
163 * This printf is embedded in the messages from env_save that
164 * will calling it. The missing \n is intentional.
166 printf("Unable to read \"%s\" from %s%d:%d... \n",
167 CONFIG_ENV_FAT_FILE, ifname, dev, part);
168 goto err_env_relocate;
171 return env_import(buf1, 1, H_EXTERNAL);
175 env_set_default(NULL, 0);
181 U_BOOT_ENV_LOCATION(fat) = {
182 .location = ENVL_FAT,
185 .load = env_fat_load,
187 .save = ENV_SAVE_PTR(env_fat_save),