<commit_before>/*****************************************************************************
** Copyright (C) 2014 Secure Systems Group.                                 **
** Copyright (C) 2014 Intel Corporation.                                    **
**                                                                          **
** Licensed under the Apache License, Version 2.0 (the "License");          **
** you may not use this file except in compliance with the License.         **
** You may obtain a copy of the License at                                  **
**                                                                          **
**      http://www.apache.org/licenses/LICENSE-2.0                          **
**                                                                          **
** Unless required by applicable law or agreed to in writing, software      **
** distributed under the License is distributed on an "AS IS" BASIS,        **
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. **
** See the License for the specific language governing permissions and      **
** limitations under the License.                                           **
*****************************************************************************/

#ifndef __COM_PROTOCOL_H__
#define __COM_PROTOCOL_H__

/*!
 * \file com_protocol.h
 * \brief
 *
 * Use com_recv_msg(), com_send_msg(), com_wait_and_recv_msg() functions, because there function
 * will add or remove and check transport information.
 * If message is corrupted, it will be discarted.
 *
 * Protocol format:
 *
 * |------------|---------------|---------------|---------------|
 * | Start bit	| Message	| Checksum	| Payload	|
 * | sequence	| length	| 64 bit	| arbitary	|
 * |  32 bit	| 32 bit	|		| length	|
 * |------------|---------------|---------------|---------------|
 *
 * Start bit sequence: Message starting with pre defined bit sequence
 * Message length: Payyload
 * Checksum: CRC32 checksum is calculated over payload
 * (TODO: calculate checksum: bit seq + msg len + payload)
 * Payload: actual message
 */

#include <limits.h>
#include <stdint.h>

#include "tee_shared_data_types.h"

/* clang-format off */
/* Communication protocol message names */
#define COM_MSG_NAME_RESERVED			0x00 /* Zero is reserved */
#define COM_MSG_NAME_CA_INIT_CONTEXT		0x01
#define COM_MSG_NAME_OPEN_SESSION		0x02
#define COM_MSG_NAME_CREATED_TA			0x03
#define COM_MSG_NAME_INVOKE_CMD			0x04
#define COM_MSG_NAME_CLOSE_SESSION		0x05
#define COM_MSG_NAME_CA_FINALIZ_CONTEXT		0x06
#define COM_MSG_NAME_PROC_STATUS_CHANGE		0x07
#define COM_MSG_NAME_FD_ERR			0x08
#define COM_MSG_NAME_ERROR			0x09

/* Request is used internally */
#define COM_TYPE_QUERY				1
#define COM_TYPE_RESPONSE			0

#define TA_MAX_NAME_LEN				255
/* clang-format on */

/*!
 * \brief The com_msg_hdr struct
 * Message header is containing generic information, which is common for all messages
 */
struct com_msg_hdr {
	uint64_t sess_id;
	uint8_t msg_name;
	uint8_t msg_type;
} __attribute__((aligned));

/*
 * ## Message section start ##
 */

/*!
 * \brief The com_msg_ca_init_tee_conn struct
 * Register new CA connection to TEE (TEEC_InitializeContext).
 */
struct com_msg_ca_init_tee_conn {
	struct com_msg_hdr msg_hdr;
	TEE_Result ret;
} __attribute__((aligned));

/*!
 * \brief The com_msg_open_session struct
 * CA is opening new connection to TA (TEEC_OpenSession).
 */
struct com_msg_open_session {
	struct com_msg_hdr msg_hdr;
	char ta_so_name[TA_MAX_NAME_LEN];
	TEE_UUID uuid;
	TEE_Result return_code_create_entry;
	TEE_Result return_code_open_session;
	uint32_t return_origin;

	/* TODO: parameters */

} __attribute__((aligned));

/*!
 * \brief The com_msg_invoke_cmd struct
 * CA or TA is invoking command from TA (TEEC_InvokeCommand)
 */
struct com_msg_invoke_cmd {
	struct com_msg_hdr msg_hdr;
	TEE_Result return_code;
	uint32_t cmd_id;
	uint32_t return_origin;

	/* TODO: parameters */

} __attribute__((aligned));

/*!
 * \brief The com_msg_ta_created struct
 * Launcher is reporting launched TA PID. If PID is -1, something went wrong with launching
 * for example clone-function call failed.
 */
struct com_msg_ta_created {
	struct com_msg_hdr msg_hdr;
	pid_t pid;
} __attribute__((aligned));

/*!
 * \brief The com_msg_close_session struct
 * CA or TA closing session (TEEC_CloseSession)
 */
struct com_msg_close_session {
	struct com_msg_hdr msg_hdr;
	int should_ta_destroy;
} __attribute__((aligned));

/*!
 * \brief The com_msg_ca_finalize_constex struct
 * CA is closing connection to TEE (TEEC_FinalizeContext)
 */
struct com_msg_ca_finalize_constex {
	struct com_msg_hdr msg_hdr;
	/* Empty */
} __attribute__((aligned));

/*!<commit_msg>Adding TA session ctx parameter handling<commit_after>/*****************************************************************************
** Copyright (C) 2014 Secure Systems Group.                                 **
** Copyright (C) 2014 Intel Corporation.                                    **
**                                                                          **
** Licensed under the Apache License, Version 2.0 (the "License");          **
** you may not use this file except in compliance with the License.         **
** You may obtain a copy of the License at                                  **
**                                                                          **
**      http://www.apache.org/licenses/LICENSE-2.0                          **
**                                                                          **
** Unless required by applicable law or agreed to in writing, software      **
** distributed under the License is distributed on an "AS IS" BASIS,        **
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. **
** See the License for the specific language governing permissions and      **
** limitations under the License.                                           **
*****************************************************************************/

#ifndef __COM_PROTOCOL_H__
#define __COM_PROTOCOL_H__

/*!
 * \file com_protocol.h
 * \brief
 *
 * Use com_recv_msg(), com_send_msg(), com_wait_and_recv_msg() functions, because there function
 * will add or remove and check transport information.
 * If message is corrupted, it will be discarted.
 *
 * Protocol format:
 *
 * |------------|---------------|---------------|---------------|
 * | Start bit	| Message	| Checksum	| Payload	|
 * | sequence	| length	| 64 bit	| arbitary	|
 * |  32 bit	| 32 bit	|		| length	|
 * |------------|---------------|---------------|---------------|
 *
 * Start bit sequence: Message starting with pre defined bit sequence
 * Message length: Payyload
 * Checksum: CRC32 checksum is calculated over payload
 * (TODO: calculate checksum: bit seq + msg len + payload)
 * Payload: actual message
 */

#include <limits.h>
#include <stdint.h>

#include "tee_shared_data_types.h"

/* clang-format off */
/* Communication protocol message names */
#define COM_MSG_NAME_RESERVED			0x00 /* Zero is reserved */
#define COM_MSG_NAME_CA_INIT_CONTEXT		0x01
#define COM_MSG_NAME_OPEN_SESSION		0x02
#define COM_MSG_NAME_CREATED_TA			0x03
#define COM_MSG_NAME_INVOKE_CMD			0x04
#define COM_MSG_NAME_CLOSE_SESSION		0x05
#define COM_MSG_NAME_CA_FINALIZ_CONTEXT		0x06
#define COM_MSG_NAME_PROC_STATUS_CHANGE		0x07
#define COM_MSG_NAME_FD_ERR			0x08
#define COM_MSG_NAME_ERROR			0x09

/* Request is used internally */
#define COM_TYPE_QUERY				1
#define COM_TYPE_RESPONSE			0

#define TA_MAX_NAME_LEN				255
/* clang-format on */

/*!
 * \brief The com_msg_hdr struct
 * Message header is containing generic information, which is common for all messages
 */
struct com_msg_hdr {
	uint64_t sess_id;
	uint8_t msg_name;
	uint8_t msg_type;
} __attribute__((aligned));

/*
 * ## Message section start ##
 */

/*!
 * \brief The com_msg_ca_init_tee_conn struct
 * Register new CA connection to TEE (TEEC_InitializeContext).
 */
struct com_msg_ca_init_tee_conn {
	struct com_msg_hdr msg_hdr;
	TEE_Result ret;
} __attribute__((aligned));

/*!
 * \brief The com_msg_open_session struct
 * CA is opening new connection to TA (TEEC_OpenSession).
 */
struct com_msg_open_session {
	struct com_msg_hdr msg_hdr;
	char ta_so_name[TA_MAX_NAME_LEN];
	TEE_UUID uuid;
	uintptr_t sess_ctx;
	TEE_Result return_code_create_entry;
	TEE_Result return_code_open_session;
	uint32_t return_origin;

	/* TODO: parameters */

} __attribute__((aligned));

/*!
 * \brief The com_msg_invoke_cmd struct
 * CA or TA is invoking command from TA (TEEC_InvokeCommand)
 */
struct com_msg_invoke_cmd {
	struct com_msg_hdr msg_hdr;
	uintptr_t sess_ctx;
	TEE_Result return_code;
	uint32_t cmd_id;
	uint32_t return_origin;

	/* TODO: parameters */

} __attribute__((aligned));

/*!
 * \brief The com_msg_ta_created struct
 * Launcher is reporting launched TA PID. If PID is -1, something went wrong with launching
 * for example clone-function call failed.
 */
struct com_msg_ta_created {
	struct com_msg_hdr msg_hdr;
	pid_t pid;
} __attribute__((aligned));

/*!
 * \brief The com_msg_close_session struct
 * CA or TA closing session (TEEC_CloseSession)
 */
struct com_msg_close_session {
	struct com_msg_hdr msg_hdr;
	uintptr_t sess_ctx;
	int should_ta_destroy;
} __attribute__((aligned));

/*!
 * \brief The com_msg_ca_finalize_constex struct
 * CA is closing connection to TEE (TEEC_FinalizeContext)
 */
struct com_msg_ca_finalize_constex {
	struct com_msg_hdr msg_hdr;
	/* Empty */
} __attribute__((aligned));

/*!
{'fraction_non_alphanumeric': 0.13804325005248794, 'fraction_numerical': 0.013856812933025405, 'mean_word_length': 2.75522270398108, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 26, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2448924', 'n_tokens_mistral': 2923, 'n_tokens_neox': 2787, 'n_words': 813}