sandbox: spi: Adjust 'sf test' to work on sandbox
[platform/kernel/u-boot.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  * SPDX-License-Identifier:     GPL-2.0+
9  */
10
11 #include <common.h>
12 #include <command.h>
13 #include <environment.h>
14 #include <linux/stddef.h>
15 #include <malloc.h>
16 #include <search.h>
17 #include <errno.h>
18 #include <onenand_uboot.h>
19
20 #include <linux/compat.h>
21 #include <linux/mtd/mtd.h>
22 #include <linux/mtd/onenand.h>
23
24 char *env_name_spec = "OneNAND";
25
26 #define ONENAND_MAX_ENV_SIZE    CONFIG_ENV_SIZE
27 #define ONENAND_ENV_SIZE(mtd)   (ONENAND_MAX_ENV_SIZE - ENV_HEADER_SIZE)
28
29 DECLARE_GLOBAL_DATA_PTR;
30
31 void env_relocate_spec(void)
32 {
33         struct mtd_info *mtd = &onenand_mtd;
34 #ifdef CONFIG_ENV_ADDR_FLEX
35         struct onenand_chip *this = &onenand_chip;
36 #endif
37         int rc;
38         size_t retlen;
39 #ifdef ENV_IS_EMBEDDED
40         char *buf = (char *)&environment;
41 #else
42         loff_t env_addr = CONFIG_ENV_ADDR;
43         char onenand_env[ONENAND_MAX_ENV_SIZE];
44         char *buf = (char *)&onenand_env[0];
45 #endif /* ENV_IS_EMBEDDED */
46
47 #ifndef ENV_IS_EMBEDDED
48 # ifdef CONFIG_ENV_ADDR_FLEX
49         if (FLEXONENAND(this))
50                 env_addr = CONFIG_ENV_ADDR_FLEX;
51 # endif
52         /* Check OneNAND exist */
53         if (mtd->writesize)
54                 /* Ignore read fail */
55                 mtd_read(mtd, env_addr, ONENAND_MAX_ENV_SIZE,
56                                 &retlen, (u_char *)buf);
57         else
58                 mtd->writesize = MAX_ONENAND_PAGESIZE;
59 #endif /* !ENV_IS_EMBEDDED */
60
61         rc = env_import(buf, 1);
62         if (rc)
63                 gd->env_valid = 1;
64 }
65
66 int saveenv(void)
67 {
68         env_t   env_new;
69         ssize_t len;
70         char    *res;
71         struct mtd_info *mtd = &onenand_mtd;
72 #ifdef CONFIG_ENV_ADDR_FLEX
73         struct onenand_chip *this = &onenand_chip;
74 #endif
75         loff_t  env_addr = CONFIG_ENV_ADDR;
76         size_t  retlen;
77         struct erase_info instr = {
78                 .callback       = NULL,
79         };
80
81         res = (char *)&env_new.data;
82         len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
83         if (len < 0) {
84                 error("Cannot export environment: errno = %d\n", errno);
85                 return 1;
86         }
87         env_new.crc = crc32(0, env_new.data, ENV_SIZE);
88
89         instr.len = CONFIG_ENV_SIZE;
90 #ifdef CONFIG_ENV_ADDR_FLEX
91         if (FLEXONENAND(this)) {
92                 env_addr = CONFIG_ENV_ADDR_FLEX;
93                 instr.len = CONFIG_ENV_SIZE_FLEX;
94                 instr.len <<= onenand_mtd.eraseregions[0].numblocks == 1 ?
95                                 1 : 0;
96         }
97 #endif
98         instr.addr = env_addr;
99         instr.mtd = mtd;
100         if (mtd_erase(mtd, &instr)) {
101                 printf("OneNAND: erase failed at 0x%08llx\n", env_addr);
102                 return 1;
103         }
104
105         if (mtd_write(mtd, env_addr, ONENAND_MAX_ENV_SIZE, &retlen,
106                         (u_char *)&env_new)) {
107                 printf("OneNAND: write failed at 0x%llx\n", instr.addr);
108                 return 2;
109         }
110
111         return 0;
112 }
113
114 int env_init(void)
115 {
116         /* use default */
117         gd->env_addr = (ulong)&default_environment[0];
118         gd->env_valid = 1;
119
120         return 0;
121 }