-
Notifications
You must be signed in to change notification settings - Fork 0
/
s5fstool.c
427 lines (357 loc) · 9.96 KB
/
s5fstool.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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/* s5fstool - Dump TI S1500 SVR3 file systems
*
* Written by Michael Engel <engel@multicores.org>
* Modified by Jeffrey H. Johnson <trnsz@pobox.com>
*/
/*
* This is free and unencumbered software released into the public domain.
*
* Anyone is free to copy, modify, publish, use, compile, sell, or
* distribute this software, either in source code form or as a compiled
* binary, for any purpose, commercial or non-commercial, and by any
* means.
*
* In jurisdictions that recognize copyright laws, the author or authors
* of this software dedicate any and all copyright interest in the
* software to the public domain. We make this dedication for the benefit
* of the public at large and to the detriment of our heirs and
* successors. We intend this dedication to be an overt act of
* relinquishment in perpetuity of all present and future rights to this
* software under copyright law.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* For more information, please refer to <http://unlicense.org/>
*/
/*
* BUGS:
* - no handling of triple indirect blocks
* - does not set file access permissions, owner, group
* - no partition decoding implemented, offsets are hardcoded (STARTPART)
*/
#define DEBUG
#include <endian.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
#undef short_t
#define short_t int16_t
#undef ushort_t
#define ushort_t uint16_t
#undef of5_t
#ifdef off_t
# define of5_t off_t
#else
# define of5_t uint64_t
#endif /* ifdef off_t */
#undef time_t
#define time_t uint32_t
int fd;
int outfd = 2;
int filesize = 0;
#define S5IFMT 0170000 /* type of file */
#define S5IFDIR 0040000 /* directory */
#define S5IFCHR 0020000 /* character special */
#define S5IFBLK 0060000 /* block special */
#define S5IFREG 0100000 /* regular */
#define S5IFLNK 0120000 /* symbolic link */
#define S5IFSOCK 0140000 /* socket */
#define S5ISUID 04000 /* set user id on execution */
#define S5ISGID 02000 /* set group id on execution */
#define S5ISVTX 01000 /* save swapped text even after use */
#define S5IREAD 0400 /* read, write, execute permissions */
#define S5IWRITE 0200
#define S5IEXEC 0100
#pragma pack(1)
struct dinode
{
ushort_t di_mode; /* mode and type of file */
short_t di_nlink; /* number of links to file */
ushort_t di_uid; /* owner's user id */
ushort_t di_gid; /* owner's group id */
of5_t di_size; /* number of bytes in file */
char di_addr[40]; /* disk block addresses */
/*
* The last 3 chars of the disk block addresses and the extra byte
* must be divided in the following structure so that SVS C will
* correctly assign the proper bit fields for the extended mode
* flags and the NFS file generation number. The address fields
* are actually 39 characters long.
*/
time_t di_atime; /* time last accessed */
time_t di_mtime; /* time last modified */
time_t di_ctime; /* time created */
};
#ifndef DIRSIZ
# define DIRSIZ 14
#endif /* ifndef DIRSIZ */
struct dirent
{
ushort_t d_ino;
char d_name[DIRSIZ];
};
uint8_t *STARTPART[] = {
(uint8_t *)0x00000000
};
int partno = 0;
uint8_t *
ino2off(int inr)
{
uint8_t *start = (uint8_t *)( STARTPART[partno] + 0x800 ); /* first inode */
start = start + 64 * ( inr - 1 );
return start;
}
void
dumpblk(uint32_t blkno)
{
uint8_t *start = (uint8_t *)STARTPART[partno]; /* first block of fs */
start = start + 0x400 * blkno;
uint8_t buf[1024];
lseek(fd, (of5_t)start, SEEK_SET);
read(fd, buf, 1024);
write(outfd, buf, ( filesize >= 1024 ) ? 1024 : filesize);
filesize = filesize - 1024;
/*
* for (int i=0; i<1024; i++) {
* printf("%c", buf[i]);
* }
*/
printf("\n");
}
void recurse_inode(uint32_t, char *name);
void
lsdir(uint32_t blkno)
{
uint8_t *start = (uint8_t *)STARTPART[partno]; /* first block of fs */
start = start + 0x400 * blkno;
struct dirent d;
for (int i = 0; i < 1024; i = i + 16)
{
lseek(fd, (of5_t)start, SEEK_SET);
read(fd, &d, sizeof ( struct dirent ));
if (d.d_ino != 0)
{
printf(">>> %05d %s:\n", ntohs(d.d_ino), &d.d_name[0]);
if (strcmp(d.d_name, ".") && strcmp(d.d_name, ".."))
{
recurse_inode(ntohs(d.d_ino), d.d_name);
}
}
start += sizeof ( struct dirent );
}
}
void
oneind(uint32_t blkno)
{
uint8_t *start = (uint8_t *)STARTPART[partno]; /* first block of fs */
start = start + 0x400 * blkno;
uint8_t buf[1024];
lseek(fd, (of5_t)start, SEEK_SET);
read(fd, buf, 1024);
for (int i = 0; i < 256; i++)
{
uint32_t blockno = ntohl(*(uint32_t *)( buf + 4 * i ));
if (blockno != 0)
{
#ifdef DEBUG
printf("1ind blk %d\n", blockno);
#endif /* ifdef DEBUG */
dumpblk(blockno);
}
}
}
void
twoind(uint32_t blkno)
{
uint8_t *start = (uint8_t *)STARTPART[partno]; /* first block of fs */
start = start + 0x400 * blkno;
uint8_t buf[1024];
lseek(fd, (of5_t)start, SEEK_SET);
read(fd, buf, 1024);
for (int i = 0; i < 256; i++)
{
uint32_t blockno = ntohl(*(uint32_t *)( buf + 4 * i ));
if (blockno != 0)
{
#ifdef DEBUG
printf("2ind blk %d\n", blockno);
#endif /* ifdef DEBUG */
oneind(blockno);
}
}
}
/* remove invalid characters from file name */
int
validopen(char *name)
{
char buf[15]; /* 14 char max svr3 file name */
strncpy(buf, name, 14);
buf[14] = '\0';
for (int i = 0; i < 14; i++)
{
if (buf[i] == '\0')
{
break;
}
if (( buf[i] < ' ' ) || ( buf[i] > 0x7e ) || buf[i] == '/')
{
buf[i] = '.';
}
}
return open(buf, O_RDWR | O_CREAT, 0777);
}
void
recurse_inode(uint32_t inodeno, char *name)
{
struct dinode ino;
/*int inr;*/
uint8_t *off = ino2off(inodeno);
lseek(fd, (of5_t)off, SEEK_SET);
read(fd, &ino, sizeof ( struct dinode ));
printf("===============\n");
#ifdef DEBUG
printf("off: %s\n", off);
printf("mode: %x\n", ntohs(ino.di_mode));
if (( ntohs(ino.di_mode) & S5IFMT ) == S5IFDIR)
{
printf(" dir\n");
}
if (( ntohs(ino.di_mode) & S5IFMT ) == S5IFREG)
{
printf(" file\n");
}
if (( ntohs(ino.di_mode) & S5IFMT ) == S5IFCHR)
{
printf(" char\n");
}
if (( ntohs(ino.di_mode) & S5IFMT ) == S5IFBLK)
{
printf(" blk\n");
}
if (ntohs(ino.di_mode) & S5ISUID)
{
printf(" setuid\n");
}
if (ntohs(ino.di_mode) & S5ISGID)
{
printf(" setgid\n");
}
printf(" access permissions: %03o \n", ntohs(ino.di_mode) & 0777);
printf("nlnk: %d\n", ntohs(ino.di_nlink));
printf("uid: %d\n", ntohs(ino.di_uid));
printf("gid: %d\n", ntohs(ino.di_gid));
printf("size: %d\n", ntohl(ino.di_size));
printf("atim: %d\n", ntohl(ino.di_atime));
printf("mtim: %d\n", ntohl(ino.di_mtime));
printf("ctim: %d\n", ntohl(ino.di_ctime));
#endif /* ifdef DEBUG */
filesize = ntohl(ino.di_size);
if (( ntohs(ino.di_mode) & S5IFMT ) == S5IFDIR)
{
uint32_t blkno;
printf("### Dir %s:\n", name);
mkdir(name, 0777);
chdir(name);
for (int i = 0; i < 10; i++)
{
blkno = (uint8_t)ino.di_addr[3 * i] << 16;
blkno += (uint8_t)ino.di_addr[3 * i + 1] << 8;
blkno += (uint8_t)ino.di_addr[3 * i + 2];
if (blkno != 0)
{
#ifdef DEBUG
printf("Block: %06x\n", blkno);
#endif /* ifdef DEBUG */
lsdir(blkno);
}
}
chdir("..");
printf("\n");
}
else if (( ntohs(ino.di_mode) & S5IFMT ) == S5IFREG)
{
uint32_t blkno;
outfd = validopen(name);
printf("File %s\n", name);
#ifdef DEBUG
printf("Blocks:\n");
#endif /* ifdef DEBUG */
if (outfd < 0)
{
perror("create");
exit(1);
}
for (int i = 0; i < 10; i++)
{
blkno = (uint8_t)ino.di_addr[3 * i] << 16;
blkno += (uint8_t)ino.di_addr[3 * i + 1] << 8;
blkno += (uint8_t)ino.di_addr[3 * i + 2];
if (blkno != 0)
{
#ifdef DEBUG
printf("0x%06x ", blkno);
#endif /* ifdef DEBUG */
dumpblk(blkno);
}
}
printf("\n");
blkno = (uint8_t)ino.di_addr[30] << 16;
blkno += (uint8_t)ino.di_addr[31] << 8;
blkno += (uint8_t)ino.di_addr[32];
if (blkno != 0)
{
#ifdef DEBUG
printf("1x ind block: ");
printf("%06x ", blkno);
printf("\n");
#endif /* ifdef DEBUG */
oneind(blkno);
}
blkno = (uint8_t)ino.di_addr[33] << 16;
blkno += (uint8_t)ino.di_addr[34] << 8;
blkno += (uint8_t)ino.di_addr[35];
if (blkno != 0)
{
#ifdef DEBUG
printf("2x ind block: ");
printf("%06x ", blkno);
printf("\n");
#endif /* ifdef DEBUG */
twoind(blkno);
}
close(outfd);
}
}
int
main(int argc, char **argv)
{
if (argc != 4)
{
printf("s5fstool v0.0.1\n\n");
printf(" Usage: %s filename partnr inodenr\n", argv[0]);
exit(1);
}
partno = atoi(argv[2]);
/* fd = open("../s1505_cp3540.dd", O_RDONLY); */
fd = open(argv[1], O_RDONLY);
if (fd < 0)
{
perror("open");
exit(1);
}
mkdir("dump", 0777);
chdir("dump");
recurse_inode(atoi(argv[3]), "root");
}