uboot-omap: switch to u-boot.mk
[openwrt/staging/yousong.git] / package / boot / uboot-oxnas / files / common / env_ext4.c
1 /*
2 * (c) Copyright 2011 by Tigris Elektronik GmbH
3 *
4 * Author:
5 * Maximilian Schwerin <mvs@tigris.de>
6 *
7 * SPDX-License-Identifier: GPL-2.0+
8 */
9
10 #include <common.h>
11
12 #include <command.h>
13 #include <environment.h>
14 #include <linux/stddef.h>
15 #include <malloc.h>
16 #include <search.h>
17 #include <errno.h>
18 #include <ext4fs.h>
19
20 char *env_name_spec = "EXT4";
21
22 env_t *env_ptr;
23
24 DECLARE_GLOBAL_DATA_PTR;
25
26 int env_init(void)
27 {
28 /* use default */
29 gd->env_addr = (ulong)&default_environment[0];
30 gd->env_valid = 1;
31
32 return 0;
33 }
34
35 #ifdef CONFIG_CMD_SAVEENV
36 int saveenv(void)
37 {
38 env_t env_new;
39 ssize_t len;
40 char *res;
41 block_dev_desc_t *dev_desc = NULL;
42 int dev = EXT4_ENV_DEVICE;
43 int part = EXT4_ENV_PART;
44 int err;
45
46 res = (char *)&env_new.data;
47 len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
48 if (len < 0) {
49 error("Cannot export environment: errno = %d\n", errno);
50 return 1;
51 }
52
53 dev_desc = get_dev(EXT4_ENV_INTERFACE, dev);
54 if (dev_desc == NULL) {
55 printf("Failed to find %s%d\n",
56 EXT4_ENV_INTERFACE, dev);
57 return 1;
58 }
59
60 err = ext4_register_device(dev_desc, part);
61 if (err) {
62 printf("Failed to register %s%d:%d\n",
63 EXT4_ENV_INTERFACE, dev, part);
64 return 1;
65 }
66
67 env_new.crc = crc32(0, env_new.data, ENV_SIZE);
68 err = ext4fs_write(EXT4_ENV_FILE, (void *)&env_new, sizeof(env_t));
69 ext4fs_close();
70 if (err == -1) {
71 printf("\n** Unable to write \"%s\" from %s%d:%d **\n",
72 EXT4_ENV_FILE, EXT4_ENV_INTERFACE, dev, part);
73 return 1;
74 }
75
76 puts("done\n");
77 return 0;
78 }
79 #endif /* CONFIG_CMD_SAVEENV */
80
81 void env_relocate_spec(void)
82 {
83 char buf[CONFIG_ENV_SIZE];
84 block_dev_desc_t *dev_desc = NULL;
85 int dev = EXT4_ENV_DEVICE;
86 int part = EXT4_ENV_PART;
87 int err;
88
89 dev_desc = get_dev(EXT4_ENV_INTERFACE, dev);
90 if (dev_desc == NULL) {
91 printf("Failed to find %s%d\n",
92 EXT4_ENV_INTERFACE, dev);
93 set_default_env(NULL);
94 return;
95 }
96
97 err = ext4_register_device(dev_desc, part);
98 if (err) {
99 printf("Failed to register %s%d:%d\n",
100 EXT4_ENV_INTERFACE, dev, part);
101 set_default_env(NULL);
102 return;
103 }
104
105 err = ext4_read_file(EXT4_ENV_FILE, (uchar *)&buf, 0, CONFIG_ENV_SIZE);
106 ext4fs_close();
107
108 if (err == -1) {
109 printf("\n** Unable to read \"%s\" from %s%d:%d **\n",
110 EXT4_ENV_FILE, EXT4_ENV_INTERFACE, dev, part);
111 set_default_env(NULL);
112 return;
113 }
114
115 env_import(buf, 1);
116 }