coreboot
coreboot is an Open Source project aimed at replacing the proprietary BIOS found in most computers.
fixup_fdt.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 
3 #include <stdint.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <console/console.h>
7 #include <soc/otp.h>
8 #include <soc/sdram.h>
9 #include <cbfs.h>
10 #include <device_tree.h>
11 #include <bootstate.h>
12 #include <mcall.h>
13 
14 static void do_fixup_mac(struct device_tree_node *node)
15 {
17  static unsigned char mac[6] = { 0x70, 0xb3, 0xd5, 0x92, 0xf0, 0x00 };
18  if (serial != ~0) {
19  mac[5] |= (serial >> 0) & 0xff;
20  mac[4] |= (serial >> 8) & 0xff;
21  mac[3] |= (serial >> 16) & 0xff;
22  }
23  dt_add_bin_prop(node, "local-mac-address", mac, 6);
24 }
25 
26 static void fixup_mac(struct device_tree_node *parent)
27 {
28  struct device_tree_property *prop;
29  const char *name = "local-mac-address";
30 
32  if (!strcmp(name, prop->prop.name))
33  do_fixup_mac(parent);
34  }
35 
36  struct device_tree_node *child;
37  list_for_each(child, parent->children, list_node) {
38  fixup_mac(child);
39  }
40 }
41 
42 static void do_fixup_memory(struct device_tree_node *node)
43 {
44  u64 addrs[1], sizes[1];
45  addrs[0] = 0x80000000;
46  sizes[0] = sdram_size_mb() * 1024 * 1024;
47  dt_add_reg_prop(node, addrs, sizes, 1, 2, 2);
48 }
49 
50 static void fixup_memory(struct device_tree_node *parent)
51 {
52  struct device_tree_property *prop;
53  const char *name = "device_type";
54  const char *value = "memory";
55 
57  if (!strcmp(name, prop->prop.name)) {
58  if (!strcmp(value, (char *)prop->prop.data))
59  do_fixup_memory(parent);
60  }
61  }
62 
63  struct device_tree_node *child;
64  list_for_each(child, parent->children, list_node) {
65  fixup_memory(child);
66  }
67 }
68 
69 static void fixup_fdt(void *unused)
70 {
71  void *fdt_rom;
72  struct device_tree *tree;
73 
74  /* load flat dt from cbfs */
75  fdt_rom = cbfs_map("fallback/DTB", NULL);
76 
77  if (fdt_rom == NULL) {
78  printk(BIOS_ERR, "Unable to load fallback/DTB from CBFS\n");
79  return;
80  }
81 
82  /* Expand DT into a tree */
83  tree = fdt_unflatten(fdt_rom);
84 
85  /* fixup tree */
86  fixup_mac(tree->root);
87  fixup_memory(tree->root);
88 
89  /* convert the tree to a flat dt */
90  void *dt = malloc(dt_flat_size(tree));
91 
92  if (dt == NULL) {
93  printk(BIOS_ERR, "Unable to allocate memory for flat device tree\n");
94  return;
95  }
96 
97  dt_flatten(tree, dt);
98 
99  /* update HLS */
100  for (int i = 0; i < CONFIG_MAX_CPUS; i++)
101  OTHER_HLS(i)->fdt = dt;
102 }
103 
pte_t value
Definition: mmu.c:91
const char * name
Definition: mmu.c:92
@ BS_WRITE_TABLES
Definition: bootstate.h:87
@ BS_ON_EXIT
Definition: bootstate.h:96
size_t sdram_size_mb(void)
Definition: sdram.c:24
static void * cbfs_map(const char *name, size_t *size_out)
Definition: cbfs.h:246
#define printk(level,...)
Definition: stdlib.h:16
void dt_add_bin_prop(struct device_tree_node *node, const char *name, void *data, size_t size)
Definition: device_tree.c:874
uint32_t dt_flat_size(const struct device_tree *tree)
Definition: device_tree.c:332
void dt_flatten(const struct device_tree *tree, void *dest)
Definition: device_tree.c:424
struct device_tree * fdt_unflatten(const void *blob)
Definition: device_tree.c:237
void dt_add_reg_prop(struct device_tree_node *node, u64 *addrs, u64 *sizes, int count, u32 addr_cells, u32 size_cells)
Definition: device_tree.c:988
static void do_fixup_mac(struct device_tree_node *node)
Definition: fixup_fdt.c:14
static void fixup_memory(struct device_tree_node *parent)
Definition: fixup_fdt.c:50
static void fixup_fdt(void *unused)
Definition: fixup_fdt.c:69
static void fixup_mac(struct device_tree_node *parent)
Definition: fixup_fdt.c:26
BOOT_STATE_INIT_ENTRY(BS_WRITE_TABLES, BS_ON_EXIT, fixup_fdt, NULL)
static void do_fixup_memory(struct device_tree_node *node)
Definition: fixup_fdt.c:42
void * malloc(size_t size)
Definition: malloc.c:53
unsigned int serial
Definition: edid.c:52
#define list_for_each(ptr, head, member)
Definition: list.h:21
#define BIOS_ERR
BIOS_ERR - System in incomplete state.
Definition: loglevel.h:72
#define OTHER_HLS(id)
Definition: mcall.h:64
u32 otp_read_serial(void)
Definition: otp.c:79
#define NULL
Definition: stddef.h:19
uint64_t u64
Definition: stdint.h:54
unsigned int uint32_t
Definition: stdint.h:14
int strcmp(const char *s1, const char *s2)
Definition: string.c:103
struct list_node children
Definition: device_tree.h:65
struct list_node properties
Definition: device_tree.h:63
struct fdt_property prop
Definition: device_tree.h:52
struct device_tree_node * root
Definition: device_tree.h:86
void * data
Definition: device_tree.h:42
const char * name
Definition: device_tree.h:41
Definition: list.h:7