pytest_system_efuse_example.py 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030
  1. # SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
  2. # SPDX-License-Identifier: Unlicense OR CC0-1.0
  3. from __future__ import unicode_literals
  4. import logging
  5. import os
  6. import pytest
  7. from pytest_embedded import Dut
  8. from pytest_embedded_qemu.dut import QemuDut
  9. @pytest.mark.generic
  10. @pytest.mark.esp32
  11. @pytest.mark.esp32c2
  12. @pytest.mark.esp32c3
  13. def test_examples_efuse(dut: Dut) -> None:
  14. dut.expect(r'example: Coding Scheme (3/4)|(NONE)|(REPEAT)|(RS \(Reed-Solomon coding\))', timeout=20)
  15. dut.expect(['example: read efuse fields',
  16. r'example: 1. read MAC address: {}'.format(r':'.join((r'[0-9a-f]{2}',) * 6)),
  17. 'example: 2. read secure_version: 0',
  18. 'example: 3. read custom fields',
  19. 'example: module_version = 0',
  20. 'example: device_role = None',
  21. 'example: setting_1 = 0',
  22. 'example: setting_2 = 0',
  23. 'example: custom_secure_version = 0',
  24. 'example: This example does not burn any efuse in reality only virtually',
  25. 'example: Write operations in efuse fields are performed virtually',
  26. 'example: write custom efuse fields',
  27. 'efuse: Virtual efuses enabled: Not really burning eFuses',
  28. 'example: module_version = 1',
  29. 'example: device_role = Slave',
  30. 'example: setting_1 = 3',
  31. 'example: setting_2 = 4',
  32. 'example: custom_secure_version = 5',
  33. 'example: Done'], expect_all=True)
  34. @pytest.mark.generic
  35. @pytest.mark.esp32
  36. @pytest.mark.esp32c2
  37. @pytest.mark.esp32c3
  38. @pytest.mark.esp32c6
  39. @pytest.mark.esp32h2
  40. @pytest.mark.esp32s2
  41. @pytest.mark.esp32s3
  42. @pytest.mark.parametrize('config', ['virt_flash_enc',], indirect=True)
  43. @pytest.mark.parametrize('skip_autoflash', ['y'], indirect=True)
  44. def test_examples_efuse_with_virt_flash_enc(dut: Dut) -> None:
  45. # check and log bin size
  46. binary_file = os.path.join(dut.app.binary_path, 'bootloader', 'bootloader.bin')
  47. bin_size = os.path.getsize(binary_file)
  48. logging.info('{}_bootloader_virt_flash_enc_bin_size: {}KB'.format(dut.app.target, bin_size // 1024))
  49. print(' - Erase flash')
  50. dut.serial.erase_flash()
  51. print(' - Start app (flash partition_table and app)')
  52. dut.serial.write_flash_no_enc()
  53. dut.expect('Loading virtual efuse blocks from real efuses')
  54. dut.expect('Checking flash encryption...')
  55. dut.expect('Generating new flash encryption key...')
  56. if dut.app.target == 'esp32':
  57. dut.expect(r'Writing EFUSE_BLK_KEY\d with purpose 2')
  58. dut.expect('Setting CRYPT_CONFIG efuse to 0xF')
  59. dut.expect('Not disabling UART bootloader encryption')
  60. dut.expect('Disable UART bootloader decryption...')
  61. dut.expect('Disable UART bootloader MMU cache...')
  62. dut.expect('Disable JTAG...')
  63. dut.expect('Disable ROM BASIC interpreter fallback...')
  64. else:
  65. if dut.app.target == 'esp32c2':
  66. dut.expect(r'Writing EFUSE_BLK_KEY\d with purpose 1')
  67. else:
  68. dut.expect(r'Writing EFUSE_BLK_KEY\d with purpose 4')
  69. dut.expect('Not disabling UART bootloader encryption')
  70. if dut.app.target != 'esp32h2':
  71. dut.expect('Disable UART bootloader cache...')
  72. dut.expect('Disable JTAG...')
  73. dut.expect('bootloader encrypted successfully')
  74. dut.expect('partition table encrypted and loaded successfully')
  75. dut.expect('Flash encryption completed', timeout=90)
  76. dut.expect('Resetting with flash encryption enabled...')
  77. dut.expect('Loading virtual efuse blocks from flash')
  78. dut.expect('Checking flash encryption...')
  79. if dut.app.target == 'esp32':
  80. dut.expect_exact('flash encryption is enabled (3 plaintext flashes left)', timeout=3)
  81. else:
  82. dut.expect_exact('flash encryption is enabled (1 plaintext flashes left)')
  83. dut.expect_exact('Flash encryption mode is DEVELOPMENT (not secure)')
  84. dut.expect('Start eFuse example')
  85. dut.expect('example: Done')
  86. @pytest.mark.generic
  87. @pytest.mark.esp32s2
  88. @pytest.mark.parametrize('config', ['virt_flash_enc_aes_256',], indirect=True)
  89. @pytest.mark.parametrize('skip_autoflash', ['y'], indirect=True)
  90. def test_examples_efuse_with_virt_flash_enc_aes_256(dut: Dut) -> None:
  91. # Only ESP32-S2 has support AES-256 FLASH_ENCRYPTION key
  92. # check and log bin size
  93. binary_file = os.path.join(dut.app.binary_path, 'bootloader', 'bootloader.bin')
  94. bin_size = os.path.getsize(binary_file)
  95. logging.info('{}_bootloader_virt_flash_enc_aes_256_bin_size: {}KB'.format(dut.app.target, bin_size // 1024))
  96. print(' - Erase flash')
  97. dut.serial.erase_flash()
  98. print(' - Start app (flash partition_table and app)')
  99. dut.serial.write_flash_no_enc()
  100. dut.expect('Loading virtual efuse blocks from real efuses')
  101. dut.expect('Checking flash encryption...')
  102. dut.expect('Generating new flash encryption key...')
  103. dut.expect(r'Writing EFUSE_BLK_KEY\d with purpose 2')
  104. dut.expect(r'Writing EFUSE_BLK_KEY\d with purpose 3')
  105. dut.expect('Not disabling UART bootloader encryption')
  106. if dut.app.target != 'esp32h2':
  107. dut.expect('Disable UART bootloader cache...')
  108. dut.expect('Disable JTAG...')
  109. dut.expect('bootloader encrypted successfully')
  110. dut.expect('partition table encrypted and loaded successfully')
  111. dut.expect('Flash encryption completed', timeout=90)
  112. dut.expect('Resetting with flash encryption enabled...')
  113. dut.expect('Loading virtual efuse blocks from flash')
  114. dut.expect('Checking flash encryption...')
  115. dut.expect_exact('flash encryption is enabled (1 plaintext flashes left)')
  116. dut.expect_exact('Flash encryption mode is DEVELOPMENT (not secure)')
  117. dut.expect('Start eFuse example')
  118. dut.expect('example: Done')
  119. @pytest.mark.generic
  120. @pytest.mark.esp32
  121. @pytest.mark.esp32c2
  122. @pytest.mark.esp32c3
  123. @pytest.mark.esp32c6
  124. @pytest.mark.esp32h2
  125. @pytest.mark.esp32s2
  126. @pytest.mark.esp32s3
  127. @pytest.mark.parametrize('config', ['virt_flash_enc',], indirect=True)
  128. @pytest.mark.parametrize('skip_autoflash', ['y'], indirect=True)
  129. def test_examples_efuse_with_virt_flash_enc_pre_loaded(dut: Dut) -> None:
  130. print(' - Erase flash')
  131. dut.serial.erase_flash()
  132. print(' - Start app (flash partition_table and app)')
  133. dut.serial.write_flash_no_enc()
  134. dut.expect('Loading virtual efuse blocks from real efuses')
  135. dut.expect('Flash encryption completed', timeout=90)
  136. dut.expect('Resetting with flash encryption enabled...')
  137. dut.expect_exact('Flash encryption mode is DEVELOPMENT (not secure)')
  138. dut.expect('Start eFuse example')
  139. dut.expect('example: Done')
  140. if dut.app.target == 'esp32':
  141. print(' - Flash emul_efuse with pre-loaded efuses (FLASH_CRYPT_CNT 1 -> 0)')
  142. # offset of this eFuse is taken from components/efuse/esp32/esp_efuse_table.csv
  143. FLASH_CRYPT_CNT = 20
  144. # Resets eFuse, which enables Flash encryption feature
  145. dut.serial.erase_field_on_emul_efuse([FLASH_CRYPT_CNT])
  146. elif dut.app.target == 'esp32c2':
  147. FLASH_CRYPT_CNT = 39
  148. dut.serial.erase_field_on_emul_efuse([FLASH_CRYPT_CNT])
  149. else:
  150. # offset of this eFuse is taken from components/efuse/{target}/esp_efuse_table.csv
  151. print(' - Flash emul_efuse with pre-loaded efuses (SPI_BOOT_CRYPT_CNT 1 -> 0)')
  152. SPI_BOOT_CRYPT_CNT = 82
  153. # Resets eFuse, which enables Flash encryption feature
  154. dut.serial.erase_field_on_emul_efuse([SPI_BOOT_CRYPT_CNT])
  155. print(' - Start app (flash partition_table and app)')
  156. dut.serial.write_flash_no_enc()
  157. dut.expect('Loading virtual efuse blocks from flash')
  158. dut.expect('Checking flash encryption...')
  159. dut.expect('Using pre-loaded flash encryption key in efuse')
  160. if dut.app.target == 'esp32':
  161. dut.expect('Setting CRYPT_CONFIG efuse to 0xF')
  162. dut.expect('Not disabling UART bootloader encryption')
  163. dut.expect('Disable UART bootloader decryption...')
  164. dut.expect('Disable UART bootloader MMU cache...')
  165. dut.expect('Disable JTAG...')
  166. dut.expect('Disable ROM BASIC interpreter fallback...')
  167. else:
  168. dut.expect('Not disabling UART bootloader encryption')
  169. if dut.app.target != 'esp32h2':
  170. dut.expect('Disable UART bootloader cache...')
  171. dut.expect('Disable JTAG...')
  172. dut.expect('bootloader encrypted successfully')
  173. dut.expect('partition table encrypted and loaded successfully')
  174. dut.expect('Flash encryption completed', timeout=90)
  175. dut.expect('Resetting with flash encryption enabled...')
  176. dut.expect('Loading virtual efuse blocks from flash')
  177. dut.expect('Checking flash encryption...')
  178. if dut.app.target == 'esp32':
  179. dut.expect_exact('flash encryption is enabled (3 plaintext flashes left)')
  180. else:
  181. dut.expect_exact('flash encryption is enabled (1 plaintext flashes left)')
  182. dut.expect_exact('Flash encryption mode is DEVELOPMENT (not secure)')
  183. dut.expect('Start eFuse example')
  184. dut.expect('example: Done')
  185. @pytest.mark.generic
  186. @pytest.mark.esp32
  187. @pytest.mark.esp32c2
  188. @pytest.mark.esp32c3
  189. @pytest.mark.esp32c6
  190. @pytest.mark.esp32h2
  191. @pytest.mark.esp32s2
  192. @pytest.mark.esp32s3
  193. @pytest.mark.parametrize('config', ['virt_flash_enc_release',], indirect=True)
  194. @pytest.mark.parametrize('skip_autoflash', ['y'], indirect=True)
  195. def test_examples_efuse_with_virt_flash_enc_release(dut: Dut) -> None:
  196. # check and log bin size
  197. binary_file = os.path.join(dut.app.binary_path, 'bootloader', 'bootloader.bin')
  198. bin_size = os.path.getsize(binary_file)
  199. logging.info('{}_bootloader_virt_flash_enc_release_bin_size: {}KB'.format(dut.app.target, bin_size // 1024))
  200. dut.serial.erase_flash()
  201. print(' - Start app (flash partition_table and app)')
  202. dut.serial.write_flash_no_enc()
  203. dut.expect('Loading virtual efuse blocks from real efuses')
  204. dut.expect('Checking flash encryption...')
  205. dut.expect('Generating new flash encryption key...')
  206. if dut.app.target == 'esp32':
  207. dut.expect(r'Writing EFUSE_BLK_KEY\d with purpose 2')
  208. dut.expect('Setting CRYPT_CONFIG efuse to 0xF')
  209. dut.expect('Disable UART bootloader encryption...')
  210. dut.expect('Disable UART bootloader decryption...')
  211. dut.expect('Disable UART bootloader MMU cache...')
  212. dut.expect('Disable JTAG...')
  213. dut.expect('Disable ROM BASIC interpreter fallback...')
  214. else:
  215. if dut.app.target == 'esp32c2':
  216. dut.expect(r'Writing EFUSE_BLK_KEY\d with purpose 1')
  217. else:
  218. dut.expect(r'Writing EFUSE_BLK_KEY\d with purpose 4')
  219. dut.expect('Disable UART bootloader encryption')
  220. if dut.app.target != 'esp32h2':
  221. dut.expect('Disable UART bootloader cache...')
  222. dut.expect('Disable JTAG...')
  223. dut.expect('bootloader encrypted successfully')
  224. dut.expect('partition table encrypted and loaded successfully')
  225. dut.expect('Setting CRYPT_CNT for permanent encryption', timeout=90)
  226. dut.expect('Flash encryption completed')
  227. dut.expect('Resetting with flash encryption enabled...')
  228. dut.expect('Loading virtual efuse blocks from flash')
  229. dut.expect('Checking flash encryption...')
  230. dut.expect_exact('flash encryption is enabled (0 plaintext flashes left)', timeout=5)
  231. dut.expect('Flash encryption mode is RELEASE')
  232. dut.expect('Start eFuse example')
  233. dut.expect('Flash Encryption is in RELEASE mode')
  234. dut.expect('example: Done')
  235. @pytest.mark.generic
  236. @pytest.mark.esp32
  237. @pytest.mark.parametrize('config', ['virt_secure_boot_v1',], indirect=True)
  238. @pytest.mark.parametrize('skip_autoflash', ['y'], indirect=True)
  239. def test_examples_efuse_with_virt_secure_boot_v1(dut: Dut) -> None:
  240. # only for ESP32
  241. # check and log bin size
  242. binary_file = os.path.join(dut.app.binary_path, 'bootloader', 'bootloader.bin')
  243. bin_size = os.path.getsize(binary_file)
  244. logging.info('{}_bootloader_virt_secure_boot_v1_bin_size: {}KB'.format(dut.app.target, bin_size // 1024))
  245. print(' - Erase flash')
  246. dut.serial.erase_flash()
  247. print(' - Flash bootloader')
  248. dut.serial.bootloader_flash()
  249. print(' - Start app (flash partition_table and app)')
  250. dut.serial.flash()
  251. dut.expect('Loading virtual efuse blocks from real efuses')
  252. dut.expect('Verifying image signature...')
  253. dut.expect('secure_boot_v1: Generating new secure boot key...')
  254. dut.expect('secure_boot_v1: Generating secure boot digest...')
  255. dut.expect('secure_boot_v1: Digest generation complete')
  256. dut.expect('Checking secure boot...')
  257. dut.expect('secure_boot_v1: blowing secure boot efuse...')
  258. dut.expect('Read & write protecting new key...')
  259. dut.expect('Disable JTAG...')
  260. dut.expect('Disable ROM BASIC interpreter fallback...')
  261. dut.expect('secure_boot_v1: secure boot is now enabled for bootloader image')
  262. dut.expect('Loading virtual efuse blocks from flash')
  263. dut.expect('main_task: Calling app_main()')
  264. dut.expect('Start eFuse example')
  265. dut.expect('example: Done')
  266. dut.serial.hard_reset()
  267. dut.expect('Loading virtual efuse blocks from flash')
  268. dut.expect('Verifying image signature...')
  269. dut.expect('secure_boot_v1: bootloader secure boot is already enabled. No need to generate digest. continuing..')
  270. dut.expect('boot: Checking secure boot...')
  271. dut.expect('secure_boot_v1: bootloader secure boot is already enabled, continuing..')
  272. dut.expect('Start eFuse example')
  273. dut.expect('example: Done')
  274. @pytest.mark.generic
  275. @pytest.mark.esp32
  276. @pytest.mark.parametrize('config', ['virt_secure_boot_v1',], indirect=True)
  277. @pytest.mark.parametrize('skip_autoflash', ['y'], indirect=True)
  278. def test_examples_efuse_with_virt_secure_boot_v1_pre_loaded(dut: Dut) -> None:
  279. print(' - Erase flash')
  280. dut.serial.erase_flash()
  281. print(' - Flash bootloader')
  282. dut.serial.bootloader_flash()
  283. print(' - Start app (flash partition_table and app)')
  284. dut.serial.flash()
  285. dut.expect('Loading virtual efuse blocks from real efuses')
  286. dut.expect('Loading virtual efuse blocks from flash')
  287. dut.expect('main_task: Calling app_main()')
  288. dut.expect('Start eFuse example')
  289. dut.expect('example: Done')
  290. print(' - Flash emul_efuse with pre-loaded efuses (ABS_DONE_0 1 -> 0)')
  291. # offset of this eFuse is taken from components/efuse/esp32/esp_efuse_table.csv
  292. ABS_DONE_0 = 196
  293. # Resets eFuse, which enables Secure boot (V1) feature
  294. dut.serial.erase_field_on_emul_efuse([ABS_DONE_0])
  295. print(' - Start app (flash partition_table and app)')
  296. dut.serial.flash()
  297. dut.expect('Loading virtual efuse blocks from flash')
  298. dut.expect('Verifying image signature...')
  299. dut.expect('secure_boot_v1: Using pre-loaded secure boot key in EFUSE block 2')
  300. dut.expect('secure_boot_v1: Generating secure boot digest...')
  301. dut.expect('secure_boot_v1: Digest generation complete')
  302. dut.expect('Checking secure boot...')
  303. dut.expect('secure_boot_v1: blowing secure boot efuse...')
  304. dut.expect('Read & write protecting new key...')
  305. dut.expect('Disable JTAG...')
  306. dut.expect('Disable ROM BASIC interpreter fallback...')
  307. dut.expect('secure_boot_v1: secure boot is now enabled for bootloader image')
  308. dut.expect('Loading virtual efuse blocks from flash')
  309. dut.expect('main_task: Calling app_main()')
  310. dut.expect('Start eFuse example')
  311. dut.expect('example: Done')
  312. dut.serial.hard_reset()
  313. dut.expect('Loading virtual efuse blocks from flash')
  314. dut.expect('Verifying image signature...')
  315. dut.expect('secure_boot_v1: bootloader secure boot is already enabled. No need to generate digest. continuing..')
  316. dut.expect('Checking secure boot...')
  317. dut.expect('secure_boot_v1: bootloader secure boot is already enabled, continuing..')
  318. dut.expect('Start eFuse example')
  319. dut.expect('example: Done')
  320. @pytest.mark.esp32
  321. @pytest.mark.esp32eco3
  322. @pytest.mark.parametrize('config', [('virt_secure_boot_v2'),], indirect=True)
  323. @pytest.mark.parametrize('skip_autoflash', ['y'], indirect=True)
  324. def test_examples_efuse_with_virt_secure_boot_v2(dut: Dut) -> None:
  325. # only for ESP32 ECO3
  326. binary_file = os.path.join(dut.app.binary_path, 'bootloader', 'bootloader.bin')
  327. bin_size = os.path.getsize(binary_file)
  328. logging.info('{}_bootloader_secure_boot_v2_bin_size: {}KB'.format(dut.app.target, bin_size // 1024))
  329. print(' - Erase flash')
  330. dut.serial.erase_flash()
  331. print(' - Flash bootloader')
  332. dut.serial.bootloader_flash()
  333. print(' - Start app (flash partition_table and app)')
  334. dut.serial.flash()
  335. dut.expect('Loading virtual efuse blocks from real efuses')
  336. dut.expect('Verifying image signature...')
  337. dut.expect('secure_boot_v2: Secure boot V2 is not enabled yet and eFuse digest keys are not set')
  338. dut.expect('secure_boot_v2: Verifying with RSA-PSS...', timeout=20)
  339. dut.expect('secure_boot_v2: Signature verified successfully!')
  340. dut.expect('secure_boot_v2: enabling secure boot v2...')
  341. dut.expect('Verifying image signature...')
  342. dut.expect('secure_boot_v2: Secure boot V2 is not enabled yet and eFuse digest keys are not set')
  343. dut.expect('secure_boot_v2: Verifying with RSA-PSS...')
  344. dut.expect('secure_boot_v2: Signature verified successfully!')
  345. dut.expect('secure_boot_v2: Secure boot digests absent, generating..')
  346. dut.expect('secure_boot_v2: Digests successfully calculated, 1 valid signatures')
  347. dut.expect_exact('secure_boot_v2: 1 signature block(s) found appended to the bootloader')
  348. dut.expect(r'Writing EFUSE_BLK_KEY\d with purpose 3')
  349. dut.expect('secure_boot_v2: Digests successfully calculated, 1 valid signatures')
  350. dut.expect_exact('secure_boot_v2: 1 signature block(s) found appended to the app')
  351. dut.expect_exact('secure_boot_v2: Application key(0) matches with bootloader key(0)')
  352. dut.expect('secure_boot_v2: blowing secure boot efuse...')
  353. dut.expect('Disable JTAG...')
  354. dut.expect('Disable ROM BASIC interpreter fallback...')
  355. dut.expect('UART ROM Download mode kept enabled - SECURITY COMPROMISED')
  356. dut.expect('Prevent read disabling of additional efuses...')
  357. dut.expect('secure_boot_v2: Secure boot permanently enabled')
  358. dut.expect('Loading virtual efuse blocks from flash')
  359. dut.expect('main_task: Calling app_main()')
  360. dut.expect('Start eFuse example')
  361. dut.expect('example: Done')
  362. dut.serial.hard_reset()
  363. dut.expect('Loading virtual efuse blocks from flash')
  364. dut.expect('Verifying image signature...')
  365. dut.expect('secure_boot_v2: Verifying with RSA-PSS...')
  366. dut.expect('secure_boot_v2: Signature verified successfully!')
  367. dut.expect('secure_boot_v2: enabling secure boot v2...')
  368. dut.expect('secure_boot_v2: secure boot v2 is already enabled, continuing..')
  369. dut.expect('Start eFuse example')
  370. dut.expect('example: Done')
  371. print(' - Erase flash')
  372. dut.serial.erase_flash()
  373. print(' - Flash bootloader and app')
  374. dut.serial.bootloader_flash()
  375. dut.serial.flash()
  376. dut.expect('Loading virtual efuse blocks from real efuses')
  377. dut.expect('Loading virtual efuse blocks from flash')
  378. dut.expect('Start eFuse example')
  379. dut.expect('example: Done')
  380. print(' - Flash emul_efuse with pre-loaded efuses (ABS_DONE_1 1 -> 0)')
  381. # offset of this eFuse is taken from components/efuse/esp32/esp_efuse_table.csv
  382. ABS_DONE_1 = 197
  383. # Resets eFuse, which enables Secure boot (V2) feature
  384. dut.serial.erase_field_on_emul_efuse([ABS_DONE_1])
  385. print(' - Start app (flash partition_table and app)')
  386. dut.serial.flash()
  387. dut.expect('Loading virtual efuse blocks from flash')
  388. dut.expect('Verifying image signature...')
  389. dut.expect('secure_boot_v2: Verifying with RSA-PSS...')
  390. dut.expect('secure_boot_v2: Signature verified successfully!')
  391. dut.expect('secure_boot_v2: enabling secure boot v2...')
  392. dut.expect('Verifying image signature...')
  393. dut.expect('secure_boot_v2: Verifying with RSA-PSS...')
  394. dut.expect('secure_boot_v2: Signature verified successfully!')
  395. dut.expect('secure_boot_v2: Secure boot digests already present')
  396. dut.expect('secure_boot_v2: Using pre-loaded public key digest in eFuse')
  397. dut.expect('secure_boot_v2: Digests successfully calculated, 1 valid signatures')
  398. dut.expect_exact('secure_boot_v2: 1 signature block(s) found appended to the app')
  399. dut.expect_exact('secure_boot_v2: Application key(0) matches with bootloader key(0)')
  400. dut.expect('secure_boot_v2: blowing secure boot efuse...')
  401. dut.expect('Disable JTAG...')
  402. dut.expect('Disable ROM BASIC interpreter fallback...')
  403. dut.expect('UART ROM Download mode kept enabled - SECURITY COMPROMISED')
  404. dut.expect('Prevent read disabling of additional efuses...')
  405. dut.expect('secure_boot_v2: Secure boot permanently enabled')
  406. dut.expect('Loading virtual efuse blocks from flash')
  407. dut.expect('main_task: Calling app_main()')
  408. dut.expect('Start eFuse example')
  409. dut.expect('example: Done')
  410. dut.serial.hard_reset()
  411. dut.expect('Loading virtual efuse blocks from flash')
  412. dut.expect('Verifying image signature...')
  413. dut.expect('secure_boot_v2: Verifying with RSA-PSS...')
  414. dut.expect('secure_boot_v2: Signature verified successfully!')
  415. dut.expect('secure_boot_v2: enabling secure boot v2...')
  416. dut.expect('secure_boot_v2: secure boot v2 is already enabled, continuing..')
  417. dut.expect('Start eFuse example')
  418. dut.expect('example: Done')
  419. @pytest.mark.esp32
  420. @pytest.mark.esp32eco3
  421. @pytest.mark.parametrize('config', [('virt_secure_boot_v2'),], indirect=True)
  422. @pytest.mark.parametrize('skip_autoflash', ['y'], indirect=True)
  423. def test_examples_efuse_with_virt_secure_boot_v2_pre_loaded(dut: Dut) -> None:
  424. print(' - Erase flash')
  425. dut.serial.erase_flash()
  426. print(' - Flash bootloader and app')
  427. dut.serial.bootloader_flash()
  428. print(' - Start app (flash partition_table and app)')
  429. dut.serial.flash()
  430. dut.expect('Loading virtual efuse blocks from real efuses')
  431. dut.expect('Loading virtual efuse blocks from flash')
  432. dut.expect('main_task: Calling app_main()')
  433. dut.expect('Start eFuse example')
  434. dut.expect('example: Done')
  435. print(' - Flash emul_efuse with pre-loaded efuses (ABS_DONE_1 1 -> 0)')
  436. # offset of this eFuse is taken from components/efuse/esp32/esp_efuse_table.csv
  437. ABS_DONE_1 = 197
  438. # Resets eFuse, which enables Secure boot (V2) feature
  439. dut.serial.erase_field_on_emul_efuse([ABS_DONE_1])
  440. print(' - Start app (flash partition_table and app)')
  441. dut.serial.flash()
  442. dut.expect('Loading virtual efuse blocks from flash')
  443. dut.expect('Verifying image signature...')
  444. dut.expect('secure_boot_v2: Verifying with RSA-PSS...')
  445. dut.expect('secure_boot_v2: Signature verified successfully!')
  446. dut.expect('secure_boot_v2: enabling secure boot v2...')
  447. dut.expect('Verifying image signature...')
  448. dut.expect('secure_boot_v2: Verifying with RSA-PSS...')
  449. dut.expect('secure_boot_v2: Signature verified successfully!')
  450. dut.expect('secure_boot_v2: Secure boot digests already present')
  451. dut.expect('secure_boot_v2: Using pre-loaded public key digest in eFuse')
  452. dut.expect('secure_boot_v2: Digests successfully calculated, 1 valid signatures')
  453. dut.expect_exact('secure_boot_v2: 1 signature block(s) found appended to the app')
  454. dut.expect_exact('secure_boot_v2: Application key(0) matches with bootloader key(0)')
  455. dut.expect('secure_boot_v2: blowing secure boot efuse...')
  456. dut.expect('Disable JTAG...')
  457. dut.expect('Disable ROM BASIC interpreter fallback...')
  458. dut.expect('UART ROM Download mode kept enabled - SECURITY COMPROMISED')
  459. dut.expect('Prevent read disabling of additional efuses...')
  460. dut.expect('secure_boot_v2: Secure boot permanently enabled')
  461. dut.expect('Loading virtual efuse blocks from flash')
  462. dut.expect('main_task: Calling app_main()')
  463. dut.expect('Start eFuse example')
  464. dut.expect('example: Done')
  465. dut.serial.hard_reset()
  466. dut.expect('Loading virtual efuse blocks from flash')
  467. dut.expect('Verifying image signature...')
  468. dut.expect('secure_boot_v2: Verifying with RSA-PSS...')
  469. dut.expect('secure_boot_v2: Signature verified successfully!')
  470. dut.expect('secure_boot_v2: enabling secure boot v2...')
  471. dut.expect('secure_boot_v2: secure boot v2 is already enabled, continuing..')
  472. dut.expect('Start eFuse example')
  473. dut.expect('example: Done')
  474. @pytest.mark.esp32c3
  475. @pytest.mark.esp32c2
  476. @pytest.mark.esp32c6
  477. @pytest.mark.esp32h2
  478. @pytest.mark.esp32p4
  479. @pytest.mark.esp32s2
  480. @pytest.mark.esp32s3
  481. @pytest.mark.generic
  482. @pytest.mark.parametrize('config', ['virt_secure_boot_v2'], indirect=True)
  483. @pytest.mark.parametrize('skip_autoflash', ['y'], indirect=True)
  484. def test_examples_efuse_with_virt_secure_boot_v2_esp32xx(dut: Dut) -> None:
  485. # check and log bin size
  486. binary_file = os.path.join(dut.app.binary_path, 'bootloader', 'bootloader.bin')
  487. bin_size = os.path.getsize(binary_file)
  488. logging.info('{}_bootloader_virt_secure_boot_v2_bin_size: {}KB'.format(dut.app.target, bin_size // 1024))
  489. print(' - Erase flash')
  490. dut.serial.erase_flash()
  491. print(' - Flash bootloader')
  492. dut.serial.bootloader_flash()
  493. print(' - Start app (flash partition_table and app)')
  494. dut.serial.flash()
  495. dut.expect('Loading virtual efuse blocks from real efuses')
  496. dut.expect('Verifying image signature...')
  497. dut.expect('secure_boot_v2: Secure boot V2 is not enabled yet and eFuse digest keys are not set')
  498. if dut.app.target == 'esp32c2':
  499. signed_scheme = 'ECDSA'
  500. else:
  501. signed_scheme = 'RSA-PSS'
  502. dut.expect('secure_boot_v2: Verifying with %s...' % signed_scheme)
  503. dut.expect('secure_boot_v2: Signature verified successfully!')
  504. dut.expect('secure_boot_v2: enabling secure boot v2...')
  505. dut.expect('Verifying image signature...')
  506. dut.expect('secure_boot_v2: Secure boot V2 is not enabled yet and eFuse digest keys are not set')
  507. dut.expect('secure_boot_v2: Verifying with %s...' % signed_scheme)
  508. dut.expect('secure_boot_v2: Signature verified successfully!')
  509. dut.expect('secure_boot_v2: Secure boot digests absent, generating..')
  510. dut.expect('secure_boot_v2: Digests successfully calculated, 1 valid signatures')
  511. dut.expect_exact('secure_boot_v2: 1 signature block(s) found appended to the bootloader')
  512. if dut.app.target == 'esp32c2':
  513. dut.expect(r'Writing EFUSE_BLK_KEY\d with purpose 3')
  514. else:
  515. dut.expect(r'Writing EFUSE_BLK_KEY\d with purpose 9')
  516. dut.expect('secure_boot_v2: Digests successfully calculated, 1 valid signatures')
  517. dut.expect_exact('secure_boot_v2: 1 signature block(s) found appended to the app')
  518. dut.expect_exact('secure_boot_v2: Application key(0) matches with bootloader key(0)')
  519. if dut.app.target != 'esp32c2':
  520. dut.expect_exact('secure_boot_v2: Revoking empty key digest slot (1)...')
  521. dut.expect_exact('secure_boot_v2: Revoking empty key digest slot (2)...')
  522. dut.expect('secure_boot_v2: blowing secure boot efuse...')
  523. dut.expect('UART ROM Download mode kept enabled - SECURITY COMPROMISED')
  524. dut.expect('Disable hardware & software JTAG...')
  525. dut.expect('secure_boot_v2: Secure boot permanently enabled')
  526. dut.expect('Loading virtual efuse blocks from flash')
  527. dut.expect('main_task: Calling app_main()')
  528. dut.expect('Start eFuse example')
  529. dut.expect('example: Done')
  530. dut.serial.hard_reset()
  531. dut.expect('Loading virtual efuse blocks from flash')
  532. dut.expect('Verifying image signature...')
  533. dut.expect('secure_boot_v2: Verifying with %s...' % signed_scheme)
  534. dut.expect('secure_boot_v2: Signature verified successfully!')
  535. dut.expect('secure_boot_v2: enabling secure boot v2...')
  536. dut.expect('secure_boot_v2: secure boot v2 is already enabled, continuing..')
  537. dut.expect('Start eFuse example')
  538. dut.expect('example: Done')
  539. @pytest.mark.esp32c3
  540. @pytest.mark.esp32c2
  541. @pytest.mark.esp32c6
  542. @pytest.mark.esp32h2
  543. @pytest.mark.esp32p4
  544. @pytest.mark.esp32s2
  545. @pytest.mark.esp32s3
  546. @pytest.mark.generic
  547. @pytest.mark.parametrize('config', ['virt_secure_boot_v2'], indirect=True)
  548. @pytest.mark.parametrize('skip_autoflash', ['y'], indirect=True)
  549. def test_example_efuse_with_virt_secure_boot_v2_esp32xx_pre_loaded(dut: Dut) -> None:
  550. print(' - Erase flash')
  551. dut.serial.erase_flash()
  552. print(' - Flash bootloader and app')
  553. dut.serial.bootloader_flash()
  554. dut.serial.flash()
  555. dut.expect('Loading virtual efuse blocks from real efuses')
  556. dut.expect('Loading virtual efuse blocks from flash')
  557. dut.expect('main_task: Calling app_main()')
  558. dut.expect('Start eFuse example')
  559. dut.expect('example: Done')
  560. print(' - Flash emul_efuse with pre-loaded efuses (SECURE_BOOT_EN 1 -> 0, SECURE_BOOT_KEY_REVOKE[0..2] -> 0)')
  561. # offsets of eFuses are taken from components/efuse/{target}/esp_efuse_table.csv
  562. if dut.app.target == 'esp32c2':
  563. SECURE_BOOT_EN = 53
  564. dut.serial.erase_field_on_emul_efuse([SECURE_BOOT_EN])
  565. else:
  566. SECURE_BOOT_EN = 116
  567. SECURE_BOOT_KEY_REVOKE0 = 85
  568. SECURE_BOOT_KEY_REVOKE1 = 86
  569. SECURE_BOOT_KEY_REVOKE2 = 87
  570. # Resets eFuse, which enables Secure boot feature
  571. # Resets eFuses, which control digest slots
  572. dut.serial.erase_field_on_emul_efuse([SECURE_BOOT_EN, SECURE_BOOT_KEY_REVOKE0, SECURE_BOOT_KEY_REVOKE1, SECURE_BOOT_KEY_REVOKE2])
  573. print(' - Start app (flash partition_table and app)')
  574. dut.serial.flash()
  575. dut.expect('Loading virtual efuse blocks from flash')
  576. dut.expect('Verifying image signature...')
  577. if dut.app.target == 'esp32c2':
  578. signed_scheme = 'ECDSA'
  579. else:
  580. signed_scheme = 'RSA-PSS'
  581. dut.expect('secure_boot_v2: Verifying with %s...' % signed_scheme)
  582. dut.expect('secure_boot_v2: Signature verified successfully!')
  583. dut.expect('secure_boot_v2: Secure boot digests already present')
  584. dut.expect('secure_boot_v2: Using pre-loaded public key digest in eFuse')
  585. dut.expect('secure_boot_v2: Digests successfully calculated, 1 valid signatures')
  586. dut.expect_exact('secure_boot_v2: 1 signature block(s) found appended to the app')
  587. if dut.app.target != 'esp32c2':
  588. dut.expect_exact('secure_boot_v2: Revoking empty key digest slot (1)...')
  589. dut.expect_exact('secure_boot_v2: Revoking empty key digest slot (2)...')
  590. dut.expect('secure_boot_v2: blowing secure boot efuse...')
  591. dut.expect('UART ROM Download mode kept enabled - SECURITY COMPROMISED')
  592. dut.expect('Disable hardware & software JTAG...')
  593. dut.expect('secure_boot_v2: Secure boot permanently enabled', timeout=20)
  594. dut.expect('Loading virtual efuse blocks from flash')
  595. dut.expect('main_task: Calling app_main()')
  596. dut.expect('Start eFuse example')
  597. dut.expect('example: Done')
  598. dut.serial.hard_reset()
  599. dut.expect('Loading virtual efuse blocks from flash')
  600. dut.expect('Verifying image signature...')
  601. dut.expect('secure_boot_v2: Verifying with %s...' % signed_scheme)
  602. dut.expect('secure_boot_v2: Signature verified successfully!')
  603. dut.expect('secure_boot_v2: enabling secure boot v2...')
  604. dut.expect('secure_boot_v2: secure boot v2 is already enabled, continuing..')
  605. dut.expect('Start eFuse example')
  606. dut.expect('example: Done')
  607. @pytest.mark.generic
  608. @pytest.mark.esp32
  609. @pytest.mark.parametrize('config', ['virt_sb_v1_and_fe',], indirect=True)
  610. @pytest.mark.parametrize('skip_autoflash', ['y'], indirect=True)
  611. def test_examples_efuse_with_virt_sb_v1_and_fe(dut: Dut) -> None:
  612. # check and log bin size
  613. binary_file = os.path.join(dut.app.binary_path, 'bootloader', 'bootloader.bin')
  614. bin_size = os.path.getsize(binary_file)
  615. logging.info('{}_bootloader_virt_sb_v1_and_fe_bin_size: {}KB'.format(dut.app.target, bin_size // 1024))
  616. print(' - Erase flash')
  617. dut.serial.erase_flash()
  618. print(' - Flash bootloader')
  619. dut.serial.bootloader_flash()
  620. print(' - Start app (flash partition_table and app)')
  621. dut.serial.write_flash_no_enc()
  622. dut.expect('Loading virtual efuse blocks from real efuses')
  623. dut.expect('Verifying image signature...')
  624. dut.expect('secure_boot_v1: Generating new secure boot key...')
  625. dut.expect('secure_boot_v1: Generating secure boot digest...')
  626. dut.expect('secure_boot_v1: Digest generation complete')
  627. dut.expect('Checking flash encryption...')
  628. dut.expect('flash_encrypt: Generating new flash encryption key...')
  629. dut.expect(r'Writing EFUSE_BLK_KEY\d with purpose 2')
  630. dut.expect('flash_encrypt: Setting CRYPT_CONFIG efuse to 0xF')
  631. dut.expect('flash_encrypt: Not disabling UART bootloader encryption')
  632. dut.expect('flash_encrypt: Disable UART bootloader decryption...')
  633. dut.expect('flash_encrypt: Disable UART bootloader MMU cache...')
  634. dut.expect('flash_encrypt: Disable JTAG...')
  635. dut.expect('flash_encrypt: Disable ROM BASIC interpreter fallback...')
  636. dut.expect('flash_encrypt: bootloader encrypted successfully')
  637. dut.expect('flash_encrypt: partition table encrypted and loaded successfully')
  638. dut.expect('Verifying image signature...')
  639. dut.expect('flash_encrypt: Flash encryption completed', timeout=90)
  640. dut.expect('Checking secure boot...')
  641. dut.expect('secure_boot_v1: blowing secure boot efuse...')
  642. dut.expect('Read & write protecting new key...')
  643. dut.expect('Disable JTAG...')
  644. dut.expect('Disable ROM BASIC interpreter fallback...')
  645. dut.expect('secure_boot_v1: secure boot is now enabled for bootloader image')
  646. dut.expect('Resetting with flash encryption enabled...')
  647. dut.expect('Verifying image signature...')
  648. dut.expect('secure_boot_v1: bootloader secure boot is already enabled. No need to generate digest. continuing..')
  649. dut.expect('Checking flash encryption...')
  650. dut.expect_exact('flash_encrypt: flash encryption is enabled (3 plaintext flashes left)')
  651. dut.expect('Checking secure boot...')
  652. dut.expect('secure_boot_v1: bootloader secure boot is already enabled, continuing..')
  653. dut.expect('Loading virtual efuse blocks from flash')
  654. dut.expect_exact('flash_encrypt: Flash encryption mode is DEVELOPMENT (not secure)')
  655. dut.expect('main_task: Calling app_main()')
  656. dut.expect('Start eFuse example')
  657. dut.expect('example: Flash Encryption is NOT in RELEASE mode')
  658. dut.expect('example: Secure Boot is in RELEASE mode')
  659. dut.expect('example: Done')
  660. @pytest.mark.esp32
  661. @pytest.mark.esp32eco3
  662. @pytest.mark.parametrize('config', ['virt_sb_v2_and_fe',], indirect=True)
  663. @pytest.mark.parametrize('skip_autoflash', ['y'], indirect=True)
  664. def test_examples_efuse_with_virt_sb_v2_and_fe(dut: Dut) -> None:
  665. # check and log bin size
  666. binary_file = os.path.join(dut.app.binary_path, 'bootloader', 'bootloader.bin')
  667. bin_size = os.path.getsize(binary_file)
  668. logging.info('{}_bootloader_virt_sb_v2_and_fe_bin_size: {}KB'.format(dut.app.target, bin_size // 1024))
  669. print(' - Erase flash')
  670. dut.serial.erase_flash()
  671. print(' - Flash bootloader')
  672. dut.serial.bootloader_flash()
  673. print(' - Start app (flash partition_table and app)')
  674. dut.serial.write_flash_no_enc()
  675. dut.expect('Loading virtual efuse blocks from real efuses')
  676. dut.expect('secure_boot_v2: Secure boot V2 is not enabled yet and eFuse digest keys are not set')
  677. dut.expect('secure_boot_v2: Verifying with RSA-PSS...')
  678. dut.expect('secure_boot_v2: Signature verified successfully!')
  679. dut.expect('secure_boot_v2: enabling secure boot v2...')
  680. dut.expect('Verifying image signature...')
  681. dut.expect('secure_boot_v2: Secure boot V2 is not enabled yet and eFuse digest keys are not set')
  682. dut.expect('secure_boot_v2: Verifying with RSA-PSS...')
  683. dut.expect('secure_boot_v2: Signature verified successfully')
  684. dut.expect('secure_boot_v2: Secure boot digests absent, generating..')
  685. dut.expect('secure_boot_v2: Digests successfully calculated, 1 valid signatures')
  686. dut.expect_exact('secure_boot_v2: 1 signature block(s) found appended to the bootloader')
  687. dut.expect(r'Writing EFUSE_BLK_KEY\d with purpose 3')
  688. dut.expect('secure_boot_v2: Digests successfully calculated, 1 valid signatures')
  689. dut.expect_exact('secure_boot_v2: 1 signature block(s) found appended to the app')
  690. dut.expect_exact('secure_boot_v2: Application key(0) matches with bootloader key(0)')
  691. dut.expect('secure_boot_v2: blowing secure boot efuse...')
  692. dut.expect('Disable JTAG...')
  693. dut.expect('Disable ROM BASIC interpreter fallback...')
  694. dut.expect('Disable ROM Download mode...')
  695. dut.expect('secure_boot_v2: Secure boot permanently enabled')
  696. dut.expect('Checking flash encryption...')
  697. dut.expect('flash_encrypt: Generating new flash encryption key...')
  698. dut.expect(r'Writing EFUSE_BLK_KEY\d with purpose 2')
  699. dut.expect('flash_encrypt: Setting CRYPT_CONFIG efuse to 0xF')
  700. dut.expect('flash_encrypt: Not disabling UART bootloader encryption')
  701. dut.expect('flash_encrypt: Disable UART bootloader decryption...')
  702. dut.expect('flash_encrypt: Disable UART bootloader MMU cache...')
  703. dut.expect('flash_encrypt: Disable JTAG...')
  704. dut.expect('flash_encrypt: Disable ROM BASIC interpreter fallback...')
  705. dut.expect('Verifying image signature...')
  706. dut.expect('secure_boot_v2: Verifying with RSA-PSS...')
  707. dut.expect('secure_boot_v2: Signature verified successfully!')
  708. dut.expect('flash_encrypt: bootloader encrypted successfully')
  709. dut.expect('flash_encrypt: partition table encrypted and loaded successfully')
  710. dut.expect('Verifying image signature...')
  711. dut.expect('secure_boot_v2: Verifying with RSA-PSS...')
  712. dut.expect('secure_boot_v2: Signature verified successfully!')
  713. dut.expect('flash_encrypt: Flash encryption completed', timeout=90)
  714. dut.expect('Resetting with flash encryption enabled...')
  715. dut.expect('Loading virtual efuse blocks from flash')
  716. dut.expect('Verifying image signature...')
  717. dut.expect('secure_boot_v2: Verifying with RSA-PSS...')
  718. dut.expect('secure_boot_v2: Signature verified successfully!')
  719. dut.expect('secure_boot_v2: enabling secure boot v2...')
  720. dut.expect('secure_boot_v2: secure boot v2 is already enabled, continuing..')
  721. dut.expect_exact('flash_encrypt: flash encryption is enabled (3 plaintext flashes left)')
  722. dut.expect('Loading virtual efuse blocks from flash')
  723. dut.expect_exact('flash_encrypt: Flash encryption mode is DEVELOPMENT (not secure)')
  724. dut.expect('main_task: Calling app_main()')
  725. dut.expect('Start eFuse example')
  726. dut.expect('example: Flash Encryption is NOT in RELEASE mode')
  727. dut.expect('example: Secure Boot is in RELEASE mode')
  728. dut.expect('example: Done')
  729. @pytest.mark.esp32
  730. @pytest.mark.host_test
  731. @pytest.mark.qemu
  732. @pytest.mark.parametrize('config', ['virt_sb_v2_and_fe.esp32.qemu',], indirect=True)
  733. @pytest.mark.parametrize(
  734. 'qemu_extra_args',
  735. [
  736. f'-drive file={os.path.join(os.path.dirname(__file__), "test", "esp32eco3_efuses.bin")},if=none,format=raw,id=efuse '
  737. '-global driver=nvram.esp32.efuse,property=drive,value=efuse '
  738. '-global driver=timer.esp32.timg,property=wdt_disable,value=true',
  739. ],
  740. indirect=True,
  741. )
  742. def test_examples_efuse_with_virt_sb_v2_and_fe_qemu(dut: QemuDut) -> None:
  743. try:
  744. dut.expect('secure_boot_v2: Secure boot V2 is not enabled yet and eFuse digest keys are not set')
  745. dut.expect('secure_boot_v2: Verifying with RSA-PSS...')
  746. dut.expect('secure_boot_v2: Signature verified successfully!')
  747. dut.expect('secure_boot_v2: enabling secure boot v2...')
  748. dut.expect('Verifying image signature...')
  749. dut.expect('secure_boot_v2: Secure boot V2 is not enabled yet and eFuse digest keys are not set')
  750. dut.expect('secure_boot_v2: Verifying with RSA-PSS...')
  751. dut.expect('secure_boot_v2: Signature verified successfully')
  752. dut.expect('secure_boot_v2: Secure boot digests absent, generating..')
  753. dut.expect('secure_boot_v2: Digests successfully calculated, 1 valid signatures')
  754. dut.expect_exact('secure_boot_v2: 1 signature block(s) found appended to the bootloader')
  755. dut.expect('Writing EFUSE_BLK_KEY1 with purpose 3')
  756. dut.expect('secure_boot_v2: Digests successfully calculated, 1 valid signatures')
  757. dut.expect_exact('secure_boot_v2: 1 signature block(s) found appended to the app')
  758. dut.expect_exact('secure_boot_v2: Application key(0) matches with bootloader key(0)')
  759. dut.expect('secure_boot_v2: blowing secure boot efuse...')
  760. dut.expect('Disable JTAG...')
  761. dut.expect('Disable ROM BASIC interpreter fallback...')
  762. dut.expect('Disable ROM Download mode...')
  763. dut.expect('secure_boot_v2: Secure boot permanently enabled')
  764. dut.expect('Checking flash encryption...')
  765. dut.expect('flash_encrypt: Generating new flash encryption key...')
  766. dut.expect('Writing EFUSE_BLK_KEY0 with purpose 2')
  767. dut.expect('flash_encrypt: Setting CRYPT_CONFIG efuse to 0xF')
  768. dut.expect('flash_encrypt: Not disabling UART bootloader encryption')
  769. dut.expect('flash_encrypt: Disable UART bootloader decryption...')
  770. dut.expect('flash_encrypt: Disable UART bootloader MMU cache...')
  771. dut.expect('flash_encrypt: Disable JTAG...')
  772. dut.expect('flash_encrypt: Disable ROM BASIC interpreter fallback...')
  773. dut.expect('Verifying image signature...')
  774. dut.expect('secure_boot_v2: Verifying with RSA-PSS...')
  775. dut.expect('secure_boot_v2: Signature verified successfully!')
  776. dut.expect('flash_encrypt: bootloader encrypted successfully')
  777. dut.expect('flash_encrypt: partition table encrypted and loaded successfully')
  778. dut.expect('Verifying image signature...')
  779. dut.expect('secure_boot_v2: Verifying with RSA-PSS...')
  780. dut.expect('secure_boot_v2: Signature verified successfully!')
  781. dut.expect('flash_encrypt: Flash encryption completed', timeout=120)
  782. dut.expect('Resetting with flash encryption enabled...')
  783. dut.expect('Verifying image signature...', timeout=180)
  784. dut.expect('secure_boot_v2: Verifying with RSA-PSS...')
  785. dut.expect('secure_boot_v2: Signature verified successfully!')
  786. dut.expect('secure_boot_v2: enabling secure boot v2...')
  787. dut.expect('secure_boot_v2: secure boot v2 is already enabled, continuing..')
  788. dut.expect_exact('flash_encrypt: flash encryption is enabled (3 plaintext flashes left)')
  789. dut.expect_exact('flash_encrypt: Flash encryption mode is DEVELOPMENT (not secure)')
  790. dut.expect('main_task: Calling app_main()')
  791. dut.expect('Start eFuse example')
  792. dut.expect('example: Flash Encryption is NOT in RELEASE mode')
  793. dut.expect('example: Secure Boot is in RELEASE mode')
  794. dut.expect('example: Done')
  795. finally:
  796. # the above example test burns the efuses, and hence the efuses file which the
  797. # qemu uses to emulate the efuses, "test/esp32eco3_efuses.bin", gets modified.
  798. # Thus, restore the efuses file values back to the default ESP32-ECO3 efuses values.
  799. with open(os.path.join(os.path.dirname(__file__), 'test', 'esp32eco3_efuses.bin'), 'wb') as efuse_file:
  800. esp32eco3_efuses = '0' * 26 + '8' + '0' * 17 + '1' + '0' * 203
  801. efuse_file.write(bytearray.fromhex(esp32eco3_efuses))
  802. @pytest.mark.esp32c3
  803. @pytest.mark.esp32c2
  804. @pytest.mark.esp32c6
  805. @pytest.mark.esp32h2
  806. @pytest.mark.esp32s2
  807. @pytest.mark.esp32s3
  808. @pytest.mark.generic
  809. @pytest.mark.parametrize('skip_autoflash', ['y'], indirect=True)
  810. @pytest.mark.parametrize('config', ['virt_sb_v2_and_fe'], indirect=True)
  811. def test_examples_efuse_with_virt_sb_v2_and_fe_esp32xx(dut: Dut) -> None:
  812. # check and log bin size
  813. binary_file = os.path.join(dut.app.binary_path, 'bootloader', 'bootloader.bin')
  814. bin_size = os.path.getsize(binary_file)
  815. logging.info('{}_bootloader_virt_sb_v2_and_fe_bin_size: {}KB'.format(dut.app.target, bin_size // 1024))
  816. dut.serial.erase_flash()
  817. print(' - Flash bootloader')
  818. dut.serial.bootloader_flash()
  819. print(' - Start app (flash partition_table and app)')
  820. dut.serial.write_flash_no_enc()
  821. dut.expect('Loading virtual efuse blocks from real efuses')
  822. dut.expect('Verifying image signature...')
  823. dut.expect('secure_boot_v2: Secure boot V2 is not enabled yet and eFuse digest keys are not set')
  824. signed_scheme = 'ECDSA' if dut.app.target == 'esp32c2' else 'RSA-PSS'
  825. dut.expect('secure_boot_v2: Verifying with %s...' % signed_scheme)
  826. dut.expect('secure_boot_v2: Signature verified successfully!')
  827. dut.expect('secure_boot_v2: enabling secure boot v2...')
  828. dut.expect('Verifying image signature...')
  829. dut.expect('secure_boot_v2: Secure boot V2 is not enabled yet and eFuse digest keys are not set')
  830. dut.expect('secure_boot_v2: Verifying with %s...' % signed_scheme)
  831. dut.expect('secure_boot_v2: Signature verified successfully!')
  832. dut.expect('secure_boot_v2: Secure boot digests absent, generating..')
  833. dut.expect('secure_boot_v2: Digests successfully calculated, 1 valid signatures')
  834. dut.expect_exact('secure_boot_v2: 1 signature block(s) found appended to the bootloader')
  835. if dut.app.target == 'esp32c2':
  836. dut.expect(r'Writing EFUSE_BLK_KEY\d with purpose 3')
  837. else:
  838. dut.expect(r'Writing EFUSE_BLK_KEY\d with purpose 9')
  839. dut.expect('secure_boot_v2: Digests successfully calculated, 1 valid signatures')
  840. dut.expect_exact('secure_boot_v2: 1 signature block(s) found appended to the app')
  841. dut.expect_exact('secure_boot_v2: Application key(0) matches with bootloader key(0)')
  842. if dut.app.target != 'esp32c2':
  843. dut.expect_exact('secure_boot_v2: Revoking empty key digest slot (1)...')
  844. dut.expect_exact('secure_boot_v2: Revoking empty key digest slot (2)...')
  845. dut.expect('secure_boot_v2: blowing secure boot efuse...')
  846. dut.expect('Enabling Security download mode...')
  847. dut.expect('Disable hardware & software JTAG...')
  848. if dut.app.target != 'esp32c2':
  849. dut.expect('secure_boot_v2: Secure boot permanently enabled')
  850. dut.expect('Checking flash encryption...')
  851. dut.expect('flash_encrypt: Generating new flash encryption key...')
  852. if dut.app.target == 'esp32c2':
  853. dut.expect(r'Writing EFUSE_BLK_KEY\d with purpose 2')
  854. else:
  855. dut.expect(r'Writing EFUSE_BLK_KEY\d with purpose 4')
  856. dut.expect('Not disabling UART bootloader encryption')
  857. if dut.app.target != 'esp32h2':
  858. dut.expect('Disable UART bootloader cache...')
  859. dut.expect('Disable JTAG...')
  860. if dut.app.target == 'esp32c2':
  861. dut.expect('boot: Secure boot permanently enabled')
  862. dut.expect('Verifying image signature...')
  863. dut.expect('secure_boot_v2: Verifying with %s...' % signed_scheme)
  864. dut.expect('secure_boot_v2: Signature verified successfully!')
  865. dut.expect('flash_encrypt: bootloader encrypted successfully')
  866. dut.expect('flash_encrypt: partition table encrypted and loaded successfully')
  867. dut.expect('Verifying image signature...')
  868. dut.expect('secure_boot_v2: Verifying with %s...' % signed_scheme)
  869. dut.expect('secure_boot_v2: Signature verified successfully!')
  870. dut.expect('flash_encrypt: Flash encryption completed', timeout=90)
  871. dut.expect('Resetting with flash encryption enabled...')
  872. dut.expect('Loading virtual efuse blocks from flash')
  873. dut.expect('Verifying image signature...')
  874. dut.expect('secure_boot_v2: Verifying with %s...' % signed_scheme)
  875. dut.expect('secure_boot_v2: Signature verified successfully!')
  876. dut.expect('secure_boot_v2: enabling secure boot v2...')
  877. dut.expect('secure_boot_v2: secure boot v2 is already enabled, continuing..')
  878. dut.expect_exact('flash_encrypt: flash encryption is enabled (1 plaintext flashes left)')
  879. dut.expect('Loading virtual efuse blocks from flash')
  880. dut.expect_exact('flash_encrypt: Flash encryption mode is DEVELOPMENT (not secure)')
  881. dut.expect('main_task: Calling app_main()')
  882. dut.expect('Start eFuse example')
  883. dut.expect('example: Flash Encryption is NOT in RELEASE mode')
  884. dut.expect('example: Secure Boot is in RELEASE mode')
  885. dut.expect('example: Done')