]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - common/env_mmc.c
MAINTAINERS: fix Andreas Bießmann AVR32 entry
[karo-tx-uboot.git] / common / env_mmc.c
1 /*
2  * (C) Copyright 2008-2011 Freescale Semiconductor, Inc.
3  *
4  * See file CREDITS for list of people who contributed to this
5  * project.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License as
9  * published by the Free Software Foundation; either version 2 of
10  * the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
20  * MA 02111-1307 USA
21  */
22
23 /* #define DEBUG */
24
25 #include <common.h>
26
27 #include <command.h>
28 #include <environment.h>
29 #include <linux/stddef.h>
30 #include <malloc.h>
31 #include <mmc.h>
32 #include <search.h>
33 #include <errno.h>
34
35 char *env_name_spec = "MMC";
36
37 #ifdef ENV_IS_EMBEDDED
38 env_t *env_ptr = &environment;
39 #else /* ! ENV_IS_EMBEDDED */
40 env_t *env_ptr;
41 #endif /* ENV_IS_EMBEDDED */
42
43 DECLARE_GLOBAL_DATA_PTR;
44
45 #if !defined(CONFIG_ENV_OFFSET)
46 #define CONFIG_ENV_OFFSET 0
47 #endif
48
49 static int __mmc_get_env_addr(struct mmc *mmc, u32 *env_addr)
50 {
51         *env_addr = CONFIG_ENV_OFFSET;
52         return 0;
53 }
54 int mmc_get_env_addr(struct mmc *mmc, u32 *env_addr)
55         __attribute__((weak, alias("__mmc_get_env_addr")));
56
57 int env_init(void)
58 {
59         /* use default */
60         gd->env_addr    = (ulong)&default_environment[0];
61         gd->env_valid   = 1;
62
63         return 0;
64 }
65
66 static int init_mmc_for_env(struct mmc *mmc)
67 {
68         if (!mmc) {
69                 puts("No MMC card found\n");
70                 return -1;
71         }
72
73         if (mmc_init(mmc)) {
74                 puts("MMC init failed\n");
75                 return -1;
76         }
77
78         return 0;
79 }
80
81 #ifdef CONFIG_CMD_SAVEENV
82 static inline int write_env(struct mmc *mmc, unsigned long size,
83                             unsigned long offset, const void *buffer)
84 {
85         uint blk_start, blk_cnt, n;
86
87         blk_start       = ALIGN(offset, mmc->write_bl_len) / mmc->write_bl_len;
88         blk_cnt         = ALIGN(size, mmc->write_bl_len) / mmc->write_bl_len;
89
90         n = mmc->block_dev.block_write(CONFIG_SYS_MMC_ENV_DEV, blk_start,
91                                         blk_cnt, (u_char *)buffer);
92
93         return (n == blk_cnt) ? 0 : -1;
94 }
95
96 int saveenv(void)
97 {
98         ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
99         ssize_t len;
100         char    *res;
101         struct mmc *mmc = find_mmc_device(CONFIG_SYS_MMC_ENV_DEV);
102         u32     offset;
103
104         if (init_mmc_for_env(mmc) || mmc_get_env_addr(mmc, &offset))
105                 return 1;
106
107         res = (char *)&env_new->data;
108         len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, 0, NULL);
109         if (len < 0) {
110                 error("Cannot export environment: errno = %d\n", errno);
111                 return 1;
112         }
113
114         env_new->crc = crc32(0, &env_new->data[0], ENV_SIZE);
115         printf("Writing to MMC(%d)... ", CONFIG_SYS_MMC_ENV_DEV);
116         if (write_env(mmc, CONFIG_ENV_SIZE, offset, (u_char *)env_new)) {
117                 puts("failed\n");
118                 return 1;
119         }
120
121         puts("done\n");
122         return 0;
123 }
124 #endif /* CONFIG_CMD_SAVEENV */
125
126 static inline int read_env(struct mmc *mmc, unsigned long size,
127                            unsigned long offset, const void *buffer)
128 {
129         uint blk_start, blk_cnt, n;
130
131         blk_start       = ALIGN(offset, mmc->read_bl_len) / mmc->read_bl_len;
132         blk_cnt         = ALIGN(size, mmc->read_bl_len) / mmc->read_bl_len;
133
134         n = mmc->block_dev.block_read(CONFIG_SYS_MMC_ENV_DEV, blk_start,
135                                         blk_cnt, (uchar *)buffer);
136
137         return (n == blk_cnt) ? 0 : -1;
138 }
139
140 void env_relocate_spec(void)
141 {
142 #if !defined(ENV_IS_EMBEDDED)
143         ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
144         struct mmc *mmc = find_mmc_device(CONFIG_SYS_MMC_ENV_DEV);
145         u32 offset;
146
147         if (init_mmc_for_env(mmc) || mmc_get_env_addr(mmc, &offset))
148                 return set_default_env(NULL);
149
150         if (read_env(mmc, CONFIG_ENV_SIZE, offset, buf))
151                 return set_default_env(NULL);
152
153         env_import(buf, 1);
154 #endif
155 }