Andrew Scull | 1883487 | 2018-10-12 11:48:09 +0100 | [diff] [blame] | 1 | /* |
Andrew Walbran | 692b325 | 2019-03-07 15:51:31 +0000 | [diff] [blame] | 2 | * Copyright 2018 The Hafnium Authors. |
Andrew Scull | 1883487 | 2018-10-12 11:48:09 +0100 | [diff] [blame] | 3 | * |
Andrew Walbran | e959ec1 | 2020-06-17 15:01:09 +0100 | [diff] [blame^] | 4 | * 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. |
Andrew Scull | 1883487 | 2018-10-12 11:48:09 +0100 | [diff] [blame] | 7 | */ |
| 8 | |
Wedson Almeida Filho | 22d5eaa | 2018-12-16 00:38:49 +0000 | [diff] [blame] | 9 | #include <gmock/gmock.h> |
Andrew Walbran | 679cdf4 | 2018-09-18 15:36:17 +0100 | [diff] [blame] | 10 | |
Wedson Almeida Filho | 22d5eaa | 2018-12-16 00:38:49 +0000 | [diff] [blame] | 11 | extern "C" { |
Andrew Walbran | 679cdf4 | 2018-09-18 15:36:17 +0100 | [diff] [blame] | 12 | #include "hf/boot_params.h" |
Wedson Almeida Filho | 22d5eaa | 2018-12-16 00:38:49 +0000 | [diff] [blame] | 13 | #include "hf/fdt_handler.h" |
| 14 | #include "hf/mpool.h" |
Andrew Walbran | 679cdf4 | 2018-09-18 15:36:17 +0100 | [diff] [blame] | 15 | } |
| 16 | |
Andrew Walbran | 0ea8ced | 2018-10-03 14:56:58 +0100 | [diff] [blame] | 17 | #include <memory> |
| 18 | |
Andrew Scull | 232d560 | 2018-10-15 11:07:45 +0100 | [diff] [blame] | 19 | namespace |
| 20 | { |
Andrew Walbran | 679cdf4 | 2018-09-18 15:36:17 +0100 | [diff] [blame] | 21 | using ::testing::Eq; |
Andrew Scull | b401ba3 | 2018-11-09 10:30:54 +0000 | [diff] [blame] | 22 | using ::testing::NotNull; |
Andrew Walbran | 679cdf4 | 2018-09-18 15:36:17 +0100 | [diff] [blame] | 23 | |
Andrew Scull | 232d560 | 2018-10-15 11:07:45 +0100 | [diff] [blame] | 24 | constexpr size_t TEST_HEAP_SIZE = PAGE_SIZE * 10; |
Andrew Walbran | 0ea8ced | 2018-10-03 14:56:58 +0100 | [diff] [blame] | 25 | |
Andrew Walbran | 679cdf4 | 2018-09-18 15:36:17 +0100 | [diff] [blame] | 26 | /* |
| 27 | * /dts-v1/; |
| 28 | * |
| 29 | * / { |
| 30 | * #address-cells = <2>; |
| 31 | * #size-cells = <2>; |
| 32 | * |
| 33 | * memory@0 { |
| 34 | * device_type = "memory"; |
| 35 | * reg = <0x00000000 0x00000000 0x00000000 0x20000000 |
| 36 | * 0x00000000 0x30000000 0x00000000 0x00010000>; |
| 37 | * }; |
| 38 | * memory@1 { |
| 39 | * device_type = "memory"; |
| 40 | * reg = <0x00000000 0x30020000 0x00000000 0x00010000>; |
| 41 | * }; |
| 42 | * |
| 43 | * chosen { |
| 44 | * linux,initrd-start = <0x00000000>; |
| 45 | * linux,initrd-end = <0x00000000>; |
| 46 | * }; |
| 47 | * }; |
| 48 | * |
| 49 | * $ dtc --boot-cpu 0 --in-format dts --out-format dtb --out-version 17 test.dts |
| 50 | * | xxd -i |
| 51 | */ |
| 52 | |
Andrew Scull | 232d560 | 2018-10-15 11:07:45 +0100 | [diff] [blame] | 53 | constexpr uint8_t test_dtb[] = { |
Andrew Walbran | 679cdf4 | 2018-09-18 15:36:17 +0100 | [diff] [blame] | 54 | 0xd0, 0x0d, 0xfe, 0xed, 0x00, 0x00, 0x01, 0x7f, 0x00, 0x00, 0x00, 0x38, |
| 55 | 0x00, 0x00, 0x01, 0x30, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x11, |
| 56 | 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4f, |
| 57 | 0x00, 0x00, 0x00, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 58 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, |
| 59 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x04, |
| 60 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x03, |
| 61 | 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x02, |
| 62 | 0x00, 0x00, 0x00, 0x01, 0x6d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x40, 0x30, |
| 63 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x07, |
| 64 | 0x00, 0x00, 0x00, 0x1b, 0x6d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x00, 0x00, |
| 65 | 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x27, |
| 66 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 67 | 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, |
| 68 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, |
| 69 | 0x00, 0x00, 0x00, 0x01, 0x6d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x40, 0x31, |
| 70 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x07, |
| 71 | 0x00, 0x00, 0x00, 0x1b, 0x6d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x00, 0x00, |
| 72 | 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x27, |
| 73 | 0x00, 0x00, 0x00, 0x00, 0x30, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 74 | 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, |
| 75 | 0x63, 0x68, 0x6f, 0x73, 0x65, 0x6e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, |
| 76 | 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x2b, 0x00, 0x00, 0x00, 0x00, |
| 77 | 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x3e, |
| 78 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x02, |
| 79 | 0x00, 0x00, 0x00, 0x09, 0x23, 0x61, 0x64, 0x64, 0x72, 0x65, 0x73, 0x73, |
| 80 | 0x2d, 0x63, 0x65, 0x6c, 0x6c, 0x73, 0x00, 0x23, 0x73, 0x69, 0x7a, 0x65, |
| 81 | 0x2d, 0x63, 0x65, 0x6c, 0x6c, 0x73, 0x00, 0x64, 0x65, 0x76, 0x69, 0x63, |
| 82 | 0x65, 0x5f, 0x74, 0x79, 0x70, 0x65, 0x00, 0x72, 0x65, 0x67, 0x00, 0x6c, |
| 83 | 0x69, 0x6e, 0x75, 0x78, 0x2c, 0x69, 0x6e, 0x69, 0x74, 0x72, 0x64, 0x2d, |
| 84 | 0x73, 0x74, 0x61, 0x72, 0x74, 0x00, 0x6c, 0x69, 0x6e, 0x75, 0x78, 0x2c, |
| 85 | 0x69, 0x6e, 0x69, 0x74, 0x72, 0x64, 0x2d, 0x65, 0x6e, 0x64, 0x00}; |
| 86 | |
Andrew Scull | b401ba3 | 2018-11-09 10:30:54 +0000 | [diff] [blame] | 87 | TEST(fdt, find_memory_ranges) |
Andrew Walbran | 679cdf4 | 2018-09-18 15:36:17 +0100 | [diff] [blame] | 88 | { |
Wedson Almeida Filho | 22d5eaa | 2018-12-16 00:38:49 +0000 | [diff] [blame] | 89 | struct mpool ppool; |
Andrew Walbran | 0ea8ced | 2018-10-03 14:56:58 +0100 | [diff] [blame] | 90 | std::unique_ptr<uint8_t[]> test_heap(new uint8_t[TEST_HEAP_SIZE]); |
Wedson Almeida Filho | 22d5eaa | 2018-12-16 00:38:49 +0000 | [diff] [blame] | 91 | |
| 92 | mpool_init(&ppool, sizeof(struct mm_page_table)); |
| 93 | mpool_add_chunk(&ppool, test_heap.get(), TEST_HEAP_SIZE); |
Andrew Scull | b291056 | 2019-09-17 14:08:27 +0100 | [diff] [blame] | 94 | mm_init(&ppool); |
Andrew Walbran | 0ea8ced | 2018-10-03 14:56:58 +0100 | [diff] [blame] | 95 | |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame] | 96 | struct fdt fdt; |
Andrew Walbran | 0ea8ced | 2018-10-03 14:56:58 +0100 | [diff] [blame] | 97 | struct boot_params params = {}; |
Andrew Scull | b401ba3 | 2018-11-09 10:30:54 +0000 | [diff] [blame] | 98 | |
Andrew Scull | 3c0a90a | 2019-07-01 11:55:53 +0100 | [diff] [blame] | 99 | struct mm_stage1_locked mm_stage1_locked = mm_lock_stage1(); |
Andrew Scull | 6c9a4ab | 2020-01-27 17:09:12 +0000 | [diff] [blame] | 100 | struct string memory = STRING_INIT("memory"); |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame] | 101 | ASSERT_TRUE(fdt_map(&fdt, mm_stage1_locked, |
| 102 | pa_init((uintpaddr_t)&test_dtb), &ppool)); |
| 103 | fdt_find_memory_ranges(&fdt, &memory, params.mem_ranges, |
Andrew Scull | 6c9a4ab | 2020-01-27 17:09:12 +0000 | [diff] [blame] | 104 | ¶ms.mem_ranges_count, MAX_MEM_RANGES); |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame] | 105 | ASSERT_TRUE(fdt_unmap(&fdt, mm_stage1_locked, &ppool)); |
Andrew Scull | 3c0a90a | 2019-07-01 11:55:53 +0100 | [diff] [blame] | 106 | mm_unlock_stage1(&mm_stage1_locked); |
Andrew Scull | b401ba3 | 2018-11-09 10:30:54 +0000 | [diff] [blame] | 107 | |
Andrew Walbran | 679cdf4 | 2018-09-18 15:36:17 +0100 | [diff] [blame] | 108 | EXPECT_THAT(params.mem_ranges_count, Eq(3)); |
| 109 | EXPECT_THAT(pa_addr(params.mem_ranges[0].begin), Eq(0x00000000)); |
| 110 | EXPECT_THAT(pa_addr(params.mem_ranges[0].end), Eq(0x20000000)); |
| 111 | EXPECT_THAT(pa_addr(params.mem_ranges[1].begin), Eq(0x30000000)); |
| 112 | EXPECT_THAT(pa_addr(params.mem_ranges[1].end), Eq(0x30010000)); |
| 113 | EXPECT_THAT(pa_addr(params.mem_ranges[2].begin), Eq(0x30020000)); |
| 114 | EXPECT_THAT(pa_addr(params.mem_ranges[2].end), Eq(0x30030000)); |
| 115 | } |
Andrew Scull | 232d560 | 2018-10-15 11:07:45 +0100 | [diff] [blame] | 116 | |
| 117 | } /* namespace */ |