FreeCalypso > hg > fc-sim-tools
comparison simtool/fplmn.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_FPLMN. | |
| 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_fplmn() | |
| 16 { | |
| 17 int rc; | |
| 18 | |
| 19 rc = select_op(DF_GSM); | |
| 20 if (rc < 0) | |
| 21 return(rc); | |
| 22 rc = select_op(EF_FPLMN); | |
| 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_FPLMN is not transparent\n"); | |
| 30 return(-1); | |
| 31 } | |
| 32 if (curfile_total_size != 12) { | |
| 33 fprintf(stderr, | |
| 34 "error: EF_FPLMN size does not equal 12 bytes\n"); | |
| 35 return(-1); | |
| 36 } | |
| 37 return(0); | |
| 38 } | |
| 39 | |
| 40 cmd_fplmn_dump(argc, argv, outf) | |
| 41 char **argv; | |
| 42 FILE *outf; | |
| 43 { | |
| 44 int rc; | |
| 45 u_char *dp; | |
| 46 char ascbuf[8]; | |
| 47 unsigned idx; | |
| 48 | |
| 49 rc = select_ef_fplmn(); | |
| 50 if (rc < 0) | |
| 51 return(rc); | |
| 52 rc = readbin_op(0, 12); | |
| 53 if (rc < 0) | |
| 54 return(rc); | |
| 55 dp = sim_resp_data; | |
| 56 for (idx = 0; idx < 4; idx++, dp += 3) { | |
| 57 if (idx) | |
| 58 putc(' ', outf); | |
| 59 if (dp[0] == 0xFF && dp[1] == 0xFF && dp[2] == 0xFF) | |
| 60 fputs("-blank-", outf); | |
| 61 else { | |
| 62 decode_plmn_3bytes(dp, ascbuf, 1); | |
| 63 fputs(ascbuf, outf); | |
| 64 } | |
| 65 } | |
| 66 putc('\n', outf); | |
| 67 return(0); | |
| 68 } | |
| 69 | |
| 70 cmd_fplmn_write(argc, argv) | |
| 71 char **argv; | |
| 72 { | |
| 73 int rc; | |
| 74 unsigned idx; | |
| 75 u_char rec[3]; | |
| 76 | |
| 77 rc = select_ef_fplmn(); | |
| 78 if (rc < 0) | |
| 79 return(rc); | |
| 80 idx = strtoul(argv[1], 0, 0); | |
| 81 if (idx >= 4) { | |
| 82 fprintf(stderr, "error: specified index is out of range\n"); | |
| 83 return(-1); | |
| 84 } | |
| 85 rc = encode_plmn_3bytes(argv[2], rec); | |
| 86 if (rc < 0) { | |
| 87 fprintf(stderr, "error: invalid MCC-MNC argument\n"); | |
| 88 return(rc); | |
| 89 } | |
| 90 return update_bin_op(idx * 3, rec, 3); | |
| 91 } | |
| 92 | |
| 93 cmd_fplmn_write_list(argc, argv) | |
| 94 char **argv; | |
| 95 { | |
| 96 int rc; | |
| 97 u_char buf[12]; | |
| 98 | |
| 99 rc = select_ef_fplmn(); | |
| 100 if (rc < 0) | |
| 101 return(rc); | |
| 102 rc = read_plmn_list_from_file(argv[1], buf, 12); | |
| 103 if (rc < 0) | |
| 104 return(rc); | |
| 105 return update_bin_op(0, buf, 12); | |
| 106 } | |
| 107 | |
| 108 cmd_fplmn_erase(argc, argv) | |
| 109 char **argv; | |
| 110 { | |
| 111 int rc; | |
| 112 unsigned idx, start, end; | |
| 113 u_char rec[3]; | |
| 114 | |
| 115 rc = select_ef_fplmn(); | |
| 116 if (rc < 0) | |
| 117 return(rc); | |
| 118 start = strtoul(argv[1], 0, 0); | |
| 119 if (start >= 4) { | |
| 120 fprintf(stderr, | |
| 121 "error: specified starting index is out of range\n"); | |
| 122 return(-1); | |
| 123 } | |
| 124 if (!argv[2]) | |
| 125 end = start; | |
| 126 else if (!strcmp(argv[2], "end")) | |
| 127 end = 3; | |
| 128 else { | |
| 129 end = strtoul(argv[1], 0, 0); | |
| 130 if (end >= 4) { | |
| 131 fprintf(stderr, | |
| 132 "error: specified ending index is out of range\n"); | |
| 133 return(-1); | |
| 134 } | |
| 135 if (start > end) { | |
| 136 fprintf(stderr, | |
| 137 "error: reverse index range specified\n"); | |
| 138 return(-1); | |
| 139 } | |
| 140 } | |
| 141 memset(rec, 0xFF, 3); | |
| 142 for (idx = start; idx <= end; idx++) { | |
| 143 rc = update_bin_op(idx * 3, rec, 3); | |
| 144 if (rc < 0) | |
| 145 return(rc); | |
| 146 } | |
| 147 return(0); | |
| 148 } | |
| 149 | |
| 150 cmd_fplmn_erase_all(argc, argv) | |
| 151 char **argv; | |
| 152 { | |
| 153 int rc; | |
| 154 u_char ffbuf[12]; | |
| 155 | |
| 156 rc = select_ef_fplmn(); | |
| 157 if (rc < 0) | |
| 158 return(rc); | |
| 159 memset(ffbuf, 0xFF, 12); | |
| 160 return update_bin_op(0, ffbuf, 12); | |
| 161 } |
