ISO/IEC 7816-4
Identification cards — Integrated circuit cards — Part 4: Organization, security and commands for interchange
Cartes d’identification — Cartes à circuit intégré — Partie 4: Organisation, sécurité et commandes pour les échanges

Data unit handling
Data units

Within each EF supporting data units, an offset shall reference each data unit. From zero for the first data unit of the EF, the offset is incremented by one for each subsequent data unit. The offset data element is binary encoded on the minimum number of bytes. Reference to a data unit not contained in the EF is an error. The card can provide a data coding byte (see Table 87) in the historical bytes (see 8.1.1), in EF.ATR (see 8.2.1.1) and in the control information of any file (see tag ’82′ in Table 12). The data coding byte fixes a data unit size.
–If the card provides data coding bytes in several places, then the data coding byte valid for a given EF is in the closest position to that EF within the path from the MF to that EF.
–In the absence of indication within the path, the data unit size is one byte (default value) for that EF.

Any command of this group shall be aborted if applied to an EF not supporting data units. It can be performed on an EF only if the security status satisfies the security attributes defined for the function, namely, read, write, update, erase or search. Each command of this group may use either a short EF identifier or a file identifier. If there is a current EF at the time of issuing the command, then the process may be completed on that EF by just setting all the corresponding bits to 0. If the process is completed, then the identified EF becomes current.

INS P1 P2——All the commands of this group shall use bit 1 of INS and bit 8 of P1 as follows.
–If bit 1 of INS is set to 0 and bit 8 of P1 to 1, then bits 7 and 6 of P1 are set to 00 (RFU), bits 5 to 1 of P1 encode a short EF identifier and P2 (eight bits) encodes an offset from zero to 255.
–If bit 1 of INS is set to 0 and bit 8 of P1 to 0, then P1-P2 (fifteen bits) encodes an offset from zero to 32 767.
–If bit 1 of INS is set to 1, then P1-P2 shall identify an EF. If the first eleven bits of P1-P2 are set to 0 and if bits 5 to 1 of P2 are not all equal and if the card and / or the EF supports selection by short EF identifier, then bits 5 to 1 of P2 encode a short EF identifier (a number from one to thirty). Otherwise, P1-P2 is a file identifier. P1-P2 set to ’0000′ identifies the current EF. At least one offset data object with tag ’54′ shall be present in the command data field. When present in a command or response data field, data shall be encapsulated in a discretionary data object with tag ’53′ or ’73′.
In this group of commands, SW1-SW2 set to ’63CX’ indicates a successful change of memory state, but after an internal retry routine; ‘X’ > ’0′ encodes the number of retries; ‘X’ = ’0′ means that no counter is provided.

READ BINARY command
The response data field gives [part of] the content of an EF supporting data units.
If the Le field contains only bytes set to ’00′, then all the bytes until the end of the file should be read within the limit of 256 for a short Le field, or 65 536 for an extended Le field.

Table 42 READ BINARYcommand-response pair
CLA INS P1-P2 As defined in 5.1.1 ‘B0′ or ‘B1′ See 7.2.2
Lcfield Absent for encoding Nc= 0, present for encoding Nc> 0
Data field Absent (INS = ‘B0′), or offset data object (INS = ‘B1′)
Lefield Present for encoding Ne> 0
Data field Data read (INS = ‘B0′), or discretionary data object for encapsulating data read (INS = ‘B1′)
SW1-SW2 See Tables 5 and 6 when relevant, e.g., ’6281′, ’6282′, ’6700′, ’6981′, ’6982′, ’6986′, ’6A81′, ’6A82′, ’6B00′, ’6CXX’

WRITE BINARY command
The command initiates one of the following operations into an EF according to the file attributes:
–the write-once of the bits given in the command data field (the command shall be aborted if the string of data units is not in the logical erased state);
–the logical-OR of the bits already present in the card with the bits given in the command data field (the logical erased state of the bits of the file is zero);
–the logical-AND of the bits already present in the card with the bits given in the command data field (the logical erased state of the bits of the file is one).
By default, i.e., when the data coding byte (see Table 87) is absent in the historical bytes (see 8.1.1), in EF.ATR (see 8.2.1.1) and in the control parameters (see tag ’82′ in Table 12) of every file within the path from the MF to a given EF, the logical-OR shall apply for that EF.

Table 43 WRITE BINARYcommand-response pair
CLA INS P1-P2 As defined in 5.1.1 ‘D0′ or ‘D1′ See 7.2.2
Lcfield Present for encoding Nc> 0
Data field String of data units to be written (INS = ‘D0′), or offset data object and discretionary data object for encapsulating the string of data units to be written (INS = ‘D1′)
Lefield Absent for encoding Ne= 0
Data field Absent
SW1-SW2 See Tables 5 and 6 when relevant, e.g., ’63CX’ (see 7.2.2), ’6581′, ’6700′, ’6981′, ’6982′, ’6B00′ (offset outside the EF)