Skip to content
This repository has been archived by the owner on Aug 20, 2024. It is now read-only.

Commit

Permalink
scmi_validation: scmi sensor protocol validation
Browse files Browse the repository at this point in the history
Use scmi API scmi_message_validation before calling
message handler, update unit test.

Signed-off-by: khaled ali ahmed <[email protected]>
Change-Id: I4408e69cd096c2c7c80dd079cc607318dc6ef3e8
  • Loading branch information
khaledAhmed-arm committed May 16, 2024
1 parent 53e2c97 commit 5fa2d90
Showing 1 changed file with 29 additions and 39 deletions.
68 changes: 29 additions & 39 deletions module/scmi_sensor/src/mod_scmi_sensor.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Arm SCP/MCP Software
* Copyright (c) 2015-2023, Arm Limited and Contributors. All rights reserved.
* Copyright (c) 2015-2024, Arm Limited and Contributors. All rights reserved.
*
* SPDX-License-Identifier: BSD-3-Clause
*
Expand Down Expand Up @@ -110,9 +110,7 @@ static const fwk_id_t mod_scmi_sensor_event_id_get_request =
*/
static struct mod_scmi_sensor_ctx scmi_sensor_ctx;

static int (*handler_table[MOD_SCMI_SENSOR_COMMAND_COUNT])(
fwk_id_t,
const uint32_t *) = {
static handler_table_t handler_table[MOD_SCMI_SENSOR_COMMAND_COUNT] = {
[MOD_SCMI_PROTOCOL_VERSION] = scmi_sensor_protocol_version_handler,
[MOD_SCMI_PROTOCOL_ATTRIBUTES] = scmi_sensor_protocol_attributes_handler,
[MOD_SCMI_PROTOCOL_MESSAGE_ATTRIBUTES] =
Expand All @@ -128,7 +126,7 @@ static int (*handler_table[MOD_SCMI_SENSOR_COMMAND_COUNT])(
[MOD_SCMI_SENSOR_READING_GET] = scmi_sensor_reading_get_handler
};

static unsigned int payload_size_table[MOD_SCMI_SENSOR_COMMAND_COUNT] = {
static size_t payload_size_table[MOD_SCMI_SENSOR_COMMAND_COUNT] = {
[MOD_SCMI_PROTOCOL_VERSION] = 0,
[MOD_SCMI_PROTOCOL_ATTRIBUTES] = 0,
[MOD_SCMI_PROTOCOL_MESSAGE_ATTRIBUTES] =
Expand Down Expand Up @@ -897,15 +895,6 @@ static int scmi_sensor_permissions_handler(
return FWK_E_ACCESS;
}

if (message_id < 3) {
perms = scmi_sensor_ctx.res_perms_api->agent_has_protocol_permission(
agent_id, MOD_SCMI_PROTOCOL_ID_SENSOR);
if (perms == MOD_RES_PERMS_ACCESS_ALLOWED) {
return FWK_SUCCESS;
}
return FWK_E_ACCESS;
}

sensor_id = get_sensor_id(payload);
if (sensor_id >= scmi_sensor_ctx.sensor_count) {
return FWK_E_PARAM;
Expand All @@ -928,47 +917,48 @@ static int scmi_sensor_message_handler(fwk_id_t protocol_id,
size_t payload_size,
unsigned int message_id)
{
int32_t return_value;
int validation_result;
#ifdef BUILD_HAS_MOD_RESOURCE_PERMS
int status;
#endif

static_assert(FWK_ARRAY_SIZE(handler_table) ==
FWK_ARRAY_SIZE(payload_size_table),
"[SCMI] Sensor management protocol table sizes not consistent");
fwk_assert(payload != NULL);

if (message_id >= FWK_ARRAY_SIZE(handler_table)) {
return_value = (int32_t)SCMI_NOT_FOUND;
goto error;
}

if (payload_size != payload_size_table[message_id]) {
/* Incorrect payload size or message is not supported */
return_value = (int32_t)SCMI_PROTOCOL_ERROR;
goto error;
}
validation_result = scmi_sensor_ctx.scmi_api->scmi_message_validation(
MOD_SCMI_PROTOCOL_ID_SENSOR,
service_id,
payload,
payload_size,
message_id,
payload_size_table,
(unsigned int)MOD_SCMI_SENSOR_COMMAND_COUNT,
handler_table);

#ifdef BUILD_HAS_MOD_RESOURCE_PERMS
status = scmi_sensor_permissions_handler(
service_id, payload, payload_size, message_id);
if (status != FWK_SUCCESS) {
if (status == FWK_E_ACCESS) {
return_value = (int32_t)SCMI_DENIED;
} else if (message_id == MOD_SCMI_SENSOR_DESCRIPTION_GET) {
return_value = (int32_t)SCMI_INVALID_PARAMETERS;
} else {
return_value = (int32_t)SCMI_NOT_FOUND;
if ((message_id >= MOD_SCMI_MESSAGE_ID_ATTRIBUTE) &&
(validation_result == SCMI_SUCCESS)) {
status = scmi_sensor_permissions_handler(
service_id, payload, payload_size, message_id);
if (status != FWK_SUCCESS) {
if (status == FWK_E_ACCESS) {
validation_result = (int)SCMI_DENIED;
} else if (message_id == MOD_SCMI_SENSOR_DESCRIPTION_GET) {
validation_result = (int)SCMI_INVALID_PARAMETERS;
} else {
validation_result = (int)SCMI_NOT_FOUND;
}
}
goto error;
}
#endif

return handler_table[message_id](service_id, payload);
if (validation_result == SCMI_SUCCESS) {
return handler_table[message_id](service_id, payload);
}

error:
return scmi_sensor_ctx.scmi_api->respond(
service_id, &return_value, sizeof(return_value));
service_id, &validation_result, sizeof(validation_result));
}

static struct mod_scmi_to_protocol_api scmi_sensor_mod_scmi_to_protocol_api = {
Expand Down

0 comments on commit 5fa2d90

Please sign in to comment.