
    :)di1                         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 dd	lmZmZmZmZmZ  G d
 de          ZdS )    )BinaryION)log   )EfuseDefineBlocks   )util)	EspEfuses)BaseCommandsNonCompositeTupleTupleParameteradd_force_write_alwaysadd_show_sensitive_info_optionc            	            e Zd ZdZeZdej        f fdZd Z	de
fdZd Z	 	 dd	ee
         d
ee         dedefdZ	 	 dd
ededefdZ xZS )ESP32CommandsESP32clic                 4    t                                          |           t                                                      }|                    ddd                    |           d          t          j        ddt          dd	t          |          t          t          j        |          t          j        d
          g                    t          j        ddd          t          t          t          j         fd                                                                        }|                    ddd          t          j        dt          j        d
                    t          j        ddd          t          t          t          j         fd                                                                        }|                    dd          t          j        dt          j        g d                     fd                        }d S )Nzburn-keyzBurn a 256-bit key to EFUSE. Arguments are pairs of block name and key file, containing 256 bits of binary key data.

Block is one of: [z, ])helpblock_keyfilez<BLOCK> <KEYFILE>Trb)metavarclsrequirednargs	max_aritytypez--no-protect-keyzDisable the default read- and write-protection of the key. If this option is not set, once the key is flashed it cannot be read back or changed.)is_flagr   c                 ^    t          | \  }}| j        }                    ||||           d S N)zipshow_sensitive_infoburn_key)ctxr   no_protect_keyr#   kwargsblockkeyfileselfs          f/var/www/menimich/repos/protonApp/venv/lib/python3.11/site-packages/espefuse/efuse/esp32/operations.pyburn_key_cliz4ESP32Commands.add_cli_commands.<locals>.burn_key_cli%   s;    8 !-0NE7"%"9MM%.:MNNNNN    zburn-key-digestz+Parse a RSA public key and burn the digest.zPParse a RSA public key and burn the digest to eFuse for use with Secure Boot V2.)
short_helpr   r)   )r   zDisable the default write-protection of the key digest. If this option is not set, once the key is flashed it cannot be changed.c                 J    | j         |d<                       |||           d S )Nr#   )r#   burn_key_digest)r%   r)   r&   r#   r'   r*   s        r+   burn_key_digest_cliz;ESP32Commands.add_cli_commands.<locals>.burn_key_digest_cliE   s2    $ -0,CF()  .:MNNNNNr-   zset-flash-voltagez5Permanently set the internal flash voltage regulator.)r.   voltage)1.8V3.3VOFFc                 2                         |            dS )zPermanently set the internal flash voltage regulator to either 1.8V, 3.3V or OFF.
            This means GPIO12 can be high or low at reset without changing the flash voltage.N)set_flash_voltage)r2   r*   s    r+   set_flash_voltage_cliz=ESP32Commands.add_cli_commands.<locals>.set_flash_voltage_cliZ   s     ""7+++++r-   )superadd_cli_commandsr   get_blocks_for_keyscommandjoinclickargumentr   lenr   ChoiceFileoptionr   r   pass_context)r*   r   blocks_for_keysr,   r1   r8   	__class__s   `     r+   r:   zESP32Commands.add_cli_commands!   s~     %%%+--AACC	?!%?!;!;? ? ? 
 

 

 
'/**"EL$A$A5:dCSCS#TUU

 

 

 
1

 

 

 
 	'			O 	O 	O 	O 
	 
(	' 
 	

 



 



 

2	O 
Dc 
 

 


 
	
4(8(8	9	9	9	W

 

 

 
 	'			O 	O 	O 	O 
	 
(	' 
 	

 

 
:	9

 

	O 
N 
 

 

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

 


	, 	, 	,r-   c                     | j         d                                         }|dk    r9t          j        d| d| j         d                                                     d S t          j        d           d S )NMAC_VERSIONr   zCustom MAC Address version z: 
CUSTOM_MACz,Custom MAC Address is not set in the device.)efusesgetr   print)r*   versions     r+   get_custom_maczESP32Commands.get_custom_macf   s    +m,0022Q;;IZgZZ\9R9V9V9X9XZZ     IDEEEEEr-   r2   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XPD_SDIO_FORCEXPD_SDIO_TIEHXPD_SDIO_REGr5   r   zICan't set flash regulator to OFF as XPD_SDIO_REG eFuse is already burned.r3   zECan't set regulator to 1.8V is XPD_SDIO_TIEH eFuse is already burned.zDisable internal flash voltage regulator (VDD_SDIO). SPI flash will need to be powered from an external source.
The following eFuse is burned: XPD_SDIO_FORCE.
It is possible to later re-enable the internal regulatorzto 3.3Vzto 1.8V or 3.3Vzby burning an additional eFuse.zSet internal flash voltage regulator (VDD_SDIO) to 1.8V.
The following eFuses are burned: XPD_SDIO_FORCE, XPD_SDIO_REG.
It is possible to later increase the voltage to 3.3V (permanently) by burning additional eFuse XPD_SDIO_TIEH.r4   zEnable internal flash voltage regulator (VDD_SDIO) to 3.3V.
The following eFuses are burned: XPD_SDIO_FORCE, XPD_SDIO_REG, XPD_SDIO_TIEH.r   zVDD_SDIO setting complete.Tcheck_batch_modeSuccessful.)rJ   rK   esptool
FatalErrorr   rL   saveburn_all)r*   r2   
sdio_force	sdio_tiehsdio_regs        r+   r7   zESP32Commands.set_flash_voltageo   s   [!12
K0	;~. e! 3 3$[   fA!5!5$W   eI2 !*1 4 499:K	2 2 2    I=    I`  
 	eMM!fNN1	.///{##T#:: 	F	-     r-   c                    | j         d         }| j         d         }|                                }|dk    rt          j        d           n*t          j        d|                                 d           |                                rt          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ADC_VREFBLK3_PART_RESERVEr   z+ADC VRef calibration: None (1100mV nominal)zADC VRef calibration: mVz$ADC readings stored in eFuse BLOCK3:z    ADC1 Low reading  (150mV): ADC1_TP_LOWz    ADC1 High reading (850mV): ADC1_TP_HIGHz    ADC2 Low reading  (150mV): ADC2_TP_LOWz    ADC2 High reading (850mV): ADC2_TP_HIGH)rJ   get_rawr   rL   rK   )r*   adc_vrefblk3_reservevref_raws       r+   adc_infozESP32Commands.adc_info   st   ;z*{#67##%%q==ICDDDDIAx||~~AAABBB 	I<===IT$+m2L2P2P2R2RTT   IU$+n2M2Q2Q2S2SUU   IT$+m2L2P2P2R2RTT   IU$+n2M2Q2Q2S2SUU    	 	r-   Fr(   r)   r&   r#   c                 f   |dt          d |D                                }|dt          d |D                                }t          j        |           t          |          t          |          k    r5t          j        dt          |           dt          |           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  }|                                }|                                 d}|dv rd}|ddd         }t          j        d|	j
         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          |	                    |           |dv r*|s(t          j        d           |	                                 |s(t          j        d           |	                                 t          j        d           |rt          j        d           d}|r|dz  }n|dz  }t          j        |d           | j                            d !          sdS t          j        d"           dS )#a  Burn a 256-bit key to EFUSE. Arguments are pairs of block name and
        key file, containing 256 bits of binary key data.

        Args:
            block: List of eFuse block names to burn keys to.
            keyfile: List of open files to read key data from.
            no_protect_key: If True, the write protection will NOT be enabled.
            show_sensitive_info: If True, the sensitive information will be shown.
        r   c                     g | ]}||S r!    ).0r)   s     r+   
<listcomp>z*ESP32Commands.burn_key.<locals>.<listcomp>   s    KKKw7JW7J7J7Jr-   c                     g | ]}||S r!   rl   )rm   r(   s     r+   rn   z*ESP32Commands.burn_key.<locals>.<listcomp>   s    CCCu1BU1B1B1Br-   zThe number of blocks (z) and datafile (z) should be the same.zBurn keys to blocks:NzUnknown block name - .   )flash_encryptionsecure_boot_v1z	Reversing the byte order...r    -  end-> [{}]??zIncorrect key file size z. Key file must be  bytes ( bits) of raw binary key data.z	Disabling read to key block...z 	Disabling write to key block... z9Key is left unprotected as per --no-protect-key argument.zBurn keys in eFuse blocks.
zLThe key block will be left readable and writeable (due to --no-protect-key).zPThe key block will be read and write protected (no further changes or readback).
TrS   rU   )r@   r   check_duplicate_name_in_listrV   rW   r   rL   r"   rJ   blocksnamealiasbit_lenreadcloseformathexifyr=   rX   disable_readdisable_writerY   )r*   r(   r)   r&   r#   datafile_listblock_name_list
block_namedatafileefuseblk	num_bytesdata
revers_msgmsgs                  r+   r$   zESP32Commands.burn_key   s      KKGKKKLLN
  CCCCCDDF
 	)/:::3}#5#555$K_)=)= K K!$]!3!3K K K  
 		()))$'$G$G (	 (	 JE{) 2 2))Z39-D-D K1E}()N)N)N)NOOO*I==??DNNJCCC<
DDbDzI(EJ((c2222I  *6DKc***4&3t99"455     &	*%%%4yyI%%(Ms4yy M MY M M(1}M M M  
 JJtCCC% )I@AAA&&(((! &	=>>>##%%%IbMMMM 	SIQRRR, 		-CC
 4C 		#t{##T#:: 	F	-     r-   c           
         | j         j        | j         j        j        k    rt	          j        d          | j                                        }|dk     r.t	          j        d                    |dz  |dz                      t          j
        |          }| j         d         }|j        dz  }t          |          |k    r.t	          j        dt          |           d| d	|dz   d
          t          j        d|j         d           t          j        d                    |rt!          j        |d          n%d                    dgt          |          z                                 |                    |           |s1t          j        d|j         d           |                                 | j                             d          sdS t          j        d           dS )a@  Parse a RSA public key and burn the digest to eFuse for use with Secure Boot V2.

        Args:
            keyfile: Open file to read key data from.
            no_protect_key: If True, the write protection will NOT be enabled.
            show_sensitive_info: If True, the sensitive information will be shown.
        z4burn-key-digest only works with 'None' coding schemei,  zOIncorrect chip revision for Secure boot v2. Detected: v{}.{}. Expected: >= v3.0d   BLOCK2rq   zIncorrect digest size z. Digest must be rz   r{   rt   ru   rv   rx   ry   zDisabling write to eFuse z...TrS   NrU   )rJ   coding_schemeREGSCODING_SCHEME_34rV   rW   espget_chip_revisionr   	espsecure_digest_sbv2_public_keyr   r@   r   rL   r   r   r   r=   rX   r   rY   )r*   r)   r&   r#   chip_revisiondigestr   r   s           r+   r0   zESP32Commands.burn_key_digest  s    ;$(8(III$F   22443$66<f!S(-#*=7 7   27;;H%MQ&	v;;)##$BV B B"+B BMB B B  
 		$
$$#....	&4FC(((XXtfs6{{233 	
 	
 	
 	

6 	"IA%*AAABBB!!!{##T#:: 	F	-     r-   )FF)__name__
__module____qualname__	CHIP_NAMEr	   	efuse_libr>   Groupr:   rN   strr7   ri   listr   boolr$   r0   __classcell__)rF   s   @r+   r   r      s+       IIA,EK A, A, A, A, A, A,JF F F.! .! .! .! .!`  :  %$)Z! Z!CyZ! hZ! 	Z!
 "Z! Z! Z! Z!~  %$)	4! 4!4! 4! "	4! 4! 4! 4! 4! 4! 4! 4!r-   r   )typingr   
rich_clickr>   r   rV   esptool.loggerr   mem_definitionr   r|   r   fieldsr	   base_operationsr
   r   r   r   r   r   rl   r-   r+   <module>r      s                        - - - - - -                         m! m! m! m! m!L m! m! m! m! m!r-   