]> git.ipfire.org Git - thirdparty/u-boot.git/blame - env/sata.c
cbfs: Move result variable into the struct
[thirdparty/u-boot.git] / env / sata.c
CommitLineData
83d290c5 1// SPDX-License-Identifier: GPL-2.0+
125d193c
PF
2/*
3 * (C) Copyright 2010-2016 Freescale Semiconductor, Inc.
125d193c
PF
4 */
5
6/* #define DEBUG */
7
8#include <common.h>
9
10#include <command.h>
0ac7d722 11#include <env.h>
f3998fdc 12#include <env_internal.h>
125d193c
PF
13#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
125d193c
PF
27__weak int sata_get_env_dev(void)
28{
29 return CONFIG_SYS_SATA_ENV_DEV;
30}
31
125d193c
PF
32#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
e5bce247 46static int env_sata_save(void)
125d193c
PF
47{
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);
17be909a 69 if (write_env(sata, CONFIG_ENV_SIZE, CONFIG_ENV_OFFSET, (u_char *)env_new)) {
125d193c
PF
70 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
e5bce247 92static void env_sata_load(void)
125d193c
PF
93{
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())
c5951991 99 return -EIO;
125d193c
PF
100
101 env_sata = sata_get_env_dev();
102
103 sata = sata_get_dev(env_sata);
104 if (sata == NULL) {
c5951991
SG
105 printf("Unknown SATA(%d) device for environment!\n", env_sata);
106 return -EIO;
125d193c
PF
107 }
108
c5951991 109 if (read_env(sata, CONFIG_ENV_SIZE, CONFIG_ENV_OFFSET, buf)) {
0ac7d722 110 env_set_default(NULL, 0);
c5951991
SG
111 return -EIO;
112 }
125d193c 113
2166ebf7 114 return env_import(buf, 1);
125d193c 115}
4415f1d1
SG
116
117U_BOOT_ENV_LOCATION(sata) = {
118 .location = ENVL_ESATA,
ac358beb 119 ENV_NAME("SATA")
e5bce247
SG
120 .load = env_sata_load,
121 .save = env_save_ptr(env_sata_save),
4415f1d1 122};