/* Copyright Statement:
 *
 * This software/firmware and related documentation ("MediaTek Software") are
 * protected under relevant copyright laws. The information contained herein is
 * confidential and proprietary to MediaTek Inc. and/or its licensors. Without
 * the prior written permission of MediaTek inc. and/or its licensors, any
 * reproduction, modification, use or disclosure of MediaTek Software, and
 * information contained herein, in whole or in part, shall be strictly
 * prohibited.
 *
 * Copyright  (C) [2020]  MediaTek Inc. All rights reserved.
 *
 * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
 * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
 * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER
 * ON AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL
 * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
 * NONINFRINGEMENT. NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH
 * RESPECT TO THE SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY,
 * INCORPORATED IN, OR SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES
 * TO LOOK ONLY TO SUCH THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO.
 * RECEIVER EXPRESSLY ACKNOWLEDGES THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO
 * OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES CONTAINED IN MEDIATEK
 * SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE
 * RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
 * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S
 * ENTIRE AND CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE
 * RELEASED HEREUNDER WILL BE, AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE
 * MEDIATEK SOFTWARE AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE
 * CHARGE PAID BY RECEIVER TO MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
 *
 * The following software/firmware and/or related documentation ("MediaTek
 * Software") have been modified by MediaTek Inc. All revisions are subject to
 * any receiver's applicable license agreements with 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
