]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - common/env_ubi.c
env: Add support for UBI environment
[karo-tx-uboot.git] / common / env_ubi.c
1 /*
2  * (c) Copyright 2012 by National Instruments,
3  *        Joe Hershberger <joe.hershberger@ni.com>
4  *
5  * See file CREDITS for list of people who contributed to this
6  * project.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of
11  * the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21  * MA 02111-1307 USA
22  */
23
24 #include <common.h>
25
26 #include <command.h>
27 #include <environment.h>
28 #include <errno.h>
29 #include <malloc.h>
30 #include <search.h>
31 #include <ubi_uboot.h>
32 #undef crc32
33
34 char *env_name_spec = "UBI";
35
36 env_t *env_ptr;
37
38 DECLARE_GLOBAL_DATA_PTR;
39
40 int env_init(void)
41 {
42         /* use default */
43         gd->env_addr = (ulong)&default_environment[0];
44         gd->env_valid = 1;
45
46         return 0;
47 }
48
49 #ifdef CONFIG_CMD_SAVEENV
50 int saveenv(void)
51 {
52         ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
53         ssize_t len;
54         char *res;
55
56         res = (char *)&env_new->data;
57         len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
58         if (len < 0) {
59                 error("Cannot export environment: errno = %d\n", errno);
60                 return 1;
61         }
62
63         if (ubi_part(CONFIG_ENV_UBI_PART, NULL)) {
64                 printf("\n** Cannot find mtd partition \"%s\"\n",
65                        CONFIG_ENV_UBI_PART);
66                 return 1;
67         }
68
69         env_new->crc = crc32(0, env_new->data, ENV_SIZE);
70
71         if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME, (void *)env_new,
72                              CONFIG_ENV_SIZE)) {
73                 printf("\n** Unable to write env to %s:%s **\n",
74                        CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME);
75                 return 1;
76         }
77
78         puts("done\n");
79         return 0;
80 }
81 #endif /* CONFIG_CMD_SAVEENV */
82
83 void env_relocate_spec(void)
84 {
85         ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
86
87         if (ubi_part(CONFIG_ENV_UBI_PART, NULL)) {
88                 printf("\n** Cannot find mtd partition \"%s\"\n",
89                        CONFIG_ENV_UBI_PART);
90                 set_default_env(NULL);
91                 return;
92         }
93
94         if (ubi_volume_read(CONFIG_ENV_UBI_VOLUME, (void *)&buf,
95                             CONFIG_ENV_SIZE)) {
96                 printf("\n** Unable to read env from %s:%s **\n",
97                        CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME);
98                 set_default_env(NULL);
99                 return;
100         }
101
102         env_import(buf, 1);
103 }