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 | |
| 35 | /* references to names in env_common.c */ |
| 36 | extern uchar default_environment[]; |
| 37 | |
| 38 | char *env_name_spec = "MMC"; |
| 39 | |
| 40 | #ifdef ENV_IS_EMBEDDED |
| 41 | extern uchar environment[]; |
| 42 | env_t *env_ptr = (env_t *)(&environment[0]); |
| 43 | #else /* ! ENV_IS_EMBEDDED */ |
| 44 | env_t *env_ptr = NULL; |
| 45 | #endif /* ENV_IS_EMBEDDED */ |
| 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 | |
Mingkai Hu | a55fce3 | 2011-01-24 17:09:55 +0000 | [diff] [blame] | 54 | #if !defined(CONFIG_ENV_OFFSET) |
| 55 | #define CONFIG_ENV_OFFSET 0 |
| 56 | #endif |
| 57 | |
| 58 | static int __mmc_get_env_addr(struct mmc *mmc, u32 *env_addr) |
| 59 | { |
| 60 | *env_addr = CONFIG_ENV_OFFSET; |
| 61 | return 0; |
| 62 | } |
| 63 | __attribute__((weak, alias("__mmc_get_env_addr"))) |
| 64 | int mmc_get_env_addr(struct mmc *mmc, u32 *env_addr); |
| 65 | |
| 66 | |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 67 | uchar env_get_char_spec(int index) |
| 68 | { |
| 69 | return *((uchar *)(gd->env_addr + index)); |
| 70 | } |
| 71 | |
| 72 | int env_init(void) |
| 73 | { |
| 74 | /* use default */ |
| 75 | gd->env_addr = (ulong)&default_environment[0]; |
| 76 | gd->env_valid = 1; |
| 77 | |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | int init_mmc_for_env(struct mmc *mmc) |
| 82 | { |
| 83 | if (!mmc) { |
| 84 | puts("No MMC card found\n"); |
| 85 | return -1; |
| 86 | } |
| 87 | |
| 88 | if (mmc_init(mmc)) { |
| 89 | puts("MMC init failed\n"); |
| 90 | return -1; |
| 91 | } |
| 92 | |
| 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | #ifdef CONFIG_CMD_SAVEENV |
| 97 | |
| 98 | inline int write_env(struct mmc *mmc, unsigned long size, |
| 99 | unsigned long offset, const void *buffer) |
| 100 | { |
| 101 | uint blk_start, blk_cnt, n; |
| 102 | |
| 103 | blk_start = ALIGN(offset, mmc->write_bl_len) / mmc->write_bl_len; |
| 104 | blk_cnt = ALIGN(size, mmc->write_bl_len) / mmc->write_bl_len; |
| 105 | |
| 106 | n = mmc->block_dev.block_write(CONFIG_SYS_MMC_ENV_DEV, blk_start, |
| 107 | blk_cnt, (u_char *)buffer); |
| 108 | |
| 109 | return (n == blk_cnt) ? 0 : -1; |
| 110 | } |
| 111 | |
| 112 | int saveenv(void) |
| 113 | { |
Lei Wen | ff833ed | 2010-10-13 11:07:21 +0800 | [diff] [blame] | 114 | env_t env_new; |
| 115 | ssize_t len; |
| 116 | char *res; |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 117 | struct mmc *mmc = find_mmc_device(CONFIG_SYS_MMC_ENV_DEV); |
Mingkai Hu | a55fce3 | 2011-01-24 17:09:55 +0000 | [diff] [blame] | 118 | u32 offset; |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 119 | |
| 120 | if (init_mmc_for_env(mmc)) |
| 121 | return 1; |
| 122 | |
Mingkai Hu | a55fce3 | 2011-01-24 17:09:55 +0000 | [diff] [blame] | 123 | if(mmc_get_env_addr(mmc, &offset)) |
| 124 | return 1; |
| 125 | |
Lei Wen | ff833ed | 2010-10-13 11:07:21 +0800 | [diff] [blame] | 126 | res = (char *)&env_new.data; |
Mike Frysinger | 92d7a99 | 2010-12-08 06:26:04 -0500 | [diff] [blame] | 127 | len = hexport_r(&env_htab, '\0', &res, ENV_SIZE); |
Lei Wen | ff833ed | 2010-10-13 11:07:21 +0800 | [diff] [blame] | 128 | if (len < 0) { |
| 129 | error("Cannot export environment: errno = %d\n", errno); |
| 130 | return 1; |
| 131 | } |
| 132 | env_new.crc = crc32(0, env_new.data, ENV_SIZE); |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 133 | printf("Writing to MMC(%d)... ", CONFIG_SYS_MMC_ENV_DEV); |
Mingkai Hu | a55fce3 | 2011-01-24 17:09:55 +0000 | [diff] [blame] | 134 | if (write_env(mmc, CONFIG_ENV_SIZE, offset, (u_char *)&env_new)) { |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 135 | puts("failed\n"); |
| 136 | return 1; |
| 137 | } |
| 138 | |
| 139 | puts("done\n"); |
| 140 | return 0; |
| 141 | } |
| 142 | #endif /* CONFIG_CMD_SAVEENV */ |
| 143 | |
| 144 | inline int read_env(struct mmc *mmc, unsigned long size, |
| 145 | unsigned long offset, const void *buffer) |
| 146 | { |
| 147 | uint blk_start, blk_cnt, n; |
| 148 | |
| 149 | blk_start = ALIGN(offset, mmc->read_bl_len) / mmc->read_bl_len; |
| 150 | blk_cnt = ALIGN(size, mmc->read_bl_len) / mmc->read_bl_len; |
| 151 | |
| 152 | n = mmc->block_dev.block_read(CONFIG_SYS_MMC_ENV_DEV, blk_start, |
| 153 | blk_cnt, (uchar *)buffer); |
| 154 | |
| 155 | return (n == blk_cnt) ? 0 : -1; |
| 156 | } |
| 157 | |
| 158 | void env_relocate_spec(void) |
| 159 | { |
| 160 | #if !defined(ENV_IS_EMBEDDED) |
Mingkai Hu | a55fce3 | 2011-01-24 17:09:55 +0000 | [diff] [blame] | 161 | char buf[CONFIG_ENV_SIZE]; |
Steve Sakoman | a93ec57 | 2010-10-11 05:51:39 -0700 | [diff] [blame] | 162 | |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 163 | struct mmc *mmc = find_mmc_device(CONFIG_SYS_MMC_ENV_DEV); |
Mingkai Hu | a55fce3 | 2011-01-24 17:09:55 +0000 | [diff] [blame] | 164 | u32 offset; |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 165 | |
Steve Sakoman | a93ec57 | 2010-10-11 05:51:39 -0700 | [diff] [blame] | 166 | if (init_mmc_for_env(mmc)) { |
| 167 | use_default(); |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 168 | return; |
Steve Sakoman | a93ec57 | 2010-10-11 05:51:39 -0700 | [diff] [blame] | 169 | } |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 170 | |
Mingkai Hu | a55fce3 | 2011-01-24 17:09:55 +0000 | [diff] [blame] | 171 | if(mmc_get_env_addr(mmc, &offset)) { |
| 172 | use_default(); |
| 173 | return ; |
| 174 | } |
| 175 | |
| 176 | if (read_env(mmc, CONFIG_ENV_SIZE, offset, buf)) { |
Steve Sakoman | a93ec57 | 2010-10-11 05:51:39 -0700 | [diff] [blame] | 177 | use_default(); |
| 178 | return; |
| 179 | } |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 180 | |
Steve Sakoman | a93ec57 | 2010-10-11 05:51:39 -0700 | [diff] [blame] | 181 | env_import(buf, 1); |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 182 | #endif |
| 183 | } |
| 184 | |
| 185 | #if !defined(ENV_IS_EMBEDDED) |
| 186 | static void use_default() |
| 187 | { |
Steve Sakoman | 0e657e8 | 2010-10-05 15:31:38 -0700 | [diff] [blame] | 188 | set_default_env(NULL); |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 189 | } |
| 190 | #endif |