]> git.sur5r.net Git - u-boot/blob - common/env_ext4.c
MIPS: remove dead code from asm/u-boot-mips.h
[u-boot] / common / env_ext4.c
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 <search.h>
29 #include <errno.h>
30 #include <ext4fs.h>
31 #include <mmc.h>
32
33 char *env_name_spec = "EXT4";
34
35 env_t *env_ptr;
36
37 DECLARE_GLOBAL_DATA_PTR;
38
39 int env_init(void)
40 {
41         /* use default */
42         gd->env_addr = (ulong)&default_environment[0];
43         gd->env_valid = 1;
44
45         return 0;
46 }
47
48 #ifdef CONFIG_CMD_SAVEENV
49 int saveenv(void)
50 {
51         env_t   env_new;
52         block_dev_desc_t *dev_desc = NULL;
53         disk_partition_t info;
54         int dev, part;
55         int err;
56
57         err = env_export(&env_new);
58         if (err)
59                 return err;
60
61         part = get_device_and_partition(EXT4_ENV_INTERFACE,
62                                         EXT4_ENV_DEVICE_AND_PART,
63                                         &dev_desc, &info, 1);
64         if (part < 0)
65                 return 1;
66
67         dev = dev_desc->dev;
68         ext4fs_set_blk_dev(dev_desc, &info);
69
70         if (!ext4fs_mount(info.size)) {
71                 printf("\n** Unable to use %s %s for saveenv **\n",
72                        EXT4_ENV_INTERFACE, EXT4_ENV_DEVICE_AND_PART);
73                 return 1;
74         }
75
76         err = ext4fs_write(EXT4_ENV_FILE, (void *)&env_new, sizeof(env_t));
77         ext4fs_close();
78
79         if (err == -1) {
80                 printf("\n** Unable to write \"%s\" from %s%d:%d **\n",
81                         EXT4_ENV_FILE, EXT4_ENV_INTERFACE, dev, part);
82                 return 1;
83         }
84
85         puts("done\n");
86         return 0;
87 }
88 #endif /* CONFIG_CMD_SAVEENV */
89
90 void env_relocate_spec(void)
91 {
92         ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
93         block_dev_desc_t *dev_desc = NULL;
94         disk_partition_t info;
95         int dev, part;
96         int err;
97
98         part = get_device_and_partition(EXT4_ENV_INTERFACE,
99                                         EXT4_ENV_DEVICE_AND_PART,
100                                         &dev_desc, &info, 1);
101         if (part < 0)
102                 goto err_env_relocate;
103
104         dev = dev_desc->dev;
105         ext4fs_set_blk_dev(dev_desc, &info);
106
107         if (!ext4fs_mount(info.size)) {
108                 printf("\n** Unable to use %s %s for loading the env **\n",
109                        EXT4_ENV_INTERFACE, EXT4_ENV_DEVICE_AND_PART);
110                 goto err_env_relocate;
111         }
112
113         err = ext4_read_file(EXT4_ENV_FILE, buf, 0, CONFIG_ENV_SIZE);
114         ext4fs_close();
115
116         if (err == -1) {
117                 printf("\n** Unable to read \"%s\" from %s%d:%d **\n",
118                         EXT4_ENV_FILE, EXT4_ENV_INTERFACE, dev, part);
119                 goto err_env_relocate;
120         }
121
122         env_import(buf, 1);
123         return;
124
125 err_env_relocate:
126         set_default_env(NULL);
127 }