| 162 |
theseven |
1 |
#!/usr/bin/env python
|
|
|
2 |
#
|
|
|
3 |
#
|
|
|
4 |
# Copyright 2010 TheSeven
|
|
|
5 |
#
|
|
|
6 |
#
|
| 427 |
farthen |
7 |
# This file is part of emCORE.
|
| 162 |
theseven |
8 |
#
|
| 427 |
farthen |
9 |
# emCORE is free software: you can redistribute it and/or
|
| 162 |
theseven |
10 |
# modify it under the terms of the GNU General Public License as
|
|
|
11 |
# published by the Free Software Foundation, either version 2 of the
|
|
|
12 |
# License, or (at your option) any later version.
|
|
|
13 |
#
|
| 427 |
farthen |
14 |
# emCORE is distributed in the hope that it will be useful,
|
| 162 |
theseven |
15 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
16 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
17 |
# See the GNU General Public License for more details.
|
|
|
18 |
#
|
|
|
19 |
# You should have received a copy of the GNU General Public License
|
| 427 |
farthen |
20 |
# along with emCORE. If not, see <http://www.gnu.org/licenses/>.
|
| 162 |
theseven |
21 |
#
|
|
|
22 |
#
|
|
|
23 |
|
|
|
24 |
import struct
|
|
|
25 |
|
| 524 |
theseven |
26 |
def encode(addr, option):
|
| 929 |
theseven |
27 |
if option == None: return b""
|
| 524 |
theseven |
28 |
addr = addr + 16
|
|
|
29 |
data1 = encode(addr, option[2])
|
|
|
30 |
if len(data1) == 0: addr1 = 0
|
|
|
31 |
else: addr1 = addr
|
| 555 |
theseven |
32 |
addr = addr + len(data1)
|
| 524 |
theseven |
33 |
data2 = encode(addr, option[3])
|
|
|
34 |
if len(data2) == 0: addr2 = 0
|
|
|
35 |
else: addr2 = addr
|
| 555 |
theseven |
36 |
addr = addr + len(data2)
|
| 929 |
theseven |
37 |
if type(option[1]) == type(b""):
|
|
|
38 |
data = option[1] + b"\0"
|
|
|
39 |
data = data.ljust((len(data) + 3) & ~3, b"\0")
|
| 524 |
theseven |
40 |
else:
|
|
|
41 |
data = ""
|
|
|
42 |
addr = option[1]
|
|
|
43 |
return struct.pack("<IIII", addr1, addr2, option[0], addr) + data1 + data2 + data
|
|
|
44 |
|
|
|
45 |
|
|
|
46 |
def configure(binary, options):
|
| 929 |
theseven |
47 |
fileaddr = binary.index(b"emCOboot")
|
| 524 |
theseven |
48 |
version = struct.unpack("<I", binary[fileaddr + 8 : fileaddr + 12])[0]
|
|
|
49 |
if version != 1: raise ValueError("Unknown boot configuration data version")
|
|
|
50 |
memaddr = struct.unpack("<I", binary[fileaddr + 12 : fileaddr + 16])[0]
|
|
|
51 |
data = encode(memaddr + 24, options)
|
|
|
52 |
if len(data) == 0: addr = 0
|
|
|
53 |
else: addr = memaddr + 24
|
|
|
54 |
return binary[:fileaddr + 16] + struct.pack("<II", len(data) + 24, addr) + data
|
| 929 |
theseven |
55 |
|