// SPDX-License-Identifier: (GPL-2.0+ OR MIT) /* * Copyright (c) 2019 Amlogic, Inc. All rights reserved. */ #include #include #ifdef CONFIG_KEY_STORE_USE_FDB #include "flashdb.h" #else #include #endif #include struct unifykey_store_ops { int32_t (*read)(uint8_t *buf, uint32_t len, uint32_t *actual_len); int32_t (*write)(uint8_t *buf, uint32_t len, uint32_t *actual_len); } keystore_ops; #ifdef CONFIG_KEY_STORE_USE_FDB #define KEY_STORE_KEY_SLICE_SIZE 1024 static struct fdb_kvdb key_db; static int32_t amlfdb_key_read(uint8_t *buf, uint32_t len, uint32_t *actual_length) { int32_t i, ret, size; struct fdb_blob blob; char key[8]; int slice_cnt = len / KEY_STORE_KEY_SLICE_SIZE; uint8_t *p_buf = buf; if (actual_length) *actual_length = 0; if (!buf || !slice_cnt) { printf("error param, len = %d\n", len); return -1; } for (i = 0, size = 0; i < slice_cnt; i++) { snprintf(key, sizeof(key), "key%d", i); ret = fdb_kv_get_blob(&key_db, key, fdb_blob_make(&blob, p_buf, KEY_STORE_KEY_SLICE_SIZE)); if (ret == 0) break; size += ret; p_buf += ret; } if (actual_length) *actual_length = size; return size >= 0 ? 0 : -1; } static int32_t amlfdb_key_write(uint8_t *buf, uint32_t len, uint32_t *actual_length) { int32_t ret; struct fdb_blob blob; char key[8]; int slice_cnt = len / KEY_STORE_KEY_SLICE_SIZE; uint8_t *p_buf = buf; if (actual_length) *actual_length = 0; if (!p_buf || slice_cnt == 0 || len > CONFIG_KEY_STORE_DATA_SIZE || len % KEY_STORE_KEY_SLICE_SIZE) { printf("error param, len = %d\n", len); return -1; } for (int i = 0; i < slice_cnt; i++) { snprintf(key, sizeof(key), "key%d", i); ret = fdb_kv_set_blob(&key_db, key, fdb_blob_make(&blob, p_buf, KEY_STORE_KEY_SLICE_SIZE)); if (ret != FDB_NO_ERR) { printf("write %s fail = %d\n", key, ret); return -1; } p_buf += KEY_STORE_KEY_SLICE_SIZE; } if (actual_length) *actual_length = len; return 0; } #else static int32_t amlsf_key_read(uint8_t *buf, uint32_t len, uint32_t *actual_length) { int ret; ret = store_read("env", CONFIG_KEY_STORE_PART_OFFSET, len, buf); if (actual_length) *actual_length = ret ? 0 : len; return ret; } static int32_t amlsf_key_write(uint8_t *buf, uint32_t len, uint32_t *actual_length) { int ret; ret = store_erase("env", CONFIG_KEY_STORE_PART_OFFSET, len, 0); ret |= store_write("env", CONFIG_KEY_STORE_PART_OFFSET, len, buf); if (actual_length) *actual_length = ret ? 0 : len; return ret; } #endif int32_t aml_key_read(uint8_t *buf, uint32_t len, uint32_t *actual_length) { return keystore_ops.read ? keystore_ops.read(buf, len, actual_length) : -1; } int32_t aml_key_write(uint8_t *buf, uint32_t len, uint32_t *actual_length) { return keystore_ops.write ? keystore_ops.write(buf, len, actual_length) : -1; } int32_t aml_key_store_init(void) { static int init_flag; if (init_flag) return 0; #ifdef CONFIG_KEY_STORE_USE_FDB int result = fdb_kvdb_init(&key_db, "env", FLASHDB_KEYSTORE_DB_NAME, NULL, NULL); if (result != FDB_NO_ERR) { printf("key_db init err=%d\n", result); return -1; } keystore_ops.read = amlfdb_key_read; keystore_ops.write = amlfdb_key_write; #else keystore_ops.read = amlsf_key_read; keystore_ops.write = amlsf_key_write; #endif init_flag = 1; return 0; }