blob: 851facdd39c26c5676c6f16a226a4083ef82d4c9 [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 Scull18c78fc2018-08-20 12:57:41 +010017#include "hf/fdt_handler.h"
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010018
Andrew Scull18c78fc2018-08-20 12:57:41 +010019#include "hf/boot_params.h"
20#include "hf/dlog.h"
21#include "hf/fdt.h"
Andrew Scull5991ec92018-10-08 14:55:02 +010022#include "hf/layout.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010023#include "hf/mm.h"
24#include "hf/std.h"
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010025
26static uint64_t convert_number(const char *data, uint32_t size)
27{
28 union {
29 volatile uint64_t v;
30 char a[8];
31 } t;
32
33 switch (size) {
34 case sizeof(uint32_t):
35 return be32toh(*(uint32_t *)data);
36 case sizeof(uint64_t):
37 memcpy(t.a, data, sizeof(uint64_t));
38 return be64toh(t.v);
39 default:
40 return 0;
41 }
42}
43
44static bool fdt_read_number(const struct fdt_node *node, const char *name,
45 uint64_t *value)
46{
47 const char *data;
48 uint32_t size;
49
50 if (!fdt_read_property(node, name, &data, &size)) {
51 return false;
52 }
53
54 switch (size) {
55 case sizeof(uint32_t):
56 case sizeof(uint64_t):
57 *value = convert_number(data, size);
58 break;
59
60 default:
61 return false;
62 }
63
64 return true;
65}
66
67static bool fdt_write_number(struct fdt_node *node, const char *name,
68 uint64_t value)
69{
70 const char *data;
71 uint32_t size;
72 union {
73 volatile uint64_t v;
74 char a[8];
75 } t;
76
77 if (!fdt_read_property(node, name, &data, &size)) {
78 return false;
79 }
80
81 switch (size) {
82 case sizeof(uint32_t):
83 *(uint32_t *)data = be32toh(value);
84 break;
85
86 case sizeof(uint64_t):
87 t.v = be64toh(value);
88 memcpy((void *)data, t.a, sizeof(uint64_t));
89 break;
90
91 default:
92 return false;
93 }
94
95 return true;
96}
97
98/**
Andrew Walbranfb88f2c2018-09-18 15:23:49 +010099 * Finds the memory region where initrd is stored, and updates the fdt node
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100100 * cursor to the node called "chosen".
101 */
102static bool find_initrd(struct fdt_node *n, struct boot_params *p)
103{
Andrew Scull265ada92018-07-30 15:19:01 +0100104 uint64_t begin;
105 uint64_t end;
106
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100107 if (!fdt_find_child(n, "chosen")) {
108 dlog("Unable to find 'chosen'\n");
109 return false;
110 }
111
Andrew Scull265ada92018-07-30 15:19:01 +0100112 if (!fdt_read_number(n, "linux,initrd-start", &begin)) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100113 dlog("Unable to read linux,initrd-start\n");
114 return false;
115 }
116
Andrew Scull265ada92018-07-30 15:19:01 +0100117 if (!fdt_read_number(n, "linux,initrd-end", &end)) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100118 dlog("Unable to read linux,initrd-end\n");
119 return false;
120 }
121
Andrew Scull265ada92018-07-30 15:19:01 +0100122 p->initrd_begin = pa_init(begin);
123 p->initrd_end = pa_init(end);
124
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100125 return true;
126}
127
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100128static void find_memory_ranges(const struct fdt_node *root,
129 struct boot_params *p)
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100130{
131 struct fdt_node n = *root;
132 const char *name;
133 uint64_t address_size;
134 uint64_t size_size;
135 uint64_t entry_size;
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100136 size_t mem_range_index = 0;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100137
138 /* Get the sizes of memory range addresses and sizes. */
139 if (fdt_read_number(&n, "#address-cells", &address_size)) {
140 address_size *= sizeof(uint32_t);
141 } else {
142 address_size = sizeof(uint32_t);
143 }
144
145 if (fdt_read_number(&n, "#size-cells", &size_size)) {
146 size_size *= sizeof(uint32_t);
147 } else {
148 size_size = sizeof(uint32_t);
149 }
150
151 entry_size = address_size + size_size;
152
153 /* Look for nodes with the device_type set to "memory". */
154 if (!fdt_first_child(&n, &name)) {
155 return;
156 }
157
158 do {
159 const char *data;
160 uint32_t size;
161 if (!fdt_read_property(&n, "device_type", &data, &size) ||
162 size != sizeof("memory") ||
163 memcmp(data, "memory", sizeof("memory")) != 0 ||
164 !fdt_read_property(&n, "reg", &data, &size)) {
165 continue;
166 }
167
168 /* Traverse all memory ranges within this node. */
169 while (size >= entry_size) {
Andrew Scull265ada92018-07-30 15:19:01 +0100170 uintpaddr_t addr = convert_number(data, address_size);
171 size_t len =
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100172 convert_number(data + address_size, size_size);
173
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100174 if (mem_range_index < MAX_MEM_RANGES) {
175 p->mem_ranges[mem_range_index].begin =
176 pa_init(addr);
177 p->mem_ranges[mem_range_index].end =
178 pa_init(addr + len);
179 ++mem_range_index;
180 } else {
181 dlog("Found memory range %u in FDT but only "
182 "%u supported, ignoring additional range "
183 "of size %u.\n",
184 mem_range_index, MAX_MEM_RANGES, len);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100185 }
186
187 size -= entry_size;
188 data += entry_size;
189 }
190 } while (fdt_next_sibling(&n, &name));
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100191 p->mem_ranges_count = mem_range_index;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100192
193 /* TODO: Check for "reserved-memory" nodes. */
194}
195
Andrew Scull265ada92018-07-30 15:19:01 +0100196bool fdt_get_boot_params(paddr_t fdt_addr, struct boot_params *p)
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100197{
Andrew Scull265ada92018-07-30 15:19:01 +0100198 struct fdt_header *fdt;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100199 struct fdt_node n;
200 bool ret = false;
201
202 /* Map the fdt header in. */
Andrew Scull80871322018-08-06 12:04:09 +0100203 fdt = mm_identity_map(fdt_addr, pa_add(fdt_addr, fdt_header_size()),
204 MM_MODE_R);
205 if (!fdt) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100206 dlog("Unable to map FDT header.\n");
207 goto err_unmap_fdt_header;
208 }
209
210 if (!fdt_root_node(&n, fdt)) {
211 dlog("FDT failed validation.\n");
212 goto err_unmap_fdt_header;
213 }
214
215 /* Map the rest of the fdt in. */
Andrew Scull80871322018-08-06 12:04:09 +0100216 fdt = mm_identity_map(fdt_addr, pa_add(fdt_addr, fdt_total_size(fdt)),
217 MM_MODE_R);
218 if (!fdt) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100219 dlog("Unable to map full FDT.\n");
220 goto err_unmap_fdt_header;
221 }
222
223 if (!fdt_find_child(&n, "")) {
224 dlog("Unable to find FDT root node.\n");
225 goto out_unmap_fdt;
226 }
227
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100228 p->mem_ranges_count = 0;
229 find_memory_ranges(&n, p);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100230
231 if (!find_initrd(&n, p)) {
232 goto out_unmap_fdt;
233 }
234
235 p->kernel_arg = (size_t)fdt;
236 ret = true;
237
238out_unmap_fdt:
Andrew Scull80871322018-08-06 12:04:09 +0100239 mm_unmap(fdt_addr, pa_add(fdt_addr, fdt_total_size(fdt)), 0);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100240 return ret;
241
242err_unmap_fdt_header:
Andrew Scull80871322018-08-06 12:04:09 +0100243 mm_unmap(fdt_addr, pa_add(fdt_addr, fdt_header_size()), 0);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100244 return false;
245}
246
Andrew Scull265ada92018-07-30 15:19:01 +0100247bool fdt_patch(paddr_t fdt_addr, struct boot_params_update *p)
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100248{
Andrew Scull265ada92018-07-30 15:19:01 +0100249 struct fdt_header *fdt;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100250 struct fdt_node n;
251 bool ret = false;
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100252 size_t i;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100253
254 /* Map the fdt header in. */
Andrew Scull80871322018-08-06 12:04:09 +0100255 fdt = mm_identity_map(fdt_addr, pa_add(fdt_addr, fdt_header_size()),
256 MM_MODE_R);
257 if (!fdt) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100258 dlog("Unable to map FDT header.\n");
259 return false;
260 }
261
262 if (!fdt_root_node(&n, fdt)) {
263 dlog("FDT failed validation.\n");
264 goto err_unmap_fdt_header;
265 }
266
267 /* Map the fdt (+ a page) in r/w mode in preparation for updating it. */
Andrew Scull80871322018-08-06 12:04:09 +0100268 fdt = mm_identity_map(fdt_addr,
269 pa_add(fdt_addr, fdt_total_size(fdt) + PAGE_SIZE),
270 MM_MODE_R | MM_MODE_W);
271 if (!fdt) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100272 dlog("Unable to map FDT in r/w mode.\n");
273 goto err_unmap_fdt_header;
274 }
275
276 if (!fdt_find_child(&n, "")) {
277 dlog("Unable to find FDT root node.\n");
278 goto out_unmap_fdt;
279 }
280
281 if (!fdt_find_child(&n, "chosen")) {
282 dlog("Unable to find 'chosen'\n");
283 goto out_unmap_fdt;
284 }
285
286 /* Patch FDT to point to new ramdisk. */
Andrew Scull265ada92018-07-30 15:19:01 +0100287 if (!fdt_write_number(&n, "linux,initrd-start",
288 pa_addr(p->initrd_begin))) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100289 dlog("Unable to write linux,initrd-start\n");
290 goto out_unmap_fdt;
291 }
292
Andrew Scull265ada92018-07-30 15:19:01 +0100293 if (!fdt_write_number(&n, "linux,initrd-end", pa_addr(p->initrd_end))) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100294 dlog("Unable to write linux,initrd-end\n");
295 goto out_unmap_fdt;
296 }
297
298 /* Patch fdt to reserve primary VM memory. */
Andrew Scull5991ec92018-10-08 14:55:02 +0100299 fdt_add_mem_reservation(fdt, pa_addr(layout_primary_begin()) & ~0xfffff,
300 0x80000);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100301
302 /* Patch fdt to reserve memory for secondary VMs. */
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100303 for (i = 0; i < p->reserved_ranges_count; ++i) {
304 fdt_add_mem_reservation(
305 fdt, pa_addr(p->reserved_ranges[i].begin),
306 pa_addr(p->reserved_ranges[i].end) -
307 pa_addr(p->reserved_ranges[i].begin));
308 }
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100309
310 ret = true;
311
312out_unmap_fdt:
313 /* Unmap FDT. */
Andrew Scull80871322018-08-06 12:04:09 +0100314 if (!mm_unmap(fdt_addr,
315 pa_add(fdt_addr, fdt_total_size(fdt) + PAGE_SIZE), 0)) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100316 dlog("Unable to unmap writable FDT.\n");
317 return false;
318 }
319 return ret;
320
321err_unmap_fdt_header:
Andrew Scull80871322018-08-06 12:04:09 +0100322 mm_unmap(fdt_addr, pa_add(fdt_addr, fdt_header_size()), 0);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100323 return false;
324}