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