blob: dc6ed5bb91993f2c05dbe6317186adce9dc1a5fe [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
56static enum manifest_return_code read_string(const struct fdt_node *node,
David Brazdil0dbb41f2019-09-09 18:03:35 +010057 const char *property, char *out,
58 rsize_t out_sz)
David Brazdil7a462ec2019-08-15 12:27:47 +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 Brazdil74e9c3b2019-08-28 11:09:08 +010067 /*
68 * Require that the value contains exactly one NULL character and that
69 * it is the last byte.
70 */
71 if (memchr(data, '\0', size) != &data[size - 1]) {
David Brazdil7a462ec2019-08-15 12:27:47 +010072 return MANIFEST_ERROR_MALFORMED_STRING;
73 }
74
David Brazdil0dbb41f2019-09-09 18:03:35 +010075 /* Check that the string fits into the buffer. */
76 if (size > out_sz) {
77 return MANIFEST_ERROR_STRING_TOO_LONG;
78 }
79
80 memcpy_s(out, out_sz, data, size);
David Brazdil7a462ec2019-08-15 12:27:47 +010081 return MANIFEST_SUCCESS;
82}
83
84static enum manifest_return_code read_uint64(const struct fdt_node *node,
85 const char *property,
86 uint64_t *out)
87{
88 const char *data;
89 uint32_t size;
90
91 if (!fdt_read_property(node, property, &data, &size)) {
92 return MANIFEST_ERROR_PROPERTY_NOT_FOUND;
93 }
94
95 if (!fdt_parse_number(data, size, out)) {
96 return MANIFEST_ERROR_MALFORMED_INTEGER;
97 }
98
99 return MANIFEST_SUCCESS;
100}
101
102static enum manifest_return_code read_uint16(const struct fdt_node *node,
103 const char *property,
104 uint16_t *out)
105{
106 uint64_t value;
107
108 TRY(read_uint64(node, property, &value));
109
110 if (value > UINT16_MAX) {
111 return MANIFEST_ERROR_INTEGER_OVERFLOW;
112 }
113
114 *out = (uint16_t)value;
115 return MANIFEST_SUCCESS;
116}
117
David Brazdil74e9c3b2019-08-28 11:09:08 +0100118/**
119 * Represents the value of property whose type is a list of strings. These are
120 * encoded as one contiguous byte buffer with NULL-separated entries.
121 */
122struct stringlist_iter {
123 struct memiter mem_it;
124};
125
126static enum manifest_return_code read_stringlist(const struct fdt_node *node,
127 const char *property,
128 struct stringlist_iter *out)
129{
130 const char *data;
131 uint32_t size;
132
133 if (!fdt_read_property(node, property, &data, &size)) {
134 return MANIFEST_ERROR_PROPERTY_NOT_FOUND;
135 }
136
137 /*
138 * Require that the value ends with a NULL terminator. Other NULL
139 * characters separate the string list entries.
140 */
141 if (data[size - 1] != '\0') {
142 return MANIFEST_ERROR_MALFORMED_STRING_LIST;
143 }
144
145 memiter_init(&out->mem_it, data, size - 1);
146 return MANIFEST_SUCCESS;
147}
148
149static bool stringlist_has_next(const struct stringlist_iter *list)
150{
151 return memiter_size(&list->mem_it) > 0;
152}
153
154static void stringlist_get_next(struct stringlist_iter *list,
155 struct memiter *out)
156{
157 const char *mem_base = memiter_base(&list->mem_it);
158 size_t mem_size = memiter_size(&list->mem_it);
159 const char *null_term;
160
161 CHECK(stringlist_has_next(list));
162
163 null_term = memchr(mem_base, '\0', mem_size);
164 if (null_term == NULL) {
165 /*
166 * NULL terminator not found, this is the last entry.
167 * Set entry memiter to the entire byte range and advance list
168 * memiter to the end of the byte range.
169 */
170 memiter_init(out, mem_base, mem_size);
171 memiter_advance(&list->mem_it, mem_size);
172 } else {
173 /*
174 * Found NULL terminator. Set entry memiter to byte range
175 * [base, null) and move list memiter past the terminator.
176 */
177 size_t entry_size = null_term - mem_base;
178
179 memiter_init(out, mem_base, entry_size);
180 memiter_advance(&list->mem_it, entry_size + 1);
181 }
182}
183
184static bool stringlist_contains(const struct stringlist_iter *list,
185 const char *str)
186{
187 struct stringlist_iter it = *list;
188 struct memiter entry;
189
190 while (stringlist_has_next(&it)) {
191 stringlist_get_next(&it, &entry);
192 if (memiter_iseq(&entry, str)) {
193 return true;
194 }
195 }
196 return false;
197}
198
David Brazdil7a462ec2019-08-15 12:27:47 +0100199static enum manifest_return_code parse_vm(struct fdt_node *node,
200 struct manifest_vm *vm,
201 spci_vm_id_t vm_id)
202{
David Brazdil0dbb41f2019-09-09 18:03:35 +0100203 TRY(read_string(node, "debug_name", vm->debug_name,
204 sizeof(vm->debug_name)));
David Brazdil7a462ec2019-08-15 12:27:47 +0100205 if (vm_id != HF_PRIMARY_VM_ID) {
206 TRY(read_string(node, "kernel_filename",
David Brazdil0dbb41f2019-09-09 18:03:35 +0100207 vm->secondary.kernel_filename,
208 sizeof(vm->secondary.kernel_filename)));
David Brazdil7a462ec2019-08-15 12:27:47 +0100209 TRY(read_uint64(node, "mem_size", &vm->secondary.mem_size));
210 TRY(read_uint16(node, "vcpu_count", &vm->secondary.vcpu_count));
211 }
212 return MANIFEST_SUCCESS;
213}
214
215/**
216 * Parse manifest from FDT.
217 */
218enum manifest_return_code manifest_init(struct manifest *manifest,
David Brazdil0dbb41f2019-09-09 18:03:35 +0100219 const struct fdt_node *fdt_root)
David Brazdil7a462ec2019-08-15 12:27:47 +0100220{
221 char vm_name_buf[VM_NAME_BUF_SIZE];
222 struct fdt_node hyp_node;
David Brazdil74e9c3b2019-08-28 11:09:08 +0100223 struct stringlist_iter compatible_list;
David Brazdil7a462ec2019-08-15 12:27:47 +0100224 size_t i = 0;
225 bool found_primary_vm = false;
226
227 memset_s(manifest, sizeof(*manifest), 0, sizeof(*manifest));
228
229 /* Find hypervisor node. */
David Brazdil0dbb41f2019-09-09 18:03:35 +0100230 hyp_node = *fdt_root;
David Brazdil7a462ec2019-08-15 12:27:47 +0100231 if (!fdt_find_child(&hyp_node, "hypervisor")) {
232 return MANIFEST_ERROR_NO_HYPERVISOR_FDT_NODE;
233 }
234
David Brazdil74e9c3b2019-08-28 11:09:08 +0100235 /* Check "compatible" property. */
236 TRY(read_stringlist(&hyp_node, "compatible", &compatible_list));
237 if (!stringlist_contains(&compatible_list, "hafnium,hafnium")) {
238 return MANIFEST_ERROR_NOT_COMPATIBLE;
239 }
240
David Brazdil7a462ec2019-08-15 12:27:47 +0100241 /* Iterate over reserved VM IDs and check no such nodes exist. */
242 for (i = 0; i < HF_VM_ID_OFFSET; i++) {
243 spci_vm_id_t vm_id = (spci_vm_id_t)i;
244 struct fdt_node vm_node = hyp_node;
245 const char *vm_name = generate_vm_node_name(vm_name_buf, vm_id);
246
247 if (fdt_find_child(&vm_node, vm_name)) {
248 return MANIFEST_ERROR_RESERVED_VM_ID;
249 }
250 }
251
252 /* Iterate over VM nodes until we find one that does not exist. */
253 for (i = 0; i <= MAX_VMS; ++i) {
254 spci_vm_id_t vm_id = HF_VM_ID_OFFSET + i;
255 struct fdt_node vm_node = hyp_node;
256 const char *vm_name = generate_vm_node_name(vm_name_buf, vm_id);
257
258 if (!fdt_find_child(&vm_node, vm_name)) {
259 break;
260 }
261
262 if (i == MAX_VMS) {
263 return MANIFEST_ERROR_TOO_MANY_VMS;
264 }
265
266 if (vm_id == HF_PRIMARY_VM_ID) {
267 CHECK(found_primary_vm == false); /* sanity check */
268 found_primary_vm = true;
269 }
270
David Brazdil0251b942019-09-10 15:59:50 +0100271 manifest->vm_count = i + 1;
David Brazdil7a462ec2019-08-15 12:27:47 +0100272 TRY(parse_vm(&vm_node, &manifest->vm[i], vm_id));
273 }
274
275 if (!found_primary_vm) {
276 return MANIFEST_ERROR_NO_PRIMARY_VM;
277 }
278
279 return MANIFEST_SUCCESS;
280}
281
282const char *manifest_strerror(enum manifest_return_code ret_code)
283{
284 switch (ret_code) {
285 case MANIFEST_SUCCESS:
286 return "Success";
David Brazdil7a462ec2019-08-15 12:27:47 +0100287 case MANIFEST_ERROR_NO_HYPERVISOR_FDT_NODE:
288 return "Could not find \"hypervisor\" node in manifest";
David Brazdil74e9c3b2019-08-28 11:09:08 +0100289 case MANIFEST_ERROR_NOT_COMPATIBLE:
290 return "Hypervisor manifest entry not compatible with Hafnium";
David Brazdil7a462ec2019-08-15 12:27:47 +0100291 case MANIFEST_ERROR_RESERVED_VM_ID:
292 return "Manifest defines a VM with a reserved ID";
293 case MANIFEST_ERROR_NO_PRIMARY_VM:
294 return "Manifest does not contain a primary VM entry";
295 case MANIFEST_ERROR_TOO_MANY_VMS:
296 return "Manifest specifies more VMs than Hafnium has "
297 "statically allocated space for";
298 case MANIFEST_ERROR_PROPERTY_NOT_FOUND:
299 return "Property not found";
300 case MANIFEST_ERROR_MALFORMED_STRING:
301 return "Malformed string property";
David Brazdil0dbb41f2019-09-09 18:03:35 +0100302 case MANIFEST_ERROR_STRING_TOO_LONG:
303 return "String too long";
David Brazdil74e9c3b2019-08-28 11:09:08 +0100304 case MANIFEST_ERROR_MALFORMED_STRING_LIST:
305 return "Malformed string list property";
David Brazdil7a462ec2019-08-15 12:27:47 +0100306 case MANIFEST_ERROR_MALFORMED_INTEGER:
307 return "Malformed integer property";
308 case MANIFEST_ERROR_INTEGER_OVERFLOW:
309 return "Integer overflow";
310 }
311
312 panic("Unexpected manifest return code.");
313}