blob: 6ef531821e015788a605836fe608077c70d17d9c [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;
Igor Grinberg9aa90c12012-09-23 05:25:21 +000061 int err;
Maximilian Schwerin57210c72012-03-12 23:57:50 +000062
63 res = (char *)&env_new.data;
64 len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, 0, NULL);
65 if (len < 0) {
66 error("Cannot export environment: errno = %d\n", errno);
67 return 1;
68 }
69
70#ifdef CONFIG_MMC
Igor Grinberg9aa90c12012-09-23 05:25:21 +000071 if (strcmp(FAT_ENV_INTERFACE, "mmc") == 0) {
Maximilian Schwerin57210c72012-03-12 23:57:50 +000072 struct mmc *mmc = find_mmc_device(dev);
73
74 if (!mmc) {
75 printf("no mmc device at slot %x\n", dev);
76 return 1;
77 }
78
79 mmc->has_init = 0;
80 mmc_init(mmc);
81 }
82#endif /* CONFIG_MMC */
83
84 dev_desc = get_dev(FAT_ENV_INTERFACE, dev);
85 if (dev_desc == NULL) {
86 printf("Failed to find %s%d\n",
87 FAT_ENV_INTERFACE, dev);
88 return 1;
89 }
Igor Grinberg9aa90c12012-09-23 05:25:21 +000090
91 err = fat_register_device(dev_desc, part);
92 if (err) {
Maximilian Schwerin57210c72012-03-12 23:57:50 +000093 printf("Failed to register %s%d:%d\n",
94 FAT_ENV_INTERFACE, dev, part);
95 return 1;
96 }
97
98 env_new.crc = crc32(0, env_new.data, ENV_SIZE);
Igor Grinberg9aa90c12012-09-23 05:25:21 +000099 err = file_fat_write(FAT_ENV_FILE, (void *)&env_new, sizeof(env_t));
100 if (err == -1) {
Maximilian Schwerin57210c72012-03-12 23:57:50 +0000101 printf("\n** Unable to write \"%s\" from %s%d:%d **\n",
102 FAT_ENV_FILE, FAT_ENV_INTERFACE, dev, part);
103 return 1;
104 }
105
106 puts("done\n");
107 return 0;
108}
109#endif /* CONFIG_CMD_SAVEENV */
110
111void env_relocate_spec(void)
112{
113 char buf[CONFIG_ENV_SIZE];
114 block_dev_desc_t *dev_desc = NULL;
115 int dev = FAT_ENV_DEVICE;
116 int part = FAT_ENV_PART;
Igor Grinberg9aa90c12012-09-23 05:25:21 +0000117 int err;
Maximilian Schwerin57210c72012-03-12 23:57:50 +0000118
119#ifdef CONFIG_MMC
Igor Grinberg9aa90c12012-09-23 05:25:21 +0000120 if (strcmp(FAT_ENV_INTERFACE, "mmc") == 0) {
Maximilian Schwerin57210c72012-03-12 23:57:50 +0000121 struct mmc *mmc = find_mmc_device(dev);
122
123 if (!mmc) {
124 printf("no mmc device at slot %x\n", dev);
125 set_default_env(NULL);
126 return;
127 }
128
129 mmc->has_init = 0;
130 mmc_init(mmc);
131 }
132#endif /* CONFIG_MMC */
133
134 dev_desc = get_dev(FAT_ENV_INTERFACE, dev);
135 if (dev_desc == NULL) {
136 printf("Failed to find %s%d\n",
137 FAT_ENV_INTERFACE, dev);
138 set_default_env(NULL);
139 return;
140 }
Igor Grinberg9aa90c12012-09-23 05:25:21 +0000141
142 err = fat_register_device(dev_desc, part);
143 if (err) {
Maximilian Schwerin57210c72012-03-12 23:57:50 +0000144 printf("Failed to register %s%d:%d\n",
145 FAT_ENV_INTERFACE, dev, part);
146 set_default_env(NULL);
147 return;
148 }
149
Igor Grinberg9aa90c12012-09-23 05:25:21 +0000150 err = file_fat_read(FAT_ENV_FILE, (uchar *)&buf, CONFIG_ENV_SIZE);
151 if (err == -1) {
Maximilian Schwerin57210c72012-03-12 23:57:50 +0000152 printf("\n** Unable to read \"%s\" from %s%d:%d **\n",
153 FAT_ENV_FILE, FAT_ENV_INTERFACE, dev, part);
154 set_default_env(NULL);
155 return;
156 }
157
158 env_import(buf, 1);
159}