blob: 59aedf4d76644964a3b48aa31d5b47308de44df2 [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>
11#include <environment.h>
12#include <linux/stddef.h>
13#include <errno.h>
14#include <memalign.h>
15#include <sata.h>
16#include <search.h>
17
18#if defined(CONFIG_ENV_SIZE_REDUND) || defined(CONFIG_ENV_OFFSET_REDUND)
19#error ENV REDUND not supported
20#endif
21
22#if !defined(CONFIG_ENV_OFFSET) || !defined(CONFIG_ENV_SIZE)
23#error CONFIG_ENV_OFFSET or CONFIG_ENV_SIZE not defined
24#endif
25
Peng Fan125d1932016-04-03 21:52:13 +080026__weak int sata_get_env_dev(void)
27{
28 return CONFIG_SYS_SATA_ENV_DEV;
29}
30
Peng Fan125d1932016-04-03 21:52:13 +080031#ifdef CONFIG_CMD_SAVEENV
32static inline int write_env(struct blk_desc *sata, unsigned long size,
33 unsigned long offset, void *buffer)
34{
35 uint blk_start, blk_cnt, n;
36
37 blk_start = ALIGN(offset, sata->blksz) / sata->blksz;
38 blk_cnt = ALIGN(size, sata->blksz) / sata->blksz;
39
40 n = blk_dwrite(sata, blk_start, blk_cnt, buffer);
41
42 return (n == blk_cnt) ? 0 : -1;
43}
44
Simon Glasse5bce242017-08-03 12:22:01 -060045static int env_sata_save(void)
Peng Fan125d1932016-04-03 21:52:13 +080046{
47 ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
48 struct blk_desc *sata = NULL;
49 int env_sata, ret;
50
51 if (sata_initialize())
52 return 1;
53
54 env_sata = sata_get_env_dev();
55
56 sata = sata_get_dev(env_sata);
57 if (sata == NULL) {
58 printf("Unknown SATA(%d) device for environment!\n",
59 env_sata);
60 return 1;
61 }
62
63 ret = env_export(env_new);
64 if (ret)
65 return 1;
66
67 printf("Writing to SATA(%d)...", env_sata);
68 if (write_env(sata, CONFIG_ENV_SIZE, CONFIG_ENV_OFFSET, &env_new)) {
69 puts("failed\n");
70 return 1;
71 }
72
73 puts("done\n");
74 return 0;
75}
76#endif /* CONFIG_CMD_SAVEENV */
77
78static inline int read_env(struct blk_desc *sata, unsigned long size,
79 unsigned long offset, void *buffer)
80{
81 uint blk_start, blk_cnt, n;
82
83 blk_start = ALIGN(offset, sata->blksz) / sata->blksz;
84 blk_cnt = ALIGN(size, sata->blksz) / sata->blksz;
85
86 n = blk_dread(sata, blk_start, blk_cnt, buffer);
87
88 return (n == blk_cnt) ? 0 : -1;
89}
90
Simon Glasse5bce242017-08-03 12:22:01 -060091static void env_sata_load(void)
Peng Fan125d1932016-04-03 21:52:13 +080092{
93 ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
94 struct blk_desc *sata = NULL;
95 int env_sata;
96
97 if (sata_initialize())
Simon Glassc5951992017-08-03 12:22:17 -060098 return -EIO;
Peng Fan125d1932016-04-03 21:52:13 +080099
100 env_sata = sata_get_env_dev();
101
102 sata = sata_get_dev(env_sata);
103 if (sata == NULL) {
Simon Glassc5951992017-08-03 12:22:17 -0600104 printf("Unknown SATA(%d) device for environment!\n", env_sata);
105 return -EIO;
Peng Fan125d1932016-04-03 21:52:13 +0800106 }
107
Simon Glassc5951992017-08-03 12:22:17 -0600108 if (read_env(sata, CONFIG_ENV_SIZE, CONFIG_ENV_OFFSET, buf)) {
Yaniv Levinskyc5d548a2018-06-24 19:16:57 +0300109 set_default_env(NULL, 0);
Simon Glassc5951992017-08-03 12:22:17 -0600110 return -EIO;
111 }
Peng Fan125d1932016-04-03 21:52:13 +0800112
Simon Goldschmidt2166ebf2018-01-31 14:47:12 +0100113 return env_import(buf, 1);
Peng Fan125d1932016-04-03 21:52:13 +0800114}
Simon Glass4415f1d2017-08-03 12:21:58 -0600115
116U_BOOT_ENV_LOCATION(sata) = {
117 .location = ENVL_ESATA,
Simon Glassac358be2017-08-03 12:22:03 -0600118 ENV_NAME("SATA")
Simon Glasse5bce242017-08-03 12:22:01 -0600119 .load = env_sata_load,
120 .save = env_save_ptr(env_sata_save),
Simon Glass4415f1d2017-08-03 12:21:58 -0600121};