Files

159 lines
4.5 KiB
C++
Raw Permalink Normal View History

2026-09-22 09:38:26 +08:00
/*
* SPDX-License-Identifier: LicenseRef-Unisoc-General-1.0
*
* Copyright 2016-2024 Unisoc (Shanghai) Technologies Co., Ltd
*
* Licensed under the Unisoc General Software License, version 1.0 (the License);
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.unisoc.com/en_us/license/UNISOC_GENERAL_LICENSE_V1.0-EN_US
*
* Software distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OF ANY KIND, either express or implied.
* See the Unisoc General Software License, version 1.0 for more details.
*/
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <syslog.h>
#include <unistd.h>
#include "tipc.h"
#include "anti-rollback.h"
#include "log.h"
#define TRUSTY_DEVICE_NAME "/dev/trusty-ipc-dev0"
static int handle_ = 0;
int trusty_anti_rollback_connect()
{
int rc = tipc_connect(TRUSTY_DEVICE_NAME, ANTI_ROLLBACK_PORT);
if (rc < 0) {
LOGE("tipc_connect() failed!");
return rc;
}
handle_ = rc;
return 0;
}
int trusty_anti_rollback_call(uint32_t cmd,
void *in,
uint32_t in_size,
uint8_t *out,
uint32_t *out_size)
{
size_t msg_size = 0;
ssize_t rc = 0;
anti_rollback_message *msg = NULL;
msg_size = in_size + sizeof(anti_rollback_message);
msg = (anti_rollback_message *)malloc(msg_size);
if (NULL == msg) {
LOGE("malloc failed! size: %ld", msg_size);
return -EINVAL;
}
msg->cmd = cmd;
if (in_size > 0) {
memcpy(msg->payload, in, in_size);
}
rc = write(handle_, msg, msg_size);
free(msg);
if (rc < 0) {
LOGE("failed to send cmd (%d) to %s: %s", cmd,
ANTI_ROLLBACK_PORT, strerror(errno));
return -errno;
}
rc = read(handle_, out, *out_size);
if (rc < 0) {
LOGE("failed to retrieve response for cmd (%d) to %s: %s",
cmd, ANTI_ROLLBACK_PORT, strerror(errno));
return -errno;
}
if ((size_t) rc < sizeof(anti_rollback_message)) {
LOGE("invalid response size (%d)", (int) rc);
return -EINVAL;
}
msg = (anti_rollback_message *) out;
if ((cmd | ANTI_ROLLBACK_BIT) != msg->cmd) {
LOGE("invalid command (%d)", msg->cmd);
return -EINVAL;
}
*out_size = ((size_t) rc) - sizeof(anti_rollback_message);
return rc;
}
void trusty_anti_rollback_disconnect()
{
if (handle_ != 0) {
tipc_close(handle_);
}
}
int update_rootfs_verison(uint8_t *data, uint32_t *size, uint8_t* out) {
int result = -1;
uint8_t out_msg[*size] = {0};
uint32_t outsize = *size;
anti_rollback_message *msg = NULL;
LOGD("%s enter, size = %d \n", __func__, *size);
result = trusty_anti_rollback_connect();
if (result != 0) {
LOGE("trusty_anti_rollback_connect failed with ret = %d", result);
return result;
}
result = trusty_anti_rollback_call(ANTI_ROLLBACK_CMD, data, *size, out_msg, &outsize);
msg = (anti_rollback_message *) out_msg;
uint8_t *payload = msg->payload;
if (NULL != out && outsize == 1)
memcpy(out, payload, outsize);
else
LOGE("out is NULL or outsize wrong(%d)", outsize);
*size = outsize;
trusty_anti_rollback_disconnect();
LOGD("%s leave, result = %d, outsize = %d \n", __func__, result, outsize);
return result;
}
int verify_data(uint8_t *data, uint32_t data_size, uint8_t* out, uint32_t *inout_size) {
int ret = -1;
uint8_t out_msg[*inout_size] = {0};
anti_rollback_message *msg = NULL;
if (*inout_size <= 0) {
LOGE("out_size means expect response size, MUST greater than 0\n");
return ret;
}
LOGD("%s enter, data_size = %d \n", __func__, data_size);
ret = trusty_anti_rollback_connect();
if (ret != 0) {
LOGE("trusty_anti_rollback_connect failed with ret = %d", ret);
return ret;
}
ret = trusty_anti_rollback_call(ANTI_ROLLBACK_VERIFY, data, data_size, out_msg, inout_size);
msg = (anti_rollback_message *) out_msg;
uint8_t *payload = msg->payload;
if (NULL != out && *inout_size == 1)
memcpy(out, payload, *inout_size);
else
LOGE("out is NULL or inout_size wrong(%d)", *inout_size);
trusty_anti_rollback_disconnect();
LOGD("%s leave, ret = %d, inout_size = %d \n", __func__, ret, *inout_size);
return ret;
}