xref: /openbmc/u-boot/env/fat.c (revision 95058fbb97b8846e5fa569d5083a76567f2b9d81)
10649cd0dSSimon Glass /*
20649cd0dSSimon Glass  * (c) Copyright 2011 by Tigris Elektronik GmbH
30649cd0dSSimon Glass  *
40649cd0dSSimon Glass  * Author:
50649cd0dSSimon Glass  *  Maximilian Schwerin <mvs@tigris.de>
60649cd0dSSimon Glass  *
70649cd0dSSimon Glass  * SPDX-License-Identifier:	GPL-2.0+
80649cd0dSSimon Glass  */
90649cd0dSSimon Glass 
100649cd0dSSimon Glass #include <common.h>
110649cd0dSSimon Glass 
120649cd0dSSimon Glass #include <command.h>
130649cd0dSSimon Glass #include <environment.h>
140649cd0dSSimon Glass #include <linux/stddef.h>
150649cd0dSSimon Glass #include <malloc.h>
160649cd0dSSimon Glass #include <memalign.h>
170649cd0dSSimon Glass #include <search.h>
180649cd0dSSimon Glass #include <errno.h>
190649cd0dSSimon Glass #include <fat.h>
200649cd0dSSimon Glass #include <mmc.h>
210649cd0dSSimon Glass 
224415f1d1SSimon Glass #ifdef CONFIG_SPL_BUILD
234415f1d1SSimon Glass /* TODO(sjg@chromium.org): Figure out why this is needed */
244415f1d1SSimon Glass # if !defined(CONFIG_TARGET_AM335X_EVM) || defined(CONFIG_SPL_OS_BOOT)
254415f1d1SSimon Glass #  define LOADENV
264415f1d1SSimon Glass # endif
274415f1d1SSimon Glass #else
284415f1d1SSimon Glass # define LOADENV
294415f1d1SSimon Glass # if defined(CONFIG_CMD_SAVEENV)
304415f1d1SSimon Glass #  define CMD_SAVEENV
314415f1d1SSimon Glass # endif
324415f1d1SSimon Glass #endif
334415f1d1SSimon Glass 
340649cd0dSSimon Glass DECLARE_GLOBAL_DATA_PTR;
350649cd0dSSimon Glass 
364415f1d1SSimon Glass #ifdef CMD_SAVEENV
37e5bce247SSimon Glass static int env_fat_save(void)
380649cd0dSSimon Glass {
39cda87ec5SAlex Kiernan 	env_t __aligned(ARCH_DMA_MINALIGN) env_new;
400649cd0dSSimon Glass 	struct blk_desc *dev_desc = NULL;
410649cd0dSSimon Glass 	disk_partition_t info;
420649cd0dSSimon Glass 	int dev, part;
430649cd0dSSimon Glass 	int err;
440649cd0dSSimon Glass 	loff_t size;
450649cd0dSSimon Glass 
460649cd0dSSimon Glass 	err = env_export(&env_new);
470649cd0dSSimon Glass 	if (err)
480649cd0dSSimon Glass 		return err;
490649cd0dSSimon Glass 
500649cd0dSSimon Glass 	part = blk_get_device_part_str(CONFIG_ENV_FAT_INTERFACE,
510649cd0dSSimon Glass 					CONFIG_ENV_FAT_DEVICE_AND_PART,
520649cd0dSSimon Glass 					&dev_desc, &info, 1);
530649cd0dSSimon Glass 	if (part < 0)
540649cd0dSSimon Glass 		return 1;
550649cd0dSSimon Glass 
560649cd0dSSimon Glass 	dev = dev_desc->devnum;
570649cd0dSSimon Glass 	if (fat_set_blk_dev(dev_desc, &info) != 0) {
58d0816da5SMaxime Ripard 		/*
59d0816da5SMaxime Ripard 		 * This printf is embedded in the messages from env_save that
60d0816da5SMaxime Ripard 		 * will calling it. The missing \n is intentional.
61d0816da5SMaxime Ripard 		 */
62d0816da5SMaxime Ripard 		printf("Unable to use %s %d:%d... ",
630649cd0dSSimon Glass 		       CONFIG_ENV_FAT_INTERFACE, dev, part);
640649cd0dSSimon Glass 		return 1;
650649cd0dSSimon Glass 	}
660649cd0dSSimon Glass 
670649cd0dSSimon Glass 	err = file_fat_write(CONFIG_ENV_FAT_FILE, (void *)&env_new, 0, sizeof(env_t),
680649cd0dSSimon Glass 			     &size);
690649cd0dSSimon Glass 	if (err == -1) {
70d0816da5SMaxime Ripard 		/*
71d0816da5SMaxime Ripard 		 * This printf is embedded in the messages from env_save that
72d0816da5SMaxime Ripard 		 * will calling it. The missing \n is intentional.
73d0816da5SMaxime Ripard 		 */
74d0816da5SMaxime Ripard 		printf("Unable to write \"%s\" from %s%d:%d... ",
750649cd0dSSimon Glass 			CONFIG_ENV_FAT_FILE, CONFIG_ENV_FAT_INTERFACE, dev, part);
760649cd0dSSimon Glass 		return 1;
770649cd0dSSimon Glass 	}
780649cd0dSSimon Glass 
790649cd0dSSimon Glass 	return 0;
800649cd0dSSimon Glass }
814415f1d1SSimon Glass #endif /* CMD_SAVEENV */
820649cd0dSSimon Glass 
834415f1d1SSimon Glass #ifdef LOADENV
84c5951991SSimon Glass static int env_fat_load(void)
850649cd0dSSimon Glass {
860649cd0dSSimon Glass 	ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
870649cd0dSSimon Glass 	struct blk_desc *dev_desc = NULL;
880649cd0dSSimon Glass 	disk_partition_t info;
890649cd0dSSimon Glass 	int dev, part;
900649cd0dSSimon Glass 	int err;
910649cd0dSSimon Glass 
92*95058fbbSHeinrich Schuchardt #ifdef CONFIG_MMC
9326862b4aSFaiz Abbas 	if (!strcmp(CONFIG_ENV_FAT_INTERFACE, "mmc"))
9426862b4aSFaiz Abbas 		mmc_initialize(NULL);
95*95058fbbSHeinrich Schuchardt #endif
9626862b4aSFaiz Abbas 
970649cd0dSSimon Glass 	part = blk_get_device_part_str(CONFIG_ENV_FAT_INTERFACE,
980649cd0dSSimon Glass 					CONFIG_ENV_FAT_DEVICE_AND_PART,
990649cd0dSSimon Glass 					&dev_desc, &info, 1);
1000649cd0dSSimon Glass 	if (part < 0)
1010649cd0dSSimon Glass 		goto err_env_relocate;
1020649cd0dSSimon Glass 
1030649cd0dSSimon Glass 	dev = dev_desc->devnum;
1040649cd0dSSimon Glass 	if (fat_set_blk_dev(dev_desc, &info) != 0) {
105d0816da5SMaxime Ripard 		/*
106d0816da5SMaxime Ripard 		 * This printf is embedded in the messages from env_save that
107d0816da5SMaxime Ripard 		 * will calling it. The missing \n is intentional.
108d0816da5SMaxime Ripard 		 */
109d0816da5SMaxime Ripard 		printf("Unable to use %s %d:%d... ",
1100649cd0dSSimon Glass 		       CONFIG_ENV_FAT_INTERFACE, dev, part);
1110649cd0dSSimon Glass 		goto err_env_relocate;
1120649cd0dSSimon Glass 	}
1130649cd0dSSimon Glass 
1140649cd0dSSimon Glass 	err = file_fat_read(CONFIG_ENV_FAT_FILE, buf, CONFIG_ENV_SIZE);
1150649cd0dSSimon Glass 	if (err == -1) {
116d0816da5SMaxime Ripard 		/*
117d0816da5SMaxime Ripard 		 * This printf is embedded in the messages from env_save that
118d0816da5SMaxime Ripard 		 * will calling it. The missing \n is intentional.
119d0816da5SMaxime Ripard 		 */
120d0816da5SMaxime Ripard 		printf("Unable to read \"%s\" from %s%d:%d... ",
1210649cd0dSSimon Glass 			CONFIG_ENV_FAT_FILE, CONFIG_ENV_FAT_INTERFACE, dev, part);
1220649cd0dSSimon Glass 		goto err_env_relocate;
1230649cd0dSSimon Glass 	}
1240649cd0dSSimon Glass 
1252166ebf7SSimon Goldschmidt 	return env_import(buf, 1);
1260649cd0dSSimon Glass 
1270649cd0dSSimon Glass err_env_relocate:
1280649cd0dSSimon Glass 	set_default_env(NULL);
129c5951991SSimon Glass 
130c5951991SSimon Glass 	return -EIO;
1310649cd0dSSimon Glass }
1324415f1d1SSimon Glass #endif /* LOADENV */
1334415f1d1SSimon Glass 
1344415f1d1SSimon Glass U_BOOT_ENV_LOCATION(fat) = {
1354415f1d1SSimon Glass 	.location	= ENVL_FAT,
136ac358bebSSimon Glass 	ENV_NAME("FAT")
1374415f1d1SSimon Glass #ifdef LOADENV
138e5bce247SSimon Glass 	.load		= env_fat_load,
1394415f1d1SSimon Glass #endif
1404415f1d1SSimon Glass #ifdef CMD_SAVEENV
141e5bce247SSimon Glass 	.save		= env_save_ptr(env_fat_save),
1424415f1d1SSimon Glass #endif
1434415f1d1SSimon Glass };
144