Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Peng Fan | 5da543c | 2016-04-03 21:52:13 +0800 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2010-2016 Freescale Semiconductor, Inc. |
Peng Fan | 5da543c | 2016-04-03 21:52:13 +0800 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | /* #define DEBUG */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | |
| 10 | #include <command.h> |
Simon Glass | 9738586 | 2019-08-01 09:47:00 -0600 | [diff] [blame] | 11 | #include <env.h> |
Simon Glass | 9d1f619 | 2019-08-02 09:44:25 -0600 | [diff] [blame] | 12 | #include <env_internal.h> |
Peng Fan | 5da543c | 2016-04-03 21:52:13 +0800 | [diff] [blame] | 13 | #include <linux/stddef.h> |
| 14 | #include <errno.h> |
| 15 | #include <memalign.h> |
| 16 | #include <sata.h> |
| 17 | #include <search.h> |
| 18 | |
Tom Rini | f4bdf1b | 2019-11-18 20:02:07 -0500 | [diff] [blame] | 19 | #if defined(CONFIG_ENV_OFFSET_REDUND) |
Peng Fan | 5da543c | 2016-04-03 21:52:13 +0800 | [diff] [blame] | 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 Fan | 5da543c | 2016-04-03 21:52:13 +0800 | [diff] [blame] | 27 | __weak int sata_get_env_dev(void) |
| 28 | { |
| 29 | return CONFIG_SYS_SATA_ENV_DEV; |
| 30 | } |
| 31 | |
Peng Fan | 5da543c | 2016-04-03 21:52:13 +0800 | [diff] [blame] | 32 | #ifdef CONFIG_CMD_SAVEENV |
| 33 | static 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 Glass | 082af92 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 46 | static int env_sata_save(void) |
Peng Fan | 5da543c | 2016-04-03 21:52:13 +0800 | [diff] [blame] | 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); |
Ye Li | ced7b35 | 2019-01-07 09:22:35 +0000 | [diff] [blame] | 69 | if (write_env(sata, CONFIG_ENV_SIZE, CONFIG_ENV_OFFSET, (u_char *)env_new)) { |
Peng Fan | 5da543c | 2016-04-03 21:52:13 +0800 | [diff] [blame] | 70 | puts("failed\n"); |
| 71 | return 1; |
| 72 | } |
| 73 | |
| 74 | puts("done\n"); |
| 75 | return 0; |
| 76 | } |
| 77 | #endif /* CONFIG_CMD_SAVEENV */ |
| 78 | |
| 79 | static 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 Glass | 082af92 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 92 | static void env_sata_load(void) |
Peng Fan | 5da543c | 2016-04-03 21:52:13 +0800 | [diff] [blame] | 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()) |
Simon Glass | 9977849 | 2017-08-03 12:22:17 -0600 | [diff] [blame] | 99 | return -EIO; |
Peng Fan | 5da543c | 2016-04-03 21:52:13 +0800 | [diff] [blame] | 100 | |
| 101 | env_sata = sata_get_env_dev(); |
| 102 | |
| 103 | sata = sata_get_dev(env_sata); |
| 104 | if (sata == NULL) { |
Simon Glass | 9977849 | 2017-08-03 12:22:17 -0600 | [diff] [blame] | 105 | printf("Unknown SATA(%d) device for environment!\n", env_sata); |
| 106 | return -EIO; |
Peng Fan | 5da543c | 2016-04-03 21:52:13 +0800 | [diff] [blame] | 107 | } |
| 108 | |
Simon Glass | 9977849 | 2017-08-03 12:22:17 -0600 | [diff] [blame] | 109 | if (read_env(sata, CONFIG_ENV_SIZE, CONFIG_ENV_OFFSET, buf)) { |
Simon Glass | 9738586 | 2019-08-01 09:47:00 -0600 | [diff] [blame] | 110 | env_set_default(NULL, 0); |
Simon Glass | 9977849 | 2017-08-03 12:22:17 -0600 | [diff] [blame] | 111 | return -EIO; |
| 112 | } |
Peng Fan | 5da543c | 2016-04-03 21:52:13 +0800 | [diff] [blame] | 113 | |
Marek Vasut | dfe4b7e | 2020-07-07 20:51:35 +0200 | [diff] [blame] | 114 | return env_import(buf, 1, H_EXTERNAL); |
Peng Fan | 5da543c | 2016-04-03 21:52:13 +0800 | [diff] [blame] | 115 | } |
Simon Glass | c10a88e | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 116 | |
| 117 | U_BOOT_ENV_LOCATION(sata) = { |
| 118 | .location = ENVL_ESATA, |
Simon Glass | d8273ed | 2017-08-03 12:22:03 -0600 | [diff] [blame] | 119 | ENV_NAME("SATA") |
Simon Glass | 082af92 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 120 | .load = env_sata_load, |
| 121 | .save = env_save_ptr(env_sata_save), |
Simon Glass | c10a88e | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 122 | }; |