]>
Commit | Line | Data |
---|---|---|
13a5695b WD |
1 | /* |
2 | * (C) Copyright 2004 | |
3 | * Jian Zhang, Texas Instruments, jzhang@ti.com. | |
4 | ||
5 | * (C) Copyright 2000-2004 | |
6 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. | |
7 | * | |
8 | * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com> | |
9 | * Andreas Heppel <aheppel@sysgo.de> | |
10 | ||
11 | * See file CREDITS for list of people who contributed to this | |
12 | * project. | |
13 | * | |
14 | * This program is free software; you can redistribute it and/or | |
15 | * modify it under the terms of the GNU General Public License as | |
16 | * published by the Free Software Foundation; either version 2 of | |
17 | * the License, or (at your option) any later version. | |
18 | * | |
19 | * This program is distributed in the hope that it will be useful, | |
20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
22 | * GNU General Public License for more details. | |
23 | * | |
24 | * You should have received a copy of the GNU General Public License | |
25 | * along with this program; if not, write to the Free Software | |
26 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, | |
27 | * MA 02111-1307 USA | |
28 | */ | |
29 | ||
30 | /* #define DEBUG */ | |
31 | ||
32 | #include <common.h> | |
33 | ||
34 | #if defined(CFG_ENV_IS_IN_NAND) /* Environment is in Nand Flash */ | |
35 | ||
36 | #include <command.h> | |
37 | #include <environment.h> | |
38 | #include <linux/stddef.h> | |
39 | #include <malloc.h> | |
40 | #include <linux/mtd/nand.h> | |
41 | ||
42 | #if ((CONFIG_COMMANDS&(CFG_CMD_ENV|CFG_CMD_NAND)) == (CFG_CMD_ENV|CFG_CMD_NAND)) | |
43 | #define CMD_SAVEENV | |
44 | #endif | |
45 | ||
46 | #if defined(CFG_ENV_SIZE_REDUND) | |
47 | #error CFG_ENV_SIZE_REDUND not supported yet | |
48 | #endif | |
49 | ||
50 | #if defined(CFG_ENV_ADDR_REDUND) | |
51 | #error CFG_ENV_ADDR_REDUND and CFG_ENV_IS_IN_NAND not supported yet | |
52 | #endif | |
53 | ||
54 | ||
55 | #ifdef CONFIG_INFERNO | |
56 | #error CONFIG_INFERNO not supported yet | |
57 | #endif | |
58 | ||
59 | /* references to names in cmd_nand.c */ | |
60 | #define NANDRW_READ 0x01 | |
61 | #define NANDRW_WRITE 0x00 | |
62 | #define NANDRW_JFFS2 0x02 | |
63 | extern struct nand_chip nand_dev_desc[]; | |
64 | int nand_rw (struct nand_chip* nand, int cmd, | |
65 | size_t start, size_t len, | |
66 | size_t * retlen, u_char * buf); | |
67 | int nand_erase(struct nand_chip* nand, size_t ofs, | |
68 | size_t len, int clean); | |
69 | ||
70 | /* references to names in env_common.c */ | |
71 | extern uchar default_environment[]; | |
72 | extern int default_environment_size; | |
73 | ||
74 | char * env_name_spec = "NAND"; | |
75 | ||
76 | ||
77 | #ifdef ENV_IS_EMBEDDED | |
78 | extern uchar environment[]; | |
79 | env_t *env_ptr = (env_t *)(&environment[0]); | |
80 | #else /* ! ENV_IS_EMBEDDED */ | |
49822e23 | 81 | env_t *env_ptr = 0; |
13a5695b WD |
82 | #endif /* ENV_IS_EMBEDDED */ |
83 | ||
84 | ||
85 | /* local functions */ | |
86 | static void use_default(void); | |
87 | ||
88 | ||
89 | uchar env_get_char_spec (int index) | |
90 | { | |
91 | DECLARE_GLOBAL_DATA_PTR; | |
92 | ||
93 | return ( *((uchar *)(gd->env_addr + index)) ); | |
94 | } | |
95 | ||
96 | ||
97 | /* this is called before nand_init() | |
98 | * so we can't read Nand to validate env data. | |
99 | * Mark it OK for now. env_relocate() in env_common.c | |
100 | * will call our relocate function which will does | |
101 | * the real validation. | |
102 | */ | |
103 | int env_init(void) | |
104 | { | |
105 | DECLARE_GLOBAL_DATA_PTR; | |
106 | ||
107 | gd->env_addr = (ulong)&default_environment[0]; | |
108 | gd->env_valid = 1; | |
109 | ||
110 | return (0); | |
111 | } | |
112 | ||
113 | #ifdef CMD_SAVEENV | |
114 | int saveenv(void) | |
115 | { | |
116 | int total, ret = 0; | |
117 | puts ("Erasing Nand..."); | |
118 | if (nand_erase(nand_dev_desc + 0, CFG_ENV_OFFSET, CFG_ENV_SIZE, 0)) | |
119 | return 1; | |
120 | ||
121 | puts ("Writing to Nand... "); | |
122 | ret = nand_rw(nand_dev_desc + 0, | |
123 | NANDRW_WRITE | NANDRW_JFFS2, CFG_ENV_OFFSET, CFG_ENV_SIZE, | |
124 | &total, (u_char*)env_ptr); | |
125 | if (ret || total != CFG_ENV_SIZE) | |
126 | return 1; | |
127 | ||
128 | puts ("done\n"); | |
129 | return ret; | |
130 | } | |
131 | #endif /* CMD_SAVEENV */ | |
132 | ||
133 | ||
134 | void env_relocate_spec (void) | |
135 | { | |
136 | #if !defined(ENV_IS_EMBEDDED) | |
137 | int ret, total; | |
138 | ||
139 | ret = nand_rw(nand_dev_desc + 0, | |
140 | NANDRW_READ | NANDRW_JFFS2, CFG_ENV_OFFSET, CFG_ENV_SIZE, | |
141 | &total, (u_char*)env_ptr); | |
142 | if (ret || total != CFG_ENV_SIZE) | |
143 | return use_default(); | |
144 | ||
145 | if (crc32(0, env_ptr->data, ENV_SIZE) != env_ptr->crc) | |
146 | return use_default(); | |
147 | #endif /* ! ENV_IS_EMBEDDED */ | |
148 | ||
149 | } | |
150 | ||
151 | static void use_default() | |
152 | { | |
153 | DECLARE_GLOBAL_DATA_PTR; | |
154 | ||
155 | puts ("*** Warning - bad CRC or NAND, using default environment\n\n"); | |
156 | ||
157 | if (default_environment_size > CFG_ENV_SIZE){ | |
158 | puts ("*** Error - default environment is too large\n\n"); | |
159 | return; | |
160 | } | |
161 | ||
162 | memset (env_ptr, 0, sizeof(env_t)); | |
163 | memcpy (env_ptr->data, | |
164 | default_environment, | |
165 | default_environment_size); | |
166 | env_ptr->crc = crc32(0, env_ptr->data, ENV_SIZE); | |
167 | gd->env_valid = 1; | |
168 | ||
169 | } | |
170 | ||
171 | #endif /* CFG_ENV_IS_IN_NAND */ |