Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 1 | /* |
Mingkai Hu | a55fce3 | 2011-01-24 17:09:55 +0000 | [diff] [blame] | 2 | * (C) Copyright 2008-2011 Freescale Semiconductor, Inc. |
Terry Lv | b4eceac | 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 | 9f84f21 | 2010-11-10 07:39:23 +0800 | [diff] [blame] | 32 | #include <search.h> |
Lei Wen | ff833ed | 2010-10-13 11:07:21 +0800 | [diff] [blame] | 33 | #include <errno.h> |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 34 | |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 35 | char *env_name_spec = "MMC"; |
| 36 | |
| 37 | #ifdef ENV_IS_EMBEDDED |
Igor Grinberg | 23b54b9 | 2011-11-17 06:07:23 +0000 | [diff] [blame] | 38 | env_t *env_ptr = &environment; |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 39 | #else /* ! ENV_IS_EMBEDDED */ |
Igor Grinberg | ecbfb62 | 2011-11-07 01:14:05 +0000 | [diff] [blame] | 40 | env_t *env_ptr; |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 41 | #endif /* ENV_IS_EMBEDDED */ |
| 42 | |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 43 | DECLARE_GLOBAL_DATA_PTR; |
| 44 | |
Mingkai Hu | a55fce3 | 2011-01-24 17:09:55 +0000 | [diff] [blame] | 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 | } |
Igor Grinberg | ecbfb62 | 2011-11-07 01:14:05 +0000 | [diff] [blame] | 54 | int mmc_get_env_addr(struct mmc *mmc, u32 *env_addr) |
| 55 | __attribute__((weak, alias("__mmc_get_env_addr"))); |
Mingkai Hu | a55fce3 | 2011-01-24 17:09:55 +0000 | [diff] [blame] | 56 | |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 57 | uchar env_get_char_spec(int index) |
| 58 | { |
| 59 | return *((uchar *)(gd->env_addr + index)); |
| 60 | } |
| 61 | |
| 62 | int env_init(void) |
| 63 | { |
| 64 | /* use default */ |
Igor Grinberg | ecbfb62 | 2011-11-07 01:14:05 +0000 | [diff] [blame] | 65 | gd->env_addr = (ulong)&default_environment[0]; |
| 66 | gd->env_valid = 1; |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 67 | |
| 68 | return 0; |
| 69 | } |
| 70 | |
Igor Grinberg | ecbfb62 | 2011-11-07 01:14:05 +0000 | [diff] [blame] | 71 | static int init_mmc_for_env(struct mmc *mmc) |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 72 | { |
| 73 | if (!mmc) { |
| 74 | puts("No MMC card found\n"); |
| 75 | return -1; |
| 76 | } |
| 77 | |
| 78 | if (mmc_init(mmc)) { |
| 79 | puts("MMC init failed\n"); |
Igor Grinberg | ecbfb62 | 2011-11-07 01:14:05 +0000 | [diff] [blame] | 80 | return -1; |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | #ifdef CONFIG_CMD_SAVEENV |
Igor Grinberg | ecbfb62 | 2011-11-07 01:14:05 +0000 | [diff] [blame] | 87 | static inline int write_env(struct mmc *mmc, unsigned long size, |
| 88 | unsigned long offset, const void *buffer) |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 89 | { |
| 90 | uint blk_start, blk_cnt, n; |
| 91 | |
Igor Grinberg | ecbfb62 | 2011-11-07 01:14:05 +0000 | [diff] [blame] | 92 | blk_start = ALIGN(offset, mmc->write_bl_len) / mmc->write_bl_len; |
| 93 | blk_cnt = ALIGN(size, mmc->write_bl_len) / mmc->write_bl_len; |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 94 | |
| 95 | n = mmc->block_dev.block_write(CONFIG_SYS_MMC_ENV_DEV, blk_start, |
| 96 | blk_cnt, (u_char *)buffer); |
| 97 | |
| 98 | return (n == blk_cnt) ? 0 : -1; |
| 99 | } |
| 100 | |
| 101 | int saveenv(void) |
| 102 | { |
Lei Wen | ff833ed | 2010-10-13 11:07:21 +0800 | [diff] [blame] | 103 | env_t env_new; |
| 104 | ssize_t len; |
| 105 | char *res; |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 106 | struct mmc *mmc = find_mmc_device(CONFIG_SYS_MMC_ENV_DEV); |
Igor Grinberg | ecbfb62 | 2011-11-07 01:14:05 +0000 | [diff] [blame] | 107 | u32 offset; |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 108 | |
Igor Grinberg | ecbfb62 | 2011-11-07 01:14:05 +0000 | [diff] [blame] | 109 | if (init_mmc_for_env(mmc) || mmc_get_env_addr(mmc, &offset)) |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 110 | return 1; |
| 111 | |
Lei Wen | ff833ed | 2010-10-13 11:07:21 +0800 | [diff] [blame] | 112 | res = (char *)&env_new.data; |
Wolfgang Denk | 1e3cdf3 | 2011-11-06 22:49:44 +0100 | [diff] [blame] | 113 | len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, 0, NULL); |
Lei Wen | ff833ed | 2010-10-13 11:07:21 +0800 | [diff] [blame] | 114 | if (len < 0) { |
| 115 | error("Cannot export environment: errno = %d\n", errno); |
| 116 | return 1; |
| 117 | } |
Igor Grinberg | ecbfb62 | 2011-11-07 01:14:05 +0000 | [diff] [blame] | 118 | |
| 119 | env_new.crc = crc32(0, env_new.data, ENV_SIZE); |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 120 | printf("Writing to MMC(%d)... ", CONFIG_SYS_MMC_ENV_DEV); |
Mingkai Hu | a55fce3 | 2011-01-24 17:09:55 +0000 | [diff] [blame] | 121 | if (write_env(mmc, CONFIG_ENV_SIZE, offset, (u_char *)&env_new)) { |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 122 | puts("failed\n"); |
| 123 | return 1; |
| 124 | } |
| 125 | |
| 126 | puts("done\n"); |
| 127 | return 0; |
| 128 | } |
| 129 | #endif /* CONFIG_CMD_SAVEENV */ |
| 130 | |
Igor Grinberg | ecbfb62 | 2011-11-07 01:14:05 +0000 | [diff] [blame] | 131 | static inline int read_env(struct mmc *mmc, unsigned long size, |
| 132 | unsigned long offset, const void *buffer) |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 133 | { |
| 134 | uint blk_start, blk_cnt, n; |
| 135 | |
Igor Grinberg | ecbfb62 | 2011-11-07 01:14:05 +0000 | [diff] [blame] | 136 | blk_start = ALIGN(offset, mmc->read_bl_len) / mmc->read_bl_len; |
| 137 | blk_cnt = ALIGN(size, mmc->read_bl_len) / mmc->read_bl_len; |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 138 | |
| 139 | n = mmc->block_dev.block_read(CONFIG_SYS_MMC_ENV_DEV, blk_start, |
| 140 | blk_cnt, (uchar *)buffer); |
| 141 | |
| 142 | return (n == blk_cnt) ? 0 : -1; |
| 143 | } |
| 144 | |
| 145 | void env_relocate_spec(void) |
| 146 | { |
| 147 | #if !defined(ENV_IS_EMBEDDED) |
Mingkai Hu | a55fce3 | 2011-01-24 17:09:55 +0000 | [diff] [blame] | 148 | char buf[CONFIG_ENV_SIZE]; |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 149 | struct mmc *mmc = find_mmc_device(CONFIG_SYS_MMC_ENV_DEV); |
Mingkai Hu | a55fce3 | 2011-01-24 17:09:55 +0000 | [diff] [blame] | 150 | u32 offset; |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 151 | |
Igor Grinberg | ecbfb62 | 2011-11-07 01:14:05 +0000 | [diff] [blame] | 152 | if (init_mmc_for_env(mmc) || mmc_get_env_addr(mmc, &offset)) |
| 153 | return set_default_env(NULL); |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 154 | |
Igor Grinberg | ecbfb62 | 2011-11-07 01:14:05 +0000 | [diff] [blame] | 155 | if (read_env(mmc, CONFIG_ENV_SIZE, offset, buf)) |
| 156 | return set_default_env(NULL); |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 157 | |
Steve Sakoman | a93ec57 | 2010-10-11 05:51:39 -0700 | [diff] [blame] | 158 | env_import(buf, 1); |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 159 | #endif |
| 160 | } |