net: dsa: felix: felix_init() can be static
[platform/kernel/u-boot.git] / env / ext4.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (c) Copyright 2016 by VRT Technology
4  *
5  * Author:
6  *  Stuart Longland <stuartl@vrt.com.au>
7  *
8  * Based on FAT environment driver
9  * (c) Copyright 2011 by Tigris Elektronik GmbH
10  *
11  * Author:
12  *  Maximilian Schwerin <mvs@tigris.de>
13  *
14  * and EXT4 filesystem implementation
15  * (C) Copyright 2011 - 2012 Samsung Electronics
16  * EXT4 filesystem implementation in Uboot by
17  * Uma Shankar <uma.shankar@samsung.com>
18  * Manjunatha C Achar <a.manjunatha@samsung.com>
19  */
20
21 #include <common.h>
22 #include <part.h>
23
24 #include <command.h>
25 #include <env.h>
26 #include <env_internal.h>
27 #include <linux/stddef.h>
28 #include <malloc.h>
29 #include <memalign.h>
30 #include <search.h>
31 #include <errno.h>
32 #include <ext4fs.h>
33 #include <mmc.h>
34 #include <asm/global_data.h>
35
36 DECLARE_GLOBAL_DATA_PTR;
37
38 __weak const char *env_ext4_get_intf(void)
39 {
40         return (const char *)CONFIG_ENV_EXT4_INTERFACE;
41 }
42
43 __weak const char *env_ext4_get_dev_part(void)
44 {
45 #ifdef CONFIG_MMC
46         static char *part_str;
47
48         if (!part_str) {
49                 part_str = CONFIG_ENV_EXT4_DEVICE_AND_PART;
50                 if (!strcmp(CONFIG_ENV_EXT4_INTERFACE, "mmc") && part_str[0] == ':') {
51                         part_str = "0" CONFIG_ENV_EXT4_DEVICE_AND_PART;
52                         part_str[0] += mmc_get_env_dev();
53                 }
54         }
55
56         return part_str;
57 #else
58         return (const char *)CONFIG_ENV_EXT4_DEVICE_AND_PART;
59 #endif
60 }
61
62 static int env_ext4_save_buffer(env_t *env_new)
63 {
64         struct blk_desc *dev_desc = NULL;
65         struct disk_partition info;
66         int dev, part;
67         int err;
68         const char *ifname = env_ext4_get_intf();
69         const char *dev_and_part = env_ext4_get_dev_part();
70
71         part = blk_get_device_part_str(ifname, dev_and_part,
72                                        &dev_desc, &info, 1);
73         if (part < 0)
74                 return 1;
75
76         dev = dev_desc->devnum;
77         ext4fs_set_blk_dev(dev_desc, &info);
78
79         if (!ext4fs_mount(info.size)) {
80                 printf("\n** Unable to use %s %s for saveenv **\n",
81                        ifname, dev_and_part);
82                 return 1;
83         }
84
85         err = ext4fs_write(CONFIG_ENV_EXT4_FILE, (void *)env_new,
86                            sizeof(env_t), FILETYPE_REG);
87         ext4fs_close();
88
89         if (err == -1) {
90                 printf("\n** Unable to write \"%s\" from %s%d:%d **\n",
91                         CONFIG_ENV_EXT4_FILE, ifname, dev, part);
92                 return 1;
93         }
94
95         return 0;
96 }
97
98 static int env_ext4_save(void)
99 {
100         env_t env_new;
101         int err;
102
103         err = env_export(&env_new);
104         if (err)
105                 return err;
106
107         err = env_ext4_save_buffer(&env_new);
108         if (err)
109                 return err;
110
111         gd->env_valid = ENV_VALID;
112         puts("done\n");
113
114         return 0;
115 }
116
117 static int env_ext4_erase(void)
118 {
119         env_t env_new;
120         int err;
121
122         memset(&env_new, 0, sizeof(env_t));
123
124         err = env_ext4_save_buffer(&env_new);
125         if (err)
126                 return err;
127
128         gd->env_valid = ENV_INVALID;
129         puts("done\n");
130
131         return 0;
132 }
133
134 static int env_ext4_load(void)
135 {
136         ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
137         struct blk_desc *dev_desc = NULL;
138         struct disk_partition info;
139         int dev, part;
140         int err;
141         loff_t off;
142         const char *ifname = env_ext4_get_intf();
143         const char *dev_and_part = env_ext4_get_dev_part();
144
145 #ifdef CONFIG_MMC
146         if (!strcmp(ifname, "mmc"))
147                 mmc_initialize(NULL);
148 #endif
149
150         part = blk_get_device_part_str(ifname, dev_and_part,
151                                        &dev_desc, &info, 1);
152         if (part < 0)
153                 goto err_env_relocate;
154
155         dev = dev_desc->devnum;
156         ext4fs_set_blk_dev(dev_desc, &info);
157
158         if (!ext4fs_mount(info.size)) {
159                 printf("\n** Unable to use %s %s for loading the env **\n",
160                        ifname, dev_and_part);
161                 goto err_env_relocate;
162         }
163
164         err = ext4_read_file(CONFIG_ENV_EXT4_FILE, buf, 0, CONFIG_ENV_SIZE,
165                              &off);
166         ext4fs_close();
167
168         if (err == -1) {
169                 printf("\n** Unable to read \"%s\" from %s%d:%d **\n",
170                         CONFIG_ENV_EXT4_FILE, ifname, dev, part);
171                 goto err_env_relocate;
172         }
173
174         err = env_import(buf, 1, H_EXTERNAL);
175         if (!err)
176                 gd->env_valid = ENV_VALID;
177
178         return err;
179
180 err_env_relocate:
181         env_set_default(NULL, 0);
182
183         return -EIO;
184 }
185
186 U_BOOT_ENV_LOCATION(ext4) = {
187         .location       = ENVL_EXT4,
188         ENV_NAME("EXT4")
189         .load           = env_ext4_load,
190         .save           = ENV_SAVE_PTR(env_ext4_save),
191         .erase          = ENV_ERASE_PTR(env_ext4_erase),
192 };