build: Be careful with unicode and byte strings for python3 compatibility.

Signed-off-by: Johannes Krampf <johannes.krampf@googlemail.com>
diff --git a/scripts/buildrom.py b/scripts/buildrom.py
index 36de14e..8ff60e2 100755
--- a/scripts/buildrom.py
+++ b/scripts/buildrom.py
@@ -7,6 +7,8 @@
 
 import sys
 
+from python23compat import as_bytes
+
 def alignpos(pos, alignbytes):
     mask = alignbytes - 1
     return (pos + mask) & ~mask
@@ -26,7 +28,7 @@
     count = len(data)
 
     # Pad to a 512 byte boundary
-    data += "\0" * (alignpos(count, 512) - count)
+    data += as_bytes("\0") * (alignpos(count, 512) - count)
     count = len(data)
 
     # Check if a pci header is present
@@ -35,7 +37,7 @@
         data = data[:pcidata + 16] + chr(int(count/512)) + chr(0) + data[pcidata + 18:]
 
     # Fill in size field; clear checksum field
-    data = data[:2] + chr(int(count/512)) + data[3:6] + "\0" + data[7:]
+    data = data[:2] + chr(int(count/512)) + data[3:6] + as_bytes("\0") + data[7:]
 
     # Checksum rom
     newsum = (256 - checksum(data)) & 0xff
diff --git a/scripts/checkrom.py b/scripts/checkrom.py
index 30c9db2..83d4671 100755
--- a/scripts/checkrom.py
+++ b/scripts/checkrom.py
@@ -8,6 +8,8 @@
 import sys
 import layoutrom
 
+from python23compat import as_bytes
+
 def subst(data, offset, new):
     return data[:offset] + new + data[offset + len(new):]
 
@@ -24,7 +26,7 @@
     objinfo, finalsize, rawfile, outfile = sys.argv[1:]
 
     # Read in symbols
-    objinfofile = open(objinfo, 'rb')
+    objinfofile = open(objinfo, 'r')
     symbols = layoutrom.parseObjDump(objinfofile, 'in')[1]
 
     # Read in raw file
@@ -90,7 +92,7 @@
 
     # Write final file
     f = open(outfile, 'wb')
-    f.write(("\0" * (finalsize - datasize)) + rawdata)
+    f.write((as_bytes("\0") * (finalsize - datasize)) + rawdata)
     f.close()
 
 if __name__ == '__main__':
diff --git a/scripts/layoutrom.py b/scripts/layoutrom.py
index 3e57787..b325406 100755
--- a/scripts/layoutrom.py
+++ b/scripts/layoutrom.py
@@ -363,7 +363,7 @@
        int(li.zonelow_base / 16),
        li.sec16_start - BUILD_BIOS_ADDR,
        outRelSections(li.sections16, 'code16_start', useseg=1))
-    outfile = open(out16, 'wb')
+    outfile = open(out16, 'w')
     outfile.write(COMMONHEADER + out + COMMONTRAILER)
     outfile.close()
 
@@ -375,7 +375,7 @@
     }
 """ % (li.sec32seg_start - BUILD_BIOS_ADDR,
        outRelSections(li.sections32seg, 'code32seg_start', useseg=1))
-    outfile = open(out32seg, 'wb')
+    outfile = open(out32seg, 'w')
     outfile.write(COMMONHEADER + out + COMMONTRAILER)
     outfile.close()
 
@@ -445,7 +445,7 @@
         text PT_LOAD AT ( code32flat_start ) ;
 }
 """
-    outfile = open(out32flat, 'wb')
+    outfile = open(out32flat, 'w')
     outfile.write(out)
     outfile.close()
 
@@ -644,7 +644,7 @@
 
 # Parser for constants in simple C header files.
 def scanconfig(file):
-    f = open(file, 'rb')
+    f = open(file, 'r')
     opts = {}
     for l in f.readlines():
         parts = l.split()
@@ -663,9 +663,9 @@
     in16, in32seg, in32flat, cfgfile, out16, out32seg, out32flat = sys.argv[1:]
 
     # Read in the objdump information
-    infile16 = open(in16, 'rb')
-    infile32seg = open(in32seg, 'rb')
-    infile32flat = open(in32flat, 'rb')
+    infile16 = open(in16, 'r')
+    infile32seg = open(in32seg, 'r')
+    infile32flat = open(in32flat, 'r')
 
     # infoX = (sections, symbols)
     info16 = parseObjDump(infile16, '16')
diff --git a/scripts/python23compat.py b/scripts/python23compat.py
new file mode 100644
index 0000000..572b7f1
--- /dev/null
+++ b/scripts/python23compat.py
@@ -0,0 +1,14 @@
+# Helper code for compatibility of the code with both Python 2 and Python 3
+#
+# Copyright (C) 2014 Johannes Krampf <johannes.krampf@googlemail.com>
+#
+# This file may be distributed under the terms of the GNU GPLv3 license.
+
+import sys
+
+if (sys.version_info > (3, 0)):
+    def as_bytes(str):
+        return bytes(str, "ASCII")
+else:
+    def as_bytes(str):
+        return str
diff --git a/scripts/readserial.py b/scripts/readserial.py
index 5b40fdc..4f29648 100755
--- a/scripts/readserial.py
+++ b/scripts/readserial.py
@@ -13,6 +13,8 @@
 import select
 import optparse
 
+from python23compat import as_bytes
+
 # Reset time counter after this much idle time.
 RESTARTINTERVAL = 60
 # Number of bits in a transmitted byte - 8N1 is 1 start bit + 8 data
@@ -25,7 +27,7 @@
     data = data * 80
     while 1:
         st = time.time()
-        outfile.write(data)
+        outfile.write(as_bytes(data))
         outfile.flush()
         et = time.time()
         sys.stdout.write(
@@ -85,11 +87,11 @@
             msg = "\n\n======= %s (adjust=%.1fus)\n" % (
                 time.asctime(time.localtime(datatime)), byteadjust * 1000000)
             sys.stdout.write(msg)
-            logfile.write(msg)
+            logfile.write(as_bytes(msg))
         lasttime = datatime
 
         # Translate unprintable chars; add timestamps
-        out = ""
+        out = as_bytes("")
         for c in d:
             if isnewline:
                 delta = datatime - starttime - (charcount * byteadjust)
@@ -113,7 +115,10 @@
                 continue
             out += c
 
-        sys.stdout.write(out)
+        if (sys.version_info > (3, 0)):
+            sys.stdout.buffer.write(out)
+        else:
+            sys.stdout.write(out)
         sys.stdout.flush()
         logfile.write(out)
         logfile.flush()
diff --git a/scripts/transdump.py b/scripts/transdump.py
index 4caaeb7..665f04a 100755
--- a/scripts/transdump.py
+++ b/scripts/transdump.py
@@ -44,7 +44,10 @@
         filehdl = open(filename, 'r')
     mem = parseMem(filehdl)
     for i in mem:
-        sys.stdout.write(struct.pack("<I", i))
+        if (sys.version_info > (3, 0)):
+            sys.stdout.buffer.write(struct.pack("<I", i))
+        else:
+            sys.stdout.write(struct.pack("<I", i))
 
 if __name__ == '__main__':
     main()
diff --git a/scripts/vgafixup.py b/scripts/vgafixup.py
index 2493f35..a981bbf 100644
--- a/scripts/vgafixup.py
+++ b/scripts/vgafixup.py
@@ -20,7 +20,7 @@
 
 def main():
     infilename, outfilename = sys.argv[1:]
-    infile = open(infilename, 'rb')
+    infile = open(infilename, 'r')
     out = []
     for line in infile:
         sline = line.strip()
@@ -33,7 +33,7 @@
         else:
             out.append(line)
     infile.close()
-    outfile = open(outfilename, 'wb')
+    outfile = open(outfilename, 'w')
     outfile.write(''.join(out))
     outfile.close()