
    diq4                         d dl mZ d dlZd dlZd dlmZmZ d dlmZmZm	Z	m
Z
 d dlT dededed	efd
Z ed           G d de                      ZdS )    )ListN)
CommandSetwith_default_category)b2h
auto_uint8auto_uint16	is_hexstr)*patternrepeatsizereturnc                     |s"|t          |           z
  }| | dd         |z  z   S |t          |           z  }||t          |           z  z
  }| |z  | d|         z   S )zNExpand the fill/repeat pattern as per TS 102 222 Section 6.3.2.2.2 Tags C1/C2.N)len)r   r   r   pad_lencountpart_lens         >/home/jenkins/workspace/simtester-sanitize/pySim/ts_102_222.pyexpand_patternr      so     4W%///G$%#g,,..(!333    z"TS 102 222 Administrative Commandsc                      e Zd ZdZ ej                    Ze                    ddd           e                    ded            e	j
        e          d	             Zd
ee         fdZ ej                    Ze                    ddd           e                    ded            e	j
        e          d             Zd
ee         fdZ e	j
        e          d             Zd
ee         fdZ ej                    Ze                    ddd            e	j
        e          d             Z ej                    Zej                                         e                    d          Ze                    d          Ze                    dded           e                    dded           e                    dded           e                    d deg d!d"#           e                    d$ed%           e                    d&dd'           e                    d(ed)           e                    d*ed+            e	j
        e          d,             Z ej                    Zej                                         e                    d          Z e                    d          Z!e                    d-          Z"e                     dded           e                     dded           e!                    d&dd'           e!                    d.ed/           e!                    d0ed1           e"                    d2d3           e"                    d4d3           e"                    d5d3           e"                    d6d3           e                    d*ed+            e	j
        e          d7             Z# ej                    Z$e$j                                         e$                    d          Z%e%                    dded           e$                    ded8            e	j
        e$          d9             Z&d
ee         fd:Z'd;S )<Ts102222Commandsz;Administrative commands for telecommunication applications.z--force-delete
store_truezSI really want to permanently delete the file. I know pySim cannot re-create it yet!)actionhelpNAMEzFile name or FID to delete)typer   c                     |j         s| j                            d           dS | j        j                            |j                  }| j        j        j                            |j                  \  }}dS )zDelete the specified file. DANGEROUS!  See TS 102 222 Section 6.4.
        This will permanently delete the specified file from the card.
        pySim has no support to re-create files yet, and even if it did, your card may not allow it!zCRefusing to permanently delete the file, please read the help text.N)	force_delete_cmdperrorlchanget_file_for_filenamer   sccdelete_filefidselfoptsf_data_sws        r   do_delete_filezTs102222Commands.do_delete_file/   sh    
   	IbcccFIO11$)<<y*66qu==r   r   c                     d| j         j        j                                        i}| j                             |||||          S )z+Command Line tab completion for DELETE FILE   
index_dictr!   r#   selected_fileget_selectable_namesindex_based_completer)   textlinebegidxendidxr2   s         r   complete_delete_filez%Ts102222Commands.complete_delete_file:   A    6KKMMN
y--dD&&U_-```r   z--forcezFI really want to terminate the file. I know I can not recover from it!zFile name or FIDc                     |j         s| j                            d           dS | j        j                            |j                  }| j        j        j                            |j                  \  }}dS )a  Terminate the specified DF. DANGEROUS!  See TS 102 222 6.7.
        This is a permanent, one-way operation on the card. There is no undo, you can not recover
        a terminated DF.  The only permitted command for a terminated DF is the DLETE FILE command.:Refusing to terminate the file, please read the help text.N)	forcer!   r"   r#   r$   r   r%   terminate_dfr'   r(   s        r   do_terminate_dfz Ts102222Commands.do_terminate_dfD   g    
 z 	IYZZZFIO11$)<<y*77>>r   c                     d| j         j        j                                        i}| j                             |||||          S )z,Command Line tab completion for TERMINATE DFr0   r1   r3   r7   s         r   complete_terminate_dfz&Ts102222Commands.complete_terminate_dfO   r=   r   c                     |j         s| j                            d           dS | j        j                            |j                  }| j        j        j                            |j                  \  }}dS )a  Terminate the specified EF. DANGEROUS!  See TS 102 222 6.8.
        This is a permanent, one-way operation on the card. There is no undo, you can not recover
        a terminated EF.  The only permitted command for a terminated EF is the DLETE FILE command.r?   N)	r@   r!   r"   r#   r$   r   r%   terminate_efr'   r(   s        r   do_terminate_efz Ts102222Commands.do_terminate_efT   rC   r   c                     d| j         j        j                                        i}| j                             |||||          S )z,Command Line tab completion for TERMINATE EFr0   r1   r3   r7   s         r   complete_terminate_efz&Ts102222Commands.complete_terminate_ef_   r=   r   z--force-terminate-cardzRI really want to permanently terminate the card. It will not be usable afterwards!c                     |j         s| j                            d           dS | j        j        j                                        \  }}dS )zTerminate the Card. SUPER DANGEROUS!  See TS 102 222 Section 6.9.
        This will permanently brick the card and can NOT be recovered from!zFRefusing to permanently terminate the card, please read the help text.N)force_terminate_cardr!   r"   r#   r%   terminate_card_usage)r)   r*   r,   r-   s       r   do_terminate_card_usagez(Ts102222Commands.do_terminate_card_usageh   sK     ( 	IefffFy*??AAr   zrequired argumentszoptional argumentsz--ef-arr-file-idTz.Referenced Security: File Identifier of EF.ARR)requiredr   r   z--ef-arr-record-nrz0Referenced Security: Record Number within EF.ARRz--file-sizezSize of file in octetsz--structure)transparentlinear_fixedber_tlvz!Structure of the to-be-created EF)rO   r   choicesr   z--short-file-idz+Short File Identifier as 2-digit hex stringz--shareablezShould the file be shareable?z--record-lengthzLength of each record in octetsFILE_IDz)File Identifier as 4-character hex stringc                 X   d|j         d|j        di}|j        dk    ri|j        s| j                            d           dS |j        |d<   |j        |j        z  |d<   |d         |d         z  |j        k    rt          d	          n'|j        d
k    r| j                            d           dS t          |          t          |j	                  t          d          t          |j        |j        d          t          |j                  t          |j                  g}t#          |          }| j        j        j                            t+          |                                                    \  }}| j        j                            | j        j        j                   dS )zNCreate a new EF below the currently selected DF.  Requires related privileges.file_descriptor_byte
working_ef	shareable	file_type	structurerQ   z8you must specify the --record-length for linear fixed EFN
record_len
num_of_recz/File size not evenly divisible by record lengthrR   z/BER-TLV creation not yet fully supported, sorrydecodedoperational_activatedef_arr_file_idef_arr_record_nrchildren)rY   r[   record_lengthr!   r"   	file_size
ValueErrorFileDescriptorFileIdentifierrT   LifeCycleStatusIntegerSecurityAttribReferencedrb   rc   FileSizeShortFileIdentifiershort_file_idFcpTemplater#   r%   create_filer   to_tlvselect_filer4   )r)   r*   file_descriptoriesfcpr,   r-   s          r   do_create_efzTs102222Commands.do_create_ef   s    #!^)!^% %
 >^++% 	  ![\\\,0,>OL),0Nd>P,POL)|,|/LLPTP^^^ !RSSS _^y((INOOOFo666t|8\8\8\%.EFFF'4CVEIEZ1] 1] ^ ^ ^///"4+=>>> 3'''y*66s3::<<7H7HII	##DIO$ABBBBBr   z sysmoISIM-SJA optional argumentsz--aidz0Application ID (creates an ADF, instead of a DF)z--total-file-sizez.Physical memory allocated for DF/ADi in octetsz--permit-rfm-create)r   z--permit-rfm-delete-terminatez--permit-other-applet-createz&--permit-other-applet-delete-terminatec                    d|j         dddi}g }|                    t          |                     |                    t          |j                             |j        r(|                    t          |j                             |                    t          d                     |                    t          |j	        |j
        d                     |j        r(|                    t          |j                             |j        s|j        s|j        s|j        rM|j        |j        |j        |j        d}|                    t#          t%          |          g	                     t'          |	          }| j        j        j                            t1          |                                                    \  }}| j        j                            | j        j        j                   d
S )zNCreate a new DF below the currently selected DF.  Requires related privileges.rV   dfno_info_givenrX   r^   r`   ra   )
rfm_createrfm_delete_terminateother_applet_createother_applet_delete_terminaterd   N)rY   appendri   rj   rT   aidDfNamerk   rl   rb   rc   total_file_sizeTotalFileSizepermit_rfm_createpermit_rfm_delete_terminatepermit_other_applet_create$permit_other_applet_delete_terminateProprietaryInformationToolkitAccessConditionsrp   r!   r#   r%   rq   r   rr   rs   r4   )r)   r*   rt   ru   
toolkit_acrv   r,   r-   s           r   do_create_dfzTs102222Commands.do_create_df   s    #!^!,% %
 

>/:::;;;

>$,7778888 	1JJvdh///000

)2IJJJKKK

+tGZIMI^5a 5a b b b 	c 	c 	c 	DJJ}T-ABBBCCC! 	gT%E 	gIh 	glp  mV 	g!3'+'G&*&E040Y	 J JJ-8OXb8c8c8c7deeefff3'''y*66s3::<<7H7HII	##DIO$ABBBBBr   z!Name or FID of file to be resizedc                    | j         j                            |j                  }t	          |j                  t          |j                  g}t          |          }| j         j        j	        
                    t          |                                                    \  }}| j         j                            | j         j        j                   dS )zTResize an existing EF below the currently selected DF.  Requires related privileges.r^   rd   N)r!   r#   r$   r   rj   r'   rm   rg   rp   r%   resize_filer   rr   rs   r4   )r)   r*   r+   ru   rv   r,   r-   s          r   do_resize_efzTs102222Commands.do_resize_ef   s     IO11$)<<ae,,,///13'''y*66s3::<<7H7HII	##DIO$ABBBBBr   c                     d| j         j        j                                        i}| j                             |||||          S )z)Command Line tab completion for RESIZE EFr0   r1   r3   r7   s         r   complete_resize_efz#Ts102222Commands.complete_resize_ef   r=   r   N)(__name__
__module____qualname____doc__argparseArgumentParserdelfile_parseradd_argumentstrcmd2with_argparserr.   r   r<   termdf_parserrB   rE   rH   rJ   tcard_parserrN   create_parser_action_groupspopadd_argument_groupcreate_requiredcreate_optionalr   r   r	   rw   createdf_parsercreatedf_requiredcreatedf_optionalcreatedf_sja_optionalr   resize_ef_parserresize_ef_requiredr   r    r   r   r   r   &   s4       EE,X,..N 0f   h h hS7STTTT((> > )(>a$s) a a a a
 ,H+--MyY  [ [ [vC6HIIIT''? ? ('?a49 a a a a
 T''? ? ('?a49 a a a a
 +8*,,L6|e  g g g T&&B B '&B ,H+--M $$&&&#667KLLO#667KLLO  !3d  TD   E  E  E  !5:  ]O   P  P  P  KVn ooo  CQ{Q{Q{&I ! K K K  !2Cp qqq  |Ji jjj  !2Kl mmmyy?jkkkT''C C ('C@ .h-//O"&&((('::;OPP'::;OPP+>>?abb""#53  VF"  G  G  G""#7$Z  _Q"  R  R  R""=Lk"lll""7As"ttt""#6[O"  A  A  A&&'<\&RRR&&'F|&\\\&&'El&[[[&&'OXd&eee  Al mmmT))C C *)CB /x.00#''))))<<=QRR##MD{Yq#rrr!!&s9\!]]]T)**C C +*CaS	 a a a a a ar   r   )typingr   r   r   r   r   osmocom.utilsr   r   r   r	   pySim.ts_102_221bytesboolintr   r   r   r   r   <module>r      s  (         2 2 2 2 2 2 2 2 A A A A A A A A A A A A    4E 44 4s 4u 4 4 4 4 ;<<@a @a @a @a @az @a @a =<@a @a @ar   