From e74e0a36d9ad6a01c04500f3a24cb0ef5dd0b283 Mon Sep 17 00:00:00 2001 From: Hector Fernandez Date: Fri, 13 Mar 2020 12:17:51 +0100 Subject: Added final flashing tasks: eeprom and nand. --- test-cli/.idea/workspace.xml | 24 +---- test-cli/test/flashers/flasheeprom.py | 36 +++++++ test-cli/test/flashers/flasher.py | 10 -- test-cli/test/flashers/flasher_sopa0000.py | 13 --- test-cli/test/flashers/flashmemory.py | 12 +++ test-cli/test/helpers/finisher.py | 151 ----------------------------- test-cli/test/helpers/globalVariables.py | 1 + test-cli/test/helpers/int_registers.py | 1 + test-cli/test/helpers/syscmd.py | 1 - test-cli/test/helpers/testsrv_db.py | 49 +++++++++- test-cli/test/runners/simple.py | 6 -- test-cli/test_main.py | 77 ++++++++------- 12 files changed, 137 insertions(+), 244 deletions(-) create mode 100644 test-cli/test/flashers/flasheeprom.py delete mode 100644 test-cli/test/flashers/flasher.py delete mode 100644 test-cli/test/flashers/flasher_sopa0000.py create mode 100644 test-cli/test/flashers/flashmemory.py delete mode 100644 test-cli/test/helpers/finisher.py diff --git a/test-cli/.idea/workspace.xml b/test-cli/.idea/workspace.xml index 6b65c5c..6167250 100644 --- a/test-cli/.idea/workspace.xml +++ b/test-cli/.idea/workspace.xml @@ -1,21 +1,7 @@ - - - - - - - - - - - - - - - + \ No newline at end of file diff --git a/test-cli/test/flashers/flasheeprom.py b/test-cli/test/flashers/flasheeprom.py new file mode 100644 index 0000000..e427b87 --- /dev/null +++ b/test-cli/test/flashers/flasheeprom.py @@ -0,0 +1,36 @@ +import os +import binascii + + +def flash_eeprom(eeprompath, boarduuid, mac0, mac1=None): + print("Start programming Eeprom...") + # check if eeprompath is correct + if os.path.isfile(eeprompath): + # create u-boot data struct + data = bytearray() + data += (2029785358).to_bytes(4, 'big') # magicid --> 0x78FC110E + data += bytearray([0, 0, 0, 0]) # crc32 = 0 + data += bytearray(boarduuid + "\0", + 'ascii') # uuid --> 'c0846c8a-5fa5-11ea-8576-f8b156ac62d7' and \0 at the end + data += binascii.unhexlify(mac0.replace(':', '')) # mac0 --> 'f8:b1:56:ac:62:d7' + if mac1 is not None: + data += binascii.unhexlify(mac1.replace(':', '')) # mac1 --> 'f8:b1:56:ac:62:d7' + else: + data += bytearray([0, 0, 0, 0, 0, 0]) # mac1 --> 0:0:0:0:0:0 + # calculate crc + crc = (binascii.crc32(data, 0)).to_bytes(4, 'big') + data[4:8] = crc + # write into eeprom and read back + f = open(eeprompath, "r+b") + f.write(data) + f.seek(0) + data_rx = f.read(57) + for i in range(57): + if data_rx[i] != data[i]: + print("Error while programming eeprom memory.") + return 1 + print("Eeprom programmed succesfully.") + return 0 + else: + print("eeprom memory not found.") + return 1 diff --git a/test-cli/test/flashers/flasher.py b/test-cli/test/flashers/flasher.py deleted file mode 100644 index d962fca..0000000 --- a/test-cli/test/flashers/flasher.py +++ /dev/null @@ -1,10 +0,0 @@ -from test.flashers.flasher_sopa0000 import flash_sopa0000 - - -def flash(modelid): - result = None - - if modelid.find("SOPA0000") == 0: - result = flash_sopa0000() - - return result diff --git a/test-cli/test/flashers/flasher_sopa0000.py b/test-cli/test/flashers/flasher_sopa0000.py deleted file mode 100644 index 818be91..0000000 --- a/test-cli/test/flashers/flasher_sopa0000.py +++ /dev/null @@ -1,13 +0,0 @@ -from test.helpers.syscmd import SysCommand - - -def flash_sopa0000(): - print("Sart programming Nand memory...") - str_cmd = "/usr/bin/igep-flash --skip-nandtest --image /opt/firmware/demo-ti-image-*-*.tar* >/dev/null 2>&1" - sync_command = SysCommand("sync_command", str_cmd) - if sync_command.execute() != 0: - print("Flasher: Could not complete flashing task.") - return 1 - else: - print("Flasher: NAND memory flashed succesfully.") - return 0 diff --git a/test-cli/test/flashers/flashmemory.py b/test-cli/test/flashers/flashmemory.py new file mode 100644 index 0000000..ac59be5 --- /dev/null +++ b/test-cli/test/flashers/flashmemory.py @@ -0,0 +1,12 @@ +import sh + + +def flash_memory(imagefile): + print("Sart programming Nand memory...") + p = sh.bash("/usr/bin/igep-flash", "--skip-nandtest", "--image", "/opt/firmware/" + imagefile) + if p.exit_code != 0: + print("Flasher: Could not complete flashing task.") + return 1 + else: + print("Flasher: NAND memory flashed succesfully.") + return 0 diff --git a/test-cli/test/helpers/finisher.py b/test-cli/test/helpers/finisher.py deleted file mode 100644 index 73142d9..0000000 --- a/test-cli/test/helpers/finisher.py +++ /dev/null @@ -1,151 +0,0 @@ -from test.helpers.syscmd import SysCommand -from test.helpers.globalVariables import globalVar -import binascii -import uuid -import subprocess -from PIL import Image, ImageDraw, ImageFont -import qrcode -import PIL - -class Finisher(object): - __muhb = None - __final_to_burn_to_eeprom = None - __qr_image = None - - def __init__(self, muhb = None): - self.__muhb = muhb - self.__final_to_burn_to_eeprom = bytearray() - self.__qr_image = None - pass - - def eeprom_burn(self): - ## Create binary file - str_cmd2 = "find /sys/ -iname 'eeprom'" - eeprom_location = SysCommand("eeprom_location", str_cmd2) - if eeprom_location.execute() == 0: - raw_out = eeprom_location.getOutput() - if raw_out == "": - self.fail("Unable to get EEPROM location. IS EEPROM CONNECTED?") - eeprom = raw_out.decode('ascii') - eeprom = eeprom.strip('\n') - ## push binary data to eeprom like if working with files - file2 = open(eeprom, "w+b") - file2.write(self.__final_to_burn_to_eeprom) - else: - self.fail("failed: could not complete find eeprom command") - - def generate_qr_stamp(self): - # Generate QR to put in a stamp - qr = qrcode.QRCode( - version=1, - error_correction=qrcode.constants.ERROR_CORRECT_L, - box_size=10, - border=4, - ) - # Use board_uuid to generate the QR code - qr.add_data(globalVar.g_uuid) - qr.make(fit=True) - # Save QR as image stamp.png - img = qr.make_image() - # Store QR as a class atrib - self.__qr_image = img - img.save('/home/root/stamp.png') - - def print_stamp(self): - # Print stamp by sending a cmd to the printer - str_cmd3 = "lpr -o scaling=4 stamp.png".format(self.__display) - printstamp = SysCommand("printstamp", str_cmd3) - if printstamp.execute() != 0: - self.fail("failed: could not print stamp") - - def generate_screen(self): - # Generate green image with board uuid and QR maybe pasted on top of green image - # Define image size - W = 1250 - H = 703 - # Create blank rectangle to write on - image = Image.new('RGB', (W, H), (46, 204, 113, 0)) - draw = ImageDraw.Draw(image) - message = "TEST OK\n\n" + "Board UUID: "+ globalVar.g_uuid - font = ImageFont.truetype('/usr/share/fonts/truetype/dejavuu/DejaVuSans.ttf', 40) - # Calculate the width and height of the text to be drawn, given font size - w, h = draw.textsize(message, font=font) - # Write the text to the image, where (x,y) is the top left corner of the text - draw.text(((W-w)/2,(H-h)/2), message, align='center', font=font) - #draw.image(self.__qr_image) - image.save('/home/root/test/files/test_ok.png') - - def show_result_screen(self): - # If test OK show test_OK.png located in /home/root, If test fail show test_fail.png, located in /home/root/test/files/test_KO.png - if globalVar.fstatus: - str_cmd4 = "fbi -T 1 --noverbose -d /dev/{} test/files/test_ok.png".format('fb0') - else: - str_cmd4 = "fbi -T 1 --noverbose -d /dev/{} test/files/test_ko.png".format('fb0') - - display_image = SysCommand("display_image", str_cmd4) - #print(display_image.execute()) - if display_image.execute() != -1: - self.fail("failed: could not display the image") - - - def end_ok(self): - # Burn retrieved igep eeprom struct - new = self.__muhb[0][0] - - # Convert from string to hex - hnew = new.encode() - # Magic_id and default crc32 0x6d, 0x6a, 0x6d, 0xe4 with endianess changed so that u-boot loads it correctly - # IF magic ever changes this magic_id should be changed. At the end always magic_id of test and u-boot should - # be the same - magic_id = bytes([0xe4, 0x6d, 0x6a, 0x6d]) - default_igep_crc32 = bytes([0x00, 0x00, 0x00, 0x00]) - - # Create bytearray - to_calculate = bytearray() - # Build the final hex binary for crc32 operation - to_calculate.extend(magic_id) - to_calculate.extend(default_igep_crc32) - to_calculate.extend(hnew) - - # Calculate crc32! - new_crc32 = binascii.crc32(to_calculate) - hnew_crc32 = new_crc32.to_bytes(4, byteorder="little") - - # Recreate final eeprom struct in bytearray - self.__final_to_burn_to_eeprom = bytearray() - self.__final_to_burn_to_eeprom.extend(magic_id) - self.__final_to_burn_to_eeprom.extend(hnew_crc32) - self.__final_to_burn_to_eeprom.extend(hnew) - self.eeprom_burn() - - # Generate QR stamp - self.generate_qr_stamp() - # Send print stamp command - #self.print_stamp() - # Generate green image with board uuid and QR maybe pasted on top of green image - self.generate_screen() - # Show test_ok.png image in screen - self.show_result_screen() - - def end_fail(self): - # Burn igep eeprom bug struct - hnew = self.__muhb.encode() - default_fail = bytes([0xD0, 0xBA, 0xD0, 0xBA]) - self.__final_burn_to_eeprom = bytearray() - self.__final_to_burn_to_eeprom.extend(default_fail) - self.__final_to_burn_to_eeprom.extend(default_fail) - self.__final_to_burn_to_eeprom.extend(hnew) - self.__final_to_burn_to_eeprom.extend(default_fail) - self.__final_to_burn_to_eeprom.extend(default_fail) - self.__final_to_burn_to_eeprom.extend(default_fail) - self.__final_to_burn_to_eeprom.extend(default_fail) - self.__final_to_burn_to_eeprom.extend(default_fail) - self.__final_to_burn_to_eeprom.extend(default_fail) - self.__final_to_burn_to_eeprom.extend(default_fail) - self.__final_to_burn_to_eeprom.extend(default_fail) - self.__final_to_burn_to_eeprom.extend(default_fail) - self.__final_to_burn_to_eeprom.extend(default_fail) - self.__final_to_burn_to_eeprom.extend(default_fail) - self.eeprom_burn() - # Show test_ko.png image in screen - self.show_result_screen() \ No newline at end of file diff --git a/test-cli/test/helpers/globalVariables.py b/test-cli/test/helpers/globalVariables.py index 6b89f4d..baaae57 100644 --- a/test-cli/test/helpers/globalVariables.py +++ b/test-cli/test/helpers/globalVariables.py @@ -6,3 +6,4 @@ def globalVar(): g_mid = "" outdata = "NONE" station = "" + taskid_ctl = "" diff --git a/test-cli/test/helpers/int_registers.py b/test-cli/test/helpers/int_registers.py index 22a3d9b..0feb8da 100644 --- a/test-cli/test/helpers/int_registers.py +++ b/test-cli/test/helpers/int_registers.py @@ -55,6 +55,7 @@ def get_mac(modelid): # mac = mac + read(rg) # #erase trailing zeros # mac = mac[4::1] + # # To be finished... mac = sh.cat("/sys/class/net/eth0/address").strip() return mac diff --git a/test-cli/test/helpers/syscmd.py b/test-cli/test/helpers/syscmd.py index a869bd7..6114449 100644 --- a/test-cli/test/helpers/syscmd.py +++ b/test-cli/test/helpers/syscmd.py @@ -1,6 +1,5 @@ import unittest import subprocess -from test.helpers.globalVariables import globalVar class TestSysCommand(unittest.TestCase): diff --git a/test-cli/test/helpers/testsrv_db.py b/test-cli/test/helpers/testsrv_db.py index 9fb61fd..c9372fc 100644 --- a/test-cli/test/helpers/testsrv_db.py +++ b/test-cli/test/helpers/testsrv_db.py @@ -51,14 +51,13 @@ class TestSrv_Database(object): try: res = self.__sqlObject.db_execute_query(sql) # print(res) - return res; + return res except Exception as err: r = find_between(str(err), '#', '#') # print(r) return None def get_test_params_list(self, testid): - '''get the board test list''' sql = "SELECT * FROM isee.f_get_test_params_list({})".format(testid) # print('>>>' + sql) try: @@ -71,7 +70,6 @@ class TestSrv_Database(object): return None def open_test(self, board_uuid): - '''get the board test list''' sql = "SELECT * FROM isee.f_open_test('{}')".format(board_uuid) # print('>>>' + sql) try: @@ -84,7 +82,6 @@ class TestSrv_Database(object): return None def run_test(self, testid_ctl, testid): - '''get the board test list''' sql = "SELECT isee.f_run_test({},{})".format(testid_ctl, testid) # print('>>>' + sql) try: @@ -95,7 +92,6 @@ class TestSrv_Database(object): return None def finish_test(self, testid_ctl, testid, newstatus): - '''get the board test list''' sql = "SELECT isee.f_finish_test({},{},'{}')".format(testid_ctl, testid, newstatus) # print('>>>' + sql) try: @@ -117,3 +113,46 @@ class TestSrv_Database(object): # print(r) return None + def get_board_macaddr(self, board_uuid): + sql = "SELECT * FROM isee.f_get_board_macaddr('{}')".format(board_uuid) + # print('>>>' + sql) + try: + res = self.__sqlObject.db_execute_query(sql) + # print(res) + return res[0][0] + except Exception as err: + r = find_between(str(err), '#', '#') + # print(r) + return None + + def create_process(self, testid_ctl): + sql = "SELECT * FROM isee.f_create_process({})".format(testid_ctl) + # print('>>>' + sql) + try: + res = self.__sqlObject.db_execute_query(sql) + # print(res) + return res[0][0] + except Exception as err: + r = find_between(str(err), '#', '#') + # print(r) + return None + + def create_task_result(self, taskid_ctl, name, newstatus): + sql = "SELECT isee.f_create_task_result({},'{}','{}')".format(taskid_ctl, name, newstatus) + # print('>>>' + sql) + try: + self.__sqlObject.db_execute_query(sql) + except Exception as err: + r = find_between(str(err), '#', '#') + # print(r) + return None + + def update_taskctl_status(self, taskid_ctl, newstatus): + sql = "SELECT isee.f_update_taskctl_status({},'{}')".format(taskid_ctl, newstatus) + # print('>>>' + sql) + try: + self.__sqlObject.db_execute_query(sql) + except Exception as err: + r = find_between(str(err), '#', '#') + # print(r) + return None diff --git a/test-cli/test/runners/simple.py b/test-cli/test/runners/simple.py index f8b4dd4..a165406 100644 --- a/test-cli/test/runners/simple.py +++ b/test-cli/test/runners/simple.py @@ -1,5 +1,3 @@ -#!/usr/bin/env python - """ Simple Test Runner for unittest module @@ -7,9 +5,6 @@ Simple Test Runner for unittest module import sys import unittest -import os -from test.helpers.globalVariables import globalVar -from test.helpers.testsrv_db import TestSrv_Database class SimpleTestRunner: @@ -65,7 +60,6 @@ class TextTestResult(unittest.TestResult): def addError(self, test, err): unittest.TestResult.addError(self, test, err) test.longMessage = err[1] - print(err[1]) self.result = self.ERROR def addFailure(self, test, err): diff --git a/test-cli/test_main.py b/test-cli/test_main.py index 6e56214..6681d4e 100644 --- a/test-cli/test_main.py +++ b/test-cli/test_main.py @@ -23,8 +23,8 @@ from test.helpers.globalVariables import globalVar import socket from test.helpers.iseelogger import ISEE_Logger import logging -import binascii -from test.flashers.flasher import flash +from test.flashers.flasheeprom import flash_eeprom +from test.flashers.flashmemory import flash_memory # global variables psdbObj = TestSrv_Database() @@ -80,8 +80,6 @@ def add_test_task(suite, testdefname, paramlist): def create_testsuite(): # create an object TestSuite suite = unittest.TestSuite() - # get id of the full test for this board - globalVar.testid_ctl = psdbObj.open_test(globalVar.g_uuid) # get list of tests for this board tests = psdbObj.get_tests_list(globalVar.g_uuid) # loop in every test for this board @@ -117,41 +115,9 @@ def create_board(): get_mac(globalVar.g_mid)) -def program_eeprom(eeprompath): - print("Start programming Eeprom...") - # check if eeprompath is correct - if os.path.isfile(eeprompath): - # create u-boot data struct - data = bytearray() - data += (2029785358).to_bytes(4, 'big') # magicid --> 0x78FC110E - data += bytearray([0, 0, 0, 0]) # crc32 = 0 - data += bytearray(globalVar.g_uuid + "\0", - 'ascii') # uuid --> 'c0846c8a-5fa5-11ea-8576-f8b156ac62d7' and \0 at the end - mac0 = get_mac(globalVar.g_mid) - data += binascii.unhexlify(mac0.replace(':', '')) # mac0 --> 'f8:b1:56:ac:62:d7' - data += bytearray([0, 0, 0, 0, 0, 0]) # mac1 --> 0:0:0:0:0:0 - # calculate crc - crc = (binascii.crc32(data, 0)).to_bytes(4, 'big') - data[4:8] = crc - # write into eeprom and read back - f = open(eeprompath, "r+b") - f.write(data) - f.seek(0) - data_rx = f.read(57) - for i in range(57): - if data_rx[i] != data[i]: - print("Error while programming eeprom memory.") - return 1 - print("Eeprom programmed succesfully.") - return 0 - else: - print("eeprom memory not found.") - return 1 - - def create_boardvariables_list(uuid): varlist = {} - for row in psdbObj.get_board_variables(globalVar.g_uuid): + for row in psdbObj.get_board_variables(uuid): varname, varvalue = row varlist[varname] = varvalue return varlist @@ -160,14 +126,47 @@ def create_boardvariables_list(uuid): def main(): # initialize the board create_board() + # create a process + globalVar.testid_ctl = psdbObj.open_test(globalVar.g_uuid) + globalVar.taskid_ctl = psdbObj.create_process(globalVar.testid_ctl) # create and run tests according to the board type runner = SimpleTestRunner(psdbObj) testresult = runner.run(create_testsuite()) # execute aditional tasks, only if the test was succesfull if testresult.wasSuccessful(): + # change status to "running tasks" + psdbObj.update_taskctl_status(globalVar.taskid_ctl, "TASK_BOARD_RUNNING") + # get extra variables varlist = create_boardvariables_list(globalVar.g_uuid) - program_eeprom(varlist["eeprompath"]) - flash(globalVar.g_mid) + + resultmemory = 0 + # flash eeprom + if "eeprompath" in varlist and len(varlist["eeprompath"]) > 0: + mac0 = psdbObj.get_board_macaddr(globalVar.g_uuid) + resulteeprom = flash_eeprom(varlist["eeprompath"], globalVar.g_uuid, mac0) + psdbObj.create_task_result(globalVar.taskid_ctl, "FLASHEEPROM", + "TASK_OK" if resulteeprom == 0 else "TASK_FAIL") + else: + resulteeprom = 0 + psdbObj.create_task_result(globalVar.taskid_ctl, "FLASHEEPROM", "TASK_SKIP") + + # flash non-volatile memory + if "image" in varlist and len(varlist["image"]) > 0: + resultmemory = flash_memory(varlist["image"]) + psdbObj.create_task_result(globalVar.taskid_ctl, "FLASHMEMORY", + "TASK_OK" if resultmemory == 0 else "TASK_FAIL") + else: + resultmemory = 0 + psdbObj.create_task_result(globalVar.taskid_ctl, "FLASHMEMORY", "TASK_SKIP") + + # update status with the result + if resulteeprom == 0 and resultmemory == 0: + psdbObj.update_taskctl_status(globalVar.taskid_ctl, "TASK_BOARD_OK") + else: + psdbObj.update_taskctl_status(globalVar.taskid_ctl, "TASK_BOARD_FAIL") + else: + # change status to "not done" + psdbObj.update_taskctl_status(globalVar.taskid_ctl, "TASK_BOARD_NOT_DONE") if __name__ == "__main__": -- cgit v1.1