-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsiboimg.c
406 lines (335 loc) · 14.9 KB
/
siboimg.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
// TODO: issues with reading Volume ID on FEFS32 (is there a pointer/offset for this?)
// TODO: Reinstate and migrate more code to sibo.h
// TODO: Do older Psion-made SSDs *really* have all attributes set on the root folder?
// TODO: Investigate Psionics claim about image offsets 29-32 (dump Flash SSDs to test with)
// TODO: Handle filesystem version values at offsets 7-8 (write version) and 9-10 (min read version)
// TODO: Handle alternative records
// TODO: Use structs to read values from image files rather than separate variables
// TODO: Handle volume name records (first byte of volume name is 0x00) (need examples of this!)
// TODO: Check for out of range pointer (to trap error and avoid segfault)
#include "sibo.h"
#include "statwrap.h"
#include "argparse/argparse.h"
static const char *const usage[] = {
"siboimg [options] [[--] args]",
"siboimg [options]",
NULL,
};
static struct {
char called_with[30];
unsigned char verbose;
bool only_list;
bool ignore_modtime;
bool ignore_attributes;
} switches = {0, 0, false, false, false};
char *rtrim(char *s) {
char *p = s + strlen(s) - 1;
const char *end = p;
while (p >= s && isspace((unsigned char) *p)) {
p--;
}
if (p != end) {
p++;
*p = '\0';
}
return s;
}
int count_dirs = 0, count_files = 0;
void printlogf(const int verbosity, const char *format, ...) {
va_list args;
va_start(args, format);
if(verbosity <= switches.verbose) {
vprintf(format, args);
}
va_end(args);
}
struct tm psidateptime (const struct PsiDateTime datetime) {
struct tm tm;
tm.tm_year = (datetime.psi_date >> 9) + 80;
tm.tm_mon = ((datetime.psi_date >> 5) % 0x10) - 1;
tm.tm_mday = datetime.psi_date % 0x20;
tm.tm_hour = (datetime.psi_time >> 11);
tm.tm_min = (datetime.psi_time >> 5) % 0x40;
tm.tm_sec = (datetime.psi_time % 0x20) * 2;
return(tm);
}
struct PsiDateTime psidateftime(const struct tm tm) {
struct PsiDateTime datetime;
datetime.psi_date = 0x200 * (tm.tm_year - 80) + 0x20 * (tm.tm_mon + 1) + tm.tm_mday;
datetime.psi_time = 0x800 * tm.tm_hour + 0x20 * tm.tm_min + tm.tm_sec / 2;
return(datetime);
}
void getfile(int pos, char path[], char *buffer[], const char localpath[], const time_t unixtime, const long buffer_len, const bool is_fefs32) {
FILE *fp;
unsigned int cur_data_ptr = 0, cur_data_len = 0;
unsigned int cur_pos = pos, next_pos = 0, file_len = 0;
char file_flags;
unsigned int entry_count = 0;
struct utimbuf ubuf;
printlogf(2, "File scanning...\n");
memcpy(&file_flags, *buffer + (cur_pos + FILE_FLAGS_OFFSET), FILE_FLAGS_LENGTH);
remove(localpath);
fp = fopen(localpath, "wb");
while (1) {
entry_count++;
printlogf(2, "Entry %d:\n", entry_count);
if (entry_count == 1) {
memcpy(&cur_data_ptr, *buffer + (cur_pos + (is_fefs32 ? ENTRY_FIRSTDATARECORDPTR_OFFSET_32 : ENTRY_FIRSTDATARECORDPTR_OFFSET_24)), (is_fefs32 ? FEFS32_PTR_LEN : FEFS24_PTR_LEN));
memcpy(&cur_data_len, *buffer + (cur_pos + (is_fefs32 ? ENTRY_FIRSTDATALEN_OFFSET_32 : ENTRY_FIRSTDATALEN_OFFSET_24)), ENTRY_FIRSTDATALEN_LENGTH);
memcpy(&file_flags, *buffer + (cur_pos + (is_fefs32 ? ENTRY_FLAGS_OFFSET_32 : ENTRY_FLAGS_OFFSET_24)), ENTRY_FLAGS_LENGTH);
memcpy(&next_pos, *buffer + (cur_pos + (is_fefs32 ? ENTRY_FIRSTENTRYRECORDPTR_OFFSET_32 : ENTRY_FIRSTENTRYRECORDPTR_OFFSET_24)), (is_fefs32 ? FEFS32_PTR_LEN : FEFS24_PTR_LEN));
} else {
memcpy(&cur_data_ptr, *buffer + (cur_pos + (is_fefs32 ? FILE_DATARECORDPTR_OFFSET_32 : FILE_DATARECORDPTR_OFFSET_24)), (is_fefs32 ? FEFS32_PTR_LEN : FEFS24_PTR_LEN));
memcpy(&cur_data_len, *buffer + (cur_pos + (is_fefs32 ? FILE_DATARECORDLEN_OFFSET_32 : FILE_DATARECORDLEN_OFFSET_24)), FILE_DATARECORDLEN_LENGTH);
memcpy(&file_flags, *buffer + (cur_pos + FILE_FLAGS_OFFSET), FILE_FLAGS_LENGTH);
memcpy(&next_pos, *buffer + (cur_pos + FILE_NEXTRECORDPTR_OFFSET), (is_fefs32 ? FEFS32_PTR_LEN : FEFS24_PTR_LEN));
}
printlogf(2, "Data record: 0x%06x (%d)\n", cur_data_ptr, cur_data_ptr);
printlogf(2, "Data length: 0x%04x (%d)\n", cur_data_len, cur_data_len);
file_len += cur_data_len;
fwrite(*buffer + cur_data_ptr, 1, cur_data_len, fp);
printlogf(2, "Next entry record: 0x%06x\n", next_pos);
if (next_pos > buffer_len && next_pos != (is_fefs32 ? FEFS32_NULL_PTR : FEFS24_NULL_PTR)) {
printf("siboimg: detected pointer out of range (0x%06x)\n", next_pos);
exit(EXIT_FAILURE);
}
if (file_flags & ENTRY_FLAG_NOENTRYRECORD) printlogf(2, "Last entry record flag set.\n");
if (!((file_flags & ENTRY_FLAG_NOENTRYRECORD) == 0 && next_pos != (is_fefs32 ? FEFS32_NULL_PTR : FEFS24_NULL_PTR))) {
printlogf(2, "End of file.\n");
break;
}
cur_pos = next_pos;
}
fclose(fp);
ubuf.modtime = unixtime;
utime(localpath, &ubuf);
printlogf(1, "Total file size: %d (in %d records)\n", file_len, entry_count);
}
void walkpath(int pos, char path[], char *buffer[], const char img_name[], const long buffer_len, const bool is_fefs32) {
char entry_name[9], entry_ext[4], entry_filename[13], newpath[128];
char entry_flags, entry_properties;
unsigned int first_entry_ptr = 0, next_entry_ptr = 0;
bool is_last_entry, is_file, is_readonly, is_hidden, is_system;
char localpath[256];
struct tm tm;
time_t unixtime;
char datetime[20];
struct PsiDateTime psidatetime;
// printf("VERBOSITY: %d\n", switches.verbose);
while (true) {
memcpy(&entry_flags, *buffer + (pos + (is_fefs32 ? ENTRY_FLAGS_OFFSET_32 : ENTRY_FLAGS_OFFSET_24)), ENTRY_FLAGS_LENGTH);
memcpy(&entry_properties, *buffer + (pos + (is_fefs32 ? ENTRY_PROPERTIES_OFFSET_32 : ENTRY_PROPERTIES_OFFSET_24)), ENTRY_PROPERTIES_LENGTH);
memcpy(entry_name, *buffer + (pos + (is_fefs32 ? ENTRY_NAME_OFFSET_32 : ENTRY_NAME_OFFSET_24)), ENTRY_NAME_LENGTH);
entry_name[8] = 0;
rtrim(entry_name);
memcpy(entry_ext, *buffer + (pos + (is_fefs32 ? ENTRY_EXT_OFFSET_32 : ENTRY_EXT_OFFSET_24)), ENTRY_EXT_LENGTH);
entry_ext[3] = 0;
rtrim(entry_ext);
strcpy(entry_filename, entry_name);
if (strlen(entry_ext)) {
strcat(entry_filename, ".");
strcat(entry_filename, entry_ext);
}
is_file = (entry_flags & ENTRY_FLAG_ISFILE);
is_last_entry = (entry_flags & ENTRY_FLAG_ISLASTENTRY);
is_readonly = (entry_properties & ENTRY_PROPERTY_ISREADONLY);
is_hidden = (entry_properties & ENTRY_PROPERTY_ISHIDDEN);
is_system = (entry_properties & ENTRY_PROPERTY_ISSYSTEM);
if (entry_flags & ENTRY_FLAG_ENTRYISVALID) {
if (strlen(path)) {
printf("%s%s", path, entry_filename);
}
if (!is_file) {
printf("%s", slash);
}
if (switches.verbose > 0) {
printf(" (");
if (!strlen(path)) {
printf("root directory");
} else if (is_file) {
printf("file");
} else {
printf("directory");
}
printf(")");
}
printf("\n");
if (is_readonly) printlogf(1, "Read-only flag set.\n");
if (is_hidden) printlogf(1, "Hidden flag set.\n");
if (is_system) printlogf(1, "System flag set.\n");
if (is_fefs32) {
printlogf(2, "Entry Starts: 0x%08x\n", pos);
} else {
printlogf(2, "Entry Starts: 0x%06x\n", pos);
}
memcpy(&psidatetime, *buffer + (pos + (is_fefs32 ? ENTRY_TIMECODE_OFFSET_32 : ENTRY_TIMECODE_OFFSET_24)), ENTRY_TIMECODE_LENGTH + ENTRY_DATECODE_LENGTH);
tm = psidateptime(psidatetime);
unixtime = mktime(&tm);
strftime(datetime, sizeof(datetime), "%Y-%m-%d %H:%M:%S", &tm);
printlogf(1, "Timestamp: %s\n", datetime);
if (!(entry_flags & ENTRY_FLAG_NOALTRECORD)) {
printf("Has an alternative record.\n");
}
memcpy(&first_entry_ptr, *buffer + (pos + (is_fefs32 ? ENTRY_FIRSTENTRYRECORDPTR_OFFSET_32 : ENTRY_FIRSTENTRYRECORDPTR_OFFSET_24)), (is_fefs32 ? FEFS32_PTR_LEN : FEFS24_PTR_LEN));
if (is_fefs32) {
printlogf(2, "First Entry Pointer: 0x%08x\n", first_entry_ptr);
} else {
printlogf(2, "First Entry Pointer: 0x%06x\n", first_entry_ptr);
}
if(first_entry_ptr > buffer_len && first_entry_ptr != (is_fefs32 ? FEFS32_NULL_PTR : FEFS24_NULL_PTR)) {
printf("%s: detected pointer out of range\n", switches.called_with);
exit(EXIT_FAILURE);
}
if (is_file) {
strcpy(localpath, img_name);
strcat(localpath, path);
strcat(localpath, entry_filename);
printlogf(2, "File to be made: %s\n", localpath);
getfile(pos, path, buffer, localpath, unixtime, buffer_len, is_fefs32);
count_files++;
} else { // it's a directory
if (strlen(path)) {
strcpy(newpath, path);
strcat(newpath, entry_filename);
strcat(newpath, slash);
count_dirs++;
} else {
strcpy(newpath, slash);
}
printlogf(1, "\n");
strcpy(localpath, img_name);
strcat(localpath, newpath);
if (fsitemexists(localpath)) {
if(!direxists(localpath)) {
printf("%s: %s: item exists and isn't a folder", switches.called_with, localpath);
exit(EXIT_FAILURE);
}
} else {
#ifdef _WIN32
CreateDirectory(localpath, NULL);
#else // _WIN32
mkdir(localpath, 0777);
#endif // _WIN32
}
walkpath (first_entry_ptr, newpath, buffer, img_name, buffer_len, is_fefs32);
if (is_readonly) {
#ifdef _WIN32
SetFileAttributesA(localpath, FILE_ATTRIBUTE_READONLY);
#else // _WIN32
chmod(localpath, (is_file) ? 0444 : 0555);
#endif // _WIN32
}
#ifdef _WIN32
if (is_hidden) {
SetFileAttributesA(localpath, FILE_ATTRIBUTE_HIDDEN);
}
if (is_system) {
SetFileAttributesA(localpath, FILE_ATTRIBUTE_SYSTEM);
}
#endif // _WIN32
}
} else {
printf("Invalid entry found, skipping ");
printf((is_file) ? "file" : "directory");
printf(": %s\n", entry_filename);
}
if (is_last_entry) {
return;
}
memcpy(&pos, *buffer + (pos + ENTRY_NEXTENTRYPTR_OFFSET), (is_fefs32 ? FEFS32_PTR_LEN : FEFS24_PTR_LEN));
if (is_fefs32) {
printlogf(2, "Next Entry Pointer: 0x%08x\n", pos);
} else {
printlogf(2, "Next Entry Pointer: 0x%06x\n", pos);
}
printlogf(1, "\n");
}
}
int main(int argc, const char **argv) {
FILE *fp;
char i, img_flags;
long file_len;
char *buffer;
char img_name[12], volume_id[33];
unsigned short img_type;
unsigned int img_flashcount = 0, img_rootstart = 0;
bool only_list, ignore_attributes, ignore_modtime, is_fefs32;
strcpy(switches.called_with, argv[0]);
// switches.verbose = 0;
struct argparse_option options[] = {
OPT_HELP(),
OPT_BOOLEAN('l', "list", &switches.only_list, "only list the contents of the image, don't extract files"),
OPT_BOOLEAN('n', "no-attributes", &switches.ignore_attributes, "ignore file attributes"),
OPT_BOOLEAN('m', "no-modtime", &switches.ignore_modtime, "ignore file modification times"),
OPT_INTEGER('v', "verbose", &switches.verbose, "set verbosity level"),
OPT_END(),
};
struct argparse argparse;
argparse_init(&argparse, options, usage, 0);
argparse_describe(&argparse, "\nExtracts files from Psion SIBO Flash and ROM SSD images.", "");
argc = argparse_parse(&argparse, argc, argv);
// printf("VERBOSITY: %d\n", switches.verbose);
if (argv[0] == NULL) {
printf("%s: missing argument\n", switches.called_with);
exit(EXIT_FAILURE);
} else if (argc > 1) {
printf("%s: too many arguments\n", switches.called_with);
exit(EXIT_FAILURE);
}
if (!fileexists(argv[0])) {
printf("%s: %s: file not found\n", switches.called_with, argv[0]);
exit(EXIT_FAILURE);
}
fp = fopen(argv[0], "rb");
fread(&img_type, 2, 1, fp);
if (img_type != FLASH_TYPE) {
printf("%s: %s: Not a Psion Flash or ROM SSD image.\n", switches.called_with, argv[0]);
fclose(fp);
exit(EXIT_FAILURE);
}
// Get file size
fseek(fp, 0, SEEK_END);
file_len = ftell(fp);
rewind(fp);
buffer = (char *)malloc((file_len + 1) * sizeof(char)); // Enough memory for file + \0
fread(buffer, file_len, 1, fp);
fclose(fp);
// Check for 24-bit or 32-bit addressing (FEFS24 or FEFS32)
memcpy(&img_flags, buffer + IMAGE_POINTERSIZE_OFFSET, 1);
is_fefs32 = (img_flags && 1);
if (is_fefs32) {
printf("FEFS32 Filesystem\n");
} else {
printf("FEFS24 Filesystem\n");
}
// Fetch ROM Name
memcpy(img_name, buffer + (is_fefs32 ? IMAGE_NAME_OFFSET_32 : IMAGE_NAME_OFFSET_24), IMAGE_NAME_LENGTH);
img_name[11] = 0;
rtrim(img_name);
printf("Image name: %s\n", img_name);
printf("Length: %ld bytes\n", file_len);
// Fetch Flash Count (or identify as ROM)
memcpy(&img_flashcount, buffer + (is_fefs32 ? IMAGE_FLASHCOUNT_OFFSET_32 : IMAGE_FLASHCOUNT_OFFSET_24), IMAGE_FLASHCOUNT_LENGTH);
if (img_flashcount == IMAGE_ISROM) {
printf("ROM image.\n");
memcpy(volume_id, buffer + (is_fefs32 ? IMAGE_ROMIDSTRING_OFFSET_32 : IMAGE_ROMIDSTRING_OFFSET_24), 32);
} else {
printf("Flashed %d times.\n", img_flashcount);
memcpy(volume_id, buffer + (is_fefs32 ? IMAGE_FLASHIDSTRING_OFFSET_32 : IMAGE_FLASHIDSTRING_OFFSET_24), 32);
}
for (i = 0; i <=32; i++) {
if ((unsigned char) volume_id[i] == 0xFF) {
volume_id[i] = 0;
break;
}
}
volume_id[32] = 0;
printf("Volume ID: %s\n", volume_id);
memcpy(&img_rootstart, buffer + IMAGE_ROOTPTR_OFFSET, (is_fefs32 ? FEFS32_PTR_LEN : FEFS24_PTR_LEN));
printlogf(2, "Root directory starts at: 0x%06x\n", img_rootstart);
printf("\n");
walkpath(img_rootstart, "", &buffer, img_name, file_len, is_fefs32);
free(buffer);
printf("\nExtracted %d files in %d directories.\n", count_files, count_dirs);
return(0);
}