1 /*
2  * Copyright (c) 2017, ARM Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <arch_helpers.h>
8 #include <assert.h>
9 #include <debug.h>
10 #include <firmware_image_package.h>
11 #include <io_block.h>
12 #include <io_driver.h>
13 #include <io_fip.h>
14 #include <io_memmap.h>
15 #include <io_storage.h>
16 #include <mmio.h>
17 #include <partition/partition.h>
18 #include <semihosting.h>
19 #include <string.h>
20 #include <tbbr_img_def.h>
21 #include <utils.h>
22 #include "platform_def.h"
23 
24 static const io_dev_connector_t *mmap_dev_con;
25 static const io_dev_connector_t *fip_dev_con;
26 
27 static uintptr_t mmap_dev_handle;
28 static uintptr_t fip_dev_handle;
29 
30 static int open_mmap(const uintptr_t spec);
31 static int open_fip(const uintptr_t spec);
32 
33 static const io_block_spec_t loader_fip_spec = {
34 	.offset		= FIP_BASE,
35 	.length		= FIP_SIZE
36 };
37 
38 static const io_uuid_spec_t bl2_uuid_spec = {
39 	.uuid = UUID_TRUSTED_BOOT_FIRMWARE_BL2,
40 };
41 
42 static const io_uuid_spec_t bl31_uuid_spec = {
43 	.uuid = UUID_EL3_RUNTIME_FIRMWARE_BL31,
44 };
45 
46 static const io_uuid_spec_t bl33_uuid_spec = {
47 	.uuid = UUID_NON_TRUSTED_FIRMWARE_BL33,
48 };
49 
50 struct plat_io_policy {
51 	uintptr_t	*dev_handle;
52 	uintptr_t	image_spec;
53 	int		(*check)(const uintptr_t spec);
54 };
55 
56 static const struct plat_io_policy policies[] = {
57 	[FIP_IMAGE_ID] = {
58 		&mmap_dev_handle,
59 		(uintptr_t)&loader_fip_spec,
60 		open_mmap
61 	},
62 	[BL2_IMAGE_ID] = {
63 		&fip_dev_handle,
64 		(uintptr_t)&bl2_uuid_spec,
65 		open_fip
66 	},
67 	[BL31_IMAGE_ID] = {
68 		&fip_dev_handle,
69 		(uintptr_t)&bl31_uuid_spec,
70 		open_fip
71 	},
72 	[BL33_IMAGE_ID] = {
73 		&fip_dev_handle,
74 		(uintptr_t)&bl33_uuid_spec,
75 		open_fip
76 	},
77 };
78 
open_mmap(const uintptr_t spec)79 static int open_mmap(const uintptr_t spec)
80 {
81 	int result;
82 	uintptr_t local_image_handle;
83 
84 	result = io_dev_init(mmap_dev_handle, (uintptr_t)NULL);
85 	if (result == 0) {
86 		result = io_open(mmap_dev_handle, spec, &local_image_handle);
87 		if (result == 0) {
88 			io_close(local_image_handle);
89 		}
90 	}
91 	return result;
92 }
93 
open_fip(const uintptr_t spec)94 static int open_fip(const uintptr_t spec)
95 {
96 	uintptr_t local_image_handle;
97 	int result;
98 
99 	result = io_dev_init(fip_dev_handle, (uintptr_t) FIP_IMAGE_ID);
100 	if (result == 0) {
101 		result = io_open(fip_dev_handle, spec, &local_image_handle);
102 		if (result == 0) {
103 			io_close(local_image_handle);
104 		} else {
105 			VERBOSE("error opening fip\n");
106 		}
107 	} else {
108 		VERBOSE("error initializing fip\n");
109 	}
110 
111 	return result;
112 }
113 
plat_get_image_source(unsigned int image_id,uintptr_t * dev_handle,uintptr_t * image_spec)114 int plat_get_image_source(unsigned int image_id, uintptr_t *dev_handle,
115 			  uintptr_t *image_spec)
116 {
117 	const struct plat_io_policy *policy;
118 	int result;
119 
120 	assert(image_id < ARRAY_SIZE(policies));
121 
122 	policy = &policies[image_id];
123 	result = policy->check(policy->image_spec);
124 	assert(result == 0);
125 
126 	*image_spec = policy->image_spec;
127 	*dev_handle = *(policy->dev_handle);
128 
129 	return result;
130 }
131 
plat_io_setup(void)132 void plat_io_setup(void)
133 {
134 	int result;
135 
136 	result = register_io_dev_memmap(&mmap_dev_con);
137 	assert(result == 0);
138 
139 	result = register_io_dev_fip(&fip_dev_con);
140 	assert(result == 0);
141 
142 	result = io_dev_open(fip_dev_con, (uintptr_t)&loader_fip_spec,
143 				&fip_dev_handle);
144 	assert(result == 0);
145 
146 	result = io_dev_open(mmap_dev_con, (uintptr_t)NULL, &mmap_dev_handle);
147 	assert(result == 0);
148 
149 	(void) result;
150 }
151