/*
 * Initialize_tl.c
 */

#include "tz_arcounter_errors.h"
#include "tlc_tz_common.h"
#include "Initialize_tl.h"

uint32_t process_Initialize(
	tl_arcounter_ctx_t * ctx,
	tz_init_payload_t * sendmsg,
	tz_init_payload_t * respmsg
)
{
	/* Slight abuse of return code
	 * Because of standards and our common init logic
	 */
	uint32_t ret = RET_TL_TZ_ARCOUNTER_OK;

	/*
	 * Slight abuse of return code
	 * Because TZ_COMMON_OK == RET_TL_TZ_ARCOUNTER_OK
	 * and it doesn't make sense to include tlc_tz_common.h here
	ret = process_init(sendmsg, respmsg);
	if (RET_TL_TZ_ARCOUNTER_OK != ret) {
		TTY_LOG
		    ("TL_TZ_ARCOUNTER: TL ARCOUNTER measurement command processing failure");
		ret = TZ_ARCOUNTER_FUNCTION_FAILED;
		goto exit;
	}
	 */
  // we will ignore the result of license and kap check fail
	if (respmsg->payload.resp.return_code == RET_TL_TZ_ARCOUNTER_OK
		|| respmsg->payload.resp.return_code == TZ_COMMON_INIT_LICENSE_KAP_CHK_FAIL) {
		ret = TZ_ARCOUNTER_SUCCESS;
		ctx->arcounter_init_count++;

		if (RET_TL_TZ_ARCOUNTER_OK != arcounter_rpmb_init()) {
			TTY_LOG("TL_TZ_ARCOUNTER: RPMB initialization failure");
			ret = TZ_ARCOUNTER_FUNCTION_FAILED;
			goto exit;
		}

	} else {
		TTY_LOG("TL_TZ_ARCOUNTER: TL ARCOUNTER general initialization failure");
		ret = respmsg->payload.resp.return_code;
	}

exit:
	respmsg->payload.resp.return_code = ret;
	return RET_TL_TZ_ARCOUNTER_OK;

}
