Peng Fan | 5da543c | 2016-04-03 21:52:13 +0800 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2010-2016 Freescale Semiconductor, Inc. |
| 3 | * |
| 4 | * SPDX-License-Identifier: GPL-2.0+ |
| 5 | */ |
| 6 | |
| 7 | /* #define DEBUG */ |
| 8 | |
| 9 | #include <common.h> |
| 10 | |
| 11 | #include <command.h> |
| 12 | #include <environment.h> |
| 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 | |
Peng Fan | 5da543c | 2016-04-03 21:52:13 +0800 | [diff] [blame] | 27 | DECLARE_GLOBAL_DATA_PTR; |
| 28 | |
| 29 | __weak int sata_get_env_dev(void) |
| 30 | { |
| 31 | return CONFIG_SYS_SATA_ENV_DEV; |
| 32 | } |
| 33 | |
Peng Fan | 5da543c | 2016-04-03 21:52:13 +0800 | [diff] [blame] | 34 | #ifdef CONFIG_CMD_SAVEENV |
| 35 | static inline int write_env(struct blk_desc *sata, unsigned long size, |
| 36 | unsigned long offset, void *buffer) |
| 37 | { |
| 38 | uint blk_start, blk_cnt, n; |
| 39 | |
| 40 | blk_start = ALIGN(offset, sata->blksz) / sata->blksz; |
| 41 | blk_cnt = ALIGN(size, sata->blksz) / sata->blksz; |
| 42 | |
| 43 | n = blk_dwrite(sata, blk_start, blk_cnt, buffer); |
| 44 | |
| 45 | return (n == blk_cnt) ? 0 : -1; |
| 46 | } |
| 47 | |
Simon Glass | 082af92 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 48 | static int env_sata_save(void) |
Peng Fan | 5da543c | 2016-04-03 21:52:13 +0800 | [diff] [blame] | 49 | { |
| 50 | ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1); |
| 51 | struct blk_desc *sata = NULL; |
| 52 | int env_sata, ret; |
| 53 | |
| 54 | if (sata_initialize()) |
| 55 | return 1; |
| 56 | |
| 57 | env_sata = sata_get_env_dev(); |
| 58 | |
| 59 | sata = sata_get_dev(env_sata); |
| 60 | if (sata == NULL) { |
| 61 | printf("Unknown SATA(%d) device for environment!\n", |
| 62 | env_sata); |
| 63 | return 1; |
| 64 | } |
| 65 | |
| 66 | ret = env_export(env_new); |
| 67 | if (ret) |
| 68 | return 1; |
| 69 | |
| 70 | printf("Writing to SATA(%d)...", env_sata); |
| 71 | if (write_env(sata, CONFIG_ENV_SIZE, CONFIG_ENV_OFFSET, &env_new)) { |
| 72 | puts("failed\n"); |
| 73 | return 1; |
| 74 | } |
| 75 | |
| 76 | puts("done\n"); |
| 77 | return 0; |
| 78 | } |
| 79 | #endif /* CONFIG_CMD_SAVEENV */ |
| 80 | |
| 81 | static inline int read_env(struct blk_desc *sata, unsigned long size, |
| 82 | unsigned long offset, void *buffer) |
| 83 | { |
| 84 | uint blk_start, blk_cnt, n; |
| 85 | |
| 86 | blk_start = ALIGN(offset, sata->blksz) / sata->blksz; |
| 87 | blk_cnt = ALIGN(size, sata->blksz) / sata->blksz; |
| 88 | |
| 89 | n = blk_dread(sata, blk_start, blk_cnt, buffer); |
| 90 | |
| 91 | return (n == blk_cnt) ? 0 : -1; |
| 92 | } |
| 93 | |
Simon Glass | 082af92 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 94 | static void env_sata_load(void) |
Peng Fan | 5da543c | 2016-04-03 21:52:13 +0800 | [diff] [blame] | 95 | { |
| 96 | ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE); |
| 97 | struct blk_desc *sata = NULL; |
| 98 | int env_sata; |
| 99 | |
| 100 | if (sata_initialize()) |
| 101 | return; |
| 102 | |
| 103 | env_sata = sata_get_env_dev(); |
| 104 | |
| 105 | sata = sata_get_dev(env_sata); |
| 106 | if (sata == NULL) { |
| 107 | printf("Unknown SATA(%d) device for environment!\n", |
| 108 | env_sata); |
| 109 | return; |
| 110 | } |
| 111 | |
| 112 | if (read_env(sata, CONFIG_ENV_SIZE, CONFIG_ENV_OFFSET, buf)) |
| 113 | return set_default_env(NULL); |
| 114 | |
| 115 | env_import(buf, 1); |
| 116 | } |
Simon Glass | c10a88e | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 117 | |
| 118 | U_BOOT_ENV_LOCATION(sata) = { |
| 119 | .location = ENVL_ESATA, |
Simon Glass | d8273ed | 2017-08-03 12:22:03 -0600 | [diff] [blame^] | 120 | ENV_NAME("SATA") |
Simon Glass | 082af92 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 121 | .load = env_sata_load, |
| 122 | .save = env_save_ptr(env_sata_save), |
Simon Glass | c10a88e | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 123 | }; |