2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2012-03-12 23:57:50 +00:00
|
|
|
/*
|
|
|
|
* (c) Copyright 2011 by Tigris Elektronik GmbH
|
|
|
|
*
|
|
|
|
* Author:
|
|
|
|
* Maximilian Schwerin <mvs@tigris.de>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
|
|
|
#include <command.h>
|
2019-08-01 15:47:00 +00:00
|
|
|
#include <env.h>
|
2019-08-02 15:44:25 +00:00
|
|
|
#include <env_internal.h>
|
2020-05-10 17:39:58 +00:00
|
|
|
#include <part.h>
|
2012-03-12 23:57:50 +00:00
|
|
|
#include <malloc.h>
|
2015-09-02 23:24:58 +00:00
|
|
|
#include <memalign.h>
|
2012-03-12 23:57:50 +00:00
|
|
|
#include <search.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fat.h>
|
|
|
|
#include <mmc.h>
|
2020-05-10 17:39:58 +00:00
|
|
|
#include <asm/cache.h>
|
|
|
|
#include <linux/stddef.h>
|
2012-03-12 23:57:50 +00:00
|
|
|
|
2017-08-03 18:21:58 +00:00
|
|
|
#ifdef CONFIG_SPL_BUILD
|
|
|
|
/* TODO(sjg@chromium.org): Figure out why this is needed */
|
|
|
|
# if !defined(CONFIG_TARGET_AM335X_EVM) || defined(CONFIG_SPL_OS_BOOT)
|
|
|
|
# define LOADENV
|
|
|
|
# endif
|
|
|
|
#else
|
|
|
|
# define LOADENV
|
|
|
|
#endif
|
|
|
|
|
2020-06-19 22:07:17 +00:00
|
|
|
__weak int mmc_get_env_dev(void)
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_SYS_MMC_ENV_DEV
|
|
|
|
return CONFIG_SYS_MMC_ENV_DEV;
|
|
|
|
#else
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *env_fat_device_and_part(void)
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_MMC
|
|
|
|
static char *part_str;
|
|
|
|
|
|
|
|
if (!part_str) {
|
|
|
|
part_str = CONFIG_ENV_FAT_DEVICE_AND_PART;
|
|
|
|
if (!strcmp(CONFIG_ENV_FAT_INTERFACE, "mmc") && part_str[0] == ':') {
|
|
|
|
part_str = "0" CONFIG_ENV_FAT_DEVICE_AND_PART;
|
|
|
|
part_str[0] += mmc_get_env_dev();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return part_str;
|
|
|
|
#else
|
|
|
|
return CONFIG_ENV_FAT_DEVICE_AND_PART;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-08-03 18:22:01 +00:00
|
|
|
static int env_fat_save(void)
|
2012-03-12 23:57:50 +00:00
|
|
|
{
|
2018-02-07 20:01:54 +00:00
|
|
|
env_t __aligned(ARCH_DMA_MINALIGN) env_new;
|
2016-02-29 22:25:34 +00:00
|
|
|
struct blk_desc *dev_desc = NULL;
|
2020-05-10 17:39:57 +00:00
|
|
|
struct disk_partition info;
|
2014-06-24 09:31:02 +00:00
|
|
|
int dev, part;
|
2012-09-23 05:25:21 +00:00
|
|
|
int err;
|
2014-11-17 22:39:35 +00:00
|
|
|
loff_t size;
|
2012-03-12 23:57:50 +00:00
|
|
|
|
2014-03-05 18:59:50 +00:00
|
|
|
err = env_export(&env_new);
|
|
|
|
if (err)
|
|
|
|
return err;
|
2012-03-12 23:57:50 +00:00
|
|
|
|
2017-07-27 01:48:00 +00:00
|
|
|
part = blk_get_device_part_str(CONFIG_ENV_FAT_INTERFACE,
|
2020-06-19 22:07:17 +00:00
|
|
|
env_fat_device_and_part(),
|
2014-06-24 09:31:02 +00:00
|
|
|
&dev_desc, &info, 1);
|
|
|
|
if (part < 0)
|
2012-03-12 23:57:50 +00:00
|
|
|
return 1;
|
2012-09-23 05:25:21 +00:00
|
|
|
|
2016-02-29 22:25:51 +00:00
|
|
|
dev = dev_desc->devnum;
|
2014-06-24 09:31:02 +00:00
|
|
|
if (fat_set_blk_dev(dev_desc, &info) != 0) {
|
2018-01-23 20:16:55 +00:00
|
|
|
/*
|
|
|
|
* This printf is embedded in the messages from env_save that
|
|
|
|
* will calling it. The missing \n is intentional.
|
|
|
|
*/
|
|
|
|
printf("Unable to use %s %d:%d... ",
|
2017-07-27 01:48:00 +00:00
|
|
|
CONFIG_ENV_FAT_INTERFACE, dev, part);
|
2012-03-12 23:57:50 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-07-27 01:48:00 +00:00
|
|
|
err = file_fat_write(CONFIG_ENV_FAT_FILE, (void *)&env_new, 0, sizeof(env_t),
|
2014-11-17 22:39:35 +00:00
|
|
|
&size);
|
2012-09-23 05:25:21 +00:00
|
|
|
if (err == -1) {
|
2018-01-23 20:16:55 +00:00
|
|
|
/*
|
|
|
|
* This printf is embedded in the messages from env_save that
|
|
|
|
* will calling it. The missing \n is intentional.
|
|
|
|
*/
|
|
|
|
printf("Unable to write \"%s\" from %s%d:%d... ",
|
2017-07-27 01:48:00 +00:00
|
|
|
CONFIG_ENV_FAT_FILE, CONFIG_ENV_FAT_INTERFACE, dev, part);
|
2012-03-12 23:57:50 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-08-03 18:21:58 +00:00
|
|
|
#ifdef LOADENV
|
2017-08-03 18:22:17 +00:00
|
|
|
static int env_fat_load(void)
|
2012-03-12 23:57:50 +00:00
|
|
|
{
|
2014-08-01 17:59:10 +00:00
|
|
|
ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
|
2016-02-29 22:25:34 +00:00
|
|
|
struct blk_desc *dev_desc = NULL;
|
2020-05-10 17:39:57 +00:00
|
|
|
struct disk_partition info;
|
2014-06-24 09:31:02 +00:00
|
|
|
int dev, part;
|
2012-09-23 05:25:21 +00:00
|
|
|
int err;
|
2012-03-12 23:57:50 +00:00
|
|
|
|
2018-04-14 13:41:00 +00:00
|
|
|
#ifdef CONFIG_MMC
|
2018-02-12 13:54:31 +00:00
|
|
|
if (!strcmp(CONFIG_ENV_FAT_INTERFACE, "mmc"))
|
|
|
|
mmc_initialize(NULL);
|
2018-04-14 13:41:00 +00:00
|
|
|
#endif
|
2018-02-12 13:54:31 +00:00
|
|
|
|
2017-07-27 01:48:00 +00:00
|
|
|
part = blk_get_device_part_str(CONFIG_ENV_FAT_INTERFACE,
|
2020-06-19 22:07:17 +00:00
|
|
|
env_fat_device_and_part(),
|
2014-06-24 09:31:02 +00:00
|
|
|
&dev_desc, &info, 1);
|
|
|
|
if (part < 0)
|
|
|
|
goto err_env_relocate;
|
|
|
|
|
2016-02-29 22:25:51 +00:00
|
|
|
dev = dev_desc->devnum;
|
2014-06-24 09:31:02 +00:00
|
|
|
if (fat_set_blk_dev(dev_desc, &info) != 0) {
|
2018-01-23 20:16:55 +00:00
|
|
|
/*
|
|
|
|
* This printf is embedded in the messages from env_save that
|
|
|
|
* will calling it. The missing \n is intentional.
|
|
|
|
*/
|
|
|
|
printf("Unable to use %s %d:%d... ",
|
2017-07-27 01:48:00 +00:00
|
|
|
CONFIG_ENV_FAT_INTERFACE, dev, part);
|
2014-06-24 09:31:02 +00:00
|
|
|
goto err_env_relocate;
|
2012-03-12 23:57:50 +00:00
|
|
|
}
|
|
|
|
|
2017-07-27 01:48:00 +00:00
|
|
|
err = file_fat_read(CONFIG_ENV_FAT_FILE, buf, CONFIG_ENV_SIZE);
|
2012-09-23 05:25:21 +00:00
|
|
|
if (err == -1) {
|
2018-01-23 20:16:55 +00:00
|
|
|
/*
|
|
|
|
* This printf is embedded in the messages from env_save that
|
|
|
|
* will calling it. The missing \n is intentional.
|
|
|
|
*/
|
|
|
|
printf("Unable to read \"%s\" from %s%d:%d... ",
|
2017-07-27 01:48:00 +00:00
|
|
|
CONFIG_ENV_FAT_FILE, CONFIG_ENV_FAT_INTERFACE, dev, part);
|
2014-06-24 09:31:02 +00:00
|
|
|
goto err_env_relocate;
|
2012-03-12 23:57:50 +00:00
|
|
|
}
|
|
|
|
|
2020-07-07 18:51:35 +00:00
|
|
|
return env_import(buf, 1, H_EXTERNAL);
|
2014-06-24 09:31:02 +00:00
|
|
|
|
|
|
|
err_env_relocate:
|
2019-08-01 15:47:00 +00:00
|
|
|
env_set_default(NULL, 0);
|
2017-08-03 18:22:17 +00:00
|
|
|
|
|
|
|
return -EIO;
|
2012-03-12 23:57:50 +00:00
|
|
|
}
|
2017-08-03 18:21:58 +00:00
|
|
|
#endif /* LOADENV */
|
|
|
|
|
|
|
|
U_BOOT_ENV_LOCATION(fat) = {
|
|
|
|
.location = ENVL_FAT,
|
2017-08-03 18:22:03 +00:00
|
|
|
ENV_NAME("FAT")
|
2017-08-03 18:21:58 +00:00
|
|
|
#ifdef LOADENV
|
2017-08-03 18:22:01 +00:00
|
|
|
.load = env_fat_load,
|
2017-08-03 18:21:58 +00:00
|
|
|
#endif
|
2020-02-19 09:47:41 +00:00
|
|
|
.save = ENV_SAVE_PTR(env_fat_save),
|
2017-08-03 18:21:58 +00:00
|
|
|
};
|