blob: cba2a4e909075b7a841e43ee2a633947047d92f5 [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 */
Andrew Scullb401ba32018-11-09 10:30:54 +0000102bool fdt_find_initrd(struct fdt_node *n, paddr_t *begin, paddr_t *end)
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100103{
Andrew Scullb401ba32018-11-09 10:30:54 +0000104 uint64_t initrd_begin;
105 uint64_t initrd_end;
Andrew Scull265ada92018-07-30 15:19:01 +0100106
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 Scullb401ba32018-11-09 10:30:54 +0000112 if (!fdt_read_number(n, "linux,initrd-start", &initrd_begin)) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100113 dlog("Unable to read linux,initrd-start\n");
114 return false;
115 }
116
Andrew Scullb401ba32018-11-09 10:30:54 +0000117 if (!fdt_read_number(n, "linux,initrd-end", &initrd_end)) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100118 dlog("Unable to read linux,initrd-end\n");
119 return false;
120 }
121
Andrew Scullb401ba32018-11-09 10:30:54 +0000122 *begin = pa_init(initrd_begin);
123 *end = pa_init(initrd_end);
Andrew Scull265ada92018-07-30 15:19:01 +0100124
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100125 return true;
126}
127
Andrew Scullb401ba32018-11-09 10:30:54 +0000128void fdt_find_memory_ranges(const struct fdt_node *root, struct boot_params *p)
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100129{
130 struct fdt_node n = *root;
131 const char *name;
132 uint64_t address_size;
133 uint64_t size_size;
134 uint64_t entry_size;
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100135 size_t mem_range_index = 0;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100136
137 /* Get the sizes of memory range addresses and sizes. */
138 if (fdt_read_number(&n, "#address-cells", &address_size)) {
139 address_size *= sizeof(uint32_t);
140 } else {
141 address_size = sizeof(uint32_t);
142 }
143
144 if (fdt_read_number(&n, "#size-cells", &size_size)) {
145 size_size *= sizeof(uint32_t);
146 } else {
147 size_size = sizeof(uint32_t);
148 }
149
150 entry_size = address_size + size_size;
151
152 /* Look for nodes with the device_type set to "memory". */
153 if (!fdt_first_child(&n, &name)) {
154 return;
155 }
156
157 do {
158 const char *data;
159 uint32_t size;
160 if (!fdt_read_property(&n, "device_type", &data, &size) ||
161 size != sizeof("memory") ||
162 memcmp(data, "memory", sizeof("memory")) != 0 ||
163 !fdt_read_property(&n, "reg", &data, &size)) {
164 continue;
165 }
166
167 /* Traverse all memory ranges within this node. */
168 while (size >= entry_size) {
Andrew Scull265ada92018-07-30 15:19:01 +0100169 uintpaddr_t addr = convert_number(data, address_size);
170 size_t len =
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100171 convert_number(data + address_size, size_size);
172
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100173 if (mem_range_index < MAX_MEM_RANGES) {
174 p->mem_ranges[mem_range_index].begin =
175 pa_init(addr);
176 p->mem_ranges[mem_range_index].end =
177 pa_init(addr + len);
178 ++mem_range_index;
179 } else {
180 dlog("Found memory range %u in FDT but only "
181 "%u supported, ignoring additional range "
182 "of size %u.\n",
183 mem_range_index, MAX_MEM_RANGES, len);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100184 }
185
186 size -= entry_size;
187 data += entry_size;
188 }
189 } while (fdt_next_sibling(&n, &name));
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100190 p->mem_ranges_count = mem_range_index;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100191
192 /* TODO: Check for "reserved-memory" nodes. */
193}
194
Andrew Scullb401ba32018-11-09 10:30:54 +0000195struct fdt_header *fdt_map(paddr_t fdt_addr, struct fdt_node *n)
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100196{
Andrew Scull265ada92018-07-30 15:19:01 +0100197 struct fdt_header *fdt;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100198
199 /* Map the fdt header in. */
Andrew Scull80871322018-08-06 12:04:09 +0100200 fdt = mm_identity_map(fdt_addr, pa_add(fdt_addr, fdt_header_size()),
201 MM_MODE_R);
202 if (!fdt) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100203 dlog("Unable to map FDT header.\n");
Andrew Scullb401ba32018-11-09 10:30:54 +0000204 return NULL;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100205 }
206
Andrew Scullb401ba32018-11-09 10:30:54 +0000207 if (!fdt_root_node(n, fdt)) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100208 dlog("FDT failed validation.\n");
Andrew Scullb401ba32018-11-09 10:30:54 +0000209 goto fail;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100210 }
211
212 /* Map the rest of the fdt in. */
Andrew Scull80871322018-08-06 12:04:09 +0100213 fdt = mm_identity_map(fdt_addr, pa_add(fdt_addr, fdt_total_size(fdt)),
214 MM_MODE_R);
215 if (!fdt) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100216 dlog("Unable to map full FDT.\n");
Andrew Scullb401ba32018-11-09 10:30:54 +0000217 goto fail;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100218 }
219
Andrew Scullb401ba32018-11-09 10:30:54 +0000220 return fdt;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100221
Andrew Scullb401ba32018-11-09 10:30:54 +0000222fail:
Andrew Scull80871322018-08-06 12:04:09 +0100223 mm_unmap(fdt_addr, pa_add(fdt_addr, fdt_header_size()), 0);
Andrew Scullb401ba32018-11-09 10:30:54 +0000224 return NULL;
225}
226
227bool fdt_unmap(struct fdt_header *fdt)
228{
229 paddr_t fdt_addr = pa_from_va(va_from_ptr(fdt));
230 return mm_unmap(fdt_addr, pa_add(fdt_addr, fdt_total_size(fdt)), 0);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100231}
232
Andrew Scull265ada92018-07-30 15:19:01 +0100233bool fdt_patch(paddr_t fdt_addr, struct boot_params_update *p)
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100234{
Andrew Scull265ada92018-07-30 15:19:01 +0100235 struct fdt_header *fdt;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100236 struct fdt_node n;
237 bool ret = false;
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100238 size_t i;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100239
240 /* Map the fdt header in. */
Andrew Scull80871322018-08-06 12:04:09 +0100241 fdt = mm_identity_map(fdt_addr, pa_add(fdt_addr, fdt_header_size()),
242 MM_MODE_R);
243 if (!fdt) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100244 dlog("Unable to map FDT header.\n");
245 return false;
246 }
247
248 if (!fdt_root_node(&n, fdt)) {
249 dlog("FDT failed validation.\n");
250 goto err_unmap_fdt_header;
251 }
252
253 /* Map the fdt (+ a page) in r/w mode in preparation for updating it. */
Andrew Scull80871322018-08-06 12:04:09 +0100254 fdt = mm_identity_map(fdt_addr,
255 pa_add(fdt_addr, fdt_total_size(fdt) + PAGE_SIZE),
256 MM_MODE_R | MM_MODE_W);
257 if (!fdt) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100258 dlog("Unable to map FDT in r/w mode.\n");
259 goto err_unmap_fdt_header;
260 }
261
262 if (!fdt_find_child(&n, "")) {
263 dlog("Unable to find FDT root node.\n");
264 goto out_unmap_fdt;
265 }
266
267 if (!fdt_find_child(&n, "chosen")) {
268 dlog("Unable to find 'chosen'\n");
269 goto out_unmap_fdt;
270 }
271
272 /* Patch FDT to point to new ramdisk. */
Andrew Scull265ada92018-07-30 15:19:01 +0100273 if (!fdt_write_number(&n, "linux,initrd-start",
274 pa_addr(p->initrd_begin))) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100275 dlog("Unable to write linux,initrd-start\n");
276 goto out_unmap_fdt;
277 }
278
Andrew Scull265ada92018-07-30 15:19:01 +0100279 if (!fdt_write_number(&n, "linux,initrd-end", pa_addr(p->initrd_end))) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100280 dlog("Unable to write linux,initrd-end\n");
281 goto out_unmap_fdt;
282 }
283
284 /* Patch fdt to reserve primary VM memory. */
Andrew Scull5991ec92018-10-08 14:55:02 +0100285 fdt_add_mem_reservation(fdt, pa_addr(layout_primary_begin()) & ~0xfffff,
286 0x80000);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100287
288 /* Patch fdt to reserve memory for secondary VMs. */
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100289 for (i = 0; i < p->reserved_ranges_count; ++i) {
290 fdt_add_mem_reservation(
291 fdt, pa_addr(p->reserved_ranges[i].begin),
292 pa_addr(p->reserved_ranges[i].end) -
293 pa_addr(p->reserved_ranges[i].begin));
294 }
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100295
296 ret = true;
297
298out_unmap_fdt:
299 /* Unmap FDT. */
Andrew Scull80871322018-08-06 12:04:09 +0100300 if (!mm_unmap(fdt_addr,
301 pa_add(fdt_addr, fdt_total_size(fdt) + PAGE_SIZE), 0)) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100302 dlog("Unable to unmap writable FDT.\n");
303 return false;
304 }
305 return ret;
306
307err_unmap_fdt_header:
Andrew Scull80871322018-08-06 12:04:09 +0100308 mm_unmap(fdt_addr, pa_add(fdt_addr, fdt_header_size()), 0);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100309 return false;
310}