example_test.py 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  1. from __future__ import unicode_literals
  2. import os
  3. import re
  4. import ttfw_idf
  5. def erase_field_on_emul_efuse(dut, pos_of_bits): # type: (ttfw_idf.TinyFW.Env, list) -> None
  6. emul_efuse_bin_path = os.path.join(dut.app.binary_path, 'emul_efuse.bin')
  7. dut.dump_flash(emul_efuse_bin_path, partition='emul_efuse')
  8. def erase_bit(pos_of_bit): # type: (int) -> None
  9. nbytes, nbits = divmod(pos_of_bit, 8)
  10. with open(emul_efuse_bin_path, 'r+b') as f:
  11. f.seek(nbytes)
  12. data = ord(f.read(1))
  13. data &= ~(1 << nbits)
  14. f.seek(-1, os.SEEK_CUR)
  15. f.write(bytes([data]))
  16. for pos_of_bit in sorted(pos_of_bits):
  17. erase_bit(pos_of_bit)
  18. offs = dut.app.partition_table['emul_efuse']['offset']
  19. flash_files = [(offs, emul_efuse_bin_path)]
  20. dut.write_flash(flash_files)
  21. @ttfw_idf.idf_example_test(env_tag='Example_GENERIC', target=['esp32', 'esp32c3'])
  22. def test_examples_efuse(env, _): # type: (ttfw_idf.TinyFW.Env, None) -> None
  23. dut = env.get_dut('efuse', 'examples/system/efuse')
  24. dut.start_app()
  25. dut.expect_all(re.compile(r'example: Coding Scheme (3/4)|(NONE)|(REPEAT)|(RS \(Reed-Solomon coding\))'),
  26. 'example: read efuse fields',
  27. re.compile(r'example: 1. read MAC address: {}'.format(r':'.join((r'[0-9a-f]{2}',) * 6))),
  28. 'example: 2. read secure_version: 0',
  29. 'example: 3. read custom fields',
  30. 'example: module_version = 0',
  31. 'example: device_role = None',
  32. 'example: setting_1 = 0',
  33. 'example: setting_2 = 0',
  34. 'example: custom_secure_version = 0',
  35. 'example: This example does not burn any efuse in reality only virtually',
  36. 'example: Write operations in efuse fields are performed virtually',
  37. 'example: write custom efuse fields',
  38. 'efuse: Virtual efuses enabled: Not really burning eFuses',
  39. 'example: module_version = 1',
  40. 'example: device_role = Slave',
  41. 'example: setting_1 = 3',
  42. 'example: setting_2 = 4',
  43. 'example: custom_secure_version = 5',
  44. 'example: Done',
  45. timeout=30)
  46. @ttfw_idf.idf_example_test(env_tag='Example_GENERIC', target=['esp32', 'esp32s2', 'esp32c3'])
  47. def test_examples_efuse_with_virt_flash_enc(env, _): # type: (ttfw_idf.TinyFW.Env, None) -> None
  48. dut = env.get_dut('efuse', 'examples/system/efuse', app_config_name='virt_flash_enc')
  49. # check and log bin size
  50. binary_file = os.path.join(dut.app.binary_path, 'bootloader', 'bootloader.bin')
  51. bin_size = os.path.getsize(binary_file)
  52. ttfw_idf.log_performance('{}_bootloader_{}_bin_size'.format(dut.app.target, dut.app.config_name), '{}KB'.format(bin_size // 1024))
  53. print(' - Erase flash')
  54. dut.erase_flash()
  55. print(' - Start app (flash partition_table and app)')
  56. dut.start_app_no_enc()
  57. dut.expect('Loading virtual efuse blocks from real efuses')
  58. dut.expect('Checking flash encryption...')
  59. dut.expect('Generating new flash encryption key...')
  60. if dut.TARGET == 'esp32':
  61. dut.expect('Writing EFUSE_BLK_KEY0 with purpose 2')
  62. dut.expect('Setting CRYPT_CONFIG efuse to 0xF')
  63. dut.expect('Not disabling UART bootloader encryption')
  64. dut.expect('Disable UART bootloader decryption...')
  65. dut.expect('Disable UART bootloader MMU cache...')
  66. dut.expect('Disable JTAG...')
  67. dut.expect('Disable ROM BASIC interpreter fallback...')
  68. else:
  69. dut.expect('Writing EFUSE_BLK_KEY0 with purpose 4')
  70. dut.expect('Not disabling UART bootloader encryption')
  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.TARGET == 'esp32':
  80. dut.expect('flash encryption is enabled (3 plaintext flashes left)')
  81. else:
  82. dut.expect('flash encryption is enabled (1 plaintext flashes left)')
  83. dut.expect('Flash encryption mode is DEVELOPMENT (not secure)')
  84. dut.expect('Start eFuse example')
  85. dut.expect('example: Done')
  86. @ttfw_idf.idf_example_test(env_tag='Example_GENERIC', target=['esp32s2'])
  87. def test_examples_efuse_with_virt_flash_enc_aes_256(env, _): # type: (ttfw_idf.TinyFW.Env, None) -> None
  88. # Only ESP32-S2 has support AES-256 FLASH_ENCRYPTION key
  89. dut = env.get_dut('efuse', 'examples/system/efuse', app_config_name='virt_flash_enc_aes_256')
  90. # check and log bin size
  91. binary_file = os.path.join(dut.app.binary_path, 'bootloader', 'bootloader.bin')
  92. bin_size = os.path.getsize(binary_file)
  93. ttfw_idf.log_performance('{}_bootloader_{}_bin_size'.format(dut.app.target, dut.app.config_name), '{}KB'.format(bin_size // 1024))
  94. print(' - Erase flash')
  95. dut.erase_flash()
  96. print(' - Start app (flash partition_table and app)')
  97. dut.start_app_no_enc()
  98. dut.expect('Loading virtual efuse blocks from real efuses')
  99. dut.expect('Checking flash encryption...')
  100. dut.expect('Generating new flash encryption key...')
  101. dut.expect('Writing EFUSE_BLK_KEY0 with purpose 2')
  102. dut.expect('Writing EFUSE_BLK_KEY1 with purpose 3')
  103. dut.expect('Not disabling UART bootloader encryption')
  104. dut.expect('Disable UART bootloader cache...')
  105. dut.expect('Disable JTAG...')
  106. dut.expect('bootloader encrypted successfully')
  107. dut.expect('partition table encrypted and loaded successfully')
  108. dut.expect('Flash encryption completed', timeout=90)
  109. dut.expect('Resetting with flash encryption enabled...')
  110. dut.expect('Loading virtual efuse blocks from flash')
  111. dut.expect('Checking flash encryption...')
  112. dut.expect('flash encryption is enabled (1 plaintext flashes left)')
  113. dut.expect('Flash encryption mode is DEVELOPMENT (not secure)')
  114. dut.expect('Start eFuse example')
  115. dut.expect('example: Done')
  116. @ttfw_idf.idf_example_test(env_tag='Example_GENERIC', target=['esp32', 'esp32s2', 'esp32c3'])
  117. def test_examples_efuse_with_virt_flash_enc_pre_loaded(env, _): # type: (ttfw_idf.TinyFW.Env, None) -> None
  118. dut = env.get_dut('efuse', 'examples/system/efuse', app_config_name='virt_flash_enc')
  119. print(' - Erase flash')
  120. dut.erase_flash()
  121. print(' - Start app (flash partition_table and app)')
  122. dut.start_app_no_enc()
  123. dut.expect('Loading virtual efuse blocks from real efuses')
  124. dut.expect('Flash encryption completed', timeout=90)
  125. dut.expect('Resetting with flash encryption enabled...')
  126. dut.expect('Flash encryption mode is DEVELOPMENT (not secure)')
  127. dut.expect('Start eFuse example')
  128. dut.expect('example: Done')
  129. if dut.TARGET == 'esp32':
  130. print(' - Flash emul_efuse with pre-loaded efuses (FLASH_CRYPT_CNT 1 -> 0)')
  131. # offset of this eFuse is taken from components/efuse/esp32/esp_efuse_table.csv
  132. FLASH_CRYPT_CNT = 20
  133. # Resets eFuse, which enables Flash encryption feature
  134. erase_field_on_emul_efuse(dut, [FLASH_CRYPT_CNT])
  135. else:
  136. # offset of this eFuse is taken from components/efuse/{target}/esp_efuse_table.csv
  137. print(' - Flash emul_efuse with pre-loaded efuses (SPI_BOOT_CRYPT_CNT 1 -> 0)')
  138. SPI_BOOT_CRYPT_CNT = 82
  139. # Resets eFuse, which enables Flash encryption feature
  140. erase_field_on_emul_efuse(dut, [SPI_BOOT_CRYPT_CNT])
  141. print(' - Start app (flash partition_table and app)')
  142. dut.start_app_no_enc()
  143. dut.expect('Loading virtual efuse blocks from flash')
  144. dut.expect('Checking flash encryption...')
  145. dut.expect('Using pre-loaded flash encryption key in efuse')
  146. if dut.TARGET == 'esp32':
  147. dut.expect('Setting CRYPT_CONFIG efuse to 0xF')
  148. dut.expect('Not disabling UART bootloader encryption')
  149. dut.expect('Disable UART bootloader decryption...')
  150. dut.expect('Disable UART bootloader MMU cache...')
  151. dut.expect('Disable JTAG...')
  152. dut.expect('Disable ROM BASIC interpreter fallback...')
  153. else:
  154. dut.expect('Not disabling UART bootloader encryption')
  155. dut.expect('Disable UART bootloader cache...')
  156. dut.expect('Disable JTAG...')
  157. dut.expect('bootloader encrypted successfully')
  158. dut.expect('partition table encrypted and loaded successfully')
  159. dut.expect('Flash encryption completed', timeout=90)
  160. dut.expect('Resetting with flash encryption enabled...')
  161. dut.expect('Loading virtual efuse blocks from flash')
  162. dut.expect('Checking flash encryption...')
  163. if dut.TARGET == 'esp32':
  164. dut.expect('flash encryption is enabled (3 plaintext flashes left)')
  165. else:
  166. dut.expect('flash encryption is enabled (1 plaintext flashes left)')
  167. dut.expect('Flash encryption mode is DEVELOPMENT (not secure)')
  168. dut.expect('Start eFuse example')
  169. dut.expect('example: Done')
  170. @ttfw_idf.idf_example_test(env_tag='Example_GENERIC', target=['esp32', 'esp32s2', 'esp32c3'])
  171. def test_examples_efuse_with_virt_flash_enc_release(env, _): # type: (ttfw_idf.TinyFW.Env, None) -> None
  172. dut = env.get_dut('efuse', 'examples/system/efuse', app_config_name='virt_flash_enc_release')
  173. # check and log bin size
  174. binary_file = os.path.join(dut.app.binary_path, 'bootloader', 'bootloader.bin')
  175. bin_size = os.path.getsize(binary_file)
  176. ttfw_idf.log_performance('{}_bootloader_{}_bin_size'.format(dut.app.target, dut.app.config_name), '{}KB'.format(bin_size // 1024))
  177. print(' - Erase flash')
  178. dut.erase_flash()
  179. print(' - Start app (flash partition_table and app)')
  180. dut.start_app_no_enc()
  181. dut.expect('Loading virtual efuse blocks from real efuses')
  182. dut.expect('Checking flash encryption...')
  183. dut.expect('Generating new flash encryption key...')
  184. if dut.TARGET == 'esp32':
  185. dut.expect('Writing EFUSE_BLK_KEY0 with purpose 2')
  186. dut.expect('Setting CRYPT_CONFIG efuse to 0xF')
  187. dut.expect('Disable UART bootloader encryption...')
  188. dut.expect('Disable UART bootloader decryption...')
  189. dut.expect('Disable UART bootloader MMU cache...')
  190. dut.expect('Disable JTAG...')
  191. dut.expect('Disable ROM BASIC interpreter fallback...')
  192. else:
  193. dut.expect('Writing EFUSE_BLK_KEY0 with purpose 4')
  194. dut.expect('Disable UART bootloader encryption')
  195. dut.expect('Disable UART bootloader cache...')
  196. dut.expect('Disable JTAG...')
  197. dut.expect('bootloader encrypted successfully')
  198. dut.expect('partition table encrypted and loaded successfully')
  199. dut.expect('Setting CRYPT_CNT for permanent encryption', timeout=90)
  200. dut.expect('Flash encryption completed')
  201. dut.expect('Resetting with flash encryption enabled...')
  202. dut.expect('Loading virtual efuse blocks from flash')
  203. dut.expect('Checking flash encryption...')
  204. dut.expect('flash encryption is enabled (0 plaintext flashes left)')
  205. dut.expect('Flash encryption mode is RELEASE')
  206. dut.expect('Start eFuse example')
  207. dut.expect('example: Done')
  208. @ttfw_idf.idf_example_test(env_tag='Example_GENERIC', target=['esp32'])
  209. def test_examples_efuse_with_virt_secure_boot_v1(env, _): # type: (ttfw_idf.TinyFW.Env, None) -> None
  210. # only for ESP32
  211. dut = env.get_dut('efuse', 'examples/system/efuse', app_config_name='virt_secure_boot_v1')
  212. # check and log bin size
  213. binary_file = os.path.join(dut.app.binary_path, 'bootloader', 'bootloader.bin')
  214. bin_size = os.path.getsize(binary_file)
  215. ttfw_idf.log_performance('{}_bootloader_{}_bin_size'.format(dut.app.target, dut.app.config_name), '{}KB'.format(bin_size // 1024))
  216. print(' - Erase flash')
  217. dut.erase_flash()
  218. print(' - Flash bootloader')
  219. dut.bootloader_flash()
  220. print(' - Start app (flash partition_table and app)')
  221. dut.start_app()
  222. dut.expect('Loading virtual efuse blocks from real efuses')
  223. dut.expect('Verifying image signature...')
  224. dut.expect('secure_boot_v1: Generating new secure boot key...')
  225. dut.expect('secure_boot_v1: Generating secure boot digest...')
  226. dut.expect('secure_boot_v1: Digest generation complete')
  227. dut.expect('Checking secure boot...')
  228. dut.expect('secure_boot_v1: blowing secure boot efuse...')
  229. dut.expect('Read & write protecting new key...')
  230. dut.expect('Disable JTAG...')
  231. dut.expect('Disable ROM BASIC interpreter fallback...')
  232. dut.expect('secure_boot_v1: secure boot is now enabled for bootloader image')
  233. dut.expect('cpu_start: Pro cpu up')
  234. dut.expect('Loading virtual efuse blocks from flash')
  235. dut.expect('Start eFuse example')
  236. dut.expect('example: Done')
  237. dut.reset()
  238. dut.expect('Loading virtual efuse blocks from flash')
  239. dut.expect('Verifying image signature...')
  240. dut.expect('secure_boot_v1: bootloader secure boot is already enabled. No need to generate digest. continuing..')
  241. dut.expect('boot: Checking secure boot...')
  242. dut.expect('secure_boot_v1: bootloader secure boot is already enabled, continuing..')
  243. dut.expect('Start eFuse example')
  244. dut.expect('example: Done')
  245. @ttfw_idf.idf_example_test(env_tag='Example_GENERIC', target=['esp32'])
  246. def test_examples_efuse_with_virt_secure_boot_v1_pre_loaded(env, _): # type: (ttfw_idf.TinyFW.Env, None) -> None
  247. # only for ESP32
  248. dut = env.get_dut('efuse', 'examples/system/efuse', app_config_name='virt_secure_boot_v1')
  249. print(' - Erase flash')
  250. dut.erase_flash()
  251. dut.bootloader_flash()
  252. dut.start_app()
  253. dut.expect('Loading virtual efuse blocks from real efuses')
  254. dut.expect('cpu_start: Pro cpu up')
  255. dut.expect('Loading virtual efuse blocks from flash')
  256. dut.expect('Start eFuse example')
  257. dut.expect('example: Done')
  258. print(' - Flash emul_efuse with pre-loaded efuses (ABS_DONE_0 1 -> 0)')
  259. # offset of this eFuse is taken from components/efuse/esp32/esp_efuse_table.csv
  260. ABS_DONE_0 = 196
  261. # Resets eFuse, which enables Secure boot (V1) feature
  262. erase_field_on_emul_efuse(dut, [ABS_DONE_0])
  263. print(' - Start app (flash partition_table and app)')
  264. dut.start_app()
  265. dut.expect('Loading virtual efuse blocks from flash')
  266. dut.expect('Verifying image signature...')
  267. dut.expect('secure_boot_v1: Using pre-loaded secure boot key in EFUSE block 2')
  268. dut.expect('secure_boot_v1: Generating secure boot digest...')
  269. dut.expect('secure_boot_v1: Digest generation complete')
  270. dut.expect('Checking secure boot...')
  271. dut.expect('secure_boot_v1: blowing secure boot efuse...')
  272. dut.expect('Read & write protecting new key...')
  273. dut.expect('Disable JTAG...')
  274. dut.expect('Disable ROM BASIC interpreter fallback...')
  275. dut.expect('secure_boot_v1: secure boot is now enabled for bootloader image')
  276. dut.expect('cpu_start: Pro cpu up')
  277. dut.expect('Loading virtual efuse blocks from flash')
  278. dut.expect('Start eFuse example')
  279. dut.expect('example: Done')
  280. dut.reset()
  281. dut.expect('Loading virtual efuse blocks from flash')
  282. dut.expect('Verifying image signature...')
  283. dut.expect('secure_boot_v1: bootloader secure boot is already enabled. No need to generate digest. continuing..')
  284. dut.expect('Checking secure boot...')
  285. dut.expect('secure_boot_v1: bootloader secure boot is already enabled, continuing..')
  286. dut.expect('Start eFuse example')
  287. dut.expect('example: Done')
  288. @ttfw_idf.idf_example_test(env_tag='Example_EthKitV12', target=['esp32'])
  289. def test_examples_efuse_with_virt_secure_boot_v2(env, _): # type: (ttfw_idf.TinyFW.Env, None) -> None
  290. # only for ESP32 ECO3
  291. dut = env.get_dut('efuse', 'examples/system/efuse', app_config_name='virt_secure_boot_v2')
  292. # check and log bin size
  293. binary_file = os.path.join(dut.app.binary_path, 'bootloader', 'bootloader.bin')
  294. bin_size = os.path.getsize(binary_file)
  295. ttfw_idf.log_performance('{}_bootloader_{}_bin_size'.format(dut.app.target, dut.app.config_name), '{}KB'.format(bin_size // 1024))
  296. print(' - Erase flash')
  297. dut.erase_flash()
  298. print(' - Flash bootloader')
  299. dut.bootloader_flash()
  300. print(' - Start app (flash partition_table and app)')
  301. dut.start_app()
  302. dut.expect('Loading virtual efuse blocks from real efuses')
  303. dut.expect('Verifying image signature...')
  304. dut.expect('secure_boot_v2: Secure boot V2 is not enabled yet and eFuse digest keys are not set')
  305. dut.expect('secure_boot_v2: Verifying with RSA-PSS...')
  306. dut.expect('secure_boot_v2: Signature verified successfully!')
  307. dut.expect('secure_boot_v2: enabling secure boot v2...')
  308. dut.expect('Verifying image signature...')
  309. dut.expect('secure_boot_v2: Secure boot V2 is not enabled yet and eFuse digest keys are not set')
  310. dut.expect('secure_boot_v2: Verifying with RSA-PSS...')
  311. dut.expect('secure_boot_v2: Signature verified successfully!')
  312. dut.expect('secure_boot_v2: Secure boot digests absent, generating..')
  313. dut.expect('secure_boot_v2: Digests successfully calculated, 1 valid signatures')
  314. dut.expect('secure_boot_v2: 1 signature block(s) found appended to the bootloader')
  315. dut.expect('Writing EFUSE_BLK_KEY1 with purpose 3')
  316. dut.expect('secure_boot_v2: Digests successfully calculated, 1 valid signatures')
  317. dut.expect('secure_boot_v2: 1 signature block(s) found appended to the app')
  318. dut.expect('secure_boot_v2: Application key(0) matches with bootloader key(0)')
  319. dut.expect('secure_boot_v2: blowing secure boot efuse...')
  320. dut.expect('Disable JTAG...')
  321. dut.expect('Disable ROM BASIC interpreter fallback...')
  322. dut.expect('UART ROM Download mode kept enabled - SECURITY COMPROMISED')
  323. dut.expect('Prevent read disabling of additional efuses...')
  324. dut.expect('secure_boot_v2: Secure boot permanently enabled')
  325. dut.expect('cpu_start: Pro cpu up')
  326. dut.expect('Loading virtual efuse blocks from flash')
  327. dut.expect('Start eFuse example')
  328. dut.expect('example: Done')
  329. dut.reset()
  330. dut.expect('Loading virtual efuse blocks from flash')
  331. dut.expect('Verifying image signature...')
  332. dut.expect('secure_boot_v2: Verifying with RSA-PSS...')
  333. dut.expect('secure_boot_v2: Signature verified successfully!')
  334. dut.expect('secure_boot_v2: enabling secure boot v2...')
  335. dut.expect('secure_boot_v2: secure boot v2 is already enabled, continuing..')
  336. dut.expect('Start eFuse example')
  337. dut.expect('example: Done')
  338. @ttfw_idf.idf_example_test(env_tag='Example_EthKitV12', target=['esp32'])
  339. def test_examples_efuse_with_virt_secure_boot_v2_pre_loaded(env, _): # type: (ttfw_idf.TinyFW.Env, None) -> None
  340. # only for ESP32 ECO3
  341. dut = env.get_dut('efuse', 'examples/system/efuse', app_config_name='virt_secure_boot_v2')
  342. print(' - Erase flash')
  343. dut.erase_flash()
  344. print(' - Flash bootloader and app')
  345. dut.bootloader_flash()
  346. dut.start_app()
  347. dut.expect('Loading virtual efuse blocks from real efuses')
  348. dut.expect('cpu_start: Pro cpu up')
  349. dut.expect('Loading virtual efuse blocks from flash')
  350. dut.expect('Start eFuse example')
  351. dut.expect('example: Done')
  352. print(' - Flash emul_efuse with pre-loaded efuses (ABS_DONE_1 1 -> 0)')
  353. # offset of this eFuse is taken from components/efuse/esp32/esp_efuse_table.csv
  354. ABS_DONE_1 = 197
  355. # Resets eFuse, which enables Secure boot (V2) feature
  356. erase_field_on_emul_efuse(dut, [ABS_DONE_1])
  357. print(' - Start app (flash partition_table and app)')
  358. dut.start_app()
  359. dut.expect('Loading virtual efuse blocks from flash')
  360. dut.expect('Verifying image signature...')
  361. dut.expect('secure_boot_v2: Verifying with RSA-PSS...')
  362. dut.expect('secure_boot_v2: Signature verified successfully!')
  363. dut.expect('secure_boot_v2: enabling secure boot v2...')
  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: Secure boot digests already present')
  368. dut.expect('secure_boot_v2: Using pre-loaded public key digest in eFuse')
  369. dut.expect('secure_boot_v2: Digests successfully calculated, 1 valid signatures')
  370. dut.expect('secure_boot_v2: 1 signature block(s) found appended to the app')
  371. dut.expect('secure_boot_v2: Application key(0) matches with bootloader key(0)')
  372. dut.expect('secure_boot_v2: blowing secure boot efuse...')
  373. dut.expect('Disable JTAG...')
  374. dut.expect('Disable ROM BASIC interpreter fallback...')
  375. dut.expect('UART ROM Download mode kept enabled - SECURITY COMPROMISED')
  376. dut.expect('Prevent read disabling of additional efuses...')
  377. dut.expect('secure_boot_v2: Secure boot permanently enabled')
  378. dut.expect('cpu_start: Pro cpu up')
  379. dut.expect('Loading virtual efuse blocks from flash')
  380. dut.expect('Start eFuse example')
  381. dut.expect('example: Done')
  382. dut.reset()
  383. dut.expect('Loading virtual efuse blocks from flash')
  384. dut.expect('Verifying image signature...')
  385. dut.expect('secure_boot_v2: Verifying with RSA-PSS...')
  386. dut.expect('secure_boot_v2: Signature verified successfully!')
  387. dut.expect('secure_boot_v2: enabling secure boot v2...')
  388. dut.expect('secure_boot_v2: secure boot v2 is already enabled, continuing..')
  389. dut.expect('Start eFuse example')
  390. dut.expect('example: Done')
  391. @ttfw_idf.idf_example_test(env_tag='Example_GENERIC', target=['esp32s2', 'esp32c3'])
  392. def test_examples_efuse_with_virt_secure_boot_v2_esp32xx(env, _): # type: (ttfw_idf.TinyFW.Env, None) -> None
  393. dut = env.get_dut('efuse', 'examples/system/efuse', app_config_name='virt_secure_boot_v2')
  394. # check and log bin size
  395. binary_file = os.path.join(dut.app.binary_path, 'bootloader', 'bootloader.bin')
  396. bin_size = os.path.getsize(binary_file)
  397. ttfw_idf.log_performance('{}_bootloader_{}_bin_size'.format(dut.app.target, dut.app.config_name), '{}KB'.format(bin_size // 1024))
  398. print(' - Erase flash')
  399. dut.erase_flash()
  400. print(' - Flash bootloader')
  401. dut.bootloader_flash()
  402. print(' - Start app (flash partition_table and app)')
  403. dut.start_app()
  404. dut.expect('Loading virtual efuse blocks from real efuses')
  405. dut.expect('Verifying image signature...')
  406. dut.expect('secure_boot_v2: Secure boot V2 is not enabled yet and eFuse digest keys are not set')
  407. dut.expect('secure_boot_v2: Verifying with RSA-PSS...')
  408. dut.expect('secure_boot_v2: Signature verified successfully!')
  409. dut.expect('secure_boot_v2: enabling secure boot v2...')
  410. dut.expect('Verifying image signature...')
  411. dut.expect('secure_boot_v2: Secure boot V2 is not enabled yet and eFuse digest keys are not set')
  412. dut.expect('secure_boot_v2: Verifying with RSA-PSS...')
  413. dut.expect('secure_boot_v2: Signature verified successfully!')
  414. dut.expect('secure_boot_v2: Secure boot digests absent, generating..')
  415. dut.expect('secure_boot_v2: Digests successfully calculated, 1 valid signatures')
  416. dut.expect('secure_boot_v2: 1 signature block(s) found appended to the bootloader')
  417. dut.expect('Writing EFUSE_BLK_KEY0 with purpose 9')
  418. dut.expect('secure_boot_v2: Digests successfully calculated, 1 valid signatures')
  419. dut.expect('secure_boot_v2: 1 signature block(s) found appended to the app')
  420. dut.expect('secure_boot_v2: Application key(0) matches with bootloader key(0)')
  421. dut.expect('secure_boot_v2: Revoking empty key digest slot (1)...')
  422. dut.expect('secure_boot_v2: Revoking empty key digest slot (2)...')
  423. dut.expect('secure_boot_v2: blowing secure boot efuse...')
  424. dut.expect('UART ROM Download mode kept enabled - SECURITY COMPROMISED')
  425. dut.expect('Disable hardware & software JTAG...')
  426. dut.expect('secure_boot_v2: Secure boot permanently enabled')
  427. dut.expect('cpu_start: Pro cpu up')
  428. dut.expect('Loading virtual efuse blocks from flash')
  429. dut.expect('Start eFuse example')
  430. dut.expect('example: Done')
  431. dut.reset()
  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('secure_boot_v2: secure boot v2 is already enabled, continuing..')
  438. dut.expect('Start eFuse example')
  439. dut.expect('example: Done')
  440. @ttfw_idf.idf_example_test(env_tag='Example_GENERIC', target=['esp32s2', 'esp32c3'])
  441. def test_examples_efuse_with_virt_secure_boot_v2_esp32xx_pre_loaded(env, _): # type: (ttfw_idf.TinyFW.Env, None) -> None
  442. dut = env.get_dut('efuse', 'examples/system/efuse', app_config_name='virt_secure_boot_v2')
  443. print(' - Erase flash')
  444. dut.erase_flash()
  445. print(' - Flash bootloader and app')
  446. dut.bootloader_flash()
  447. dut.start_app()
  448. dut.expect('Loading virtual efuse blocks from real efuses')
  449. dut.expect('cpu_start: Pro cpu up')
  450. dut.expect('Loading virtual efuse blocks from flash')
  451. dut.expect('Start eFuse example')
  452. dut.expect('example: Done')
  453. print(' - Flash emul_efuse with pre-loaded efuses (SECURE_BOOT_EN 1 -> 0, SECURE_BOOT_KEY_REVOKE[0..2] -> 0)')
  454. # offsets of eFuses are taken from components/efuse/{target}/esp_efuse_table.csv
  455. SECURE_BOOT_EN = 116
  456. SECURE_BOOT_KEY_REVOKE0 = 85
  457. SECURE_BOOT_KEY_REVOKE1 = 86
  458. SECURE_BOOT_KEY_REVOKE2 = 87
  459. # Resets eFuse, which enables Secure boot feature
  460. # Resets eFuses, which control digest slots
  461. erase_field_on_emul_efuse(dut, [SECURE_BOOT_EN, SECURE_BOOT_KEY_REVOKE0, SECURE_BOOT_KEY_REVOKE1, SECURE_BOOT_KEY_REVOKE2])
  462. print(' - Start app (flash partition_table and app)')
  463. dut.start_app()
  464. dut.expect('Loading virtual efuse blocks from flash')
  465. dut.expect('Verifying image signature...')
  466. dut.expect('secure_boot_v2: Verifying with RSA-PSS...')
  467. dut.expect('secure_boot_v2: Signature verified successfully!')
  468. dut.expect('secure_boot_v2: Secure boot digests already present')
  469. dut.expect('secure_boot_v2: Using pre-loaded public key digest in eFuse')
  470. dut.expect('secure_boot_v2: Digests successfully calculated, 1 valid signatures')
  471. dut.expect('secure_boot_v2: 1 signature block(s) found appended to the app')
  472. dut.expect('secure_boot_v2: Application key(0) matches with bootloader key(0)')
  473. dut.expect('secure_boot_v2: Revoking empty key digest slot (1)...')
  474. dut.expect('secure_boot_v2: Revoking empty key digest slot (2)...')
  475. dut.expect('secure_boot_v2: blowing secure boot efuse...')
  476. dut.expect('UART ROM Download mode kept enabled - SECURITY COMPROMISED')
  477. dut.expect('Disable hardware & software JTAG...')
  478. dut.expect('secure_boot_v2: Secure boot permanently enabled')
  479. dut.expect('cpu_start: Pro cpu up')
  480. dut.expect('Loading virtual efuse blocks from flash')
  481. dut.expect('Start eFuse example')
  482. dut.expect('example: Done')
  483. dut.reset()
  484. dut.expect('Loading virtual efuse blocks from flash')
  485. dut.expect('Verifying image signature...')
  486. dut.expect('secure_boot_v2: Verifying with RSA-PSS...')
  487. dut.expect('secure_boot_v2: Signature verified successfully!')
  488. dut.expect('secure_boot_v2: enabling secure boot v2...')
  489. dut.expect('secure_boot_v2: secure boot v2 is already enabled, continuing..')
  490. dut.expect('Start eFuse example')
  491. dut.expect('example: Done')
  492. @ttfw_idf.idf_example_test(env_tag='Example_GENERIC', target=['esp32'])
  493. def test_examples_efuse_with_virt_sb_v1_and_fe(env, _): # type: (ttfw_idf.TinyFW.Env, None) -> None
  494. dut = env.get_dut('efuse', 'examples/system/efuse', app_config_name='virt_sb_v1_and_fe')
  495. # check and log bin size
  496. binary_file = os.path.join(dut.app.binary_path, 'bootloader', 'bootloader.bin')
  497. bin_size = os.path.getsize(binary_file)
  498. ttfw_idf.log_performance('{}_bootloader_{}_bin_size'.format(dut.app.target, dut.app.config_name), '{}KB'.format(bin_size // 1024))
  499. print(' - Erase flash')
  500. dut.erase_flash()
  501. print(' - Flash bootloader')
  502. dut.bootloader_flash()
  503. print(' - Start app (flash partition_table and app)')
  504. dut.start_app_no_enc()
  505. dut.expect('Loading virtual efuse blocks from real efuses')
  506. dut.expect('Verifying image signature...')
  507. dut.expect('secure_boot_v1: Generating new secure boot key...')
  508. dut.expect('secure_boot_v1: Generating secure boot digest...')
  509. dut.expect('secure_boot_v1: Digest generation complete')
  510. dut.expect('Checking flash encryption...')
  511. dut.expect('flash_encrypt: Generating new flash encryption key...')
  512. dut.expect('Writing EFUSE_BLK_KEY0 with purpose 2')
  513. dut.expect('flash_encrypt: Setting CRYPT_CONFIG efuse to 0xF')
  514. dut.expect('flash_encrypt: Not disabling UART bootloader encryption')
  515. dut.expect('flash_encrypt: Disable UART bootloader decryption...')
  516. dut.expect('flash_encrypt: Disable UART bootloader MMU cache...')
  517. dut.expect('flash_encrypt: Disable JTAG...')
  518. dut.expect('flash_encrypt: Disable ROM BASIC interpreter fallback...')
  519. dut.expect('flash_encrypt: bootloader encrypted successfully')
  520. dut.expect('flash_encrypt: partition table encrypted and loaded successfully')
  521. dut.expect('Verifying image signature...')
  522. dut.expect('flash_encrypt: Flash encryption completed', timeout=90)
  523. dut.expect('Checking secure boot...')
  524. dut.expect('secure_boot_v1: blowing secure boot efuse...')
  525. dut.expect('Read & write protecting new key...')
  526. dut.expect('Disable JTAG...')
  527. dut.expect('Disable ROM BASIC interpreter fallback...')
  528. dut.expect('secure_boot_v1: secure boot is now enabled for bootloader image')
  529. dut.expect('Resetting with flash encryption enabled...')
  530. dut.expect('Verifying image signature...')
  531. dut.expect('secure_boot_v1: bootloader secure boot is already enabled. No need to generate digest. continuing..')
  532. dut.expect('Checking flash encryption...')
  533. dut.expect('flash_encrypt: flash encryption is enabled (3 plaintext flashes left)')
  534. dut.expect('Checking secure boot...')
  535. dut.expect('secure_boot_v1: bootloader secure boot is already enabled, continuing..')
  536. dut.expect('cpu_start: Pro cpu up')
  537. dut.expect('Loading virtual efuse blocks from flash')
  538. dut.expect('flash_encrypt: Flash encryption mode is DEVELOPMENT (not secure)')
  539. dut.expect('Start eFuse example')
  540. dut.expect('example: Done')
  541. @ttfw_idf.idf_example_test(env_tag='Example_EthKitV12', target=['esp32'])
  542. def test_examples_efuse_with_virt_sb_v2_and_fe(env, _): # type: (ttfw_idf.TinyFW.Env, None) -> None
  543. # only for ESP32 ECO3
  544. dut = env.get_dut('efuse', 'examples/system/efuse', app_config_name='virt_sb_v2_and_fe')
  545. # check and log bin size
  546. binary_file = os.path.join(dut.app.binary_path, 'bootloader', 'bootloader.bin')
  547. bin_size = os.path.getsize(binary_file)
  548. ttfw_idf.log_performance('{}_bootloader_{}_bin_size'.format(dut.app.target, dut.app.config_name), '{}KB'.format(bin_size // 1024))
  549. print(' - Erase flash')
  550. dut.erase_flash()
  551. print(' - Flash bootloader')
  552. dut.bootloader_flash()
  553. print(' - Start app (flash partition_table and app)')
  554. dut.start_app_no_enc()
  555. dut.expect('Loading virtual efuse blocks from real efuses')
  556. dut.expect('secure_boot_v2: Secure boot V2 is not enabled yet and eFuse digest keys are not set')
  557. dut.expect('secure_boot_v2: Verifying with RSA-PSS...')
  558. dut.expect('secure_boot_v2: Signature verified successfully!')
  559. dut.expect('secure_boot_v2: enabling secure boot v2...')
  560. dut.expect('Verifying image signature...')
  561. dut.expect('secure_boot_v2: Secure boot V2 is not enabled yet and eFuse digest keys are not set')
  562. dut.expect('secure_boot_v2: Verifying with RSA-PSS...')
  563. dut.expect('secure_boot_v2: Signature verified successfully')
  564. dut.expect('secure_boot_v2: Secure boot digests absent, generating..')
  565. dut.expect('secure_boot_v2: Digests successfully calculated, 1 valid signatures')
  566. dut.expect('secure_boot_v2: 1 signature block(s) found appended to the bootloader')
  567. dut.expect('Writing EFUSE_BLK_KEY1 with purpose 3')
  568. dut.expect('secure_boot_v2: Digests successfully calculated, 1 valid signatures')
  569. dut.expect('secure_boot_v2: 1 signature block(s) found appended to the app')
  570. dut.expect('secure_boot_v2: Application key(0) matches with bootloader key(0)')
  571. dut.expect('secure_boot_v2: blowing secure boot efuse...')
  572. dut.expect('Disable JTAG...')
  573. dut.expect('Disable ROM BASIC interpreter fallback...')
  574. dut.expect('UART ROM Download mode kept enabled - SECURITY COMPROMISED')
  575. dut.expect('secure_boot_v2: Secure boot permanently enabled')
  576. dut.expect('Checking flash encryption...')
  577. dut.expect('flash_encrypt: Generating new flash encryption key...')
  578. dut.expect('Writing EFUSE_BLK_KEY0 with purpose 2')
  579. dut.expect('flash_encrypt: Setting CRYPT_CONFIG efuse to 0xF')
  580. dut.expect('flash_encrypt: Not disabling UART bootloader encryption')
  581. dut.expect('flash_encrypt: Disable UART bootloader decryption...')
  582. dut.expect('flash_encrypt: Disable UART bootloader MMU cache...')
  583. dut.expect('flash_encrypt: Disable JTAG...')
  584. dut.expect('flash_encrypt: Disable ROM BASIC interpreter fallback...')
  585. dut.expect('Verifying image signature...')
  586. dut.expect('secure_boot_v2: Verifying with RSA-PSS...')
  587. dut.expect('secure_boot_v2: Signature verified successfully!')
  588. dut.expect('flash_encrypt: bootloader encrypted successfully')
  589. dut.expect('flash_encrypt: partition table encrypted and loaded successfully')
  590. dut.expect('Verifying image signature...')
  591. dut.expect('secure_boot_v2: Verifying with RSA-PSS...')
  592. dut.expect('secure_boot_v2: Signature verified successfully!')
  593. dut.expect('flash_encrypt: Flash encryption completed', timeout=90)
  594. dut.expect('Resetting with flash encryption enabled...')
  595. dut.expect('Loading virtual efuse blocks from flash')
  596. dut.expect('Verifying image signature...')
  597. dut.expect('secure_boot_v2: Verifying with RSA-PSS...')
  598. dut.expect('secure_boot_v2: Signature verified successfully!')
  599. dut.expect('secure_boot_v2: enabling secure boot v2...')
  600. dut.expect('secure_boot_v2: secure boot v2 is already enabled, continuing..')
  601. dut.expect('flash_encrypt: flash encryption is enabled (3 plaintext flashes left)')
  602. dut.expect('cpu_start: Pro cpu up')
  603. dut.expect('Loading virtual efuse blocks from flash')
  604. dut.expect('flash_encrypt: Flash encryption mode is DEVELOPMENT (not secure)')
  605. dut.expect('Start eFuse example')
  606. dut.expect('example: Done')
  607. @ttfw_idf.idf_example_test(env_tag='Example_GENERIC', target=['esp32s2', 'esp32c3'])
  608. def test_examples_efuse_with_virt_sb_v2_and_fe_esp32xx(env, _): # type: (ttfw_idf.TinyFW.Env, None) -> None
  609. dut = env.get_dut('efuse', 'examples/system/efuse', app_config_name='virt_sb_v2_and_fe')
  610. # check and log bin size
  611. binary_file = os.path.join(dut.app.binary_path, 'bootloader', 'bootloader.bin')
  612. bin_size = os.path.getsize(binary_file)
  613. ttfw_idf.log_performance('{}_bootloader_{}_bin_size'.format(dut.app.target, dut.app.config_name), '{}KB'.format(bin_size // 1024))
  614. print(' - Erase flash')
  615. dut.erase_flash()
  616. print(' - Flash bootloader')
  617. dut.bootloader_flash()
  618. print(' - Start app (flash partition_table and app)')
  619. dut.start_app_no_enc()
  620. dut.expect('Loading virtual efuse blocks from real efuses')
  621. dut.expect('Verifying image signature...')
  622. dut.expect('secure_boot_v2: Secure boot V2 is not enabled yet and eFuse digest keys are not set')
  623. dut.expect('secure_boot_v2: Verifying with RSA-PSS...')
  624. dut.expect('secure_boot_v2: Signature verified successfully!')
  625. dut.expect('secure_boot_v2: enabling secure boot v2...')
  626. dut.expect('Verifying image signature...')
  627. dut.expect('secure_boot_v2: Secure boot V2 is not enabled yet and eFuse digest keys are not set')
  628. dut.expect('secure_boot_v2: Verifying with RSA-PSS...')
  629. dut.expect('secure_boot_v2: Signature verified successfully!')
  630. dut.expect('secure_boot_v2: Secure boot digests absent, generating..')
  631. dut.expect('secure_boot_v2: Digests successfully calculated, 1 valid signatures')
  632. dut.expect('secure_boot_v2: 1 signature block(s) found appended to the bootloader')
  633. dut.expect('Writing EFUSE_BLK_KEY0 with purpose 9')
  634. dut.expect('secure_boot_v2: Digests successfully calculated, 1 valid signatures')
  635. dut.expect('secure_boot_v2: 1 signature block(s) found appended to the app')
  636. dut.expect('secure_boot_v2: Application key(0) matches with bootloader key(0)')
  637. dut.expect('secure_boot_v2: Revoking empty key digest slot (1)...')
  638. dut.expect('secure_boot_v2: Revoking empty key digest slot (2)...')
  639. dut.expect('secure_boot_v2: blowing secure boot efuse...')
  640. dut.expect('UART ROM Download mode kept enabled - SECURITY COMPROMISED')
  641. dut.expect('Disable hardware & software JTAG...')
  642. dut.expect('secure_boot_v2: Secure boot permanently enabled')
  643. dut.expect('Checking flash encryption...')
  644. dut.expect('flash_encrypt: Generating new flash encryption key...')
  645. dut.expect('Writing EFUSE_BLK_KEY1 with purpose 4')
  646. dut.expect('Not disabling UART bootloader encryption')
  647. dut.expect('Disable UART bootloader cache...')
  648. dut.expect('Disable JTAG...')
  649. dut.expect('Verifying image signature...')
  650. dut.expect('secure_boot_v2: Verifying with RSA-PSS...')
  651. dut.expect('secure_boot_v2: Signature verified successfully!')
  652. dut.expect('flash_encrypt: bootloader encrypted successfully')
  653. dut.expect('flash_encrypt: partition table encrypted and loaded successfully')
  654. dut.expect('Verifying image signature...')
  655. dut.expect('secure_boot_v2: Verifying with RSA-PSS...')
  656. dut.expect('secure_boot_v2: Signature verified successfully!')
  657. dut.expect('flash_encrypt: Flash encryption completed', timeout=90)
  658. dut.expect('Resetting with flash encryption enabled...')
  659. dut.expect('Loading virtual efuse blocks from flash')
  660. dut.expect('Verifying image signature...')
  661. dut.expect('secure_boot_v2: Verifying with RSA-PSS...')
  662. dut.expect('secure_boot_v2: Signature verified successfully!')
  663. dut.expect('secure_boot_v2: enabling secure boot v2...')
  664. dut.expect('secure_boot_v2: secure boot v2 is already enabled, continuing..')
  665. dut.expect('flash_encrypt: flash encryption is enabled (1 plaintext flashes left)')
  666. dut.expect('cpu_start: Pro cpu up')
  667. dut.expect('Loading virtual efuse blocks from flash')
  668. dut.expect('flash_encrypt: Flash encryption mode is DEVELOPMENT (not secure)')
  669. dut.expect('Start eFuse example')
  670. dut.expect('example: Done')
  671. if __name__ == '__main__':
  672. test_examples_efuse()
  673. test_examples_efuse_with_virt_flash_enc()
  674. test_examples_efuse_with_virt_flash_enc_pre_loaded()
  675. test_examples_efuse_with_virt_flash_enc_aes_256()
  676. test_examples_efuse_with_virt_flash_enc_release()
  677. test_examples_efuse_with_virt_secure_boot_v1()
  678. test_examples_efuse_with_virt_secure_boot_v1_pre_loaded()
  679. test_examples_efuse_with_virt_secure_boot_v2()
  680. test_examples_efuse_with_virt_secure_boot_v2_pre_loaded()
  681. test_examples_efuse_with_virt_secure_boot_v2_esp32xx()
  682. test_examples_efuse_with_virt_secure_boot_v2_esp32xx_pre_loaded()
  683. test_examples_efuse_with_virt_sb_v1_and_fe()
  684. test_examples_efuse_with_virt_sb_v2_and_fe()
  685. test_examples_efuse_with_virt_sb_v2_and_fe_esp32xx()