/*
 * app_readData.c
 */

#include "app_main.h"
#include "app_core.h"
#include "app_readData.h"

#include "tz_iccc_comdef.h"

uint32_t Iccc_Core_ReadData_TA(uint32_t type, uint32_t *value)
{

#if DEBUG_ICCC
    ICCC_LOG("TZ_ICCC: %s: Type:%#x", __func__, type);
#endif

    return process_iccc_cmd(type, value, ICCC_READ);
}

uint32_t ICCC_read_data(tz_iccc_generic_payload_t *sendmsg, tz_iccc_generic_payload_t *respmsg)
{
    uint32_t type = 0;
    uint32_t secure_value;
    uint32_t ret = ICCC_SUCCESS;

    if (respmsg == NULL || sendmsg == NULL) {
        ICCC_LOG("TZ_ICCC: sendmsg or respmsg is NULL ");
        return ICCC_FAILURE;
    }

    type = sendmsg->content.iccc_req.type;

    ICCC_LOG("TZ_ICCC: ICCC_read_data, type = %x", type);

    if (type == ATN_BLOB_HASH || type == VERIFIEDBOOT_HASH || type == VERIFIEDBOOTKEY_FLAG || type == ROT_STRUCT || type == BL_STRUCT) {
        ICCC_LOG("TZ_ICCC: ICCC_read_data permission is denied on type %x", type);
        ret = ICCC_PERMISSION_DENIED;
        goto error;
    }

    ret = Iccc_Core_ReadData_TA(type, &secure_value);
    if (ret) {
        ICCC_LOG("TZ_ICCC: Iccc_Core_ReadData_TA failed with ret = %x", ret);
        ret = ICCC_ERROR_READ_FAILED;
    } else {
        respmsg->content.iccc_rsp.value = secure_value;
        respmsg->content.iccc_rsp.type = type;
    }

    ICCC_LOG("TZ_ICCC: value = %x", secure_value);
    ICCC_LOG("TZ_ICCC: respmsg->content.iccc_rsp.type = %x", respmsg->content.iccc_rsp.type);

error:
    respmsg->content.iccc_rsp.ret = ret;
    return ret;
}
