Commit 805ad43d authored by Carmelo Magnafico's avatar Carmelo Magnafico
Browse files

adding MDOR code reloader validator tool

parent 881bab33
Loading
Loading
Loading
Loading
+192 −0
Original line number Diff line number Diff line
#/usr/bin/env python

#########################################################
#   Credits                                             #
#   Author: Carmelo Magnafico - BepiColombo ISA Team    #
#   Association: IASP/INAF                              #
#                                                       #
#   Ver: 0.1                                            #
#########################################################


#usage
#python MDORbuilder.py ICEThcFixImage.bin MDOR_BPSA_C 0007 DM RAM 01 0000

from __future__ import division
import sys
import xml.etree.ElementTree as ET
import os
import binascii
import math
from datetime import datetime
from datetime import timedelta
import copy 
import memoryMap as memap #ISA memory mapping
import string
import numpy as np


#set some errors
class wrongdatapacklength(Exception):
    pass
class wrongAddress(Exception):
    pass
class wrongMemoryType(Exception):
    pass


#Settings
datasplit = 224 #maximum length in byte for a single data sent in one command
dataword = 16 #bit of a word
output_folder = 'output/'
INT_BITS = 16


# Function to left
# rotate n by d bits
def leftRotate(n, d):
    # In n<<d, last d bits are 0.
    # To put first 3 bits of n at
    # last, do bitwise or of n<<d
    # with n >>(INT_BITS - d)
    return ( (n << d)|(n >> (INT_BITS - d))) & ((1 << INT_BITS) - 1)
 

def checksum(data):

    chsum = 0xF900
    for i in range(int(len(data)/4)):
        chsum = leftRotate(chsum , 1) ^ int(data[i*4:(i+1)*4],INT_BITS)

    return hex(chsum);


thisdir = os.path.dirname(os.path.realpath(__file__))

inputfile = os.path.join(thisdir,"input",sys.argv[1])
datafile = os.path.join(thisdir,"output",sys.argv[2])


print("Image Source input:"+inputfile)
print("Data MDOR to check:"+datafile)


with open(inputfile, 'rb') as f:
    imagedata = binascii.hexlify(f.read())

memory = memap.ISA_DataMemory

tree = ET.parse(datafile)
MDORxml = tree.getroot()


genTime = MDORxml.find('.//genTime').text #find recursivelly ".//word"
occurrenceList = MDORxml.find('.//occurrenceList')
commands = occurrenceList.findall('.//command')

print('-------- MDOR data  ------')
print('Generation Time: %s' % genTime)
print('occurrenceList declared count: %s' % occurrenceList.get('count'))
print('<command> tag found: %i' % len(commands))
if int(occurrenceList.get('count')) == len(commands):
    print('numbers of command check: passed')
else:
    print('ERROR: declared numbers of commands and real commands doen\'t match' )

uniqueIDs = list()
commandNum = 0
checks = {'uniqueID': np.ones(len(commands), dtype=bool),'PSA06060':np.ones(len(commands), dtype=bool), 'PSA06065': np.ones(len(commands), dtype=bool), 'data': '', 'PSA06069':np.ones(len(commands), dtype=bool)  }
for command in commands:
    uniqueID = command.find('.//uniqueID')
    if uniqueID in uniqueIDs:
        checks['uniqueID'][commandNum] = False
        print('ERROR: %s already in command' % uniqueID)
    
    uniqueIDs.append(uniqueID)

    parameters = command.findall('.//parameter')

    for parameter in parameters:
        if parameter.attrib['name']=='PSA06060':
            if not parameter.find('.//value').text == 'RAM':
                checks['PSA06060'][commandNum] = False
                print('ERROR: value in PSA06060 is %s, "RAM" expected' % parameter.find('.//value').text)
                memoryID = parameter.find('.//value').text
            else:
                memoryID = 'RAM'

        if parameter.attrib['name']=='PSA06065':
            memoryaddress = parameter.find('.//value').text
            saddr = memoryaddress[-6:]
            start_address = memoryaddress[-4:]
            if not len(memoryaddress) == 8:
                checks['PSA06065'][commandNum] = False
                print('ERROR: Address in PSA06065 %s is wrong' % parameter.find('.//value').text)
            if not memoryaddress[0:2] == '00':
                checks['PSA06065'][commandNum] = False
                print('ERROR: Address in PSA06065 %s does not start with DM "00" location' % parameter.find('.//value').text)
            page_code = format(int(memoryaddress[2:4]), '02X')
            if not (page_code == '08' or  page_code == '09'):
                checks['PSA06065'][commandNum] = False
                print('ERROR: wrong page address in %s, 08 or 09 expected' % parameter.find('.//value').text)            

        if parameter.attrib['name']=='PSA06066':
            commandlen = int(parameter.find('.//value').text)
            if commandlen > datasplit*8/dataword:
                checks['PSA06066'][commandNum] = False
                print('ERROR: command data exeed the maximum %i word, %i passed' % datasplit*8/dataword, commandlen)            

            eaddr = (int(start_address,16) + int(commandlen*dataword/4) )


        if parameter.attrib['name']=='PSAY6069':
            commanddata = parameter.find('.//value').text
            checks['data'] += commanddata
            if len(commanddata) !=  int(commandlen*dataword/4):
                checks['PSA06069'][commandNum] = False
                print('ERROR: command data size %i are not of the size declared %i' % len(commanddata), commandlen)            

            dataaddr_start  = int( ((int(page_code) - 8)*65536 +int(start_address,16) ))
            dataaddr_end  = int( ((int(page_code) - 8)*65536 +int(format(eaddr,'04X'),16)))
            if not imagedata[dataaddr_start:dataaddr_end].decode('ascii') == commanddata:
                checks['PSA06069'][commandNum] = False
                print('ERROR: command data are not follow the input image')
    '''
    memorypage = memory[ memap.memorydecode(saddr,page_code+format(eaddr,'04X'))]
    if not memoryID == memory[ memap.memorydecode(saddr,saddr)]['type']:
        checks['PSA06065'][commandNum] = False
        print('ERROR: memoryID checked from PSA06065 address and from PSA06066 is %s, "RAM" expected' % memory[ memap.memorydecode(saddr,saddr)]['type'])
    '''



    commandNum = commandNum + 1

if checks['uniqueID'].all():
    print('UniqueIDs are unique: passed')
else:
    print('UniqueIDs are not unique, check for Errors')

if checks['PSA06060'].all():
    print('PSA06060 value equal to RAM: passed') 
else:
    print('PSA06060 is not all RAM value, check for Errors')

if checks['PSA06065'].all():
    print('Check data Adressed: passed')
else:
    print('data Addressed not matching please check')

dataaddr_start  = int( (int(page_code) - 8)*65536)
dataaddr_end  = int( (int(page_code) - 8)*65536 + 65536)
if imagedata[dataaddr_start:dataaddr_end].decode('ascii') == checks['data']:
    print('Compare data with image: passed')
else:
    print('Compare data with image not matching, please check')


    



+1 −0

File added.

Preview size limit exceeded, changes collapsed.