]> git.ipfire.org Git - people/ms/u-boot.git/blame - env/ext4.c
env: Drop common init() functions
[people/ms/u-boot.git] / env / ext4.c
CommitLineData
fd1000b9
SL
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>
bd62e241 28#include <memalign.h>
fd1000b9
SL
29#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
fd1000b9 40#ifdef CONFIG_CMD_SAVEENV
e5bce247 41static int env_ext4_save(void)
fd1000b9
SL
42{
43 env_t env_new;
bd62e241 44 struct blk_desc *dev_desc = NULL;
fd1000b9
SL
45 disk_partition_t info;
46 int dev, part;
47 int err;
48
49 err = env_export(&env_new);
50 if (err)
51 return err;
52
bd62e241 53 part = blk_get_device_part_str(EXT4_ENV_INTERFACE,
fd1000b9
SL
54 EXT4_ENV_DEVICE_AND_PART,
55 &dev_desc, &info, 1);
56 if (part < 0)
57 return 1;
58
bd62e241 59 dev = dev_desc->devnum;
fd1000b9
SL
60 ext4fs_set_blk_dev(dev_desc, &info);
61
62 if (!ext4fs_mount(info.size)) {
63 printf("\n** Unable to use %s %s for saveenv **\n",
64 EXT4_ENV_INTERFACE, EXT4_ENV_DEVICE_AND_PART);
65 return 1;
66 }
67
68 err = ext4fs_write(EXT4_ENV_FILE, (void *)&env_new, sizeof(env_t));
69 ext4fs_close();
70
71 if (err == -1) {
72 printf("\n** Unable to write \"%s\" from %s%d:%d **\n",
73 EXT4_ENV_FILE, EXT4_ENV_INTERFACE, dev, part);
74 return 1;
75 }
76
77 puts("done\n");
78 return 0;
79}
80#endif /* CONFIG_CMD_SAVEENV */
81
e5bce247 82static void env_ext4_load(void)
fd1000b9
SL
83{
84 ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
bd62e241 85 struct blk_desc *dev_desc = NULL;
fd1000b9
SL
86 disk_partition_t info;
87 int dev, part;
88 int err;
bd62e241 89 loff_t off;
fd1000b9 90
bd62e241 91 part = blk_get_device_part_str(EXT4_ENV_INTERFACE,
fd1000b9
SL
92 EXT4_ENV_DEVICE_AND_PART,
93 &dev_desc, &info, 1);
94 if (part < 0)
95 goto err_env_relocate;
96
bd62e241 97 dev = dev_desc->devnum;
fd1000b9
SL
98 ext4fs_set_blk_dev(dev_desc, &info);
99
100 if (!ext4fs_mount(info.size)) {
101 printf("\n** Unable to use %s %s for loading the env **\n",
102 EXT4_ENV_INTERFACE, EXT4_ENV_DEVICE_AND_PART);
103 goto err_env_relocate;
104 }
105
bd62e241 106 err = ext4_read_file(EXT4_ENV_FILE, buf, 0, CONFIG_ENV_SIZE, &off);
fd1000b9
SL
107 ext4fs_close();
108
109 if (err == -1) {
110 printf("\n** Unable to read \"%s\" from %s%d:%d **\n",
111 EXT4_ENV_FILE, EXT4_ENV_INTERFACE, dev, part);
112 goto err_env_relocate;
113 }
114
115 env_import(buf, 1);
116 return;
117
118err_env_relocate:
119 set_default_env(NULL);
120}
4415f1d1
SG
121
122U_BOOT_ENV_LOCATION(ext4) = {
123 .location = ENVL_EXT4,
e5bce247
SG
124 .load = env_ext4_load,
125 .save = env_save_ptr(env_ext4_save),
4415f1d1 126};