]>
Commit | Line | Data |
---|---|---|
a8060359 | 1 | /* |
97039ab9 | 2 | * (C) Copyright 2008-2011 Freescale Semiconductor, Inc. |
a8060359 TL |
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> | |
6d1d51b3 | 32 | #include <search.h> |
e79f4839 | 33 | #include <errno.h> |
a8060359 | 34 | |
a8060359 TL |
35 | char *env_name_spec = "MMC"; |
36 | ||
37 | #ifdef ENV_IS_EMBEDDED | |
38 | extern uchar environment[]; | |
39 | env_t *env_ptr = (env_t *)(&environment[0]); | |
40 | #else /* ! ENV_IS_EMBEDDED */ | |
41 | env_t *env_ptr = NULL; | |
42 | #endif /* ENV_IS_EMBEDDED */ | |
43 | ||
44 | /* local functions */ | |
45 | #if !defined(ENV_IS_EMBEDDED) | |
46 | static void use_default(void); | |
47 | #endif | |
48 | ||
49 | DECLARE_GLOBAL_DATA_PTR; | |
50 | ||
97039ab9 MH |
51 | #if !defined(CONFIG_ENV_OFFSET) |
52 | #define CONFIG_ENV_OFFSET 0 | |
53 | #endif | |
54 | ||
55 | static int __mmc_get_env_addr(struct mmc *mmc, u32 *env_addr) | |
56 | { | |
57 | *env_addr = CONFIG_ENV_OFFSET; | |
58 | return 0; | |
59 | } | |
60 | __attribute__((weak, alias("__mmc_get_env_addr"))) | |
61 | int mmc_get_env_addr(struct mmc *mmc, u32 *env_addr); | |
62 | ||
63 | ||
a8060359 TL |
64 | uchar env_get_char_spec(int index) |
65 | { | |
66 | return *((uchar *)(gd->env_addr + index)); | |
67 | } | |
68 | ||
69 | int env_init(void) | |
70 | { | |
71 | /* use default */ | |
72 | gd->env_addr = (ulong)&default_environment[0]; | |
73 | gd->env_valid = 1; | |
74 | ||
75 | return 0; | |
76 | } | |
77 | ||
78 | int init_mmc_for_env(struct mmc *mmc) | |
79 | { | |
80 | if (!mmc) { | |
81 | puts("No MMC card found\n"); | |
82 | return -1; | |
83 | } | |
84 | ||
85 | if (mmc_init(mmc)) { | |
86 | puts("MMC init failed\n"); | |
87 | return -1; | |
88 | } | |
89 | ||
90 | return 0; | |
91 | } | |
92 | ||
93 | #ifdef CONFIG_CMD_SAVEENV | |
94 | ||
95 | inline int write_env(struct mmc *mmc, unsigned long size, | |
96 | unsigned long offset, const void *buffer) | |
97 | { | |
98 | uint blk_start, blk_cnt, n; | |
99 | ||
100 | blk_start = ALIGN(offset, mmc->write_bl_len) / mmc->write_bl_len; | |
101 | blk_cnt = ALIGN(size, mmc->write_bl_len) / mmc->write_bl_len; | |
102 | ||
103 | n = mmc->block_dev.block_write(CONFIG_SYS_MMC_ENV_DEV, blk_start, | |
104 | blk_cnt, (u_char *)buffer); | |
105 | ||
106 | return (n == blk_cnt) ? 0 : -1; | |
107 | } | |
108 | ||
109 | int saveenv(void) | |
110 | { | |
e79f4839 LW |
111 | env_t env_new; |
112 | ssize_t len; | |
113 | char *res; | |
a8060359 | 114 | struct mmc *mmc = find_mmc_device(CONFIG_SYS_MMC_ENV_DEV); |
97039ab9 | 115 | u32 offset; |
a8060359 TL |
116 | |
117 | if (init_mmc_for_env(mmc)) | |
118 | return 1; | |
119 | ||
97039ab9 MH |
120 | if(mmc_get_env_addr(mmc, &offset)) |
121 | return 1; | |
122 | ||
e79f4839 | 123 | res = (char *)&env_new.data; |
37f2fe74 | 124 | len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, 0, NULL); |
e79f4839 LW |
125 | if (len < 0) { |
126 | error("Cannot export environment: errno = %d\n", errno); | |
127 | return 1; | |
128 | } | |
129 | env_new.crc = crc32(0, env_new.data, ENV_SIZE); | |
a8060359 | 130 | printf("Writing to MMC(%d)... ", CONFIG_SYS_MMC_ENV_DEV); |
97039ab9 | 131 | if (write_env(mmc, CONFIG_ENV_SIZE, offset, (u_char *)&env_new)) { |
a8060359 TL |
132 | puts("failed\n"); |
133 | return 1; | |
134 | } | |
135 | ||
136 | puts("done\n"); | |
137 | return 0; | |
138 | } | |
139 | #endif /* CONFIG_CMD_SAVEENV */ | |
140 | ||
141 | inline int read_env(struct mmc *mmc, unsigned long size, | |
142 | unsigned long offset, const void *buffer) | |
143 | { | |
144 | uint blk_start, blk_cnt, n; | |
145 | ||
146 | blk_start = ALIGN(offset, mmc->read_bl_len) / mmc->read_bl_len; | |
147 | blk_cnt = ALIGN(size, mmc->read_bl_len) / mmc->read_bl_len; | |
148 | ||
149 | n = mmc->block_dev.block_read(CONFIG_SYS_MMC_ENV_DEV, blk_start, | |
150 | blk_cnt, (uchar *)buffer); | |
151 | ||
152 | return (n == blk_cnt) ? 0 : -1; | |
153 | } | |
154 | ||
155 | void env_relocate_spec(void) | |
156 | { | |
157 | #if !defined(ENV_IS_EMBEDDED) | |
97039ab9 | 158 | char buf[CONFIG_ENV_SIZE]; |
d470a6f6 | 159 | |
a8060359 | 160 | struct mmc *mmc = find_mmc_device(CONFIG_SYS_MMC_ENV_DEV); |
97039ab9 | 161 | u32 offset; |
a8060359 | 162 | |
d470a6f6 SS |
163 | if (init_mmc_for_env(mmc)) { |
164 | use_default(); | |
a8060359 | 165 | return; |
d470a6f6 | 166 | } |
a8060359 | 167 | |
97039ab9 MH |
168 | if(mmc_get_env_addr(mmc, &offset)) { |
169 | use_default(); | |
170 | return ; | |
171 | } | |
172 | ||
173 | if (read_env(mmc, CONFIG_ENV_SIZE, offset, buf)) { | |
d470a6f6 SS |
174 | use_default(); |
175 | return; | |
176 | } | |
a8060359 | 177 | |
d470a6f6 | 178 | env_import(buf, 1); |
a8060359 TL |
179 | #endif |
180 | } | |
181 | ||
182 | #if !defined(ENV_IS_EMBEDDED) | |
183 | static void use_default() | |
184 | { | |
a2f69d35 | 185 | set_default_env(NULL); |
a8060359 TL |
186 | } |
187 | #endif |