blob: bbff8a5de55a2df28497983b254163b77fc0299d [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
34#define VM_NAME_BUF_SIZE (2 + 5 + 1) /* "vm" + number + null terminator */
35static_assert(MAX_VMS <= 99999, "Insufficient VM_NAME_BUF_SIZE");
Andrew Walbran9daa57e2019-09-27 13:33:20 +010036static_assert(HF_TEE_VM_ID > MAX_VMS,
37 "TrustZone VM ID clashes with normal VM range.");
David Brazdil7a462ec2019-08-15 12:27:47 +010038
39/**
40 * Generates a string with the two letters "vm" followed by an integer.
41 * Assumes `buf` is of size VM_NAME_BUF_SIZE.
42 */
43static const char *generate_vm_node_name(char *buf, spci_vm_id_t vm_id)
44{
45 static const char *digits = "0123456789";
46 char *ptr = buf + VM_NAME_BUF_SIZE;
47
48 *(--ptr) = '\0';
49 do {
50 *(--ptr) = digits[vm_id % 10];
51 vm_id /= 10;
52 } while (vm_id);
53 *(--ptr) = 'm';
54 *(--ptr) = 'v';
55
56 return ptr;
57}
58
Andrew Scullae9962e2019-10-03 16:51:16 +010059/**
Andrew Scullb2c3a242019-11-04 13:52:36 +000060 * Read a boolean property: true if present; false if not. If present, the value
61 * of the property must be empty else it is considered malformed.
Andrew Scullae9962e2019-10-03 16:51:16 +010062 */
Andrew Scullb2c3a242019-11-04 13:52:36 +000063static enum manifest_return_code read_bool(const struct fdt_node *node,
64 const char *property, bool *out)
Andrew Scullae9962e2019-10-03 16:51:16 +010065{
66 const char *data;
67 uint32_t size;
Andrew Scullb2c3a242019-11-04 13:52:36 +000068 bool present = fdt_read_property(node, property, &data, &size);
Andrew Scullae9962e2019-10-03 16:51:16 +010069
Andrew Scullb2c3a242019-11-04 13:52:36 +000070 if (present && size != 0) {
71 return MANIFEST_ERROR_MALFORMED_BOOLEAN;
72 }
73
74 *out = present;
75 return MANIFEST_SUCCESS;
Andrew Scullae9962e2019-10-03 16:51:16 +010076}
77
Andrew Scull72b43c02019-09-18 13:53:45 +010078static enum manifest_return_code read_string(const struct fdt_node *node,
David Brazdil136f2942019-09-23 14:11:03 +010079 const char *property,
80 struct string *out)
Andrew Scull72b43c02019-09-18 13:53:45 +010081{
82 const char *data;
83 uint32_t size;
84
85 if (!fdt_read_property(node, property, &data, &size)) {
86 return MANIFEST_ERROR_PROPERTY_NOT_FOUND;
87 }
88
David Brazdil136f2942019-09-23 14:11:03 +010089 switch (string_init(out, data, size)) {
90 case STRING_SUCCESS:
91 return MANIFEST_SUCCESS;
92 case STRING_ERROR_INVALID_INPUT:
93 return MANIFEST_ERROR_MALFORMED_STRING;
94 case STRING_ERROR_TOO_LONG:
95 return MANIFEST_ERROR_STRING_TOO_LONG;
96 }
Andrew Scull72b43c02019-09-18 13:53:45 +010097}
98
99static enum manifest_return_code read_optional_string(
David Brazdil136f2942019-09-23 14:11:03 +0100100 const struct fdt_node *node, const char *property, struct string *out)
Andrew Scull72b43c02019-09-18 13:53:45 +0100101{
David Brazdil136f2942019-09-23 14:11:03 +0100102 enum manifest_return_code ret;
Andrew Scull72b43c02019-09-18 13:53:45 +0100103
David Brazdil136f2942019-09-23 14:11:03 +0100104 ret = read_string(node, property, out);
105 if (ret == MANIFEST_ERROR_PROPERTY_NOT_FOUND) {
106 string_init_empty(out);
107 ret = MANIFEST_SUCCESS;
Andrew Scull72b43c02019-09-18 13:53:45 +0100108 }
David Brazdil136f2942019-09-23 14:11:03 +0100109 return ret;
Andrew Scull72b43c02019-09-18 13:53:45 +0100110}
111
David Brazdil7a462ec2019-08-15 12:27:47 +0100112static enum manifest_return_code read_uint64(const struct fdt_node *node,
113 const char *property,
114 uint64_t *out)
115{
116 const char *data;
117 uint32_t size;
118
119 if (!fdt_read_property(node, property, &data, &size)) {
120 return MANIFEST_ERROR_PROPERTY_NOT_FOUND;
121 }
122
123 if (!fdt_parse_number(data, size, out)) {
124 return MANIFEST_ERROR_MALFORMED_INTEGER;
125 }
126
127 return MANIFEST_SUCCESS;
128}
129
David Brazdil080ee312020-02-25 15:30:30 -0800130static enum manifest_return_code read_optional_uint64(
131 const struct fdt_node *node, const char *property,
132 uint64_t default_value, uint64_t *out)
133{
134 enum manifest_return_code ret;
135
136 ret = read_uint64(node, property, out);
137 if (ret == MANIFEST_ERROR_PROPERTY_NOT_FOUND) {
138 *out = default_value;
139 return MANIFEST_SUCCESS;
140 }
141 return ret;
142}
143
David Brazdil7a462ec2019-08-15 12:27:47 +0100144static enum manifest_return_code read_uint16(const struct fdt_node *node,
145 const char *property,
146 uint16_t *out)
147{
148 uint64_t value;
149
150 TRY(read_uint64(node, property, &value));
151
152 if (value > UINT16_MAX) {
153 return MANIFEST_ERROR_INTEGER_OVERFLOW;
154 }
155
156 *out = (uint16_t)value;
157 return MANIFEST_SUCCESS;
158}
159
Andrew Scullae9962e2019-10-03 16:51:16 +0100160struct uint32list_iter {
161 struct memiter mem_it;
162};
163
164static enum manifest_return_code read_optional_uint32list(
165 const struct fdt_node *node, const char *property,
166 struct uint32list_iter *out)
167{
168 const char *data;
169 uint32_t size;
170
171 if (!fdt_read_property(node, property, &data, &size)) {
172 memiter_init(&out->mem_it, NULL, 0);
173 return MANIFEST_SUCCESS;
174 }
175
176 if ((size % sizeof(uint32_t)) != 0) {
177 return MANIFEST_ERROR_MALFORMED_INTEGER_LIST;
178 }
179
180 memiter_init(&out->mem_it, data, size);
181 return MANIFEST_SUCCESS;
182}
183
David Brazdil74e9c3b2019-08-28 11:09:08 +0100184/**
185 * Represents the value of property whose type is a list of strings. These are
186 * encoded as one contiguous byte buffer with NULL-separated entries.
187 */
188struct stringlist_iter {
189 struct memiter mem_it;
190};
191
192static enum manifest_return_code read_stringlist(const struct fdt_node *node,
193 const char *property,
194 struct stringlist_iter *out)
195{
196 const char *data;
197 uint32_t size;
198
199 if (!fdt_read_property(node, property, &data, &size)) {
200 return MANIFEST_ERROR_PROPERTY_NOT_FOUND;
201 }
202
203 /*
204 * Require that the value ends with a NULL terminator. Other NULL
205 * characters separate the string list entries.
206 */
207 if (data[size - 1] != '\0') {
208 return MANIFEST_ERROR_MALFORMED_STRING_LIST;
209 }
210
211 memiter_init(&out->mem_it, data, size - 1);
212 return MANIFEST_SUCCESS;
213}
214
Andrew Scullae9962e2019-10-03 16:51:16 +0100215static bool uint32list_has_next(const struct uint32list_iter *list)
216{
217 return memiter_size(&list->mem_it) > 0;
218}
219
220static uint32_t uint32list_get_next(struct uint32list_iter *list)
221{
222 const char *mem_base = memiter_base(&list->mem_it);
223 uint64_t num;
224
225 CHECK(uint32list_has_next(list));
226
227 if (!fdt_parse_number(mem_base, sizeof(uint32_t), &num)) {
228 return MANIFEST_ERROR_MALFORMED_INTEGER;
229 }
230
231 memiter_advance(&list->mem_it, sizeof(uint32_t));
232 return num;
233}
234
David Brazdil74e9c3b2019-08-28 11:09:08 +0100235static bool stringlist_has_next(const struct stringlist_iter *list)
236{
237 return memiter_size(&list->mem_it) > 0;
238}
239
240static void stringlist_get_next(struct stringlist_iter *list,
241 struct memiter *out)
242{
243 const char *mem_base = memiter_base(&list->mem_it);
244 size_t mem_size = memiter_size(&list->mem_it);
245 const char *null_term;
246
247 CHECK(stringlist_has_next(list));
248
249 null_term = memchr(mem_base, '\0', mem_size);
250 if (null_term == NULL) {
251 /*
252 * NULL terminator not found, this is the last entry.
253 * Set entry memiter to the entire byte range and advance list
254 * memiter to the end of the byte range.
255 */
256 memiter_init(out, mem_base, mem_size);
257 memiter_advance(&list->mem_it, mem_size);
258 } else {
259 /*
260 * Found NULL terminator. Set entry memiter to byte range
261 * [base, null) and move list memiter past the terminator.
262 */
263 size_t entry_size = null_term - mem_base;
264
265 memiter_init(out, mem_base, entry_size);
266 memiter_advance(&list->mem_it, entry_size + 1);
267 }
268}
269
270static bool stringlist_contains(const struct stringlist_iter *list,
271 const char *str)
272{
273 struct stringlist_iter it = *list;
274 struct memiter entry;
275
276 while (stringlist_has_next(&it)) {
277 stringlist_get_next(&it, &entry);
278 if (memiter_iseq(&entry, str)) {
279 return true;
280 }
281 }
282 return false;
283}
284
David Brazdil7a462ec2019-08-15 12:27:47 +0100285static enum manifest_return_code parse_vm(struct fdt_node *node,
286 struct manifest_vm *vm,
287 spci_vm_id_t vm_id)
288{
Andrew Scullae9962e2019-10-03 16:51:16 +0100289 struct uint32list_iter smcs;
290
David Brazdil136f2942019-09-23 14:11:03 +0100291 TRY(read_string(node, "debug_name", &vm->debug_name));
292 TRY(read_optional_string(node, "kernel_filename",
293 &vm->kernel_filename));
Andrew Scullae9962e2019-10-03 16:51:16 +0100294
295 TRY(read_optional_uint32list(node, "smc_whitelist", &smcs));
296 while (uint32list_has_next(&smcs) &&
297 vm->smc_whitelist.smc_count < MAX_SMCS) {
298 vm->smc_whitelist.smcs[vm->smc_whitelist.smc_count++] =
299 uint32list_get_next(&smcs);
300 }
301
302 if (uint32list_has_next(&smcs)) {
Andrew Walbran17eebf92020-02-05 16:35:49 +0000303 dlog_warning("%s SMC whitelist too long.\n", vm->debug_name);
Andrew Scullae9962e2019-10-03 16:51:16 +0100304 }
305
Andrew Scullb2c3a242019-11-04 13:52:36 +0000306 TRY(read_bool(node, "smc_whitelist_permissive",
307 &vm->smc_whitelist.permissive));
Andrew Scullae9962e2019-10-03 16:51:16 +0100308
David Brazdile6f83222019-09-23 14:47:37 +0100309 if (vm_id == HF_PRIMARY_VM_ID) {
310 TRY(read_optional_string(node, "ramdisk_filename",
311 &vm->primary.ramdisk_filename));
David Brazdil080ee312020-02-25 15:30:30 -0800312 TRY(read_optional_uint64(node, "boot_address",
313 MANIFEST_INVALID_ADDRESS,
314 &vm->primary.boot_address));
David Brazdile6f83222019-09-23 14:47:37 +0100315 } else {
David Brazdil7a462ec2019-08-15 12:27:47 +0100316 TRY(read_uint64(node, "mem_size", &vm->secondary.mem_size));
317 TRY(read_uint16(node, "vcpu_count", &vm->secondary.vcpu_count));
318 }
319 return MANIFEST_SUCCESS;
320}
321
322/**
323 * Parse manifest from FDT.
324 */
325enum manifest_return_code manifest_init(struct manifest *manifest,
David Brazdila2358d42020-01-27 18:51:38 +0000326 struct memiter *manifest_fdt)
David Brazdil7a462ec2019-08-15 12:27:47 +0100327{
328 char vm_name_buf[VM_NAME_BUF_SIZE];
David Brazdila2358d42020-01-27 18:51:38 +0000329 const struct fdt_header *fdt;
David Brazdil7a462ec2019-08-15 12:27:47 +0100330 struct fdt_node hyp_node;
David Brazdil74e9c3b2019-08-28 11:09:08 +0100331 struct stringlist_iter compatible_list;
David Brazdil7a462ec2019-08-15 12:27:47 +0100332 size_t i = 0;
333 bool found_primary_vm = false;
334
335 memset_s(manifest, sizeof(*manifest), 0, sizeof(*manifest));
336
David Brazdila2358d42020-01-27 18:51:38 +0000337 fdt = (const struct fdt_header *)memiter_base(manifest_fdt);
338 if (memiter_size(manifest_fdt) != fdt_total_size(fdt)) {
339 return MANIFEST_ERROR_FILE_SIZE;
340 }
341
David Brazdil7a462ec2019-08-15 12:27:47 +0100342 /* Find hypervisor node. */
David Brazdila2358d42020-01-27 18:51:38 +0000343 if (!fdt_root_node(&hyp_node, fdt)) {
344 return MANIFEST_ERROR_NO_ROOT_NODE;
345 }
346 if (!fdt_find_child(&hyp_node, "")) {
347 return MANIFEST_ERROR_NO_ROOT_NODE;
348 }
David Brazdil7a462ec2019-08-15 12:27:47 +0100349 if (!fdt_find_child(&hyp_node, "hypervisor")) {
350 return MANIFEST_ERROR_NO_HYPERVISOR_FDT_NODE;
351 }
352
David Brazdil74e9c3b2019-08-28 11:09:08 +0100353 /* Check "compatible" property. */
354 TRY(read_stringlist(&hyp_node, "compatible", &compatible_list));
355 if (!stringlist_contains(&compatible_list, "hafnium,hafnium")) {
356 return MANIFEST_ERROR_NOT_COMPATIBLE;
357 }
358
David Brazdil7a462ec2019-08-15 12:27:47 +0100359 /* Iterate over reserved VM IDs and check no such nodes exist. */
360 for (i = 0; i < HF_VM_ID_OFFSET; i++) {
361 spci_vm_id_t vm_id = (spci_vm_id_t)i;
362 struct fdt_node vm_node = hyp_node;
363 const char *vm_name = generate_vm_node_name(vm_name_buf, vm_id);
364
365 if (fdt_find_child(&vm_node, vm_name)) {
366 return MANIFEST_ERROR_RESERVED_VM_ID;
367 }
368 }
369
370 /* Iterate over VM nodes until we find one that does not exist. */
371 for (i = 0; i <= MAX_VMS; ++i) {
372 spci_vm_id_t vm_id = HF_VM_ID_OFFSET + i;
373 struct fdt_node vm_node = hyp_node;
374 const char *vm_name = generate_vm_node_name(vm_name_buf, vm_id);
375
376 if (!fdt_find_child(&vm_node, vm_name)) {
377 break;
378 }
379
380 if (i == MAX_VMS) {
381 return MANIFEST_ERROR_TOO_MANY_VMS;
382 }
383
384 if (vm_id == HF_PRIMARY_VM_ID) {
385 CHECK(found_primary_vm == false); /* sanity check */
386 found_primary_vm = true;
387 }
388
David Brazdil0251b942019-09-10 15:59:50 +0100389 manifest->vm_count = i + 1;
David Brazdil7a462ec2019-08-15 12:27:47 +0100390 TRY(parse_vm(&vm_node, &manifest->vm[i], vm_id));
391 }
392
393 if (!found_primary_vm) {
394 return MANIFEST_ERROR_NO_PRIMARY_VM;
395 }
396
397 return MANIFEST_SUCCESS;
398}
399
400const char *manifest_strerror(enum manifest_return_code ret_code)
401{
402 switch (ret_code) {
403 case MANIFEST_SUCCESS:
404 return "Success";
David Brazdila2358d42020-01-27 18:51:38 +0000405 case MANIFEST_ERROR_FILE_SIZE:
406 return "Total size in header does not match file size";
407 case MANIFEST_ERROR_NO_ROOT_NODE:
408 return "Could not find root node in manifest";
David Brazdil7a462ec2019-08-15 12:27:47 +0100409 case MANIFEST_ERROR_NO_HYPERVISOR_FDT_NODE:
410 return "Could not find \"hypervisor\" node in manifest";
David Brazdil74e9c3b2019-08-28 11:09:08 +0100411 case MANIFEST_ERROR_NOT_COMPATIBLE:
412 return "Hypervisor manifest entry not compatible with Hafnium";
David Brazdil7a462ec2019-08-15 12:27:47 +0100413 case MANIFEST_ERROR_RESERVED_VM_ID:
414 return "Manifest defines a VM with a reserved ID";
415 case MANIFEST_ERROR_NO_PRIMARY_VM:
416 return "Manifest does not contain a primary VM entry";
417 case MANIFEST_ERROR_TOO_MANY_VMS:
418 return "Manifest specifies more VMs than Hafnium has "
419 "statically allocated space for";
420 case MANIFEST_ERROR_PROPERTY_NOT_FOUND:
421 return "Property not found";
422 case MANIFEST_ERROR_MALFORMED_STRING:
423 return "Malformed string property";
David Brazdil0dbb41f2019-09-09 18:03:35 +0100424 case MANIFEST_ERROR_STRING_TOO_LONG:
425 return "String too long";
David Brazdil74e9c3b2019-08-28 11:09:08 +0100426 case MANIFEST_ERROR_MALFORMED_STRING_LIST:
427 return "Malformed string list property";
David Brazdil7a462ec2019-08-15 12:27:47 +0100428 case MANIFEST_ERROR_MALFORMED_INTEGER:
429 return "Malformed integer property";
430 case MANIFEST_ERROR_INTEGER_OVERFLOW:
431 return "Integer overflow";
Andrew Scullae9962e2019-10-03 16:51:16 +0100432 case MANIFEST_ERROR_MALFORMED_INTEGER_LIST:
433 return "Malformed integer list property";
Andrew Scullb2c3a242019-11-04 13:52:36 +0000434 case MANIFEST_ERROR_MALFORMED_BOOLEAN:
435 return "Malformed boolean property";
David Brazdil7a462ec2019-08-15 12:27:47 +0100436 }
437
438 panic("Unexpected manifest return code.");
439}