Achin Gupta | da6ef0e | 2019-10-11 14:54:48 +0100 | [diff] [blame] | 1 | /* |
Manish V Badarkhe | a0d5335 | 2023-02-07 11:26:38 +0000 | [diff] [blame] | 2 | * Copyright (c) 2020-2023, Arm Limited. All rights reserved. |
Achin Gupta | da6ef0e | 2019-10-11 14:54:48 +0100 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
| 7 | #include <assert.h> |
Olivier Deprez | 2bae35f | 2020-04-16 13:39:06 +0200 | [diff] [blame] | 8 | #include <errno.h> |
Scott Branden | e5dcf98 | 2020-08-25 13:49:32 -0700 | [diff] [blame] | 9 | #include <inttypes.h> |
Achin Gupta | da6ef0e | 2019-10-11 14:54:48 +0100 | [diff] [blame] | 10 | #include <libfdt.h> |
Scott Branden | e5dcf98 | 2020-08-25 13:49:32 -0700 | [diff] [blame] | 11 | #include <stdint.h> |
| 12 | #include <string.h> |
Achin Gupta | da6ef0e | 2019-10-11 14:54:48 +0100 | [diff] [blame] | 13 | |
Varun Wadekar | 2e70a34 | 2020-07-19 21:28:10 -0700 | [diff] [blame] | 14 | #include <common/bl_common.h> |
Achin Gupta | da6ef0e | 2019-10-11 14:54:48 +0100 | [diff] [blame] | 15 | #include <common/debug.h> |
| 16 | #include <common/fdt_wrappers.h> |
Olivier Deprez | 69ca84a | 2020-02-07 15:44:43 +0100 | [diff] [blame] | 17 | #include <lib/xlat_tables/xlat_tables_v2.h> |
Achin Gupta | da6ef0e | 2019-10-11 14:54:48 +0100 | [diff] [blame] | 18 | #include <platform_def.h> |
| 19 | #include <services/spm_core_manifest.h> |
| 20 | |
Olivier Deprez | 2bae35f | 2020-04-16 13:39:06 +0200 | [diff] [blame] | 21 | #define ATTRIBUTE_ROOT_NODE_STR "attribute" |
| 22 | |
Achin Gupta | da6ef0e | 2019-10-11 14:54:48 +0100 | [diff] [blame] | 23 | /******************************************************************************* |
Olivier Deprez | 2bae35f | 2020-04-16 13:39:06 +0200 | [diff] [blame] | 24 | * SPMC attribute node parser |
Achin Gupta | da6ef0e | 2019-10-11 14:54:48 +0100 | [diff] [blame] | 25 | ******************************************************************************/ |
Olivier Deprez | 2bae35f | 2020-04-16 13:39:06 +0200 | [diff] [blame] | 26 | static int manifest_parse_attribute(spmc_manifest_attribute_t *attr, |
Achin Gupta | da6ef0e | 2019-10-11 14:54:48 +0100 | [diff] [blame] | 27 | const void *fdt, |
| 28 | int node) |
| 29 | { |
Andre Przywara | fe5bdf5 | 2020-03-26 11:22:37 +0000 | [diff] [blame] | 30 | uint32_t val32; |
Olivier Deprez | 2bae35f | 2020-04-16 13:39:06 +0200 | [diff] [blame] | 31 | int rc; |
Achin Gupta | da6ef0e | 2019-10-11 14:54:48 +0100 | [diff] [blame] | 32 | |
Olivier Deprez | 2bae35f | 2020-04-16 13:39:06 +0200 | [diff] [blame] | 33 | assert((attr != NULL) && (fdt != NULL)); |
Achin Gupta | da6ef0e | 2019-10-11 14:54:48 +0100 | [diff] [blame] | 34 | |
Andre Przywara | fe5bdf5 | 2020-03-26 11:22:37 +0000 | [diff] [blame] | 35 | rc = fdt_read_uint32(fdt, node, "maj_ver", &attr->major_version); |
Olivier Deprez | 2bae35f | 2020-04-16 13:39:06 +0200 | [diff] [blame] | 36 | if (rc != 0) { |
J-Alves | 2672cde | 2020-05-07 18:42:25 +0100 | [diff] [blame] | 37 | ERROR("Missing FFA %s version in SPM Core manifest.\n", |
Olivier Deprez | 2bae35f | 2020-04-16 13:39:06 +0200 | [diff] [blame] | 38 | "major"); |
| 39 | return rc; |
Achin Gupta | da6ef0e | 2019-10-11 14:54:48 +0100 | [diff] [blame] | 40 | } |
| 41 | |
Andre Przywara | fe5bdf5 | 2020-03-26 11:22:37 +0000 | [diff] [blame] | 42 | rc = fdt_read_uint32(fdt, node, "min_ver", &attr->minor_version); |
Olivier Deprez | 2bae35f | 2020-04-16 13:39:06 +0200 | [diff] [blame] | 43 | if (rc != 0) { |
J-Alves | 2672cde | 2020-05-07 18:42:25 +0100 | [diff] [blame] | 44 | ERROR("Missing FFA %s version in SPM Core manifest.\n", |
Olivier Deprez | 2bae35f | 2020-04-16 13:39:06 +0200 | [diff] [blame] | 45 | "minor"); |
| 46 | return rc; |
Achin Gupta | da6ef0e | 2019-10-11 14:54:48 +0100 | [diff] [blame] | 47 | } |
| 48 | |
Andre Przywara | fe5bdf5 | 2020-03-26 11:22:37 +0000 | [diff] [blame] | 49 | rc = fdt_read_uint32(fdt, node, "spmc_id", &val32); |
Olivier Deprez | 2bae35f | 2020-04-16 13:39:06 +0200 | [diff] [blame] | 50 | if (rc != 0) { |
Max Shvetsov | e79062e | 2020-03-12 15:16:40 +0000 | [diff] [blame] | 51 | ERROR("Missing SPMC ID in manifest.\n"); |
Olivier Deprez | 2bae35f | 2020-04-16 13:39:06 +0200 | [diff] [blame] | 52 | return rc; |
Max Shvetsov | e79062e | 2020-03-12 15:16:40 +0000 | [diff] [blame] | 53 | } |
Olivier Deprez | 2bae35f | 2020-04-16 13:39:06 +0200 | [diff] [blame] | 54 | |
| 55 | attr->spmc_id = val32 & 0xffff; |
Max Shvetsov | e79062e | 2020-03-12 15:16:40 +0000 | [diff] [blame] | 56 | |
Andre Przywara | fe5bdf5 | 2020-03-26 11:22:37 +0000 | [diff] [blame] | 57 | rc = fdt_read_uint32(fdt, node, "exec_state", &attr->exec_state); |
Olivier Deprez | 2bae35f | 2020-04-16 13:39:06 +0200 | [diff] [blame] | 58 | if (rc != 0) { |
| 59 | NOTICE("%s not specified in SPM Core manifest.\n", |
| 60 | "Execution state"); |
| 61 | } |
Achin Gupta | da6ef0e | 2019-10-11 14:54:48 +0100 | [diff] [blame] | 62 | |
Andre Przywara | fe5bdf5 | 2020-03-26 11:22:37 +0000 | [diff] [blame] | 63 | rc = fdt_read_uint32(fdt, node, "binary_size", &attr->binary_size); |
Olivier Deprez | 2bae35f | 2020-04-16 13:39:06 +0200 | [diff] [blame] | 64 | if (rc != 0) { |
| 65 | NOTICE("%s not specified in SPM Core manifest.\n", |
| 66 | "Binary size"); |
| 67 | } |
Achin Gupta | da6ef0e | 2019-10-11 14:54:48 +0100 | [diff] [blame] | 68 | |
Andre Przywara | fe5bdf5 | 2020-03-26 11:22:37 +0000 | [diff] [blame] | 69 | rc = fdt_read_uint64(fdt, node, "load_address", &attr->load_address); |
Olivier Deprez | 2bae35f | 2020-04-16 13:39:06 +0200 | [diff] [blame] | 70 | if (rc != 0) { |
| 71 | NOTICE("%s not specified in SPM Core manifest.\n", |
| 72 | "Load address"); |
| 73 | } |
Achin Gupta | da6ef0e | 2019-10-11 14:54:48 +0100 | [diff] [blame] | 74 | |
Andre Przywara | fe5bdf5 | 2020-03-26 11:22:37 +0000 | [diff] [blame] | 75 | rc = fdt_read_uint64(fdt, node, "entrypoint", &attr->entrypoint); |
Olivier Deprez | 2bae35f | 2020-04-16 13:39:06 +0200 | [diff] [blame] | 76 | if (rc != 0) { |
| 77 | NOTICE("%s not specified in SPM Core manifest.\n", |
| 78 | "Entry point"); |
| 79 | } |
Achin Gupta | da6ef0e | 2019-10-11 14:54:48 +0100 | [diff] [blame] | 80 | |
Olivier Deprez | 2bae35f | 2020-04-16 13:39:06 +0200 | [diff] [blame] | 81 | VERBOSE("SPM Core manifest attribute section:\n"); |
| 82 | VERBOSE(" version: %u.%u\n", attr->major_version, attr->minor_version); |
| 83 | VERBOSE(" spmc_id: 0x%x\n", attr->spmc_id); |
Achin Gupta | da6ef0e | 2019-10-11 14:54:48 +0100 | [diff] [blame] | 84 | VERBOSE(" binary_size: 0x%x\n", attr->binary_size); |
Scott Branden | e5dcf98 | 2020-08-25 13:49:32 -0700 | [diff] [blame] | 85 | VERBOSE(" load_address: 0x%" PRIx64 "\n", attr->load_address); |
| 86 | VERBOSE(" entrypoint: 0x%" PRIx64 "\n", attr->entrypoint); |
Achin Gupta | da6ef0e | 2019-10-11 14:54:48 +0100 | [diff] [blame] | 87 | |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | /******************************************************************************* |
| 92 | * Root node handler |
| 93 | ******************************************************************************/ |
Olivier Deprez | 2bae35f | 2020-04-16 13:39:06 +0200 | [diff] [blame] | 94 | static int manifest_parse_root(spmc_manifest_attribute_t *manifest, |
| 95 | const void *fdt, |
| 96 | int root) |
Achin Gupta | da6ef0e | 2019-10-11 14:54:48 +0100 | [diff] [blame] | 97 | { |
| 98 | int node; |
Olivier Deprez | 2bae35f | 2020-04-16 13:39:06 +0200 | [diff] [blame] | 99 | |
| 100 | assert(manifest != NULL); |
Achin Gupta | da6ef0e | 2019-10-11 14:54:48 +0100 | [diff] [blame] | 101 | |
Olivier Deprez | 2bae35f | 2020-04-16 13:39:06 +0200 | [diff] [blame] | 102 | node = fdt_subnode_offset_namelen(fdt, root, ATTRIBUTE_ROOT_NODE_STR, |
| 103 | sizeof(ATTRIBUTE_ROOT_NODE_STR) - 1); |
Achin Gupta | da6ef0e | 2019-10-11 14:54:48 +0100 | [diff] [blame] | 104 | if (node < 0) { |
Olivier Deprez | 2bae35f | 2020-04-16 13:39:06 +0200 | [diff] [blame] | 105 | ERROR("Root node doesn't contain subnode '%s'\n", |
| 106 | ATTRIBUTE_ROOT_NODE_STR); |
| 107 | return node; |
Achin Gupta | da6ef0e | 2019-10-11 14:54:48 +0100 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | return manifest_parse_attribute(manifest, fdt, node); |
| 111 | } |
| 112 | |
| 113 | /******************************************************************************* |
Olivier Deprez | 2bae35f | 2020-04-16 13:39:06 +0200 | [diff] [blame] | 114 | * Platform handler to parse a SPM Core manifest. |
Achin Gupta | da6ef0e | 2019-10-11 14:54:48 +0100 | [diff] [blame] | 115 | ******************************************************************************/ |
Olivier Deprez | 2bae35f | 2020-04-16 13:39:06 +0200 | [diff] [blame] | 116 | int plat_spm_core_manifest_load(spmc_manifest_attribute_t *manifest, |
Olivier Deprez | 69ca84a | 2020-02-07 15:44:43 +0100 | [diff] [blame] | 117 | const void *pm_addr) |
Achin Gupta | da6ef0e | 2019-10-11 14:54:48 +0100 | [diff] [blame] | 118 | { |
Olivier Deprez | 69ca84a | 2020-02-07 15:44:43 +0100 | [diff] [blame] | 119 | int rc, unmap_ret; |
| 120 | uintptr_t pm_base, pm_base_align; |
| 121 | size_t mapped_size; |
Achin Gupta | da6ef0e | 2019-10-11 14:54:48 +0100 | [diff] [blame] | 122 | |
| 123 | assert(manifest != NULL); |
Olivier Deprez | 69ca84a | 2020-02-07 15:44:43 +0100 | [diff] [blame] | 124 | assert(pm_addr != NULL); |
| 125 | |
| 126 | /* |
| 127 | * Assume TOS_FW_CONFIG is not necessarily aligned to a page |
| 128 | * boundary, thus calculate the remaining space between SPMC |
| 129 | * manifest start address and upper page limit. |
| 130 | * |
| 131 | */ |
| 132 | pm_base = (uintptr_t)pm_addr; |
| 133 | pm_base_align = page_align(pm_base, UP); |
Manish Pandey | bc62d15 | 2020-07-09 00:39:16 +0100 | [diff] [blame] | 134 | |
| 135 | if (pm_base == pm_base_align) { |
| 136 | /* Page aligned */ |
| 137 | mapped_size = PAGE_SIZE; |
| 138 | } else { |
| 139 | mapped_size = pm_base_align - pm_base; |
| 140 | } |
Achin Gupta | da6ef0e | 2019-10-11 14:54:48 +0100 | [diff] [blame] | 141 | |
Olivier Deprez | 69ca84a | 2020-02-07 15:44:43 +0100 | [diff] [blame] | 142 | /* Check space within the page at least maps the FDT header */ |
| 143 | if (mapped_size < sizeof(struct fdt_header)) { |
| 144 | ERROR("Error while mapping SPM Core manifest.\n"); |
| 145 | return -EINVAL; |
| 146 | } |
Achin Gupta | da6ef0e | 2019-10-11 14:54:48 +0100 | [diff] [blame] | 147 | |
Olivier Deprez | 69ca84a | 2020-02-07 15:44:43 +0100 | [diff] [blame] | 148 | /* Map first SPMC manifest page in the SPMD translation regime */ |
| 149 | pm_base_align = page_align(pm_base, DOWN); |
| 150 | rc = mmap_add_dynamic_region((unsigned long long)pm_base_align, |
| 151 | pm_base_align, |
| 152 | PAGE_SIZE, |
Manish V Badarkhe | a0d5335 | 2023-02-07 11:26:38 +0000 | [diff] [blame] | 153 | MT_RO_DATA | EL3_PAS); |
Achin Gupta | da6ef0e | 2019-10-11 14:54:48 +0100 | [diff] [blame] | 154 | if (rc != 0) { |
Olivier Deprez | 69ca84a | 2020-02-07 15:44:43 +0100 | [diff] [blame] | 155 | ERROR("Error while mapping SPM Core manifest (%d).\n", rc); |
Olivier Deprez | 2bae35f | 2020-04-16 13:39:06 +0200 | [diff] [blame] | 156 | return rc; |
Achin Gupta | da6ef0e | 2019-10-11 14:54:48 +0100 | [diff] [blame] | 157 | } |
| 158 | |
Olivier Deprez | 69ca84a | 2020-02-07 15:44:43 +0100 | [diff] [blame] | 159 | rc = fdt_check_header(pm_addr); |
| 160 | if (rc != 0) { |
| 161 | ERROR("Wrong format for SPM Core manifest (%d).\n", rc); |
| 162 | goto exit_unmap; |
| 163 | } |
| 164 | |
| 165 | /* Check SPMC manifest fits within the upper mapped page boundary */ |
| 166 | if (mapped_size < fdt_totalsize(pm_addr)) { |
| 167 | ERROR("SPM Core manifest too large.\n"); |
| 168 | rc = -EINVAL; |
| 169 | goto exit_unmap; |
| 170 | } |
| 171 | |
| 172 | VERBOSE("Reading SPM Core manifest at address %p\n", pm_addr); |
| 173 | |
| 174 | rc = fdt_node_offset_by_compatible(pm_addr, -1, |
J-Alves | 2672cde | 2020-05-07 18:42:25 +0100 | [diff] [blame] | 175 | "arm,ffa-core-manifest-1.0"); |
Olivier Deprez | 2bae35f | 2020-04-16 13:39:06 +0200 | [diff] [blame] | 176 | if (rc < 0) { |
| 177 | ERROR("Unrecognized SPM Core manifest\n"); |
Olivier Deprez | 69ca84a | 2020-02-07 15:44:43 +0100 | [diff] [blame] | 178 | goto exit_unmap; |
Achin Gupta | da6ef0e | 2019-10-11 14:54:48 +0100 | [diff] [blame] | 179 | } |
| 180 | |
Olivier Deprez | 69ca84a | 2020-02-07 15:44:43 +0100 | [diff] [blame] | 181 | rc = manifest_parse_root(manifest, pm_addr, rc); |
| 182 | |
| 183 | exit_unmap: |
| 184 | unmap_ret = mmap_remove_dynamic_region(pm_base_align, PAGE_SIZE); |
| 185 | if (unmap_ret != 0) { |
| 186 | ERROR("Error while unmapping SPM Core manifest (%d).\n", |
| 187 | unmap_ret); |
| 188 | if (rc == 0) { |
| 189 | rc = unmap_ret; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | return rc; |
Achin Gupta | da6ef0e | 2019-10-11 14:54:48 +0100 | [diff] [blame] | 194 | } |