diff uicc/select.c @ 87:0e46bbb801e0

fc-uicc-tool: internal code in preparation for porting extended readef, savebin, restore-file and erase-file commands from fc-simtool
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 11 Apr 2021 03:52:48 +0000
parents b70d35f5476f
children db131929ee96
line wrap: on
line diff
--- a/uicc/select.c	Sun Apr 11 03:19:22 2021 +0000
+++ b/uicc/select.c	Sun Apr 11 03:52:48 2021 +0000
@@ -5,6 +5,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include "simresp.h"
+#include "efstruct.h"
 
 u_char std_aid_usim[7] = {0xA0, 0x00, 0x00, 0x00, 0x87, 0x10, 0x02};
 u_char std_aid_isim[7] = {0xA0, 0x00, 0x00, 0x00, 0x87, 0x10, 0x04};
@@ -392,3 +393,58 @@
 		*rec_count_ret = tlv[6];
 	return(0);
 }
+
+select_resp_get_ef_struct(efs)
+	struct ef_struct *efs;
+{
+	u_char *tlv;
+
+	tlv = extract_select_resp_tag(0x82);
+	if (!tlv)
+		return(-1);
+	if (tlv[1] < 2) {
+bad_file_desc:	fprintf(stderr, "error: unable to figure out file structure\n");
+		return(-1);
+	}
+	if (tlv[2] & 0x80)
+		goto bad_file_desc;
+	if ((tlv[2] & 0x38) == 0x38)
+		goto bad_file_desc;
+	efs->structure = tlv[2] & 0x07;
+	switch (efs->structure) {
+	case 0x01:
+		if (tlv[1] != 2) {
+			fprintf(stderr,
+		"error: file descriptor TLV element has wrong length\n");
+			return(-1);
+		}
+		tlv = extract_select_resp_tag(0x80);
+		if (!tlv)
+			return(-1);
+		if (tlv[1] != 2) {
+			fprintf(stderr,
+			"error: file size TLV element has wrong length\n");
+			return(-1);
+		}
+		efs->total_size = (tlv[2] << 8) | tlv[3];
+		return(0);
+	case 0x02:
+	case 0x06:
+		if (tlv[1] != 5) {
+			fprintf(stderr,
+		"error: file descriptor TLV element has wrong length\n");
+			return(-1);
+		}
+		efs->record_len = (tlv[4] << 8) | tlv[5];
+		if (efs->record_len < 1 || efs->record_len > 255) {
+			fprintf(stderr,
+			"error: SELECT response gives invalid record length\n");
+			return(-1);
+		}
+		efs->record_count = tlv[6];
+		efs->total_size = efs->record_len * efs->record_count;
+		return(0);
+	default:
+		goto bad_file_desc;
+	}
+}