ext4.c 2.8 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 33
#include <search.h>
#include <errno.h>
#include <ext4fs.h>
#include <mmc.h>

#ifdef CONFIG_CMD_SAVEENV
34
static int env_ext4_save(void)
35 36
{
	env_t	env_new;
37
	struct blk_desc *dev_desc = NULL;
38 39 40 41 42 43 44 45
	disk_partition_t info;
	int dev, part;
	int err;

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

46 47 48
	part = blk_get_device_part_str(CONFIG_ENV_EXT4_INTERFACE,
				       CONFIG_ENV_EXT4_DEVICE_AND_PART,
				       &dev_desc, &info, 1);
49 50 51
	if (part < 0)
		return 1;

52
	dev = dev_desc->devnum;
53 54 55 56
	ext4fs_set_blk_dev(dev_desc, &info);

	if (!ext4fs_mount(info.size)) {
		printf("\n** Unable to use %s %s for saveenv **\n",
57 58
		       CONFIG_ENV_EXT4_INTERFACE,
		       CONFIG_ENV_EXT4_DEVICE_AND_PART);
59 60 61
		return 1;
	}

62
	err = ext4fs_write(CONFIG_ENV_EXT4_FILE, (void *)&env_new,
63
			   sizeof(env_t), FILETYPE_REG);
64 65 66 67
	ext4fs_close();

	if (err == -1) {
		printf("\n** Unable to write \"%s\" from %s%d:%d **\n",
68 69
			CONFIG_ENV_EXT4_FILE, CONFIG_ENV_EXT4_INTERFACE, dev,
			part);
70 71 72 73 74 75 76 77
		return 1;
	}

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

78
static int env_ext4_load(void)
79 80
{
	ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
81
	struct blk_desc *dev_desc = NULL;
82 83 84
	disk_partition_t info;
	int dev, part;
	int err;
85
	loff_t off;
86

87
#ifdef CONFIG_MMC
88 89
	if (!strcmp(CONFIG_ENV_EXT4_INTERFACE, "mmc"))
		mmc_initialize(NULL);
90
#endif
91

92 93 94
	part = blk_get_device_part_str(CONFIG_ENV_EXT4_INTERFACE,
				       CONFIG_ENV_EXT4_DEVICE_AND_PART,
				       &dev_desc, &info, 1);
95 96 97
	if (part < 0)
		goto err_env_relocate;

98
	dev = dev_desc->devnum;
99 100 101 102
	ext4fs_set_blk_dev(dev_desc, &info);

	if (!ext4fs_mount(info.size)) {
		printf("\n** Unable to use %s %s for loading the env **\n",
103 104
		       CONFIG_ENV_EXT4_INTERFACE,
		       CONFIG_ENV_EXT4_DEVICE_AND_PART);
105 106 107
		goto err_env_relocate;
	}

108 109
	err = ext4_read_file(CONFIG_ENV_EXT4_FILE, buf, 0, CONFIG_ENV_SIZE,
			     &off);
110 111 112 113
	ext4fs_close();

	if (err == -1) {
		printf("\n** Unable to read \"%s\" from %s%d:%d **\n",
114 115
			CONFIG_ENV_EXT4_FILE, CONFIG_ENV_EXT4_INTERFACE, dev,
			part);
116 117 118
		goto err_env_relocate;
	}

119
	return env_import(buf, 1);
120 121

err_env_relocate:
122
	set_default_env(NULL, 0);
123 124

	return -EIO;
125
}
126 127 128

U_BOOT_ENV_LOCATION(ext4) = {
	.location	= ENVL_EXT4,
S
Simon Glass 已提交
129
	ENV_NAME("EXT4")
130 131
	.load		= env_ext4_load,
	.save		= env_save_ptr(env_ext4_save),
132
};