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