183d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
20649cd0dSSimon Glass /*
30649cd0dSSimon Glass * (c) Copyright 2011 by Tigris Elektronik GmbH
40649cd0dSSimon Glass *
50649cd0dSSimon Glass * Author:
60649cd0dSSimon Glass * Maximilian Schwerin <mvs@tigris.de>
70649cd0dSSimon Glass */
80649cd0dSSimon Glass
90649cd0dSSimon Glass #include <common.h>
100649cd0dSSimon Glass
110649cd0dSSimon Glass #include <command.h>
120649cd0dSSimon Glass #include <environment.h>
130649cd0dSSimon Glass #include <linux/stddef.h>
140649cd0dSSimon Glass #include <malloc.h>
150649cd0dSSimon Glass #include <memalign.h>
160649cd0dSSimon Glass #include <search.h>
170649cd0dSSimon Glass #include <errno.h>
180649cd0dSSimon Glass #include <fat.h>
190649cd0dSSimon Glass #include <mmc.h>
200649cd0dSSimon Glass
214415f1d1SSimon Glass #ifdef CONFIG_SPL_BUILD
224415f1d1SSimon Glass /* TODO(sjg@chromium.org): Figure out why this is needed */
234415f1d1SSimon Glass # if !defined(CONFIG_TARGET_AM335X_EVM) || defined(CONFIG_SPL_OS_BOOT)
244415f1d1SSimon Glass # define LOADENV
254415f1d1SSimon Glass # endif
264415f1d1SSimon Glass #else
274415f1d1SSimon Glass # define LOADENV
284415f1d1SSimon Glass # if defined(CONFIG_CMD_SAVEENV)
294415f1d1SSimon Glass # define CMD_SAVEENV
304415f1d1SSimon Glass # endif
314415f1d1SSimon Glass #endif
324415f1d1SSimon Glass
334415f1d1SSimon Glass #ifdef CMD_SAVEENV
env_fat_save(void)34e5bce247SSimon Glass static int env_fat_save(void)
350649cd0dSSimon Glass {
36cda87ec5SAlex Kiernan env_t __aligned(ARCH_DMA_MINALIGN) env_new;
370649cd0dSSimon Glass struct blk_desc *dev_desc = NULL;
380649cd0dSSimon Glass disk_partition_t info;
390649cd0dSSimon Glass int dev, part;
400649cd0dSSimon Glass int err;
410649cd0dSSimon Glass loff_t size;
420649cd0dSSimon Glass
430649cd0dSSimon Glass err = env_export(&env_new);
440649cd0dSSimon Glass if (err)
450649cd0dSSimon Glass return err;
460649cd0dSSimon Glass
470649cd0dSSimon Glass part = blk_get_device_part_str(CONFIG_ENV_FAT_INTERFACE,
480649cd0dSSimon Glass CONFIG_ENV_FAT_DEVICE_AND_PART,
490649cd0dSSimon Glass &dev_desc, &info, 1);
500649cd0dSSimon Glass if (part < 0)
510649cd0dSSimon Glass return 1;
520649cd0dSSimon Glass
530649cd0dSSimon Glass dev = dev_desc->devnum;
540649cd0dSSimon Glass if (fat_set_blk_dev(dev_desc, &info) != 0) {
55d0816da5SMaxime Ripard /*
56d0816da5SMaxime Ripard * This printf is embedded in the messages from env_save that
57d0816da5SMaxime Ripard * will calling it. The missing \n is intentional.
58d0816da5SMaxime Ripard */
59d0816da5SMaxime Ripard printf("Unable to use %s %d:%d... ",
600649cd0dSSimon Glass CONFIG_ENV_FAT_INTERFACE, dev, part);
610649cd0dSSimon Glass return 1;
620649cd0dSSimon Glass }
630649cd0dSSimon Glass
640649cd0dSSimon Glass err = file_fat_write(CONFIG_ENV_FAT_FILE, (void *)&env_new, 0, sizeof(env_t),
650649cd0dSSimon Glass &size);
660649cd0dSSimon Glass if (err == -1) {
67d0816da5SMaxime Ripard /*
68d0816da5SMaxime Ripard * This printf is embedded in the messages from env_save that
69d0816da5SMaxime Ripard * will calling it. The missing \n is intentional.
70d0816da5SMaxime Ripard */
71d0816da5SMaxime Ripard printf("Unable to write \"%s\" from %s%d:%d... ",
720649cd0dSSimon Glass CONFIG_ENV_FAT_FILE, CONFIG_ENV_FAT_INTERFACE, dev, part);
730649cd0dSSimon Glass return 1;
740649cd0dSSimon Glass }
750649cd0dSSimon Glass
760649cd0dSSimon Glass return 0;
770649cd0dSSimon Glass }
784415f1d1SSimon Glass #endif /* CMD_SAVEENV */
790649cd0dSSimon Glass
804415f1d1SSimon Glass #ifdef LOADENV
env_fat_load(void)81c5951991SSimon Glass static int env_fat_load(void)
820649cd0dSSimon Glass {
830649cd0dSSimon Glass ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
840649cd0dSSimon Glass struct blk_desc *dev_desc = NULL;
850649cd0dSSimon Glass disk_partition_t info;
860649cd0dSSimon Glass int dev, part;
870649cd0dSSimon Glass int err;
880649cd0dSSimon Glass
8995058fbbSHeinrich Schuchardt #ifdef CONFIG_MMC
9026862b4aSFaiz Abbas if (!strcmp(CONFIG_ENV_FAT_INTERFACE, "mmc"))
9126862b4aSFaiz Abbas mmc_initialize(NULL);
9295058fbbSHeinrich Schuchardt #endif
9326862b4aSFaiz Abbas
940649cd0dSSimon Glass part = blk_get_device_part_str(CONFIG_ENV_FAT_INTERFACE,
950649cd0dSSimon Glass CONFIG_ENV_FAT_DEVICE_AND_PART,
960649cd0dSSimon Glass &dev_desc, &info, 1);
970649cd0dSSimon Glass if (part < 0)
980649cd0dSSimon Glass goto err_env_relocate;
990649cd0dSSimon Glass
1000649cd0dSSimon Glass dev = dev_desc->devnum;
1010649cd0dSSimon Glass if (fat_set_blk_dev(dev_desc, &info) != 0) {
102d0816da5SMaxime Ripard /*
103d0816da5SMaxime Ripard * This printf is embedded in the messages from env_save that
104d0816da5SMaxime Ripard * will calling it. The missing \n is intentional.
105d0816da5SMaxime Ripard */
106d0816da5SMaxime Ripard printf("Unable to use %s %d:%d... ",
1070649cd0dSSimon Glass CONFIG_ENV_FAT_INTERFACE, dev, part);
1080649cd0dSSimon Glass goto err_env_relocate;
1090649cd0dSSimon Glass }
1100649cd0dSSimon Glass
1110649cd0dSSimon Glass err = file_fat_read(CONFIG_ENV_FAT_FILE, buf, CONFIG_ENV_SIZE);
1120649cd0dSSimon Glass if (err == -1) {
113d0816da5SMaxime Ripard /*
114d0816da5SMaxime Ripard * This printf is embedded in the messages from env_save that
115d0816da5SMaxime Ripard * will calling it. The missing \n is intentional.
116d0816da5SMaxime Ripard */
117d0816da5SMaxime Ripard printf("Unable to read \"%s\" from %s%d:%d... ",
1180649cd0dSSimon Glass CONFIG_ENV_FAT_FILE, CONFIG_ENV_FAT_INTERFACE, dev, part);
1190649cd0dSSimon Glass goto err_env_relocate;
1200649cd0dSSimon Glass }
1210649cd0dSSimon Glass
1222166ebf7SSimon Goldschmidt return env_import(buf, 1);
1230649cd0dSSimon Glass
1240649cd0dSSimon Glass err_env_relocate:
125*c5d548a9SYaniv Levinsky set_default_env(NULL, 0);
126c5951991SSimon Glass
127c5951991SSimon Glass return -EIO;
1280649cd0dSSimon Glass }
1294415f1d1SSimon Glass #endif /* LOADENV */
1304415f1d1SSimon Glass
1314415f1d1SSimon Glass U_BOOT_ENV_LOCATION(fat) = {
1324415f1d1SSimon Glass .location = ENVL_FAT,
133ac358bebSSimon Glass ENV_NAME("FAT")
1344415f1d1SSimon Glass #ifdef LOADENV
135e5bce247SSimon Glass .load = env_fat_load,
1364415f1d1SSimon Glass #endif
1374415f1d1SSimon Glass #ifdef CMD_SAVEENV
138e5bce247SSimon Glass .save = env_save_ptr(env_fat_save),
1394415f1d1SSimon Glass #endif
1404415f1d1SSimon Glass };
141