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