]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - common/env_onenand.c
env: move extern default_environment[] to environment.h
[karo-tx-uboot.git] / common / env_onenand.c
1 /*
2  * (C) Copyright 2010 DENX Software Engineering
3  * Wolfgang Denk <wd@denx.de>
4  *
5  * (C) Copyright 2005-2009 Samsung Electronics
6  * Kyungmin Park <kyungmin.park@samsung.com>
7  *
8  * See file CREDITS for list of people who contributed to this
9  * project.
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License as
13  * published by the Free Software Foundation; either version 2 of
14  * the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
24  * MA 02111-1307 USA
25  */
26
27 #include <common.h>
28 #include <command.h>
29 #include <environment.h>
30 #include <linux/stddef.h>
31 #include <malloc.h>
32 #include <search.h>
33 #include <errno.h>
34
35 #include <linux/mtd/compat.h>
36 #include <linux/mtd/mtd.h>
37 #include <linux/mtd/onenand.h>
38
39 extern struct mtd_info onenand_mtd;
40 extern struct onenand_chip onenand_chip;
41
42 char *env_name_spec = "OneNAND";
43
44 #define ONENAND_MAX_ENV_SIZE    4096
45 #define ONENAND_ENV_SIZE(mtd)   (ONENAND_MAX_ENV_SIZE - ENV_HEADER_SIZE)
46
47 #ifdef ENV_IS_EMBEDDED
48 extern uchar environment[];
49 #endif /* ENV_IS_EMBEDDED */
50
51 DECLARE_GLOBAL_DATA_PTR;
52
53 uchar env_get_char_spec(int index)
54 {
55         return (*((uchar *)(gd->env_addr + index)));
56 }
57
58 void env_relocate_spec(void)
59 {
60         struct mtd_info *mtd = &onenand_mtd;
61 #ifdef CONFIG_ENV_ADDR_FLEX
62         struct onenand_chip *this = &onenand_chip;
63 #endif
64         int rc;
65         size_t retlen;
66 #ifdef ENV_IS_EMBEDDED
67         char *buf = (char *)&environment[0];
68 #else
69         loff_t env_addr = CONFIG_ENV_ADDR;
70         char onenand_env[ONENAND_MAX_ENV_SIZE];
71         char *buf = (char *)&onenand_env[0];
72 #endif /* ENV_IS_EMBEDDED */
73
74 #ifndef ENV_IS_EMBEDDED
75 # ifdef CONFIG_ENV_ADDR_FLEX
76         if (FLEXONENAND(this))
77                 env_addr = CONFIG_ENV_ADDR_FLEX;
78 # endif
79         /* Check OneNAND exist */
80         if (mtd->writesize)
81                 /* Ignore read fail */
82                 mtd->read(mtd, env_addr, ONENAND_MAX_ENV_SIZE,
83                              &retlen, (u_char *)buf);
84         else
85                 mtd->writesize = MAX_ONENAND_PAGESIZE;
86 #endif /* !ENV_IS_EMBEDDED */
87
88         rc = env_import(buf, 1);
89         if (rc)
90                 gd->env_valid = 1;
91 }
92
93 int saveenv(void)
94 {
95         env_t   env_new;
96         ssize_t len;
97         char    *res;
98         struct mtd_info *mtd = &onenand_mtd;
99 #ifdef CONFIG_ENV_ADDR_FLEX
100         struct onenand_chip *this = &onenand_chip;
101 #endif
102         loff_t  env_addr = CONFIG_ENV_ADDR;
103         size_t  retlen;
104         struct erase_info instr = {
105                 .callback       = NULL,
106         };
107
108         res = (char *)&env_new.data;
109         len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, 0, NULL);
110         if (len < 0) {
111                 error("Cannot export environment: errno = %d\n", errno);
112                 return 1;
113         }
114         env_new.crc = crc32(0, env_new.data, ENV_SIZE);
115
116         instr.len = CONFIG_ENV_SIZE;
117 #ifdef CONFIG_ENV_ADDR_FLEX
118         if (FLEXONENAND(this)) {
119                 env_addr = CONFIG_ENV_ADDR_FLEX;
120                 instr.len = CONFIG_ENV_SIZE_FLEX;
121                 instr.len <<= onenand_mtd.eraseregions[0].numblocks == 1 ?
122                                 1 : 0;
123         }
124 #endif
125         instr.addr = env_addr;
126         instr.mtd = mtd;
127         if (mtd->erase(mtd, &instr)) {
128                 printf("OneNAND: erase failed at 0x%08llx\n", env_addr);
129                 return 1;
130         }
131
132         if (mtd->write(mtd, env_addr, ONENAND_MAX_ENV_SIZE, &retlen,
133              (u_char *)&env_new)) {
134                 printf("OneNAND: write failed at 0x%llx\n", instr.addr);
135                 return 2;
136         }
137
138         return 0;
139 }
140
141 int env_init(void)
142 {
143         /* use default */
144         gd->env_addr = (ulong) & default_environment[0];
145         gd->env_valid = 1;
146
147         return 0;
148 }