blob: 600eb86cfe26eaf5f35a090543b20cd9c51bbe76 [file] [log] [blame]
Simon Glass29aa7362018-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 Glass0c9d5b52018-09-14 04:57:22 -060021# True to use fake device-tree files for testing (see U_BOOT_DTB_DATA in
22# ftest.py)
23use_fake_dtb = True
24
Simon Glassbdb40312018-09-14 04:57:20 -060025# Set of all device tree files references by images
26fdt_set = Set()
27
28# Same as above, but excluding the main one
29fdt_subset = Set()
30
31# The DTB which contains the full image information
32main_dtb = None
33
Simon Glass29aa7362018-09-14 04:57:19 -060034def GetFdt(fname):
35 """Get the Fdt object for a particular device-tree filename
36
37 Binman keeps track of at least one device-tree file called u-boot.dtb but
38 can also have others (e.g. for SPL). This function looks up the given
39 filename and returns the associated Fdt object.
40
41 Args:
42 fname: Filename to look up (e.g. 'u-boot.dtb').
43
44 Returns:
45 Fdt object associated with the filename
46 """
47 return fdt_files[fname]
48
49def GetFdtPath(fname):
50 """Get the full pathname of a particular Fdt object
51
52 Similar to GetFdt() but returns the pathname associated with the Fdt.
53
54 Args:
55 fname: Filename to look up (e.g. 'u-boot.dtb').
56
57 Returns:
58 Full path name to the associated Fdt
59 """
60 return fdt_files[fname]._fname
61
62def SetEntryArgs(args):
63 """Set the value of the entry args
64
65 This sets up the entry_args dict which is used to supply entry arguments to
66 entries.
67
68 Args:
69 args: List of entry arguments, each in the format "name=value"
70 """
71 global entry_args
72
73 entry_args = {}
74 if args:
75 for arg in args:
76 m = re.match('([^=]*)=(.*)', arg)
77 if not m:
78 raise ValueError("Invalid entry arguemnt '%s'" % arg)
79 entry_args[m.group(1)] = m.group(2)
80
81def GetEntryArg(name):
82 """Get the value of an entry argument
83
84 Args:
85 name: Name of argument to retrieve
86
87 Returns:
88 String value of argument
89 """
90 return entry_args.get(name)
Simon Glassbdb40312018-09-14 04:57:20 -060091
Simon Glass0c9d5b52018-09-14 04:57:22 -060092def Prepare(images, dtb):
Simon Glassbdb40312018-09-14 04:57:20 -060093 """Get device tree files ready for use
94
95 This sets up a set of device tree files that can be retrieved by GetFdts().
96 At present there is only one, that for U-Boot proper.
97
98 Args:
Simon Glass0c9d5b52018-09-14 04:57:22 -060099 images: List of images being used
Simon Glassbdb40312018-09-14 04:57:20 -0600100 dtb: Main dtb
101 """
102 global fdt_set, fdt_subset, fdt_files, main_dtb
103 # Import these here in case libfdt.py is not available, in which case
104 # the above help option still works.
105 import fdt
106 import fdt_util
107
108 # If we are updating the DTBs we need to put these updated versions
109 # where Entry_blob_dtb can find them. We can ignore 'u-boot.dtb'
110 # since it is assumed to be the one passed in with options.dt, and
111 # was handled just above.
112 main_dtb = dtb
113 fdt_files.clear()
114 fdt_files['u-boot.dtb'] = dtb
Simon Glassbdb40312018-09-14 04:57:20 -0600115 fdt_subset = Set()
Simon Glass0c9d5b52018-09-14 04:57:22 -0600116 if not use_fake_dtb:
117 for image in images.values():
118 fdt_subset.update(image.GetFdtSet())
119 fdt_subset.discard('u-boot.dtb')
120 for other_fname in fdt_subset:
121 infile = tools.GetInputFilename(other_fname)
122 other_fname_dtb = fdt_util.EnsureCompiled(infile)
123 out_fname = tools.GetOutputFilename('%s.out' %
124 os.path.split(other_fname)[1])
125 tools.WriteFile(out_fname, tools.ReadFile(other_fname_dtb))
126 other_dtb = fdt.FdtScan(out_fname)
127 fdt_files[other_fname] = other_dtb
Simon Glassbdb40312018-09-14 04:57:20 -0600128
129def GetFdts():
130 """Yield all device tree files being used by binman
131
132 Yields:
133 Device trees being used (U-Boot proper, SPL, TPL)
134 """
135 yield main_dtb
136
Simon Glassc8135dc2018-09-14 04:57:21 -0600137def GetUpdateNodes(node):
138 """Yield all the nodes that need to be updated in all device trees
139
140 The property referenced by this node is added to any device trees which
141 have the given node. Due to removable of unwanted notes, SPL and TPL may
142 not have this node.
143
144 Args:
145 node: Node object in the main device tree to look up
146
147 Yields:
148 Node objects in each device tree that is in use (U-Boot proper, which
149 is node, SPL and TPL)
150 """
151 yield node
152
153def AddZeroProp(node, prop):
154 """Add a new property to affected device trees with an integer value of 0.
155
156 Args:
157 prop_name: Name of property
158 """
159 for n in GetUpdateNodes(node):
160 n.AddZeroProp(prop)
161
162def SetInt(node, prop, value):
163 """Update an integer property in affected device trees with an integer value
164
165 This is not allowed to change the size of the FDT.
166
167 Args:
168 prop_name: Name of property
169 """
170 for n in GetUpdateNodes(node):
171 n.SetInt(prop, value)