comparison uicc/erasefile.c @ 92:5560261fc516

fc-uicc-tool: erase-file ported over from fc-simtool
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 11 Apr 2021 04:55:02 +0000
parents simtool/erasefile.c@ddd767f6e15b
children
comparison
equal deleted inserted replaced
91:abef3d5668b9 92:5560261fc516
1 /*
2 * This module implements the erase-file command.
3 */
4
5 #include <sys/types.h>
6 #include <ctype.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <strings.h>
11 #include "efstruct.h"
12
13 static
14 erase_transparent(efs, fill_byte)
15 struct ef_struct *efs;
16 {
17 u_char data[255];
18 unsigned off, cc;
19 int rc;
20
21 memset(data, fill_byte, 255);
22 for (off = 0; off < efs->total_size; off += cc) {
23 cc = efs->total_size - off;
24 if (cc > 255)
25 cc = 255;
26 rc = update_bin_op(off, data, cc);
27 if (rc < 0)
28 return(rc);
29 }
30 return(0);
31 }
32
33 static
34 erase_records(efs, fill_byte)
35 struct ef_struct *efs;
36 {
37 u_char data[255];
38 unsigned recno;
39 int rc;
40
41 memset(data, fill_byte, efs->record_len);
42 for (recno = 1; recno <= efs->record_count; recno++) {
43 rc = update_rec_op(recno, 0x04, data, efs->record_len);
44 if (rc < 0)
45 return(rc);
46 }
47 return(0);
48 }
49
50 static
51 erase_cyclic(efs, fill_byte)
52 struct ef_struct *efs;
53 {
54 u_char data[255];
55 unsigned count;
56 int rc;
57
58 memset(data, fill_byte, efs->record_len);
59 for (count = 0; count < efs->record_count; count++) {
60 rc = update_rec_op(0, 0x03, data, efs->record_len);
61 if (rc < 0)
62 return(rc);
63 }
64 return(0);
65 }
66
67 cmd_erase_file(argc, argv)
68 char **argv;
69 {
70 int file_id, rc;
71 struct ef_struct efs;
72 unsigned fill_byte;
73
74 if (isxdigit(argv[1][0]) && isxdigit(argv[1][1]) &&
75 isxdigit(argv[1][2]) && isxdigit(argv[1][3]) && !argv[1][4])
76 file_id = strtoul(argv[1], 0, 16);
77 else
78 file_id = find_symbolic_file_name(argv[1]);
79 if (file_id < 0) {
80 fprintf(stderr,
81 "error: file ID argument is not a hex value or a recognized symbolic name\n");
82 return(-1);
83 }
84 rc = select_op(file_id);
85 if (rc < 0)
86 return(rc);
87 rc = select_resp_get_ef_struct(&efs);
88 if (rc < 0)
89 return(rc);
90 if (argc > 2) {
91 fill_byte = strtoul(argv[2], 0, 16);
92 if (fill_byte > 0xFF) {
93 fprintf(stderr, "error: invalid fill byte argument\n");
94 return(-1);
95 }
96 } else
97 fill_byte = 0xFF;
98 switch (efs.structure) {
99 case 0x01:
100 /* transparent */
101 rc = erase_transparent(&efs, fill_byte);
102 break;
103 case 0x02:
104 /* record-based */
105 rc = erase_records(&efs, fill_byte);
106 break;
107 case 0x06:
108 /* cyclic */
109 rc = erase_cyclic(&efs, fill_byte);
110 break;
111 }
112 return(rc);
113 }