drivers/misc/optee: Add error conversion from errno to TEE errors

This commit simplifies sending errors back to OP-TEE and avoids
code duplication when handling errno values.

Signed-off-by: Theodore Karatapanis <tkaratapanis@census-labs.com>
This commit is contained in:
Theodore Karatapanis 2025-07-21 13:23:37 +03:00 committed by Xiang Xiao
parent 707cdaf42e
commit 9970e6a54e
4 changed files with 57 additions and 41 deletions

View file

@ -1251,6 +1251,51 @@ int optee_convert_to_errno(uint32_t oterr)
}
}
/****************************************************************************
* Name: optee_convert_from_errno
*
* Description:
* Convert errno values to TEE errors.
*
* Parameters:
* err - errno value (negative).
*
* Returned Values:
* The converted TEE error code.
*
****************************************************************************/
uint32_t optee_convert_from_errno(int err)
{
/* Make sure we handle negative errno values */
switch (-err)
{
case 0:
return TEE_SUCCESS;
case EACCES:
return TEE_ERROR_ACCESS_DENIED;
case EINVAL:
return TEE_ERROR_BAD_PARAMETERS;
case ENOTSUP:
case EOPNOTSUPP:
return TEE_ERROR_NOT_SUPPORTED;
case ENOMEM:
return TEE_ERROR_OUT_OF_MEMORY;
case EBUSY:
return TEE_ERROR_BUSY;
case ECOMM:
case EPROTO:
return TEE_ERROR_COMMUNICATION;
case ENOBUFS:
return TEE_ERROR_SHORT_BUFFER;
case ETIMEDOUT:
return TEE_ERROR_TIMEOUT;
default:
return TEE_ERROR_GENERIC;
}
}
/****************************************************************************
* Name: optee_va_to_pa
*

View file

@ -126,6 +126,7 @@ int optee_to_msg_param(FAR struct optee_priv_data *priv,
FAR const struct tee_ioctl_param *params);
int optee_convert_to_errno(uint32_t oterr);
uint32_t optee_convert_from_errno(int err);
#undef EXTERN
#if defined(__cplusplus)
}

View file

@ -188,32 +188,16 @@ static void optee_rpc_cmd_shm_alloc(FAR struct optee_priv_data *priv,
ret = optee_supplicant_alloc(priv, size, &shm);
break;
#else
arg->ret = TEE_ERROR_NOT_SUPPORTED;
return;
ret = -ENOTSUP;
#endif
case OPTEE_MSG_RPC_SHM_TYPE_KERNEL:
ret = optee_shm_alloc(priv, NULL, size, TEE_SHM_ALLOC, &shm);
break;
}
if (ret == -ENOMEM)
arg->ret = optee_convert_from_errno(ret);
if (arg->ret != TEE_SUCCESS)
{
arg->ret = TEE_ERROR_OUT_OF_MEMORY;
return;
}
else if (ret == -ECOMM)
{
arg->ret = TEE_ERROR_COMMUNICATION;
return;
}
else if (ret == -EINVAL)
{
arg->ret = TEE_ERROR_BAD_PARAMETERS;
return;
}
else if (ret != OK)
{
arg->ret = TEE_ERROR_GENERIC;
return;
}
@ -233,8 +217,6 @@ static void optee_rpc_cmd_shm_alloc(FAR struct optee_priv_data *priv,
arg->params[0].u.tmem.size = size;
arg->params[0].u.tmem.shm_ref = (unsigned long)shm;
}
arg->ret = TEE_SUCCESS;
}
/****************************************************************************

View file

@ -558,33 +558,21 @@ void optee_supplicant_cmd(FAR struct optee_priv_data *priv,
return;
}
if (optee_from_msg_param(params, arg->num_params, arg->params))
if ((ret = optee_from_msg_param(params, arg->num_params, arg->params))
!= 0)
{
arg->ret = TEE_ERROR_BAD_PARAMETERS;
arg->ret = optee_convert_from_errno(ret);
goto out;
}
arg->ret = optee_supplicant_request(arg->cmd, arg->num_params, params);
if (arg->ret != TEE_SUCCESS)
{
goto out;
}
if ((ret = optee_to_msg_param(priv, arg->params, arg->num_params, params)))
{
if (ret == -ENOMEM)
{
arg->ret = TEE_ERROR_OUT_OF_MEMORY;
}
else if (ret == -EPROTO)
{
arg->ret = TEE_ERROR_COMMUNICATION;
}
else if (ret == -EINVAL)
{
arg->ret = TEE_ERROR_BAD_PARAMETERS;
}
else
{
arg->ret = TEE_ERROR_GENERIC;
}
}
ret = optee_to_msg_param(priv, arg->params, arg->num_params, params);
arg->ret = optee_convert_from_errno(ret);
out:
kmm_free(params);