1 From 7bd43eb9e5cdf2035793d50a31bf13052eb9812a Mon Sep 17 00:00:00 2001
2 From: Yangbo Lu <yangbo.lu@nxp.com>
3 Date: Wed, 19 Jun 2019 10:25:41 +0800
4 Subject: [PATCH 2/2] Convert to python3
6 Python 2.7 will not be maintained past 2020. Let's convert
7 to python3 for rcw. Below were the changes of this patch.
9 - Didn't use tab anymore
10 - Handled str and bytes type separately
13 Signed-off-by: Yangbo Lu <yangbo.lu@nxp.com>
16 rcw.py | 144 +++++++++++++++++++++++++++++------------------------------
17 2 files changed, 73 insertions(+), 73 deletions(-)
19 diff --git a/Makefile.inc b/Makefile.inc
20 index 7d9a3d3..8bee2be 100644
24 DESTDIR = $(shell basename $(CURDIR))
28 RCW = $(PYTHON) ../rcw.py
29 QSPI_SWAP_LIST = $(shell pwd)/../qspi_swap_list.txt
30 QSPI_SWAP_SCRIPT=$(shell pwd)/../qspi_swap.sh
31 diff --git a/rcw.py b/rcw.py
32 index e5cd28b..619770a 100755
36 -#!/usr/bin/env python2
37 +#!/usr/bin/env python3
39 # rcw.py -- compiles an RCW source file into an PBL/RCW binary
41 @@ -95,7 +95,7 @@ from optparse import OptionParser, OptionGroup
42 class ordered_dict(dict):
43 def __init__(self, *args, **kwargs):
44 dict.__init__(self, *args, **kwargs)
45 - self._order = self.keys()
46 + self._order = list(self.keys())
48 def __setitem__(self, key, value):
49 dict.__setitem__(self, key, value)
50 @@ -132,7 +132,7 @@ def crc32(data):
54 - crc = (crc << 8) ^ table[(crc >> 24) ^ ord(i)]
55 + crc = (crc << 8) ^ table[(crc >> 24) ^ int(i)]
56 crc = crc & 0xffffffff
59 @@ -187,7 +187,7 @@ def command_line():
60 options.output = '/dev/stdout'
62 if options.reverse and not options.rcwi:
63 - print "Error: -r option requires --rcw"
64 + print("Error: -r option requires --rcw")
67 # Checks if the bits for the given field overlap those of another field that
68 @@ -196,27 +196,27 @@ def check_for_overlap(name, begin, end):
72 - print 'Error: Duplicate bitfield definition for', name
73 + print('Error: Duplicate bitfield definition for', name)
76 # Iterate over the list of symbols that have already been defined
77 - for n, [b, e] in symbols.iteritems():
78 + for n, [b, e] in symbols.items():
79 # check if either 'begin' or 'end' is inside an bitfield range
80 if (b <= begin <= e) or (b <= end <= e):
81 - print 'Error: Bitfield', name, 'overlaps with', n
82 + print('Error: Bitfield', name, 'overlaps with', n)
85 # Build a u-boot PBI section for SPI/SD/NAND boot
86 -# refer: Chapter 10, u-boot of QorIQ_SDK_Infocenter.pdf
87 +# refer: Chapter 10, u-boot of QorIQ_SDK_Infocenter.pdf
89 # pre-cond 1: u-boot.xxd should be created
90 # how to create u-boot.xxd
91 -# xxd u-boot.bin > u-boot.xxd1 && cut -d " " -f1-10 u-boot.xxd1 > u-boot.xxd && rm -f u-boot.xxd1
92 +# xxd u-boot.bin > u-boot.xxd1 && cut -d " " -f1-10 u-boot.xxd1 > u-boot.xxd && rm -f u-boot.xxd1
94 # rcw file should include spi_boot.rcw as well
96 def build_pbi_uboot(lines):
102 @@ -224,55 +224,55 @@ def build_pbi_uboot(lines):
104 # prepare 0x40 per lines except the last one
105 # add flush at the end
107 - addr = int(lstr[0][:-1], 16)
109 + addr = int(lstr[0][:-1], 16)
113 # last two lines take 0x20 numbers
115 - if ((cnt % 2 == 0) and (cnt > len(lines) -4)):
116 + if ((cnt % 2 == 0) and (cnt > len(lines) -4)):
124 - for i in range(1, len(lstr)):
125 + for i in range(1, len(lstr)):
126 b.append(int(lstr[i], 16))
128 subsection += struct.pack('>LHHHHHHHHHHHHHHHH',\
129 - 0x0C1F80000 + (addr - 0x10),\
130 - b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7],\
131 - b[8], b[9], b[10], b[11], b[12], b[13], b[14], b[15])
132 + 0x0C1F80000 + (addr - 0x10),\
133 + b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7],\
134 + b[8], b[9], b[10], b[11], b[12], b[13], b[14], b[15])
137 # the rest of lines take 0x40 numbers
145 - for i in range(1, len(lstr)):
146 + for i in range(1, len(lstr)):
147 b.append(int(lstr[i], 16))
149 subsection += struct.pack('>LHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH',\
150 - 0x081F80000 + (addr - 0x30),\
151 - b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7],\
152 - b[8], b[9], b[10], b[11], b[12], b[13], b[14], b[15],\
153 - b[16], b[17], b[18], b[19], b[20], b[21], b[22], b[23], \
154 - b[24], b[25], b[26], b[27], b[28], b[29], b[30], b[31])
155 + 0x081F80000 + (addr - 0x30),\
156 + b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7],\
157 + b[8], b[9], b[10], b[11], b[12], b[13], b[14], b[15],\
158 + b[16], b[17], b[18], b[19], b[20], b[21], b[22], b[23], \
159 + b[24], b[25], b[26], b[27], b[28], b[29], b[30], b[31])
169 # Build a PBI section
170 def build_pbi(lines):
175 if 'pbiformat' in vars:
176 @@ -286,9 +286,9 @@ def build_pbi(lines):
178 # Check for an instruction without 0-2 parameters
179 # The + ' ' is a hack to make the regex work for just 'flush'
180 - m = re.match(r'\s*([a-z]+)(|\.b1|\.b2|\.b4|\.short|\.long)\s*(?<=\s)([^,]*),?([^,]*),?([^,]*),?([^,]*)', l + ' ')
181 + m = re.match(r'\s*([a-z]+)(|\.b1|\.b2|\.b4|\.short|\.long)\s*(?<=\s)([^,]*),?([^,]*),?([^,]*),?([^,]*)', l.decode("ascii") + ' ')
183 - print 'Unknown PBI subsection command "%s"' % l
184 + print('Unknown PBI subsection command "%s"' % l)
188 @@ -306,7 +306,7 @@ def build_pbi(lines):
189 p4 = eval(p4, {"__builtins__":None}, {}) if len(p4) else None
192 - print 'Error: "wait" instruction requires one parameter'
193 + print('Error: "wait" instruction requires one parameter')
196 v1 = struct.pack(endianess + 'L', 0x80820000 | p1)
197 @@ -318,7 +318,7 @@ def build_pbi(lines):
200 if p1 == None or p2 == None:
201 - print 'Error: "write" instruction requires two parameters'
202 + print('Error: "write" instruction requires two parameters')
205 v1 = struct.pack(endianess + 'L', (opsizebytes << 28) | p1)
206 @@ -329,7 +329,7 @@ def build_pbi(lines):
209 if p1 == None or p2 == None:
210 - print 'Error: "awrite" instruction requires two parameters'
211 + print('Error: "awrite" instruction requires two parameters')
214 v1 = struct.pack(endianess + 'L', 0x80000000 + (opsizebytes << 26) + p1)
215 @@ -340,10 +340,10 @@ def build_pbi(lines):
219 - print 'Error: "poll" not support for old PBI format'
220 + print('Error: "poll" not support for old PBI format')
222 if p1 == None or p2 == None or p3 == None:
223 - print 'Error: "poll" instruction requires three parameters'
224 + print('Error: "poll" instruction requires three parameters')
226 if opsize == '.long':
228 @@ -357,19 +357,19 @@ def build_pbi(lines):
230 elif op == 'loadacwindow':
232 - print 'Error: "loadacwindow" not supported for old PBI format'
233 + print('Error: "loadacwindow" not supported for old PBI format')
236 - print 'Error: "loadacwindow" instruction requires one parameter'
237 + print('Error: "loadacwindow" instruction requires one parameter')
239 v1 = struct.pack(endianess + 'L', 0x80120000 + p1)
241 elif op == 'blockcopy':
243 - print 'Error: "blockcopy" not supported for old PBI format'
244 + print('Error: "blockcopy" not supported for old PBI format')
246 if p1 == None or p2 == None or p3 == None or p4 == None:
247 - print 'Error: "blockcopy" instruction requires four parameters'
248 + print('Error: "blockcopy" instruction requires four parameters')
250 v1 = struct.pack(endianess + 'L', 0x80000000 + (p1 & 0xff))
251 v2 = struct.pack(endianess + 'L', p2)
252 @@ -382,7 +382,7 @@ def build_pbi(lines):
254 subsection += struct.pack('>LL', 0x09000000 | (int(vars['pbladdr'], 16) & 0x00ffff00), 0)
256 - print 'Unknown PBI subsection command "%s"' % l
257 + print('Unknown PBI subsection command "%s"' % l)
261 @@ -394,7 +394,7 @@ def parse_subsection(header, lines):
262 elif header == "uboot":
263 return build_pbi_uboot(lines)
265 - print 'Error: unknown subsection "%s"' % header
266 + print('Error: unknown subsection "%s"' % header)
269 # Parse the .rcw file, one line at a time
270 @@ -408,10 +408,10 @@ def parse_source_file(source):
271 symbols = ordered_dict()
273 in_subsection = False # True == we're in a subsection
278 - l = re.sub(r'\s+', '', l2) # Remove all whitespace
279 + l = re.sub(r'\s+', '', l2.decode("ascii")) # Remove all whitespace
281 if not len(l): # Skip blank or comment-only lines
283 @@ -466,14 +466,14 @@ def parse_source_file(source):
284 (name, value) = m.groups()
285 value = int(value, 0)
286 if not name in symbols:
287 - print 'Error: Unknown bitfield', name
288 + print('Error: Unknown bitfield', name)
290 if options.warnings and (name in assignments):
291 - print 'Warning: Duplicate assignment for bitfield', name
292 + print('Warning: Duplicate assignment for bitfield', name)
293 assignments[name] = value
296 - print 'Error: unknown command', ' '.join(l2)
297 + print('Error: unknown command', ' '.join(l2))
299 # Parse the -D command line parameter for additional bitfield assignments
300 def parse_cmdline_bitfields():
301 @@ -484,12 +484,12 @@ def parse_cmdline_bitfields():
302 # This is the same regex as used in parse_source_file()
303 m = re.search(r'([A-Z0-9_]+)=([0-9a-zA-Z]+)', l)
305 - print 'Unrecognized command-line bitfield:', l
306 + print('Unrecognized command-line bitfield:', l)
308 (name, value) = m.groups()
309 value = int(value, 0)
310 if not name in symbols:
311 - print 'Error: Unknown bitfield', name
312 + print('Error: Unknown bitfield', name)
314 # Don't bother printing a warning, since the command-line will
315 # normally be used to overwrite values in the .rcw file
316 @@ -510,7 +510,7 @@ def read_source_file(filename):
319 if not find_program('gcc'):
320 - print 'Could not find gcc in PATH'
321 + print('Could not find gcc in PATH')
324 i = ['-I', '.'] # Always look in the current directory
325 @@ -521,7 +521,7 @@ def read_source_file(filename):
326 shell=False, stdin=None, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
327 ret = p.communicate()
328 if p.returncode != 0:
333 return ret[0].splitlines()
334 @@ -532,13 +532,13 @@ def check_vars():
337 if not 'size' in vars:
338 - print 'Error: "%size" variable must be specified'
339 + print('Error: "%size" variable must be specified')
343 if 'pbiformat' in vars and int(vars['pbiformat'], 0) == 2:
344 if 'sysaddr' in vars:
345 - print 'Error: PBL format does not use %sysaddr'
346 + print('Error: PBL format does not use %sysaddr')
348 #if 'pbladdr' in vars:
349 # print 'Error: PBL format does not use %pbladdr'
350 @@ -546,7 +546,7 @@ def check_vars():
352 # If we want the PBL header/footer, the vars for those must be defined
353 if not 'sysaddr' in vars:
354 - print 'Error: PBL format requires %sysaddr to be defined'
355 + print('Error: PBL format requires %sysaddr to be defined')
359 @@ -581,7 +581,7 @@ def create_binary():
361 if 'dont64bswapcrc' in vars and int(vars['dont64bswapcrc'], 0):
366 # Magic hack. If a pbi is specified and we didn't set the size,
367 # set it for the new format!
368 @@ -593,7 +593,7 @@ def create_binary():
370 assignments['PBI_LENGTH'] = pbilen
372 - for n, v in assignments.iteritems():
373 + for n, v in assignments.items():
375 # v = value to assign
376 bb, ee = symbols[n] # First bit and last bit
377 @@ -603,13 +603,13 @@ def create_binary():
379 # Make sure it's not too large
381 - print 'Error: Value', v, 'is too large for field', n
382 + print('Error: Value', v, 'is too large for field', n)
385 # If we treat the bitfield as "classic" numbered, reverse
386 # the value before adding it!
388 - v = int(bin(v)[2:].zfill(s)[::-1], 2)
389 + v = int(bin(int(v))[2:].zfill(s)[::-1], 2)
391 # Set the bits. We assume that bits [b:e] are already zero. They can be
392 # non-zero only if we have overlapping bitfield definitions, which we
393 @@ -617,7 +617,7 @@ def create_binary():
394 bits += v << ((size - 1) - e)
396 # Generate the binary. First, apply the preamble, if requested
400 # Starting with LS2, we have a larger field and a different
402 @@ -626,7 +626,7 @@ def create_binary():
404 binary += struct.pack(endianess + 'L', 0x80100000)
406 - length_byte = (((size / 8) & 63) << 1) | 1
407 + length_byte = (((size // 8) & 63) << 1) | 1
408 binary += struct.pack(endianess + 'L', (length_byte << 24) | (int(vars['sysaddr'], 16) & 0x00ffffff))
410 # Then convert 'bits' into an array of bytes
411 @@ -634,7 +634,7 @@ def create_binary():
412 byte = bits >> i & 0xff
413 if classicbitnumbers:
414 byte = int(bin(byte)[2:].zfill(8)[::-1], 2)
415 - binary += chr(byte)
416 + binary += bytes([byte])
420 @@ -672,11 +672,11 @@ def create_binary():
421 # Precise bit any byte ordering of the CRC calculation is
422 # not clearly specified. This is empirical.
423 if classicbitnumbers:
429 byte = int(bin(byte)[2:].zfill(8)[::-1], 2)
430 - newcrcbinary += chr(byte)
431 + newcrcbinary += bytes([byte])
432 crcbinary = newcrcbinary
434 # Calculate and add the CRC
435 @@ -693,7 +693,7 @@ def create_binary():
437 if dont64bswapcrc and options.pbl:
441 for i in range(0, l, 8):
442 x64 = struct.unpack('>Q', binary[i:i + 8])[0]
443 newbinary += struct.pack('<Q', x64)
444 @@ -771,7 +771,7 @@ def create_source():
445 # We skip the checksum field
446 pbi = binary[8 + (size / 8) + 4:]
448 - print 'Weird binary RCW format!'
449 + print('Weird binary RCW format!')
452 if binary[0:4] == preambletst:
453 @@ -780,7 +780,7 @@ def create_source():
455 pbi = binary[8 + (size / 8):]
457 - print 'Weird binary RCW format!'
458 + print('Weird binary RCW format!')
462 @@ -827,16 +827,16 @@ def create_source():
463 bits &= ~(mask << shift)
466 - print 'Unknown bits in positions:',
467 + print('Unknown bits in positions:',)
482 @@ -953,7 +953,7 @@ def create_source():
486 - print 'Error in write 0x%08x at offset %d within PBI\n' % (word, i)
487 + print('Error in write 0x%08x at offset %d within PBI\n' % (word, i))
488 if (addr & 0x00ffff00 == pbladdr):
489 arg1 = struct.unpack(endianess + 'L', pbi[i:i+4])[0]
491 @@ -986,8 +986,8 @@ def create_source():
495 -if (sys.version_info < (2,6)) or (sys.version_info >= (3,0)):
496 - print 'Only Python versions 2.6 or 2.7 are supported.'
497 +if (sys.version_info < (3,0)):
498 + print('Only Python versions 3.0+ are supported.')
501 # Make all 'print' statements output to stderr instead of stdout