blob: 6bbdb3cf2847638f5fd835bd8933d68985845a3d [file] [log] [blame]
Louis Mayencourtbadcac82019-10-24 15:18:46 +01001/*
2 * Copyright (c) 2015-2020, ARM Limited. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <assert.h>
8
9#include <common/debug.h>
10#include <drivers/io/io_driver.h>
11#include <drivers/io/io_fip.h>
12#include <drivers/io/io_memmap.h>
13#include <drivers/io/io_storage.h>
14#include <lib/utils.h>
15#include <tools_share/firmware_image_package.h>
16
17#include <plat/arm/common/arm_fconf_getter.h>
18#include <plat/arm/common/arm_fconf_io_storage.h>
19#include <plat/arm/common/plat_arm.h>
20#include <plat/common/platform.h>
21#include <platform_def.h>
22
23/* IO devices */
24static const io_dev_connector_t *fip_dev_con;
25uintptr_t fip_dev_handle;
26static const io_dev_connector_t *memmap_dev_con;
27uintptr_t memmap_dev_handle;
28
29/* Weak definitions may be overridden in specific ARM standard platform */
30#pragma weak plat_arm_io_setup
31#pragma weak plat_arm_get_alt_image_source
32
33int open_fip(const uintptr_t spec)
34{
35 int result;
36 uintptr_t local_image_handle;
37
38 /* See if a Firmware Image Package is available */
39 result = io_dev_init(fip_dev_handle, (uintptr_t)FIP_IMAGE_ID);
40 if (result == 0) {
41 result = io_open(fip_dev_handle, spec, &local_image_handle);
42 if (result == 0) {
43 VERBOSE("Using FIP\n");
44 io_close(local_image_handle);
45 }
46 }
47 return result;
48}
49
50int open_memmap(const uintptr_t spec)
51{
52 int result;
53 uintptr_t local_image_handle;
54
55 result = io_dev_init(memmap_dev_handle, (uintptr_t)NULL);
56 if (result == 0) {
57 result = io_open(memmap_dev_handle, spec, &local_image_handle);
58 if (result == 0) {
59 VERBOSE("Using Memmap\n");
60 io_close(local_image_handle);
61 }
62 }
63 return result;
64}
65
66void arm_io_setup(void)
67{
68 int io_result;
69
70 io_result = register_io_dev_fip(&fip_dev_con);
71 assert(io_result == 0);
72
73 io_result = register_io_dev_memmap(&memmap_dev_con);
74 assert(io_result == 0);
75
76 /* Open connections to devices and cache the handles */
77 io_result = io_dev_open(fip_dev_con, (uintptr_t)NULL,
78 &fip_dev_handle);
79 assert(io_result == 0);
80
81 io_result = io_dev_open(memmap_dev_con, (uintptr_t)NULL,
82 &memmap_dev_handle);
83 assert(io_result == 0);
84
85 /* Ignore improbable errors in release builds */
86 (void)io_result;
87}
88
89void plat_arm_io_setup(void)
90{
91 arm_io_setup();
92}
93
94int plat_arm_get_alt_image_source(
95 unsigned int image_id __unused,
96 uintptr_t *dev_handle __unused,
97 uintptr_t *image_spec __unused)
98{
99 /* By default do not try an alternative */
100 return -ENOENT;
101}
102
103/* Return an IO device handle and specification which can be used to access
104 * an image. Use this to enforce platform load policy */
105int plat_get_image_source(unsigned int image_id, uintptr_t *dev_handle,
106 uintptr_t *image_spec)
107{
108 int result;
109 const struct plat_io_policy *policy;
110
111 assert(image_id < MAX_NUMBER_IDS);
112
113 policy = FCONF_GET_PROPERTY(arm, io_policies, image_id);
114 result = policy->check(policy->image_spec);
115 if (result == 0) {
116 *image_spec = policy->image_spec;
117 *dev_handle = *(policy->dev_handle);
118 } else {
119 VERBOSE("Trying alternative IO\n");
120 result = plat_arm_get_alt_image_source(image_id, dev_handle,
121 image_spec);
122 }
123
124 return result;
125}
126
127/*
128 * See if a Firmware Image Package is available,
129 * by checking if TOC is valid or not.
130 */
Louis Mayencourt70d7c092020-01-29 11:42:31 +0000131bool arm_io_is_toc_valid(void)
Louis Mayencourtbadcac82019-10-24 15:18:46 +0100132{
Louis Mayencourt70d7c092020-01-29 11:42:31 +0000133 return (io_dev_init(fip_dev_handle, (uintptr_t)FIP_IMAGE_ID) == 0);
Louis Mayencourtbadcac82019-10-24 15:18:46 +0100134}