blob: ca76967a5fa12d405dd882207ab910a2fdbe2f8c [file] [log] [blame]
Maximilian Schwerin57210c72012-03-12 23:57:50 +00001/*
2 * (c) Copyright 2011 by Tigris Elektronik GmbH
3 *
4 * Author:
5 * Maximilian Schwerin <mvs@tigris.de>
6 *
7 * See file CREDITS for list of people who contributed to this
8 * project.
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of
13 * the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
23 * MA 02111-1307 USA
24 */
25
26#include <common.h>
27
28#include <command.h>
29#include <environment.h>
30#include <linux/stddef.h>
31#include <malloc.h>
32#include <search.h>
33#include <errno.h>
34#include <fat.h>
35#include <mmc.h>
36
37char *env_name_spec = "FAT";
38
39env_t *env_ptr;
40
41DECLARE_GLOBAL_DATA_PTR;
42
Maximilian Schwerin57210c72012-03-12 23:57:50 +000043int env_init(void)
44{
45 /* use default */
46 gd->env_addr = (ulong)&default_environment[0];
47 gd->env_valid = 1;
48
49 return 0;
50}
51
52#ifdef CONFIG_CMD_SAVEENV
53int saveenv(void)
54{
55 env_t env_new;
56 ssize_t len;
57 char *res;
58 block_dev_desc_t *dev_desc = NULL;
59 int dev = FAT_ENV_DEVICE;
60 int part = FAT_ENV_PART;
61
62 res = (char *)&env_new.data;
63 len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, 0, NULL);
64 if (len < 0) {
65 error("Cannot export environment: errno = %d\n", errno);
66 return 1;
67 }
68
69#ifdef CONFIG_MMC
70 if (strcmp (FAT_ENV_INTERFACE, "mmc") == 0) {
71 struct mmc *mmc = find_mmc_device(dev);
72
73 if (!mmc) {
74 printf("no mmc device at slot %x\n", dev);
75 return 1;
76 }
77
78 mmc->has_init = 0;
79 mmc_init(mmc);
80 }
81#endif /* CONFIG_MMC */
82
83 dev_desc = get_dev(FAT_ENV_INTERFACE, dev);
84 if (dev_desc == NULL) {
85 printf("Failed to find %s%d\n",
86 FAT_ENV_INTERFACE, dev);
87 return 1;
88 }
89 if (fat_register_device(dev_desc, part) != 0) {
90 printf("Failed to register %s%d:%d\n",
91 FAT_ENV_INTERFACE, dev, part);
92 return 1;
93 }
94
95 env_new.crc = crc32(0, env_new.data, ENV_SIZE);
96 if (file_fat_write(FAT_ENV_FILE, (void *)&env_new, sizeof(env_t)) == -1) {
97 printf("\n** Unable to write \"%s\" from %s%d:%d **\n",
98 FAT_ENV_FILE, FAT_ENV_INTERFACE, dev, part);
99 return 1;
100 }
101
102 puts("done\n");
103 return 0;
104}
105#endif /* CONFIG_CMD_SAVEENV */
106
107void env_relocate_spec(void)
108{
109 char buf[CONFIG_ENV_SIZE];
110 block_dev_desc_t *dev_desc = NULL;
111 int dev = FAT_ENV_DEVICE;
112 int part = FAT_ENV_PART;
113
114#ifdef CONFIG_MMC
115 if (strcmp (FAT_ENV_INTERFACE, "mmc") == 0) {
116 struct mmc *mmc = find_mmc_device(dev);
117
118 if (!mmc) {
119 printf("no mmc device at slot %x\n", dev);
120 set_default_env(NULL);
121 return;
122 }
123
124 mmc->has_init = 0;
125 mmc_init(mmc);
126 }
127#endif /* CONFIG_MMC */
128
129 dev_desc = get_dev(FAT_ENV_INTERFACE, dev);
130 if (dev_desc == NULL) {
131 printf("Failed to find %s%d\n",
132 FAT_ENV_INTERFACE, dev);
133 set_default_env(NULL);
134 return;
135 }
136 if (fat_register_device(dev_desc, part) != 0) {
137 printf("Failed to register %s%d:%d\n",
138 FAT_ENV_INTERFACE, dev, part);
139 set_default_env(NULL);
140 return;
141 }
142
143 if (file_fat_read(FAT_ENV_FILE, (unsigned char *)&buf, CONFIG_ENV_SIZE) == -1) {
144 printf("\n** Unable to read \"%s\" from %s%d:%d **\n",
145 FAT_ENV_FILE, FAT_ENV_INTERFACE, dev, part);
146 set_default_env(NULL);
147 return;
148 }
149
150 env_import(buf, 1);
151}