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