]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - common/env_fat.c
Merge branch 'master' of git://www.denx.de/git/u-boot-cfi-flash
[karo-tx-uboot.git] / common / env_fat.c
1 /*
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
37 char *env_name_spec = "FAT";
38
39 env_t *env_ptr;
40 static char env_buf[CONFIG_ENV_SIZE];
41
42 DECLARE_GLOBAL_DATA_PTR;
43
44 int env_init(void)
45 {
46         /* use default */
47         gd->env_addr = (ulong)&default_environment[0];
48         gd->env_valid = 1;
49
50         return 0;
51 }
52
53 #ifdef CONFIG_CMD_SAVEENV
54 int saveenv(void)
55 {
56         env_t   *env_new = env_buf;
57         ssize_t len;
58         char    *res;
59         block_dev_desc_t *dev_desc = NULL;
60         int dev = FAT_ENV_DEVICE;
61         int part = FAT_ENV_PART;
62         int err;
63
64         res = (char *)env_new->data;
65         len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
66         if (len < 0) {
67                 error("Cannot export environment: errno = %d\n", errno);
68                 return 1;
69         }
70
71 #ifdef CONFIG_MMC
72         if (strcmp(FAT_ENV_INTERFACE, "mmc") == 0) {
73                 struct mmc *mmc = find_mmc_device(dev);
74
75                 if (!mmc) {
76                         printf("no mmc device at slot %x\n", dev);
77                         return 1;
78                 }
79
80                 mmc->has_init = 0;
81                 mmc_init(mmc);
82         }
83 #endif /* CONFIG_MMC */
84
85         dev_desc = get_dev(FAT_ENV_INTERFACE, dev);
86         if (dev_desc == NULL) {
87                 printf("Failed to find %s%d\n",
88                         FAT_ENV_INTERFACE, dev);
89                 return 1;
90         }
91
92         err = fat_register_device(dev_desc, part);
93         if (err) {
94                 printf("Failed to register %s%d:%d\n",
95                         FAT_ENV_INTERFACE, dev, part);
96                 return 1;
97         }
98
99         env_new->crc = crc32(0, env_new->data, ENV_SIZE);
100         err = file_fat_write(FAT_ENV_FILE, (void *)env_new, sizeof(env_t));
101         if (err == -1) {
102                 printf("\n** Unable to write \"%s\" from %s%d:%d **\n",
103                         FAT_ENV_FILE, FAT_ENV_INTERFACE, dev, part);
104                 return 1;
105         }
106
107         puts("done\n");
108         return 0;
109 }
110 #endif /* CONFIG_CMD_SAVEENV */
111
112 void env_relocate_spec(void)
113 {
114         char *buf = env_buf;
115         block_dev_desc_t *dev_desc = NULL;
116         int dev = FAT_ENV_DEVICE;
117         int part = FAT_ENV_PART;
118         int err;
119
120 #ifdef CONFIG_MMC
121         if (strcmp(FAT_ENV_INTERFACE, "mmc") == 0) {
122                 struct mmc *mmc = find_mmc_device(dev);
123
124                 if (!mmc) {
125                         printf("no mmc device at slot %x\n", dev);
126                         set_default_env(NULL);
127                         return;
128                 }
129
130                 mmc->has_init = 0;
131                 mmc_init(mmc);
132         }
133 #endif /* CONFIG_MMC */
134
135         dev_desc = get_dev(FAT_ENV_INTERFACE, dev);
136         if (dev_desc == NULL) {
137                 printf("Failed to find %s%d\n",
138                         FAT_ENV_INTERFACE, dev);
139                 set_default_env(NULL);
140                 return;
141         }
142
143         err = fat_register_device(dev_desc, part);
144         if (err) {
145                 printf("Failed to register %s%d:%d\n",
146                         FAT_ENV_INTERFACE, dev, part);
147                 set_default_env(NULL);
148                 return;
149         }
150
151         err = file_fat_read(FAT_ENV_FILE, (uchar *)&buf, CONFIG_ENV_SIZE);
152         if (err == -1) {
153                 printf("\n** Unable to read \"%s\" from %s%d:%d **\n",
154                         FAT_ENV_FILE, FAT_ENV_INTERFACE, dev, part);
155                 set_default_env(NULL);
156                 return;
157         }
158
159         env_import(buf, 1);
160 }