annotate simtool/saverestore.c @ 99:2e35070d289f

fc-simtool: savebin command implemented
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 25 Jan 2021 00:14:19 +0000
parents
children fa7005185b84
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
99
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * This module implements commands for saving SIM file content in UNIX host
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 * files and restoring these backups back to the SIM.
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 */
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 #include <sys/types.h>
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 #include <stdio.h>
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include <stdlib.h>
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 #include <pcsclite.h>
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 #include <winscard.h>
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 #include "globals.h"
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 static
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 savebin_transparent(outf)
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 FILE *outf;
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 {
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 unsigned off, cc;
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 int rc;
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 for (off = 0; off < curfile_total_size; off += cc) {
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 cc = curfile_total_size - off;
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 if (cc > 256)
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 cc = 256;
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 rc = readbin_op(off, cc);
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 if (rc < 0)
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 return(rc);
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 fwrite(sim_resp_data, 1, cc, outf);
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 }
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 return(0);
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 }
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 static
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 savebin_records(outf)
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 FILE *outf;
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 {
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 unsigned recno;
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 int rc;
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 for (recno = 1; recno <= curfile_record_count; recno++) {
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 rc = readrec_op(recno, 0x04, curfile_record_len);
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 if (rc < 0)
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 return(rc);
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 fwrite(sim_resp_data, curfile_record_len, 1, outf);
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 }
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 return(0);
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 }
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 cmd_savebin(argc, argv)
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 char **argv;
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 {
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 int file_id, rc;
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 unsigned readlen;
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 FILE *of;
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 if (isxdigit(argv[1][0]) && isxdigit(argv[1][1]) &&
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 isxdigit(argv[1][2]) && isxdigit(argv[1][3]) && !argv[1][4])
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 file_id = strtoul(argv[1], 0, 16);
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 else
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 file_id = find_symbolic_file_name(argv[1]);
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 if (file_id < 0) {
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61 fprintf(stderr,
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62 "error: file ID argument is not a hex value or a recognized symbolic name\n");
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 return(-1);
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 }
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 rc = select_op(file_id);
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 if (rc < 0)
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 return(rc);
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68 rc = parse_ef_select_response();
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69 if (rc < 0)
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70 return(rc);
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71 of = fopen(argv[2], "w");
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 if (!of) {
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73 perror(argv[2]);
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74 return(-1);
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 }
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76 switch (curfile_structure) {
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
77 case 0x00:
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
78 /* transparent */
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
79 rc = savebin_transparent(of);
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
80 break;
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
81 case 0x01:
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
82 case 0x03:
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
83 /* record-based */
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
84 rc = savebin_records(of);
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
85 break;
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
86 }
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
87 fclose(of);
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
88 return(rc);
2e35070d289f fc-simtool: savebin command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
89 }