2929from elftools .elf .sections import SymbolTableSection
3030
3131# This is needed to load edt.pickle files.
32- sys .path .insert (0 , os .path .join (os .path .dirname (__file__ ), ".." ,
33- "dts" , "python-devicetree" , "src" ))
32+ sys .path .insert (0 , os .path .join (os .path .dirname (__file__ ), ".." , "dts" , "python-devicetree" , "src" ))
3433from devicetree import edtlib # pylint: disable=unused-import
3534
3635# Prefix used for "struct device" reference initialized based on devicetree
3736# entries with a known ordinal.
3837_DEVICE_ORD_PREFIX = "__device_dts_ord_"
3938
4039# Defined init level in order of priority.
41- _DEVICE_INIT_LEVELS = ["EARLY" , "PRE_KERNEL_1" , "PRE_KERNEL_2" , "POST_KERNEL" ,
42- "APPLICATION" , "SMP" ]
40+ _DEVICE_INIT_LEVELS = ["EARLY" , "PRE_KERNEL_1" , "PRE_KERNEL_2" , "POST_KERNEL" , "APPLICATION" , "SMP" ]
4341
4442# List of compatibles for nodes where we don't check the priority.
45- _IGNORE_COMPATIBLES = frozenset ([
43+ _IGNORE_COMPATIBLES = frozenset (
44+ [
4645 # There is no direct dependency between the CDC ACM UART and the USB
4746 # device controller, the logical connection is established after USB
4847 # device support is enabled.
4948 "zephyr,cdc-acm-uart" ,
50- ])
49+ ]
50+ )
5151
5252# The offset of the init pointer in "struct device", in number of pointers.
5353DEVICE_INIT_OFFSET = 5
5454
55+
5556class Priority :
5657 """Parses and holds a device initialization priority.
5758
@@ -60,6 +61,7 @@ class Priority:
6061 Attributes:
6162 name: the section name
6263 """
64+
6365 def __init__ (self , level , priority ):
6466 for idx , level_name in enumerate (_DEVICE_INIT_LEVELS ):
6567 if level_name == level :
@@ -72,8 +74,11 @@ def __init__(self, level, priority):
7274 raise ValueError ("Unknown level in %s" % level )
7375
7476 def __repr__ (self ):
75- return "<%s %s %d>" % (self .__class__ .__name__ ,
76- _DEVICE_INIT_LEVELS [self ._level ], self ._priority )
77+ return "<%s %s %d>" % (
78+ self .__class__ .__name__ ,
79+ _DEVICE_INIT_LEVELS [self ._level ],
80+ self ._priority ,
81+ )
7782
7883 def __str__ (self ):
7984 return "%s+%d" % (_DEVICE_INIT_LEVELS [self ._level ], self ._priority )
@@ -101,6 +106,7 @@ class variables in the {"level name": ["call", ...]} format.
101106 Attributes:
102107 file_path: path of the file to be loaded.
103108 """
109+
104110 def __init__ (self , file_path , elf_file ):
105111 self .file_path = file_path
106112 self ._elf = ELFFile (elf_file )
@@ -118,11 +124,16 @@ def _load_objects(self):
118124 continue
119125
120126 for sym in section .iter_symbols ():
121- if (sym .name and
122- sym .entry .st_size > 0 and
123- sym .entry .st_info .type in ["STT_OBJECT" , "STT_FUNC" ]):
127+ if (
128+ sym .name
129+ and sym .entry .st_size > 0
130+ and sym .entry .st_info .type in ["STT_OBJECT" , "STT_FUNC" ]
131+ ):
124132 self ._objects [sym .entry .st_value ] = (
125- sym .name , sym .entry .st_size , sym .entry .st_shndx )
133+ sym .name ,
134+ sym .entry .st_size ,
135+ sym .entry .st_shndx ,
136+ )
126137 self ._object_addr [sym .name ] = sym .entry .st_value
127138
128139 def _load_level_addr (self ):
@@ -214,8 +225,9 @@ def _process_initlevels(self):
214225 if ordinal :
215226 dev_addr = self ._object_addr [arg1_name ]
216227 _ , _ , shidx = self ._objects [dev_addr ]
217- arg0_name = self ._object_name (self ._initlevel_pointer (
218- dev_addr , DEVICE_INIT_OFFSET , shidx ))
228+ arg0_name = self ._object_name (
229+ self ._initlevel_pointer (dev_addr , DEVICE_INIT_OFFSET , shidx )
230+ )
219231
220232 prio = Priority (level , priority )
221233 self .devices [ordinal ] = (prio , arg0_name )
@@ -225,7 +237,8 @@ def _process_initlevels(self):
225237 addr += size
226238 priority += 1
227239
228- class Validator ():
240+
241+ class Validator :
229242 """Validates the initialization priorities.
230243
231244 Scans through a build folder for object files and list all the device
@@ -237,12 +250,11 @@ class Validator():
237250 edt_pickle: name of the EDT pickle file
238251 log: a logging.Logger object
239252 """
253+
240254 def __init__ (self , elf_file_path , edt_pickle , log , elf_file ):
241255 self .log = log
242256
243- edt_pickle_path = pathlib .Path (
244- pathlib .Path (elf_file_path ).parent ,
245- edt_pickle )
257+ edt_pickle_path = pathlib .Path (pathlib .Path (elf_file_path ).parent , edt_pickle )
246258 with open (edt_pickle_path , "rb" ) as f :
247259 edt = pickle .load (f )
248260
@@ -273,21 +285,25 @@ def _check_dep(self, dev_ord, dep_ord):
273285 return
274286
275287 if dev_prio == dep_prio :
276- raise ValueError (f"{ dev_node .path } and { dep_node .path } have the "
277- f"same priority: { dev_prio } " )
288+ raise ValueError (
289+ f"{ dev_node .path } and { dep_node .path } have the same priority: { dev_prio } "
290+ )
278291 elif dev_prio < dep_prio :
279292 if not self .errors :
280- self .log .error ("Device initialization priority validation failed, "
281- "the sequence of initialization calls does not match "
282- "the devicetree dependencies." )
293+ self .log .error (
294+ "Device initialization priority validation failed, "
295+ "the sequence of initialization calls does not match "
296+ "the devicetree dependencies."
297+ )
283298 self .errors += 1
284299 self .log .error (
285- f"{ dev_node .path } <{ dev_init } > is initialized before its dependency "
286- f"{ dep_node .path } <{ dep_init } > ({ dev_prio } < { dep_prio } )" )
300+ f"{ dev_node .path } <{ dev_init } > is initialized before its dependency "
301+ f"{ dep_node .path } <{ dep_init } > ({ dev_prio } < { dep_prio } )"
302+ )
287303 else :
288304 self .log .info (
289- f"{ dev_node .path } <{ dev_init } > { dev_prio } > "
290- f" { dep_node . path } < { dep_init } > { dep_prio } " )
305+ f"{ dev_node .path } <{ dev_init } > { dev_prio } > { dep_node . path } < { dep_init } > { dep_prio } "
306+ )
291307
292308 def check_edt (self ):
293309 """Scan through all known devices and validate the init priorities."""
@@ -302,30 +318,49 @@ def print_initlevels(self):
302318 for call in calls :
303319 print (f" { call } " )
304320
321+
305322def _parse_args (argv ):
306323 """Parse the command line arguments."""
307324 parser = argparse .ArgumentParser (
308325 description = __doc__ ,
309326 formatter_class = argparse .RawDescriptionHelpFormatter ,
310- allow_abbrev = False )
311-
312- parser .add_argument ("-f" , "--elf-file" , default = pathlib .Path ("build" , "zephyr" , "zephyr.elf" ),
313- help = "ELF file to use" )
314- parser .add_argument ("-v" , "--verbose" , action = "count" ,
315- help = ("enable verbose output, can be used multiple times "
316- "to increase verbosity level" ))
317- parser .add_argument ("--always-succeed" , action = "store_true" ,
318- help = "always exit with a return code of 0, used for testing" )
319- parser .add_argument ("-o" , "--output" ,
320- help = "write the output to a file in addition to stdout" )
321- parser .add_argument ("-i" , "--initlevels" , action = "store_true" ,
322- help = "print the initlevel functions instead of checking the device dependencies" )
323- parser .add_argument ("--edt-pickle" , default = pathlib .Path ("edt.pickle" ),
324- help = "name of the pickled edtlib.EDT file" ,
325- type = pathlib .Path )
327+ allow_abbrev = False ,
328+ )
329+
330+ parser .add_argument (
331+ "-f" ,
332+ "--elf-file" ,
333+ default = pathlib .Path ("build" , "zephyr" , "zephyr.elf" ),
334+ help = "ELF file to use" ,
335+ )
336+ parser .add_argument (
337+ "-v" ,
338+ "--verbose" ,
339+ action = "count" ,
340+ help = ("enable verbose output, can be used multiple times to increase verbosity level" ),
341+ )
342+ parser .add_argument (
343+ "--always-succeed" ,
344+ action = "store_true" ,
345+ help = "always exit with a return code of 0, used for testing" ,
346+ )
347+ parser .add_argument ("-o" , "--output" , help = "write the output to a file in addition to stdout" )
348+ parser .add_argument (
349+ "-i" ,
350+ "--initlevels" ,
351+ action = "store_true" ,
352+ help = "print the initlevel functions instead of checking the device dependencies" ,
353+ )
354+ parser .add_argument (
355+ "--edt-pickle" ,
356+ default = pathlib .Path ("edt.pickle" ),
357+ help = "name of the pickled edtlib.EDT file" ,
358+ type = pathlib .Path ,
359+ )
326360
327361 return parser .parse_args (argv )
328362
363+
329364def _init_log (verbose , output ):
330365 """Initialize a logger object."""
331366 log = logging .getLogger (__file__ )
@@ -348,6 +383,7 @@ def _init_log(verbose, output):
348383
349384 return log
350385
386+
351387def main (argv = None ):
352388 args = _parse_args (argv )
353389
@@ -370,5 +406,6 @@ def main(argv=None):
370406
371407 return 0
372408
409+
373410if __name__ == "__main__" :
374411 sys .exit (main (sys .argv [1 :]))
0 commit comments