blob: adefa7dc991ee5f7cd4f415cd79223803bf38a16 [file] [log] [blame]
Stuart Longlandd1f40632016-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 Rosanob7763472016-06-21 17:54:25 +020028#include <memalign.h>
Stuart Longlandd1f40632016-02-23 15:51:26 +100029#include <search.h>
30#include <errno.h>
31#include <ext4fs.h>
32#include <mmc.h>
33
34char *env_name_spec = "EXT4";
35
36env_t *env_ptr;
37
38DECLARE_GLOBAL_DATA_PTR;
39
40int env_init(void)
41{
42 /* use default */
43 gd->env_addr = (ulong)&default_environment[0];
44 gd->env_valid = 1;
45
46 return 0;
47}
48
49#ifdef CONFIG_CMD_SAVEENV
50int saveenv(void)
51{
52 env_t env_new;
Andrej Rosanob7763472016-06-21 17:54:25 +020053 struct blk_desc *dev_desc = NULL;
Stuart Longlandd1f40632016-02-23 15:51:26 +100054 disk_partition_t info;
55 int dev, part;
56 int err;
57
58 err = env_export(&env_new);
59 if (err)
60 return err;
61
Andrej Rosanob7763472016-06-21 17:54:25 +020062 part = blk_get_device_part_str(EXT4_ENV_INTERFACE,
Stuart Longlandd1f40632016-02-23 15:51:26 +100063 EXT4_ENV_DEVICE_AND_PART,
64 &dev_desc, &info, 1);
65 if (part < 0)
66 return 1;
67
Andrej Rosanob7763472016-06-21 17:54:25 +020068 dev = dev_desc->devnum;
Stuart Longlandd1f40632016-02-23 15:51:26 +100069 ext4fs_set_blk_dev(dev_desc, &info);
70
71 if (!ext4fs_mount(info.size)) {
72 printf("\n** Unable to use %s %s for saveenv **\n",
73 EXT4_ENV_INTERFACE, EXT4_ENV_DEVICE_AND_PART);
74 return 1;
75 }
76
77 err = ext4fs_write(EXT4_ENV_FILE, (void *)&env_new, sizeof(env_t));
78 ext4fs_close();
79
80 if (err == -1) {
81 printf("\n** Unable to write \"%s\" from %s%d:%d **\n",
82 EXT4_ENV_FILE, EXT4_ENV_INTERFACE, dev, part);
83 return 1;
84 }
85
86 puts("done\n");
87 return 0;
88}
89#endif /* CONFIG_CMD_SAVEENV */
90
91void env_relocate_spec(void)
92{
93 ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
Andrej Rosanob7763472016-06-21 17:54:25 +020094 struct blk_desc *dev_desc = NULL;
Stuart Longlandd1f40632016-02-23 15:51:26 +100095 disk_partition_t info;
96 int dev, part;
97 int err;
Andrej Rosanob7763472016-06-21 17:54:25 +020098 loff_t off;
Stuart Longlandd1f40632016-02-23 15:51:26 +100099
Andrej Rosanob7763472016-06-21 17:54:25 +0200100 part = blk_get_device_part_str(EXT4_ENV_INTERFACE,
Stuart Longlandd1f40632016-02-23 15:51:26 +1000101 EXT4_ENV_DEVICE_AND_PART,
102 &dev_desc, &info, 1);
103 if (part < 0)
104 goto err_env_relocate;
105
Andrej Rosanob7763472016-06-21 17:54:25 +0200106 dev = dev_desc->devnum;
Stuart Longlandd1f40632016-02-23 15:51:26 +1000107 ext4fs_set_blk_dev(dev_desc, &info);
108
109 if (!ext4fs_mount(info.size)) {
110 printf("\n** Unable to use %s %s for loading the env **\n",
111 EXT4_ENV_INTERFACE, EXT4_ENV_DEVICE_AND_PART);
112 goto err_env_relocate;
113 }
114
Andrej Rosanob7763472016-06-21 17:54:25 +0200115 err = ext4_read_file(EXT4_ENV_FILE, buf, 0, CONFIG_ENV_SIZE, &off);
Stuart Longlandd1f40632016-02-23 15:51:26 +1000116 ext4fs_close();
117
118 if (err == -1) {
119 printf("\n** Unable to read \"%s\" from %s%d:%d **\n",
120 EXT4_ENV_FILE, EXT4_ENV_INTERFACE, dev, part);
121 goto err_env_relocate;
122 }
123
124 env_import(buf, 1);
125 return;
126
127err_env_relocate:
128 set_default_env(NULL);
129}