annotate simtool/erasefile.c @ 222:8c151bb01d28

doc/GrcardSIM2-WEKI-file: update for partial understanding and implementation
author Mychaela Falconia <falcon@freecalypso.org>
date Tue, 09 Mar 2021 03:13:41 +0000
parents f1cf569dbba3
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
193
bc127ba44488 fc-simtool erase-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 86
diff changeset
2 * This module implements the erase-file command.
1
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 */
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5 #include <sys/types.h>
16
a6ca422323b9 simtool: saverestore.c split into savebin.c and restorebin.c
Mychaela Falconia <falcon@freecalypso.org>
parents: 1
diff changeset
6 #include <ctype.h>
1
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 #include <stdio.h>
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include <stdlib.h>
193
bc127ba44488 fc-simtool erase-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 86
diff changeset
9 #include <string.h>
bc127ba44488 fc-simtool erase-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 86
diff changeset
10 #include <strings.h>
1
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 #include "curfile.h"
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12
193
bc127ba44488 fc-simtool erase-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 86
diff changeset
13 static
bc127ba44488 fc-simtool erase-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 86
diff changeset
14 erase_transparent(fill_byte)
1
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 {
193
bc127ba44488 fc-simtool erase-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 86
diff changeset
16 u_char data[255];
1
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 unsigned off, cc;
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 int rc;
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19
193
bc127ba44488 fc-simtool erase-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 86
diff changeset
20 memset(data, fill_byte, 255);
1
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 for (off = 0; off < curfile_total_size; off += cc) {
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 cc = curfile_total_size - off;
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 if (cc > 255)
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 cc = 255;
193
bc127ba44488 fc-simtool erase-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 86
diff changeset
25 rc = update_bin_op(off, data, cc);
1
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 if (rc < 0)
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 return(rc);
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 }
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 return(0);
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 }
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31
193
bc127ba44488 fc-simtool erase-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 86
diff changeset
32 static
bc127ba44488 fc-simtool erase-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 86
diff changeset
33 erase_records(fill_byte)
1
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 {
193
bc127ba44488 fc-simtool erase-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 86
diff changeset
35 u_char data[255];
1
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 unsigned recno;
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 int rc;
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38
193
bc127ba44488 fc-simtool erase-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 86
diff changeset
39 memset(data, fill_byte, curfile_record_len);
1
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 for (recno = 1; recno <= curfile_record_count; recno++) {
193
bc127ba44488 fc-simtool erase-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 86
diff changeset
41 rc = update_rec_op(recno, 0x04, data, curfile_record_len);
80
54cebab70c19 fc-simtool restore-file: add support for cyclic files
Mychaela Falconia <falcon@freecalypso.org>
parents: 16
diff changeset
42 if (rc < 0)
54cebab70c19 fc-simtool restore-file: add support for cyclic files
Mychaela Falconia <falcon@freecalypso.org>
parents: 16
diff changeset
43 return(rc);
54cebab70c19 fc-simtool restore-file: add support for cyclic files
Mychaela Falconia <falcon@freecalypso.org>
parents: 16
diff changeset
44 }
54cebab70c19 fc-simtool restore-file: add support for cyclic files
Mychaela Falconia <falcon@freecalypso.org>
parents: 16
diff changeset
45 return(0);
54cebab70c19 fc-simtool restore-file: add support for cyclic files
Mychaela Falconia <falcon@freecalypso.org>
parents: 16
diff changeset
46 }
54cebab70c19 fc-simtool restore-file: add support for cyclic files
Mychaela Falconia <falcon@freecalypso.org>
parents: 16
diff changeset
47
193
bc127ba44488 fc-simtool erase-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 86
diff changeset
48 static
bc127ba44488 fc-simtool erase-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 86
diff changeset
49 erase_cyclic(fill_byte)
bc127ba44488 fc-simtool erase-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 86
diff changeset
50 {
bc127ba44488 fc-simtool erase-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 86
diff changeset
51 u_char data[255];
bc127ba44488 fc-simtool erase-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 86
diff changeset
52 unsigned count;
bc127ba44488 fc-simtool erase-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 86
diff changeset
53 int rc;
bc127ba44488 fc-simtool erase-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 86
diff changeset
54
bc127ba44488 fc-simtool erase-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 86
diff changeset
55 memset(data, fill_byte, curfile_record_len);
bc127ba44488 fc-simtool erase-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 86
diff changeset
56 for (count = 0; count < curfile_record_count; count++) {
bc127ba44488 fc-simtool erase-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 86
diff changeset
57 rc = update_rec_op(0, 0x03, data, curfile_record_len);
bc127ba44488 fc-simtool erase-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 86
diff changeset
58 if (rc < 0)
bc127ba44488 fc-simtool erase-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 86
diff changeset
59 return(rc);
bc127ba44488 fc-simtool erase-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 86
diff changeset
60 }
bc127ba44488 fc-simtool erase-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 86
diff changeset
61 return(0);
bc127ba44488 fc-simtool erase-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 86
diff changeset
62 }
bc127ba44488 fc-simtool erase-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 86
diff changeset
63
bc127ba44488 fc-simtool erase-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 86
diff changeset
64 cmd_erase_file(argc, argv)
1
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 char **argv;
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 {
193
bc127ba44488 fc-simtool erase-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 86
diff changeset
67 int file_id, rc;
bc127ba44488 fc-simtool erase-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 86
diff changeset
68 unsigned fill_byte;
1
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70 if (isxdigit(argv[1][0]) && isxdigit(argv[1][1]) &&
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71 isxdigit(argv[1][2]) && isxdigit(argv[1][3]) && !argv[1][4])
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 file_id = strtoul(argv[1], 0, 16);
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73 else
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74 file_id = find_symbolic_file_name(argv[1]);
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 if (file_id < 0) {
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76 fprintf(stderr,
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
77 "error: file ID argument is not a hex value or a recognized symbolic name\n");
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
78 return(-1);
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
79 }
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
80 rc = select_op(file_id);
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
81 if (rc < 0)
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
82 return(rc);
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
83 rc = parse_ef_select_response();
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
84 if (rc < 0)
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
85 return(rc);
193
bc127ba44488 fc-simtool erase-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 86
diff changeset
86 if (argc > 2) {
206
f1cf569dbba3 fc-simtool erase-file & update-rec-fill: make fill byte always hex
Mychaela Falconia <falcon@freecalypso.org>
parents: 193
diff changeset
87 fill_byte = strtoul(argv[2], 0, 16);
193
bc127ba44488 fc-simtool erase-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 86
diff changeset
88 if (fill_byte > 0xFF) {
bc127ba44488 fc-simtool erase-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 86
diff changeset
89 fprintf(stderr, "error: invalid fill byte argument\n");
bc127ba44488 fc-simtool erase-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 86
diff changeset
90 return(-1);
bc127ba44488 fc-simtool erase-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 86
diff changeset
91 }
bc127ba44488 fc-simtool erase-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 86
diff changeset
92 } else
bc127ba44488 fc-simtool erase-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 86
diff changeset
93 fill_byte = 0xFF;
1
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
94 switch (curfile_structure) {
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
95 case 0x00:
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
96 /* transparent */
193
bc127ba44488 fc-simtool erase-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 86
diff changeset
97 rc = erase_transparent(fill_byte);
1
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
98 break;
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
99 case 0x01:
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
100 /* record-based */
193
bc127ba44488 fc-simtool erase-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 86
diff changeset
101 rc = erase_records(fill_byte);
1
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
102 break;
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
103 case 0x03:
80
54cebab70c19 fc-simtool restore-file: add support for cyclic files
Mychaela Falconia <falcon@freecalypso.org>
parents: 16
diff changeset
104 /* cyclic */
193
bc127ba44488 fc-simtool erase-file command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 86
diff changeset
105 rc = erase_cyclic(fill_byte);
80
54cebab70c19 fc-simtool restore-file: add support for cyclic files
Mychaela Falconia <falcon@freecalypso.org>
parents: 16
diff changeset
106 break;
1
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
107 }
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
108 return(rc);
2071b28cd0c7 simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
109 }