test.py 3.13 KB
Newer Older
1 2 3 4
#!/usr/bin/env python3

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


16 17 18 19 20 21 22 23 24
def get_serial():
    ports = serial.tools.list_ports.comports()
    devices = [
        p.device
        for p in ports
        if (p.vid == 4292 and p.pid == 60000)
    ]
    devices.sort()
    return devices[0]
25 26


27
class ESP32(DeviceUnderTestAeadUARTP):
28

29 30 31
    def __init__(self, build_dir):
        DeviceUnderTestAeadUARTP.__init__(self)

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

38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
        self.pio_packages_path = "/home/tester/.platformio/packages/"
        self.esptoolpy_path = os.path.join(
            self.pio_packages_path, "tool-esptoolpy/esptool.py")

        self.partitionspath = os.path.join(build_dir, 'partitions.bin')
        self.firmware_path = os.path.join(build_dir, 'firmware.bin')

        # Convert elf to bin
        cmd = ['python3', self.esptoolpy_path]
        cmd += ['--chip', 'esp32']
        cmd += ['elf2image', os.path.join(build_dir, 'firmware.elf')]
        subprocess.check_call(cmd)

    def firmware_size(self):
        return os.stat(self.firmware_path).st_size
53

54 55
    def reset(self):
        self.ser.setDTR(False)  # IO0=HIGH
56
        self.ser.setRTS(True)   # EN=LOW, chip in reset1
57 58 59 60 61
        time.sleep(0.1)
        self.ser.setDTR(False)  # IO0=HIGH
        self.ser.setRTS(False)  # EN=HIGH, chip out of reset
        time.sleep(1)

62
    def flash(self):
lwc-tester committed
63
        arduinoespressif32_path = os.path.join(
64
            self.pio_packages_path, "framework-arduinoespressif32/")
lwc-tester committed
65 66 67 68 69 70 71 72 73 74
        boot_app0_path = os.path.join(
            arduinoespressif32_path,
            "tools/partitions/boot_app0.bin")
        bootloader_path = os.path.join(
            arduinoespressif32_path,
            "tools/sdk/bin/bootloader_dio_80m.bin")

        partitions = [
            (0xe000, boot_app0_path),
            (0x1000, bootloader_path),
75 76
            (0x10000, self.firmware_path),
            (0x8000, self.partitionspath)
lwc-tester committed
77 78
        ]

79
        cmd = ['python3', self.esptoolpy_path]
lwc-tester committed
80 81 82 83 84 85 86 87
        cmd += ['--chip', 'esp32']
        cmd += ['--before', 'default_reset', '--after', 'hard_reset']
        cmd += ['--port', self.uart_device]
        cmd += ['write_flash', '-z']
        for addr, path in partitions:
            cmd += ['0x%x' % addr, path]
        subprocess.check_call(cmd)

88 89
        eprint("Firmware flashed.")

90 91 92 93 94 95
        self.ser = serial.Serial(
            self.uart_device,
            baudrate=500000,
            timeout=5)
        self.reset()

96 97
    def dump_ram(self):
        return None
98 99 100


def main(argv):
lwc-tester committed
101 102
    if len(argv) != 2:
        print("Usage: test build_dir")
103
        return 1
104

lwc-tester committed
105 106 107
    build_dir = argv[1]
    kat_path = os.path.join(build_dir, 'LWC_AEAD_KAT.txt')

108 109
    dut = ESP32(build_dir)
    run_nist_lws_aead_test(dut, kat_path, build_dir, 0x0020)
lwc-tester committed
110
    return 0
111

112 113 114

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