/* SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause) */
/*
 * Copyright (c) 2023 MediaTek Inc.
 */

#ifndef __CE_IMPL_H__
#define __CE_IMPL_H__

#include <hexdump.h>
#include <debug_impl.h>
#include <vsprintf.h>

#ifdef CMD_CE_DEBUG
#define trace_dump_hex(b, l, info) do { \
	printf(info"\n");	\
	print_hex_dump("", DUMP_PREFIX_OFFSET, 16, 1, b, l, 1); \
} while(0)
#else
#define trace_dump_hex(b, l, info) do { } while(0)
#endif

// DEVICE_ID
#define APMCU_RIUBASE               (0x1C000000)
#define EFUSE_DID_BASE              (APMCU_RIUBASE + 0x40600)
#define DEV_ID_LENGTH               (6)
#define DEV_ID_SIZE                 (12)
#define LOG_ENABLE_SIZE             (16)

#define REG_DEVICE_ID_0             (0x0000)    //0x00
#define REG_DEVICE_ID_1             (0x0004)    //0x01
#define REG_DEVICE_ID_2             (0x0008)    //0x02
#define REG_DEVICE_ID_3             (0x0010)    //0x03
#define REG_DEVICE_ID_4             (0x0014)    //0x04
#define REG_DEVICE_ID_5             (0x0018)    //0x05

#define MASK_0XFF                   (0xff)

#define IO_READ16(base, offset)     *((volatile unsigned short*) (base + offset))

enum DID_INDEX
{
    DID_INDEX_0,
    DID_INDEX_1,
    DID_INDEX_2,
    DID_INDEX_3,
    DID_INDEX_4,
    DID_INDEX_5
};

void get_did(unsigned char *did);
int do_ce(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
int do_check_ce(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]);
bool ce_verify(uint8_t *in, size_t in_len, uint8_t *ans, size_t ans_len);
uint8_t *get_response_signed(size_t *len);

#ifdef CONFIG_CONSOLE_UNLOCK_USB
int do_ce_usb(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]);
int sha256_crypto_prefix(uint8_t *in, size_t in_len, uint8_t *out, size_t out_len);
uint8_t *get_preset_prefix(size_t *len);
const char *get_device_ID(void);
const char *get_FW_version(void);
const char *get_serial_number(void);
const char *get_response_filename(void);
#else
int sha256_crypto_magic(uint8_t *in, size_t in_len, uint8_t *out, size_t out_len);
uint8_t *get_preset_nonce(size_t *len);
const char *get_build_type(void);
const char *get_build_project(void);
#endif

#endif
