When U-Boot is booting from a fresh device, the environment volume may not exist in the factory UBI image. This is a common case where factory UBI image contains only volumes with valid data. With the current design, even if the volume is created manually, the environment will still be unusable (e.g., saveenv) before a reboot. This patch adds support to automatically create missing volumes before loading environment. This will make environment available at first boot. There are two options: CONFIG_ENV_UBI_VOLUME_CREATE: whether to enable volume creation CONFIG_ENV_UBI_VOLUME_STATIC: create static volume (default is dynamic) Reviewed-by: Simon Glass <sjg@chromium.org> Signed-off-by: Weijie Gao <weijie.gao@mediatek.com>
266 lines
6.6 KiB
C
266 lines
6.6 KiB
C
// SPDX-License-Identifier: GPL-2.0+
|
|
/*
|
|
* (c) Copyright 2012 by National Instruments,
|
|
* Joe Hershberger <joe.hershberger@ni.com>
|
|
*/
|
|
|
|
#include <asm/global_data.h>
|
|
|
|
#include <command.h>
|
|
#include <env.h>
|
|
#include <env_internal.h>
|
|
#include <errno.h>
|
|
#include <malloc.h>
|
|
#include <memalign.h>
|
|
#include <search.h>
|
|
#include <ubi_uboot.h>
|
|
#undef crc32
|
|
|
|
#define _QUOTE(x) #x
|
|
#define QUOTE(x) _QUOTE(x)
|
|
|
|
#if (CONFIG_ENV_UBI_VID_OFFSET == 0)
|
|
#define UBI_VID_OFFSET NULL
|
|
#else
|
|
#define UBI_VID_OFFSET QUOTE(CONFIG_ENV_UBI_VID_OFFSET)
|
|
#endif
|
|
|
|
DECLARE_GLOBAL_DATA_PTR;
|
|
|
|
#if CONFIG_ENV_REDUNDANT
|
|
#define ENV_UBI_VOLUME_REDUND CONFIG_ENV_UBI_VOLUME_REDUND
|
|
#else
|
|
#define ENV_UBI_VOLUME_REDUND "invalid"
|
|
#endif
|
|
|
|
#ifdef CONFIG_ENV_REDUNDANT
|
|
static int env_ubi_save(void)
|
|
{
|
|
ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
|
|
int ret;
|
|
|
|
ret = env_export(env_new);
|
|
if (ret)
|
|
return ret;
|
|
|
|
if (ubi_part(CONFIG_ENV_UBI_PART, UBI_VID_OFFSET)) {
|
|
printf("\n** Cannot find mtd partition \"%s\"\n",
|
|
CONFIG_ENV_UBI_PART);
|
|
return 1;
|
|
}
|
|
|
|
if (gd->env_valid == ENV_VALID) {
|
|
puts("Writing to redundant UBI... ");
|
|
if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME_REDUND,
|
|
(void *)env_new, 0, CONFIG_ENV_SIZE)) {
|
|
printf("\n** Unable to write env to %s:%s **\n",
|
|
CONFIG_ENV_UBI_PART,
|
|
CONFIG_ENV_UBI_VOLUME_REDUND);
|
|
return 1;
|
|
}
|
|
} else {
|
|
puts("Writing to UBI... ");
|
|
if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME,
|
|
(void *)env_new, 0, CONFIG_ENV_SIZE)) {
|
|
printf("\n** Unable to write env to %s:%s **\n",
|
|
CONFIG_ENV_UBI_PART,
|
|
CONFIG_ENV_UBI_VOLUME);
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
puts("done\n");
|
|
|
|
gd->env_valid = gd->env_valid == ENV_VALID ? ENV_REDUND : ENV_VALID;
|
|
|
|
return 0;
|
|
}
|
|
#else /* ! CONFIG_ENV_REDUNDANT */
|
|
static int env_ubi_save(void)
|
|
{
|
|
ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
|
|
int ret;
|
|
|
|
ret = env_export(env_new);
|
|
if (ret)
|
|
return ret;
|
|
|
|
if (ubi_part(CONFIG_ENV_UBI_PART, UBI_VID_OFFSET)) {
|
|
printf("\n** Cannot find mtd partition \"%s\"\n",
|
|
CONFIG_ENV_UBI_PART);
|
|
return 1;
|
|
}
|
|
|
|
if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME, (void *)env_new, 0,
|
|
CONFIG_ENV_SIZE)) {
|
|
printf("\n** Unable to write env to %s:%s **\n",
|
|
CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME);
|
|
return 1;
|
|
}
|
|
|
|
puts("done\n");
|
|
return 0;
|
|
}
|
|
#endif /* CONFIG_ENV_REDUNDANT */
|
|
|
|
static int env_ubi_volume_create(const char *volume)
|
|
{
|
|
bool dynamic = !IS_ENABLED(CONFIG_ENV_UBI_VOLUME_STATIC);
|
|
struct ubi_volume *vol;
|
|
int ret;
|
|
|
|
vol = ubi_find_volume(volume);
|
|
if (vol)
|
|
return 0;
|
|
|
|
ret = ubi_create_vol(volume, CONFIG_ENV_SIZE, dynamic, UBI_VOL_NUM_AUTO,
|
|
false);
|
|
if (ret)
|
|
printf("Failed to create environment volume '%s'\n", volume);
|
|
|
|
return ret;
|
|
}
|
|
|
|
#ifdef CONFIG_ENV_REDUNDANT
|
|
static int env_ubi_load(void)
|
|
{
|
|
ALLOC_CACHE_ALIGN_BUFFER(char, env1_buf, CONFIG_ENV_SIZE);
|
|
ALLOC_CACHE_ALIGN_BUFFER(char, env2_buf, CONFIG_ENV_SIZE);
|
|
int read1_fail, read2_fail, create1_fail = 0, create2_fail = 0;
|
|
env_t *tmp_env1, *tmp_env2;
|
|
|
|
/*
|
|
* In case we have restarted u-boot there is a chance that buffer
|
|
* contains old environment (from the previous boot).
|
|
* If UBI volume is zero size, ubi_volume_read() doesn't modify the
|
|
* buffer.
|
|
* We need to clear buffer manually here, so the invalid CRC will
|
|
* cause setting default environment as expected.
|
|
*/
|
|
memset(env1_buf, 0x0, CONFIG_ENV_SIZE);
|
|
memset(env2_buf, 0x0, CONFIG_ENV_SIZE);
|
|
|
|
tmp_env1 = (env_t *)env1_buf;
|
|
tmp_env2 = (env_t *)env2_buf;
|
|
|
|
if (ubi_part(CONFIG_ENV_UBI_PART, UBI_VID_OFFSET)) {
|
|
printf("\n** Cannot find mtd partition \"%s\"\n",
|
|
CONFIG_ENV_UBI_PART);
|
|
env_set_default(NULL, 0);
|
|
return -EIO;
|
|
}
|
|
|
|
if (IS_ENABLED(CONFIG_ENV_UBI_VOLUME_CREATE)) {
|
|
create1_fail = env_ubi_volume_create(CONFIG_ENV_UBI_VOLUME);
|
|
create2_fail = env_ubi_volume_create(CONFIG_ENV_UBI_VOLUME_REDUND);
|
|
if (create1_fail && create2_fail) {
|
|
env_set_default(NULL, 0);
|
|
return -ENODEV;
|
|
}
|
|
}
|
|
|
|
if (!create1_fail) {
|
|
read1_fail = ubi_volume_read(CONFIG_ENV_UBI_VOLUME, tmp_env1, 0,
|
|
CONFIG_ENV_SIZE);
|
|
if (read1_fail)
|
|
printf("\n** Unable to read env from %s:%s **\n",
|
|
CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME);
|
|
} else {
|
|
read1_fail = create1_fail;
|
|
}
|
|
|
|
if (!create2_fail) {
|
|
read2_fail = ubi_volume_read(CONFIG_ENV_UBI_VOLUME_REDUND,
|
|
tmp_env2, 0, CONFIG_ENV_SIZE);
|
|
if (read2_fail)
|
|
printf("\n** Unable to read redundant env from %s:%s **\n",
|
|
CONFIG_ENV_UBI_PART,
|
|
CONFIG_ENV_UBI_VOLUME_REDUND);
|
|
} else {
|
|
read2_fail = create2_fail;
|
|
}
|
|
|
|
return env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2,
|
|
read2_fail, H_EXTERNAL);
|
|
}
|
|
#else /* ! CONFIG_ENV_REDUNDANT */
|
|
static int env_ubi_load(void)
|
|
{
|
|
ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
|
|
|
|
/*
|
|
* In case we have restarted u-boot there is a chance that buffer
|
|
* contains old environment (from the previous boot).
|
|
* If UBI volume is zero size, ubi_volume_read() doesn't modify the
|
|
* buffer.
|
|
* We need to clear buffer manually here, so the invalid CRC will
|
|
* cause setting default environment as expected.
|
|
*/
|
|
memset(buf, 0x0, CONFIG_ENV_SIZE);
|
|
|
|
if (ubi_part(CONFIG_ENV_UBI_PART, UBI_VID_OFFSET)) {
|
|
printf("\n** Cannot find mtd partition \"%s\"\n",
|
|
CONFIG_ENV_UBI_PART);
|
|
env_set_default(NULL, 0);
|
|
return -EIO;
|
|
}
|
|
|
|
if (IS_ENABLED(CONFIG_ENV_UBI_VOLUME_CREATE)) {
|
|
if (env_ubi_volume_create(CONFIG_ENV_UBI_VOLUME)) {
|
|
env_set_default(NULL, 0);
|
|
return -ENODEV;
|
|
}
|
|
}
|
|
|
|
if (ubi_volume_read(CONFIG_ENV_UBI_VOLUME, buf, 0, CONFIG_ENV_SIZE)) {
|
|
printf("\n** Unable to read env from %s:%s **\n",
|
|
CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME);
|
|
env_set_default(NULL, 0);
|
|
return -EIO;
|
|
}
|
|
|
|
return env_import(buf, 1, H_EXTERNAL);
|
|
}
|
|
#endif /* CONFIG_ENV_REDUNDANT */
|
|
|
|
static int env_ubi_erase(void)
|
|
{
|
|
ALLOC_CACHE_ALIGN_BUFFER(char, env_buf, CONFIG_ENV_SIZE);
|
|
int ret = 0;
|
|
|
|
if (ubi_part(CONFIG_ENV_UBI_PART, UBI_VID_OFFSET)) {
|
|
printf("\n** Cannot find mtd partition \"%s\"\n",
|
|
CONFIG_ENV_UBI_PART);
|
|
return 1;
|
|
}
|
|
|
|
memset(env_buf, 0x0, CONFIG_ENV_SIZE);
|
|
|
|
if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME,
|
|
(void *)env_buf, 0, CONFIG_ENV_SIZE)) {
|
|
printf("\n** Unable to erase env to %s:%s **\n",
|
|
CONFIG_ENV_UBI_PART,
|
|
CONFIG_ENV_UBI_VOLUME);
|
|
ret = 1;
|
|
}
|
|
if (IS_ENABLED(CONFIG_ENV_REDUNDANT)) {
|
|
if (ubi_volume_write(ENV_UBI_VOLUME_REDUND,
|
|
(void *)env_buf, 0, CONFIG_ENV_SIZE)) {
|
|
printf("\n** Unable to erase env to %s:%s **\n",
|
|
CONFIG_ENV_UBI_PART,
|
|
ENV_UBI_VOLUME_REDUND);
|
|
ret = 1;
|
|
}
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
U_BOOT_ENV_LOCATION(ubi) = {
|
|
.location = ENVL_UBI,
|
|
ENV_NAME("UBI")
|
|
.load = env_ubi_load,
|
|
.save = ENV_SAVE_PTR(env_ubi_save),
|
|
.erase = ENV_ERASE_PTR(env_ubi_erase),
|
|
};
|