J-Alves | 2f86c1e | 2022-02-23 18:44:19 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2022 The Hafnium Authors. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style |
| 5 | * license that can be found in the LICENSE file or at |
| 6 | * https://opensource.org/licenses/BSD-3-Clause. |
| 7 | */ |
| 8 | |
| 9 | #include "hf/sp_pkg.h" |
| 10 | |
| 11 | #include <stdint.h> |
| 12 | |
| 13 | #include "hf/arch/std.h" |
| 14 | |
| 15 | #include "hf/addr.h" |
| 16 | #include "hf/check.h" |
| 17 | #include "hf/dlog.h" |
| 18 | #include "hf/std.h" |
| 19 | |
J-Alves | 60984ce | 2022-04-27 15:27:29 +0100 | [diff] [blame] | 20 | /** |
| 21 | * Function initializes the Secure Partition Package version 0x1: |
| 22 | * - Partition manifest is at offset configured in the package header. |
| 23 | * - Image offset expected to be 4kb aligned, and to follow the pm manifest. |
| 24 | */ |
| 25 | static bool sp_pkg_init_v1(struct mm_stage1_locked stage1_locked, |
| 26 | paddr_t pkg_start, struct sp_pkg_header *header, |
| 27 | struct mpool *ppool) |
| 28 | { |
| 29 | size_t manifest_size; |
| 30 | |
| 31 | assert(header != NULL); |
| 32 | |
| 33 | /* Expect DTB to immediately follow header */ |
| 34 | if (header->pm_offset != sizeof(struct sp_pkg_header)) { |
| 35 | dlog_error("Invalid package manifest offset.\n"); |
| 36 | return false; |
| 37 | } |
| 38 | |
| 39 | if ((header->img_offset % PAGE_SIZE) != 0U) { |
| 40 | dlog_error("Image offset in SP pkg is not page aligned.\n"); |
| 41 | return false; |
| 42 | } |
| 43 | |
| 44 | manifest_size = align_up(header->pm_size + sizeof(struct sp_pkg_header), |
| 45 | PAGE_SIZE); |
| 46 | |
| 47 | /* Check that the pm shouldn't override the image. */ |
| 48 | if (manifest_size > header->img_offset) { |
| 49 | dlog_error("Partition manifest bigger than its region.\n"); |
| 50 | return false; |
| 51 | } |
| 52 | |
| 53 | /* |
| 54 | * Map remainder of header + manifest. This assumes that PAGE_SIZE has |
| 55 | * been mapped already, prior to calling this function. |
| 56 | */ |
| 57 | if (manifest_size > PAGE_SIZE) { |
| 58 | CHECK(mm_identity_map(stage1_locked, pkg_start, |
| 59 | pa_add(pkg_start, manifest_size), |
| 60 | MM_MODE_R, ppool)); |
| 61 | } |
| 62 | |
| 63 | return true; |
| 64 | } |
| 65 | |
J-Alves | 2f86c1e | 2022-02-23 18:44:19 +0000 | [diff] [blame] | 66 | /* |
J-Alves | 60984ce | 2022-04-27 15:27:29 +0100 | [diff] [blame] | 67 | * Function initializes the Secure Partition Package version 0x2: |
J-Alves | 2f86c1e | 2022-02-23 18:44:19 +0000 | [diff] [blame] | 68 | * - Maps whole region up to image such that Hafnium can parse the FF-A manifest |
| 69 | * and can use the first chunk of memory for booting purposes. |
| 70 | */ |
J-Alves | 60984ce | 2022-04-27 15:27:29 +0100 | [diff] [blame] | 71 | static bool sp_pkg_init_v2(struct mm_stage1_locked stage1_locked, |
| 72 | paddr_t pkg_start, struct sp_pkg_header *header, |
| 73 | struct mpool *ppool) |
| 74 | { |
J-Alves | 60984ce | 2022-04-27 15:27:29 +0100 | [diff] [blame] | 75 | assert(header != NULL); |
J-Alves | 6e0abc4 | 2024-12-30 16:51:16 +0000 | [diff] [blame^] | 76 | (void)pkg_start; |
| 77 | (void)ppool; |
| 78 | (void)stage1_locked; |
J-Alves | 60984ce | 2022-04-27 15:27:29 +0100 | [diff] [blame] | 79 | |
| 80 | if (header->pm_offset % PAGE_SIZE != 0 || |
| 81 | header->img_offset % PAGE_SIZE != 0) { |
| 82 | dlog_error("SP pkg offsets are not page aligned.\n"); |
| 83 | return false; |
| 84 | } |
| 85 | |
| 86 | if (header->pm_offset > header->img_offset) { |
| 87 | dlog_error( |
| 88 | "SP pkg offsets must be in order: boot info < " |
| 89 | "partition manifest < image offset.\n"); |
| 90 | return false; |
| 91 | } |
| 92 | |
| 93 | /* |
| 94 | * Check for overflow and then check the pm shouldn't override the |
| 95 | * image. |
| 96 | */ |
| 97 | assert(UINT32_MAX - header->pm_offset >= header->pm_size); |
| 98 | if (header->pm_offset + header->pm_size > header->img_offset) { |
| 99 | dlog_error("Partition manifest bigger than its region.\n"); |
| 100 | return false; |
| 101 | } |
| 102 | |
J-Alves | 60984ce | 2022-04-27 15:27:29 +0100 | [diff] [blame] | 103 | return true; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Initializes the Secure Partition Package. It relies on helper functions |
| 108 | * for the respective versions 1 and 2. Returns true if the initialization goes |
| 109 | * well, otherwise returns false. |
| 110 | */ |
J-Alves | 2f86c1e | 2022-02-23 18:44:19 +0000 | [diff] [blame] | 111 | bool sp_pkg_init(struct mm_stage1_locked stage1_locked, paddr_t pkg_start, |
| 112 | struct sp_pkg_header *header, struct mpool *ppool) |
| 113 | { |
J-Alves | 6e0abc4 | 2024-12-30 16:51:16 +0000 | [diff] [blame^] | 114 | /* |
| 115 | * Assumes the page the first page of the package, has been mapped |
| 116 | * already. |
| 117 | */ |
| 118 | memcpy_s(header, sizeof(struct sp_pkg_header), |
| 119 | ptr_from_va(va_from_pa(pkg_start)), |
J-Alves | 2f86c1e | 2022-02-23 18:44:19 +0000 | [diff] [blame] | 120 | sizeof(struct sp_pkg_header)); |
| 121 | |
J-Alves | 60984ce | 2022-04-27 15:27:29 +0100 | [diff] [blame] | 122 | switch (header->version) { |
| 123 | case SP_PKG_HEADER_VERSION_1: |
| 124 | if (sp_pkg_init_v1(stage1_locked, pkg_start, header, ppool)) { |
| 125 | return true; |
| 126 | } |
| 127 | break; |
| 128 | case SP_PKG_HEADER_VERSION_2: |
| 129 | if (sp_pkg_init_v2(stage1_locked, pkg_start, header, ppool)) { |
| 130 | return true; |
| 131 | } |
| 132 | break; |
| 133 | default: |
J-Alves | 6e0abc4 | 2024-12-30 16:51:16 +0000 | [diff] [blame^] | 134 | dlog_error("%s: Unrecognized Secure Partition Pkg format.\n", |
| 135 | __func__); |
J-Alves | 2f86c1e | 2022-02-23 18:44:19 +0000 | [diff] [blame] | 136 | } |
| 137 | |
J-Alves | 60984ce | 2022-04-27 15:27:29 +0100 | [diff] [blame] | 138 | return false; |
J-Alves | 2f86c1e | 2022-02-23 18:44:19 +0000 | [diff] [blame] | 139 | } |