In Op-tee, TA and CA Execute process-------tee-supplicant (TA requests processing of specific requests) __op-tee

Source: Internet
Author: User
Tags 04x uuid

When Tee_supplicant receives a request from TA and resolves the corresponding request func ID, Tee_supplicant will perform the specific request operation based on the Func ID. Mainly for the operation of the file system at the Ree end. 1. Load TA image to shared memory

When TA requests a Func ID of Rpc_cmd_load_ta, Tee_supplicant reads the contents of TA mirrors into shared memory in the file system. This is done by calling the Load_ta function, which is defined in the Tee_supplicant.c file as follows:

Static uint32_t Load_ta (size_t num_params, struct Tee_ioctl_param *params)
{
	int ta_found = 0;
	size_t size = 0;
	Teec_uuid UUID;
	struct Tee_ioctl_param_value *val_cmd;
	Teec_sharedmemory Shm_ta;

	memset (&shm_ta, 0, sizeof (SHM_TA));

/* resolves the uuid for which TA mirrors need to be loaded as well as the content location where the TA mirror is to be read
	/if (num_params!= 2 | | get_value (num_params, params, 0, &val_cmd) | |
	    Get_param (Num_params, params, 1, &shm_ta))
		return teec_error_bad_parameters;

/* Converts the value of the UUID to TEEC_UUID format *
	/Uuid_from_octets (&uuid, (void *) val_cmd);

/* Look for a TA mirror that matches the UUID from the directory specified by the TA_DIR variable and read its contents into shared memory. * *
	size = shm_ta.size;
	Ta_found = Teeci_loadsecuremodule (Ta_dir, &uuid, Shm_ta.buffer, &size);
	if (Ta_found!= ta_binary_found) {
		emsg ("  ta not Found");
		return teec_error_item_not_found;
	}

/* Set the size of the TA image to be read to the return parameter
	/params[1].u.memref.size = size;
	return teec_success;
}
The execution flow of the function is as follows:



2. Perform operations on the REE-side file system when the func ID is rpc_cmd_fs, tee_supplicant will be called on the TA request Tee_supp_fs_ Process functions to complete the operation of the file system, including regular file and directory open, close, read, write, rename, delete, and so on. The contents are as follows:

Teec_result tee_supp_fs_process (size_t num_params struct Tee_ioctl_param) {/* resolves *params/if (params =

= 1 && tee_supp_param_is_memref (params)) {void *va = Tee_supp_param_to_va (params);
		/* If the Num_params is 1 and the converted VA is valid, call the Tee_supp_fs_process_primitive function for file operation/if (!VA) return teec_error_bad_parameters;
	Return tee_supp_fs_process_primitive (VA, params->u.memref.size);

} if (!num_params | |!tee_supp_param_is_value (params)) return teec_error_bad_parameters; /* If the Num_params parameter is not 1, determine what action is performed based on the value in params, and specify the filename according to the data in params/switch (PARAMS->U.VALUE.A) {case OPTEE_MRF
	_open:return Ree_fs_new_open (num_params, params);
	Case Optee_mrf_create:return Ree_fs_new_create (num_params, params);
	Case Optee_mrf_close:return Ree_fs_new_close (num_params, params);
	Case Optee_mrf_read:return Ree_fs_new_read (num_params, params);
	Case Optee_mrf_write:return Ree_fs_new_write (num_params, params); Case Optee_mrf_truncate:return Ree_fs_new_truncate (num_params, params);
	Case Optee_mrf_remove:return Ree_fs_new_remove (num_params, params);
	Case Optee_mrf_rename:return Ree_fs_new_rename (num_params, params);
	Case Optee_mrf_opendir:return Ree_fs_new_opendir (num_params, params);
	Case Optee_mrf_closedir:return Ree_fs_new_closedir (num_params, params);
	Case Optee_mrf_readdir:return Ree_fs_new_readdir (num_params, params);
	Default:return teec_error_bad_parameters; }
}
3. Perform operations on the RPMB partitionWhen the Func ID is RPC_CMD_RPMB, Tee_supplicant will invoke the PROCESS_RPMB function on TA request to complete the EMMC partition operation in RMPB. In eMMC, the RPMB partition performs verification and decryption operations during the read and write process. Its contents are as follows:
Static uint32_t PROCESS_RPMB (size_t num_params, struct Tee_ioctl_param *params)
{
	teec_sharedmemory req;
	Teec_sharedmemory RSP;

/* Specifies the shared memory that holds the request and return data */
	if (Get_param (num_params, params, 0, &req) | |
	    Get_param (Num_params, params, 1, &RSP)) return
		teec_error_bad_parameters;

/* Specifies the operation of the RPMB partition
	/Return Rpmb_process_request (Req.buffer, Req.size, Rsp.buffer, rsp.size);
4. Perform an operation to allocate shared memoryWhen the Func ID is rpc_cmd_shm_alloc, Tee_supplicant will invoke the Process_alloc function to allocate the shared memory between TA and tee_supplicant according to TA request. Its contents are as follows:
static uint32_t process_alloc (int fd, size_t num_params, struct tee_ioctl_param *params) {struct TEE_IOCTL_SHM_
	Alloc_data data;
	struct Tee_ioctl_param_value *val;
	struct TEE_SHM *shm;

	int shm_fd;

memset (&data, 0, sizeof (data)); /* Get value */if (num_params!= 1 | | get_value (num_params, params, 0, &val)) sent from TA to Tee_supplicant return teec_error

_bad_parameters;
	/* Allocate SHM variable space * * SHM = calloc (1, sizeof (*SHM));

if (!SHM) return teec_error_out_of_memory;
	/* Call Tee Driver allocation Shared space * * Data.size = val->b;
	SHM_FD = IOCTL (FD, Tee_ioc_shm_alloc, &data);
		if (SHM_FD < 0) {free (SHM);
	return teec_error_out_of_memory; /* * Map handle of allocated shared memory to system memory/* Shm->p = mmap (NULL, data.size, Prot_read |
	Prot_write, map_shared, SHM_FD, 0);
	Close (SHM_FD);
		if (shm->p = = (void *) map_failed) {free (SHM);
	return teec_error_out_of_memory;
	/* Records allocated Shared memory data/* Shm->id = data.id;
	Shm->size = data.size;

Val->c = data.id; /* Add shared memory allocated to the shared memory list/* pusH_TSHM (SHM);
return teec_success; }
5. Perform actions to free shared memoryWhen the Func ID is rpc_cmd_shm_free, Tee_supplicant will invoke the Process_free function based on TA request to release the shared memory between TA and Tee_supplicant. Its contents are as follows:
Static uint32_t Process_free (size_t num_params, struct Tee_ioctl_param *params)
{
	struct TEE_IOCTL_PARAM_ Value *val;
	struct TEE_SHM *shm;
	int id;

/* Get Val data passed from TA to tee_supplicant *
	/if (num_params!= 1 | | get_value (num_params, params, 0, &val)) return
		TEEC _error_bad_parameters;

/* Gets the ID value of the shared memory that needs to be freed * *
	id = val->b;

/* Delete the specified node from the shared memory list
	/* SHM = POP_TSHM (ID);
	if (!SHM) return
		teec_error_bad_parameters;

/* Cancel the system memory mapping */
	if (Munmap (shm->p, shm->size)!= 0) {
		emsg ("Munmap (%p,%zu) Failed-error =%s",
		     SHM ->p, Shm->size, Strerror (errno));
		Free (SHM);
		return teec_error_bad_parameters;
	}

/* Perform free operation *
	/free (SHM);
	return teec_success;
}
6. Execute the recording procedure to perform the efficiency operation   When the Func ID is rpc_cmd_gprof, Tee_supplicant will invoke the Gprof_process function to log a specific TA execution efficiency information to the file system based on TA request. Its contents are as follows:
Teec_result gprof_process (size_t num_params, struct Tee_ioctl_param *params) {char vers[5] = "";
	Char path[255];
	size_t bufsize;
	Teec_uuid *u;
	int FD =-1;
	void *buf;
	int flags;
	int id;
	Int St;

int n;
	    /* TA pass to tee_supplicant parameter check/if (num_params!= 3 | |
	    (Params[0].attr & Tee_ioctl_param_attr_type_mask)!= Tee_ioctl_param_attr_type_value_inout | |
	    (Params[1].attr & Tee_ioctl_param_attr_type_mask)!= Tee_ioctl_param_attr_type_memref_input | | (Params[2].attr & Tee_ioctl_param_attr_type_mask)!= Tee_ioctl_param_attr_type_memref_input) return

Teec_error_bad_parameters;

	/* Used to determine whether a special file needs to be created for record execution efficiency information */id = params[0].u.value.a;
if (params[1].u.memref.size!= sizeof (TEEC_UUID)) return teec_error_bad_parameters;
	/* Get the UUID value of TA that needs to be recorded/U = Tee_supp_param_to_va (params + 1);
if (!u) return teec_error_bad_parameters;
	/* Get the information you need to record/* buf = Tee_supp_param_to_va (params + 2);

	if (!BUF) return teec_error_bad_parameters; BufSize= Params[2].u.memref.size;

	if (ID < 0 | | | ID >) return teec_error_bad_parameters; Flags = O_append |
	O_wronly; if (!id) {/* id = 0 means create file */flags |= O_creat |
		O_EXCL;
	id = 1; /* * Log the information in the buffer into the/tmp/gmon-[uuid].out file/for (;;)
			 {if (id > 1) {/* id = = 1 is file 0 (no suffix), id = = 2 is file. 1 * etc.
		* * snprintf (vers, sizeof (vers), ".%d", id-1);
			} n = snprintf (path, sizeof (path), "/tmp/gmon-" "%08x-%04x-%04x-%02x%02x%02x%02x%02x%02x%02x%02x" "%s.out", U->timelow, U->timemid, U->timehiandversion, u->clockseqandnode[0], u->clockseqandnode[1], U->clo CKSEQANDNODE[2], u->clockseqandnode[3], u->clockseqandnode[4], u->clockseqandnode[5], u->
	        CLOCKSEQANDNODE[6], u->clockseqandnode[7], vers); if ((N < 0) | |
			(n >= (int) sizeof (path))
		Break
		FD = open (path, flags, 0600);
			if (FD >= 0) {do {st = write (fd, buf, bufsize); } while (St < 0 && errno = = eintr);
			Close (FD);
			if (St < 0 | | | St!= (int) bufsize) break;
			Params[0].u.value.a = ID;
		Goto success;
		} if (errno!= eexist) break;
	if (id++ = =) break;

}/* An error occured/return teec_error_generic;
Success:return teec_success; }
7. Perform network socket operationWhen the Func ID is optee_msg_rpc_cmd_socket, Tee_supplicant will invoke the Tee_socket_process function based on TA request to complete the operation of the network socket, including the setting up of the socket, sending, Receive and IOCTL operations. Its contents are as follows:
Teec_result tee_socket_process (size_t num_params,
			       struct Tee_ioctl_param *params)
{
	if (!num_params | |!) Tee_supp_param_is_value (params)) return
		teec_error_bad_parameters;

/* According to the value of VALUE.A to determine what action to perform, and the operation of the data required
    from the params to get
	/switch (PARAMS->U.VALUE.A) {case
	optee_mrc_socket_ OPEN: Return
		tee_socket_open (num_params, params);
	Case Optee_mrc_socket_close: Return
		tee_socket_close (num_params, params);
	Case Optee_mrc_socket_close_all: Return
		tee_socket_close_all (num_params, params);
	Case Optee_mrc_socket_send: Return
		tee_socket_send (num_params, params);
	Case OPTEE_MRC_SOCKET_RECV: Return
		tee_socket_recv (num_params, params);
	Case OPTEE_MRC_SOCKET_IOCTL: Return
		tee_socket_ioctl (num_params, params);
	Default: Return
		teec_error_bad_parameters
	}
}








Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.