1 // SPDX-License-Identifier: GPL-2.0+
3 * (c) Copyright 2011 by Tigris Elektronik GmbH
11 #include <env_internal.h>
21 #include <asm/cache.h>
22 #include <asm/global_data.h>
23 #include <linux/stddef.h>
25 #ifdef CONFIG_XPL_BUILD
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 /* reserve one more char for the manipulation below */
45 static char part_str[] = CONFIG_ENV_FAT_DEVICE_AND_PART "\0";
47 if (!strcmp(CONFIG_ENV_FAT_INTERFACE, "mmc") && part_str[0] == ':') {
48 part_str[0] = '0' + mmc_get_env_dev();
49 strcpy(&part_str[1], CONFIG_ENV_FAT_DEVICE_AND_PART);
54 return CONFIG_ENV_FAT_DEVICE_AND_PART;
58 static int env_fat_save(void)
60 env_t __aligned(ARCH_DMA_MINALIGN) env_new;
61 struct blk_desc *dev_desc = NULL;
62 struct disk_partition info;
63 const char *file = CONFIG_ENV_FAT_FILE;
67 const char *ifname = env_fat_get_intf();
68 const char *dev_and_part = env_fat_get_dev_part();
70 err = env_export(&env_new);
74 part = blk_get_device_part_str(ifname, dev_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...\n", ifname, dev, part);
89 #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
90 if (gd->env_valid == ENV_VALID)
91 file = CONFIG_ENV_FAT_FILE_REDUND;
94 err = file_fat_write(file, (void *)&env_new, 0, sizeof(env_t), &size);
97 * This printf is embedded in the messages from env_save that
98 * will calling it. The missing \n is intentional.
100 printf("Unable to write \"%s\" from %s%d:%d...\n", file, ifname, dev, part);
104 #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
105 gd->env_valid = (gd->env_valid == ENV_REDUND) ? ENV_VALID : ENV_REDUND;
112 static int env_fat_load(void)
114 ALLOC_CACHE_ALIGN_BUFFER(char, buf1, CONFIG_ENV_SIZE);
115 #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
116 ALLOC_CACHE_ALIGN_BUFFER(char, buf2, CONFIG_ENV_SIZE);
119 struct blk_desc *dev_desc = NULL;
120 struct disk_partition info;
123 const char *ifname = env_fat_get_intf();
124 const char *dev_and_part = env_fat_get_dev_part();
127 if (!strcmp(ifname, "mmc"))
128 mmc_initialize(NULL);
130 #ifndef CONFIG_XPL_BUILD
131 #if defined(CONFIG_AHCI) || defined(CONFIG_SCSI)
132 if (!strcmp(CONFIG_ENV_FAT_INTERFACE, "scsi"))
135 #if defined(CONFIG_VIRTIO)
136 if (!strcmp(ifname, "virtio"))
140 part = blk_get_device_part_str(ifname, dev_and_part,
141 &dev_desc, &info, 1);
143 goto err_env_relocate;
145 dev = dev_desc->devnum;
146 if (fat_set_blk_dev(dev_desc, &info) != 0) {
148 * This printf is embedded in the messages from env_save that
149 * will calling it. The missing \n is intentional.
151 printf("Unable to use %s %d:%d...\n", ifname, dev, part);
152 goto err_env_relocate;
155 err1 = file_fat_read(CONFIG_ENV_FAT_FILE, buf1, CONFIG_ENV_SIZE);
156 #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
157 err2 = file_fat_read(CONFIG_ENV_FAT_FILE_REDUND, buf2, CONFIG_ENV_SIZE);
159 err1 = (err1 >= 0) ? 0 : -1;
160 err2 = (err2 >= 0) ? 0 : -1;
161 return env_import_redund(buf1, err1, buf2, err2, H_EXTERNAL);
165 * This printf is embedded in the messages from env_save that
166 * will calling it. The missing \n is intentional.
168 printf("Unable to read \"%s\" from %s%d:%d... \n",
169 CONFIG_ENV_FAT_FILE, ifname, dev, part);
170 goto err_env_relocate;
173 return env_import(buf1, 1, H_EXTERNAL);
177 env_set_default(NULL, 0);
183 U_BOOT_ENV_LOCATION(fat) = {
184 .location = ENVL_FAT,
187 .load = env_fat_load,
189 .save = ENV_SAVE_PTR(env_fat_save),