|
1 |
| -#!/usr/local/bin/python2.7 |
| 1 | +#!/usr/bin/python |
2 | 2 |
|
3 | 3 | import os, io, sys, math, zlib
|
4 | 4 | from ConfigParser import ConfigParser
|
|
15 | 15 | new_image = open('data/rebuilt_image.bin','w')
|
16 | 16 | new_image.truncate()
|
17 | 17 |
|
18 |
| -for i in range(0,config.getint('flash','total_size')): |
19 |
| - new_image.write("\xFF") |
| 18 | +if config.get('global','type') == 'aspeed': |
| 19 | + aspeed = True |
| 20 | +else: |
| 21 | + aspeed = False |
| 22 | + |
| 23 | +total_size = config.getint('flash','total_size') |
| 24 | +if not aspeed: |
| 25 | + for i in range(0,total_size): |
| 26 | + new_image.write('\xFF') |
| 27 | +else: |
| 28 | + # Apseed image has some parts filled with nulls |
| 29 | + # it probably does not matter, but let's try make identical image |
| 30 | + if total_size < 0x1F40000: |
| 31 | + print "Unexpected ASpeed image size" |
| 32 | + os.exit(1) |
| 33 | + for i in range(0,0x100000): |
| 34 | + new_image.write('\xFF') |
| 35 | + for i in range(0x100000,0x400000): |
| 36 | + new_image.write('\x00') |
| 37 | + for i in range(0x400000,0x1F40000): |
| 38 | + new_image.write('\xFF') |
| 39 | + for i in range(0x1F40000,total_size): |
| 40 | + new_image.write('\x00') |
20 | 41 |
|
21 | 42 | new_image.seek(0)
|
22 | 43 |
|
23 |
| -print "Writing bootloader..." |
24 |
| -with open('data/bootloader.bin','r') as f: |
25 |
| - new_image.write(f.read()) |
| 44 | +if not aspeed: |
| 45 | + print "Writing bootloader..." |
| 46 | + with open('data/bootloader.bin','r') as f: |
| 47 | + new_image.write(f.read()) |
26 | 48 |
|
27 | 49 | images = []
|
28 | 50 | for (imagenum, dummy) in config.items('images'):
|
|
39 | 61 | # can't use getint, it doesn't support hex
|
40 | 62 | old_length = int(config.get(configkey, 'length'),0)
|
41 | 63 | base_addr = int(config.get(configkey, 'base_addr'),0)
|
42 |
| - load_addr = int(config.get(configkey, 'load_addr'),0) |
43 |
| - exec_addr = int(config.get(configkey, 'exec_addr'),0) |
| 64 | + if not aspeed: |
| 65 | + load_addr = int(config.get(configkey, 'load_addr'),0) |
| 66 | + exec_addr = int(config.get(configkey, 'exec_addr'),0) |
| 67 | + type = int(config.get(configkey, 'type'),0) |
44 | 68 | name = config.get(configkey, 'name')
|
45 |
| - type = int(config.get(configkey, 'type'),0) |
46 | 69 |
|
47 |
| - imagestart = base_addr |
48 |
| - if imagestart > 0x40000000: |
49 |
| - # I'm unsure where this 0x40000000 byte offset is coming from. Perhaps I'm not parsing the footer correctly? |
50 |
| - imagestart -= 0x40000000 |
| 70 | + imagestart = base_addr |
| 71 | + if imagestart > 0x40000000: |
| 72 | + # I'm unsure where this 0x40000000 byte offset is coming from. Perhaps I'm not parsing the footer correctly? |
| 73 | + imagestart -= 0x40000000 |
51 | 74 |
|
52 | 75 | if imagestart < new_image.tell():
|
53 | 76 | print "ERROR: Previous image was too big, and has overriten data where the current image should begin."
|
|
57 | 80 | # Seek to where this image will start
|
58 | 81 | new_image.seek(imagestart)
|
59 | 82 |
|
60 |
| - with open('data/%s.bin' % name,'r') as img: |
| 83 | + if name[-4:] != '.bin': |
| 84 | + name = name + '.bin' |
| 85 | + with open('data/%s' % name,'r') as img: |
61 | 86 | cur_image = img.read()
|
62 | 87 |
|
63 | 88 | # Write the actual image contents
|
|
67 | 92 | curcrc = zlib.crc32(cur_image) & 0xffffffff
|
68 | 93 | imagecrc.append(curcrc)
|
69 | 94 |
|
70 |
| - # Prepare the image footer based on the data we've stored previously |
71 |
| - fi = FirmwareImage() |
72 |
| - fi.imagenum = imagenum |
73 |
| - fi.base_address = base_addr |
74 |
| - fi.exec_address = exec_addr |
75 |
| - fi.load_address = load_addr |
76 |
| - fi.name = name |
77 |
| - fi.image_checksum = FirmwareImage.computeChecksum(cur_image) |
78 |
| - fi.type = type |
79 |
| - fi.length = len(cur_image) |
| 95 | + config.set(configkey, 'curcrc', '0x%x' % curcrc) |
| 96 | + config.set(configkey, 'curlen', '0x%x' % len(cur_image)) |
80 | 97 |
|
81 |
| - # Calculate the new checksum.. |
82 |
| - fi.footer_checksum = fi.computeFooterChecksum() |
| 98 | + if not aspeed: |
| 99 | + # Prepare the image footer based on the data we've stored previously |
| 100 | + fi = FirmwareImage() |
| 101 | + fi.imagenum = imagenum |
| 102 | + fi.base_address = base_addr |
| 103 | + fi.exec_address = exec_addr |
| 104 | + fi.load_address = load_addr |
| 105 | + fi.type = type |
| 106 | + fi.name = name |
| 107 | + fi.image_checksum = FirmwareImage.computeChecksum(cur_image) |
| 108 | + fi.length = len(cur_image) |
83 | 109 |
|
84 |
| - # flash chip breaks data down into 64KB blocks. Footer should be at the end of one of these |
85 |
| - curblock = int(math.floor(new_image.tell() / 65536)) |
| 110 | + # Calculate the new checksum.. |
| 111 | + fi.footer_checksum = fi.computeFooterChecksum() |
86 | 112 |
|
87 |
| - curblockend = curblock * 65536 |
| 113 | + # flash chip breaks data down into 64KB blocks. Footer should be at the end of one of these |
| 114 | + curblock = int(math.floor(new_image.tell() / 65536)) |
88 | 115 |
|
89 |
| - last_image_end = new_image.tell() |
| 116 | + curblockend = curblock * 65536 |
90 | 117 |
|
91 |
| - # If we don't have space to write the footer at the end of the current block, move to the next block |
92 |
| - if curblockend - 61 < last_image_end: |
93 |
| - curblock += 1 |
| 118 | + last_image_end = new_image.tell() |
94 | 119 |
|
95 |
| - footerpos = (curblock * 65536) - 61 |
| 120 | + # If we don't have space to write the footer at the end of the current block, move to the next block |
| 121 | + if curblockend - 61 < last_image_end: |
| 122 | + curblock += 1 |
96 | 123 |
|
97 |
| - new_image.seek(footerpos) |
| 124 | + footerpos = (curblock * 65536) - 61 |
98 | 125 |
|
99 |
| - # And write the footer to the output file |
100 |
| - new_image.write(fi.getRawString()) |
| 126 | + new_image.seek(footerpos) |
101 | 127 |
|
| 128 | + # And write the footer to the output file |
| 129 | + new_image.write(fi.getRawString()) |
| 130 | + |
| 131 | + else: |
| 132 | + footerpos = new_image.tell() |
| 133 | + if curcrc == int(config.get(configkey, 'checksum'), 0): |
| 134 | + print " Image unchanged" |
| 135 | + else: |
| 136 | + print " Image modified" |
102 | 137 |
|
103 | 138 | footer = FirmwareFooter()
|
104 | 139 | footer.rev1 = int(config.get('global','major_version'),0)
|
105 | 140 | footer.rev2 = int(config.get('global','minor_version'),0)
|
106 | 141 | footer.footerver = int(config.get('global','footer_version'),0)
|
107 | 142 | footer.checksum = footer.computeFooterChecksum(imagecrc)
|
108 | 143 |
|
109 |
| -# Hmm... no documentation on where this should be, but in the firmware I have it's been palced right before the last image footer |
110 |
| -# Unsure if that's where it goes, or if it doesn't matter |
111 |
| -# 16 includes 8 padding \xFF's between the global footer and the last image footer |
112 |
| -global_start = footerpos-16 |
113 |
| -if global_start < curblockend: |
114 |
| - print "ERROR: Would have written global footer over last image" |
115 |
| - print "Aborting" |
116 |
| - sys.exit(1) |
| 144 | +if not aspeed: |
| 145 | + # Hmm... no documentation on where this should be, but in the firmware I have it's been palced right before the last image footer |
| 146 | + # Unsure if that's where it goes, or if it doesn't matter |
| 147 | + # 16 includes 8 padding \xFF's between the global footer and the last image footer |
| 148 | + global_start = footerpos-16 |
| 149 | + if global_start < curblockend: |
| 150 | + print "ERROR: Would have written global footer over last image" |
| 151 | + print "Aborting" |
| 152 | + sys.exit(1) |
| 153 | +else: |
| 154 | + # ASpeed seems to place global footer right after the last image |
| 155 | + global_start = footerpos - 10 |
| 156 | + crc2 = config.get('image_2', 'curcrc') |
| 157 | + len2 = config.get('image_2', 'curlen') |
| 158 | + crc4 = config.get('image_4', 'curcrc') |
| 159 | + len4 = config.get('image_4', 'curlen') |
| 160 | + footer.rootfs_nfo = '%4s%4s' % (crc2[2:6], len2[2:6]) |
| 161 | + footer.webfs_nfo = '%4s%4s' % (crc4[2:6], len4[2:6]) |
117 | 162 |
|
118 | 163 | # Write the global footer
|
119 | 164 | new_image.seek(global_start)
|
120 | 165 | new_image.write(footer.getRawString())
|
121 | 166 |
|
| 167 | +# add ASpeed specific index |
| 168 | +if aspeed: |
| 169 | + new_image.seek(0x1FC0000) |
| 170 | + for imagenum in images: |
| 171 | + configkey = 'image_%i' % imagenum |
| 172 | + img_base = int(config.get(configkey, 'base_addr'),0) |
| 173 | + img_len = int(config.get(configkey, 'curlen'),0) |
| 174 | + img_crc = int(config.get(configkey, 'curcrc'),0) |
| 175 | + img_name = config.get(configkey, 'name') |
| 176 | + |
| 177 | + new_image.write("[img]: ") |
| 178 | + new_image.write("%x %x %x %s" % (img_base, img_len, img_crc, img_name)) |
| 179 | + new_image.write("[end]") |
| 180 | + |
122 | 181 | print "Done, new firmware written to data/rebuild_image.bin"
|
123 | 182 |
|
0 commit comments