FreeCalypso > hg > fc-sim-tools
annotate simtool/restorebin.c @ 63:f4eb486aab40
doc/Admin-write-commands: fix verify-hex command name
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Tue, 23 Mar 2021 23:29:31 +0000 |
parents | ddd767f6e15b |
children |
rev | line source |
---|---|
10
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
1 /* |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
2 * This module implements the restore-file command; this command |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
3 * reads binary files previously saved with the savebin command |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
4 * and writes the backed-up bits back to the SIM. |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
5 */ |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
6 |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
7 #include <sys/types.h> |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
8 #include <sys/file.h> |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
9 #include <sys/stat.h> |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
10 #include <ctype.h> |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
11 #include <stdio.h> |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
12 #include <stdlib.h> |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
13 #include <unistd.h> |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
14 #include "curfile.h" |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
15 |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
16 restore_bin_transparent(data) |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
17 u_char *data; |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
18 { |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
19 unsigned off, cc; |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
20 int rc; |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
21 |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
22 for (off = 0; off < curfile_total_size; off += cc) { |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
23 cc = curfile_total_size - off; |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
24 if (cc > 255) |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
25 cc = 255; |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
26 rc = update_bin_op(off, data + off, cc); |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
27 if (rc < 0) |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
28 return(rc); |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
29 } |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
30 return(0); |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
31 } |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
32 |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
33 restore_bin_records(data) |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
34 u_char *data; |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
35 { |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
36 unsigned recno; |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
37 u_char *dp; |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
38 int rc; |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
39 |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
40 dp = data; |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
41 for (recno = 1; recno <= curfile_record_count; recno++) { |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
42 rc = update_rec_op(recno, 0x04, dp, curfile_record_len); |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
43 if (rc < 0) |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
44 return(rc); |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
45 dp += curfile_record_len; |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
46 } |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
47 return(0); |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
48 } |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
49 |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
50 restore_bin_cyclic(data) |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
51 u_char *data; |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
52 { |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
53 unsigned count; |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
54 u_char *dp; |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
55 int rc; |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
56 |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
57 dp = data + curfile_total_size; |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
58 for (count = 0; count < curfile_record_count; count++) { |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
59 dp -= curfile_record_len; |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
60 rc = update_rec_op(0, 0x03, dp, curfile_record_len); |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
61 if (rc < 0) |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
62 return(rc); |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
63 } |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
64 return(0); |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
65 } |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
66 |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
67 cmd_restore_file(argc, argv) |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
68 char **argv; |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
69 { |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
70 int file_id, rc, fd; |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
71 struct stat st; |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
72 u_char *databuf; |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
73 |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
74 if (isxdigit(argv[1][0]) && isxdigit(argv[1][1]) && |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
75 isxdigit(argv[1][2]) && isxdigit(argv[1][3]) && !argv[1][4]) |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
76 file_id = strtoul(argv[1], 0, 16); |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
77 else |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
78 file_id = find_symbolic_file_name(argv[1]); |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
79 if (file_id < 0) { |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
80 fprintf(stderr, |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
81 "error: file ID argument is not a hex value or a recognized symbolic name\n"); |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
82 return(-1); |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
83 } |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
84 rc = select_op(file_id); |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
85 if (rc < 0) |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
86 return(rc); |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
87 rc = parse_ef_select_response(); |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
88 if (rc < 0) |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
89 return(rc); |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
90 if (!curfile_total_size) { |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
91 printf("SIM indicates file of zero length, nothing to do\n"); |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
92 return(0); |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
93 } |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
94 fd = open(argv[2], O_RDONLY); |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
95 if (fd < 0) { |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
96 perror(argv[2]); |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
97 return(-1); |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
98 } |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
99 fstat(fd, &st); |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
100 if ((st.st_mode & S_IFMT) != S_IFREG) { |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
101 fprintf(stderr, "error: %s is not a regular file\n", argv[2]); |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
102 close(fd); |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
103 return(-1); |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
104 } |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
105 if (st.st_size != curfile_total_size) { |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
106 fprintf(stderr, |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
107 "error: length of %s does not match SIM EF length of %u bytes\n", |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
108 argv[2], curfile_total_size); |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
109 close(fd); |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
110 return(-1); |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
111 } |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
112 databuf = malloc(curfile_total_size); |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
113 if (!databuf) { |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
114 perror("malloc for file data"); |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
115 close(fd); |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
116 return(-1); |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
117 } |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
118 read(fd, databuf, curfile_total_size); |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
119 close(fd); |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
120 switch (curfile_structure) { |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
121 case 0x00: |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
122 /* transparent */ |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
123 rc = restore_bin_transparent(databuf); |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
124 break; |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
125 case 0x01: |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
126 /* record-based */ |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
127 rc = restore_bin_records(databuf); |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
128 break; |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
129 case 0x03: |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
130 /* cyclic */ |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
131 rc = restore_bin_cyclic(databuf); |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
132 break; |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
133 } |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
134 free(databuf); |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
135 return(rc); |
ddd767f6e15b
fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
136 } |