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>
|
2022-05-11 21:20:28 +00:00
|
|
|
#include <scsi.h>
|
2020-05-10 17:39:58 +00:00
|
|
|
#include <asm/cache.h>
|
2021-01-16 21:14:43 +00:00
|
|
|
#include <asm/global_data.h>
|
2020-05-10 17:39:58 +00:00
|
|
|
#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
|
|
|
|
|
2021-01-16 21:14:43 +00:00
|
|
|
DECLARE_GLOBAL_DATA_PTR;
|
|
|
|
|
2022-02-17 16:07:25 +00:00
|
|
|
__weak const char *env_fat_get_intf(void)
|
|
|
|
{
|
|
|
|
return (const char *)CONFIG_ENV_FAT_INTERFACE;
|
|
|
|
}
|
|
|
|
|
|
|
|
__weak char *env_fat_get_dev_part(void)
|
2020-06-19 22:07:17 +00:00
|
|
|
{
|
|
|
|
#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;
|
2021-01-16 21:14:43 +00:00
|
|
|
const char *file = CONFIG_ENV_FAT_FILE;
|
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;
|
2022-02-17 16:07:25 +00:00
|
|
|
const char *ifname = env_fat_get_intf();
|
|
|
|
const char *dev_and_part = env_fat_get_dev_part();
|
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
|
|
|
|
2022-02-17 16:07:25 +00:00
|
|
|
part = blk_get_device_part_str(ifname, dev_and_part,
|
|
|
|
&dev_desc, &info, 1);
|
2014-06-24 09:31:02 +00:00
|
|
|
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.
|
|
|
|
*/
|
2022-02-17 16:07:25 +00:00
|
|
|
printf("Unable to use %s %d:%d...\n", ifname, dev, part);
|
2012-03-12 23:57:50 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-01-16 21:14:43 +00:00
|
|
|
#ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
|
|
|
|
if (gd->env_valid == ENV_VALID)
|
|
|
|
file = CONFIG_ENV_FAT_FILE_REDUND;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
err = file_fat_write(file, (void *)&env_new, 0, sizeof(env_t), &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.
|
|
|
|
*/
|
2022-02-17 16:07:25 +00:00
|
|
|
printf("Unable to write \"%s\" from %s%d:%d...\n", file, ifname, dev, part);
|
2012-03-12 23:57:50 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-01-16 21:14:43 +00:00
|
|
|
#ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
|
|
|
|
gd->env_valid = (gd->env_valid == ENV_REDUND) ? ENV_VALID : ENV_REDUND;
|
|
|
|
#endif
|
|
|
|
|
2012-03-12 23:57:50 +00:00
|
|
|
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
|
|
|
{
|
2021-01-16 21:14:43 +00:00
|
|
|
ALLOC_CACHE_ALIGN_BUFFER(char, buf1, CONFIG_ENV_SIZE);
|
|
|
|
#ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
|
|
|
|
ALLOC_CACHE_ALIGN_BUFFER(char, buf2, CONFIG_ENV_SIZE);
|
|
|
|
int err2;
|
|
|
|
#endif
|
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;
|
2021-01-16 21:14:43 +00:00
|
|
|
int err1;
|
2022-02-17 16:07:25 +00:00
|
|
|
const char *ifname = env_fat_get_intf();
|
|
|
|
const char *dev_and_part = env_fat_get_dev_part();
|
2012-03-12 23:57:50 +00:00
|
|
|
|
2018-04-14 13:41:00 +00:00
|
|
|
#ifdef CONFIG_MMC
|
2022-02-17 16:07:25 +00:00
|
|
|
if (!strcmp(ifname, "mmc"))
|
2018-02-12 13:54:31 +00:00
|
|
|
mmc_initialize(NULL);
|
2018-04-14 13:41:00 +00:00
|
|
|
#endif
|
2022-05-11 21:20:28 +00:00
|
|
|
#ifndef CONFIG_SPL_BUILD
|
|
|
|
#if defined(CONFIG_AHCI) || defined(CONFIG_SCSI)
|
|
|
|
if (!strcmp(CONFIG_ENV_FAT_INTERFACE, "scsi"))
|
|
|
|
scsi_scan(true);
|
|
|
|
#endif
|
|
|
|
#endif
|
2022-02-17 16:07:25 +00:00
|
|
|
part = blk_get_device_part_str(ifname, dev_and_part,
|
|
|
|
&dev_desc, &info, 1);
|
2014-06-24 09:31:02 +00:00
|
|
|
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.
|
|
|
|
*/
|
2022-02-17 16:07:25 +00:00
|
|
|
printf("Unable to use %s %d:%d...\n", ifname, dev, part);
|
2014-06-24 09:31:02 +00:00
|
|
|
goto err_env_relocate;
|
2012-03-12 23:57:50 +00:00
|
|
|
}
|
|
|
|
|
2021-01-16 21:14:43 +00:00
|
|
|
err1 = file_fat_read(CONFIG_ENV_FAT_FILE, buf1, CONFIG_ENV_SIZE);
|
|
|
|
#ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
|
|
|
|
err2 = file_fat_read(CONFIG_ENV_FAT_FILE_REDUND, buf2, CONFIG_ENV_SIZE);
|
|
|
|
|
|
|
|
err1 = (err1 >= 0) ? 0 : -1;
|
|
|
|
err2 = (err2 >= 0) ? 0 : -1;
|
|
|
|
return env_import_redund(buf1, err1, buf2, err2, H_EXTERNAL);
|
|
|
|
#else
|
|
|
|
if (err1 < 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.
|
|
|
|
*/
|
2022-01-02 11:38:35 +00:00
|
|
|
printf("Unable to read \"%s\" from %s%d:%d... \n",
|
2022-02-17 16:07:25 +00:00
|
|
|
CONFIG_ENV_FAT_FILE, ifname, dev, part);
|
2014-06-24 09:31:02 +00:00
|
|
|
goto err_env_relocate;
|
2012-03-12 23:57:50 +00:00
|
|
|
}
|
|
|
|
|
2021-01-16 21:14:43 +00:00
|
|
|
return env_import(buf1, 1, H_EXTERNAL);
|
|
|
|
#endif
|
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
|
|
|
};
|