test_bootloader.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. # SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
  2. # SPDX-License-Identifier: Apache-2.0
  3. import logging
  4. import shutil
  5. from pathlib import Path
  6. from typing import Union
  7. from test_build_system_helpers import EnvDict, IdfPyFunc, file_contains
  8. def get_two_header_bytes(file_path: Union[str, Path]) -> str:
  9. '''
  10. get the bytes 3-4 of the given file
  11. https://docs.espressif.com/projects/esptool/en/latest/esp32/advanced-topics/firmware-image-format.html
  12. '''
  13. data = b''
  14. with open(file_path, 'rb') as f:
  15. data = f.read(4)
  16. extracted_bytes = data[2:4]
  17. return extracted_bytes.hex()
  18. def test_bootloader_custom_overrides_original(test_app_copy: Path, idf_py: IdfPyFunc, default_idf_env: EnvDict) -> None:
  19. logging.info('Custom bootloader overrides original')
  20. idf_path = Path(default_idf_env.get('IDF_PATH'))
  21. shutil.copytree(idf_path / 'components' / 'bootloader', test_app_copy / 'components' / 'bootloader')
  22. # Because of relative include of Kconfig, also esp_bootloader_format needs to be copied.
  23. shutil.copytree(idf_path / 'components' / 'esp_bootloader_format', test_app_copy / 'components' / 'esp_bootloader_format')
  24. idf_py('bootloader')
  25. assert file_contains(test_app_copy / 'build' / 'bootloader' / 'compile_commands.json',
  26. str(test_app_copy / 'components' / 'bootloader' / 'subproject' / 'main' / 'bootloader_start.c'))
  27. def test_bootloader_correctly_set_image_header(test_app_copy: Path, idf_py: IdfPyFunc) -> None:
  28. logging.info('Flash size is correctly set in the bootloader image header')
  29. # Build with the default 2MB setting
  30. idf_py('bootloader')
  31. assert get_two_header_bytes(test_app_copy / 'build' / 'bootloader' / 'bootloader.bin') == '0210'
  32. # Change to 4MB
  33. (test_app_copy / 'sdkconfig').write_text('CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y')
  34. idf_py('reconfigure', 'bootloader')
  35. assert get_two_header_bytes(test_app_copy / 'build' / 'bootloader' / 'bootloader.bin') == '0220'
  36. # Change to QIO, bootloader should still be DIO (will change to QIO in 2nd stage bootloader)
  37. (test_app_copy / 'sdkconfig').write_text('CONFIG_FLASHMODE_QIO=y')
  38. idf_py('reconfigure', 'bootloader')
  39. assert get_two_header_bytes(test_app_copy / 'build' / 'bootloader' / 'bootloader.bin') == '0210'
  40. # Change to 80 MHz
  41. (test_app_copy / 'sdkconfig').write_text('CONFIG_ESPTOOLPY_FLASHFREQ_80M=y')
  42. idf_py('reconfigure', 'bootloader')
  43. assert get_two_header_bytes(test_app_copy / 'build' / 'bootloader' / 'bootloader.bin') == '021f'