From 49b22a19625614bd25f9fbef8861ada3c3740bea Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Mon, 25 Feb 2019 15:16:19 +0800 Subject: [PATCH] foboot-bitstream: use new-style random rom Use a new pattern with a new function to generate the random ROM, used for ROM patching. Signed-off-by: Sean Cross --- foboot-bitstream.py | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/foboot-bitstream.py b/foboot-bitstream.py index 76589a5..ae1430a 100644 --- a/foboot-bitstream.py +++ b/foboot-bitstream.py @@ -82,14 +82,29 @@ class _CRG(Module): ] class RandomFirmwareROM(wishbone.SRAM): + """ + Seed the random data with a fixed number, so different bitstreams + can all share firmware. + """ def __init__(self, size, seed=2373): - import random - # Seed the random data with a fixed number, so different bitstreams - # can all share firmware. - random.seed(seed) + def xorshift32(x): + x = x ^ (x << 13) & 0xffffffff + x = x ^ (x >> 17) & 0xffffffff + x = x ^ (x << 5) & 0xffffffff + return x & 0xffffffff + + def get_rand(x): + out = 0 + for i in range(32): + x = xorshift32(x) + if (x & 1) == 1: + out = out | (1 << i) + return out & 0xffffffff data = [] + seed = 1 for d in range(int(size / 4)): - data.append(random.getrandbits(32)) + seed = get_rand(seed) + data.append(seed) print("Firmware {} bytes of random data".format(size)) wishbone.SRAM.__init__(self, size, read_only=True, init=data)