blob: 6d8b4bad4dd527c74873919d507d1bff9518510f [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"
21#include "hf/fdt.h"
22#include "hf/static_assert.h"
23#include "hf/std.h"
24
25#define TRY(expr) \
26 do { \
27 enum manifest_return_code ret_code = (expr); \
28 if (ret_code != MANIFEST_SUCCESS) { \
29 return ret_code; \
30 } \
31 } while (0)
32
33#define VM_NAME_BUF_SIZE (2 + 5 + 1) /* "vm" + number + null terminator */
34static_assert(MAX_VMS <= 99999, "Insufficient VM_NAME_BUF_SIZE");
35
36/**
37 * Generates a string with the two letters "vm" followed by an integer.
38 * Assumes `buf` is of size VM_NAME_BUF_SIZE.
39 */
40static const char *generate_vm_node_name(char *buf, spci_vm_id_t vm_id)
41{
42 static const char *digits = "0123456789";
43 char *ptr = buf + VM_NAME_BUF_SIZE;
44
45 *(--ptr) = '\0';
46 do {
47 *(--ptr) = digits[vm_id % 10];
48 vm_id /= 10;
49 } while (vm_id);
50 *(--ptr) = 'm';
51 *(--ptr) = 'v';
52
53 return ptr;
54}
55
Andrew Scull72b43c02019-09-18 13:53:45 +010056static enum manifest_return_code read_string(const struct fdt_node *node,
David Brazdil136f2942019-09-23 14:11:03 +010057 const char *property,
58 struct string *out)
Andrew Scull72b43c02019-09-18 13:53:45 +010059{
60 const char *data;
61 uint32_t size;
62
63 if (!fdt_read_property(node, property, &data, &size)) {
64 return MANIFEST_ERROR_PROPERTY_NOT_FOUND;
65 }
66
David Brazdil136f2942019-09-23 14:11:03 +010067 switch (string_init(out, data, size)) {
68 case STRING_SUCCESS:
69 return MANIFEST_SUCCESS;
70 case STRING_ERROR_INVALID_INPUT:
71 return MANIFEST_ERROR_MALFORMED_STRING;
72 case STRING_ERROR_TOO_LONG:
73 return MANIFEST_ERROR_STRING_TOO_LONG;
74 }
Andrew Scull72b43c02019-09-18 13:53:45 +010075}
76
77static enum manifest_return_code read_optional_string(
David Brazdil136f2942019-09-23 14:11:03 +010078 const struct fdt_node *node, const char *property, struct string *out)
Andrew Scull72b43c02019-09-18 13:53:45 +010079{
David Brazdil136f2942019-09-23 14:11:03 +010080 enum manifest_return_code ret;
Andrew Scull72b43c02019-09-18 13:53:45 +010081
David Brazdil136f2942019-09-23 14:11:03 +010082 ret = read_string(node, property, out);
83 if (ret == MANIFEST_ERROR_PROPERTY_NOT_FOUND) {
84 string_init_empty(out);
85 ret = MANIFEST_SUCCESS;
Andrew Scull72b43c02019-09-18 13:53:45 +010086 }
David Brazdil136f2942019-09-23 14:11:03 +010087 return ret;
Andrew Scull72b43c02019-09-18 13:53:45 +010088}
89
David Brazdil7a462ec2019-08-15 12:27:47 +010090static enum manifest_return_code read_uint64(const struct fdt_node *node,
91 const char *property,
92 uint64_t *out)
93{
94 const char *data;
95 uint32_t size;
96
97 if (!fdt_read_property(node, property, &data, &size)) {
98 return MANIFEST_ERROR_PROPERTY_NOT_FOUND;
99 }
100
101 if (!fdt_parse_number(data, size, out)) {
102 return MANIFEST_ERROR_MALFORMED_INTEGER;
103 }
104
105 return MANIFEST_SUCCESS;
106}
107
108static enum manifest_return_code read_uint16(const struct fdt_node *node,
109 const char *property,
110 uint16_t *out)
111{
112 uint64_t value;
113
114 TRY(read_uint64(node, property, &value));
115
116 if (value > UINT16_MAX) {
117 return MANIFEST_ERROR_INTEGER_OVERFLOW;
118 }
119
120 *out = (uint16_t)value;
121 return MANIFEST_SUCCESS;
122}
123
David Brazdil74e9c3b2019-08-28 11:09:08 +0100124/**
125 * Represents the value of property whose type is a list of strings. These are
126 * encoded as one contiguous byte buffer with NULL-separated entries.
127 */
128struct stringlist_iter {
129 struct memiter mem_it;
130};
131
132static enum manifest_return_code read_stringlist(const struct fdt_node *node,
133 const char *property,
134 struct stringlist_iter *out)
135{
136 const char *data;
137 uint32_t size;
138
139 if (!fdt_read_property(node, property, &data, &size)) {
140 return MANIFEST_ERROR_PROPERTY_NOT_FOUND;
141 }
142
143 /*
144 * Require that the value ends with a NULL terminator. Other NULL
145 * characters separate the string list entries.
146 */
147 if (data[size - 1] != '\0') {
148 return MANIFEST_ERROR_MALFORMED_STRING_LIST;
149 }
150
151 memiter_init(&out->mem_it, data, size - 1);
152 return MANIFEST_SUCCESS;
153}
154
155static bool stringlist_has_next(const struct stringlist_iter *list)
156{
157 return memiter_size(&list->mem_it) > 0;
158}
159
160static void stringlist_get_next(struct stringlist_iter *list,
161 struct memiter *out)
162{
163 const char *mem_base = memiter_base(&list->mem_it);
164 size_t mem_size = memiter_size(&list->mem_it);
165 const char *null_term;
166
167 CHECK(stringlist_has_next(list));
168
169 null_term = memchr(mem_base, '\0', mem_size);
170 if (null_term == NULL) {
171 /*
172 * NULL terminator not found, this is the last entry.
173 * Set entry memiter to the entire byte range and advance list
174 * memiter to the end of the byte range.
175 */
176 memiter_init(out, mem_base, mem_size);
177 memiter_advance(&list->mem_it, mem_size);
178 } else {
179 /*
180 * Found NULL terminator. Set entry memiter to byte range
181 * [base, null) and move list memiter past the terminator.
182 */
183 size_t entry_size = null_term - mem_base;
184
185 memiter_init(out, mem_base, entry_size);
186 memiter_advance(&list->mem_it, entry_size + 1);
187 }
188}
189
190static bool stringlist_contains(const struct stringlist_iter *list,
191 const char *str)
192{
193 struct stringlist_iter it = *list;
194 struct memiter entry;
195
196 while (stringlist_has_next(&it)) {
197 stringlist_get_next(&it, &entry);
198 if (memiter_iseq(&entry, str)) {
199 return true;
200 }
201 }
202 return false;
203}
204
David Brazdil7a462ec2019-08-15 12:27:47 +0100205static enum manifest_return_code parse_vm(struct fdt_node *node,
206 struct manifest_vm *vm,
207 spci_vm_id_t vm_id)
208{
David Brazdil136f2942019-09-23 14:11:03 +0100209 TRY(read_string(node, "debug_name", &vm->debug_name));
210 TRY(read_optional_string(node, "kernel_filename",
211 &vm->kernel_filename));
David Brazdil7a462ec2019-08-15 12:27:47 +0100212 if (vm_id != HF_PRIMARY_VM_ID) {
David Brazdil7a462ec2019-08-15 12:27:47 +0100213 TRY(read_uint64(node, "mem_size", &vm->secondary.mem_size));
214 TRY(read_uint16(node, "vcpu_count", &vm->secondary.vcpu_count));
215 }
216 return MANIFEST_SUCCESS;
217}
218
219/**
220 * Parse manifest from FDT.
221 */
222enum manifest_return_code manifest_init(struct manifest *manifest,
David Brazdil0dbb41f2019-09-09 18:03:35 +0100223 const struct fdt_node *fdt_root)
David Brazdil7a462ec2019-08-15 12:27:47 +0100224{
225 char vm_name_buf[VM_NAME_BUF_SIZE];
226 struct fdt_node hyp_node;
David Brazdil74e9c3b2019-08-28 11:09:08 +0100227 struct stringlist_iter compatible_list;
David Brazdil7a462ec2019-08-15 12:27:47 +0100228 size_t i = 0;
229 bool found_primary_vm = false;
230
231 memset_s(manifest, sizeof(*manifest), 0, sizeof(*manifest));
232
233 /* Find hypervisor node. */
David Brazdil0dbb41f2019-09-09 18:03:35 +0100234 hyp_node = *fdt_root;
David Brazdil7a462ec2019-08-15 12:27:47 +0100235 if (!fdt_find_child(&hyp_node, "hypervisor")) {
236 return MANIFEST_ERROR_NO_HYPERVISOR_FDT_NODE;
237 }
238
David Brazdil74e9c3b2019-08-28 11:09:08 +0100239 /* Check "compatible" property. */
240 TRY(read_stringlist(&hyp_node, "compatible", &compatible_list));
241 if (!stringlist_contains(&compatible_list, "hafnium,hafnium")) {
242 return MANIFEST_ERROR_NOT_COMPATIBLE;
243 }
244
David Brazdil7a462ec2019-08-15 12:27:47 +0100245 /* Iterate over reserved VM IDs and check no such nodes exist. */
246 for (i = 0; i < HF_VM_ID_OFFSET; i++) {
247 spci_vm_id_t vm_id = (spci_vm_id_t)i;
248 struct fdt_node vm_node = hyp_node;
249 const char *vm_name = generate_vm_node_name(vm_name_buf, vm_id);
250
251 if (fdt_find_child(&vm_node, vm_name)) {
252 return MANIFEST_ERROR_RESERVED_VM_ID;
253 }
254 }
255
256 /* Iterate over VM nodes until we find one that does not exist. */
257 for (i = 0; i <= MAX_VMS; ++i) {
258 spci_vm_id_t vm_id = HF_VM_ID_OFFSET + i;
259 struct fdt_node vm_node = hyp_node;
260 const char *vm_name = generate_vm_node_name(vm_name_buf, vm_id);
261
262 if (!fdt_find_child(&vm_node, vm_name)) {
263 break;
264 }
265
266 if (i == MAX_VMS) {
267 return MANIFEST_ERROR_TOO_MANY_VMS;
268 }
269
270 if (vm_id == HF_PRIMARY_VM_ID) {
271 CHECK(found_primary_vm == false); /* sanity check */
272 found_primary_vm = true;
273 }
274
David Brazdil0251b942019-09-10 15:59:50 +0100275 manifest->vm_count = i + 1;
David Brazdil7a462ec2019-08-15 12:27:47 +0100276 TRY(parse_vm(&vm_node, &manifest->vm[i], vm_id));
277 }
278
279 if (!found_primary_vm) {
280 return MANIFEST_ERROR_NO_PRIMARY_VM;
281 }
282
283 return MANIFEST_SUCCESS;
284}
285
286const char *manifest_strerror(enum manifest_return_code ret_code)
287{
288 switch (ret_code) {
289 case MANIFEST_SUCCESS:
290 return "Success";
David Brazdil7a462ec2019-08-15 12:27:47 +0100291 case MANIFEST_ERROR_NO_HYPERVISOR_FDT_NODE:
292 return "Could not find \"hypervisor\" node in manifest";
David Brazdil74e9c3b2019-08-28 11:09:08 +0100293 case MANIFEST_ERROR_NOT_COMPATIBLE:
294 return "Hypervisor manifest entry not compatible with Hafnium";
David Brazdil7a462ec2019-08-15 12:27:47 +0100295 case MANIFEST_ERROR_RESERVED_VM_ID:
296 return "Manifest defines a VM with a reserved ID";
297 case MANIFEST_ERROR_NO_PRIMARY_VM:
298 return "Manifest does not contain a primary VM entry";
299 case MANIFEST_ERROR_TOO_MANY_VMS:
300 return "Manifest specifies more VMs than Hafnium has "
301 "statically allocated space for";
302 case MANIFEST_ERROR_PROPERTY_NOT_FOUND:
303 return "Property not found";
304 case MANIFEST_ERROR_MALFORMED_STRING:
305 return "Malformed string property";
David Brazdil0dbb41f2019-09-09 18:03:35 +0100306 case MANIFEST_ERROR_STRING_TOO_LONG:
307 return "String too long";
David Brazdil74e9c3b2019-08-28 11:09:08 +0100308 case MANIFEST_ERROR_MALFORMED_STRING_LIST:
309 return "Malformed string list property";
David Brazdil7a462ec2019-08-15 12:27:47 +0100310 case MANIFEST_ERROR_MALFORMED_INTEGER:
311 return "Malformed integer property";
312 case MANIFEST_ERROR_INTEGER_OVERFLOW:
313 return "Integer overflow";
314 }
315
316 panic("Unexpected manifest return code.");
317}