Bo Lv | 4a46502 | 2023-02-28 05:51:47 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: (GPL-2.0+ OR MIT) |
| 2 | /* |
| 3 | * Copyright (c) 2023 Amlogic, Inc. All rights reserved. |
| 4 | */ |
| 5 | |
| 6 | #include <env.h> |
| 7 | #include <memalign.h> |
| 8 | #include <errno.h> |
| 9 | #include <amlogic/storage.h> |
| 10 | #include <asm/global_data.h> |
| 11 | #include <common.h> |
| 12 | #include <env_internal.h> |
| 13 | #include <search.h> |
| 14 | |
| 15 | DECLARE_GLOBAL_DATA_PTR; |
| 16 | |
| 17 | #ifdef CONFIG_CMD_SAVEENV |
| 18 | static int env_storage_save(void) |
| 19 | { |
| 20 | if (store_get_type() == BOOT_NONE) { |
| 21 | printf("env_storage: must init before save\n"); |
| 22 | return -ENOENT; |
| 23 | } |
| 24 | ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1); |
| 25 | if (env_export(env_new)) { |
| 26 | printf("env_storage: export failed\n"); |
| 27 | return -EINVAL; |
| 28 | } |
| 29 | |
| 30 | if (store_rsv_write(RSV_ENV, CONFIG_ENV_SIZE, (void *)env_new)) { |
| 31 | printf("env_storage: write failed\n"); |
| 32 | return -EIO; |
| 33 | } |
| 34 | |
| 35 | return 0; |
| 36 | } |
| 37 | #endif /* CONFIG_CMD_SAVEENV */ |
| 38 | |
| 39 | static int env_storage_load(void) |
| 40 | { |
| 41 | if (store_get_type() == BOOT_NONE) { |
| 42 | printf("env_storage: must init before load\n"); |
| 43 | return -ENOENT; |
| 44 | } |
| 45 | ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE); |
| 46 | |
| 47 | if (store_rsv_read(RSV_ENV, CONFIG_ENV_SIZE, (void *)buf)) { |
| 48 | env_set_default("!env_storage: read failed", 0); |
| 49 | return -EIO; |
| 50 | } |
| 51 | else if (env_import(buf, 1, H_EXTERNAL)) { |
| 52 | env_set_default("!env_storage: import failed", 0); |
| 53 | return -EINVAL; |
| 54 | } |
| 55 | |
| 56 | return 0; |
| 57 | } |
| 58 | |
| 59 | |
| 60 | U_BOOT_ENV_LOCATION(storage) = { |
| 61 | .location = ENVL_STORAGE, |
| 62 | ENV_NAME("STORAGE") |
| 63 | .load = env_storage_load, |
| 64 | #ifdef CONFIG_CMD_SAVEENV |
| 65 | .save = env_save_ptr(env_storage_save), |
| 66 | #endif |
| 67 | }; |