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>
20 #include <asm/cache.h>
21 #include <linux/stddef.h>
23 #ifdef CONFIG_SPL_BUILD
24 /* TODO(sjg@chromium.org): Figure out why this is needed */
25 # if !defined(CONFIG_TARGET_AM335X_EVM) || defined(CONFIG_SPL_OS_BOOT)
32 static char *env_fat_device_and_part(void)
35 static char *part_str;
38 part_str = CONFIG_ENV_FAT_DEVICE_AND_PART;
39 if (!strcmp(CONFIG_ENV_FAT_INTERFACE, "mmc") && part_str[0] == ':') {
40 part_str = "0" CONFIG_ENV_FAT_DEVICE_AND_PART;
41 part_str[0] += mmc_get_env_dev();
47 return CONFIG_ENV_FAT_DEVICE_AND_PART;
51 static int env_fat_save(void)
53 env_t __aligned(ARCH_DMA_MINALIGN) env_new;
54 struct blk_desc *dev_desc = NULL;
55 struct disk_partition info;
60 err = env_export(&env_new);
64 part = blk_get_device_part_str(CONFIG_ENV_FAT_INTERFACE,
65 env_fat_device_and_part(),
70 dev = dev_desc->devnum;
71 if (fat_set_blk_dev(dev_desc, &info) != 0) {
73 * This printf is embedded in the messages from env_save that
74 * will calling it. The missing \n is intentional.
76 printf("Unable to use %s %d:%d... ",
77 CONFIG_ENV_FAT_INTERFACE, dev, part);
81 err = file_fat_write(CONFIG_ENV_FAT_FILE, (void *)&env_new, 0, sizeof(env_t),
85 * This printf is embedded in the messages from env_save that
86 * will calling it. The missing \n is intentional.
88 printf("Unable to write \"%s\" from %s%d:%d... ",
89 CONFIG_ENV_FAT_FILE, CONFIG_ENV_FAT_INTERFACE, dev, part);
97 static int env_fat_load(void)
99 ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
100 struct blk_desc *dev_desc = NULL;
101 struct disk_partition info;
106 if (!strcmp(CONFIG_ENV_FAT_INTERFACE, "mmc"))
107 mmc_initialize(NULL);
110 part = blk_get_device_part_str(CONFIG_ENV_FAT_INTERFACE,
111 env_fat_device_and_part(),
112 &dev_desc, &info, 1);
114 goto err_env_relocate;
116 dev = dev_desc->devnum;
117 if (fat_set_blk_dev(dev_desc, &info) != 0) {
119 * This printf is embedded in the messages from env_save that
120 * will calling it. The missing \n is intentional.
122 printf("Unable to use %s %d:%d... ",
123 CONFIG_ENV_FAT_INTERFACE, dev, part);
124 goto err_env_relocate;
127 err = file_fat_read(CONFIG_ENV_FAT_FILE, buf, CONFIG_ENV_SIZE);
130 * This printf is embedded in the messages from env_save that
131 * will calling it. The missing \n is intentional.
133 printf("Unable to read \"%s\" from %s%d:%d... ",
134 CONFIG_ENV_FAT_FILE, CONFIG_ENV_FAT_INTERFACE, dev, part);
135 goto err_env_relocate;
138 return env_import(buf, 1, H_EXTERNAL);
141 env_set_default(NULL, 0);
147 U_BOOT_ENV_LOCATION(fat) = {
148 .location = ENVL_FAT,
151 .load = env_fat_load,
153 .save = ENV_SAVE_PTR(env_fat_save),