1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
|
# ---------------------------------------------------------------------------------------------------------------------
# link_bitstreams.py
# ---------------------------------------------------------------------------------------------------------------------
#
# Glue iCE40 bitstream to the main FPGA bitstream. The MKM bitstream is aligned on a sector boundary.
#
# ---------------------------------------------------------------------------------------------------------------------
#
# Copyright 2021 The Commons Conservancy Cryptech Project
#
# SPDX-License-Identifier: BSD-3-Clause
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# * Neither the name of the copyright holder nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# ---------------------------------------------------------------------------------------------------------------------
# ---------------------------------------------------------------------------------------------------------------------
# Imports
# ---------------------------------------------------------------------------------------------------------------------
import os
import sys
import math
# ---------------------------------------------------------------------------------------------------------------------
# More Imports
# ---------------------------------------------------------------------------------------------------------------------
from enum import Enum
from typing import Optional
# ---------------------------------------------------------------------------------------------------------------------
# Markers
# ---------------------------------------------------------------------------------------------------------------------
MARKER_XILINX = b"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" \
b"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" \
b"\x00\x00\x00\xBB\x11\x22\x00\x44\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" \
b"\xAA\x99\x55\x66"
MARKER_LATTICE = b"\x7E\xAA\x99\x7E"
# ---------------------------------------------------------------------------------------------------------------------
class FPGA(Enum):
Artix7 = 'artix7'
iCE40 = 'ice40'
# ---------------------------------------------------------------------------------------------------------------------
# ---------------------------------------------------------------------------------------------------------------------
# Settings
# ---------------------------------------------------------------------------------------------------------------------
PROM_PAGE_BYTES = 256
PROM_SECTOR_BYTES = 256 * PROM_PAGE_BYTES
MARKER_DICT = {FPGA.Artix7: MARKER_XILINX,
FPGA.iCE40: MARKER_LATTICE}
# ---------------------------------------------------------------------------------------------------------------------
def _load_bitstream(filename: str, fpga: FPGA) -> Optional[bytes]:
# try to load input bitstream
try:
with open(filename, 'rb') as f:
bitstream = f.read()
except FileNotFoundError:
print("File '%s' not found." % filename)
return
finally:
len_bitstream_orig = len(bitstream)
print("File '%s' loaded, %d bytes." % (filename, len_bitstream_orig))
# check magic marker and strip leading bytes
bitstream = _check_marker(bitstream, fpga)
# done
return bitstream
# ---------------------------------------------------------------------------------------------------------------------
# ---------------------------------------------------------------------------------------------------------------------
def _check_marker(bitstream: bytes, fpga: FPGA) -> Optional[bytes]:
len_bitstream_orig = len(bitstream)
marker = MARKER_DICT[fpga]
# check, that marker is present
if marker not in bitstream:
print(" Magic marker for device '%s' not found, did you specify the right filename?" % fpga.value)
return
# try to strip leading redundant bytes
while not bitstream.startswith(marker):
bitstream = bitstream[1:]
# print how many bytes we stripped
num_stripped_bytes = len_bitstream_orig - len(bitstream)
print(" Stripped %d bytes before the magic marker." % num_stripped_bytes)
# done
return bitstream
# ---------------------------------------------------------------------------------------------------------------------
# ---------------------------------------------------------------------------------------------------------------------
def _save_bit(bitstream: bytes, filename: str) -> int:
# write memory to file
with open(filename, 'wb') as f:
f.write(bitstream)
print("Output bitstream written.")
# done
return 0
# ---------------------------------------------------------------------------------------------------------------------
# ---------------------------------------------------------------------------------------------------------------------
def _link_bitstreams(filename_artix7: str, filename_ice40: str, filename_output: str) -> int:
# load bitstream
bitstream_artix7 = _load_bitstream(filename_artix7, FPGA.Artix7)
bitstream_ice40 = _load_bitstream(filename_ice40, FPGA.iCE40)
if bitstream_artix7 is None or bitstream_ice40 is None:
return 1
# grow main bitstream to sector boundary
num_sectors_artix7 = math.ceil(len(bitstream_artix7) / PROM_SECTOR_BYTES)
num_padding_bytes = num_sectors_artix7 * PROM_SECTOR_BYTES - len(bitstream_artix7)
print("Added %d byte(s) after the main bitstream to align the MKM bitmap on sector boundary." % num_padding_bytes)
padding_bytes = bytes(0xFF for _ in range(num_padding_bytes))
bitstream_artix7_padded = bitstream_artix7 + padding_bytes
# glue two bitstreams
bitstream_output = bitstream_artix7_padded + bitstream_ice40
# fill bitstream with 0xFF's up to page boundary
num_padding_bytes = PROM_PAGE_BYTES - (len(bitstream_output) % 256)
print("Added %d byte(s) after the mkm bitmap to align output on page boundary." % num_padding_bytes)
padding_bytes = bytes(0xFF for _ in range(num_padding_bytes))
bitstream_output += padding_bytes
# save bit
return _save_bit(bitstream_output, filename_output)
# ---------------------------------------------------------------------------------------------------------------------
# ---------------------------------------------------------------------------------------------------------------------
def print_usage() -> int:
print("USAGE: %s <fpga_bitstream.bit> <mkm_bitmap.bin> <combined_bitstream.bit>" %
os.path.basename(sys.argv[0]))
return 2
# ---------------------------------------------------------------------------------------------------------------------
# ---------------------------------------------------------------------------------------------------------------------
def main() -> int:
# check, that exactly three arguments were supplied
if len(sys.argv) != 4:
return print_usage()
# now run the script
return _link_bitstreams(*sys.argv[1:4])
# ---------------------------------------------------------------------------------------------------------------------
# ---------------------------------------------------------------------------------------------------------------------
if __name__ == '__main__':
sys.exit(main())
# ---------------------------------------------------------------------------------------------------------------------
# ---------------------------------------------------------------------------------------------------------------------
# End-of-File
# ---------------------------------------------------------------------------------------------------------------------
|