blob: 5f25b907b9ef9873594ce4159029728fc362ef8f [file] [log] [blame]
Simon Glassc55a50f2018-09-14 04:57:19 -06001# SPDX-License-Identifier: GPL-2.0+
2# Copyright 2018 Google, Inc
3# Written by Simon Glass <sjg@chromium.org>
4#
5# Holds and modifies the state information held by binman
6#
7
8import re
9from sets import Set
10
11import os
12import tools
13
14# Records the device-tree files known to binman, keyed by filename (e.g.
15# 'u-boot-spl.dtb')
16fdt_files = {}
17
18# Arguments passed to binman to provide arguments to entries
19entry_args = {}
20
Simon Glass2a72cc72018-09-14 04:57:20 -060021# Set of all device tree files references by images
22fdt_set = Set()
23
24# Same as above, but excluding the main one
25fdt_subset = Set()
26
27# The DTB which contains the full image information
28main_dtb = None
29
Simon Glassc55a50f2018-09-14 04:57:19 -060030def GetFdt(fname):
31 """Get the Fdt object for a particular device-tree filename
32
33 Binman keeps track of at least one device-tree file called u-boot.dtb but
34 can also have others (e.g. for SPL). This function looks up the given
35 filename and returns the associated Fdt object.
36
37 Args:
38 fname: Filename to look up (e.g. 'u-boot.dtb').
39
40 Returns:
41 Fdt object associated with the filename
42 """
43 return fdt_files[fname]
44
45def GetFdtPath(fname):
46 """Get the full pathname of a particular Fdt object
47
48 Similar to GetFdt() but returns the pathname associated with the Fdt.
49
50 Args:
51 fname: Filename to look up (e.g. 'u-boot.dtb').
52
53 Returns:
54 Full path name to the associated Fdt
55 """
56 return fdt_files[fname]._fname
57
58def SetEntryArgs(args):
59 """Set the value of the entry args
60
61 This sets up the entry_args dict which is used to supply entry arguments to
62 entries.
63
64 Args:
65 args: List of entry arguments, each in the format "name=value"
66 """
67 global entry_args
68
69 entry_args = {}
70 if args:
71 for arg in args:
72 m = re.match('([^=]*)=(.*)', arg)
73 if not m:
74 raise ValueError("Invalid entry arguemnt '%s'" % arg)
75 entry_args[m.group(1)] = m.group(2)
76
77def GetEntryArg(name):
78 """Get the value of an entry argument
79
80 Args:
81 name: Name of argument to retrieve
82
83 Returns:
84 String value of argument
85 """
86 return entry_args.get(name)
Simon Glass2a72cc72018-09-14 04:57:20 -060087
88def Prepare(dtb):
89 """Get device tree files ready for use
90
91 This sets up a set of device tree files that can be retrieved by GetFdts().
92 At present there is only one, that for U-Boot proper.
93
94 Args:
95 dtb: Main dtb
96 """
97 global fdt_set, fdt_subset, fdt_files, main_dtb
98 # Import these here in case libfdt.py is not available, in which case
99 # the above help option still works.
100 import fdt
101 import fdt_util
102
103 # If we are updating the DTBs we need to put these updated versions
104 # where Entry_blob_dtb can find them. We can ignore 'u-boot.dtb'
105 # since it is assumed to be the one passed in with options.dt, and
106 # was handled just above.
107 main_dtb = dtb
108 fdt_files.clear()
109 fdt_files['u-boot.dtb'] = dtb
110 fdt_set = Set()
111 fdt_subset = Set()
112
113def GetFdts():
114 """Yield all device tree files being used by binman
115
116 Yields:
117 Device trees being used (U-Boot proper, SPL, TPL)
118 """
119 yield main_dtb
120
Simon Glassf46621d2018-09-14 04:57:21 -0600121def GetUpdateNodes(node):
122 """Yield all the nodes that need to be updated in all device trees
123
124 The property referenced by this node is added to any device trees which
125 have the given node. Due to removable of unwanted notes, SPL and TPL may
126 not have this node.
127
128 Args:
129 node: Node object in the main device tree to look up
130
131 Yields:
132 Node objects in each device tree that is in use (U-Boot proper, which
133 is node, SPL and TPL)
134 """
135 yield node
136
137def AddZeroProp(node, prop):
138 """Add a new property to affected device trees with an integer value of 0.
139
140 Args:
141 prop_name: Name of property
142 """
143 for n in GetUpdateNodes(node):
144 n.AddZeroProp(prop)
145
146def SetInt(node, prop, value):
147 """Update an integer property in affected device trees with an integer value
148
149 This is not allowed to change the size of the FDT.
150
151 Args:
152 prop_name: Name of property
153 """
154 for n in GetUpdateNodes(node):
155 n.SetInt(prop, value)