soc_core/get_mem_data: add json support

example of json file:
{
    "vmlinux.bin":    "0x00000000",
    "vmlinux.dtb":    "0x01000000",
    "initramdisk.gz": "0x01002000"
}
This commit is contained in:
Florent Kermarrec 2019-03-16 21:23:36 +01:00
parent 7bc13ba841
commit 3f386dad7d
1 changed files with 34 additions and 13 deletions

View File

@ -1,5 +1,7 @@
import os
import struct import struct
import inspect import inspect
import json
from operator import itemgetter from operator import itemgetter
from migen import * from migen import *
@ -36,24 +38,43 @@ def mem_decoder(address, start=26, end=29):
def get_mem_data(filename, endianness="big", mem_size=None): def get_mem_data(filename, endianness="big", mem_size=None):
data = [] # create memory regions
with open(filename, "rb") as mem_file: _, ext = os.path.splitext(filename)
while True: if ext == ".json":
w = mem_file.read(4) f = open(filename, "r")
if not w: regions = json.load(f)
break f.close()
if endianness == "little": else:
data.append(struct.unpack("<I", w)[0]) regions = {filename: "0x00000000"}
else:
data.append(struct.unpack(">I", w)[0]) # determine data_size
data_size = len(data)*4 data_size = 0
for filename, base in regions.items():
data_size = max(int(base, 16) + os.path.getsize(filename), data_size)
assert data_size > 0 assert data_size > 0
if mem_size is not None: if mem_size is not None:
assert data_size < mem_size, ( assert data_size < mem_size, (
"file is too big: {}/{} bytes".format( "file is too big: {}/{} bytes".format(
data_size, mem_size)) data_size, mem_size))
return data
# fill data
data = [0]*(data_size//4)
for filename, base in regions.items():
with open(filename, "rb") as f:
i = 0
while True:
w = f.read(4)
if not w:
break
if len(w) != 4:
for i in range(len(w), 4):
w += b'\x00'
if endianness == "little":
data[i] = struct.unpack("<I", w)[0]
else:
data[i] = struct.unpack(">I", w)[0]
i += 1
return data
class ReadOnlyDict(dict): class ReadOnlyDict(dict):
def __readonly__(self, *args, **kwargs): def __readonly__(self, *args, **kwargs):