ext4.c 3.0 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0+
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*
 * (c) Copyright 2016 by VRT Technology
 *
 * Author:
 *  Stuart Longland <stuartl@vrt.com.au>
 *
 * Based on FAT environment driver
 * (c) Copyright 2011 by Tigris Elektronik GmbH
 *
 * Author:
 *  Maximilian Schwerin <mvs@tigris.de>
 *
 * and EXT4 filesystem implementation
 * (C) Copyright 2011 - 2012 Samsung Electronics
 * EXT4 filesystem implementation in Uboot by
 * Uma Shankar <uma.shankar@samsung.com>
 * Manjunatha C Achar <a.manjunatha@samsung.com>
 */

#include <common.h>

#include <command.h>
#include <environment.h>
#include <linux/stddef.h>
#include <malloc.h>
27
#include <memalign.h>
28 29 30 31 32
#include <search.h>
#include <errno.h>
#include <ext4fs.h>
#include <mmc.h>

33 34 35 36 37 38 39 40 41 42
__weak const char *env_ext4_get_intf(void)
{
	return (const char *)CONFIG_ENV_EXT4_INTERFACE;
}

__weak const char *env_ext4_get_dev_part(void)
{
	return (const char *)CONFIG_ENV_EXT4_DEVICE_AND_PART;
}

43
#ifdef CONFIG_CMD_SAVEENV
44
static int env_ext4_save(void)
45 46
{
	env_t	env_new;
47
	struct blk_desc *dev_desc = NULL;
48 49 50
	disk_partition_t info;
	int dev, part;
	int err;
51 52
	const char *ifname = env_ext4_get_intf();
	const char *dev_and_part = env_ext4_get_dev_part();
53 54 55 56 57

	err = env_export(&env_new);
	if (err)
		return err;

58
	part = blk_get_device_part_str(ifname, dev_and_part,
59
				       &dev_desc, &info, 1);
60 61 62
	if (part < 0)
		return 1;

63
	dev = dev_desc->devnum;
64 65 66 67
	ext4fs_set_blk_dev(dev_desc, &info);

	if (!ext4fs_mount(info.size)) {
		printf("\n** Unable to use %s %s for saveenv **\n",
68
		       ifname, dev_and_part);
69 70 71
		return 1;
	}

72
	err = ext4fs_write(CONFIG_ENV_EXT4_FILE, (void *)&env_new,
73
			   sizeof(env_t), FILETYPE_REG);
74 75 76 77
	ext4fs_close();

	if (err == -1) {
		printf("\n** Unable to write \"%s\" from %s%d:%d **\n",
78
			CONFIG_ENV_EXT4_FILE, ifname, dev, part);
79 80 81 82 83 84 85 86
		return 1;
	}

	puts("done\n");
	return 0;
}
#endif /* CONFIG_CMD_SAVEENV */

87
static int env_ext4_load(void)
88 89
{
	ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
90
	struct blk_desc *dev_desc = NULL;
91 92 93
	disk_partition_t info;
	int dev, part;
	int err;
94
	loff_t off;
95 96
	const char *ifname = env_ext4_get_intf();
	const char *dev_and_part = env_ext4_get_dev_part();
97

98
#ifdef CONFIG_MMC
99
	if (!strcmp(ifname, "mmc"))
100
		mmc_initialize(NULL);
101
#endif
102

103
	part = blk_get_device_part_str(ifname, dev_and_part,
104
				       &dev_desc, &info, 1);
105 106 107
	if (part < 0)
		goto err_env_relocate;

108
	dev = dev_desc->devnum;
109 110 111 112
	ext4fs_set_blk_dev(dev_desc, &info);

	if (!ext4fs_mount(info.size)) {
		printf("\n** Unable to use %s %s for loading the env **\n",
113
		       ifname, dev_and_part);
114 115 116
		goto err_env_relocate;
	}

117 118
	err = ext4_read_file(CONFIG_ENV_EXT4_FILE, buf, 0, CONFIG_ENV_SIZE,
			     &off);
119 120 121 122
	ext4fs_close();

	if (err == -1) {
		printf("\n** Unable to read \"%s\" from %s%d:%d **\n",
123
			CONFIG_ENV_EXT4_FILE, ifname, dev, part);
124 125 126
		goto err_env_relocate;
	}

127
	return env_import(buf, 1);
128 129

err_env_relocate:
130
	set_default_env(NULL, 0);
131 132

	return -EIO;
133
}
134 135 136

U_BOOT_ENV_LOCATION(ext4) = {
	.location	= ENVL_EXT4,
S
Simon Glass 已提交
137
	ENV_NAME("EXT4")
138 139
	.load		= env_ext4_load,
	.save		= env_save_ptr(env_ext4_save),
140
};