test 3.19 KB
Newer Older
1 2 3 4
#!/usr/bin/env python3

import os
import sys
5 6 7 8
import serial.tools.list_ports
from test_common import (
    DeviceUnderTestAeadUARTP,
    eprint,
lwc-tester committed
9 10
    OpenOcd,
    FileMutex,
lwc-tester committed
11
    run_nist_lws_aead_test
12
)
13 14


15 16 17 18 19 20 21 22
def get_serial():
    ports = serial.tools.list_ports.comports()
    devices = [
        p.device
        for p in ports
        if p.serial_number == 'FT2XCRZ1'
    ]
    devices.sort()
lwc-tester committed
23 24
    eprint("Serial port for BluePill is %s" % devices[0])
    return devices[0]
25 26 27 28 29 30


class BluePill(DeviceUnderTestAeadUARTP):
    RAM_SIZE = 0x5000

    def __init__(self, build_dir):
lwc-tester committed
31 32
        DeviceUnderTestAeadUARTP.__init__(self)

33 34 35 36
        self.uart_device = get_serial()
        devname = os.path.basename(self.uart_device)
        self.lock = FileMutex('/var/lock/lwc-compare.%s.lock' % devname)
        self.build_dir = build_dir
lwc-tester committed
37
        self.template_path = os.path.dirname(sys.argv[0])
38 39

        self.firmware_path = os.path.join(
lwc-tester committed
40
            build_dir, 'firmware.elf')
41 42
        self.firmware_bin_path = os.path.join(
            build_dir, 'firmware.bin')
43
        self.ram_pattern_path = os.path.join(
lwc-tester committed
44
            self.template_path, 'empty_ram.bin')
45 46 47
        self.ram_dump_path = os.path.join(
            build_dir, 'ram_dump.bin')
        self.openocd_cfg_path = os.path.join(
lwc-tester committed
48
            self.template_path, 'openocd.cfg')
49

lwc-tester committed
50 51
        self.ocd = OpenOcd(self.openocd_cfg_path)

52 53 54
    def firmware_size(self):
        return os.stat(self.firmware_bin_path).st_size

55
    def flash(self):
lwc-tester committed
56 57 58 59
        ocd_cmd = 'program %s verify reset' % self.firmware_path
        res = self.ocd.send(ocd_cmd)
        eprint(res)
        assert res == ''
60 61
        eprint("Firmware flashed.")

lwc-tester committed
62 63 64 65 66 67 68 69 70
        ocd_cmd = 'reset halt'
        res = self.ocd.send(ocd_cmd)
        eprint(res)
        assert res == ''

        ocd_cmd = 'load_image %s 0x20000000' % self.ram_pattern_path
        res = self.ocd.send(ocd_cmd)
        eprint(res)
        assert res == ''
71 72
        eprint("RAM flashed.")

lwc-tester committed
73 74 75 76 77
        ocd_cmd = 'resume'
        res = self.ocd.send(ocd_cmd)
        eprint(res)
        assert res == ''

78 79 80 81 82 83
        self.ser = serial.Serial(
            self.uart_device,
            baudrate=115200,
            timeout=5)
        self.reset()

lwc-tester committed
84 85 86 87 88 89 90
    def reset(self, halt=False):
        ocd_cmd = 'reset halt' if halt else 'reset run'
        res = self.ocd.send(ocd_cmd)
        eprint(res)
        assert res == ''
        eprint("Reset!")

91
    def dump_ram(self):
lwc-tester committed
92 93 94 95 96 97
        res = self.ocd.send(
            'dump_image %s 0x20000000 0x%x' %
            (self.ram_dump_path, BluePill.RAM_SIZE))
        eprint(res)
        assert res == ''

98 99 100 101 102 103 104 105
        eprint("RAM dumped.")
        with open(self.ram_dump_path, 'rb') as ram:
            ram = ram.read()
            if len(ram) != BluePill.RAM_SIZE:
                raise Exception(
                    "RAM dump was %d bytes instead of %d" %
                    (len(ram), BluePill.RAM_SIZE))
            return ram
106 107


108
def main(argv):
lwc-tester committed
109 110
    if len(argv) != 2:
        print("Usage: test build_dir")
111
        return 1
112

lwc-tester committed
113 114 115
    build_dir = argv[1]
    kat_path = os.path.join(build_dir, 'LWC_AEAD_KAT.txt')

116 117
    dut = BluePill(build_dir)
    run_nist_lws_aead_test(dut, kat_path, build_dir, 0x0002)
lwc-tester committed
118
    return 0
lwc-tester committed
119

120 121 122

if __name__ == "__main__":
    sys.exit(main(sys.argv))