FreeCalypso > hg > fc-sim-tools
comparison simtool/plmnsel.c @ 10:ddd767f6e15b
fc-simtool ported over
| author | Mychaela Falconia <falcon@freecalypso.org> |
|---|---|
| date | Sun, 14 Mar 2021 07:11:25 +0000 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 9:c9ef9e91dd8e | 10:ddd767f6e15b |
|---|---|
| 1 /* | |
| 2 * This module implements commands for working with EF_PLMNsel. | |
| 3 */ | |
| 4 | |
| 5 #include <sys/types.h> | |
| 6 #include <stdio.h> | |
| 7 #include <stdlib.h> | |
| 8 #include <string.h> | |
| 9 #include <strings.h> | |
| 10 #include "simresp.h" | |
| 11 #include "curfile.h" | |
| 12 #include "file_id.h" | |
| 13 | |
| 14 static | |
| 15 select_ef_plmnsel() | |
| 16 { | |
| 17 int rc; | |
| 18 | |
| 19 rc = select_op(DF_GSM); | |
| 20 if (rc < 0) | |
| 21 return(rc); | |
| 22 rc = select_op(EF_PLMNsel); | |
| 23 if (rc < 0) | |
| 24 return(rc); | |
| 25 rc = parse_ef_select_response(); | |
| 26 if (rc < 0) | |
| 27 return(rc); | |
| 28 if (curfile_structure != 0x00) { | |
| 29 fprintf(stderr, "error: EF_PLMNsel is not transparent\n"); | |
| 30 return(-1); | |
| 31 } | |
| 32 if (curfile_total_size < 24) { | |
| 33 fprintf(stderr, | |
| 34 "error: EF_PLMNsel is shorter than spec minimum of 24 bytes\n"); | |
| 35 return(-1); | |
| 36 } | |
| 37 if (curfile_total_size > 255) { | |
| 38 fprintf(stderr, | |
| 39 "error: EF_PLMNsel is longer than our 255 byte limit\n"); | |
| 40 return(-1); | |
| 41 } | |
| 42 if (curfile_total_size % 3) { | |
| 43 fprintf(stderr, | |
| 44 "error: EF_PLMNsel length is not a multiple of 3 bytes\n"); | |
| 45 return(-1); | |
| 46 } | |
| 47 return(0); | |
| 48 } | |
| 49 | |
| 50 cmd_plmnsel_dump(argc, argv, outf) | |
| 51 char **argv; | |
| 52 FILE *outf; | |
| 53 { | |
| 54 int rc, gap_flag; | |
| 55 u_char *dp, *endp; | |
| 56 char ascbuf[8]; | |
| 57 unsigned idx, linelen; | |
| 58 | |
| 59 rc = select_ef_plmnsel(); | |
| 60 if (rc < 0) | |
| 61 return(rc); | |
| 62 rc = readbin_op(0, curfile_total_size); | |
| 63 if (rc < 0) | |
| 64 return(rc); | |
| 65 dp = sim_resp_data; | |
| 66 endp = sim_resp_data + sim_resp_data_len; | |
| 67 gap_flag = 0; | |
| 68 linelen = 0; | |
| 69 for (idx = 0; dp < endp; idx++, dp += 3) { | |
| 70 if (dp[0] == 0xFF && dp[1] == 0xFF && dp[2] == 0xFF) { | |
| 71 gap_flag = 1; | |
| 72 continue; | |
| 73 } | |
| 74 if (gap_flag) { | |
| 75 if (linelen) { | |
| 76 putc('\n', outf); | |
| 77 linelen = 0; | |
| 78 } | |
| 79 fprintf(outf, "GAP, continuing at index %u:\n", idx); | |
| 80 gap_flag = 0; | |
| 81 } | |
| 82 if (linelen >= 10) { | |
| 83 putc('\n', outf); | |
| 84 linelen = 0; | |
| 85 } | |
| 86 decode_plmn_3bytes(dp, ascbuf, 1); | |
| 87 if (linelen) | |
| 88 putc(' ', outf); | |
| 89 fputs(ascbuf, outf); | |
| 90 linelen++; | |
| 91 } | |
| 92 if (linelen) | |
| 93 putc('\n', outf); | |
| 94 return(0); | |
| 95 } | |
| 96 | |
| 97 cmd_plmnsel_write(argc, argv) | |
| 98 char **argv; | |
| 99 { | |
| 100 int rc; | |
| 101 unsigned idx; | |
| 102 u_char rec[3]; | |
| 103 | |
| 104 rc = select_ef_plmnsel(); | |
| 105 if (rc < 0) | |
| 106 return(rc); | |
| 107 idx = strtoul(argv[1], 0, 0); | |
| 108 if (idx >= curfile_total_size / 3) { | |
| 109 fprintf(stderr, "error: specified index is out of range\n"); | |
| 110 return(-1); | |
| 111 } | |
| 112 rc = encode_plmn_3bytes(argv[2], rec); | |
| 113 if (rc < 0) { | |
| 114 fprintf(stderr, "error: invalid MCC-MNC argument\n"); | |
| 115 return(rc); | |
| 116 } | |
| 117 return update_bin_op(idx * 3, rec, 3); | |
| 118 } | |
| 119 | |
| 120 cmd_plmnsel_write_list(argc, argv) | |
| 121 char **argv; | |
| 122 { | |
| 123 int rc; | |
| 124 u_char buf[255]; | |
| 125 | |
| 126 rc = select_ef_plmnsel(); | |
| 127 if (rc < 0) | |
| 128 return(rc); | |
| 129 rc = read_plmn_list_from_file(argv[1], buf, curfile_total_size); | |
| 130 if (rc < 0) | |
| 131 return(rc); | |
| 132 return update_bin_op(0, buf, curfile_total_size); | |
| 133 } | |
| 134 | |
| 135 cmd_plmnsel_erase(argc, argv) | |
| 136 char **argv; | |
| 137 { | |
| 138 int rc; | |
| 139 unsigned idx, start, end, nrec; | |
| 140 u_char rec[3]; | |
| 141 | |
| 142 rc = select_ef_plmnsel(); | |
| 143 if (rc < 0) | |
| 144 return(rc); | |
| 145 nrec = curfile_total_size / 3; | |
| 146 start = strtoul(argv[1], 0, 0); | |
| 147 if (start >= nrec) { | |
| 148 fprintf(stderr, | |
| 149 "error: specified starting index is out of range\n"); | |
| 150 return(-1); | |
| 151 } | |
| 152 if (!argv[2]) | |
| 153 end = start; | |
| 154 else if (!strcmp(argv[2], "end")) | |
| 155 end = nrec - 1; | |
| 156 else { | |
| 157 end = strtoul(argv[1], 0, 0); | |
| 158 if (end >= nrec) { | |
| 159 fprintf(stderr, | |
| 160 "error: specified ending index is out of range\n"); | |
| 161 return(-1); | |
| 162 } | |
| 163 if (start > end) { | |
| 164 fprintf(stderr, | |
| 165 "error: reverse index range specified\n"); | |
| 166 return(-1); | |
| 167 } | |
| 168 } | |
| 169 memset(rec, 0xFF, 3); | |
| 170 for (idx = start; idx <= end; idx++) { | |
| 171 rc = update_bin_op(idx * 3, rec, 3); | |
| 172 if (rc < 0) | |
| 173 return(rc); | |
| 174 } | |
| 175 return(0); | |
| 176 } | |
| 177 | |
| 178 cmd_plmnsel_erase_all(argc, argv) | |
| 179 char **argv; | |
| 180 { | |
| 181 int rc; | |
| 182 u_char ffbuf[255]; | |
| 183 | |
| 184 rc = select_ef_plmnsel(); | |
| 185 if (rc < 0) | |
| 186 return(rc); | |
| 187 memset(ffbuf, 0xFF, curfile_total_size); | |
| 188 return update_bin_op(0, ffbuf, curfile_total_size); | |
| 189 } |
