Timothy Chen | feaf322 | 2021-11-02 13:49:43 -0700 | [diff] [blame] | 1 | #!/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 |
| 5 | r"""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 | |
| 10 | import argparse |
| 11 | import math |
| 12 | import re |
| 13 | from pathlib import Path |
| 14 | |
| 15 | import secded_gen |
| 16 | |
| 17 | |
Timothy Chen | c1a1c18 | 2022-01-31 17:35:06 -0800 | [diff] [blame] | 18 | def _add_intg_ecc(in_val: int) -> str: |
Timothy Chen | feaf322 | 2021-11-02 13:49:43 -0700 | [diff] [blame] | 19 | 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 Chen | c1a1c18 | 2022-01-31 17:35:06 -0800 | [diff] [blame] | 28 | def _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 Chen | feaf322 | 2021-11-02 13:49:43 -0700 | [diff] [blame] | 38 | def 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 Chen | c1a1c18 | 2022-01-31 17:35:06 -0800 | [diff] [blame] | 61 | 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 Chen | feaf322 | 2021-11-02 13:49:43 -0700 | [diff] [blame] | 64 | |
| 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 | |
| 76 | if __name__ == "__main__": |
| 77 | main() |