test 3.11 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 38 39
        self.template_path = os.path.dirname(sys.argv[0])

        self.firmware_path = os.path.join(
            build_dir, 'firmware.elf')
40

41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
    def reset(self):
        self.ser.setDTR(False)  # IO0=HIGH
        self.ser.setRTS(True)   # EN=LOW, chip in reset
        time.sleep(0.1)
        self.ser.setDTR(False)  # IO0=HIGH
        self.ser.setRTS(False)  # EN=HIGH, chip out of reset
        time.sleep(1)

    def prepare(self):
        self.ser = serial.Serial(
            self.uart_device,
            baudrate=500000,
            timeout=5)
        self.reset()
        DeviceUnderTestAeadUARTP.prepare(self)

57
    def flash(self):
lwc-tester committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
        pio_packages_path = "/home/tester/.platformio/packages/"
        esptoolpy_path = os.path.join(
            pio_packages_path, "tool-esptoolpy/esptool.py")
        arduinoespressif32_path = os.path.join(
            pio_packages_path, "framework-arduinoespressif32/")
        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")

        elfpath = os.path.abspath(self.firmware_path)
        binpath = os.path.splitext(elfpath)[0] + '.bin'
        partpath = os.path.join(*os.path.split(elfpath)[:-1], 'partitions.bin')

        partitions = [
            (0xe000, boot_app0_path),
            (0x1000, bootloader_path),
            (0x10000, binpath),
            (0x8000, partpath)
        ]

        cmd = ['python3', esptoolpy_path]
        cmd += ['--chip', 'esp32']
        cmd += ['elf2image', elfpath]
        subprocess.check_call(cmd)

        cmd = ['python3', esptoolpy_path]
        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)

95 96 97 98
        eprint("Firmware flashed.")

    def dump_ram(self):
        return None
99 100 101


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

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

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

112 113 114

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