FreeCalypso > hg > fc-sim-tools
comparison simtool/restorebin.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 the restore-file command; this command | |
| 3 * reads binary files previously saved with the savebin command | |
| 4 * and writes the backed-up bits back to the SIM. | |
| 5 */ | |
| 6 | |
| 7 #include <sys/types.h> | |
| 8 #include <sys/file.h> | |
| 9 #include <sys/stat.h> | |
| 10 #include <ctype.h> | |
| 11 #include <stdio.h> | |
| 12 #include <stdlib.h> | |
| 13 #include <unistd.h> | |
| 14 #include "curfile.h" | |
| 15 | |
| 16 restore_bin_transparent(data) | |
| 17 u_char *data; | |
| 18 { | |
| 19 unsigned off, cc; | |
| 20 int rc; | |
| 21 | |
| 22 for (off = 0; off < curfile_total_size; off += cc) { | |
| 23 cc = curfile_total_size - off; | |
| 24 if (cc > 255) | |
| 25 cc = 255; | |
| 26 rc = update_bin_op(off, data + off, cc); | |
| 27 if (rc < 0) | |
| 28 return(rc); | |
| 29 } | |
| 30 return(0); | |
| 31 } | |
| 32 | |
| 33 restore_bin_records(data) | |
| 34 u_char *data; | |
| 35 { | |
| 36 unsigned recno; | |
| 37 u_char *dp; | |
| 38 int rc; | |
| 39 | |
| 40 dp = data; | |
| 41 for (recno = 1; recno <= curfile_record_count; recno++) { | |
| 42 rc = update_rec_op(recno, 0x04, dp, curfile_record_len); | |
| 43 if (rc < 0) | |
| 44 return(rc); | |
| 45 dp += curfile_record_len; | |
| 46 } | |
| 47 return(0); | |
| 48 } | |
| 49 | |
| 50 restore_bin_cyclic(data) | |
| 51 u_char *data; | |
| 52 { | |
| 53 unsigned count; | |
| 54 u_char *dp; | |
| 55 int rc; | |
| 56 | |
| 57 dp = data + curfile_total_size; | |
| 58 for (count = 0; count < curfile_record_count; count++) { | |
| 59 dp -= curfile_record_len; | |
| 60 rc = update_rec_op(0, 0x03, dp, curfile_record_len); | |
| 61 if (rc < 0) | |
| 62 return(rc); | |
| 63 } | |
| 64 return(0); | |
| 65 } | |
| 66 | |
| 67 cmd_restore_file(argc, argv) | |
| 68 char **argv; | |
| 69 { | |
| 70 int file_id, rc, fd; | |
| 71 struct stat st; | |
| 72 u_char *databuf; | |
| 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 = parse_ef_select_response(); | |
| 88 if (rc < 0) | |
| 89 return(rc); | |
| 90 if (!curfile_total_size) { | |
| 91 printf("SIM indicates file of zero length, nothing to do\n"); | |
| 92 return(0); | |
| 93 } | |
| 94 fd = open(argv[2], O_RDONLY); | |
| 95 if (fd < 0) { | |
| 96 perror(argv[2]); | |
| 97 return(-1); | |
| 98 } | |
| 99 fstat(fd, &st); | |
| 100 if ((st.st_mode & S_IFMT) != S_IFREG) { | |
| 101 fprintf(stderr, "error: %s is not a regular file\n", argv[2]); | |
| 102 close(fd); | |
| 103 return(-1); | |
| 104 } | |
| 105 if (st.st_size != curfile_total_size) { | |
| 106 fprintf(stderr, | |
| 107 "error: length of %s does not match SIM EF length of %u bytes\n", | |
| 108 argv[2], curfile_total_size); | |
| 109 close(fd); | |
| 110 return(-1); | |
| 111 } | |
| 112 databuf = malloc(curfile_total_size); | |
| 113 if (!databuf) { | |
| 114 perror("malloc for file data"); | |
| 115 close(fd); | |
| 116 return(-1); | |
| 117 } | |
| 118 read(fd, databuf, curfile_total_size); | |
| 119 close(fd); | |
| 120 switch (curfile_structure) { | |
| 121 case 0x00: | |
| 122 /* transparent */ | |
| 123 rc = restore_bin_transparent(databuf); | |
| 124 break; | |
| 125 case 0x01: | |
| 126 /* record-based */ | |
| 127 rc = restore_bin_records(databuf); | |
| 128 break; | |
| 129 case 0x03: | |
| 130 /* cyclic */ | |
| 131 rc = restore_bin_cyclic(databuf); | |
| 132 break; | |
| 133 } | |
| 134 free(databuf); | |
| 135 return(rc); | |
| 136 } |
