]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - common/env_mmc.c
applied patches from Freescale and Ka-Ro
[karo-tx-uboot.git] / common / env_mmc.c
1 /*
2  * (C) Copyright 2008-2010 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
33 /* references to names in env_common.c */
34 extern uchar default_environment[];
35
36 char *env_name_spec = "MMC";
37
38 #ifdef ENV_IS_EMBEDDED
39 extern uchar environment[];
40 env_t *env_ptr = (env_t *)(&environment[0]);
41 #else /* ! ENV_IS_EMBEDDED */
42 env_t *env_ptr;
43 #endif /* ENV_IS_EMBEDDED */
44
45 static int mmc_env_devno;
46
47 /* local functions */
48 #if !defined(ENV_IS_EMBEDDED)
49 static void use_default(void);
50 #endif
51
52 DECLARE_GLOBAL_DATA_PTR;
53
54 uchar env_get_char_spec(int index)
55 {
56         return *((uchar *)(gd->env_addr + index));
57 }
58
59 int env_init(void)
60 {
61         /* use default */
62         gd->env_addr = (ulong)&default_environment[0];
63         gd->env_valid = 1;
64
65 #ifdef CONFIG_DYNAMIC_MMC_DEVNO
66         mmc_env_devno = get_mmc_env_devno();
67 #else
68         mmc_env_devno = CONFIG_SYS_MMC_ENV_DEV;
69 #endif
70
71         return 0;
72 }
73
74 int init_mmc_for_env(struct mmc *mmc)
75 {
76         if (!mmc) {
77                 puts("No MMC card found\n");
78                 return -1;
79         }
80
81         if (mmc_init(mmc)) {
82                 puts("MMC init failed\n");
83                 return  -1;
84         }
85
86         return 0;
87 }
88
89 #ifdef CONFIG_CMD_SAVEENV
90
91 inline int write_env(struct mmc *mmc, unsigned long size,
92                         unsigned long offset, const void *buffer)
93 {
94         uint blk_start, blk_cnt, n;
95
96         blk_start = ALIGN(offset, mmc->write_bl_len) / mmc->write_bl_len;
97         blk_cnt   = ALIGN(size, mmc->write_bl_len) / mmc->write_bl_len;
98
99         n = mmc->block_dev.block_write(mmc_env_devno, blk_start,
100                                         blk_cnt, (u_char *)buffer);
101
102         return (n == blk_cnt) ? 0 : -1;
103 }
104
105 int saveenv(void)
106 {
107         struct mmc *mmc = find_mmc_device(mmc_env_devno);
108
109         if (init_mmc_for_env(mmc))
110                 return 1;
111
112         printf("Writing to MMC(%d)... ", mmc_env_devno);
113         if (write_env(mmc, CONFIG_ENV_SIZE, CONFIG_ENV_OFFSET, env_ptr)) {
114                 puts("failed\n");
115                 return 1;
116         }
117
118         puts("done\n");
119         return 0;
120 }
121 #endif /* CONFIG_CMD_SAVEENV */
122
123 inline int read_env(struct mmc *mmc, unsigned long size,
124                         unsigned long offset, const void *buffer)
125 {
126         uint blk_start, blk_cnt, n;
127
128         blk_start = ALIGN(offset, mmc->read_bl_len) / mmc->read_bl_len;
129         blk_cnt   = ALIGN(size, mmc->read_bl_len) / mmc->read_bl_len;
130
131         n = mmc->block_dev.block_read(mmc_env_devno, blk_start,
132                                         blk_cnt, (uchar *)buffer);
133
134         return (n == blk_cnt) ? 0 : -1;
135 }
136
137 void env_relocate_spec(void)
138 {
139 #if !defined(ENV_IS_EMBEDDED)
140         struct mmc *mmc = find_mmc_device(mmc_env_devno);
141
142         if (init_mmc_for_env(mmc))
143                 return;
144
145         if (read_env(mmc, CONFIG_ENV_SIZE, CONFIG_ENV_OFFSET, env_ptr))
146                 return use_default();
147
148         if (crc32(0, env_ptr->data, ENV_SIZE) != env_ptr->crc)
149                 return use_default();
150
151         gd->env_valid = 1;
152 #endif
153 }
154
155 #if !defined(ENV_IS_EMBEDDED)
156 static void use_default()
157 {
158         puts ("*** Warning - bad CRC or MMC, using default environment\n\n");
159         set_default_env();
160 }
161 #endif
162