comparison simtool/readcmd.c @ 92:0ead9444a698

fc-simtool: read operation functions factored out
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 24 Jan 2021 19:59:10 +0000
parents 226b231d00f3
children 5f826e428641
comparison
equal deleted inserted replaced
91:226b231d00f3 92:0ead9444a698
7 7
8 cmd_readbin(argc, argv) 8 cmd_readbin(argc, argv)
9 char **argv; 9 char **argv;
10 { 10 {
11 unsigned offset, len; 11 unsigned offset, len;
12 u_char cmd[5];
13 int rc; 12 int rc;
14 13
15 offset = strtoul(argv[1], 0, 0); 14 offset = strtoul(argv[1], 0, 0);
16 if (offset > 0xFFFF) { 15 if (offset > 0xFFFF) {
17 fprintf(stderr, "error: offset argument is out of range\n"); 16 fprintf(stderr, "error: offset argument is out of range\n");
20 len = strtoul(argv[2], 0, 0); 19 len = strtoul(argv[2], 0, 0);
21 if (len < 1 || len > 256) { 20 if (len < 1 || len > 256) {
22 fprintf(stderr, "error: length argument is out of range\n"); 21 fprintf(stderr, "error: length argument is out of range\n");
23 return(-1); 22 return(-1);
24 } 23 }
25 /* READ BINARY command APDU */ 24 rc = readbin_op(offset, len);
26 cmd[0] = 0xA0;
27 cmd[1] = 0xB0;
28 cmd[2] = offset >> 8;
29 cmd[3] = offset;
30 cmd[4] = len;
31 rc = apdu_exchange(cmd, 5);
32 if (rc < 0) 25 if (rc < 0)
33 return(rc); 26 return(rc);
34 if (sim_resp_sw != 0x9000) {
35 fprintf(stderr, "bad SW response to READ BINARY: %04X\n",
36 sim_resp_sw);
37 return(-1);
38 }
39 if (sim_resp_data_len != len) {
40 fprintf(stderr,
41 "error: READ BINARY returned %u bytes, expected %u\n",
42 sim_resp_data_len, len);
43 return(-1);
44 }
45 display_sim_resp_in_hex(); 27 display_sim_resp_in_hex();
46 return(0); 28 return(0);
47 } 29 }
48 30
49 cmd_readrec(argc, argv) 31 cmd_readrec(argc, argv)
50 char **argv; 32 char **argv;
51 { 33 {
52 unsigned recno, len; 34 unsigned recno, len;
53 u_char cmd[5];
54 int rc; 35 int rc;
55 36
56 recno = strtoul(argv[1], 0, 0); 37 recno = strtoul(argv[1], 0, 0);
57 if (recno < 1 || recno > 255) { 38 if (recno < 1 || recno > 255) {
58 fprintf(stderr, 39 fprintf(stderr,
72 "error: no current file record length is available\n"); 53 "error: no current file record length is available\n");
73 return(-1); 54 return(-1);
74 } 55 }
75 len = curfile_record_len; 56 len = curfile_record_len;
76 } 57 }
77 /* READ RECORD command APDU */ 58 rc = readrec_op(recno, 0x04, len);
78 cmd[0] = 0xA0;
79 cmd[1] = 0xB2;
80 cmd[2] = recno;
81 cmd[3] = 0x04;
82 cmd[4] = len;
83 rc = apdu_exchange(cmd, 5);
84 if (rc < 0) 59 if (rc < 0)
85 return(rc); 60 return(rc);
86 if (sim_resp_sw != 0x9000) {
87 fprintf(stderr, "bad SW response to READ RECORD: %04X\n",
88 sim_resp_sw);
89 return(-1);
90 }
91 if (sim_resp_data_len != len) {
92 fprintf(stderr,
93 "error: READ RECORD returned %u bytes, expected %u\n",
94 sim_resp_data_len, len);
95 return(-1);
96 }
97 display_sim_resp_in_hex(); 61 display_sim_resp_in_hex();
98 return(0); 62 return(0);
99 } 63 }