blob: 36d1c66f6dd465f3d215b68ff15844f660797054 [file] [log] [blame]
Timothy Chenfeaf3222021-11-02 13:49:43 -07001#!/usr/bin/env python3
2# Copyright lowRISC contributors.
3# Licensed under the Apache License, Version 2.0, see LICENSE for details.
4# SPDX-License-Identifier: Apache-2.0
5r"""Takes a compiled vmem image and processes it for flash.
6 Long term this should include both layers of ECC and scrambling,
7 The first version has only the truncated plaintext ECC.
8"""
9
10import argparse
11import math
12import re
13from pathlib import Path
14
15import secded_gen
16
17
Timothy Chenc1a1c182022-01-31 17:35:06 -080018def _add_intg_ecc(in_val: int) -> str:
Timothy Chenfeaf3222021-11-02 13:49:43 -070019 result, m = secded_gen.ecc_encode("hamming", 64, in_val)
20
21 m_nibbles = math.ceil(m / 4)
22 result = format(result, '0' + str(16 + m_nibbles) + 'x')
23
24 # due to lack of storage space, the first nibble of the ECC is truncated
25 return result[1:]
26
27
Timothy Chenc1a1c182022-01-31 17:35:06 -080028def _add_reliability_ecc(in_val: int) -> str:
29 result, m = secded_gen.ecc_encode("hamming", 68, in_val)
30
31 m_nibbles = math.ceil((68 + m) / 4)
32 result = format(result, '0' + str(m_nibbles) + 'x')
33
34 # return full result
35 return result
36
37
Timothy Chenfeaf3222021-11-02 13:49:43 -070038def main():
39 parser = argparse.ArgumentParser()
40 parser.add_argument('infile')
41 parser.add_argument('outfile')
42 args = parser.parse_args()
43
44 # open original vmem and extract relevant content
45 try:
46 vmem_orig = Path(args.infile).read_text()
47 except IOError:
48 raise Exception(f"Unable to open {args.infile}")
49
50 # search only for lines that contain data, skip all other comments
51 result = re.findall(r"^@.*$", vmem_orig, flags=re.MULTILINE)
52
53 output = []
54 for line in result:
55 items = line.split()
56 result = ""
57 for item in items:
58 if re.match(r"^@", item):
59 result += item
60 else:
Timothy Chenc1a1c182022-01-31 17:35:06 -080061 data_w_intg_ecc = _add_intg_ecc(int(item, 16))
62 full_ecc = _add_reliability_ecc(int(data_w_intg_ecc, 16))
63 result += f' {full_ecc}'
Timothy Chenfeaf3222021-11-02 13:49:43 -070064
65 # add processed element to output
66 output.append(result)
67
68 # open output file
69 outfile = open(args.outfile, "w")
70
71 # write processed content
72 for entry in output:
73 outfile.write(entry + "\n")
74
75
76if __name__ == "__main__":
77 main()