blob: a917fb4ca5117eddb26c63672f706316d865165f [file] [log] [blame]
David Brazdil7a462ec2019-08-15 12:27:47 +01001/*
2 * Copyright 2019 The Hafnium Authors.
3 *
Andrew Walbrane959ec12020-06-17 15:01:09 +01004 * Use of this source code is governed by a BSD-style
5 * license that can be found in the LICENSE file or at
6 * https://opensource.org/licenses/BSD-3-Clause.
David Brazdil7a462ec2019-08-15 12:27:47 +01007 */
8
David Brazdil52256ff2019-08-23 15:15:15 +01009#include <array>
10#include <cstdio>
Andrew Scullae9962e2019-10-03 16:51:16 +010011#include <span>
David Brazdil52256ff2019-08-23 15:15:15 +010012#include <sstream>
13
David Brazdil7a462ec2019-08-15 12:27:47 +010014#include <gmock/gmock.h>
15
16extern "C" {
J-Alves2f86c1e2022-02-23 18:44:19 +000017#include "hf/arch/std.h"
18
David Brazdil7a462ec2019-08-15 12:27:47 +010019#include "hf/manifest.h"
J-Alves2f86c1e2022-02-23 18:44:19 +000020#include "hf/sp_pkg.h"
David Brazdil7a462ec2019-08-15 12:27:47 +010021}
22
23namespace
24{
Andrew Scullae9962e2019-10-03 16:51:16 +010025using ::testing::ElementsAre;
David Brazdil7a462ec2019-08-15 12:27:47 +010026using ::testing::Eq;
Andrew Scullae9962e2019-10-03 16:51:16 +010027using ::testing::IsEmpty;
David Brazdil7a462ec2019-08-15 12:27:47 +010028using ::testing::NotNull;
29
Daniel Boulby801f8ef2022-06-27 14:21:01 +010030using struct_manifest = struct manifest;
31
32constexpr size_t TEST_HEAP_SIZE = PAGE_SIZE * 32;
33
David Brazdil52256ff2019-08-23 15:15:15 +010034template <typename T>
David Brazdil0dbb41f2019-09-09 18:03:35 +010035void exec(const char *program, const char *args[], const T &stdin,
David Brazdil52256ff2019-08-23 15:15:15 +010036 std::vector<char> *stdout)
37{
38 /* Create two pipes, one for stdin and one for stdout. */
39 int pipes[2][2];
40 pipe(pipes[0]);
41 pipe(pipes[1]);
David Brazdil7a462ec2019-08-15 12:27:47 +010042
David Brazdil52256ff2019-08-23 15:15:15 +010043 /* Assign FDs for reading/writing by the parent/child. */
44 int parent_read_fd = pipes[1][0]; /* stdout pipe, read FD */
45 int parent_write_fd = pipes[0][1]; /* stdin pipe, write FD */
46 int child_read_fd = pipes[0][0]; /* stdin pipe, read FD */
47 int child_write_fd = pipes[1][1]; /* stdout pipe, write FD */
David Brazdil7a462ec2019-08-15 12:27:47 +010048
David Brazdil52256ff2019-08-23 15:15:15 +010049 if (fork()) {
50 /* Parent process. */
51 std::array<char, 128> buf;
52 ssize_t res;
53
54 /* Close child FDs which won't be used. */
55 close(child_read_fd);
56 close(child_write_fd);
57
58 /* Write to stdin. */
59 for (size_t count = 0; count < stdin.size();) {
60 res = write(parent_write_fd, stdin.data() + count,
61 stdin.size() - count);
62 if (res < 0) {
63 std::cerr << "IO error" << std::endl;
64 exit(1);
65 }
66 count += res;
67 }
68 close(parent_write_fd);
69
70 /* Read from stdout. */
71 while (true) {
72 res = read(parent_read_fd, buf.data(), buf.size());
73 if (res == 0) {
74 /* EOF */
75 break;
76 } else if (res < 0) {
77 std::cerr << "IO error" << std::endl;
78 exit(1);
79 }
80 stdout->insert(stdout->end(), buf.begin(),
81 buf.begin() + res);
82 }
83 close(parent_read_fd);
84 } else {
85 /* Child process. */
86
87 /* Redirect stdin/stdout to read/write FDs. */
88 dup2(child_read_fd, STDIN_FILENO);
89 dup2(child_write_fd, STDOUT_FILENO);
90
91 /* Close all FDs which are now unused. */
92 close(child_read_fd);
93 close(child_write_fd);
94 close(parent_read_fd);
95 close(parent_write_fd);
96
97 /* Execute the given program. */
David Brazdil0dbb41f2019-09-09 18:03:35 +010098 execv(program, const_cast<char *const *>(args));
David Brazdil52256ff2019-08-23 15:15:15 +010099 }
100}
101
102/**
103 * Class for programatically building a Device Tree.
104 *
105 * Usage:
106 * std::vector<char> dtb = ManifestDtBuilder()
107 * .Command1()
108 * .Command2()
109 * ...
110 * .CommandN()
111 * .Build();
112 */
113class ManifestDtBuilder
114{
115 public:
116 ManifestDtBuilder()
117 {
118 dts_ << "/dts-v1/;" << std::endl;
119 dts_ << std::endl;
120
121 /* Start root node. */
122 StartChild("/");
123 }
124
Andrew Scullae9962e2019-10-03 16:51:16 +0100125 std::vector<char> Build(bool dump = false)
David Brazdil52256ff2019-08-23 15:15:15 +0100126 {
David Brazdil0dbb41f2019-09-09 18:03:35 +0100127 const char *program = "./build/image/dtc.py";
128 const char *dtc_args[] = {program, "compile", NULL};
David Brazdil52256ff2019-08-23 15:15:15 +0100129 std::vector<char> dtc_stdout;
130
131 /* Finish root node. */
132 EndChild();
133
Andrew Scullae9962e2019-10-03 16:51:16 +0100134 if (dump) {
135 Dump();
136 }
137
David Brazdil0dbb41f2019-09-09 18:03:35 +0100138 exec(program, dtc_args, dts_.str(), &dtc_stdout);
David Brazdil52256ff2019-08-23 15:15:15 +0100139 return dtc_stdout;
140 }
141
Andrew Scullae9962e2019-10-03 16:51:16 +0100142 void Dump()
143 {
144 std::cerr << dts_.str() << std::endl;
145 }
146
David Brazdil52256ff2019-08-23 15:15:15 +0100147 ManifestDtBuilder &StartChild(const std::string_view &name)
148 {
149 dts_ << name << " {" << std::endl;
150 return *this;
151 }
152
153 ManifestDtBuilder &EndChild()
154 {
155 dts_ << "};" << std::endl;
156 return *this;
157 }
158
David Brazdil74e9c3b2019-08-28 11:09:08 +0100159 ManifestDtBuilder &Compatible(const std::vector<std::string_view>
160 &value = {"hafnium,hafnium"})
161 {
162 return StringListProperty("compatible", value);
163 }
164
David Brazdil52256ff2019-08-23 15:15:15 +0100165 ManifestDtBuilder &DebugName(const std::string_view &value)
166 {
167 return StringProperty("debug_name", value);
168 }
169
Manish Pandey6542f5c2020-04-27 14:37:46 +0100170 ManifestDtBuilder &Description(const std::string_view &value)
171 {
172 return StringProperty("description", value);
173 }
174
David Brazdil52256ff2019-08-23 15:15:15 +0100175 ManifestDtBuilder &KernelFilename(const std::string_view &value)
176 {
177 return StringProperty("kernel_filename", value);
178 }
179
David Brazdile6f83222019-09-23 14:47:37 +0100180 ManifestDtBuilder &RamdiskFilename(const std::string_view &value)
181 {
182 return StringProperty("ramdisk_filename", value);
183 }
184
David Brazdil080ee312020-02-25 15:30:30 -0800185 ManifestDtBuilder &BootAddress(uint64_t value)
186 {
187 return Integer64Property("boot_address", value);
188 }
189
Andrew Scullae9962e2019-10-03 16:51:16 +0100190 ManifestDtBuilder &VcpuCount(uint32_t value)
David Brazdil52256ff2019-08-23 15:15:15 +0100191 {
192 return IntegerProperty("vcpu_count", value);
193 }
194
Andrew Scullae9962e2019-10-03 16:51:16 +0100195 ManifestDtBuilder &MemSize(uint32_t value)
David Brazdil52256ff2019-08-23 15:15:15 +0100196 {
197 return IntegerProperty("mem_size", value);
198 }
199
Andrew Scullae9962e2019-10-03 16:51:16 +0100200 ManifestDtBuilder &SmcWhitelist(const std::vector<uint32_t> &value)
201 {
202 return IntegerListProperty("smc_whitelist", value);
203 }
204
205 ManifestDtBuilder &SmcWhitelistPermissive()
206 {
207 return BooleanProperty("smc_whitelist_permissive");
208 }
209
Olivier Deprez62d99e32020-01-09 15:58:07 +0100210 ManifestDtBuilder &LoadAddress(uint64_t value)
211 {
212 return Integer64Property("load_address", value);
213 }
214
215 ManifestDtBuilder &FfaPartition()
216 {
217 return BooleanProperty("is_ffa_partition");
218 }
219
Andrew Scullae9962e2019-10-03 16:51:16 +0100220 ManifestDtBuilder &Property(const std::string_view &name,
221 const std::string_view &value)
222 {
223 dts_ << name << " = " << value << ";" << std::endl;
224 return *this;
225 }
226
Manish Pandeyfa1f2912020-05-05 12:57:01 +0100227 ManifestDtBuilder &Label(const std::string_view &name)
228 {
229 dts_ << name << ": ";
230 return *this;
231 }
232
Manish Pandeycb8fbb22020-08-18 00:04:43 +0100233 ManifestDtBuilder &FfaValidManifest()
234 {
235 Compatible({"arm,ffa-manifest-1.0"});
236 Property("ffa-version", "<0x10000>");
237 Property("uuid",
238 "<0xb4b5671e 0x4a904fe1 0xb81ffb13 0xdae1dacb>");
239 Property("execution-ctx-count", "<1>");
240 Property("exception-level", "<2>");
241 Property("execution-state", "<0>");
J-Alves2f86c1e2022-02-23 18:44:19 +0000242 Property("entrypoint-offset", "<0x00002000>");
Manish Pandeycb8fbb22020-08-18 00:04:43 +0100243 Property("xlat-granule", "<0>");
J-Alvesb37fd082020-10-22 12:29:21 +0100244 Property("boot-order", "<0>");
Maksims Svecovsb596eab2021-04-27 00:52:27 +0100245 Property("messaging-method", "<4>");
Maksims Svecovs9ddf86a2021-05-06 17:17:21 +0100246 BooleanProperty("managed-exit");
Manish Pandeycb8fbb22020-08-18 00:04:43 +0100247 return *this;
248 }
249
David Brazdil52256ff2019-08-23 15:15:15 +0100250 private:
251 ManifestDtBuilder &StringProperty(const std::string_view &name,
252 const std::string_view &value)
253 {
254 dts_ << name << " = \"" << value << "\";" << std::endl;
255 return *this;
256 }
257
David Brazdil74e9c3b2019-08-28 11:09:08 +0100258 ManifestDtBuilder &StringListProperty(
259 const std::string_view &name,
260 const std::vector<std::string_view> &value)
261 {
262 bool is_first = true;
263
264 dts_ << name << " = ";
265 for (const std::string_view &entry : value) {
266 if (is_first) {
267 is_first = false;
268 } else {
269 dts_ << ", ";
270 }
271 dts_ << "\"" << entry << "\"";
272 }
273 dts_ << ";" << std::endl;
274 return *this;
275 }
276
David Brazdil52256ff2019-08-23 15:15:15 +0100277 ManifestDtBuilder &IntegerProperty(const std::string_view &name,
Andrew Scullae9962e2019-10-03 16:51:16 +0100278 uint32_t value)
David Brazdil52256ff2019-08-23 15:15:15 +0100279 {
280 dts_ << name << " = <" << value << ">;" << std::endl;
281 return *this;
282 }
283
David Brazdil080ee312020-02-25 15:30:30 -0800284 ManifestDtBuilder &Integer64Property(const std::string_view &name,
285 uint64_t value)
286 {
287 uint32_t high = value >> 32;
288 uint32_t low = (uint32_t)value;
289 dts_ << name << " = <" << high << " " << low << ">;"
290 << std::endl;
291 return *this;
292 }
293
Andrew Scullae9962e2019-10-03 16:51:16 +0100294 ManifestDtBuilder &IntegerListProperty(
295 const std::string_view &name,
296 const std::vector<uint32_t> &value)
297 {
298 dts_ << name << " = < ";
299 for (const uint32_t entry : value) {
300 dts_ << entry << " ";
301 }
302 dts_ << ">;" << std::endl;
303 return *this;
304 }
305
306 ManifestDtBuilder &BooleanProperty(const std::string_view &name)
307 {
Andrew Scull5dc089e2019-11-04 13:21:03 +0000308 dts_ << name << ";" << std::endl;
309 return *this;
Andrew Scullae9962e2019-10-03 16:51:16 +0100310 }
311
David Brazdil52256ff2019-08-23 15:15:15 +0100312 std::stringstream dts_;
313};
314
Daniel Boulby801f8ef2022-06-27 14:21:01 +0100315class manifest : public ::testing::Test
David Brazdil0dbb41f2019-09-09 18:03:35 +0100316{
Daniel Boulby801f8ef2022-06-27 14:21:01 +0100317 void SetUp() override
318 {
319 test_heap = std::make_unique<uint8_t[]>(TEST_HEAP_SIZE);
320 mpool_init(&ppool, MM_PPOOL_ENTRY_SIZE);
321 mpool_add_chunk(&ppool, test_heap.get(), TEST_HEAP_SIZE);
322 }
323
324 std::unique_ptr<uint8_t[]> test_heap;
325
326 protected:
Olivier Deprez62d99e32020-01-09 15:58:07 +0100327 struct mpool ppool;
David Brazdil0dbb41f2019-09-09 18:03:35 +0100328
Daniel Boulby801f8ef2022-06-27 14:21:01 +0100329 public:
330 /**
331 * Class for programatically building a Partition package.
332 */
333 class Partition_package
334 {
335 public:
336 __attribute__((aligned(PAGE_SIZE))) struct sp_pkg_header spkg;
337 __attribute__((
338 aligned(PAGE_SIZE))) char manifest_dtb[PAGE_SIZE] = {};
339 __attribute__((aligned(PAGE_SIZE))) char img[PAGE_SIZE] = {};
David Brazdil0dbb41f2019-09-09 18:03:35 +0100340
Daniel Boulby801f8ef2022-06-27 14:21:01 +0100341 Partition_package(const std::vector<char> &vec)
342 {
343 // Initialise header field
344 spkg.magic = SP_PKG_HEADER_MAGIC;
345 spkg.version = SP_PKG_HEADER_VERSION_2;
346 spkg.pm_offset = PAGE_SIZE;
347 spkg.pm_size = vec.size();
348 spkg.img_offset = 2 * PAGE_SIZE;
349 spkg.img_size = ARRAY_SIZE(img);
350
351 // Copy dtb into package
352 std::copy(vec.begin(), vec.end(), manifest_dtb);
353 }
354 };
355
356 enum manifest_return_code manifest_from_vec(
357 struct_manifest *m, const std::vector<char> &vec)
358 {
359 struct memiter it;
360 struct mm_stage1_locked mm_stage1_locked;
361 enum manifest_return_code ret;
362
363 memiter_init(&it, vec.data(), vec.size());
364 ret = manifest_init(mm_stage1_locked, m, &it, &ppool);
365
366 manifest_deinit(&ppool);
367 return ret;
368 }
369
370 enum manifest_return_code ffa_manifest_from_vec(
371 struct_manifest *m, const std::vector<char> &vec)
372 {
373 struct memiter it;
374 struct mm_stage1_locked mm_stage1_locked;
375 enum manifest_return_code ret;
376
377 Partition_package spkg(vec);
378
379 /* clang-format off */
380 std::vector<char> core_dtb = ManifestDtBuilder()
381 .StartChild("hypervisor")
382 .Compatible()
383 .StartChild("vm1")
384 .DebugName("primary_vm")
385 .FfaPartition()
386 .LoadAddress((uint64_t)&spkg)
387 .EndChild()
388 .EndChild()
389 .Build();
390 /* clang-format on */
391 memiter_init(&it, core_dtb.data(), core_dtb.size());
392 ret = manifest_init(mm_stage1_locked, m, &it, &ppool);
393
394 manifest_deinit(&ppool);
395 return ret;
396 }
397};
398
399TEST_F(manifest, no_hypervisor_node)
David Brazdil7a462ec2019-08-15 12:27:47 +0100400{
Daniel Boulby801f8ef2022-06-27 14:21:01 +0100401 struct_manifest m;
David Brazdil52256ff2019-08-23 15:15:15 +0100402 std::vector<char> dtb = ManifestDtBuilder().Build();
David Brazdil7a462ec2019-08-15 12:27:47 +0100403
David Brazdila2358d42020-01-27 18:51:38 +0000404 ASSERT_EQ(manifest_from_vec(&m, dtb),
David Brazdil7a462ec2019-08-15 12:27:47 +0100405 MANIFEST_ERROR_NO_HYPERVISOR_FDT_NODE);
406}
407
Daniel Boulby801f8ef2022-06-27 14:21:01 +0100408TEST_F(manifest, no_compatible_property)
David Brazdil7a462ec2019-08-15 12:27:47 +0100409{
Daniel Boulby801f8ef2022-06-27 14:21:01 +0100410 struct_manifest m;
David Brazdil7a462ec2019-08-15 12:27:47 +0100411
David Brazdil52256ff2019-08-23 15:15:15 +0100412 /* clang-format off */
413 std::vector<char> dtb = ManifestDtBuilder()
414 .StartChild("hypervisor")
415 .EndChild()
416 .Build();
417 /* clang-format on */
418
David Brazdilf4925382020-03-25 13:33:51 +0000419 ASSERT_EQ(manifest_from_vec(&m, dtb), MANIFEST_ERROR_NOT_COMPATIBLE);
David Brazdil7a462ec2019-08-15 12:27:47 +0100420}
421
Daniel Boulby801f8ef2022-06-27 14:21:01 +0100422TEST_F(manifest, not_compatible)
David Brazdil7a462ec2019-08-15 12:27:47 +0100423{
Daniel Boulby801f8ef2022-06-27 14:21:01 +0100424 struct_manifest m;
David Brazdil7a462ec2019-08-15 12:27:47 +0100425
David Brazdil52256ff2019-08-23 15:15:15 +0100426 /* clang-format off */
427 std::vector<char> dtb = ManifestDtBuilder()
428 .StartChild("hypervisor")
David Brazdil74e9c3b2019-08-28 11:09:08 +0100429 .Compatible({ "foo,bar" })
430 .EndChild()
431 .Build();
432 /* clang-format on */
433
David Brazdila2358d42020-01-27 18:51:38 +0000434 ASSERT_EQ(manifest_from_vec(&m, dtb), MANIFEST_ERROR_NOT_COMPATIBLE);
David Brazdil74e9c3b2019-08-28 11:09:08 +0100435}
436
Daniel Boulby801f8ef2022-06-27 14:21:01 +0100437TEST_F(manifest, compatible_one_of_many)
David Brazdil74e9c3b2019-08-28 11:09:08 +0100438{
Daniel Boulby801f8ef2022-06-27 14:21:01 +0100439 struct_manifest m;
David Brazdil74e9c3b2019-08-28 11:09:08 +0100440
441 /* clang-format off */
442 std::vector<char> dtb = ManifestDtBuilder()
443 .StartChild("hypervisor")
444 .Compatible({ "foo,bar", "hafnium,hafnium" })
445 .StartChild("vm1")
446 .DebugName("primary")
447 .EndChild()
448 .EndChild()
449 .Build();
450 /* clang-format on */
451
David Brazdila2358d42020-01-27 18:51:38 +0000452 ASSERT_EQ(manifest_from_vec(&m, dtb), MANIFEST_SUCCESS);
David Brazdil74e9c3b2019-08-28 11:09:08 +0100453}
454
Daniel Boulby801f8ef2022-06-27 14:21:01 +0100455TEST_F(manifest, no_vm_nodes)
David Brazdil74e9c3b2019-08-28 11:09:08 +0100456{
Daniel Boulby801f8ef2022-06-27 14:21:01 +0100457 struct_manifest m;
David Brazdil74e9c3b2019-08-28 11:09:08 +0100458
459 /* clang-format off */
460 std::vector<char> dtb = ManifestDtBuilder()
461 .StartChild("hypervisor")
462 .Compatible()
463 .EndChild()
464 .Build();
465 /* clang-format on */
466
David Brazdila2358d42020-01-27 18:51:38 +0000467 ASSERT_EQ(manifest_from_vec(&m, dtb), MANIFEST_ERROR_NO_PRIMARY_VM);
David Brazdil0dbb41f2019-09-09 18:03:35 +0100468}
469
470static std::vector<char> gen_long_string_dtb(bool valid)
471{
472 const char last_valid[] = "1234567890123456789012345678901";
473 const char first_invalid[] = "12345678901234567890123456789012";
David Brazdil136f2942019-09-23 14:11:03 +0100474 static_assert(sizeof(last_valid) == STRING_MAX_SIZE);
475 static_assert(sizeof(first_invalid) == STRING_MAX_SIZE + 1);
David Brazdil0dbb41f2019-09-09 18:03:35 +0100476
477 /* clang-format off */
478 return ManifestDtBuilder()
479 .StartChild("hypervisor")
480 .Compatible()
481 .StartChild("vm1")
482 .DebugName(valid ? last_valid : first_invalid)
483 .EndChild()
484 .EndChild()
485 .Build();
486 /* clang-format on */
487}
488
Daniel Boulby801f8ef2022-06-27 14:21:01 +0100489TEST_F(manifest, long_string)
David Brazdil0dbb41f2019-09-09 18:03:35 +0100490{
Daniel Boulby801f8ef2022-06-27 14:21:01 +0100491 struct_manifest m;
David Brazdil0dbb41f2019-09-09 18:03:35 +0100492 std::vector<char> dtb_last_valid = gen_long_string_dtb(true);
493 std::vector<char> dtb_first_invalid = gen_long_string_dtb(false);
494
David Brazdila2358d42020-01-27 18:51:38 +0000495 ASSERT_EQ(manifest_from_vec(&m, dtb_last_valid), MANIFEST_SUCCESS);
496 ASSERT_EQ(manifest_from_vec(&m, dtb_first_invalid),
497 MANIFEST_ERROR_STRING_TOO_LONG);
David Brazdil74e9c3b2019-08-28 11:09:08 +0100498}
499
Daniel Boulby801f8ef2022-06-27 14:21:01 +0100500TEST_F(manifest, reserved_vm_id)
David Brazdil74e9c3b2019-08-28 11:09:08 +0100501{
Daniel Boulby801f8ef2022-06-27 14:21:01 +0100502 struct_manifest m;
David Brazdil74e9c3b2019-08-28 11:09:08 +0100503
504 /* clang-format off */
505 std::vector<char> dtb = ManifestDtBuilder()
506 .StartChild("hypervisor")
507 .Compatible()
David Brazdil52256ff2019-08-23 15:15:15 +0100508 .StartChild("vm1")
509 .DebugName("primary_vm")
510 .EndChild()
511 .StartChild("vm0")
512 .DebugName("reserved_vm")
513 .VcpuCount(1)
514 .MemSize(0x1000)
515 .KernelFilename("kernel")
516 .EndChild()
517 .EndChild()
518 .Build();
519 /* clang-format on */
520
David Brazdila2358d42020-01-27 18:51:38 +0000521 ASSERT_EQ(manifest_from_vec(&m, dtb), MANIFEST_ERROR_RESERVED_VM_ID);
David Brazdil7a462ec2019-08-15 12:27:47 +0100522}
523
Andrew Scullae9962e2019-10-03 16:51:16 +0100524static std::vector<char> gen_vcpu_count_limit_dtb(uint32_t vcpu_count)
David Brazdil52256ff2019-08-23 15:15:15 +0100525{
526 /* clang-format off */
527 return ManifestDtBuilder()
528 .StartChild("hypervisor")
David Brazdil74e9c3b2019-08-28 11:09:08 +0100529 .Compatible()
David Brazdil52256ff2019-08-23 15:15:15 +0100530 .StartChild("vm1")
531 .DebugName("primary_vm")
532 .EndChild()
533 .StartChild("vm2")
534 .DebugName("secondary_vm")
535 .VcpuCount(vcpu_count)
536 .MemSize(0x1000)
537 .KernelFilename("kernel")
538 .EndChild()
539 .EndChild()
540 .Build();
541 /* clang-format on */
542}
David Brazdil7a462ec2019-08-15 12:27:47 +0100543
Daniel Boulby801f8ef2022-06-27 14:21:01 +0100544TEST_F(manifest, vcpu_count_limit)
David Brazdil7a462ec2019-08-15 12:27:47 +0100545{
Daniel Boulby801f8ef2022-06-27 14:21:01 +0100546 struct_manifest m;
David Brazdil52256ff2019-08-23 15:15:15 +0100547 std::vector<char> dtb_last_valid = gen_vcpu_count_limit_dtb(UINT16_MAX);
548 std::vector<char> dtb_first_invalid =
549 gen_vcpu_count_limit_dtb(UINT16_MAX + 1);
David Brazdil7a462ec2019-08-15 12:27:47 +0100550
David Brazdila2358d42020-01-27 18:51:38 +0000551 ASSERT_EQ(manifest_from_vec(&m, dtb_last_valid), MANIFEST_SUCCESS);
David Brazdil0251b942019-09-10 15:59:50 +0100552 ASSERT_EQ(m.vm_count, 2);
David Brazdil7a462ec2019-08-15 12:27:47 +0100553 ASSERT_EQ(m.vm[1].secondary.vcpu_count, UINT16_MAX);
554
David Brazdila2358d42020-01-27 18:51:38 +0000555 ASSERT_EQ(manifest_from_vec(&m, dtb_first_invalid),
David Brazdil0dbb41f2019-09-09 18:03:35 +0100556 MANIFEST_ERROR_INTEGER_OVERFLOW);
David Brazdil7a462ec2019-08-15 12:27:47 +0100557}
558
Daniel Boulby801f8ef2022-06-27 14:21:01 +0100559TEST_F(manifest, no_ramdisk_primary)
David Brazdile6f83222019-09-23 14:47:37 +0100560{
Daniel Boulby801f8ef2022-06-27 14:21:01 +0100561 struct_manifest m;
David Brazdile6f83222019-09-23 14:47:37 +0100562
563 /* clang-format off */
564 std::vector<char> dtb = ManifestDtBuilder()
565 .StartChild("hypervisor")
566 .Compatible()
567 .StartChild("vm1")
568 .DebugName("primary_vm")
569 .EndChild()
570 .EndChild()
571 .Build();
572 /* clang-format on */
573
David Brazdila2358d42020-01-27 18:51:38 +0000574 ASSERT_EQ(manifest_from_vec(&m, dtb), MANIFEST_SUCCESS);
David Brazdile6f83222019-09-23 14:47:37 +0100575 ASSERT_EQ(m.vm_count, 1);
576 ASSERT_STREQ(string_data(&m.vm[0].debug_name), "primary_vm");
577 ASSERT_STREQ(string_data(&m.vm[0].primary.ramdisk_filename), "");
578}
579
Daniel Boulby801f8ef2022-06-27 14:21:01 +0100580TEST_F(manifest, no_boot_address_primary)
David Brazdil080ee312020-02-25 15:30:30 -0800581{
Daniel Boulby801f8ef2022-06-27 14:21:01 +0100582 struct_manifest m;
David Brazdil080ee312020-02-25 15:30:30 -0800583
584 /* clang-format off */
585 std::vector<char> dtb = ManifestDtBuilder()
586 .StartChild("hypervisor")
587 .Compatible()
588 .StartChild("vm1")
589 .DebugName("primary_vm")
590 .EndChild()
591 .EndChild()
592 .Build();
593 /* clang-format on */
594
595 ASSERT_EQ(manifest_from_vec(&m, dtb), MANIFEST_SUCCESS);
596 ASSERT_EQ(m.vm_count, 1);
597 ASSERT_STREQ(string_data(&m.vm[0].debug_name), "primary_vm");
598 ASSERT_EQ(m.vm[0].primary.boot_address, MANIFEST_INVALID_ADDRESS);
599}
600
Daniel Boulby801f8ef2022-06-27 14:21:01 +0100601TEST_F(manifest, boot_address_primary)
David Brazdil080ee312020-02-25 15:30:30 -0800602{
Daniel Boulby801f8ef2022-06-27 14:21:01 +0100603 struct_manifest m;
David Brazdil080ee312020-02-25 15:30:30 -0800604 const uint64_t addr = UINT64_C(0x12345678ABCDEFEF);
605
606 /* clang-format off */
607 std::vector<char> dtb = ManifestDtBuilder()
608 .StartChild("hypervisor")
609 .Compatible()
610 .StartChild("vm1")
611 .DebugName("primary_vm")
612 .BootAddress(addr)
613 .EndChild()
614 .EndChild()
615 .Build();
616 /* clang-format on */
617
618 ASSERT_EQ(manifest_from_vec(&m, dtb), MANIFEST_SUCCESS);
619 ASSERT_EQ(m.vm_count, 1);
620 ASSERT_STREQ(string_data(&m.vm[0].debug_name), "primary_vm");
621 ASSERT_EQ(m.vm[0].primary.boot_address, addr);
622}
623
Andrew Scullb2c3a242019-11-04 13:52:36 +0000624static std::vector<char> gen_malformed_boolean_dtb(
625 const std::string_view &value)
Andrew Scullae9962e2019-10-03 16:51:16 +0100626{
Andrew Scullae9962e2019-10-03 16:51:16 +0100627 /* clang-format off */
Andrew Scullb2c3a242019-11-04 13:52:36 +0000628 return ManifestDtBuilder()
Andrew Scullae9962e2019-10-03 16:51:16 +0100629 .StartChild("hypervisor")
630 .Compatible()
631 .StartChild("vm1")
632 .DebugName("primary_vm")
Andrew Scullb2c3a242019-11-04 13:52:36 +0000633 .Property("smc_whitelist_permissive", value)
Andrew Scull5dc089e2019-11-04 13:21:03 +0000634 .EndChild()
Andrew Scullae9962e2019-10-03 16:51:16 +0100635 .EndChild()
636 .Build();
637 /* clang-format on */
Andrew Scullb2c3a242019-11-04 13:52:36 +0000638}
Andrew Scullae9962e2019-10-03 16:51:16 +0100639
Daniel Boulby801f8ef2022-06-27 14:21:01 +0100640TEST_F(manifest, malformed_booleans)
Andrew Scullb2c3a242019-11-04 13:52:36 +0000641{
Daniel Boulby801f8ef2022-06-27 14:21:01 +0100642 struct_manifest m;
Andrew Scullae9962e2019-10-03 16:51:16 +0100643
Andrew Scullb2c3a242019-11-04 13:52:36 +0000644 std::vector<char> dtb_false = gen_malformed_boolean_dtb("\"false\"");
645 std::vector<char> dtb_true = gen_malformed_boolean_dtb("\"true\"");
646 std::vector<char> dtb_0 = gen_malformed_boolean_dtb("\"<0>\"");
647 std::vector<char> dtb_1 = gen_malformed_boolean_dtb("\"<1>\"");
Andrew Scullae9962e2019-10-03 16:51:16 +0100648
David Brazdila2358d42020-01-27 18:51:38 +0000649 ASSERT_EQ(manifest_from_vec(&m, dtb_false),
Andrew Scullb2c3a242019-11-04 13:52:36 +0000650 MANIFEST_ERROR_MALFORMED_BOOLEAN);
David Brazdila2358d42020-01-27 18:51:38 +0000651 ASSERT_EQ(manifest_from_vec(&m, dtb_true),
Andrew Scullb2c3a242019-11-04 13:52:36 +0000652 MANIFEST_ERROR_MALFORMED_BOOLEAN);
David Brazdila2358d42020-01-27 18:51:38 +0000653 ASSERT_EQ(manifest_from_vec(&m, dtb_0),
Andrew Scullb2c3a242019-11-04 13:52:36 +0000654 MANIFEST_ERROR_MALFORMED_BOOLEAN);
David Brazdila2358d42020-01-27 18:51:38 +0000655 ASSERT_EQ(manifest_from_vec(&m, dtb_1),
Andrew Scullb2c3a242019-11-04 13:52:36 +0000656 MANIFEST_ERROR_MALFORMED_BOOLEAN);
Andrew Scullae9962e2019-10-03 16:51:16 +0100657}
658
Daniel Boulby801f8ef2022-06-27 14:21:01 +0100659TEST_F(manifest, valid)
David Brazdil7a462ec2019-08-15 12:27:47 +0100660{
Daniel Boulby801f8ef2022-06-27 14:21:01 +0100661 struct_manifest m;
David Brazdil7a462ec2019-08-15 12:27:47 +0100662 struct manifest_vm *vm;
David Brazdil7a462ec2019-08-15 12:27:47 +0100663
David Brazdil52256ff2019-08-23 15:15:15 +0100664 /* clang-format off */
665 std::vector<char> dtb = ManifestDtBuilder()
666 .StartChild("hypervisor")
David Brazdil74e9c3b2019-08-28 11:09:08 +0100667 .Compatible()
David Brazdil52256ff2019-08-23 15:15:15 +0100668 .StartChild("vm1")
669 .DebugName("primary_vm")
Andrew Scull72b43c02019-09-18 13:53:45 +0100670 .KernelFilename("primary_kernel")
David Brazdile6f83222019-09-23 14:47:37 +0100671 .RamdiskFilename("primary_ramdisk")
Andrew Scullae9962e2019-10-03 16:51:16 +0100672 .SmcWhitelist({0x32000000, 0x33001111})
David Brazdil52256ff2019-08-23 15:15:15 +0100673 .EndChild()
674 .StartChild("vm3")
675 .DebugName("second_secondary_vm")
676 .VcpuCount(43)
677 .MemSize(0x12345)
Andrew Scull72b43c02019-09-18 13:53:45 +0100678 .KernelFilename("second_secondary_kernel")
David Brazdil52256ff2019-08-23 15:15:15 +0100679 .EndChild()
680 .StartChild("vm2")
681 .DebugName("first_secondary_vm")
682 .VcpuCount(42)
683 .MemSize(12345)
Andrew Scullae9962e2019-10-03 16:51:16 +0100684 .SmcWhitelist({0x04000000, 0x30002222, 0x31445566})
685 .SmcWhitelistPermissive()
David Brazdil52256ff2019-08-23 15:15:15 +0100686 .EndChild()
687 .EndChild()
688 .Build();
689 /* clang-format on */
690
David Brazdila2358d42020-01-27 18:51:38 +0000691 ASSERT_EQ(manifest_from_vec(&m, dtb), MANIFEST_SUCCESS);
David Brazdil0251b942019-09-10 15:59:50 +0100692 ASSERT_EQ(m.vm_count, 3);
David Brazdil7a462ec2019-08-15 12:27:47 +0100693
694 vm = &m.vm[0];
David Brazdil136f2942019-09-23 14:11:03 +0100695 ASSERT_STREQ(string_data(&vm->debug_name), "primary_vm");
696 ASSERT_STREQ(string_data(&vm->kernel_filename), "primary_kernel");
David Brazdile6f83222019-09-23 14:47:37 +0100697 ASSERT_STREQ(string_data(&vm->primary.ramdisk_filename),
698 "primary_ramdisk");
Andrew Scullae9962e2019-10-03 16:51:16 +0100699 ASSERT_THAT(
700 std::span(vm->smc_whitelist.smcs, vm->smc_whitelist.smc_count),
701 ElementsAre(0x32000000, 0x33001111));
702 ASSERT_FALSE(vm->smc_whitelist.permissive);
David Brazdil7a462ec2019-08-15 12:27:47 +0100703
704 vm = &m.vm[1];
David Brazdil136f2942019-09-23 14:11:03 +0100705 ASSERT_STREQ(string_data(&vm->debug_name), "first_secondary_vm");
706 ASSERT_STREQ(string_data(&vm->kernel_filename), "");
David Brazdil7a462ec2019-08-15 12:27:47 +0100707 ASSERT_EQ(vm->secondary.vcpu_count, 42);
708 ASSERT_EQ(vm->secondary.mem_size, 12345);
Andrew Scullae9962e2019-10-03 16:51:16 +0100709 ASSERT_THAT(
710 std::span(vm->smc_whitelist.smcs, vm->smc_whitelist.smc_count),
711 ElementsAre(0x04000000, 0x30002222, 0x31445566));
712 ASSERT_TRUE(vm->smc_whitelist.permissive);
David Brazdil7a462ec2019-08-15 12:27:47 +0100713
714 vm = &m.vm[2];
David Brazdil136f2942019-09-23 14:11:03 +0100715 ASSERT_STREQ(string_data(&vm->debug_name), "second_secondary_vm");
716 ASSERT_STREQ(string_data(&vm->kernel_filename),
717 "second_secondary_kernel");
David Brazdil7a462ec2019-08-15 12:27:47 +0100718 ASSERT_EQ(vm->secondary.vcpu_count, 43);
719 ASSERT_EQ(vm->secondary.mem_size, 0x12345);
Andrew Scullae9962e2019-10-03 16:51:16 +0100720 ASSERT_THAT(
721 std::span(vm->smc_whitelist.smcs, vm->smc_whitelist.smc_count),
722 IsEmpty());
723 ASSERT_FALSE(vm->smc_whitelist.permissive);
David Brazdil7a462ec2019-08-15 12:27:47 +0100724}
725
Daniel Boulby801f8ef2022-06-27 14:21:01 +0100726TEST_F(manifest, ffa_not_compatible)
Olivier Deprez62d99e32020-01-09 15:58:07 +0100727{
Daniel Boulby801f8ef2022-06-27 14:21:01 +0100728 struct_manifest m;
Olivier Deprez62d99e32020-01-09 15:58:07 +0100729
730 /* clang-format off */
731 std::vector<char> dtb = ManifestDtBuilder()
732 .Compatible({ "arm,ffa-manifest-2.0" })
733 .Property("ffa-version", "<0x10000>")
734 .Property("uuid", "<0xb4b5671e 0x4a904fe1 0xb81ffb13 0xdae1dacb>")
735 .Property("execution-ctx-count", "<1>")
736 .Property("exception-level", "<2>")
737 .Property("execution-state", "<0>")
J-Alves2f86c1e2022-02-23 18:44:19 +0000738 .Property("entrypoint-offset", "<0x00002000>")
Olivier Deprez62d99e32020-01-09 15:58:07 +0100739 .Property("xlat-granule", "<0>")
740 .Property("messaging-method", "<1>")
741 .Build();
742 /* clang-format on */
743
744 ASSERT_EQ(ffa_manifest_from_vec(&m, dtb),
745 MANIFEST_ERROR_NOT_COMPATIBLE);
746}
747
Daniel Boulby801f8ef2022-06-27 14:21:01 +0100748TEST_F(manifest, ffa_missing_property)
Olivier Deprez62d99e32020-01-09 15:58:07 +0100749{
Daniel Boulby801f8ef2022-06-27 14:21:01 +0100750 struct_manifest m;
Olivier Deprez62d99e32020-01-09 15:58:07 +0100751
752 /* clang-format off */
753 std::vector<char> dtb = ManifestDtBuilder()
754 .Compatible({ "arm,ffa-manifest-1.0" })
755 .Property("ffa-version", "<0x10000>")
756 .Build();
757 /* clang-format on */
758
759 ASSERT_EQ(ffa_manifest_from_vec(&m, dtb),
760 MANIFEST_ERROR_PROPERTY_NOT_FOUND);
761}
762
Daniel Boulby801f8ef2022-06-27 14:21:01 +0100763TEST_F(manifest, ffa_validate_sanity_check)
Olivier Deprez62d99e32020-01-09 15:58:07 +0100764{
J-Alvesb37fd082020-10-22 12:29:21 +0100765 /*
766 * TODO: write test excluding all optional fields of the manifest, in
767 * accordance with specification.
768 */
Daniel Boulby801f8ef2022-06-27 14:21:01 +0100769 struct_manifest m;
Olivier Deprez62d99e32020-01-09 15:58:07 +0100770
771 /* Incompatible version */
772 /* clang-format off */
773 std::vector<char> dtb = ManifestDtBuilder()
774 .Compatible({ "arm,ffa-manifest-1.0" })
775 .Property("ffa-version", "<0xa1>")
776 .Property("uuid", "<0xb4b5671e 0x4a904fe1 0xb81ffb13 0xdae1dacb>")
777 .Property("execution-ctx-count", "<1>")
778 .Property("exception-level", "<2>")
779 .Property("execution-state", "<0>")
J-Alves2f86c1e2022-02-23 18:44:19 +0000780 .Property("entrypoint-offset", "<0x00002000>")
Olivier Deprez62d99e32020-01-09 15:58:07 +0100781 .Property("xlat-granule", "<0>")
J-Alvesb37fd082020-10-22 12:29:21 +0100782 .Property("boot-order", "<0>")
Olivier Deprez62d99e32020-01-09 15:58:07 +0100783 .Property("messaging-method", "<1>")
784 .Build();
785 /* clang-format on */
786 ASSERT_EQ(ffa_manifest_from_vec(&m, dtb),
787 MANIFEST_ERROR_NOT_COMPATIBLE);
788
789 /* Incompatible translation granule */
790 /* clang-format off */
791 dtb = ManifestDtBuilder()
792 .Compatible({ "arm,ffa-manifest-1.0" })
793 .Property("ffa-version", "<0x10000>")
794 .Property("uuid", "<0xb4b5671e 0x4a904fe1 0xb81ffb13 0xdae1dacb>")
795 .Property("execution-ctx-count", "<1>")
796 .Property("exception-level", "<2>")
797 .Property("execution-state", "<0>")
J-Alves2f86c1e2022-02-23 18:44:19 +0000798 .Property("entrypoint-offset", "<0x00002000>")
Olivier Deprez62d99e32020-01-09 15:58:07 +0100799 .Property("xlat-granule", "<3>")
J-Alvesb37fd082020-10-22 12:29:21 +0100800 .Property("boot-order", "<0>")
Olivier Deprez62d99e32020-01-09 15:58:07 +0100801 .Property("messaging-method", "<1>")
802 .Build();
803 /* clang-format on */
804 ASSERT_EQ(ffa_manifest_from_vec(&m, dtb),
805 MANIFEST_ERROR_NOT_COMPATIBLE);
806
807 /* Incompatible exeption level */
808 /* clang-format off */
809 dtb = ManifestDtBuilder()
810 .Compatible({ "arm,ffa-manifest-1.0" })
811 .Property("ffa-version", "<0x10000>")
812 .Property("uuid", "<0xb4b5671e 0x4a904fe1 0xb81ffb13 0xdae1dacb>")
813 .Property("execution-ctx-count", "<1>")
814 .Property("exception-level", "<6>")
815 .Property("execution-state", "<0>")
J-Alves2f86c1e2022-02-23 18:44:19 +0000816 .Property("entrypoint-offset", "<0x00002000>")
Olivier Deprez62d99e32020-01-09 15:58:07 +0100817 .Property("xlat-granule", "<0>")
J-Alvesb37fd082020-10-22 12:29:21 +0100818 .Property("boot-order", "<0>")
Olivier Deprez62d99e32020-01-09 15:58:07 +0100819 .Property("messaging-method", "<1>")
820 .Build();
821 /* clang-format on */
822 ASSERT_EQ(ffa_manifest_from_vec(&m, dtb),
823 MANIFEST_ERROR_NOT_COMPATIBLE);
824
825 /* Incompatible execution state */
826 /* clang-format off */
827 dtb = ManifestDtBuilder()
828 .Compatible({ "arm,ffa-manifest-1.0" })
829 .Property("ffa-version", "<0x10000>")
830 .Property("uuid", "<0xb4b5671e 0x4a904fe1 0xb81ffb13 0xdae1dacb>")
831 .Property("execution-ctx-count", "<1>")
832 .Property("exception-level", "<2>")
833 .Property("execution-state", "<2>")
J-Alves2f86c1e2022-02-23 18:44:19 +0000834 .Property("entrypoint-offset", "<0x00002000>")
Olivier Deprez62d99e32020-01-09 15:58:07 +0100835 .Property("xlat-granule", "<0>")
J-Alvesb37fd082020-10-22 12:29:21 +0100836 .Property("boot-order", "<0>")
Olivier Deprez62d99e32020-01-09 15:58:07 +0100837 .Property("messaging-method", "<1>")
838 .Build();
839 /* clang-format on */
840 ASSERT_EQ(ffa_manifest_from_vec(&m, dtb),
841 MANIFEST_ERROR_NOT_COMPATIBLE);
842
843 /* Incompatible messaging method */
844 /* clang-format off */
845 dtb = ManifestDtBuilder()
846 .Compatible({ "arm,ffa-manifest-1.0" })
847 .Property("ffa-version", "<0x10000>")
848 .Property("uuid", "<0xb4b5671e 0x4a904fe1 0xb81ffb13 0xdae1dacb>")
849 .Property("execution-ctx-count", "<1>")
850 .Property("exception-level", "<2>")
851 .Property("execution-state", "<0>")
J-Alves2f86c1e2022-02-23 18:44:19 +0000852 .Property("entrypoint-offset", "<0x00002000>")
Olivier Deprez62d99e32020-01-09 15:58:07 +0100853 .Property("xlat-granule", "<0>")
J-Alvesb37fd082020-10-22 12:29:21 +0100854 .Property("boot-order", "<0>")
Maksims Svecovsb596eab2021-04-27 00:52:27 +0100855 .Property("messaging-method", "<16>")
Olivier Deprez62d99e32020-01-09 15:58:07 +0100856 .Build();
857 /* clang-format on */
858 ASSERT_EQ(ffa_manifest_from_vec(&m, dtb),
859 MANIFEST_ERROR_NOT_COMPATIBLE);
860}
861
Daniel Boulby801f8ef2022-06-27 14:21:01 +0100862TEST_F(manifest, ffa_validate_rxtx_info)
Manish Pandeyfa1f2912020-05-05 12:57:01 +0100863{
Daniel Boulby801f8ef2022-06-27 14:21:01 +0100864 struct_manifest m;
Manish Pandeyfa1f2912020-05-05 12:57:01 +0100865
866 /* Not Compatible */
867 /* clang-format off */
868 std::vector<char> dtb = ManifestDtBuilder()
869 .FfaValidManifest()
870 .StartChild("rx_tx-info")
871 .Compatible({ "foo,bar" })
872 .EndChild()
873 .Build();
874 /* clang-format on */
875 ASSERT_EQ(ffa_manifest_from_vec(&m, dtb),
876 MANIFEST_ERROR_NOT_COMPATIBLE);
877
878 /* Missing Properties */
879 /* clang-format off */
880 dtb = ManifestDtBuilder()
881 .FfaValidManifest()
882 .StartChild("rx_tx-info")
883 .Compatible({ "arm,ffa-manifest-rx_tx-buffer" })
884 .EndChild()
885 .Build();
886 /* clang-format on */
887 ASSERT_EQ(ffa_manifest_from_vec(&m, dtb),
888 MANIFEST_ERROR_PROPERTY_NOT_FOUND);
889}
890
Daniel Boulby801f8ef2022-06-27 14:21:01 +0100891TEST_F(manifest, ffa_validate_mem_regions)
Manish Pandey6542f5c2020-04-27 14:37:46 +0100892{
Daniel Boulby801f8ef2022-06-27 14:21:01 +0100893 struct_manifest m;
Manish Pandey6542f5c2020-04-27 14:37:46 +0100894
895 /* Not Compatible */
896 /* clang-format off */
897 std::vector<char> dtb = ManifestDtBuilder()
898 .FfaValidManifest()
899 .StartChild("memory-regions")
900 .Compatible({ "foo,bar" })
901 .EndChild()
902 .Build();
903 /* clang-format on */
904 ASSERT_EQ(ffa_manifest_from_vec(&m, dtb),
905 MANIFEST_ERROR_NOT_COMPATIBLE);
906
907 /* Memory regions unavailable */
908 /* clang-format off */
909 dtb = ManifestDtBuilder()
910 .FfaValidManifest()
911 .StartChild("memory-regions")
912 .Compatible({ "arm,ffa-manifest-memory-regions" })
913 .EndChild()
914 .Build();
915 /* clang-format on */
916 ASSERT_EQ(ffa_manifest_from_vec(&m, dtb),
917 MANIFEST_ERROR_MEMORY_REGION_NODE_EMPTY);
918
919 /* Missing Properties */
920 /* clang-format off */
921 dtb = ManifestDtBuilder()
922 .FfaValidManifest()
923 .StartChild("memory-regions")
924 .Compatible({ "arm,ffa-manifest-memory-regions" })
925 .StartChild("test-memory")
926 .Description("test-memory")
927 .EndChild()
928 .EndChild()
929 .Build();
930 /* clang-format on */
931 ASSERT_EQ(ffa_manifest_from_vec(&m, dtb),
932 MANIFEST_ERROR_PROPERTY_NOT_FOUND);
Manish Pandeyf06c9072020-09-29 15:41:58 +0100933
Daniel Boulby9279b552022-06-28 17:04:01 +0100934 /* Overlapping memory regions */
935 /* clang-format off */
936 dtb = ManifestDtBuilder()
937 .FfaValidManifest()
938 .StartChild("memory-regions")
939 .Compatible({ "arm,ffa-manifest-memory-regions" })
940 .Label("rx")
941 .StartChild("rx")
942 .Description("rx-buffer")
943 .Property("base-address", "<0x7300000>")
944 .Property("pages-count", "<1>")
945 .Property("attributes", "<1>")
946 .EndChild()
947 .Label("tx")
948 .StartChild("tx")
949 .Description("tx-buffer")
950 .Property("base-address", "<0x7300000>")
951 .Property("pages-count", "<2>")
952 .Property("attributes", "<3>")
953 .EndChild()
954 .EndChild()
955 .Build();
956 /* clang-format on */
957 ASSERT_EQ(ffa_manifest_from_vec(&m, dtb),
958 MANIFEST_ERROR_MEM_REGION_OVERLAP);
959
960 /* clang-format off */
961 dtb = ManifestDtBuilder()
962 .FfaValidManifest()
963 .StartChild("memory-regions")
964 .Compatible({ "arm,ffa-manifest-memory-regions" })
965 .Label("rx")
966 .StartChild("rx")
967 .Description("rx-buffer")
968 .Property("base-address", "<0x7300000>")
969 .Property("pages-count", "<2>")
970 .Property("attributes", "<1>")
971 .EndChild()
972 .Label("tx")
973 .StartChild("tx")
974 .Description("tx-buffer")
975 .Property("base-address", "<0x7301000>")
976 .Property("pages-count", "<2>")
977 .Property("attributes", "<3>")
978 .EndChild()
979 .EndChild()
980 .Build();
981 /* clang-format on */
982 ASSERT_EQ(ffa_manifest_from_vec(&m, dtb),
983 MANIFEST_ERROR_MEM_REGION_OVERLAP);
984
985 /* clang-format off */
986 dtb = ManifestDtBuilder()
987 .FfaValidManifest()
988 .StartChild("memory-regions")
989 .Compatible({ "arm,ffa-manifest-memory-regions" })
990 .Label("rx")
991 .StartChild("rx")
992 .Description("rx-buffer")
993 .Property("base-address", "<0x7300000>")
994 .Property("pages-count", "<2>")
995 .Property("attributes", "<1>")
996 .EndChild()
997 .Label("tx")
998 .StartChild("tx")
999 .Description("tx-buffer")
1000 .Property("base-address", "<0x7301FFF>")
1001 .Property("pages-count", "<2>")
1002 .Property("attributes", "<3>")
1003 .EndChild()
1004 .EndChild()
1005 .Build();
1006 /* clang-format on */
1007 ASSERT_EQ(ffa_manifest_from_vec(&m, dtb),
1008 MANIFEST_ERROR_MEM_REGION_OVERLAP);
1009
Manish Pandeyf06c9072020-09-29 15:41:58 +01001010 /* Different RXTX buffer sizes */
1011 /* clang-format off */
1012 dtb = ManifestDtBuilder()
1013 .FfaValidManifest()
1014 .StartChild("memory-regions")
1015 .Compatible({ "arm,ffa-manifest-memory-regions" })
1016 .Label("rx")
1017 .StartChild("rx")
1018 .Description("rx-buffer")
1019 .Property("base-address", "<0x7300000>")
1020 .Property("pages-count", "<1>")
1021 .Property("attributes", "<1>")
1022 .EndChild()
1023 .Label("tx")
1024 .StartChild("tx")
1025 .Description("tx-buffer")
1026 .Property("base-address", "<0x7310000>")
1027 .Property("pages-count", "<2>")
1028 .Property("attributes", "<3>")
1029 .EndChild()
1030 .EndChild()
1031 .StartChild("rx_tx-info")
1032 .Compatible({ "arm,ffa-manifest-rx_tx-buffer" })
1033 .Property("rx-buffer", "<&rx>")
1034 .Property("tx-buffer", "<&tx>")
1035 .EndChild()
1036 .Build();
1037 /* clang-format on */
1038 ASSERT_EQ(ffa_manifest_from_vec(&m, dtb),
1039 MANIFEST_ERROR_RXTX_SIZE_MISMATCH);
Manish Pandey6542f5c2020-04-27 14:37:46 +01001040}
1041
Daniel Boulby801f8ef2022-06-27 14:21:01 +01001042TEST_F(manifest, ffa_validate_dev_regions)
Manish Pandeye68e7932020-04-23 15:29:28 +01001043{
Daniel Boulby801f8ef2022-06-27 14:21:01 +01001044 struct_manifest m;
Manish Pandeye68e7932020-04-23 15:29:28 +01001045
1046 /* Not Compatible */
1047 /* clang-format off */
1048 std::vector<char> dtb = ManifestDtBuilder()
1049 .FfaValidManifest()
1050 .StartChild("device-regions")
1051 .Compatible({ "foo,bar" })
1052 .EndChild()
1053 .Build();
1054 /* clang-format on */
1055 ASSERT_EQ(ffa_manifest_from_vec(&m, dtb),
1056 MANIFEST_ERROR_NOT_COMPATIBLE);
1057
1058 /* Memory regions unavailable */
1059 /* clang-format off */
1060 dtb = ManifestDtBuilder()
1061 .FfaValidManifest()
1062 .StartChild("device-regions")
1063 .Compatible({ "arm,ffa-manifest-device-regions" })
1064 .EndChild()
1065 .Build();
1066 /* clang-format on */
1067 ASSERT_EQ(ffa_manifest_from_vec(&m, dtb),
1068 MANIFEST_ERROR_DEVICE_REGION_NODE_EMPTY);
1069
1070 /* Missing Properties */
1071 /* clang-format off */
1072 dtb = ManifestDtBuilder()
1073 .FfaValidManifest()
1074 .StartChild("device-regions")
1075 .Compatible({ "arm,ffa-manifest-device-regions" })
1076 .StartChild("test-device")
1077 .Description("test-device")
1078 .EndChild()
1079 .EndChild()
1080 .Build();
1081 /* clang-format on */
1082 ASSERT_EQ(ffa_manifest_from_vec(&m, dtb),
1083 MANIFEST_ERROR_PROPERTY_NOT_FOUND);
1084
1085 /* Malformed interrupt list pair */
1086 /* clang-format off */
1087 dtb = ManifestDtBuilder()
1088 .FfaValidManifest()
1089 .StartChild("device-regions")
1090 .Compatible({ "arm,ffa-manifest-device-regions" })
1091 .StartChild("test-device")
1092 .Description("test-device")
1093 .Property("base-address", "<0x7200000>")
1094 .Property("pages-count", "<16>")
1095 .Property("attributes", "<3>")
1096 .Property("smmu-id", "<1>")
1097 .Property("stream-ids", "<0 1>")
1098 .Property("interrupts", "<2 3>, <4>")
1099 .EndChild()
1100 .EndChild()
1101 .Build();
1102 /* clang-format on */
1103 ASSERT_EQ(ffa_manifest_from_vec(&m, dtb),
1104 MANIFEST_ERROR_MALFORMED_INTEGER_LIST);
Daniel Boulby667334f2022-06-27 15:23:21 +01001105
1106 /* Non-unique interrupt IDs */
1107 /* clang-format off */
1108 dtb = ManifestDtBuilder()
1109 .FfaValidManifest()
1110 .StartChild("device-regions")
1111 .Compatible({ "arm,ffa-manifest-device-regions" })
1112 .StartChild("test-device-0")
1113 .Description("test-device-0")
1114 .Property("base-address", "<0x7200000>")
1115 .Property("pages-count", "<16>")
1116 .Property("attributes", "<3>")
1117 .Property("interrupts", "<2 3>")
1118 .EndChild()
1119 .StartChild("test-device-1")
1120 .Description("test-device-1")
1121 .Property("base-address", "<0x8200000>")
1122 .Property("pages-count", "<16>")
1123 .Property("attributes", "<3>")
1124 .Property("interrupts", "<1 3>, <2 5> ")
1125 .EndChild()
1126 .EndChild()
1127 .Build();
1128 /* clang-format on */
1129 ASSERT_EQ(ffa_manifest_from_vec(&m, dtb),
1130 MANIFEST_ERROR_INTERRUPT_ID_REPEATED);
1131 /* Check valid interrupts were still mapped */
1132 ASSERT_EQ(m.vm[0].partition.dev_regions[0].interrupts[0].id, 2);
1133 ASSERT_EQ(m.vm[0].partition.dev_regions[0].interrupts[0].attributes, 3);
1134 ASSERT_EQ(m.vm[0].partition.dev_regions[1].interrupts[0].id, 1);
1135 ASSERT_EQ(m.vm[0].partition.dev_regions[1].interrupts[0].attributes, 3);
Manish Pandeye68e7932020-04-23 15:29:28 +01001136}
Daniel Boulby801f8ef2022-06-27 14:21:01 +01001137
1138TEST_F(manifest, ffa_invalid_memory_region_attributes)
Raghu Krishnamurthy384693c2021-10-11 13:56:24 -07001139{
Daniel Boulby801f8ef2022-06-27 14:21:01 +01001140 struct_manifest m;
Raghu Krishnamurthy384693c2021-10-11 13:56:24 -07001141
1142 /* clang-format off */
1143 std::vector<char> dtb = ManifestDtBuilder()
1144 .FfaValidManifest()
1145 .StartChild("rx_tx-info")
1146 .Compatible({ "arm,ffa-manifest-rx_tx-buffer" })
1147 .Property("rx-buffer", "<&rx>")
1148 .Property("tx-buffer", "<&tx>")
1149 .EndChild()
1150 .StartChild("memory-regions")
1151 .Compatible({ "arm,ffa-manifest-memory-regions" })
1152 .StartChild("test-memory")
1153 .Description("test-memory")
1154 .Property("base-address", "<0x7100000>")
1155 .Property("pages-count", "<4>")
1156 .Property("attributes", "<7>")
1157 .EndChild()
1158 .Label("rx")
1159 .StartChild("rx")
1160 .Description("rx-buffer")
1161 .Property("base-address", "<0x7300000>")
1162 .Property("pages-count", "<1>")
1163 .Property("attributes", "<1>")
1164 .EndChild()
1165 .Label("tx")
1166 .StartChild("tx")
1167 .Description("tx-buffer")
1168 .Property("base-address", "<0x7310000>")
1169 .Property("pages-count", "<1>")
1170 .Property("attributes", "<3>")
1171 .EndChild()
1172 .EndChild()
1173 .Build();
1174 /* clang-format on */
1175
1176 ASSERT_EQ(ffa_manifest_from_vec(&m, dtb),
1177 MANIFEST_ERROR_INVALID_MEM_PERM);
1178}
1179
Daniel Boulby801f8ef2022-06-27 14:21:01 +01001180TEST_F(manifest, ffa_invalid_device_region_attributes)
Raghu Krishnamurthy384693c2021-10-11 13:56:24 -07001181{
Daniel Boulby801f8ef2022-06-27 14:21:01 +01001182 struct_manifest m;
Raghu Krishnamurthy384693c2021-10-11 13:56:24 -07001183
1184 /* clang-format off */
1185 std::vector<char> dtb = ManifestDtBuilder()
1186 .FfaValidManifest()
1187 .StartChild("rx_tx-info")
1188 .Compatible({ "arm,ffa-manifest-rx_tx-buffer" })
1189 .Property("rx-buffer", "<&rx>")
1190 .Property("tx-buffer", "<&tx>")
1191 .EndChild()
1192 .StartChild("memory-regions")
1193 .Compatible({ "arm,ffa-manifest-memory-regions" })
1194 .StartChild("test-memory")
1195 .Description("test-memory")
1196 .Property("base-address", "<0x7100000>")
1197 .Property("pages-count", "<4>")
1198 .Property("attributes", "<3>")
1199 .EndChild()
1200 .Label("rx")
1201 .StartChild("rx")
1202 .Description("rx-buffer")
1203 .Property("base-address", "<0x7300000>")
1204 .Property("pages-count", "<1>")
1205 .Property("attributes", "<1>")
1206 .EndChild()
1207 .Label("tx")
1208 .StartChild("tx")
1209 .Description("tx-buffer")
1210 .Property("base-address", "<0x7310000>")
1211 .Property("pages-count", "<1>")
1212 .Property("attributes", "<3>")
1213 .EndChild()
1214 .EndChild()
1215 .StartChild("device-regions")
1216 .Compatible({ "arm,ffa-manifest-device-regions" })
1217 .StartChild("test-device")
1218 .Description("test-device")
1219 .Property("base-address", "<0x7200000>")
1220 .Property("pages-count", "<16>")
1221 .Property("attributes", "<5>")
1222 .Property("smmu-id", "<1>")
1223 .Property("stream-ids", "<0 1>")
1224 .Property("interrupts", "<2 3>, <4 5>")
1225 .EndChild()
1226 .EndChild()
1227 .Build();
1228 /* clang-format on */
1229
1230 ASSERT_EQ(ffa_manifest_from_vec(&m, dtb),
1231 MANIFEST_ERROR_INVALID_MEM_PERM);
1232}
Manish Pandeye68e7932020-04-23 15:29:28 +01001233
Daniel Boulby801f8ef2022-06-27 14:21:01 +01001234TEST_F(manifest, ffa_valid)
Olivier Deprez62d99e32020-01-09 15:58:07 +01001235{
Daniel Boulby801f8ef2022-06-27 14:21:01 +01001236 struct_manifest m;
Olivier Deprez62d99e32020-01-09 15:58:07 +01001237
1238 /* clang-format off */
1239 std::vector<char> dtb = ManifestDtBuilder()
Manish Pandeycb8fbb22020-08-18 00:04:43 +01001240 .FfaValidManifest()
Manish Pandeyfa1f2912020-05-05 12:57:01 +01001241 .StartChild("rx_tx-info")
1242 .Compatible({ "arm,ffa-manifest-rx_tx-buffer" })
1243 .Property("rx-buffer", "<&rx>")
1244 .Property("tx-buffer", "<&tx>")
1245 .EndChild()
Manish Pandey6542f5c2020-04-27 14:37:46 +01001246 .StartChild("memory-regions")
1247 .Compatible({ "arm,ffa-manifest-memory-regions" })
1248 .StartChild("test-memory")
1249 .Description("test-memory")
1250 .Property("base-address", "<0x7100000>")
1251 .Property("pages-count", "<4>")
Raghu Krishnamurthy384693c2021-10-11 13:56:24 -07001252 .Property("attributes", "<3>")
Manish Pandey6542f5c2020-04-27 14:37:46 +01001253 .EndChild()
Olivier Deprez035fa152022-03-14 11:19:10 +01001254 .StartChild("test-memory-ns")
1255 .Description("test-memory")
1256 .Property("base-address", "<0x7200000>")
1257 .Property("pages-count", "<1>")
1258 .Property("attributes", "<0xb>")
1259 .EndChild()
Manish Pandeyfa1f2912020-05-05 12:57:01 +01001260 .Label("rx")
1261 .StartChild("rx")
1262 .Description("rx-buffer")
1263 .Property("base-address", "<0x7300000>")
1264 .Property("pages-count", "<1>")
1265 .Property("attributes", "<1>")
1266 .EndChild()
1267 .Label("tx")
1268 .StartChild("tx")
1269 .Description("tx-buffer")
Daniel Boulby9279b552022-06-28 17:04:01 +01001270 .Property("base-address", "<0x7301000>")
Manish Pandeyfa1f2912020-05-05 12:57:01 +01001271 .Property("pages-count", "<1>")
1272 .Property("attributes", "<3>")
1273 .EndChild()
Manish Pandey6542f5c2020-04-27 14:37:46 +01001274 .EndChild()
Manish Pandeye68e7932020-04-23 15:29:28 +01001275 .StartChild("device-regions")
1276 .Compatible({ "arm,ffa-manifest-device-regions" })
1277 .StartChild("test-device")
1278 .Description("test-device")
Olivier Deprez035fa152022-03-14 11:19:10 +01001279 .Property("base-address", "<0x7400000>")
Manish Pandeye68e7932020-04-23 15:29:28 +01001280 .Property("pages-count", "<16>")
1281 .Property("attributes", "<3>")
1282 .Property("smmu-id", "<1>")
1283 .Property("stream-ids", "<0 1>")
1284 .Property("interrupts", "<2 3>, <4 5>")
1285 .EndChild()
Olivier Deprez035fa152022-03-14 11:19:10 +01001286 .StartChild("test-device-ns")
1287 .Description("test-device")
1288 .Property("base-address", "<0x7500000>")
1289 .Property("pages-count", "<1>")
1290 .Property("attributes", "<0x9>")
1291 .EndChild()
Manish Pandeye68e7932020-04-23 15:29:28 +01001292 .EndChild()
Olivier Deprez62d99e32020-01-09 15:58:07 +01001293 .Build();
1294 /* clang-format on */
1295
1296 ASSERT_EQ(ffa_manifest_from_vec(&m, dtb), MANIFEST_SUCCESS);
1297
Raghu Krishnamurthy8c250a92021-07-02 12:16:42 -07001298 ASSERT_EQ(m.vm[0].partition.ffa_version, 0x10000);
Olivier Deprez62d99e32020-01-09 15:58:07 +01001299 ASSERT_THAT(
Raghu Krishnamurthy8c250a92021-07-02 12:16:42 -07001300 std::span(m.vm[0].partition.uuid.uuid, 4),
Olivier Deprez62d99e32020-01-09 15:58:07 +01001301 ElementsAre(0xb4b5671e, 0x4a904fe1, 0xb81ffb13, 0xdae1dacb));
Raghu Krishnamurthy8c250a92021-07-02 12:16:42 -07001302 ASSERT_EQ(m.vm[0].partition.execution_ctx_count, 1);
1303 ASSERT_EQ(m.vm[0].partition.run_time_el, S_EL1);
1304 ASSERT_EQ(m.vm[0].partition.execution_state, AARCH64);
J-Alves2f86c1e2022-02-23 18:44:19 +00001305 ASSERT_EQ(m.vm[0].partition.ep_offset, 0x00002000);
Raghu Krishnamurthy8c250a92021-07-02 12:16:42 -07001306 ASSERT_EQ(m.vm[0].partition.xlat_granule, PAGE_4KB);
1307 ASSERT_EQ(m.vm[0].partition.boot_order, 0);
1308 ASSERT_EQ(m.vm[0].partition.messaging_method,
1309 FFA_PARTITION_INDIRECT_MSG);
1310 ASSERT_EQ(m.vm[0].partition.managed_exit, true);
1311 ASSERT_EQ(m.vm[0].partition.mem_regions[0].base_address, 0x7100000);
1312 ASSERT_EQ(m.vm[0].partition.mem_regions[0].page_count, 4);
Raghu Krishnamurthy384693c2021-10-11 13:56:24 -07001313 ASSERT_EQ(m.vm[0].partition.mem_regions[0].attributes, 3);
Olivier Deprez035fa152022-03-14 11:19:10 +01001314 ASSERT_EQ(m.vm[0].partition.mem_regions[1].attributes, (8 | 3));
Raghu Krishnamurthy8c250a92021-07-02 12:16:42 -07001315 ASSERT_EQ(m.vm[0].partition.rxtx.available, true);
1316 ASSERT_EQ(m.vm[0].partition.rxtx.rx_buffer->base_address, 0x7300000);
1317 ASSERT_EQ(m.vm[0].partition.rxtx.rx_buffer->page_count, 1);
1318 ASSERT_EQ(m.vm[0].partition.rxtx.rx_buffer->attributes, 1);
Daniel Boulby9279b552022-06-28 17:04:01 +01001319 ASSERT_EQ(m.vm[0].partition.rxtx.tx_buffer->base_address, 0x7301000);
Raghu Krishnamurthy8c250a92021-07-02 12:16:42 -07001320 ASSERT_EQ(m.vm[0].partition.rxtx.tx_buffer->page_count, 1);
1321 ASSERT_EQ(m.vm[0].partition.rxtx.tx_buffer->attributes, 3);
Olivier Deprez035fa152022-03-14 11:19:10 +01001322 ASSERT_EQ(m.vm[0].partition.dev_regions[0].base_address, 0x7400000);
Raghu Krishnamurthy8c250a92021-07-02 12:16:42 -07001323 ASSERT_EQ(m.vm[0].partition.dev_regions[0].page_count, 16);
1324
Olivier Deprez035fa152022-03-14 11:19:10 +01001325 ASSERT_EQ(m.vm[0].partition.dev_regions[0].attributes, 3);
Raghu Krishnamurthy8c250a92021-07-02 12:16:42 -07001326 ASSERT_EQ(m.vm[0].partition.dev_regions[0].smmu_id, 1);
1327 ASSERT_EQ(m.vm[0].partition.dev_regions[0].stream_ids[0], 0);
1328 ASSERT_EQ(m.vm[0].partition.dev_regions[0].stream_ids[1], 1);
1329 ASSERT_EQ(m.vm[0].partition.dev_regions[0].interrupts[0].id, 2);
1330 ASSERT_EQ(m.vm[0].partition.dev_regions[0].interrupts[0].attributes, 3);
1331 ASSERT_EQ(m.vm[0].partition.dev_regions[0].interrupts[1].id, 4);
1332 ASSERT_EQ(m.vm[0].partition.dev_regions[0].interrupts[1].attributes, 5);
Olivier Deprez035fa152022-03-14 11:19:10 +01001333 ASSERT_EQ(m.vm[0].partition.dev_regions[1].attributes, (8 | 1));
Olivier Deprez62d99e32020-01-09 15:58:07 +01001334}
1335
David Brazdil7a462ec2019-08-15 12:27:47 +01001336} /* namespace */