blob: 8bfcc94306d3a2a8b12ba9f18379d901e0ce7628 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Peng Fan125d1932016-04-03 21:52:13 +08002/*
3 * (C) Copyright 2010-2016 Freescale Semiconductor, Inc.
Peng Fan125d1932016-04-03 21:52:13 +08004 */
5
6/* #define DEBUG */
7
8#include <common.h>
9
10#include <command.h>
Simon Glass0ac7d722019-08-01 09:47:00 -060011#include <env.h>
Simon Glassf3998fd2019-08-02 09:44:25 -060012#include <env_internal.h>
Peng Fan125d1932016-04-03 21:52:13 +080013#include <linux/stddef.h>
14#include <errno.h>
15#include <memalign.h>
16#include <sata.h>
17#include <search.h>
18
Tom Rinia8992e72019-11-18 20:02:07 -050019#if defined(CONFIG_ENV_OFFSET_REDUND)
Peng Fan125d1932016-04-03 21:52:13 +080020#error ENV REDUND not supported
21#endif
22
23#if !defined(CONFIG_ENV_OFFSET) || !defined(CONFIG_ENV_SIZE)
24#error CONFIG_ENV_OFFSET or CONFIG_ENV_SIZE not defined
25#endif
26
Peng Fan125d1932016-04-03 21:52:13 +080027__weak int sata_get_env_dev(void)
28{
29 return CONFIG_SYS_SATA_ENV_DEV;
30}
31
Peng Fan125d1932016-04-03 21:52:13 +080032#ifdef CONFIG_CMD_SAVEENV
33static inline int write_env(struct blk_desc *sata, unsigned long size,
34 unsigned long offset, void *buffer)
35{
36 uint blk_start, blk_cnt, n;
37
38 blk_start = ALIGN(offset, sata->blksz) / sata->blksz;
39 blk_cnt = ALIGN(size, sata->blksz) / sata->blksz;
40
41 n = blk_dwrite(sata, blk_start, blk_cnt, buffer);
42
43 return (n == blk_cnt) ? 0 : -1;
44}
45
Simon Glasse5bce242017-08-03 12:22:01 -060046static int env_sata_save(void)
Peng Fan125d1932016-04-03 21:52:13 +080047{
48 ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
49 struct blk_desc *sata = NULL;
50 int env_sata, ret;
51
52 if (sata_initialize())
53 return 1;
54
55 env_sata = sata_get_env_dev();
56
57 sata = sata_get_dev(env_sata);
58 if (sata == NULL) {
59 printf("Unknown SATA(%d) device for environment!\n",
60 env_sata);
61 return 1;
62 }
63
64 ret = env_export(env_new);
65 if (ret)
66 return 1;
67
68 printf("Writing to SATA(%d)...", env_sata);
Ye Li17be9092019-01-07 09:22:35 +000069 if (write_env(sata, CONFIG_ENV_SIZE, CONFIG_ENV_OFFSET, (u_char *)env_new)) {
Peng Fan125d1932016-04-03 21:52:13 +080070 puts("failed\n");
71 return 1;
72 }
73
74 puts("done\n");
75 return 0;
76}
77#endif /* CONFIG_CMD_SAVEENV */
78
79static inline int read_env(struct blk_desc *sata, unsigned long size,
80 unsigned long offset, void *buffer)
81{
82 uint blk_start, blk_cnt, n;
83
84 blk_start = ALIGN(offset, sata->blksz) / sata->blksz;
85 blk_cnt = ALIGN(size, sata->blksz) / sata->blksz;
86
87 n = blk_dread(sata, blk_start, blk_cnt, buffer);
88
89 return (n == blk_cnt) ? 0 : -1;
90}
91
Simon Glasse5bce242017-08-03 12:22:01 -060092static void env_sata_load(void)
Peng Fan125d1932016-04-03 21:52:13 +080093{
94 ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
95 struct blk_desc *sata = NULL;
96 int env_sata;
97
98 if (sata_initialize())
Simon Glassc5951992017-08-03 12:22:17 -060099 return -EIO;
Peng Fan125d1932016-04-03 21:52:13 +0800100
101 env_sata = sata_get_env_dev();
102
103 sata = sata_get_dev(env_sata);
104 if (sata == NULL) {
Simon Glassc5951992017-08-03 12:22:17 -0600105 printf("Unknown SATA(%d) device for environment!\n", env_sata);
106 return -EIO;
Peng Fan125d1932016-04-03 21:52:13 +0800107 }
108
Simon Glassc5951992017-08-03 12:22:17 -0600109 if (read_env(sata, CONFIG_ENV_SIZE, CONFIG_ENV_OFFSET, buf)) {
Simon Glass0ac7d722019-08-01 09:47:00 -0600110 env_set_default(NULL, 0);
Simon Glassc5951992017-08-03 12:22:17 -0600111 return -EIO;
112 }
Peng Fan125d1932016-04-03 21:52:13 +0800113
Simon Goldschmidt2166ebf2018-01-31 14:47:12 +0100114 return env_import(buf, 1);
Peng Fan125d1932016-04-03 21:52:13 +0800115}
Simon Glass4415f1d2017-08-03 12:21:58 -0600116
117U_BOOT_ENV_LOCATION(sata) = {
118 .location = ENVL_ESATA,
Simon Glassac358be2017-08-03 12:22:03 -0600119 ENV_NAME("SATA")
Simon Glasse5bce242017-08-03 12:22:01 -0600120 .load = env_sata_load,
121 .save = env_save_ptr(env_sata_save),
Simon Glass4415f1d2017-08-03 12:21:58 -0600122};