forked from bfranske/hfdisk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dump.c
467 lines (414 loc) · 10.7 KB
/
dump.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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
//
// dump.c - dumping partition maps
//
// Written by Eryk Vershen ([email protected])
//
/*
* Linux/m68k version by Christiaan Welvaart
* minor fixes and glibc change by Michael Schmitz
*/
/*
* Copyright 1996,1997 by Apple Computer, Inc.
* All Rights Reserved
*
* Permission to use, copy, modify, and distribute this software and
* its documentation for any purpose and without fee is hereby granted,
* provided that the above copyright notice appears in all copies and
* that both the copyright notice and this permission notice appear in
* supporting documentation.
*
* APPLE COMPUTER DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE.
*
* IN NO EVENT SHALL APPLE COMPUTER BE LIABLE FOR ANY SPECIAL, INDIRECT, OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT,
* NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
* WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include "hfdisk.h"
#include "io.h"
#include "errors.h"
#include "partition_map.h"
#include "dump.h"
//
// Defines
//
#define ONE_KILOBYTE_IN_BLOCKS (1024.0/PBLOCK_SIZE)
//
// Types
//
//
// Global Constants
//
const char * kStringEmpty = "";
const char * kStringNot = " not";
//
// Global Variables
//
//
// Forward declarations
//
void dump_block_zero(partition_map_header *map);
void dump_partition_entry(partition_map *entry, int digits, char *dev);
//
// Routines
//
void
dump(char *name)
{
partition_map_header *map;
int junk;
map = open_partition_map(name, &junk);
if (map == NULL) {
return;
}
dump_partition_map(map, 1);
close_partition_map(map);
}
void
dump_block_zero(partition_map_header *map)
{
Block0 *p;
DDMap *m;
int i;
p = map->misc;
if (p->sbSig != BLOCK0_SIGNATURE) {
return;
}
printf("\nBlock size=%u, Number of Blocks=%u\n",
p->sbBlkSize, p->sbBlkCount);
printf("DeviceType=0x%x, DeviceId=0x%x\n",
p->sbDevType, p->sbDevId);
if (p->sbDrvrCount > 0) {
printf("Drivers-\n");
m = (DDMap *) p->sbMap;
for (i = 0; i < p->sbDrvrCount; i++) {
printf("%u: @ %u for %u, type=0x%x\n", i+1, m[i].ddBlock,
m[i].ddSize, m[i].ddType);
}
}
printf("\n");
}
void
dump_partition_map(partition_map_header *map, int disk_order)
{
partition_map * entry;
int j;
size_t len;
char *buf;
if (map == NULL) {
bad_input("No partition map exists");
return;
}
printf("%s\n", map->name);
j = number_of_digits(map->media_size);
if (j < 7) {
j = 7;
}
// convert size_t to int for "*" format specifier
int mapNameWidth = strlen(map->name)+1;
printf("%*s type name "
"%*s %-*s ( size ) system\n", mapNameWidth, "#", j, "length", j, "base");
/* Grok devfs names. (courtesy Colin Walters)*/
len = strlen(map->name);
buf = strdup(map->name);
if (len >= 4 && !strcmp(buf+len-4, "disc")) {
strcpy(buf+len-4, "part");
}
if (disk_order) {
for (entry = map->disk_order; entry != NULL;
entry = entry->next_on_disk) {
dump_partition_entry(entry, j, buf);
}
} else {
for (entry = map->base_order; entry != NULL;
entry = entry->next_by_base) {
dump_partition_entry(entry, j, buf);
}
}
dump_block_zero(map);
}
void
dump_partition_entry(partition_map *entry, int digits, char *dev)
{
int j;
DPME *p;
char *s;
int aflag = 0;
int pflag = 1;
uint32_t size;
double bytes;
p = entry->data;
if (aflag) {
s = "????";
for (j = 0; plist[j].abbr != 0; j++) {
if (strcmp(p->dpme_type, plist[j].full) == 0) {
s = plist[j].abbr;
break;
}
}
printf("%s%-4ld %.4s %-18.32s ", dev, entry->disk_address, s, p->dpme_name);
} else {
printf("%s%-4ld %20.32s %-18.32s ", dev,
entry->disk_address, p->dpme_type, p->dpme_name);
}
if (pflag) {
printf("%*u ", digits, p->dpme_pblocks);
size = p->dpme_pblocks;
} else if (p->dpme_lblocks + p->dpme_lblock_start != p->dpme_pblocks) {
printf("%*u+", digits, p->dpme_lblocks);
size = p->dpme_lblocks;
} else if (p->dpme_lblock_start != 0) {
printf("%*u ", digits, p->dpme_lblocks);
size = p->dpme_lblocks;
} else {
printf("%*u ", digits, p->dpme_pblocks);
size = p->dpme_pblocks;
}
if (pflag || p->dpme_lblock_start == 0) {
printf("@ %-*u", digits, p->dpme_pblock_start);
} else {
printf("@~%-*u", digits, p->dpme_pblock_start + p->dpme_lblock_start);
}
j = 's';
bytes = size / ONE_KILOBYTE_IN_BLOCKS;
j = 'k';
if (bytes >= 1024.0) {
bytes = bytes / 1024.0;
if (bytes < 1024.0) {
j = 'M';
} else {
bytes = bytes / 1024.0;
j = 'G';
}
}
printf(" (%#5.1f%c) ", bytes, j);
if (!strcmp(p->dpme_type, "Apple_UNIX_SVR2"))
{
if (!strcmp(p->dpme_name, "Swap") || !strcmp(p->dpme_name, "swap"))
printf("Linux swap");
else
printf("Linux native");
}
else
if (!strcmp(p->dpme_type, "Apple_Bootstrap"))
printf("NewWorld bootblock");
else
if (!strcmp(p->dpme_type, "Apple_Scratch"))
printf("Linux swap"); //not just linux, but who cares
else
if (!strcmp(p->dpme_type, "Apple_HFS"))
printf("HFS");
else
if (!strcmp(p->dpme_type, "Apple_MFS"))
printf("MFS");
else
if (!strcmp(p->dpme_type, "Apple_Driver"))
printf("Driver");
else
if (!strcmp(p->dpme_type, "Apple_Driver43"))
printf("Driver 4.3");
else
if (!strcmp(p->dpme_type, "Apple_partition_map"))
printf("Partition map");
else
if (!strcmp(p->dpme_type, "Apple_PRODOS"))
printf("ProDOS");
else
if (!strcmp(p->dpme_type, "Apple_Free"))
printf("Free space");
else
printf("Unknown");
printf("\n");
}
void
list_all_disks()
{
char name[20];
int i;
int fd;
DPME * data;
data = (DPME *) malloc(PBLOCK_SIZE);
if (data == NULL) {
error(errno, "can't allocate memory for try buffer");
return;
}
for (i = 0; i < 7; i++) {
sprintf(name, "/dev/sd%c", 'a'+i);
if ((fd = open_device(name, O_RDONLY)) < 0) {
if (errno == EACCES) {
error(errno, "can't open file '%s'", name);
}
continue;
}
if (read_block(fd, 1, (char *)data, 1) == 0) {
close_device(fd);
continue;
}
close_device(fd);
dump(name);
}
for (i = 0; i < 4; i++) {
sprintf(name, "/dev/hd%c", 'a'+i);
if ((fd = open_device(name, O_RDONLY)) < 0) {
if (errno == EACCES) {
error(errno, "can't open file '%s'", name);
}
continue;
}
if (read_block(fd, 1, (char *)data, 1) == 0) {
close_device(fd);
continue;
}
close_device(fd);
dump(name);
}
free(data);
}
void
show_data_structures(partition_map_header *map)
{
Block0 *zp;
DDMap *m;
int i;
int j;
partition_map * entry;
DPME *p;
BZB *bp;
char *s;
if (map == NULL) {
printf("No partition map exists\n");
return;
}
printf("Header:\n");
printf("fd=%d (%s)\n", map->fd, (map->regular_file)?"file":"device");
printf("map %d blocks out of %d, media %u blocks\n",
map->blocks_in_map, map->maximum_in_map, map->media_size);
printf("Map is%s writeable", (map->writeable)?kStringEmpty:kStringNot);
printf(", but%s changed\n", (map->changed)?kStringEmpty:kStringNot);
printf("\n");
if (map->misc == NULL) {
printf("No block zero\n");
} else {
zp = map->misc;
printf("Block0:\n");
printf("signature 0x%x", zp->sbSig);
if (zp->sbSig == BLOCK0_SIGNATURE) {
printf("\n");
} else {
printf(" should be 0x%x\n", BLOCK0_SIGNATURE);
}
printf("Block size=%u, Number of Blocks=%u\n",
zp->sbBlkSize, zp->sbBlkCount);
printf("DeviceType=0x%x, DeviceId=0x%x, sbData=0x%x\n",
zp->sbDevType, zp->sbDevId, zp->sbData);
if (zp->sbDrvrCount == 0) {
printf("No drivers\n");
} else {
printf("%u driver%s-\n", zp->sbDrvrCount,
(zp->sbDrvrCount>1)?"s":kStringEmpty);
m = (DDMap *) zp->sbMap;
for (i = 0; i < zp->sbDrvrCount; i++) {
printf("%u: @ %u for %u, type=0x%x\n", i+1, m[i].ddBlock,
m[i].ddSize, m[i].ddType);
}
}
}
printf("\n");
/*
uint32_t dpme_boot_args[32] ;
uint32_t dpme_reserved_3[62] ;
*/
printf(" #: type length base "
"flags (logical)\n");
for (entry = map->disk_order; entry != NULL; entry = entry->next_on_disk) {
p = entry->data;
printf("%2ld: %20.32s ",
entry->disk_address, p->dpme_type);
printf("%7u @ %-7u ", p->dpme_pblocks, p->dpme_pblock_start);
printf("%c%c%c%c%c%c%c%c%c%c ",
(dpme_valid_get(p))?'V':'v',
(dpme_allocated_get(p))?'A':'a',
(dpme_in_use_get(p))?'I':'i',
(dpme_bootable_get(p))?'B':'b',
(dpme_readable_get(p))?'R':'r',
(dpme_writable_get(p))?'W':'w',
(dpme_os_pic_code_get(p))?'P':'p',
(dpme_os_specific_1_get(p))?'1':'.',
(dpme_os_specific_2_get(p))?'2':'.',
(dpme_automount_get(p))?'M':'m');
if (p->dpme_lblock_start != 0 || p->dpme_pblocks != p->dpme_lblocks) {
printf("(%u @ %u)", p->dpme_lblocks, p->dpme_lblock_start);
}
printf("\n");
}
printf("\n");
printf(" #: booter bytes load_address "
"goto_address checksum processor\n");
for (entry = map->disk_order; entry != NULL; entry = entry->next_on_disk) {
p = entry->data;
printf("%2ld: ", entry->disk_address);
printf("%7u ", p->dpme_boot_block);
printf("%7u ", p->dpme_boot_bytes);
printf("%8x ", p->dpme_load_addr);
printf("%8x ", p->dpme_load_addr_2);
printf("%8x ", p->dpme_goto_addr);
printf("%8x ", p->dpme_goto_addr_2);
printf("%8x ", p->dpme_checksum);
printf("%.32s", p->dpme_process_id);
printf("\n");
}
printf("\n");
/*
xx: cccc RU *dd s...
*/
for (entry = map->disk_order; entry != NULL; entry = entry->next_on_disk) {
p = entry->data;
printf("%2ld: ", entry->disk_address);
bp = (BZB *) (p->dpme_bzb);
j = -1;
if (bp->bzb_magic == BZBMAGIC) {
switch (bp->bzb_type) {
case FSTEFS:
s = "esch";
break;
case FSTSFS:
s = "swap";
j = 1;
break;
case FST:
default:
s = "fsys";
if (bzb_root_get(bp) != 0) {
j = 0;
} else if (bzb_usr_get(bp) != 0) {
j = 2;
}
break;
}
printf("%4s ", s);
printf("%c%c ",
(bzb_root_get(bp))?'R':' ',
(bzb_usr_get(bp))?'U':' ');
if (bzb_slice_get(bp) != 0) {
printf(" %2d", bzb_slice_get(bp)-1);
} else if (j >= 0) {
printf(" *%2d", j);
} else {
printf(" ");
}
if (bp->bzb_mount_point[0] != 0) {
printf(" %.64s", bp->bzb_mount_point);
}
}
printf("\n");
}
}