Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018, Arm Limited. All rights reserved. |
| 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
| 7 | /******************************************************************************* |
| 8 | * Test the FWU SMC interface in Trusted Firmware-A, which is implemented in |
| 9 | * BL1. |
| 10 | ******************************************************************************/ |
| 11 | |
Sandrine Bailleux | 7d34d30 | 2018-11-26 10:13:51 +0100 | [diff] [blame] | 12 | #include <bl1.h> |
Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 13 | #include <debug.h> |
Antonio Nino Diaz | 09a00ef | 2019-01-11 13:12:58 +0000 | [diff] [blame^] | 14 | #include <drivers/io/io_fip.h> |
Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 15 | #include <errno.h> |
Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 16 | #include <platform_def.h> |
| 17 | #include <smccc.h> |
| 18 | #include <status.h> |
| 19 | #include <tftf.h> |
| 20 | #include <tftf_lib.h> |
| 21 | |
Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 22 | typedef struct { |
| 23 | /* Description to print before sending the SMC */ |
| 24 | const char *description; |
| 25 | /* The arguments to pass to the SMC */ |
| 26 | const smc_args args; |
| 27 | /* The expected SMC return value */ |
| 28 | u_register_t expect; |
| 29 | } ns_bl1u_test_t; |
| 30 | |
| 31 | /* |
| 32 | * The tests consist in sending a succession of SMCs to trigger FWU operations |
| 33 | * in BL1. The order of the SMCs is important because they internally change the |
| 34 | * FWU state machine in Trusted Firmware-A. |
| 35 | */ |
| 36 | static const ns_bl1u_test_t tests[] = { |
| 37 | /* Basic FWU SMC handler test cases. */ |
| 38 | { |
| 39 | .description = "BL1_SMC_CALL_COUNT", |
| 40 | .args = { BL1_SMC_CALL_COUNT, 0, 0, 0, 0 }, |
| 41 | .expect = BL1_NUM_SMC_CALLS, |
| 42 | }, |
| 43 | |
| 44 | { |
| 45 | .description = "BL1_SMC_VERSION", |
| 46 | .args = { BL1_SMC_VERSION, 0, 0, 0, 0 }, |
Sandrine Bailleux | 7d34d30 | 2018-11-26 10:13:51 +0100 | [diff] [blame] | 47 | .expect = BL1_VERSION, |
Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 48 | }, |
| 49 | |
| 50 | { |
| 51 | .description = "Invalid SMC", |
| 52 | .args = { 0xdeadbeef, 0, 0, 0, 0 }, |
| 53 | .expect = SMC_UNKNOWN, |
| 54 | }, |
| 55 | |
| 56 | /* FWU_SMC_IMAGE_COPY test cases. */ |
| 57 | { |
| 58 | .description = "IMAGE_COPY with invalid image_id", |
| 59 | .args = { FWU_SMC_IMAGE_COPY, 0xdeadbeef, 0, 0, 0 }, |
| 60 | .expect = -EPERM, |
| 61 | }, |
| 62 | |
| 63 | { |
| 64 | .description = "IMAGE_COPY with non-secure image_id", |
| 65 | .args = { FWU_SMC_IMAGE_COPY, NS_BL2U_IMAGE_ID, 0, 0, 0 }, |
| 66 | .expect = -EPERM, |
| 67 | }, |
| 68 | |
| 69 | { |
| 70 | .description = "IMAGE_COPY with valid args", |
| 71 | .args = { FWU_SMC_IMAGE_COPY, FWU_CERT_ID, PLAT_ARM_FWU_FIP_BASE, 0x20, 0x20 }, |
| 72 | .expect = STATUS_SUCCESS, |
| 73 | }, |
| 74 | |
| 75 | { |
| 76 | .description = "IMAGE_COPY to copy an image_id again", |
| 77 | .args = { FWU_SMC_IMAGE_COPY, FWU_CERT_ID, PLAT_ARM_FWU_FIP_BASE, 0x20, 0x20 }, |
| 78 | .expect = -EPERM, |
| 79 | }, |
| 80 | |
| 81 | { |
| 82 | .description = "IMAGE_COPY with source address not mapped", |
| 83 | .args = { FWU_SMC_IMAGE_COPY, BL2U_IMAGE_ID, 0, 0, 0 }, |
| 84 | .expect = -ENOMEM, |
| 85 | }, |
| 86 | |
| 87 | { |
| 88 | .description = "IMAGE_COPY with source size not mapped", |
| 89 | .args = { FWU_SMC_IMAGE_COPY, BL2U_IMAGE_ID, PLAT_ARM_FWU_FIP_BASE, 0xdeadbeef, 0xdeadbeef }, |
| 90 | .expect = -ENOMEM, |
| 91 | }, |
| 92 | |
| 93 | { |
| 94 | .description = "IMAGE_COPY with image size more than secure mem", |
| 95 | .args = { FWU_SMC_IMAGE_COPY, BL2U_IMAGE_ID, PLAT_ARM_FWU_FIP_BASE, 0x40000, 0x40000 }, |
| 96 | .expect = -ENOMEM, |
| 97 | }, |
| 98 | |
| 99 | { |
| 100 | .description = "IMAGE_COPY with image size 0", |
| 101 | .args = { FWU_SMC_IMAGE_COPY, BL2U_IMAGE_ID, PLAT_ARM_FWU_FIP_BASE, 0, 0 }, |
| 102 | .expect = -ENOMEM, |
| 103 | }, |
| 104 | |
| 105 | /* |
| 106 | * At this point, the FWU Certificate Image has been copied by a |
| 107 | * previous test. Try to load the BL2U image over it at the same |
| 108 | * address. |
| 109 | */ |
| 110 | { |
| 111 | .description = "IMAGE_COPY with an image that overlaps a different one", |
| 112 | .args = { FWU_SMC_IMAGE_COPY, BL2U_IMAGE_ID, PLAT_ARM_FWU_FIP_BASE, 0x20, 0x40 }, |
| 113 | .expect = -EPERM, |
| 114 | }, |
| 115 | |
| 116 | /* |
| 117 | * The authentication of the FWU certificate will fail, which will set |
| 118 | * the state of this image to "RESET" for the following tests. |
| 119 | */ |
| 120 | { |
| 121 | .description = "IMAGE_AUTH with an invalid image", |
| 122 | .args = { FWU_SMC_IMAGE_AUTH, FWU_CERT_ID, PLAT_ARM_FWU_FIP_BASE, 0x20, 0x20 }, |
| 123 | .expect = -EAUTH, |
| 124 | }, |
| 125 | |
| 126 | { |
| 127 | .description = "IMAGE_COPY with 1st block size in partial copy", |
| 128 | .args = { FWU_SMC_IMAGE_COPY, BL2U_IMAGE_ID, PLAT_ARM_FWU_FIP_BASE, 0x20, 0x40 }, |
| 129 | .expect = STATUS_SUCCESS, |
| 130 | }, |
| 131 | |
| 132 | { |
| 133 | .description = "IMAGE_AUTH while copying the image", |
| 134 | .args = { FWU_SMC_IMAGE_AUTH, BL2U_IMAGE_ID, PLAT_ARM_FWU_FIP_BASE, 0x40, 0 }, |
| 135 | .expect = -EPERM, |
| 136 | }, |
| 137 | |
| 138 | { |
| 139 | .description = "IMAGE_COPY with last block with invalid source in partial copy", |
| 140 | .args = { FWU_SMC_IMAGE_COPY, BL2U_IMAGE_ID, 0, 0x21, 0x40 }, |
| 141 | .expect = -ENOMEM, |
| 142 | }, |
| 143 | |
| 144 | { |
| 145 | .description = "IMAGE_COPY with last block size > total size in partial copy", |
| 146 | .args = { FWU_SMC_IMAGE_COPY, BL2U_IMAGE_ID, PLAT_ARM_FWU_FIP_BASE, 0x21, 0x40 }, |
| 147 | .expect = STATUS_SUCCESS, |
| 148 | }, |
| 149 | |
| 150 | { |
| 151 | .description = "IMAGE_AUTH to RESET the image state", |
| 152 | .args = { FWU_SMC_IMAGE_AUTH, BL2U_IMAGE_ID, PLAT_ARM_FWU_FIP_BASE, 0x40, 0 }, |
| 153 | .expect = -EAUTH, |
| 154 | }, |
| 155 | |
| 156 | { |
| 157 | .description = "IMAGE_COPY with block size > total size", |
| 158 | .args = { FWU_SMC_IMAGE_COPY, BL2U_IMAGE_ID, PLAT_ARM_FWU_FIP_BASE, 0x21, 0x20 }, |
| 159 | .expect = STATUS_SUCCESS, |
| 160 | }, |
| 161 | |
| 162 | { |
| 163 | .description = "IMAGE_RESET to RESET the image state", |
| 164 | .args = { FWU_SMC_IMAGE_RESET, BL2U_IMAGE_ID, 0, 0, 0 }, |
| 165 | .expect = STATUS_SUCCESS, |
| 166 | }, |
| 167 | |
| 168 | |
| 169 | /* FWU_SMC_IMAGE_AUTH test cases. */ |
| 170 | { |
| 171 | .description = "IMAGE_AUTH with invalid image_id", |
| 172 | .args = { FWU_SMC_IMAGE_AUTH, 0, 0, 0, 0 }, |
| 173 | .expect = -EPERM, |
| 174 | }, |
| 175 | |
| 176 | { |
| 177 | .description = "IMAGE_AUTH with secure image not copied", |
| 178 | .args = { FWU_SMC_IMAGE_AUTH, BL2U_IMAGE_ID, 0, 0, 0 }, |
| 179 | .expect = -EPERM, |
| 180 | }, |
| 181 | |
| 182 | { |
| 183 | .description = "IMAGE_AUTH with source address not mapped", |
| 184 | .args = { FWU_SMC_IMAGE_AUTH, NS_BL2U_IMAGE_ID, 0, 0, 0 }, |
| 185 | .expect = -ENOMEM, |
| 186 | }, |
| 187 | |
| 188 | { |
| 189 | .description = "IMAGE_AUTH with source size not mapped", |
| 190 | .args = { FWU_SMC_IMAGE_AUTH, NS_BL2U_IMAGE_ID, PLAT_ARM_FWU_FIP_BASE, 0xdeadbeef, 0 }, |
| 191 | .expect = -ENOMEM, |
| 192 | }, |
| 193 | |
| 194 | { |
| 195 | .description = "IMAGE_COPY to copy after auth failure", |
| 196 | .args = { FWU_SMC_IMAGE_COPY, FWU_CERT_ID, PLAT_ARM_FWU_FIP_BASE, 0x40, 0x40 }, |
| 197 | .expect = STATUS_SUCCESS, |
| 198 | }, |
| 199 | |
| 200 | { |
| 201 | .description = "IMAGE_AUTH with valid args for copied image", |
| 202 | .args = { FWU_SMC_IMAGE_AUTH, FWU_CERT_ID, 0, 0, 0 }, |
| 203 | .expect = -EAUTH, |
| 204 | }, |
| 205 | |
| 206 | /* FWU_SMC_IMAGE_EXECUTE test cases. */ |
| 207 | { |
| 208 | .description = "IMAGE_EXECUTE with invalid image_id", |
| 209 | .args = { FWU_SMC_IMAGE_EXECUTE, 0, 0, 0, 0 }, |
| 210 | .expect = -EPERM, |
| 211 | }, |
| 212 | |
| 213 | { |
| 214 | .description = "IMAGE_EXECUTE with non-executable image_id", |
| 215 | .args = { FWU_SMC_IMAGE_EXECUTE, FWU_CERT_ID, 0, 0, 0 }, |
| 216 | .expect = -EPERM, |
| 217 | }, |
| 218 | |
| 219 | { |
| 220 | .description = "IMAGE_EXECUTE with un-authenticated image_id", |
| 221 | .args = { FWU_SMC_IMAGE_EXECUTE, BL2U_IMAGE_ID, 0, 0, 0 }, |
| 222 | .expect = -EPERM, |
| 223 | }, |
| 224 | |
| 225 | |
| 226 | /* FWU_SMC_IMAGE_RESUME test case. */ |
| 227 | { |
| 228 | .description = "IMAGE_RESUME with invalid args", |
| 229 | .args = { FWU_SMC_IMAGE_RESUME, 0, 0, 0, 0 }, |
| 230 | .expect = -EPERM, |
| 231 | }, |
| 232 | }; |
| 233 | |
| 234 | |
| 235 | void ns_bl1u_fwu_test_main(void) |
| 236 | { |
| 237 | NOTICE("NS_BL1U: ***** Starting NS_BL1U FWU test *****\n"); |
| 238 | |
| 239 | for (int i = 0 ; i < ARRAY_SIZE(tests); ++i) { |
| 240 | u_register_t result; |
| 241 | |
| 242 | INFO("NS_BL1U: %s\n", tests[i].description); |
| 243 | |
| 244 | smc_ret_values smc_ret; |
| 245 | smc_ret = tftf_smc(&tests[i].args); |
| 246 | result = smc_ret.ret0; |
| 247 | |
| 248 | if (result != tests[i].expect) { |
| 249 | ERROR("NS_BL1U: Unexpected SMC return value 0x%lX, " |
| 250 | "expected 0x%lX\n", result, tests[i].expect); |
| 251 | panic(); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | NOTICE("NS_BL1U: ***** All FWU test passed *****\n\n"); |
| 256 | } |