0002-Convert-to-python3.patch 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. From 7bd43eb9e5cdf2035793d50a31bf13052eb9812a Mon Sep 17 00:00:00 2001
  2. From: Yangbo Lu <[email protected]>
  3. Date: Wed, 19 Jun 2019 10:25:41 +0800
  4. Subject: [PATCH 2/2] Convert to python3
  5. Python 2.7 will not be maintained past 2020. Let's convert
  6. to python3 for rcw. Below were the changes of this patch.
  7. - Adapted print usage
  8. - Didn't use tab anymore
  9. - Handled str and bytes type separately
  10. - Other minor changes
  11. Signed-off-by: Yangbo Lu <[email protected]>
  12. ---
  13. Makefile.inc | 2 +-
  14. rcw.py | 144 +++++++++++++++++++++++++++++------------------------------
  15. 2 files changed, 73 insertions(+), 73 deletions(-)
  16. diff --git a/Makefile.inc b/Makefile.inc
  17. index 7d9a3d3..8bee2be 100644
  18. --- a/Makefile.inc
  19. +++ b/Makefile.inc
  20. @@ -1,6 +1,6 @@
  21. DESTDIR = $(shell basename $(CURDIR))
  22. INSTALL = install
  23. -PYTHON ?= python2
  24. +PYTHON ?= python3
  25. RCW = $(PYTHON) ../rcw.py
  26. QSPI_SWAP_LIST = $(shell pwd)/../qspi_swap_list.txt
  27. QSPI_SWAP_SCRIPT=$(shell pwd)/../qspi_swap.sh
  28. diff --git a/rcw.py b/rcw.py
  29. index e5cd28b..619770a 100755
  30. --- a/rcw.py
  31. +++ b/rcw.py
  32. @@ -1,4 +1,4 @@
  33. -#!/usr/bin/env python2
  34. +#!/usr/bin/env python3
  35. # rcw.py -- compiles an RCW source file into an PBL/RCW binary
  36. @@ -95,7 +95,7 @@ from optparse import OptionParser, OptionGroup
  37. class ordered_dict(dict):
  38. def __init__(self, *args, **kwargs):
  39. dict.__init__(self, *args, **kwargs)
  40. - self._order = self.keys()
  41. + self._order = list(self.keys())
  42. def __setitem__(self, key, value):
  43. dict.__setitem__(self, key, value)
  44. @@ -132,7 +132,7 @@ def crc32(data):
  45. crc = 0xffffffff
  46. for i in data:
  47. - crc = (crc << 8) ^ table[(crc >> 24) ^ ord(i)]
  48. + crc = (crc << 8) ^ table[(crc >> 24) ^ int(i)]
  49. crc = crc & 0xffffffff
  50. return crc
  51. @@ -187,7 +187,7 @@ def command_line():
  52. options.output = '/dev/stdout'
  53. if options.reverse and not options.rcwi:
  54. - print "Error: -r option requires --rcw"
  55. + print("Error: -r option requires --rcw")
  56. sys.exit(1)
  57. # Checks if the bits for the given field overlap those of another field that
  58. @@ -196,27 +196,27 @@ def check_for_overlap(name, begin, end):
  59. global symbols
  60. if name in symbols:
  61. - print 'Error: Duplicate bitfield definition for', name
  62. + print('Error: Duplicate bitfield definition for', name)
  63. return
  64. # Iterate over the list of symbols that have already been defined
  65. - for n, [b, e] in symbols.iteritems():
  66. + for n, [b, e] in symbols.items():
  67. # check if either 'begin' or 'end' is inside an bitfield range
  68. if (b <= begin <= e) or (b <= end <= e):
  69. - print 'Error: Bitfield', name, 'overlaps with', n
  70. + print('Error: Bitfield', name, 'overlaps with', n)
  71. #
  72. # Build a u-boot PBI section for SPI/SD/NAND boot
  73. -# refer: Chapter 10, u-boot of QorIQ_SDK_Infocenter.pdf
  74. +# refer: Chapter 10, u-boot of QorIQ_SDK_Infocenter.pdf
  75. #
  76. # pre-cond 1: u-boot.xxd should be created
  77. # how to create u-boot.xxd
  78. -# xxd u-boot.bin > u-boot.xxd1 && cut -d " " -f1-10 u-boot.xxd1 > u-boot.xxd && rm -f u-boot.xxd1
  79. +# xxd u-boot.bin > u-boot.xxd1 && cut -d " " -f1-10 u-boot.xxd1 > u-boot.xxd && rm -f u-boot.xxd1
  80. #
  81. # rcw file should include spi_boot.rcw as well
  82. #
  83. def build_pbi_uboot(lines):
  84. - subsection = ''
  85. + subsection = b''
  86. cnt = 1
  87. l_tmp = []
  88. @@ -224,55 +224,55 @@ def build_pbi_uboot(lines):
  89. for l in lines:
  90. # prepare 0x40 per lines except the last one
  91. # add flush at the end
  92. - lstr = l.split()
  93. - addr = int(lstr[0][:-1], 16)
  94. + lstr = l.split()
  95. + addr = int(lstr[0][:-1], 16)
  96. # print l
  97. #
  98. # last two lines take 0x20 numbers
  99. #
  100. - if ((cnt % 2 == 0) and (cnt > len(lines) -4)):
  101. + if ((cnt % 2 == 0) and (cnt > len(lines) -4)):
  102. l_tmp.append(l)
  103. b = []
  104. - for t in l_tmp:
  105. + for t in l_tmp:
  106. lstr = t.split()
  107. - for i in range(1, len(lstr)):
  108. + for i in range(1, len(lstr)):
  109. b.append(int(lstr[i], 16))
  110. subsection += struct.pack('>LHHHHHHHHHHHHHHHH',\
  111. - 0x0C1F80000 + (addr - 0x10),\
  112. - b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7],\
  113. - b[8], b[9], b[10], b[11], b[12], b[13], b[14], b[15])
  114. + 0x0C1F80000 + (addr - 0x10),\
  115. + b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7],\
  116. + b[8], b[9], b[10], b[11], b[12], b[13], b[14], b[15])
  117. l_tmp = []
  118. #
  119. # the rest of lines take 0x40 numbers
  120. elif (cnt % 4 == 0):
  121. l_tmp.append(l)
  122. - b = []
  123. - for t in l_tmp:
  124. + b = []
  125. + for t in l_tmp:
  126. lstr = t.split()
  127. - for i in range(1, len(lstr)):
  128. + for i in range(1, len(lstr)):
  129. b.append(int(lstr[i], 16))
  130. subsection += struct.pack('>LHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH',\
  131. - 0x081F80000 + (addr - 0x30),\
  132. - b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7],\
  133. - b[8], b[9], b[10], b[11], b[12], b[13], b[14], b[15],\
  134. - b[16], b[17], b[18], b[19], b[20], b[21], b[22], b[23], \
  135. - b[24], b[25], b[26], b[27], b[28], b[29], b[30], b[31])
  136. + 0x081F80000 + (addr - 0x30),\
  137. + b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7],\
  138. + b[8], b[9], b[10], b[11], b[12], b[13], b[14], b[15],\
  139. + b[16], b[17], b[18], b[19], b[20], b[21], b[22], b[23], \
  140. + b[24], b[25], b[26], b[27], b[28], b[29], b[30], b[31])
  141. l_tmp = []
  142. else:
  143. l_tmp.append(l)
  144. - cnt = cnt + 1
  145. + cnt = cnt + 1
  146. return subsection
  147. # Build a PBI section
  148. def build_pbi(lines):
  149. - subsection = ''
  150. + subsection = b''
  151. global vars
  152. if 'pbiformat' in vars:
  153. @@ -286,9 +286,9 @@ def build_pbi(lines):
  154. for l in lines:
  155. # Check for an instruction without 0-2 parameters
  156. # The + ' ' is a hack to make the regex work for just 'flush'
  157. - m = re.match(r'\s*([a-z]+)(|\.b1|\.b2|\.b4|\.short|\.long)\s*(?<=\s)([^,]*),?([^,]*),?([^,]*),?([^,]*)', l + ' ')
  158. + m = re.match(r'\s*([a-z]+)(|\.b1|\.b2|\.b4|\.short|\.long)\s*(?<=\s)([^,]*),?([^,]*),?([^,]*),?([^,]*)', l.decode("ascii") + ' ')
  159. if not m:
  160. - print 'Unknown PBI subsection command "%s"' % l
  161. + print('Unknown PBI subsection command "%s"' % l)
  162. return ''
  163. op = m.group(1)
  164. opsize = m.group(2)
  165. @@ -306,7 +306,7 @@ def build_pbi(lines):
  166. p4 = eval(p4, {"__builtins__":None}, {}) if len(p4) else None
  167. if op == 'wait':
  168. if p1 == None:
  169. - print 'Error: "wait" instruction requires one parameter'
  170. + print('Error: "wait" instruction requires one parameter')
  171. return ''
  172. if pbiformat == 2:
  173. v1 = struct.pack(endianess + 'L', 0x80820000 | p1)
  174. @@ -318,7 +318,7 @@ def build_pbi(lines):
  175. subsection += v2
  176. elif op == 'write':
  177. if p1 == None or p2 == None:
  178. - print 'Error: "write" instruction requires two parameters'
  179. + print('Error: "write" instruction requires two parameters')
  180. return ''
  181. if pbiformat == 2:
  182. v1 = struct.pack(endianess + 'L', (opsizebytes << 28) | p1)
  183. @@ -329,7 +329,7 @@ def build_pbi(lines):
  184. subsection += v2
  185. elif op == 'awrite':
  186. if p1 == None or p2 == None:
  187. - print 'Error: "awrite" instruction requires two parameters'
  188. + print('Error: "awrite" instruction requires two parameters')
  189. return ''
  190. if pbiformat == 2:
  191. v1 = struct.pack(endianess + 'L', 0x80000000 + (opsizebytes << 26) + p1)
  192. @@ -340,10 +340,10 @@ def build_pbi(lines):
  193. subsection += v2
  194. elif op == 'poll':
  195. if pbiformat != 2:
  196. - print 'Error: "poll" not support for old PBI format'
  197. + print('Error: "poll" not support for old PBI format')
  198. return ''
  199. if p1 == None or p2 == None or p3 == None:
  200. - print 'Error: "poll" instruction requires three parameters'
  201. + print('Error: "poll" instruction requires three parameters')
  202. return ''
  203. if opsize == '.long':
  204. cmd = 0x81
  205. @@ -357,19 +357,19 @@ def build_pbi(lines):
  206. subsection += v3
  207. elif op == 'loadacwindow':
  208. if pbiformat != 2:
  209. - print 'Error: "loadacwindow" not supported for old PBI format'
  210. + print('Error: "loadacwindow" not supported for old PBI format')
  211. return ''
  212. if p1 == None:
  213. - print 'Error: "loadacwindow" instruction requires one parameter'
  214. + print('Error: "loadacwindow" instruction requires one parameter')
  215. return ''
  216. v1 = struct.pack(endianess + 'L', 0x80120000 + p1)
  217. subsection += v1
  218. elif op == 'blockcopy':
  219. if pbiformat != 2:
  220. - print 'Error: "blockcopy" not supported for old PBI format'
  221. + print('Error: "blockcopy" not supported for old PBI format')
  222. return ''
  223. if p1 == None or p2 == None or p3 == None or p4 == None:
  224. - print 'Error: "blockcopy" instruction requires four parameters'
  225. + print('Error: "blockcopy" instruction requires four parameters')
  226. return ''
  227. v1 = struct.pack(endianess + 'L', 0x80000000 + (p1 & 0xff))
  228. v2 = struct.pack(endianess + 'L', p2)
  229. @@ -382,7 +382,7 @@ def build_pbi(lines):
  230. elif op == 'flush':
  231. subsection += struct.pack('>LL', 0x09000000 | (int(vars['pbladdr'], 16) & 0x00ffff00), 0)
  232. else:
  233. - print 'Unknown PBI subsection command "%s"' % l
  234. + print('Unknown PBI subsection command "%s"' % l)
  235. return ''
  236. return subsection
  237. @@ -394,7 +394,7 @@ def parse_subsection(header, lines):
  238. elif header == "uboot":
  239. return build_pbi_uboot(lines)
  240. - print 'Error: unknown subsection "%s"' % header
  241. + print('Error: unknown subsection "%s"' % header)
  242. return ''
  243. # Parse the .rcw file, one line at a time
  244. @@ -408,10 +408,10 @@ def parse_source_file(source):
  245. symbols = ordered_dict()
  246. in_subsection = False # True == we're in a subsection
  247. - pbi = ''
  248. + pbi = b''
  249. for l2 in source:
  250. - l = re.sub(r'\s+', '', l2) # Remove all whitespace
  251. + l = re.sub(r'\s+', '', l2.decode("ascii")) # Remove all whitespace
  252. if not len(l): # Skip blank or comment-only lines
  253. continue
  254. @@ -466,14 +466,14 @@ def parse_source_file(source):
  255. (name, value) = m.groups()
  256. value = int(value, 0)
  257. if not name in symbols:
  258. - print 'Error: Unknown bitfield', name
  259. + print('Error: Unknown bitfield', name)
  260. else:
  261. if options.warnings and (name in assignments):
  262. - print 'Warning: Duplicate assignment for bitfield', name
  263. + print('Warning: Duplicate assignment for bitfield', name)
  264. assignments[name] = value
  265. continue
  266. - print 'Error: unknown command', ' '.join(l2)
  267. + print('Error: unknown command', ' '.join(l2))
  268. # Parse the -D command line parameter for additional bitfield assignments
  269. def parse_cmdline_bitfields():
  270. @@ -484,12 +484,12 @@ def parse_cmdline_bitfields():
  271. # This is the same regex as used in parse_source_file()
  272. m = re.search(r'([A-Z0-9_]+)=([0-9a-zA-Z]+)', l)
  273. if not m:
  274. - print 'Unrecognized command-line bitfield:', l
  275. + print('Unrecognized command-line bitfield:', l)
  276. else:
  277. (name, value) = m.groups()
  278. value = int(value, 0)
  279. if not name in symbols:
  280. - print 'Error: Unknown bitfield', name
  281. + print('Error: Unknown bitfield', name)
  282. else:
  283. # Don't bother printing a warning, since the command-line will
  284. # normally be used to overwrite values in the .rcw file
  285. @@ -510,7 +510,7 @@ def read_source_file(filename):
  286. global options
  287. if not find_program('gcc'):
  288. - print 'Could not find gcc in PATH'
  289. + print('Could not find gcc in PATH')
  290. return None
  291. i = ['-I', '.'] # Always look in the current directory
  292. @@ -521,7 +521,7 @@ def read_source_file(filename):
  293. shell=False, stdin=None, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  294. ret = p.communicate()
  295. if p.returncode != 0:
  296. - print ret[1],
  297. + print(ret[1],)
  298. return None
  299. return ret[0].splitlines()
  300. @@ -532,13 +532,13 @@ def check_vars():
  301. global options
  302. if not 'size' in vars:
  303. - print 'Error: "%size" variable must be specified'
  304. + print('Error: "%size" variable must be specified')
  305. sys.exit(1)
  306. if options.pbl:
  307. if 'pbiformat' in vars and int(vars['pbiformat'], 0) == 2:
  308. if 'sysaddr' in vars:
  309. - print 'Error: PBL format does not use %sysaddr'
  310. + print('Error: PBL format does not use %sysaddr')
  311. sys.exit(1)
  312. #if 'pbladdr' in vars:
  313. # print 'Error: PBL format does not use %pbladdr'
  314. @@ -546,7 +546,7 @@ def check_vars():
  315. else:
  316. # If we want the PBL header/footer, the vars for those must be defined
  317. if not 'sysaddr' in vars:
  318. - print 'Error: PBL format requires %sysaddr to be defined'
  319. + print('Error: PBL format requires %sysaddr to be defined')
  320. sys.exit(1)
  321. # Create a .bin file
  322. @@ -581,7 +581,7 @@ def create_binary():
  323. dont64bswapcrc = 0
  324. if 'dont64bswapcrc' in vars and int(vars['dont64bswapcrc'], 0):
  325. dont64bswapcrc = 1
  326. - bits = 0L
  327. + bits = 0
  328. # Magic hack. If a pbi is specified and we didn't set the size,
  329. # set it for the new format!
  330. @@ -593,7 +593,7 @@ def create_binary():
  331. pbilen += 2
  332. assignments['PBI_LENGTH'] = pbilen
  333. - for n, v in assignments.iteritems():
  334. + for n, v in assignments.items():
  335. # n = name of symbol
  336. # v = value to assign
  337. bb, ee = symbols[n] # First bit and last bit
  338. @@ -603,13 +603,13 @@ def create_binary():
  339. # Make sure it's not too large
  340. if v >= (1 << s):
  341. - print 'Error: Value', v, 'is too large for field', n
  342. + print('Error: Value', v, 'is too large for field', n)
  343. continue
  344. # If we treat the bitfield as "classic" numbered, reverse
  345. # the value before adding it!
  346. if b != bb:
  347. - v = int(bin(v)[2:].zfill(s)[::-1], 2)
  348. + v = int(bin(int(v))[2:].zfill(s)[::-1], 2)
  349. # Set the bits. We assume that bits [b:e] are already zero. They can be
  350. # non-zero only if we have overlapping bitfield definitions, which we
  351. @@ -617,7 +617,7 @@ def create_binary():
  352. bits += v << ((size - 1) - e)
  353. # Generate the binary. First, apply the preamble, if requested
  354. - binary = ''
  355. + binary = b''
  356. if options.pbl:
  357. # Starting with LS2, we have a larger field and a different
  358. # format.
  359. @@ -626,7 +626,7 @@ def create_binary():
  360. # Load RCW command
  361. binary += struct.pack(endianess + 'L', 0x80100000)
  362. else:
  363. - length_byte = (((size / 8) & 63) << 1) | 1
  364. + length_byte = (((size // 8) & 63) << 1) | 1
  365. binary += struct.pack(endianess + 'L', (length_byte << 24) | (int(vars['sysaddr'], 16) & 0x00ffffff))
  366. # Then convert 'bits' into an array of bytes
  367. @@ -634,7 +634,7 @@ def create_binary():
  368. byte = bits >> i & 0xff
  369. if classicbitnumbers:
  370. byte = int(bin(byte)[2:].zfill(8)[::-1], 2)
  371. - binary += chr(byte)
  372. + binary += bytes([byte])
  373. if options.pbl:
  374. if pbiformat == 2:
  375. @@ -672,11 +672,11 @@ def create_binary():
  376. # Precise bit any byte ordering of the CRC calculation is
  377. # not clearly specified. This is empirical.
  378. if classicbitnumbers:
  379. - newcrcbinary = ''
  380. + newcrcbinary = b''
  381. for c in crcbinary:
  382. - byte = ord(c)
  383. + byte = int(c)
  384. byte = int(bin(byte)[2:].zfill(8)[::-1], 2)
  385. - newcrcbinary += chr(byte)
  386. + newcrcbinary += bytes([byte])
  387. crcbinary = newcrcbinary
  388. # Calculate and add the CRC
  389. @@ -693,7 +693,7 @@ def create_binary():
  390. l = len(binary)
  391. if dont64bswapcrc and options.pbl:
  392. l -= 8
  393. - newbinary = ''
  394. + newbinary = b''
  395. for i in range(0, l, 8):
  396. x64 = struct.unpack('>Q', binary[i:i + 8])[0]
  397. newbinary += struct.pack('<Q', x64)
  398. @@ -771,7 +771,7 @@ def create_source():
  399. # We skip the checksum field
  400. pbi = binary[8 + (size / 8) + 4:]
  401. else:
  402. - print 'Weird binary RCW format!'
  403. + print('Weird binary RCW format!')
  404. bitbytes = ''
  405. else:
  406. if binary[0:4] == preambletst:
  407. @@ -780,7 +780,7 @@ def create_source():
  408. bitbytes = rcw
  409. pbi = binary[8 + (size / 8):]
  410. else:
  411. - print 'Weird binary RCW format!'
  412. + print('Weird binary RCW format!')
  413. bitbytes = ''
  414. else:
  415. bitbytes = binary
  416. @@ -827,16 +827,16 @@ def create_source():
  417. bits &= ~(mask << shift)
  418. if bits:
  419. - print 'Unknown bits in positions:',
  420. + print('Unknown bits in positions:',)
  421. mask = 1
  422. n = 0
  423. while bits:
  424. if (bits & mask):
  425. - print n,
  426. + print(n,)
  427. n += 1
  428. bits &= ~mask
  429. mask <<= 1
  430. - print
  431. + print()
  432. if len(pbi) > 0:
  433. l = len(pbi)
  434. @@ -953,7 +953,7 @@ def create_source():
  435. if cnt == 0:
  436. cnt = 64
  437. if i + cnt >= l:
  438. - print 'Error in write 0x%08x at offset %d within PBI\n' % (word, i)
  439. + print('Error in write 0x%08x at offset %d within PBI\n' % (word, i))
  440. if (addr & 0x00ffff00 == pbladdr):
  441. arg1 = struct.unpack(endianess + 'L', pbi[i:i+4])[0]
  442. i += 4
  443. @@ -986,8 +986,8 @@ def create_source():
  444. return source
  445. -if (sys.version_info < (2,6)) or (sys.version_info >= (3,0)):
  446. - print 'Only Python versions 2.6 or 2.7 are supported.'
  447. +if (sys.version_info < (3,0)):
  448. + print('Only Python versions 3.0+ are supported.')
  449. sys.exit(1)
  450. # Make all 'print' statements output to stderr instead of stdout
  451. --
  452. 2.7.4