Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 1 | /* |
Mingkai Hu | 97039ab | 2011-01-24 17:09:55 +0000 | [diff] [blame] | 2 | * (C) Copyright 2008-2011 Freescale Semiconductor, Inc. |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 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> |
Lei Wen | 6d1d51b | 2010-11-10 07:39:23 +0800 | [diff] [blame] | 32 | #include <search.h> |
Lei Wen | e79f483 | 2010-10-13 11:07:21 +0800 | [diff] [blame] | 33 | #include <errno.h> |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 34 | |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 35 | char *env_name_spec = "MMC"; |
| 36 | |
| 37 | #ifdef ENV_IS_EMBEDDED |
Igor Grinberg | 994bc67 | 2011-11-17 06:07:23 +0000 | [diff] [blame] | 38 | env_t *env_ptr = &environment; |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 39 | #else /* ! ENV_IS_EMBEDDED */ |
Igor Grinberg | e8db8f7 | 2011-11-07 01:14:05 +0000 | [diff] [blame] | 40 | env_t *env_ptr; |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 41 | #endif /* ENV_IS_EMBEDDED */ |
| 42 | |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 43 | DECLARE_GLOBAL_DATA_PTR; |
| 44 | |
Mingkai Hu | 97039ab | 2011-01-24 17:09:55 +0000 | [diff] [blame] | 45 | #if !defined(CONFIG_ENV_OFFSET) |
| 46 | #define CONFIG_ENV_OFFSET 0 |
| 47 | #endif |
| 48 | |
Fabio Estevam | b565408 | 2013-01-08 05:36:11 +0000 | [diff] [blame] | 49 | __weak int mmc_get_env_addr(struct mmc *mmc, u32 *env_addr) |
Mingkai Hu | 97039ab | 2011-01-24 17:09:55 +0000 | [diff] [blame] | 50 | { |
| 51 | *env_addr = CONFIG_ENV_OFFSET; |
| 52 | return 0; |
| 53 | } |
Mingkai Hu | 97039ab | 2011-01-24 17:09:55 +0000 | [diff] [blame] | 54 | |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 55 | int env_init(void) |
| 56 | { |
| 57 | /* use default */ |
Igor Grinberg | e8db8f7 | 2011-11-07 01:14:05 +0000 | [diff] [blame] | 58 | gd->env_addr = (ulong)&default_environment[0]; |
| 59 | gd->env_valid = 1; |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 60 | |
| 61 | return 0; |
| 62 | } |
| 63 | |
Igor Grinberg | e8db8f7 | 2011-11-07 01:14:05 +0000 | [diff] [blame] | 64 | static int init_mmc_for_env(struct mmc *mmc) |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 65 | { |
| 66 | if (!mmc) { |
| 67 | puts("No MMC card found\n"); |
| 68 | return -1; |
| 69 | } |
| 70 | |
| 71 | if (mmc_init(mmc)) { |
| 72 | puts("MMC init failed\n"); |
Igor Grinberg | e8db8f7 | 2011-11-07 01:14:05 +0000 | [diff] [blame] | 73 | return -1; |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 74 | } |
| 75 | |
Stephen Warren | 9404a5f | 2012-07-30 10:55:44 +0000 | [diff] [blame] | 76 | #ifdef CONFIG_SYS_MMC_ENV_PART |
| 77 | if (CONFIG_SYS_MMC_ENV_PART != mmc->part_num) { |
| 78 | if (mmc_switch_part(CONFIG_SYS_MMC_ENV_DEV, |
| 79 | CONFIG_SYS_MMC_ENV_PART)) { |
| 80 | puts("MMC partition switch failed\n"); |
| 81 | return -1; |
| 82 | } |
| 83 | } |
| 84 | #endif |
| 85 | |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 86 | return 0; |
| 87 | } |
| 88 | |
Stephen Warren | 9404a5f | 2012-07-30 10:55:44 +0000 | [diff] [blame] | 89 | static void fini_mmc_for_env(struct mmc *mmc) |
| 90 | { |
| 91 | #ifdef CONFIG_SYS_MMC_ENV_PART |
| 92 | if (CONFIG_SYS_MMC_ENV_PART != mmc->part_num) |
| 93 | mmc_switch_part(CONFIG_SYS_MMC_ENV_DEV, |
| 94 | mmc->part_num); |
| 95 | #endif |
| 96 | } |
| 97 | |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 98 | #ifdef CONFIG_CMD_SAVEENV |
Igor Grinberg | e8db8f7 | 2011-11-07 01:14:05 +0000 | [diff] [blame] | 99 | static inline int write_env(struct mmc *mmc, unsigned long size, |
| 100 | unsigned long offset, const void *buffer) |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 101 | { |
| 102 | uint blk_start, blk_cnt, n; |
| 103 | |
Igor Grinberg | e8db8f7 | 2011-11-07 01:14:05 +0000 | [diff] [blame] | 104 | blk_start = ALIGN(offset, mmc->write_bl_len) / mmc->write_bl_len; |
| 105 | blk_cnt = ALIGN(size, mmc->write_bl_len) / mmc->write_bl_len; |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 106 | |
| 107 | n = mmc->block_dev.block_write(CONFIG_SYS_MMC_ENV_DEV, blk_start, |
| 108 | blk_cnt, (u_char *)buffer); |
| 109 | |
| 110 | return (n == blk_cnt) ? 0 : -1; |
| 111 | } |
| 112 | |
| 113 | int saveenv(void) |
| 114 | { |
Tom Rini | cd0f4fa | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 115 | ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1); |
Lei Wen | e79f483 | 2010-10-13 11:07:21 +0800 | [diff] [blame] | 116 | ssize_t len; |
| 117 | char *res; |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 118 | struct mmc *mmc = find_mmc_device(CONFIG_SYS_MMC_ENV_DEV); |
Igor Grinberg | e8db8f7 | 2011-11-07 01:14:05 +0000 | [diff] [blame] | 119 | u32 offset; |
Stephen Warren | 9404a5f | 2012-07-30 10:55:44 +0000 | [diff] [blame] | 120 | int ret; |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 121 | |
Stephen Warren | 9404a5f | 2012-07-30 10:55:44 +0000 | [diff] [blame] | 122 | if (init_mmc_for_env(mmc)) |
Mingkai Hu | 97039ab | 2011-01-24 17:09:55 +0000 | [diff] [blame] | 123 | return 1; |
| 124 | |
Stephen Warren | 9404a5f | 2012-07-30 10:55:44 +0000 | [diff] [blame] | 125 | if (mmc_get_env_addr(mmc, &offset)) { |
| 126 | ret = 1; |
| 127 | goto fini; |
| 128 | } |
| 129 | |
Tom Rini | cd0f4fa | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 130 | res = (char *)&env_new->data; |
Joe Hershberger | be11235 | 2012-12-11 22:16:23 -0600 | [diff] [blame] | 131 | len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL); |
Lei Wen | e79f483 | 2010-10-13 11:07:21 +0800 | [diff] [blame] | 132 | if (len < 0) { |
| 133 | error("Cannot export environment: errno = %d\n", errno); |
Stephen Warren | 9404a5f | 2012-07-30 10:55:44 +0000 | [diff] [blame] | 134 | ret = 1; |
| 135 | goto fini; |
Lei Wen | e79f483 | 2010-10-13 11:07:21 +0800 | [diff] [blame] | 136 | } |
Igor Grinberg | e8db8f7 | 2011-11-07 01:14:05 +0000 | [diff] [blame] | 137 | |
Tom Rini | cd0f4fa | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 138 | env_new->crc = crc32(0, &env_new->data[0], ENV_SIZE); |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 139 | printf("Writing to MMC(%d)... ", CONFIG_SYS_MMC_ENV_DEV); |
Stephen Warren | 4036b63 | 2012-05-24 11:38:33 +0000 | [diff] [blame] | 140 | if (write_env(mmc, CONFIG_ENV_SIZE, offset, (u_char *)env_new)) { |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 141 | puts("failed\n"); |
Stephen Warren | 9404a5f | 2012-07-30 10:55:44 +0000 | [diff] [blame] | 142 | ret = 1; |
| 143 | goto fini; |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | puts("done\n"); |
Stephen Warren | 9404a5f | 2012-07-30 10:55:44 +0000 | [diff] [blame] | 147 | ret = 0; |
| 148 | |
| 149 | fini: |
| 150 | fini_mmc_for_env(mmc); |
| 151 | return ret; |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 152 | } |
| 153 | #endif /* CONFIG_CMD_SAVEENV */ |
| 154 | |
Igor Grinberg | e8db8f7 | 2011-11-07 01:14:05 +0000 | [diff] [blame] | 155 | static inline int read_env(struct mmc *mmc, unsigned long size, |
| 156 | unsigned long offset, const void *buffer) |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 157 | { |
| 158 | uint blk_start, blk_cnt, n; |
| 159 | |
Igor Grinberg | e8db8f7 | 2011-11-07 01:14:05 +0000 | [diff] [blame] | 160 | blk_start = ALIGN(offset, mmc->read_bl_len) / mmc->read_bl_len; |
| 161 | blk_cnt = ALIGN(size, mmc->read_bl_len) / mmc->read_bl_len; |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 162 | |
| 163 | n = mmc->block_dev.block_read(CONFIG_SYS_MMC_ENV_DEV, blk_start, |
| 164 | blk_cnt, (uchar *)buffer); |
| 165 | |
| 166 | return (n == blk_cnt) ? 0 : -1; |
| 167 | } |
| 168 | |
| 169 | void env_relocate_spec(void) |
| 170 | { |
| 171 | #if !defined(ENV_IS_EMBEDDED) |
Tom Rini | cd0f4fa | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 172 | ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE); |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 173 | struct mmc *mmc = find_mmc_device(CONFIG_SYS_MMC_ENV_DEV); |
Mingkai Hu | 97039ab | 2011-01-24 17:09:55 +0000 | [diff] [blame] | 174 | u32 offset; |
Stephen Warren | 9404a5f | 2012-07-30 10:55:44 +0000 | [diff] [blame] | 175 | int ret; |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 176 | |
Stephen Warren | 9404a5f | 2012-07-30 10:55:44 +0000 | [diff] [blame] | 177 | if (init_mmc_for_env(mmc)) { |
| 178 | ret = 1; |
| 179 | goto err; |
| 180 | } |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 181 | |
Stephen Warren | 9404a5f | 2012-07-30 10:55:44 +0000 | [diff] [blame] | 182 | if (mmc_get_env_addr(mmc, &offset)) { |
| 183 | ret = 1; |
| 184 | goto fini; |
| 185 | } |
| 186 | |
Tom Rini | cd0f4fa | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 187 | if (read_env(mmc, CONFIG_ENV_SIZE, offset, buf)) { |
Stephen Warren | 9404a5f | 2012-07-30 10:55:44 +0000 | [diff] [blame] | 188 | ret = 1; |
| 189 | goto fini; |
| 190 | } |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 191 | |
Tom Rini | cd0f4fa | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 192 | env_import(buf, 1); |
Stephen Warren | 9404a5f | 2012-07-30 10:55:44 +0000 | [diff] [blame] | 193 | ret = 0; |
| 194 | |
| 195 | fini: |
| 196 | fini_mmc_for_env(mmc); |
| 197 | err: |
| 198 | if (ret) |
| 199 | set_default_env(NULL); |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 200 | #endif |
| 201 | } |