efifdt.c revision 1.2 1 1.2 jmcneill /* $NetBSD: efifdt.c,v 1.2 2018/08/24 23:20:41 jmcneill Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*-
4 1.1 jmcneill * Copyright (c) 2018 Jared McNeill <jmcneill (at) invisible.ca>
5 1.1 jmcneill * All rights reserved.
6 1.1 jmcneill *
7 1.1 jmcneill * Redistribution and use in source and binary forms, with or without
8 1.1 jmcneill * modification, are permitted provided that the following conditions
9 1.1 jmcneill * are met:
10 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright
11 1.1 jmcneill * notice, this list of conditions and the following disclaimer.
12 1.1 jmcneill * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 jmcneill * notice, this list of conditions and the following disclaimer in the
14 1.1 jmcneill * documentation and/or other materials provided with the distribution.
15 1.1 jmcneill *
16 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17 1.1 jmcneill * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 1.1 jmcneill * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.1 jmcneill * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20 1.1 jmcneill * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.1 jmcneill * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 1.1 jmcneill * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.1 jmcneill * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.1 jmcneill * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 jmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 jmcneill * SUCH DAMAGE.
27 1.1 jmcneill */
28 1.1 jmcneill
29 1.1 jmcneill #include "efiboot.h"
30 1.1 jmcneill #include "efifdt.h"
31 1.1 jmcneill
32 1.1 jmcneill #include <libfdt.h>
33 1.1 jmcneill
34 1.1 jmcneill #define FDT_TABLE_GUID \
35 1.1 jmcneill { 0xb1b621d5, 0xf19c, 0x41a5, { 0x83, 0x0b, 0xd9, 0x15, 0x2c, 0x69, 0xaa, 0xe0 } }
36 1.1 jmcneill static EFI_GUID FdtTableGuid = FDT_TABLE_GUID;
37 1.1 jmcneill
38 1.1 jmcneill #define FDT_MEMORY_NODE_PATH "/memory"
39 1.1 jmcneill #define FDT_MEMORY_NODE_NAME "memory"
40 1.1 jmcneill #define FDT_CHOSEN_NODE_PATH "/chosen"
41 1.1 jmcneill
42 1.1 jmcneill #define FDT_MEMORY_USABLE(_md) \
43 1.1 jmcneill ((_md)->Type == EfiLoaderCode || (_md)->Type == EfiLoaderData || \
44 1.1 jmcneill (_md)->Type == EfiBootServicesCode || (_md)->Type == EfiBootServicesData || \
45 1.1 jmcneill (_md)->Type == EfiConventionalMemory)
46 1.1 jmcneill
47 1.1 jmcneill static void *fdt_data = NULL;
48 1.1 jmcneill
49 1.1 jmcneill int
50 1.1 jmcneill efi_fdt_probe(void)
51 1.1 jmcneill {
52 1.1 jmcneill EFI_STATUS status;
53 1.1 jmcneill
54 1.1 jmcneill status = LibGetSystemConfigurationTable(&FdtTableGuid, &fdt_data);
55 1.1 jmcneill if (EFI_ERROR(status))
56 1.1 jmcneill return EIO;
57 1.1 jmcneill
58 1.1 jmcneill if (fdt_check_header(fdt_data) != 0) {
59 1.1 jmcneill fdt_data = NULL;
60 1.1 jmcneill return EINVAL;
61 1.1 jmcneill }
62 1.1 jmcneill
63 1.1 jmcneill return 0;
64 1.1 jmcneill }
65 1.1 jmcneill
66 1.1 jmcneill void *
67 1.1 jmcneill efi_fdt_data(void)
68 1.1 jmcneill {
69 1.1 jmcneill return fdt_data;
70 1.1 jmcneill }
71 1.1 jmcneill
72 1.1 jmcneill int
73 1.1 jmcneill efi_fdt_size(void)
74 1.1 jmcneill {
75 1.1 jmcneill return fdt_data == NULL ? 0 : fdt_totalsize(fdt_data);
76 1.1 jmcneill }
77 1.1 jmcneill
78 1.1 jmcneill void
79 1.1 jmcneill efi_fdt_memory_map(void)
80 1.1 jmcneill {
81 1.1 jmcneill UINTN nentries = 0, mapkey, descsize;
82 1.2 jmcneill EFI_MEMORY_DESCRIPTOR *md, *memmap;
83 1.1 jmcneill UINT32 descver;
84 1.2 jmcneill UINT64 phys_start, phys_size;
85 1.1 jmcneill int n, memory;
86 1.1 jmcneill
87 1.1 jmcneill memory = fdt_path_offset(fdt_data, FDT_MEMORY_NODE_PATH);
88 1.1 jmcneill if (memory < 0)
89 1.1 jmcneill memory = fdt_add_subnode(fdt_data, fdt_path_offset(fdt_data, "/"), FDT_MEMORY_NODE_NAME);
90 1.1 jmcneill if (memory < 0)
91 1.1 jmcneill panic("FDT: Failed to create " FDT_MEMORY_NODE_PATH " node");
92 1.1 jmcneill
93 1.1 jmcneill fdt_delprop(fdt_data, memory, "reg");
94 1.2 jmcneill while (fdt_num_mem_rsv(fdt_data) > 0) {
95 1.2 jmcneill if (fdt_del_mem_rsv(fdt_data, 0) < 0)
96 1.2 jmcneill panic("FDT: Failed to remove reserved memory map entry");
97 1.2 jmcneill }
98 1.1 jmcneill
99 1.1 jmcneill const int address_cells = fdt_address_cells(fdt_data, fdt_path_offset(fdt_data, "/"));
100 1.1 jmcneill const int size_cells = fdt_size_cells(fdt_data, fdt_path_offset(fdt_data, "/"));
101 1.1 jmcneill
102 1.2 jmcneill memmap = LibMemoryMap(&nentries, &mapkey, &descsize, &descver);
103 1.2 jmcneill for (n = 0, md = memmap; n < nentries; n++, md = NextMemoryDescriptor(md, descsize)) {
104 1.1 jmcneill if ((md->Attribute & EFI_MEMORY_WB) == 0)
105 1.1 jmcneill continue;
106 1.1 jmcneill if (!FDT_MEMORY_USABLE(md))
107 1.1 jmcneill continue;
108 1.2 jmcneill if ((address_cells == 1 || size_cells == 1) && md->PhysicalStart + (md->NumberOfPages * EFI_PAGE_SIZE) > 0xffffffff)
109 1.2 jmcneill continue;
110 1.2 jmcneill if (md->NumberOfPages <= 1)
111 1.2 jmcneill continue;
112 1.2 jmcneill
113 1.2 jmcneill phys_start = md->PhysicalStart;
114 1.2 jmcneill phys_size = md->NumberOfPages * EFI_PAGE_SIZE;
115 1.2 jmcneill
116 1.2 jmcneill if (phys_start & EFI_PAGE_MASK) {
117 1.2 jmcneill /* UEFI spec says these should be 4KB aligned, but U-Boot doesn't always.. */
118 1.2 jmcneill phys_start = (phys_start + EFI_PAGE_SIZE) & ~EFI_PAGE_MASK;
119 1.2 jmcneill phys_size -= (EFI_PAGE_SIZE * 2);
120 1.2 jmcneill if (phys_size == 0)
121 1.2 jmcneill continue;
122 1.2 jmcneill }
123 1.1 jmcneill
124 1.1 jmcneill if (address_cells == 1)
125 1.1 jmcneill fdt_appendprop_u32(fdt_data, fdt_path_offset(fdt_data, FDT_MEMORY_NODE_PATH),
126 1.2 jmcneill "reg", (uint32_t)phys_start);
127 1.1 jmcneill else
128 1.1 jmcneill fdt_appendprop_u64(fdt_data, fdt_path_offset(fdt_data, FDT_MEMORY_NODE_PATH),
129 1.2 jmcneill "reg", phys_start);
130 1.1 jmcneill
131 1.1 jmcneill if (size_cells == 1)
132 1.1 jmcneill fdt_appendprop_u32(fdt_data, fdt_path_offset(fdt_data, FDT_MEMORY_NODE_PATH),
133 1.2 jmcneill "reg", (uint32_t)phys_size);
134 1.1 jmcneill else
135 1.1 jmcneill fdt_appendprop_u64(fdt_data, fdt_path_offset(fdt_data, FDT_MEMORY_NODE_PATH),
136 1.2 jmcneill "reg", phys_size);
137 1.1 jmcneill }
138 1.1 jmcneill }
139 1.1 jmcneill
140 1.1 jmcneill void
141 1.1 jmcneill efi_fdt_bootargs(const char *bootargs)
142 1.1 jmcneill {
143 1.1 jmcneill fdt_setprop_string(fdt_data, fdt_path_offset(fdt_data, FDT_CHOSEN_NODE_PATH), "bootargs", bootargs);
144 1.1 jmcneill }
145