summaryrefslogtreecommitdiff
path: root/dfugen.py
diff options
context:
space:
mode:
authorVegard Storheil Eriksen <zyp@jvnv.net>2013-10-11 21:19:08 +0200
committerVegard Storheil Eriksen <zyp@jvnv.net>2013-10-11 21:19:08 +0200
commit9a09653330f2af11149b83e1df42a05a6cb018dc (patch)
treeba7a72f9500039f923ebc1f021d742bed2c6c063 /dfugen.py
parent99246622402221c3a368cd1433c8f744cd9d41ea (diff)
Added bootloader.
Diffstat (limited to 'dfugen.py')
-rwxr-xr-xdfugen.py47
1 files changed, 47 insertions, 0 deletions
diff --git a/dfugen.py b/dfugen.py
new file mode 100755
index 0000000..dc93d1d
--- /dev/null
+++ b/dfugen.py
@@ -0,0 +1,47 @@
+#!/usr/bin/env python
+
+import sys, struct, zlib
+from elftools.elf.elffile import ELFFile
+
+infile = sys.argv[1]
+outfile = sys.argv[2]
+
+e = ELFFile(open(infile))
+
+buf = ''
+
+for segment in sorted(e.iter_segments(), key = lambda x: x.header.p_paddr):
+ if segment.header.p_type != 'PT_LOAD':
+ continue
+
+ data = segment.data()
+ lma = segment.header.p_paddr
+
+ # Workaround for LD aligning segments to a larger boundary than 8k.
+ if lma == 0x8000000:
+ lma += 0x2000
+ data = data[0x2000:]
+
+ # Add padding if necessary.
+ buf += '\0' * (lma - 0x8002000 - len(buf))
+
+ buf += data
+
+# Align to 64B
+if len(buf) & (64 - 1):
+ buf += '\0' * (64 - (len(buf) & (64 - 1)))
+
+# Add DFU suffix
+buf += struct.pack('<HHHH3sB',
+ 0xffff,
+ 0x5679,
+ 0x1234,
+ 0x0100,
+ 'UFD',
+ 16,
+)
+
+# Calculate CRC.
+buf += struct.pack('<I', ~zlib.crc32(buf) & 0xffffffff)
+
+open(outfile, 'w').write(buf)