/*
 * app_saveData.c
 */

#include "app_main.h"
#include "app_core.h"
#include "app_saveData.h"

#include "tz_iccc_comdef.h"

uint32_t Iccc_Core_SaveData_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_WRITE);
}

uint32_t ICCC_save_data(tz_iccc_generic_payload_t *sendmsg, tz_iccc_generic_payload_t *respmsg)
{
    uint32_t type = 0;
    uint32_t secure_value = 0;
    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;
    secure_value = sendmsg->content.iccc_req.value;

    ICCC_LOG("TZ_ICCC: ICCC_save_data, type = %x , value = %d", type, secure_value);

    if (ICCC_SECTION_TYPE(type) == TA_ICCC_TYPE_START && type != SELINUX_STATUS) {
        ICCC_LOG("TZ_ICCC: ICCC_save_data permission is denied on type %x", type);
        ret = ICCC_PERMISSION_DENIED;
        goto error;
    }

    ret = Iccc_Core_SaveData_TA(type, &secure_value);
    if (ret) {
        ICCC_LOG("TZ_ICCC: Iccc_Core_SaveData_TA failed with ret = %x", ret);
        ret = ICCC_ERROR_WRITE_FAILED;
    }

error:
    respmsg->content.iccc_rsp.ret = ret;
    return ret;
}
