conftest.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. # SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
  2. # SPDX-License-Identifier: Apache-2.0
  3. # pylint: disable=W0621 # redefined-outer-name
  4. import os
  5. import subprocess
  6. import sys
  7. import time
  8. import pytest
  9. import serial
  10. from _pytest.fixtures import FixtureRequest
  11. from _pytest.monkeypatch import MonkeyPatch
  12. from pytest_embedded_idf.dut import IdfDut
  13. from pytest_embedded_idf.serial import IdfSerial
  14. from pytest_embedded_serial_esp.serial import EspSerial
  15. efuse_reset_port = os.getenv('EFUSEPORT')
  16. esp_port = os.getenv('ESPPORT')
  17. # This is a custom Serial Class for the FPGA
  18. class FpgaSerial(IdfSerial):
  19. def __init__(self, *args, **kwargs) -> None: # type: ignore
  20. super().__init__(*args, **kwargs)
  21. self.efuse_reset_port = efuse_reset_port
  22. if self.efuse_reset_port is None:
  23. raise RuntimeError('EFUSEPORT not specified')
  24. self.esp_port = esp_port
  25. if self.esp_port is None:
  26. raise RuntimeError('ESPPORT not specified')
  27. @EspSerial.use_esptool(hard_reset_after=False, no_stub=True)
  28. def bootloader_flash(self, bootloader_path: str) -> None:
  29. """
  30. Flash bootloader.
  31. :return: None
  32. """
  33. offs = int(self.app.sdkconfig.get('BOOTLOADER_OFFSET_IN_FLASH', 0))
  34. if subprocess.run(
  35. f'{sys.executable} -m esptool --port {self.esp_port} --no-stub write_flash {str(offs)} {bootloader_path} --force'.split()
  36. ).returncode != 0:
  37. raise RuntimeError('Flashing the bootloader binary failed')
  38. @EspSerial.use_esptool(hard_reset_after=False, no_stub=True)
  39. def partition_table_flash(self, partition_table_path: str) -> None:
  40. """
  41. Flash Partition Table.
  42. :return: None
  43. """
  44. offs = int(self.app.flash_args['partition-table']['offset'], 16)
  45. if subprocess.run(
  46. f'{sys.executable} -m esptool --port {self.esp_port} --no-stub write_flash {str(offs)} {partition_table_path}'.split()
  47. ).returncode != 0:
  48. raise RuntimeError('Flashing the patition table binary failed')
  49. @EspSerial.use_esptool(hard_reset_after=True, no_stub=True)
  50. def app_flash(self, app_path: str) -> None:
  51. """
  52. Flash App.
  53. :return: None
  54. """
  55. offs = int(self.app.flash_args['app']['offset'], 16)
  56. if subprocess.run(
  57. f'{sys.executable} -m esptool --port {self.esp_port} --no-stub write_flash {str(offs)} {app_path}'.split()
  58. ).returncode != 0:
  59. raise RuntimeError('Flashing the app binary failed')
  60. def erase_app_header(self) -> None:
  61. """
  62. Erase app header
  63. :return None
  64. """
  65. if not os.path.exists('erase_app_header.bin'):
  66. binstr = b'\xff' * 4096
  67. with open('erase_app_header.bin', 'wb') as f:
  68. f.write(binstr)
  69. self.app_flash('erase_app_header.bin')
  70. @EspSerial.use_esptool(hard_reset_after=True, no_stub=True)
  71. def burn_efuse_key_digest(self, key: str, purpose: str, block: str) -> None:
  72. if subprocess.run(
  73. f'{sys.executable} -m espefuse --port {self.esp_port} burn_key_digest {block} {key} {purpose} --do-not-confirm'.split()
  74. ).returncode != 0:
  75. raise RuntimeError('Burning the key digest for the key {key} into the efuse block {block} failed')
  76. @EspSerial.use_esptool(hard_reset_after=False, no_stub=True)
  77. def burn_efuse(self, field: str, val: int) -> None:
  78. if subprocess.run(
  79. f'{sys.executable} -m espefuse --port {self.esp_port} burn_efuse {field} {str(val)} --do-not-confirm'.split()
  80. ).returncode != 0:
  81. raise RuntimeError(f'Burning the {field} efuse failed')
  82. @EspSerial.use_esptool(hard_reset_after=False, no_stub=True)
  83. def burn_efuse_key(self, key: str, purpose: str, block: str) -> None:
  84. if subprocess.run(
  85. f'{sys.executable} -m espefuse --port {self.esp_port} burn_key {block} {key} {purpose} --do-not-confirm'.split()
  86. ).returncode != 0:
  87. raise RuntimeError('Burning the key {key} into the efuse block {block} failed.')
  88. def reset_efuses(self) -> None:
  89. with serial.Serial(self.efuse_reset_port) as efuseport:
  90. print('Resetting efuses')
  91. efuseport.dtr = 0
  92. self.proc.setRTS(0)
  93. time.sleep(1)
  94. efuseport.dtr = 1
  95. self.proc.setRTS(1)
  96. time.sleep(1)
  97. self.proc.setRTS(0)
  98. efuseport.dtr = 0
  99. class FpgaDut(IdfDut):
  100. SECURE_BOOT_EN_KEY = None # type: str
  101. SECURE_BOOT_EN_VAL = 0
  102. def __init__(self, *args, **kwargs) -> None: # type: ignore
  103. super().__init__(*args, **kwargs)
  104. self.efuse_reset_port = efuse_reset_port
  105. class Esp32c3FpgaDut(FpgaDut):
  106. SECURE_BOOT_EN_KEY = 'SECURE_BOOT_EN'
  107. SECURE_BOOT_EN_VAL = 1
  108. WAFER_VERSION = 'WAFER_VERSION_MINOR_LO'
  109. WAFER_VERSION_VAL = 3
  110. def burn_wafer_version(self) -> None:
  111. self.serial.burn_efuse(self.WAFER_VERSION, self.WAFER_VERSION_VAL)
  112. def secure_boot_burn_en_bit(self) -> None:
  113. self.serial.burn_efuse(self.SECURE_BOOT_EN_KEY, self.SECURE_BOOT_EN_VAL)
  114. def secure_boot_burn_digest(self, digest: str, key_index: int = 0, block: int = 0) -> None:
  115. self.serial.burn_efuse_key_digest(digest, 'SECURE_BOOT_DIGEST%d' % key_index, 'BLOCK_KEY%d' % block)
  116. class Esp32s3FpgaDut(FpgaDut):
  117. SECURE_BOOT_EN_KEY = 'SECURE_BOOT_EN'
  118. SECURE_BOOT_EN_VAL = 1
  119. WAFER_VERSION = 'WAFER_VERSION_MINOR_LO'
  120. WAFER_VERSION_VAL = 1
  121. def burn_wafer_version(self) -> None:
  122. self.serial.burn_efuse(self.WAFER_VERSION, self.WAFER_VERSION_VAL)
  123. def secure_boot_burn_en_bit(self) -> None:
  124. self.serial.burn_efuse(self.SECURE_BOOT_EN_KEY, self.SECURE_BOOT_EN_VAL)
  125. def secure_boot_burn_digest(self, digest: str, key_index: int = 0, block: int = 0) -> None:
  126. self.serial.burn_efuse_key_digest(digest, 'SECURE_BOOT_DIGEST%d' % key_index, 'BLOCK_KEY%d' % block)
  127. class Esp32p4FpgaDut(FpgaDut):
  128. SECURE_BOOT_EN_KEY = 'SECURE_BOOT_EN'
  129. SECURE_BOOT_EN_VAL = 1
  130. def burn_wafer_version(self) -> None:
  131. pass
  132. def secure_boot_burn_en_bit(self) -> None:
  133. self.serial.burn_efuse(self.SECURE_BOOT_EN_KEY, self.SECURE_BOOT_EN_VAL)
  134. def secure_boot_burn_digest(self, digest: str, key_index: int = 0, block: int = 0) -> None:
  135. self.serial.burn_efuse_key_digest(digest, 'SECURE_BOOT_DIGEST%d' % key_index, 'BLOCK_KEY%d' % block)
  136. @pytest.fixture(scope='module')
  137. def monkeypatch_module(request: FixtureRequest) -> MonkeyPatch:
  138. mp = MonkeyPatch()
  139. request.addfinalizer(mp.undo)
  140. return mp
  141. @pytest.fixture(scope='module', autouse=True)
  142. def replace_dut_class(monkeypatch_module: MonkeyPatch, pytestconfig: pytest.Config) -> None:
  143. target = pytestconfig.getoption('target')
  144. if target == 'esp32c3':
  145. monkeypatch_module.setattr('pytest_embedded_idf.IdfDut', Esp32c3FpgaDut)
  146. elif target == 'esp32s3':
  147. monkeypatch_module.setattr('pytest_embedded_idf.IdfDut', Esp32s3FpgaDut)
  148. elif target == 'esp32p4':
  149. monkeypatch_module.setattr('pytest_embedded_idf.IdfDut', Esp32p4FpgaDut)
  150. monkeypatch_module.setattr('pytest_embedded_idf.IdfSerial', FpgaSerial)