blob: c3fbb92bc6eadb2e4cf3c34eec90240c0fd9bd7d [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
28using ::testing::Eq;
29
Andrew Walbran0ea8ced2018-10-03 14:56:58 +010030static constexpr size_t TEST_HEAP_SIZE = PAGE_SIZE * 10;
31
Andrew Walbran679cdf42018-09-18 15:36:17 +010032/*
33 * /dts-v1/;
34 *
35 * / {
36 * #address-cells = <2>;
37 * #size-cells = <2>;
38 *
39 * memory@0 {
40 * device_type = "memory";
41 * reg = <0x00000000 0x00000000 0x00000000 0x20000000
42 * 0x00000000 0x30000000 0x00000000 0x00010000>;
43 * };
44 * memory@1 {
45 * device_type = "memory";
46 * reg = <0x00000000 0x30020000 0x00000000 0x00010000>;
47 * };
48 *
49 * chosen {
50 * linux,initrd-start = <0x00000000>;
51 * linux,initrd-end = <0x00000000>;
52 * };
53 * };
54 *
55 * $ dtc --boot-cpu 0 --in-format dts --out-format dtb --out-version 17 test.dts
56 * | xxd -i
57 */
58
Andrew Walbran0ea8ced2018-10-03 14:56:58 +010059static constexpr uint8_t test_dtb[] = {
Andrew Walbran679cdf42018-09-18 15:36:17 +010060 0xd0, 0x0d, 0xfe, 0xed, 0x00, 0x00, 0x01, 0x7f, 0x00, 0x00, 0x00, 0x38,
61 0x00, 0x00, 0x01, 0x30, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x11,
62 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4f,
63 0x00, 0x00, 0x00, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
64 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
65 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x04,
66 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x03,
67 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x02,
68 0x00, 0x00, 0x00, 0x01, 0x6d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x40, 0x30,
69 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x07,
70 0x00, 0x00, 0x00, 0x1b, 0x6d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x00, 0x00,
71 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x27,
72 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
73 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00,
74 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02,
75 0x00, 0x00, 0x00, 0x01, 0x6d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x40, 0x31,
76 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x07,
77 0x00, 0x00, 0x00, 0x1b, 0x6d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x00, 0x00,
78 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x27,
79 0x00, 0x00, 0x00, 0x00, 0x30, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
80 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01,
81 0x63, 0x68, 0x6f, 0x73, 0x65, 0x6e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
82 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x2b, 0x00, 0x00, 0x00, 0x00,
83 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x3e,
84 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x02,
85 0x00, 0x00, 0x00, 0x09, 0x23, 0x61, 0x64, 0x64, 0x72, 0x65, 0x73, 0x73,
86 0x2d, 0x63, 0x65, 0x6c, 0x6c, 0x73, 0x00, 0x23, 0x73, 0x69, 0x7a, 0x65,
87 0x2d, 0x63, 0x65, 0x6c, 0x6c, 0x73, 0x00, 0x64, 0x65, 0x76, 0x69, 0x63,
88 0x65, 0x5f, 0x74, 0x79, 0x70, 0x65, 0x00, 0x72, 0x65, 0x67, 0x00, 0x6c,
89 0x69, 0x6e, 0x75, 0x78, 0x2c, 0x69, 0x6e, 0x69, 0x74, 0x72, 0x64, 0x2d,
90 0x73, 0x74, 0x61, 0x72, 0x74, 0x00, 0x6c, 0x69, 0x6e, 0x75, 0x78, 0x2c,
91 0x69, 0x6e, 0x69, 0x74, 0x72, 0x64, 0x2d, 0x65, 0x6e, 0x64, 0x00};
92
93TEST(fdt, get_boot_params)
94{
Andrew Walbran0ea8ced2018-10-03 14:56:58 +010095 std::unique_ptr<uint8_t[]> test_heap(new uint8_t[TEST_HEAP_SIZE]);
96 halloc_init((size_t)test_heap.get(), TEST_HEAP_SIZE);
97 ASSERT_TRUE(mm_init());
98
99 struct boot_params params = {};
Andrew Walbran679cdf42018-09-18 15:36:17 +0100100 EXPECT_TRUE(
101 fdt_get_boot_params(pa_init((uintpaddr_t)&test_dtb), &params));
102 EXPECT_THAT(params.mem_ranges_count, Eq(3));
103 EXPECT_THAT(pa_addr(params.mem_ranges[0].begin), Eq(0x00000000));
104 EXPECT_THAT(pa_addr(params.mem_ranges[0].end), Eq(0x20000000));
105 EXPECT_THAT(pa_addr(params.mem_ranges[1].begin), Eq(0x30000000));
106 EXPECT_THAT(pa_addr(params.mem_ranges[1].end), Eq(0x30010000));
107 EXPECT_THAT(pa_addr(params.mem_ranges[2].begin), Eq(0x30020000));
108 EXPECT_THAT(pa_addr(params.mem_ranges[2].end), Eq(0x30030000));
109}