/*
 * app_main.h
 */

#ifndef _TZ_TEST_APP_MAIN_H_
#define _TZ_TEST_APP_MAIN_H_

#include "icccOperations_v4.h"

#define MIN_PADDING_NEEDED 9

/**
 * Commands for TZ application.
 * */
typedef enum {
    CMD_ICCC_TEST_SAVEDATA      = 0x00000100,
    CMD_ICCC_TEST_READDATA      = 0x00000101,
    CMD_ICCC_TEST_GETDEVISESTAT = 0x00000102,
    CMD_ICCC_TEST_NEGATIVE      = 0x00000103,
    CMD_ICCC_TEST_UNKNOWN       = 0x7FFFFFFF
} tz_test_cmd_type;

typedef struct tz_test_req_s {
    tz_test_cmd_type cmd_id;
    uint32_t type;
    uint32_t value;
    uint32_t padding[MIN_PADDING_NEEDED]; // only padding, just to make tciMessage_t's size >= 64
} __attribute__ ((packed)) tz_test_req_t;

typedef struct tz_test_rsp_s {
    tz_test_cmd_type cmd_id;
    uint32_t type;
    uint32_t value;
    int ret;
    bl_secure_info_t bl_secure_info;
    ta_secure_info_t ta_secure_info;
    kern_secure_info_t kern_secure_info;
    sys_secure_info_t sys_secure_info;
    rot_secure_info_t rot_secure_info;
    uint32_t result_code;
} __attribute__ ((packed)) tz_test_rsp_t;

typedef struct {
    union content_u {
        tz_test_req_t iccc_req;
        tz_test_rsp_t iccc_rsp;
    } __attribute__ ((packed)) content;
} __attribute__ ((packed)) tz_test_generic_payload_t;

// .../tz_common/public/tz_msg.h
typedef struct tz_msg_header {
    uint32_t id; // First 4 bytes should always be id: either cmd_id or rsp_id
    uint32_t content_id;
    uint32_t len;
    uint32_t status;
} __attribute__ ((packed)) tz_msg_header_t;

typedef struct {
    tz_msg_header_t header;
    union payload_u {
        tz_test_generic_payload_t generic;
    } __attribute__ ((packed)) payload;
} __attribute__ ((packed)) tciMessage_t;

typedef struct rot_bl_struct {
    secure_param_header_t header;
    uint32_t verified_boot_state;
    uint32_t device_locked;
    uint32_t os_version;
    uint32_t patch_month_year;
    uint32_t verified_boot_key[8];
    uint32_t boot_patch_level;
    uint32_t vendor_patch_level;
} __attribute__ ((packed)) rot_bl_struct_t;

// .../tz_common/public/tci.h
/* Responses have bit 31 set */
#define RSP_ID_MASK (1U << 31)
#define RSP_ID(cmdId) (((uint32_t)(cmdId)) | RSP_ID_MASK)
#define IS_CMD(cmdId) ((((uint32_t)(cmdId)) & RSP_ID_MASK) == 0)
#define IS_RSP(cmdId) ((((uint32_t)(cmdId)) & RSP_ID_MASK) == RSP_ID_MASK)

#endif // _TZ_TEST_APP_MAIN_H_
