1ac5abc70SArd Biesheuvel // SPDX-License-Identifier: GPL-2.0-only
2ac5abc70SArd Biesheuvel
3ac5abc70SArd Biesheuvel #define pr_fmt(fmt) "efi: " fmt
4ac5abc70SArd Biesheuvel
5ac5abc70SArd Biesheuvel #include <linux/module.h>
6ac5abc70SArd Biesheuvel #include <linux/init.h>
7ac5abc70SArd Biesheuvel #include <linux/efi.h>
8e457ed51SArd Biesheuvel #include <linux/libfdt.h>
9ac5abc70SArd Biesheuvel #include <linux/of_fdt.h>
10ac5abc70SArd Biesheuvel
11*5f60d5f6SAl Viro #include <linux/unaligned.h>
12ac5abc70SArd Biesheuvel
13e457ed51SArd Biesheuvel enum {
14e457ed51SArd Biesheuvel SYSTAB,
15e457ed51SArd Biesheuvel MMBASE,
16e457ed51SArd Biesheuvel MMSIZE,
17e457ed51SArd Biesheuvel DCSIZE,
18e457ed51SArd Biesheuvel DCVERS,
19ac5abc70SArd Biesheuvel
20e457ed51SArd Biesheuvel PARAMCOUNT
213b2e4b4cSArd Biesheuvel };
223b2e4b4cSArd Biesheuvel
23e457ed51SArd Biesheuvel static __initconst const char name[][22] = {
24e457ed51SArd Biesheuvel [SYSTAB] = "System Table ",
25e457ed51SArd Biesheuvel [MMBASE] = "MemMap Address ",
26e457ed51SArd Biesheuvel [MMSIZE] = "MemMap Size ",
27e457ed51SArd Biesheuvel [DCSIZE] = "MemMap Desc. Size ",
28e457ed51SArd Biesheuvel [DCVERS] = "MemMap Desc. Version ",
29ac5abc70SArd Biesheuvel };
30ac5abc70SArd Biesheuvel
31e457ed51SArd Biesheuvel static __initconst const struct {
32e457ed51SArd Biesheuvel const char path[17];
33d85e3e34SArd Biesheuvel u8 paravirt;
34e457ed51SArd Biesheuvel const char params[PARAMCOUNT][26];
35ac5abc70SArd Biesheuvel } dt_params[] = {
36e457ed51SArd Biesheuvel {
37e457ed51SArd Biesheuvel #ifdef CONFIG_XEN // <-------17------>
38e457ed51SArd Biesheuvel .path = "/hypervisor/uefi",
39d85e3e34SArd Biesheuvel .paravirt = 1,
40e457ed51SArd Biesheuvel .params = {
41e457ed51SArd Biesheuvel [SYSTAB] = "xen,uefi-system-table",
42e457ed51SArd Biesheuvel [MMBASE] = "xen,uefi-mmap-start",
43e457ed51SArd Biesheuvel [MMSIZE] = "xen,uefi-mmap-size",
44e457ed51SArd Biesheuvel [DCSIZE] = "xen,uefi-mmap-desc-size",
45e457ed51SArd Biesheuvel [DCVERS] = "xen,uefi-mmap-desc-ver",
46e457ed51SArd Biesheuvel }
47e457ed51SArd Biesheuvel }, {
48e457ed51SArd Biesheuvel #endif
49e457ed51SArd Biesheuvel .path = "/chosen",
50e457ed51SArd Biesheuvel .params = { // <-----------26----------->
51e457ed51SArd Biesheuvel [SYSTAB] = "linux,uefi-system-table",
52e457ed51SArd Biesheuvel [MMBASE] = "linux,uefi-mmap-start",
53e457ed51SArd Biesheuvel [MMSIZE] = "linux,uefi-mmap-size",
54e457ed51SArd Biesheuvel [DCSIZE] = "linux,uefi-mmap-desc-size",
55e457ed51SArd Biesheuvel [DCVERS] = "linux,uefi-mmap-desc-ver",
56e457ed51SArd Biesheuvel }
57e457ed51SArd Biesheuvel }
58ac5abc70SArd Biesheuvel };
59ac5abc70SArd Biesheuvel
efi_get_fdt_prop(const void * fdt,int node,const char * pname,const char * rname,void * var,int size)60e457ed51SArd Biesheuvel static int __init efi_get_fdt_prop(const void *fdt, int node, const char *pname,
61e457ed51SArd Biesheuvel const char *rname, void *var, int size)
62ac5abc70SArd Biesheuvel {
63ac5abc70SArd Biesheuvel const void *prop;
64e457ed51SArd Biesheuvel int len;
65ac5abc70SArd Biesheuvel u64 val;
66ac5abc70SArd Biesheuvel
67e457ed51SArd Biesheuvel prop = fdt_getprop(fdt, node, pname, &len);
68e457ed51SArd Biesheuvel if (!prop)
69e457ed51SArd Biesheuvel return 1;
70ac5abc70SArd Biesheuvel
71e457ed51SArd Biesheuvel val = (len == 4) ? (u64)be32_to_cpup(prop) : get_unaligned_be64(prop);
72ac5abc70SArd Biesheuvel
73e457ed51SArd Biesheuvel if (size == 8)
74e457ed51SArd Biesheuvel *(u64 *)var = val;
75ac5abc70SArd Biesheuvel else
76e457ed51SArd Biesheuvel *(u32 *)var = (val < U32_MAX) ? val : U32_MAX; // saturate
77ac5abc70SArd Biesheuvel
78ac5abc70SArd Biesheuvel if (efi_enabled(EFI_DBG))
79e457ed51SArd Biesheuvel pr_info(" %s: 0x%0*llx\n", rname, size * 2, val);
80e457ed51SArd Biesheuvel
81e457ed51SArd Biesheuvel return 0;
82ac5abc70SArd Biesheuvel }
83ac5abc70SArd Biesheuvel
efi_get_fdt_params(struct efi_memory_map_data * mm)84e457ed51SArd Biesheuvel u64 __init efi_get_fdt_params(struct efi_memory_map_data *mm)
85ac5abc70SArd Biesheuvel {
86e457ed51SArd Biesheuvel const void *fdt = initial_boot_params;
87e457ed51SArd Biesheuvel unsigned long systab;
88e457ed51SArd Biesheuvel int i, j, node;
89e457ed51SArd Biesheuvel struct {
90e457ed51SArd Biesheuvel void *var;
91e457ed51SArd Biesheuvel int size;
92e457ed51SArd Biesheuvel } target[] = {
93e457ed51SArd Biesheuvel [SYSTAB] = { &systab, sizeof(systab) },
94e457ed51SArd Biesheuvel [MMBASE] = { &mm->phys_map, sizeof(mm->phys_map) },
95e457ed51SArd Biesheuvel [MMSIZE] = { &mm->size, sizeof(mm->size) },
96e457ed51SArd Biesheuvel [DCSIZE] = { &mm->desc_size, sizeof(mm->desc_size) },
97e457ed51SArd Biesheuvel [DCVERS] = { &mm->desc_version, sizeof(mm->desc_version) },
98e457ed51SArd Biesheuvel };
99e457ed51SArd Biesheuvel
100e457ed51SArd Biesheuvel BUILD_BUG_ON(ARRAY_SIZE(target) != ARRAY_SIZE(name));
101e457ed51SArd Biesheuvel BUILD_BUG_ON(ARRAY_SIZE(target) != ARRAY_SIZE(dt_params[0].params));
102ac5abc70SArd Biesheuvel
103668a84c1SChangbin Du if (!fdt)
104668a84c1SChangbin Du return 0;
105668a84c1SChangbin Du
106ac5abc70SArd Biesheuvel for (i = 0; i < ARRAY_SIZE(dt_params); i++) {
107e457ed51SArd Biesheuvel node = fdt_path_offset(fdt, dt_params[i].path);
108e457ed51SArd Biesheuvel if (node < 0)
109ac5abc70SArd Biesheuvel continue;
110ac5abc70SArd Biesheuvel
111e457ed51SArd Biesheuvel if (efi_enabled(EFI_DBG))
112e457ed51SArd Biesheuvel pr_info("Getting UEFI parameters from %s in DT:\n",
113e457ed51SArd Biesheuvel dt_params[i].path);
114ac5abc70SArd Biesheuvel
115e457ed51SArd Biesheuvel for (j = 0; j < ARRAY_SIZE(target); j++) {
116e457ed51SArd Biesheuvel const char *pname = dt_params[i].params[j];
117ac5abc70SArd Biesheuvel
118e457ed51SArd Biesheuvel if (!efi_get_fdt_prop(fdt, node, pname, name[j],
119e457ed51SArd Biesheuvel target[j].var, target[j].size))
120e457ed51SArd Biesheuvel continue;
121e457ed51SArd Biesheuvel if (!j)
122e457ed51SArd Biesheuvel goto notfound;
123e457ed51SArd Biesheuvel pr_err("Can't find property '%s' in DT!\n", pname);
124ac5abc70SArd Biesheuvel return 0;
125ac5abc70SArd Biesheuvel }
126d85e3e34SArd Biesheuvel if (dt_params[i].paravirt)
127d85e3e34SArd Biesheuvel set_bit(EFI_PARAVIRT, &efi.flags);
128e457ed51SArd Biesheuvel return systab;
129e457ed51SArd Biesheuvel }
130e457ed51SArd Biesheuvel notfound:
131ac5abc70SArd Biesheuvel pr_info("UEFI not found.\n");
1323b2e4b4cSArd Biesheuvel return 0;
133ac5abc70SArd Biesheuvel }
134