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