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