blob: 93697100815d080ae6c1e0bd81669dfa666314e0 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Peng Fan5da543c2016-04-03 21:52:13 +08002/*
3 * (C) Copyright 2010-2016 Freescale Semiconductor, Inc.
Peng Fan5da543c2016-04-03 21:52:13 +08004 */
5
6/* #define DEBUG */
7
8#include <common.h>
9
10#include <command.h>
Simon Glass97385862019-08-01 09:47:00 -060011#include <env.h>
Simon Glass9d1f6192019-08-02 09:44:25 -060012#include <env_internal.h>
Peng Fan5da543c2016-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
19#if defined(CONFIG_ENV_SIZE_REDUND) || defined(CONFIG_ENV_OFFSET_REDUND)
20#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 Fan5da543c2016-04-03 21:52:13 +080027__weak int sata_get_env_dev(void)
28{
29 return CONFIG_SYS_SATA_ENV_DEV;
30}
31
Peng Fan5da543c2016-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 Glass082af922017-08-03 12:22:01 -060046static int env_sata_save(void)
Peng Fan5da543c2016-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 Liced7b352019-01-07 09:22:35 +000069 if (write_env(sata, CONFIG_ENV_SIZE, CONFIG_ENV_OFFSET, (u_char *)env_new)) {
Peng Fan5da543c2016-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 Glass082af922017-08-03 12:22:01 -060092static void env_sata_load(void)
Peng Fan5da543c2016-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 Glass99778492017-08-03 12:22:17 -060099 return -EIO;
Peng Fan5da543c2016-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 Glass99778492017-08-03 12:22:17 -0600105 printf("Unknown SATA(%d) device for environment!\n", env_sata);
106 return -EIO;
Peng Fan5da543c2016-04-03 21:52:13 +0800107 }
108
Simon Glass99778492017-08-03 12:22:17 -0600109 if (read_env(sata, CONFIG_ENV_SIZE, CONFIG_ENV_OFFSET, buf)) {
Simon Glass97385862019-08-01 09:47:00 -0600110 env_set_default(NULL, 0);
Simon Glass99778492017-08-03 12:22:17 -0600111 return -EIO;
112 }
Peng Fan5da543c2016-04-03 21:52:13 +0800113
Simon Goldschmidt35b4c322018-01-31 14:47:12 +0100114 return env_import(buf, 1);
Peng Fan5da543c2016-04-03 21:52:13 +0800115}
Simon Glassc10a88e2017-08-03 12:21:58 -0600116
117U_BOOT_ENV_LOCATION(sata) = {
118 .location = ENVL_ESATA,
Simon Glassd8273ed2017-08-03 12:22:03 -0600119 ENV_NAME("SATA")
Simon Glass082af922017-08-03 12:22:01 -0600120 .load = env_sata_load,
121 .save = env_save_ptr(env_sata_save),
Simon Glassc10a88e2017-08-03 12:21:58 -0600122};