conftest.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
  2. # SPDX-License-Identifier: Apache-2.0
  3. import logging
  4. import os
  5. import pytest
  6. from _pytest.fixtures import FixtureRequest
  7. from _pytest.monkeypatch import MonkeyPatch
  8. from pytest_embedded_idf.app import FlashFile
  9. from pytest_embedded_idf.serial import IdfSerial
  10. # This is a custom IdfSerial class to support custom functionality
  11. # which is required only for this test
  12. class EfuseFlashEncSerial(IdfSerial):
  13. @IdfSerial.use_esptool()
  14. def write_flash_no_enc(self) -> None:
  15. self.app.flash_settings['encrypt'] = False
  16. flash_files = []
  17. for file in self.app.flash_files:
  18. # Set encrypted flag to false for each file.
  19. flash_files.append(file._replace(encrypted=False))
  20. # Replace the original tuple with modified tuple with all the files marked as unencrypted.
  21. self.app.flash_files = tuple(flash_files)
  22. # Now flash the files
  23. self.flash()
  24. def bootloader_flash(self) -> None:
  25. """
  26. Flash bootloader.
  27. :return: None
  28. """
  29. logging.info('Flashing bootloader')
  30. bootloader_path = os.path.join(self.app.binary_path, 'bootloader', 'bootloader.bin')
  31. offs = int(self.app.sdkconfig.get('BOOTLOADER_OFFSET_IN_FLASH', 0))
  32. logging.info('bootloader offset is {0}'.format(hex(offs)))
  33. prev_flash_files = self.app.flash_files
  34. flash_files = []
  35. flash_files.append(
  36. FlashFile(
  37. offs,
  38. bootloader_path,
  39. False,
  40. )
  41. )
  42. self.app.flash_files = flash_files
  43. self.app.flash_settings['encrypt'] = False
  44. self.flash()
  45. # Restore self.app.flash files to original value
  46. self.app.flash_files = prev_flash_files
  47. def erase_field_on_emul_efuse(self, pos_of_bits: list) -> None:
  48. emul_efuse_bin_path = os.path.join(self.app.binary_path, 'emul_efuse.bin')
  49. self.dump_flash(output=emul_efuse_bin_path, partition='emul_efuse')
  50. logging.info('Erasing field on emulated efuse')
  51. def erase_bit(pos_of_bit: int) -> None:
  52. nbytes, nbits = divmod(pos_of_bit, 8)
  53. with open(emul_efuse_bin_path, 'r+b') as f:
  54. f.seek(nbytes)
  55. data = ord(f.read(1))
  56. data &= ~(1 << nbits)
  57. f.seek(-1, os.SEEK_CUR)
  58. f.write(bytes([data]))
  59. for pos_of_bit in sorted(pos_of_bits):
  60. erase_bit(pos_of_bit)
  61. offs = self.app.partition_table['emul_efuse']['offset']
  62. logging.info('emul efuse offset is {0}'.format(hex(offs)))
  63. prev_flash_files = self.app.flash_files
  64. flash_files = []
  65. flash_files.append(
  66. FlashFile(
  67. offs,
  68. emul_efuse_bin_path,
  69. False,
  70. )
  71. )
  72. self.app.flash_files = flash_files
  73. self.app.flash_settings['encrypt'] = False
  74. self.flash()
  75. self.app.flash_files = prev_flash_files
  76. @pytest.fixture(scope='module')
  77. def monkeypatch_module(request: FixtureRequest) -> MonkeyPatch:
  78. mp = MonkeyPatch()
  79. request.addfinalizer(mp.undo)
  80. return mp
  81. @pytest.fixture(scope='module', autouse=True)
  82. def replace_dut_class(monkeypatch_module: MonkeyPatch) -> None:
  83. monkeypatch_module.setattr('pytest_embedded_idf.IdfSerial', EfuseFlashEncSerial)