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