blob: 5acdf0eda0dbb6d368622565237a87721ef0fc37 [file] [log] [blame]
Stuart Longlandfd1000b2016-02-23 15:51:26 +10001/*
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>
Andrej Rosanobd62e242016-06-21 17:54:25 +020028#include <memalign.h>
Stuart Longlandfd1000b2016-02-23 15:51:26 +100029#include <search.h>
30#include <errno.h>
31#include <ext4fs.h>
32#include <mmc.h>
33
Stuart Longlandfd1000b2016-02-23 15:51:26 +100034env_t *env_ptr;
35
36DECLARE_GLOBAL_DATA_PTR;
37
Stuart Longlandfd1000b2016-02-23 15:51:26 +100038#ifdef CONFIG_CMD_SAVEENV
Simon Glasse5bce242017-08-03 12:22:01 -060039static int env_ext4_save(void)
Stuart Longlandfd1000b2016-02-23 15:51:26 +100040{
41 env_t env_new;
Andrej Rosanobd62e242016-06-21 17:54:25 +020042 struct blk_desc *dev_desc = NULL;
Stuart Longlandfd1000b2016-02-23 15:51:26 +100043 disk_partition_t info;
44 int dev, part;
45 int err;
46
47 err = env_export(&env_new);
48 if (err)
49 return err;
50
Andrej Rosanobd62e242016-06-21 17:54:25 +020051 part = blk_get_device_part_str(EXT4_ENV_INTERFACE,
Stuart Longlandfd1000b2016-02-23 15:51:26 +100052 EXT4_ENV_DEVICE_AND_PART,
53 &dev_desc, &info, 1);
54 if (part < 0)
55 return 1;
56
Andrej Rosanobd62e242016-06-21 17:54:25 +020057 dev = dev_desc->devnum;
Stuart Longlandfd1000b2016-02-23 15:51:26 +100058 ext4fs_set_blk_dev(dev_desc, &info);
59
60 if (!ext4fs_mount(info.size)) {
61 printf("\n** Unable to use %s %s for saveenv **\n",
62 EXT4_ENV_INTERFACE, EXT4_ENV_DEVICE_AND_PART);
63 return 1;
64 }
65
66 err = ext4fs_write(EXT4_ENV_FILE, (void *)&env_new, sizeof(env_t));
67 ext4fs_close();
68
69 if (err == -1) {
70 printf("\n** Unable to write \"%s\" from %s%d:%d **\n",
71 EXT4_ENV_FILE, EXT4_ENV_INTERFACE, dev, part);
72 return 1;
73 }
74
75 puts("done\n");
76 return 0;
77}
78#endif /* CONFIG_CMD_SAVEENV */
79
Simon Glasse5bce242017-08-03 12:22:01 -060080static void env_ext4_load(void)
Stuart Longlandfd1000b2016-02-23 15:51:26 +100081{
82 ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
Andrej Rosanobd62e242016-06-21 17:54:25 +020083 struct blk_desc *dev_desc = NULL;
Stuart Longlandfd1000b2016-02-23 15:51:26 +100084 disk_partition_t info;
85 int dev, part;
86 int err;
Andrej Rosanobd62e242016-06-21 17:54:25 +020087 loff_t off;
Stuart Longlandfd1000b2016-02-23 15:51:26 +100088
Andrej Rosanobd62e242016-06-21 17:54:25 +020089 part = blk_get_device_part_str(EXT4_ENV_INTERFACE,
Stuart Longlandfd1000b2016-02-23 15:51:26 +100090 EXT4_ENV_DEVICE_AND_PART,
91 &dev_desc, &info, 1);
92 if (part < 0)
93 goto err_env_relocate;
94
Andrej Rosanobd62e242016-06-21 17:54:25 +020095 dev = dev_desc->devnum;
Stuart Longlandfd1000b2016-02-23 15:51:26 +100096 ext4fs_set_blk_dev(dev_desc, &info);
97
98 if (!ext4fs_mount(info.size)) {
99 printf("\n** Unable to use %s %s for loading the env **\n",
100 EXT4_ENV_INTERFACE, EXT4_ENV_DEVICE_AND_PART);
101 goto err_env_relocate;
102 }
103
Andrej Rosanobd62e242016-06-21 17:54:25 +0200104 err = ext4_read_file(EXT4_ENV_FILE, buf, 0, CONFIG_ENV_SIZE, &off);
Stuart Longlandfd1000b2016-02-23 15:51:26 +1000105 ext4fs_close();
106
107 if (err == -1) {
108 printf("\n** Unable to read \"%s\" from %s%d:%d **\n",
109 EXT4_ENV_FILE, EXT4_ENV_INTERFACE, dev, part);
110 goto err_env_relocate;
111 }
112
113 env_import(buf, 1);
114 return;
115
116err_env_relocate:
117 set_default_env(NULL);
118}
Simon Glass4415f1d2017-08-03 12:21:58 -0600119
120U_BOOT_ENV_LOCATION(ext4) = {
121 .location = ENVL_EXT4,
Simon Glassac358be2017-08-03 12:22:03 -0600122 ENV_NAME("EXT4")
Simon Glasse5bce242017-08-03 12:22:01 -0600123 .load = env_ext4_load,
124 .save = env_save_ptr(env_ext4_save),
Simon Glass4415f1d2017-08-03 12:21:58 -0600125};