efifdt.c revision 1.1 1 1.1 jmcneill /* $NetBSD: efifdt.c,v 1.1 2018/08/24 02:01:06 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.1 jmcneill EFI_MEMORY_DESCRIPTOR *md;
83 1.1 jmcneill UINT32 descver;
84 1.1 jmcneill int n, memory;
85 1.1 jmcneill
86 1.1 jmcneill memory = fdt_path_offset(fdt_data, FDT_MEMORY_NODE_PATH);
87 1.1 jmcneill if (memory < 0)
88 1.1 jmcneill memory = fdt_add_subnode(fdt_data, fdt_path_offset(fdt_data, "/"), FDT_MEMORY_NODE_NAME);
89 1.1 jmcneill if (memory < 0)
90 1.1 jmcneill panic("FDT: Failed to create " FDT_MEMORY_NODE_PATH " node");
91 1.1 jmcneill
92 1.1 jmcneill fdt_delprop(fdt_data, memory, "reg");
93 1.1 jmcneill
94 1.1 jmcneill const int address_cells = fdt_address_cells(fdt_data, fdt_path_offset(fdt_data, "/"));
95 1.1 jmcneill const int size_cells = fdt_size_cells(fdt_data, fdt_path_offset(fdt_data, "/"));
96 1.1 jmcneill
97 1.1 jmcneill md = LibMemoryMap(&nentries, &mapkey, &descsize, &descver);
98 1.1 jmcneill for (n = 0; n < nentries; n++, md = NextMemoryDescriptor(md, descsize)) {
99 1.1 jmcneill if ((md->Attribute & EFI_MEMORY_WB) == 0)
100 1.1 jmcneill continue;
101 1.1 jmcneill if (!FDT_MEMORY_USABLE(md))
102 1.1 jmcneill continue;
103 1.1 jmcneill
104 1.1 jmcneill if (address_cells == 1)
105 1.1 jmcneill fdt_appendprop_u32(fdt_data, fdt_path_offset(fdt_data, FDT_MEMORY_NODE_PATH),
106 1.1 jmcneill "reg", (uint32_t)md->PhysicalStart);
107 1.1 jmcneill else
108 1.1 jmcneill fdt_appendprop_u64(fdt_data, fdt_path_offset(fdt_data, FDT_MEMORY_NODE_PATH),
109 1.1 jmcneill "reg", md->PhysicalStart);
110 1.1 jmcneill
111 1.1 jmcneill if (size_cells == 1)
112 1.1 jmcneill fdt_appendprop_u32(fdt_data, fdt_path_offset(fdt_data, FDT_MEMORY_NODE_PATH),
113 1.1 jmcneill "reg", (uint32_t)md->NumberOfPages * EFI_PAGE_SIZE);
114 1.1 jmcneill else
115 1.1 jmcneill fdt_appendprop_u64(fdt_data, fdt_path_offset(fdt_data, FDT_MEMORY_NODE_PATH),
116 1.1 jmcneill "reg", (uint64_t)md->NumberOfPages * EFI_PAGE_SIZE);
117 1.1 jmcneill }
118 1.1 jmcneill }
119 1.1 jmcneill
120 1.1 jmcneill void
121 1.1 jmcneill efi_fdt_bootargs(const char *bootargs)
122 1.1 jmcneill {
123 1.1 jmcneill fdt_setprop_string(fdt_data, fdt_path_offset(fdt_data, FDT_CHOSEN_NODE_PATH), "bootargs", bootargs);
124 1.1 jmcneill }
125