blob: 9b801068bc0e6e0c052cd91244c93f2123250879 [file] [log] [blame]
David Brazdil7a462ec2019-08-15 12:27:47 +01001/*
2 * Copyright 2019 The Hafnium Authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * https://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "hf/manifest.h"
18
19#include "hf/addr.h"
20#include "hf/check.h"
Andrew Scullae9962e2019-10-03 16:51:16 +010021#include "hf/dlog.h"
David Brazdil7a462ec2019-08-15 12:27:47 +010022#include "hf/fdt.h"
23#include "hf/static_assert.h"
24#include "hf/std.h"
25
26#define TRY(expr) \
27 do { \
28 enum manifest_return_code ret_code = (expr); \
29 if (ret_code != MANIFEST_SUCCESS) { \
30 return ret_code; \
31 } \
32 } while (0)
33
David Brazdilb856be62020-03-25 10:14:55 +000034#define VM_ID_MAX (HF_VM_ID_OFFSET + MAX_VMS - 1)
35#define VM_ID_MAX_DIGITS (5)
36#define VM_NAME_EXTRA_CHARS (3) /* "vm" + number + '\0' */
37#define VM_NAME_MAX_SIZE (VM_ID_MAX_DIGITS + VM_NAME_EXTRA_CHARS)
38static_assert(VM_NAME_MAX_SIZE <= STRING_MAX_SIZE,
39 "VM name does not fit into a struct string.");
40static_assert(VM_ID_MAX <= 99999, "Insufficient VM_NAME_BUF_SIZE");
41static_assert(HF_TEE_VM_ID > VM_ID_MAX,
Andrew Walbran9daa57e2019-09-27 13:33:20 +010042 "TrustZone VM ID clashes with normal VM range.");
David Brazdil7a462ec2019-08-15 12:27:47 +010043
David Brazdilb856be62020-03-25 10:14:55 +000044static inline size_t count_digits(spci_vm_id_t vm_id)
45{
46 size_t digits = 0;
47
48 do {
49 digits++;
50 vm_id /= 10;
51 } while (vm_id);
52 return digits;
53}
54
David Brazdil7a462ec2019-08-15 12:27:47 +010055/**
56 * Generates a string with the two letters "vm" followed by an integer.
57 * Assumes `buf` is of size VM_NAME_BUF_SIZE.
58 */
David Brazdilb856be62020-03-25 10:14:55 +000059static void generate_vm_node_name(struct string *str, spci_vm_id_t vm_id)
David Brazdil7a462ec2019-08-15 12:27:47 +010060{
61 static const char *digits = "0123456789";
David Brazdilb856be62020-03-25 10:14:55 +000062 size_t vm_id_digits = count_digits(vm_id);
63 char *base = str->data;
64 char *ptr = base + (VM_NAME_EXTRA_CHARS + vm_id_digits);
David Brazdil7a462ec2019-08-15 12:27:47 +010065
David Brazdilb856be62020-03-25 10:14:55 +000066 CHECK(vm_id_digits <= VM_ID_MAX_DIGITS);
David Brazdil7a462ec2019-08-15 12:27:47 +010067 *(--ptr) = '\0';
68 do {
69 *(--ptr) = digits[vm_id % 10];
70 vm_id /= 10;
71 } while (vm_id);
72 *(--ptr) = 'm';
73 *(--ptr) = 'v';
David Brazdilb856be62020-03-25 10:14:55 +000074 CHECK(ptr == base);
David Brazdil7a462ec2019-08-15 12:27:47 +010075}
76
Andrew Scullae9962e2019-10-03 16:51:16 +010077/**
Andrew Scullb2c3a242019-11-04 13:52:36 +000078 * Read a boolean property: true if present; false if not. If present, the value
79 * of the property must be empty else it is considered malformed.
Andrew Scullae9962e2019-10-03 16:51:16 +010080 */
Andrew Scullb2c3a242019-11-04 13:52:36 +000081static enum manifest_return_code read_bool(const struct fdt_node *node,
82 const char *property, bool *out)
Andrew Scullae9962e2019-10-03 16:51:16 +010083{
David Brazdilb856be62020-03-25 10:14:55 +000084 struct memiter data;
85 bool present = fdt_read_property(node, property, &data);
Andrew Scullae9962e2019-10-03 16:51:16 +010086
David Brazdilb856be62020-03-25 10:14:55 +000087 if (present && memiter_size(&data) != 0) {
Andrew Scullb2c3a242019-11-04 13:52:36 +000088 return MANIFEST_ERROR_MALFORMED_BOOLEAN;
89 }
90
91 *out = present;
92 return MANIFEST_SUCCESS;
Andrew Scullae9962e2019-10-03 16:51:16 +010093}
94
Andrew Scull72b43c02019-09-18 13:53:45 +010095static enum manifest_return_code read_string(const struct fdt_node *node,
David Brazdil136f2942019-09-23 14:11:03 +010096 const char *property,
97 struct string *out)
Andrew Scull72b43c02019-09-18 13:53:45 +010098{
David Brazdilb856be62020-03-25 10:14:55 +000099 struct memiter data;
Andrew Scull72b43c02019-09-18 13:53:45 +0100100
David Brazdilb856be62020-03-25 10:14:55 +0000101 if (!fdt_read_property(node, property, &data)) {
Andrew Scull72b43c02019-09-18 13:53:45 +0100102 return MANIFEST_ERROR_PROPERTY_NOT_FOUND;
103 }
104
David Brazdilb856be62020-03-25 10:14:55 +0000105 switch (string_init(out, &data)) {
David Brazdil136f2942019-09-23 14:11:03 +0100106 case STRING_SUCCESS:
107 return MANIFEST_SUCCESS;
108 case STRING_ERROR_INVALID_INPUT:
109 return MANIFEST_ERROR_MALFORMED_STRING;
110 case STRING_ERROR_TOO_LONG:
111 return MANIFEST_ERROR_STRING_TOO_LONG;
112 }
Andrew Scull72b43c02019-09-18 13:53:45 +0100113}
114
115static enum manifest_return_code read_optional_string(
David Brazdil136f2942019-09-23 14:11:03 +0100116 const struct fdt_node *node, const char *property, struct string *out)
Andrew Scull72b43c02019-09-18 13:53:45 +0100117{
David Brazdil136f2942019-09-23 14:11:03 +0100118 enum manifest_return_code ret;
Andrew Scull72b43c02019-09-18 13:53:45 +0100119
David Brazdil136f2942019-09-23 14:11:03 +0100120 ret = read_string(node, property, out);
121 if (ret == MANIFEST_ERROR_PROPERTY_NOT_FOUND) {
122 string_init_empty(out);
123 ret = MANIFEST_SUCCESS;
Andrew Scull72b43c02019-09-18 13:53:45 +0100124 }
David Brazdil136f2942019-09-23 14:11:03 +0100125 return ret;
Andrew Scull72b43c02019-09-18 13:53:45 +0100126}
127
David Brazdil7a462ec2019-08-15 12:27:47 +0100128static enum manifest_return_code read_uint64(const struct fdt_node *node,
129 const char *property,
130 uint64_t *out)
131{
David Brazdilb856be62020-03-25 10:14:55 +0000132 struct memiter data;
David Brazdil7a462ec2019-08-15 12:27:47 +0100133
David Brazdilb856be62020-03-25 10:14:55 +0000134 if (!fdt_read_property(node, property, &data)) {
David Brazdil7a462ec2019-08-15 12:27:47 +0100135 return MANIFEST_ERROR_PROPERTY_NOT_FOUND;
136 }
137
David Brazdilb856be62020-03-25 10:14:55 +0000138 if (!fdt_parse_number(&data, memiter_size(&data), out)) {
David Brazdil7a462ec2019-08-15 12:27:47 +0100139 return MANIFEST_ERROR_MALFORMED_INTEGER;
140 }
141
142 return MANIFEST_SUCCESS;
143}
144
David Brazdil080ee312020-02-25 15:30:30 -0800145static enum manifest_return_code read_optional_uint64(
146 const struct fdt_node *node, const char *property,
147 uint64_t default_value, uint64_t *out)
148{
149 enum manifest_return_code ret;
150
151 ret = read_uint64(node, property, out);
152 if (ret == MANIFEST_ERROR_PROPERTY_NOT_FOUND) {
153 *out = default_value;
154 return MANIFEST_SUCCESS;
155 }
156 return ret;
157}
158
David Brazdil7a462ec2019-08-15 12:27:47 +0100159static enum manifest_return_code read_uint16(const struct fdt_node *node,
160 const char *property,
161 uint16_t *out)
162{
163 uint64_t value;
164
165 TRY(read_uint64(node, property, &value));
166
167 if (value > UINT16_MAX) {
168 return MANIFEST_ERROR_INTEGER_OVERFLOW;
169 }
170
171 *out = (uint16_t)value;
172 return MANIFEST_SUCCESS;
173}
174
Andrew Scullae9962e2019-10-03 16:51:16 +0100175struct uint32list_iter {
176 struct memiter mem_it;
177};
178
179static enum manifest_return_code read_optional_uint32list(
180 const struct fdt_node *node, const char *property,
181 struct uint32list_iter *out)
182{
David Brazdilb856be62020-03-25 10:14:55 +0000183 struct memiter data;
Andrew Scullae9962e2019-10-03 16:51:16 +0100184
David Brazdilb856be62020-03-25 10:14:55 +0000185 if (!fdt_read_property(node, property, &data)) {
Andrew Scullae9962e2019-10-03 16:51:16 +0100186 memiter_init(&out->mem_it, NULL, 0);
187 return MANIFEST_SUCCESS;
188 }
189
David Brazdilb856be62020-03-25 10:14:55 +0000190 if ((memiter_size(&data) % sizeof(uint32_t)) != 0) {
Andrew Scullae9962e2019-10-03 16:51:16 +0100191 return MANIFEST_ERROR_MALFORMED_INTEGER_LIST;
192 }
193
David Brazdilb856be62020-03-25 10:14:55 +0000194 out->mem_it = data;
Andrew Scullae9962e2019-10-03 16:51:16 +0100195 return MANIFEST_SUCCESS;
196}
197
Andrew Scullae9962e2019-10-03 16:51:16 +0100198static bool uint32list_has_next(const struct uint32list_iter *list)
199{
200 return memiter_size(&list->mem_it) > 0;
201}
202
David Brazdil5ea99462020-03-25 13:01:47 +0000203static enum manifest_return_code uint32list_get_next(
204 struct uint32list_iter *list, uint32_t *out)
Andrew Scullae9962e2019-10-03 16:51:16 +0100205{
Andrew Scullae9962e2019-10-03 16:51:16 +0100206 uint64_t num;
207
208 CHECK(uint32list_has_next(list));
David Brazdilb856be62020-03-25 10:14:55 +0000209 if (!fdt_parse_number(&list->mem_it, sizeof(uint32_t), &num)) {
Andrew Scullae9962e2019-10-03 16:51:16 +0100210 return MANIFEST_ERROR_MALFORMED_INTEGER;
211 }
212
David Brazdil5ea99462020-03-25 13:01:47 +0000213 *out = (uint32_t)num;
214 return MANIFEST_SUCCESS;
Andrew Scullae9962e2019-10-03 16:51:16 +0100215}
216
David Brazdilb856be62020-03-25 10:14:55 +0000217static enum manifest_return_code parse_vm(const struct fdt_node *node,
David Brazdil7a462ec2019-08-15 12:27:47 +0100218 struct manifest_vm *vm,
219 spci_vm_id_t vm_id)
220{
Andrew Scullae9962e2019-10-03 16:51:16 +0100221 struct uint32list_iter smcs;
David Brazdil5ea99462020-03-25 13:01:47 +0000222 size_t idx;
Andrew Scullae9962e2019-10-03 16:51:16 +0100223
David Brazdil136f2942019-09-23 14:11:03 +0100224 TRY(read_string(node, "debug_name", &vm->debug_name));
225 TRY(read_optional_string(node, "kernel_filename",
226 &vm->kernel_filename));
Andrew Scullae9962e2019-10-03 16:51:16 +0100227
228 TRY(read_optional_uint32list(node, "smc_whitelist", &smcs));
229 while (uint32list_has_next(&smcs) &&
230 vm->smc_whitelist.smc_count < MAX_SMCS) {
David Brazdil5ea99462020-03-25 13:01:47 +0000231 idx = vm->smc_whitelist.smc_count++;
232 TRY(uint32list_get_next(&smcs, &vm->smc_whitelist.smcs[idx]));
Andrew Scullae9962e2019-10-03 16:51:16 +0100233 }
234
235 if (uint32list_has_next(&smcs)) {
Andrew Walbran17eebf92020-02-05 16:35:49 +0000236 dlog_warning("%s SMC whitelist too long.\n", vm->debug_name);
Andrew Scullae9962e2019-10-03 16:51:16 +0100237 }
238
Andrew Scullb2c3a242019-11-04 13:52:36 +0000239 TRY(read_bool(node, "smc_whitelist_permissive",
240 &vm->smc_whitelist.permissive));
Andrew Scullae9962e2019-10-03 16:51:16 +0100241
David Brazdile6f83222019-09-23 14:47:37 +0100242 if (vm_id == HF_PRIMARY_VM_ID) {
243 TRY(read_optional_string(node, "ramdisk_filename",
244 &vm->primary.ramdisk_filename));
David Brazdil080ee312020-02-25 15:30:30 -0800245 TRY(read_optional_uint64(node, "boot_address",
246 MANIFEST_INVALID_ADDRESS,
247 &vm->primary.boot_address));
David Brazdile6f83222019-09-23 14:47:37 +0100248 } else {
David Brazdil7a462ec2019-08-15 12:27:47 +0100249 TRY(read_uint64(node, "mem_size", &vm->secondary.mem_size));
250 TRY(read_uint16(node, "vcpu_count", &vm->secondary.vcpu_count));
251 }
252 return MANIFEST_SUCCESS;
253}
254
255/**
256 * Parse manifest from FDT.
257 */
258enum manifest_return_code manifest_init(struct manifest *manifest,
David Brazdila2358d42020-01-27 18:51:38 +0000259 struct memiter *manifest_fdt)
David Brazdil7a462ec2019-08-15 12:27:47 +0100260{
David Brazdilb856be62020-03-25 10:14:55 +0000261 struct string vm_name;
262 struct fdt fdt;
David Brazdil7a462ec2019-08-15 12:27:47 +0100263 struct fdt_node hyp_node;
264 size_t i = 0;
265 bool found_primary_vm = false;
266
267 memset_s(manifest, sizeof(*manifest), 0, sizeof(*manifest));
268
David Brazdilb856be62020-03-25 10:14:55 +0000269 if (!fdt_init_from_memiter(&fdt, manifest_fdt)) {
270 return MANIFEST_ERROR_FILE_SIZE; /* TODO */
David Brazdila2358d42020-01-27 18:51:38 +0000271 }
272
David Brazdil7a462ec2019-08-15 12:27:47 +0100273 /* Find hypervisor node. */
David Brazdilb856be62020-03-25 10:14:55 +0000274 if (!fdt_find_node(&fdt, "/hypervisor", &hyp_node)) {
David Brazdil7a462ec2019-08-15 12:27:47 +0100275 return MANIFEST_ERROR_NO_HYPERVISOR_FDT_NODE;
276 }
277
David Brazdil74e9c3b2019-08-28 11:09:08 +0100278 /* Check "compatible" property. */
David Brazdilf4925382020-03-25 13:33:51 +0000279 if (!fdt_is_compatible(&hyp_node, "hafnium,hafnium")) {
David Brazdil74e9c3b2019-08-28 11:09:08 +0100280 return MANIFEST_ERROR_NOT_COMPATIBLE;
281 }
282
Andrew Walbran41a49d82020-01-10 17:46:38 +0000283 TRY(read_bool(&hyp_node, "spci_tee", &manifest->spci_tee_enabled));
284
David Brazdil7a462ec2019-08-15 12:27:47 +0100285 /* Iterate over reserved VM IDs and check no such nodes exist. */
286 for (i = 0; i < HF_VM_ID_OFFSET; i++) {
287 spci_vm_id_t vm_id = (spci_vm_id_t)i;
288 struct fdt_node vm_node = hyp_node;
David Brazdil7a462ec2019-08-15 12:27:47 +0100289
David Brazdilb856be62020-03-25 10:14:55 +0000290 generate_vm_node_name(&vm_name, vm_id);
291 if (fdt_find_child(&vm_node, &vm_name)) {
David Brazdil7a462ec2019-08-15 12:27:47 +0100292 return MANIFEST_ERROR_RESERVED_VM_ID;
293 }
294 }
295
296 /* Iterate over VM nodes until we find one that does not exist. */
297 for (i = 0; i <= MAX_VMS; ++i) {
298 spci_vm_id_t vm_id = HF_VM_ID_OFFSET + i;
299 struct fdt_node vm_node = hyp_node;
David Brazdil7a462ec2019-08-15 12:27:47 +0100300
David Brazdilb856be62020-03-25 10:14:55 +0000301 generate_vm_node_name(&vm_name, vm_id);
302 if (!fdt_find_child(&vm_node, &vm_name)) {
David Brazdil7a462ec2019-08-15 12:27:47 +0100303 break;
304 }
305
306 if (i == MAX_VMS) {
307 return MANIFEST_ERROR_TOO_MANY_VMS;
308 }
309
310 if (vm_id == HF_PRIMARY_VM_ID) {
311 CHECK(found_primary_vm == false); /* sanity check */
312 found_primary_vm = true;
313 }
314
David Brazdil0251b942019-09-10 15:59:50 +0100315 manifest->vm_count = i + 1;
David Brazdil7a462ec2019-08-15 12:27:47 +0100316 TRY(parse_vm(&vm_node, &manifest->vm[i], vm_id));
317 }
318
319 if (!found_primary_vm) {
320 return MANIFEST_ERROR_NO_PRIMARY_VM;
321 }
322
323 return MANIFEST_SUCCESS;
324}
325
326const char *manifest_strerror(enum manifest_return_code ret_code)
327{
328 switch (ret_code) {
329 case MANIFEST_SUCCESS:
330 return "Success";
David Brazdila2358d42020-01-27 18:51:38 +0000331 case MANIFEST_ERROR_FILE_SIZE:
332 return "Total size in header does not match file size";
333 case MANIFEST_ERROR_NO_ROOT_NODE:
334 return "Could not find root node in manifest";
David Brazdil7a462ec2019-08-15 12:27:47 +0100335 case MANIFEST_ERROR_NO_HYPERVISOR_FDT_NODE:
336 return "Could not find \"hypervisor\" node in manifest";
David Brazdil74e9c3b2019-08-28 11:09:08 +0100337 case MANIFEST_ERROR_NOT_COMPATIBLE:
338 return "Hypervisor manifest entry not compatible with Hafnium";
David Brazdil7a462ec2019-08-15 12:27:47 +0100339 case MANIFEST_ERROR_RESERVED_VM_ID:
340 return "Manifest defines a VM with a reserved ID";
341 case MANIFEST_ERROR_NO_PRIMARY_VM:
342 return "Manifest does not contain a primary VM entry";
343 case MANIFEST_ERROR_TOO_MANY_VMS:
344 return "Manifest specifies more VMs than Hafnium has "
345 "statically allocated space for";
346 case MANIFEST_ERROR_PROPERTY_NOT_FOUND:
347 return "Property not found";
348 case MANIFEST_ERROR_MALFORMED_STRING:
349 return "Malformed string property";
David Brazdil0dbb41f2019-09-09 18:03:35 +0100350 case MANIFEST_ERROR_STRING_TOO_LONG:
351 return "String too long";
David Brazdil7a462ec2019-08-15 12:27:47 +0100352 case MANIFEST_ERROR_MALFORMED_INTEGER:
353 return "Malformed integer property";
354 case MANIFEST_ERROR_INTEGER_OVERFLOW:
355 return "Integer overflow";
Andrew Scullae9962e2019-10-03 16:51:16 +0100356 case MANIFEST_ERROR_MALFORMED_INTEGER_LIST:
357 return "Malformed integer list property";
Andrew Scullb2c3a242019-11-04 13:52:36 +0000358 case MANIFEST_ERROR_MALFORMED_BOOLEAN:
359 return "Malformed boolean property";
David Brazdil7a462ec2019-08-15 12:27:47 +0100360 }
361
362 panic("Unexpected manifest return code.");
363}