xref: /openbmc/u-boot/env/ext4.c (revision d024236e5a31a2b4b82cbcc98b31b8170fc88d28)
1 /*
2  * (c) Copyright 2016 by VRT Technology
3  *
4  * Author:
5  *  Stuart Longland <stuartl@vrt.com.au>
6  *
7  * Based on FAT environment driver
8  * (c) Copyright 2011 by Tigris Elektronik GmbH
9  *
10  * Author:
11  *  Maximilian Schwerin <mvs@tigris.de>
12  *
13  * and EXT4 filesystem implementation
14  * (C) Copyright 2011 - 2012 Samsung Electronics
15  * EXT4 filesystem implementation in Uboot by
16  * Uma Shankar <uma.shankar@samsung.com>
17  * Manjunatha C Achar <a.manjunatha@samsung.com>
18  *
19  * SPDX-License-Identifier:	GPL-2.0+
20  */
21 
22 #include <common.h>
23 
24 #include <command.h>
25 #include <environment.h>
26 #include <linux/stddef.h>
27 #include <malloc.h>
28 #include <memalign.h>
29 #include <search.h>
30 #include <errno.h>
31 #include <ext4fs.h>
32 #include <mmc.h>
33 
34 #ifdef CONFIG_CMD_SAVEENV
35 static int env_ext4_save(void)
36 {
37 	env_t	env_new;
38 	struct blk_desc *dev_desc = NULL;
39 	disk_partition_t info;
40 	int dev, part;
41 	int err;
42 
43 	err = env_export(&env_new);
44 	if (err)
45 		return err;
46 
47 	part = blk_get_device_part_str(CONFIG_ENV_EXT4_INTERFACE,
48 				       CONFIG_ENV_EXT4_DEVICE_AND_PART,
49 				       &dev_desc, &info, 1);
50 	if (part < 0)
51 		return 1;
52 
53 	dev = dev_desc->devnum;
54 	ext4fs_set_blk_dev(dev_desc, &info);
55 
56 	if (!ext4fs_mount(info.size)) {
57 		printf("\n** Unable to use %s %s for saveenv **\n",
58 		       CONFIG_ENV_EXT4_INTERFACE,
59 		       CONFIG_ENV_EXT4_DEVICE_AND_PART);
60 		return 1;
61 	}
62 
63 	err = ext4fs_write(CONFIG_ENV_EXT4_FILE, (void *)&env_new,
64 			   sizeof(env_t));
65 	ext4fs_close();
66 
67 	if (err == -1) {
68 		printf("\n** Unable to write \"%s\" from %s%d:%d **\n",
69 			CONFIG_ENV_EXT4_FILE, CONFIG_ENV_EXT4_INTERFACE, dev,
70 			part);
71 		return 1;
72 	}
73 
74 	puts("done\n");
75 	return 0;
76 }
77 #endif /* CONFIG_CMD_SAVEENV */
78 
79 static int env_ext4_load(void)
80 {
81 	ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
82 	struct blk_desc *dev_desc = NULL;
83 	disk_partition_t info;
84 	int dev, part;
85 	int err;
86 	loff_t off;
87 
88 #ifdef CONFIG_MMC
89 	if (!strcmp(CONFIG_ENV_EXT4_INTERFACE, "mmc"))
90 		mmc_initialize(NULL);
91 #endif
92 
93 	part = blk_get_device_part_str(CONFIG_ENV_EXT4_INTERFACE,
94 				       CONFIG_ENV_EXT4_DEVICE_AND_PART,
95 				       &dev_desc, &info, 1);
96 	if (part < 0)
97 		goto err_env_relocate;
98 
99 	dev = dev_desc->devnum;
100 	ext4fs_set_blk_dev(dev_desc, &info);
101 
102 	if (!ext4fs_mount(info.size)) {
103 		printf("\n** Unable to use %s %s for loading the env **\n",
104 		       CONFIG_ENV_EXT4_INTERFACE,
105 		       CONFIG_ENV_EXT4_DEVICE_AND_PART);
106 		goto err_env_relocate;
107 	}
108 
109 	err = ext4_read_file(CONFIG_ENV_EXT4_FILE, buf, 0, CONFIG_ENV_SIZE,
110 			     &off);
111 	ext4fs_close();
112 
113 	if (err == -1) {
114 		printf("\n** Unable to read \"%s\" from %s%d:%d **\n",
115 			CONFIG_ENV_EXT4_FILE, CONFIG_ENV_EXT4_INTERFACE, dev,
116 			part);
117 		goto err_env_relocate;
118 	}
119 
120 	return env_import(buf, 1);
121 
122 err_env_relocate:
123 	set_default_env(NULL);
124 
125 	return -EIO;
126 }
127 
128 U_BOOT_ENV_LOCATION(ext4) = {
129 	.location	= ENVL_EXT4,
130 	ENV_NAME("EXT4")
131 	.load		= env_ext4_load,
132 	.save		= env_save_ptr(env_ext4_save),
133 };
134