blob: 7402366371b6c31c6f2931083496762f07931fb6 [file] [log] [blame]
Jiafei Pan46367ad2018-03-02 07:23:30 +00001/*
2 * Copyright (c) 2018, ARM Limited and Contributors. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6#include <assert.h>
7#include <debug.h>
8#include <firmware_image_package.h>
9#include <io_driver.h>
10#include <io_fip.h>
11#include <io_memmap.h>
12#include <io_storage.h>
13#include "platform_def.h"
14
15/* IO devices */
16static const io_dev_connector_t *fip_dev_con;
17static uintptr_t fip_dev_handle;
18static const io_dev_connector_t *memmap_dev_con;
19static uintptr_t memmap_dev_handle;
20
21static const io_block_spec_t fip_block_spec = {
22 .offset = PLAT_LS_FIP_BASE,
23 .length = PLAT_LS_FIP_MAX_SIZE
24};
25
26static const io_uuid_spec_t bl2_uuid_spec = {
27 .uuid = UUID_TRUSTED_BOOT_FIRMWARE_BL2,
28};
29
30static const io_uuid_spec_t bl31_uuid_spec = {
31 .uuid = UUID_EL3_RUNTIME_FIRMWARE_BL31,
32};
33
34static const io_uuid_spec_t bl32_uuid_spec = {
35 .uuid = UUID_SECURE_PAYLOAD_BL32,
36};
37
38static const io_uuid_spec_t bl33_uuid_spec = {
39 .uuid = UUID_NON_TRUSTED_FIRMWARE_BL33,
40};
41
42static int open_fip(const uintptr_t spec);
43static int open_memmap(const uintptr_t spec);
44
45struct plat_io_policy {
46 uintptr_t *dev_handle;
47 uintptr_t image_spec;
48 int (*check)(const uintptr_t spec);
49};
50
51static const struct plat_io_policy policies[] = {
52 [FIP_IMAGE_ID] = {
53 &memmap_dev_handle,
54 (uintptr_t)&fip_block_spec,
55 open_memmap
56 },
57 [BL2_IMAGE_ID] = {
58 &fip_dev_handle,
59 (uintptr_t)&bl2_uuid_spec,
60 open_fip
61 },
62 [BL31_IMAGE_ID] = {
63 &fip_dev_handle,
64 (uintptr_t)&bl31_uuid_spec,
65 open_fip
66 },
67 [BL32_IMAGE_ID] = {
68 &fip_dev_handle,
69 (uintptr_t)&bl32_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
79static int open_fip(const uintptr_t spec)
80{
81 int result;
82 uintptr_t local_image_handle;
83
84 /* See if a Firmware Image Package is available */
85 result = io_dev_init(fip_dev_handle, (uintptr_t)FIP_IMAGE_ID);
86 if (result == 0) {
87 result = io_open(fip_dev_handle, spec, &local_image_handle);
88 if (result == 0) {
89 VERBOSE("Using FIP\n");
90 io_close(local_image_handle);
91 }
92 }
93 return result;
94}
95
96
97static int open_memmap(const uintptr_t spec)
98{
99 int result;
100 uintptr_t local_image_handle;
101
102 result = io_dev_init(memmap_dev_handle, (uintptr_t)NULL);
103 if (result == 0) {
104 result = io_open(memmap_dev_handle, spec, &local_image_handle);
105 if (result == 0) {
106 VERBOSE("Using Memmap\n");
107 io_close(local_image_handle);
108 }
109 }
110 return result;
111}
112
113
114void ls_io_setup(void)
115{
116 int io_result;
117
118 io_result = register_io_dev_fip(&fip_dev_con);
119 assert(io_result == 0);
120
121 io_result = register_io_dev_memmap(&memmap_dev_con);
122 assert(io_result == 0);
123
124 /* Open connections to devices and cache the handles */
125 io_result = io_dev_open(fip_dev_con, (uintptr_t)NULL,
126 &fip_dev_handle);
127 assert(io_result == 0);
128
129 io_result = io_dev_open(memmap_dev_con, (uintptr_t)NULL,
130 &memmap_dev_handle);
131 assert(io_result == 0);
132
133 /* Ignore improbable errors in release builds */
134 (void)io_result;
135}
136
137void plat_ls_io_setup(void)
138{
139 ls_io_setup();
140}
141
142int plat_ls_get_alt_image_source(
143 unsigned int image_id __unused,
144 uintptr_t *dev_handle __unused,
145 uintptr_t *image_spec __unused)
146{
147 /* By default do not try an alternative */
148 return -ENOENT;
149}
150
151/*
152 * Return an IO device handle and specification which can be used to access
153 * an image. Use this to enforce platform load policy.
154 */
155int plat_get_image_source(unsigned int image_id, uintptr_t *dev_handle,
156 uintptr_t *image_spec)
157{
158 int result;
159 const struct plat_io_policy *policy;
160
161 assert(image_id < ARRAY_SIZE(policies));
162
163 policy = &policies[image_id];
164 result = policy->check(policy->image_spec);
165 if (result == 0) {
166 *image_spec = policy->image_spec;
167 *dev_handle = *(policy->dev_handle);
168 } else {
169 VERBOSE("Trying alternative IO\n");
170 result = plat_ls_get_alt_image_source(image_id, dev_handle,
171 image_spec);
172 }
173
174 return result;
175}