
    :)di>                         d dl Z d dlmZ d dlmZ d dlZd dlZd dlZddl	m
Z
 ddlmZ ddl	mZ dd	lmZmZmZmZmZmZ  G d
 de          ZdS )    N)BinaryIO)log   )fields)EfuseDefineBlocks   )util)BaseCommandsTupleParameterNonCompositeTupleadd_force_write_alwaysadd_show_sensitive_info_optionprotect_optionsc                       e Zd ZdZej        Zdej        f fdZ	de
fdZd Z	 	 	 	 dd	ee
         d
ee         dee
         dedededee         dz  fdZ	 	 	 dd	ee
         d
ee         dee
         dededefdZ xZS )ESP32S3CommandszESP32-S3clic                     t                                          |           t                                                      }|                    ddd                    |           dd                    t          j        j                   d          t          j
        ddt          d	d
t          |          t          t          j        |          t          j        d          t          j        t          j        j                  g                    t           t"          t$          t          j         fd                                                                        }|                    ddd                    |           dd                    t          j        j                   d          t          j
        ddt          d	d
t          |          t          t          j        |          t          j        d          t          j        t          j        j                  g                    t           t"          t$          t          j         fd                                                                        }|                    dd          t          j
        dt          j        g d                     fd                        }d S )Nzburn-keyzBurn the key block with the specified name. Arguments are groups of block name, key file (containing 256 bits of binary key data) and key purpose.

Block is one of: [z, z]

Key purpose is one of: [])helpblock_keyfile_keypurposez<BLOCK> <KEYFILE> <KEYPURPOSE>Trb)metavarclsrequirednargs	max_aritytypec                     |                     d           t          |                     d           \  }}}| j        |d<    j        |||fi | dS )z*Burn the key block with the specified nameforce_write_alwaysr   show_sensitive_infoN)popzipr!   burn_keyctxkwargsblockkeyfile
keypurposeselfs        h/var/www/menimich/repos/protonApp/venv/lib/python3.11/site-packages/espefuse/efuse/esp32s3/operations.pyburn_key_cliz6ESP32S3Commands.add_cli_commands.<locals>.burn_key_cli'   sh    8 JJ+,,,),fjj9S.T.T)U&E7J,/,CF()DM%*???????    zburn-key-digestc                     |                     d           t          |                     d           \  }}}| j        |d<    j        |||fi | dS )z=Parse a RSA public key and burn the digest to key eFuse blockr    r   r!   N)r"   r#   r!   burn_key_digestr%   s        r,   burn_key_digest_cliz=ESP32S3Commands.add_cli_commands.<locals>.burn_key_digest_cliH   si    8 JJ+,,,),fjj9S.T.T)U&E7J,/,CF() D FFvFFFFFr.   zset-flash-voltagez5Permanently set the internal flash voltage regulator.)
short_helpvoltage)1.8V3.3VOFF)r   c                 2                         |            dS )zPermanently set the internal flash voltage regulator to either 1.8V, 3.3V or OFF.
            This means GPIO45 can be high or low at reset without changing the flash voltage.N)set_flash_voltage)r3   r+   s    r,   set_flash_voltage_cliz?ESP32S3Commands.add_cli_commands.<locals>.set_flash_voltage_clii   s     ""7+++++r.   )superadd_cli_commandsr   get_blocks_for_keyscommandjoinr   EfuseKeyPurposeFieldKEY_PURPOSES_NAMEclickargumentr   lenr   ChoiceFiler   r   r   pass_contextDIGEST_KEY_PURPOSES)r+   r   blocks_for_keysr-   r1   r9   	__class__s   `     r,   r;   z ESP32S3Commands.add_cli_commands#   s     %%%+--AACC	c!%?!;!;c c (,yy1L1^'_'_c c c 
 

 

 
&4/**"L11Jt$$L!<!NOO 

 

 

 
		'			@ 	@ 	@ 	@ 
	 
(	' 
 	 


 



 

4	@ 
e!%?!;!;e e (,yy1L1`'a'ae e e 
 

 

 
&4/**"L11Jt$$L!<!PQQ 

 

 

 
		'			G 	G 	G 	G 
	 
(	' 
 	 


 



 

4	G 
N 
 

 

 
	5L5L5L(M(M	N	N	N	, 	, 	, 	, 
O	N	

 


	, 	, 	,r.   r3   c                    | j         d         }| j         d         }| j         d         }|dk    r,|                                dk    rt          j        d          |dk    r,|                                dk    rt          j        d          |dk    r3t	          j        d	|                                dk    rd
nd d           n5|dk    rt	          j        d           n|dk    rt	          j        d           |                    d           |dk    r|                    d           |dk    r|                    d           t	          j        d           | j                             d          sd S t	          j        d           d S )NVDD_SPI_FORCEVDD_SPI_TIEHVDD_SPI_XPDr6   r   zGCan't set flash regulator to OFF as VDD_SPI_XPD eFuse is already burnedr4   zCCan't set regulator to 1.8V is VDD_SPI_TIEH eFuse is already burnedzDisable internal flash voltage regulator (VDD_SPI). SPI flash will need to be powered from an external source.
The following eFuse is burned: VDD_SPI_FORCE.
It is possible to later re-enable the internal regulatorzto 3.3Vzto 1.8V or 3.3Vzby burning an additional eFusezSet internal flash voltage regulator (VDD_SPI) to 1.8V.
The following eFuses are burned: VDD_SPI_FORCE, VDD_SPI_XPD.
It is possible to later increase the voltage to 3.3V (permanently) by burning additional eFuse VDD_SPI_TIEHr5   zEnable internal flash voltage regulator (VDD_SPI) to 3.3V.
The following eFuses are burned: VDD_SPI_FORCE, VDD_SPI_XPD, VDD_SPI_TIEH.r   zVDD_SPI setting complete.Tcheck_batch_modeSuccessful.)efusesgetesptool
FatalErrorr   printsaveburn_all)r+   r3   
sdio_force	sdio_tiehsdio_regs        r,   r8   z!ESP32S3Commands.set_flash_voltageu   s   [1
K/	;}- e! 3 3$Y   fA!5!5$U   eI1 !*1 4 499:K	1 1 1    I;    I]  
 	eMM!fNN1	-...{##T#:: 	F	-     r.   c                    t          j        d           t          j        d| j                                                   | j                                        dk    rt          j        d| j        d                                          d           t          j        d| j        d                                                    t          j        d	           t          j        d
| j        d                                                    t          j        d| j        d                                                    t          j        d| j        d                                                    t          j        d| j        d                                                    t          j        d| j        d                                                    t          j        d| j        d                                                    t          j        d| j        d                                                    t          j        d| j        d                                                    t          j        d           t          j        d
| j        d                                                    t          j        d| j        d                                                    t          j        d| j        d                                                    t          j        d| j        d                                                    t          j        d| j        d                                                    t          j        d| j        d                                                     t          j        d| j        d!                                                    d S d S )"N zBlock version:d   z!Temperature Sensor Calibration = 
TEMP_CALIBCzADC OCode        = OCODEzADC1:zINIT_CODE_ATTEN0 = ADC1_INIT_CODE_ATTEN0zINIT_CODE_ATTEN1 = ADC1_INIT_CODE_ATTEN1zINIT_CODE_ATTEN2 = ADC1_INIT_CODE_ATTEN2zINIT_CODE_ATTEN3 = ADC1_INIT_CODE_ATTEN3zCAL_VOL_ATTEN0   = ADC1_CAL_VOL_ATTEN0zCAL_VOL_ATTEN1   = ADC1_CAL_VOL_ATTEN1zCAL_VOL_ATTEN2   = ADC1_CAL_VOL_ATTEN2zCAL_VOL_ATTEN3   = ADC1_CAL_VOL_ATTEN3zADC2:ADC2_INIT_CODE_ATTEN0ADC2_INIT_CODE_ATTEN1ADC2_INIT_CODE_ATTEN2ADC2_INIT_CODE_ATTEN3ADC2_CAL_VOL_ATTEN0ADC2_CAL_VOL_ATTEN1ADC2_CAL_VOL_ATTEN2)r   rU   rQ   get_block_versionrR   )r+   s    r,   adc_infozESP32S3Commands.adc_info   s   	"	"DK$A$A$C$CDDD;((**c11I\$+l:S:W:W:Y:Y\\\]]]I+T[-A-E-E-G-GHHHIgI+T[9P-Q-U-U-W-WXXXI+T[9P-Q-U-U-W-WXXXI+T[9P-Q-U-U-W-WXXXI+T[9P-Q-U-U-W-WXXXI+T[9N-O-S-S-U-UVVVI+T[9N-O-S-S-U-UVVVI+T[9N-O-S-S-U-UVVVI+T[9N-O-S-S-U-UVVVIgI+T[9P-Q-U-U-W-WXXXI+T[9P-Q-U-U-W-WXXXI+T[9P-Q-U-U-W-WXXXI+T[9P-Q-U-U-W-WXXXI+T[9N-O-S-S-U-UVVVI+T[9N-O-S-S-U-UVVVI+T[9N-O-S-S-U-UVVVVV) 21r.   FNblockskeyfileskeypurposesno_write_protectno_read_protectr!   digestc                 2
   |"|dt          d |D                                }n!|dt          d |D                                }|dt          d |D                                }	|dt          d |D                                }
|                     |	||
          \  }	}}
t          j        d           t	          |	||
          D ]\  }}}d}| j        j        D ](}||j        k    s	||j        v r| j        |j                 })|t          j
        d| d	          |j        d
z  }| j                            |          }| j        j        |         }t          |t          j                  r)|                                }|                                 n|}t          j        d|j         d           d}| j        |j                                     |          rd}|ddd         }t          j        d                    |rt+          j        |d          n%d                    dgt          |          z                                 |rt          j        |           t          |          |k    r.t          j
        dt          |           d| d|d
z   d          | j        |j                                     |          r|rdnd}nd}| }|                    |           d}| j        |j                                                 |k    r| j        |j                                                 rmt          j        d|j         d| j        |j                                                  d| d           | j        |j                                     |           d}nft          j
        d|j         d| d          t          j        d|j         d| d           | j        |j                                                 rd}|rAt          j        d |j         d!           | j        |j                                                  |r(t          j        d"           |                                 |r(t          j        d#           |                                 t          j        d$           |st          j        d%           |rt          j        d&           | j                            d'          sdS t          j        d(           dS ))a  Burn the key block with the specified name. Arguments are groups of block name,
        key file (containing 256 bits of binary key data) and key purpose.

        Args:
            blocks: List of eFuse block names to burn keys to.
            keyfiles: List of open files to read key data from.
            keypurposes: List of key purposes to burn.
            no_write_protect: If True, the write protection will NOT be enabled.
            no_read_protect: If True, the read protection will NOT be enabled.
            show_sensitive_info: If True, the sensitive information will be shown.
            digest: List of digests to burn.
        Nr   c                     g | ]}||S N .0names     r,   
<listcomp>z,ESP32S3Commands.burn_key.<locals>.<listcomp>   s    GGG$d6F6F6F6Fr.   c                     g | ]}||S rz   r{   r|   s     r,   r   z,ESP32S3Commands.burn_key.<locals>.<listcomp>   s    EEE$D4D4D4D4Dr.   c                     g | ]}||S rz   r{   r|   s     r,   r   z,ESP32S3Commands.burn_key.<locals>.<listcomp>   s    AAAd0@T0@0@0@r.   c                     g | ]}||S rz   r{   r|   s     r,   r   z,ESP32S3Commands.burn_key.<locals>.<listcomp>   s    FFFdT5ET5E5E5Er.   zBurn keys to blocks:Unknown block name - .   z -  )endz8	Reversing byte order for AES-XTS hardware peripheral...r   z-> [{}]z??zIncorrect key file size z. Key file must be z bytes (z bits) of raw binary key data.FTz	'z': 'z' -> 'z'.zIt is not possible to change 'z' to 'z&' because write protection bit is set.z' is already 'z	Disabling write to 'z'...z	Disabling read to key block...z 	Disabling write to key block...r\   z7Keys will remain writeable (due to --no-write-protect).z5Keys will remain readable (due to --no-read-protect).rN   rP   )rC   _adjust_key_data_for_blocksr   rU   r#   rQ   rr   r~   aliasrS   rT   bit_lenget_index_block_by_name
isinstanceioIOBasereadclosekey_purpose_nameneed_reverseformatr	   hexifyr>   need_rd_protectrV   rR   is_writeabledisable_writedisable_readrW   )r+   rr   rs   rt   ru   rv   r!   rw   datafile_listblock_name_listkeypurpose_list
block_namedatafiler*   efuseblk	num_bytes	block_numr(   data
revers_msgread_protectwrite_protectdisable_wr_protect_key_purposes                           r,   r$   zESP32S3Commands.burn_key   s   . >$CGG(GGGHHJMM #CEE&EEEFFHM !AAfAAABBD
 &FFkFFFGGI

 ,,  	8 		()))03]O1
 1
 O	 O	,J* E{) 2 2))Z39-D-D K1E}()N)N)N)NOOO*I;;JGGIK&y1E(BI..  }}    I(EJ((c2222J{512??
KK "X
DDbDzI  *6DKc***4&3t99"455     &	*%%%4yyI%%(Ls4yy L LY L L'!mL L L  
 {512BB:NN %(7AuuT$ 00M JJt-2*{5126688JFF;u56CCEE I\e4 \ \ K(>?CCEE\ \MW\ \ \   K 67<<ZHHH5922!,R9O R R)R R R  
 	T 6TTjTTTUUU;u56CCEE :592- D	O53IOOOPPPE23AACCC %	<===""$$$ &	=>>>##%%%IbMMMM 	QIOPPP 	OIMNNN{##T#:: 	F	-     r.   c           	         g }|dt          d |D                                }|dt          d |D                                }	t          |	|          D ]\  }
}d}| j        j        D ](}|
|j        k    s	|
|j        v r| j        |j                 })|t          j        d|
 d          |j        dz  }t          j
        |          }t          |          |k    r9t          j        d                    t          |          ||dz                      |                    |           |                     |	||||||	           dS )
a  Parse a RSA public key and burn the digest to key eFuse block.

        Args:
            blocks: List of eFuse block names to burn keys to.
            keyfiles: List of open files to read key data from.
            keypurposes: List of key purposes to burn.
            no_write_protect: If True, the write protection will NOT be enabled.
            no_read_protect: If True, the read protection will NOT be enabled.
            show_sensitive_info: If True, the sensitive information will be shown.
        r   c                     g | ]}||S rz   r{   r|   s     r,   r   z3ESP32S3Commands.burn_key_digest.<locals>.<listcomp>^  s    CCCd$2BT2B2B2Br.   c                     g | ]}||S rz   r{   )r}   r(   s     r,   r   z3ESP32S3Commands.burn_key_digest.<locals>.<listcomp>`  s    $T$T$Tu%BSUBSBSBSr.   Nr   r   r   zSIncorrect digest size {}. Digest must be {} bytes ({} bits) of raw binary key data.)rw   )rC   r#   rQ   rr   r~   r   rS   rT   r   	espsecure_digest_sbv2_public_keyr   appendr$   )r+   rr   rs   rt   ru   rv   r!   digest_listr   
block_listr   r   r   r   r   rw   s                   r,   r0   zESP32S3Commands.burn_key_digestI  s   &  CChCCCDDF
 A$T$T$T$T$T U UWX
$'
M$B$B 	' 	' JE{) 2 2))Z39-D-D K1E}()N)N)N)NOOO*I6x@@F6{{i''(..4fFY	A/ /   v&&&& 	 	
 	
 	
 	
 	
r.   )FFFN)FFF)__name__
__module____qualname__	CHIP_NAMEr   	EspEfuses	efuse_librA   Groupr;   strr8   rq   listr   boolbytesr$   r0   __classcell__)rI   s   @r,   r   r      s       I IN,EK N, N, N, N, N, N,d/! /! /! /! /!bW W W> "' %$)%)G! G!S	G! x.G! #Y	G!
 G! G! "G! Ud"G! G! G! G!\ "' %$)3
 3
S	3
 x.3
 #Y	3

 3
 3
 "3
 3
 3
 3
 3
 3
 3
 3
r.   r   )r   typingr   esptool.loggerr   
rich_clickrA   r   rS   r\   r   mem_definitionr   r	   base_operationsr
   r   r   r   r   r   r   r{   r.   r,   <module>r      s   
			                            - - - - - -                     _
 _
 _
 _
 _
l _
 _
 _
 _
 _
r.   