xref: /optee_os/ldelf/main.c (revision d1911a85142da16fef5ebdcdac0348d29ce37cd8)
1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (c) 2019, Linaro Limited
4  */
5 
6 #include <ldelf.h>
7 #include <malloc.h>
8 #include <sys/queue.h>
9 #include <tee_api_types.h>
10 #include <trace.h>
11 #include <types_ext.h>
12 
13 #include "ta_elf.h"
14 #include "sys.h"
15 
16 static size_t mpool_size = 2 * SMALL_PAGE_SIZE;
17 static vaddr_t mpool_base;
18 
19 /*
20  * ldelf()- Loads ELF into memory
21  * @arg:	Argument passing to/from TEE Core
22  *
23  * Only called from assembly
24  */
25 void __noreturn ldelf(struct ldelf_arg *arg);
26 void ldelf(struct ldelf_arg *arg)
27 {
28 	TEE_Result res = TEE_SUCCESS;
29 	struct ta_elf *elf = NULL;
30 
31 	DMSG("Loading TA %pUl", (void *)&arg->uuid);
32 	res = sys_map_zi(mpool_size, 0, &mpool_base, 0, 0);
33 	if (res) {
34 		EMSG("sys_map_zi(%zu): result %"PRIx32, mpool_size, res);
35 		panic();
36 	}
37 	malloc_add_pool((void *)mpool_base, mpool_size);
38 
39 	/* Load the main binary and get a list of dependencies, if any. */
40 	ta_elf_load_main(&arg->uuid, &arg->is_32bit, &arg->entry_func,
41 			 &arg->stack_ptr, &arg->flags);
42 
43 	/*
44 	 * Load binaries, ta_elf_load() may add external libraries to the
45 	 * list, so the loop will end when all the dependencies are
46 	 * satisfied.
47 	 */
48 	TAILQ_FOREACH(elf, &main_elf_queue, link)
49 		ta_elf_load_dependency(elf, arg->is_32bit);
50 
51 	TAILQ_FOREACH(elf, &main_elf_queue, link) {
52 		ta_elf_relocate(elf);
53 		ta_elf_finalize_mappings(elf);
54 	}
55 
56 	TAILQ_FOREACH(elf, &main_elf_queue, link)
57 		DMSG("ELF (%pUl) at %#"PRIxVA,
58 		     (void *)&elf->uuid, elf->load_addr);
59 
60 	res = sys_unmap(mpool_base, mpool_size);
61 	if (res) {
62 		EMSG("sys_unmap(%p, %zu): result %"PRIx32,
63 		     (void *)mpool_base, mpool_size, res);
64 		panic();
65 	}
66 	sys_return_cleanup();
67 }
68