pytest_system_efuse_example.py 45 KB

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