blob: c5444546f03a82fb2c807b22b2b25b89f4ff2e18 [file] [log] [blame]
Andrew Scull18834872018-10-12 11:48:09 +01001/*
2 * Copyright 2018 Google LLC
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
Andrew Walbran679cdf42018-09-18 15:36:17 +010017extern "C" {
18#include "hf/fdt_handler.h"
19
Andrew Walbran0ea8ced2018-10-03 14:56:58 +010020#include "hf/alloc.h"
Andrew Walbran679cdf42018-09-18 15:36:17 +010021#include "hf/boot_params.h"
22}
23
Andrew Walbran0ea8ced2018-10-03 14:56:58 +010024#include <memory>
25
Andrew Walbran679cdf42018-09-18 15:36:17 +010026#include <gmock/gmock.h>
27
Andrew Scull232d5602018-10-15 11:07:45 +010028namespace
29{
Andrew Walbran679cdf42018-09-18 15:36:17 +010030using ::testing::Eq;
Andrew Scullb401ba32018-11-09 10:30:54 +000031using ::testing::NotNull;
Andrew Walbran679cdf42018-09-18 15:36:17 +010032
Andrew Scull232d5602018-10-15 11:07:45 +010033constexpr size_t TEST_HEAP_SIZE = PAGE_SIZE * 10;
Andrew Walbran0ea8ced2018-10-03 14:56:58 +010034
Andrew Walbran679cdf42018-09-18 15:36:17 +010035/*
36 * /dts-v1/;
37 *
38 * / {
39 * #address-cells = <2>;
40 * #size-cells = <2>;
41 *
42 * memory@0 {
43 * device_type = "memory";
44 * reg = <0x00000000 0x00000000 0x00000000 0x20000000
45 * 0x00000000 0x30000000 0x00000000 0x00010000>;
46 * };
47 * memory@1 {
48 * device_type = "memory";
49 * reg = <0x00000000 0x30020000 0x00000000 0x00010000>;
50 * };
51 *
52 * chosen {
53 * linux,initrd-start = <0x00000000>;
54 * linux,initrd-end = <0x00000000>;
55 * };
56 * };
57 *
58 * $ dtc --boot-cpu 0 --in-format dts --out-format dtb --out-version 17 test.dts
59 * | xxd -i
60 */
61
Andrew Scull232d5602018-10-15 11:07:45 +010062constexpr uint8_t test_dtb[] = {
Andrew Walbran679cdf42018-09-18 15:36:17 +010063 0xd0, 0x0d, 0xfe, 0xed, 0x00, 0x00, 0x01, 0x7f, 0x00, 0x00, 0x00, 0x38,
64 0x00, 0x00, 0x01, 0x30, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x11,
65 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4f,
66 0x00, 0x00, 0x00, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
67 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
68 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x04,
69 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x03,
70 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x02,
71 0x00, 0x00, 0x00, 0x01, 0x6d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x40, 0x30,
72 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x07,
73 0x00, 0x00, 0x00, 0x1b, 0x6d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x00, 0x00,
74 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x27,
75 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
76 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00,
77 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02,
78 0x00, 0x00, 0x00, 0x01, 0x6d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x40, 0x31,
79 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x07,
80 0x00, 0x00, 0x00, 0x1b, 0x6d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x00, 0x00,
81 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x27,
82 0x00, 0x00, 0x00, 0x00, 0x30, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
83 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01,
84 0x63, 0x68, 0x6f, 0x73, 0x65, 0x6e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
85 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x2b, 0x00, 0x00, 0x00, 0x00,
86 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x3e,
87 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x02,
88 0x00, 0x00, 0x00, 0x09, 0x23, 0x61, 0x64, 0x64, 0x72, 0x65, 0x73, 0x73,
89 0x2d, 0x63, 0x65, 0x6c, 0x6c, 0x73, 0x00, 0x23, 0x73, 0x69, 0x7a, 0x65,
90 0x2d, 0x63, 0x65, 0x6c, 0x6c, 0x73, 0x00, 0x64, 0x65, 0x76, 0x69, 0x63,
91 0x65, 0x5f, 0x74, 0x79, 0x70, 0x65, 0x00, 0x72, 0x65, 0x67, 0x00, 0x6c,
92 0x69, 0x6e, 0x75, 0x78, 0x2c, 0x69, 0x6e, 0x69, 0x74, 0x72, 0x64, 0x2d,
93 0x73, 0x74, 0x61, 0x72, 0x74, 0x00, 0x6c, 0x69, 0x6e, 0x75, 0x78, 0x2c,
94 0x69, 0x6e, 0x69, 0x74, 0x72, 0x64, 0x2d, 0x65, 0x6e, 0x64, 0x00};
95
Andrew Scullb401ba32018-11-09 10:30:54 +000096TEST(fdt, find_memory_ranges)
Andrew Walbran679cdf42018-09-18 15:36:17 +010097{
Andrew Walbran0ea8ced2018-10-03 14:56:58 +010098 std::unique_ptr<uint8_t[]> test_heap(new uint8_t[TEST_HEAP_SIZE]);
99 halloc_init((size_t)test_heap.get(), TEST_HEAP_SIZE);
100 ASSERT_TRUE(mm_init());
101
Andrew Scullb401ba32018-11-09 10:30:54 +0000102 struct fdt_header *fdt;
103 struct fdt_node n;
Andrew Walbran0ea8ced2018-10-03 14:56:58 +0100104 struct boot_params params = {};
Andrew Scullb401ba32018-11-09 10:30:54 +0000105
106 fdt = fdt_map(pa_init((uintpaddr_t)&test_dtb), &n);
107 ASSERT_THAT(fdt, NotNull());
108 ASSERT_TRUE(fdt_find_child(&n, ""));
109 fdt_find_memory_ranges(&n, &params);
110 ASSERT_TRUE(fdt_unmap(fdt));
111
Andrew Walbran679cdf42018-09-18 15:36:17 +0100112 EXPECT_THAT(params.mem_ranges_count, Eq(3));
113 EXPECT_THAT(pa_addr(params.mem_ranges[0].begin), Eq(0x00000000));
114 EXPECT_THAT(pa_addr(params.mem_ranges[0].end), Eq(0x20000000));
115 EXPECT_THAT(pa_addr(params.mem_ranges[1].begin), Eq(0x30000000));
116 EXPECT_THAT(pa_addr(params.mem_ranges[1].end), Eq(0x30010000));
117 EXPECT_THAT(pa_addr(params.mem_ranges[2].begin), Eq(0x30020000));
118 EXPECT_THAT(pa_addr(params.mem_ranges[2].end), Eq(0x30030000));
119}
Andrew Scull232d5602018-10-15 11:07:45 +0100120
121} /* namespace */