summaryrefslogtreecommitdiff
path: root/sbin/pdisk/dump.c
blob: a288d81b6d8580b644c51061541f94c619fdddb6 (plain)
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
/*	$OpenBSD: dump.c,v 1.75 2016/02/23 02:39:54 krw Exp $	*/

/*
 * dump.c - dumping partition maps
 *
 * Written by Eryk Vershen
 */

/*
 * Copyright 1996,1997,1998 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 <sys/queue.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <util.h>

#include "partition_map.h"
#include "dump.h"
#include "io.h"

void	dump_block(unsigned char *, int);
void	dump_block_zero(struct partition_map *);
void	dump_partition_entry(struct entry *, int, int, int);
int	get_max_base_or_length(struct partition_map *);
int	get_max_name_string_length(struct partition_map *);
int	get_max_type_string_length(struct partition_map *);

void
dump_block_zero(struct partition_map *map)
{
	char buf[FMT_SCALED_STRSIZE];
	struct ddmap  *m;
	int i;

	printf("\nDevice block size=%u, Number of Blocks=%u",
	       map->sbBlkSize, map->sbBlkCount);
	if (fmt_scaled((long long)map->sbBlkCount * map->sbBlkSize, buf) == 0)
		printf(" (%s)\n", buf);
	else
		printf("\n");

	printf("DeviceType=0x%x, DeviceId=0x%x\n", map->sbDevType,
	    map->sbDevId);
	if (map->sbDrvrCount > 0) {
		printf("Drivers-\n");
		m = map->sbDDMap;
		for (i = 0; i < map->sbDrvrCount; i++) {
			printf("%d: %3u @ %u, ", i + 1, m[i].ddSize,
			    m[i].ddBlock);
			printf("type=0x%x\n", m[i].ddType);
		}
	}
	printf("\n");
}


void
dump_partition_map(struct partition_map *map)
{
	struct entry *entry;
	int digits, max_type_length, max_name_length;

	printf("\nPartition map (with %d byte blocks) on '%s'\n",
	       map->sbBlkSize, map->name);

	digits = number_of_digits(get_max_base_or_length(map));
	if (digits < 6)
		digits = 6;
	max_type_length = get_max_type_string_length(map);
	if (max_type_length < 4)
		max_type_length = 4;
	max_name_length = get_max_name_string_length(map);
	if (max_name_length < 6)
		max_name_length = 6;
	printf(" #: %*s %-*s %*s   %-*s\n", max_type_length, "type",
	    max_name_length, "name", digits, "length", digits, "base");

	LIST_FOREACH(entry, &map->disk_order, disk_entry) {
		dump_partition_entry(entry, max_type_length,
		    max_name_length, digits);
	}
	dump_block_zero(map);
}


void
dump_partition_entry(struct entry *entry, int type_length, int name_length,
    int digits)
{
	char buf[FMT_SCALED_STRSIZE];

	printf("%2ld: %*.32s", entry->disk_address, type_length,
	    entry->dpme_type);
	printf("%c%-*.32s ", contains_driver(entry) ? '*' : ' ',
	    name_length, entry->dpme_name);

	printf("%*u @ %-*u", digits, entry->dpme_pblocks, digits,
	    entry->dpme_pblock_start);

	if (fmt_scaled((long long)entry->dpme_pblocks *
	    entry->the_map->sbBlkSize, buf) == 0)
		printf("(%s)\n", buf);
	else
		printf("\n");
}


void
show_data_structures(struct partition_map *map)
{
	struct entry *entry;
	struct ddmap *m;
	int i;

	printf("Header:\n");
	printf("map %d blocks out of %d,  media %lu blocks (%d byte blocks)\n",
	    map->blocks_in_map, map->maximum_in_map, map->media_size,
	    map->sbBlkSize);
	printf("Map is%s writable", rflag ? " not" : "");
	printf(" and has%s been changed\n", (map->changed) ? "" : " not");
	printf("\n");

	printf("Block0:\n");
	printf("signature 0x%x", map->sbSig);
	printf("Block size=%u, Number of Blocks=%u\n", map->sbBlkSize,
	    map->sbBlkCount);
	printf("DeviceType=0x%x, DeviceId=0x%x, sbData=0x%x\n", map->sbDevType,
	    map->sbDevId, map->sbData);
	if (map->sbDrvrCount == 0) {
		printf("No drivers\n");
	} else {
		printf("%u driver%s-\n", map->sbDrvrCount,
		    (map->sbDrvrCount > 1) ? "s" : "");
		m = map->sbDDMap;
		for (i = 0; i < map->sbDrvrCount; i++) {
			printf("%u: @ %u for %u, type=0x%x\n", i + 1,
			    m[i].ddBlock, m[i].ddSize, m[i].ddType);
		}
	}
	printf("\n");
	printf(" #:                 type  length   base    "
	       "flags     (      logical      )\n");
	LIST_FOREACH(entry, &map->disk_order, disk_entry) {
		printf("%2ld: %20.32s ", entry->disk_address, entry->dpme_type);
		printf("%7u @ %-7u ", entry->dpme_pblocks,
		    entry->dpme_pblock_start);
		printf("%c%c%c%c%c%c%c%c%c ",
		       (entry->dpme_flags & DPME_VALID) ? 'V' : '.',
		       (entry->dpme_flags & DPME_ALLOCATED) ? 'A' : '.',
		       (entry->dpme_flags & DPME_IN_USE) ? 'I' : '.',
		       (entry->dpme_flags & DPME_BOOTABLE) ? 'B' : '.',
		       (entry->dpme_flags & DPME_READABLE) ? 'R' : '.',
		       (entry->dpme_flags & DPME_WRITABLE) ? 'W' : '.',
		       (entry->dpme_flags & DPME_OS_PIC_CODE) ? 'P' : '.',
		       (entry->dpme_flags & DPME_OS_SPECIFIC_2) ? '2' : '.',
		       (entry->dpme_flags & DPME_OS_SPECIFIC_1) ? '1' : '.');
		printf("( %7u @ %-7u )\n", entry->dpme_lblocks,
		    entry->dpme_lblock_start);
	}
	printf("\n");
	printf(" #:  booter   bytes      load_address      "
	    "goto_address checksum processor\n");
	LIST_FOREACH(entry, &map->disk_order, disk_entry) {
		printf("%2ld: ", entry->disk_address);
		printf("%7u ", entry->dpme_boot_block);
		printf("%7u ", entry->dpme_boot_bytes);
		printf("%8x ", entry->dpme_load_addr);
		printf("%8x ", entry->dpme_goto_addr);
		printf("%8x ", entry->dpme_checksum);
		printf("%.32s", entry->dpme_processor_id);
		printf("\n");
	}
	printf("\n");
}


void
full_dump_partition_entry(struct partition_map *map, int ix)
{
	struct entry *entry;
	int i;
	uint32_t t;

	entry = find_entry_by_disk_address(ix, map);
	if (entry == NULL) {
		printf("No such partition\n");
		return;
	}
	printf("             signature: 0x%x\n", entry->dpme_signature);
	printf(" number of map entries: %u\n", entry->dpme_map_entries);
	printf("        physical start: %10u  length: %10u\n",
	    entry->dpme_pblock_start, entry->dpme_pblocks);
	printf("         logical start: %10u  length: %10u\n",
	    entry->dpme_lblock_start, entry->dpme_lblocks);

	printf("                 flags: 0x%x\n", entry->dpme_flags);
	printf("                        ");
	if (entry->dpme_flags & DPME_VALID)
		printf("valid ");
	if (entry->dpme_flags & DPME_ALLOCATED)
		printf("alloc ");
	if (entry->dpme_flags & DPME_IN_USE)
		printf("in-use ");
	if (entry->dpme_flags & DPME_BOOTABLE)
		printf("boot ");
	if (entry->dpme_flags & DPME_READABLE)
		printf("read ");
	if (entry->dpme_flags & DPME_WRITABLE)
		printf("write ");
	if (entry->dpme_flags & DPME_OS_PIC_CODE)
		printf("pic ");
	t = entry->dpme_flags >> 7;
	for (i = 7; i <= 31; i++) {
		if (t & 0x1)
			printf("%d ", i);
		t = t >> 1;
	}
	printf("\n");

	printf("                  name: '%.32s'\n", entry->dpme_name);
	printf("                  type: '%.32s'\n", entry->dpme_type);
	printf("      boot start block: %10u\n", entry->dpme_boot_block);
	printf("boot length (in bytes): %10u\n", entry->dpme_boot_bytes);
	printf("          load address: 0x%08x\n", entry->dpme_load_addr);
	printf("         start address: 0x%08x\n", entry->dpme_goto_addr);
	printf("              checksum: 0x%08x\n", entry->dpme_checksum);
	printf("             processor: '%.32s'\n", entry->dpme_processor_id);
	printf("dpme_reserved_1 -");
	dump_block(entry->dpme_reserved_1, sizeof(entry->dpme_reserved_1));
	printf("dpme_reserved_2 -");
	dump_block(entry->dpme_reserved_2, sizeof(entry->dpme_reserved_2));
	printf("dpme_reserved_3 -");
	dump_block(entry->dpme_reserved_3, sizeof(entry->dpme_reserved_3));
	printf("dpme_reserved_4 -");
	dump_block(entry->dpme_reserved_4, sizeof(entry->dpme_reserved_4));
}


void
dump_block(unsigned char *addr, int len)
{
	int i, j, limit1, limit;

#define LINE_LEN 16
#define UNIT_LEN  4
#define OTHER_LEN  8

	for (i = 0; i < len; i = limit) {
		limit1 = i + LINE_LEN;
		if (limit1 > len)
			limit = len;
		else
			limit = limit1;
		printf("\n%03x: ", i);
		for (j = i; j < limit1; j++) {
			if (j % UNIT_LEN == 0)
				printf(" ");
			if (j < limit)
				printf("%02x", addr[j]);
			else
				printf("  ");
		}
		printf(" ");
		for (j = i; j < limit; j++) {
			if (j % OTHER_LEN == 0)
				printf(" ");
			if (addr[j] < ' ')
				printf(".");
			else
				printf("%c", addr[j]);
		}
	}
	printf("\n");
}

void
full_dump_block_zero(struct partition_map *map)
{
	struct ddmap *m;
	int i;

	m = map->sbDDMap;

	printf("             signature: 0x%x\n", map->sbSig);
	printf("       size of a block: %u\n", map->sbBlkSize);
	printf("      number of blocks: %u\n", map->sbBlkCount);
	printf("           device type: 0x%x\n", map->sbDevType);
	printf("             device id: 0x%x\n", map->sbDevId);
	printf("                  data: 0x%x\n", map->sbData);
	printf("          driver count: %u\n", map->sbDrvrCount);
	for (i = 0; i < 8; i++) {
		if (m[i].ddBlock == 0 && m[i].ddSize == 0 && m[i].ddType == 0)
			break;
		printf("      driver %3u block: %u\n", i + 1, m[i].ddBlock);
		printf("        size in blocks: %u\n", m[i].ddSize);
		printf("           driver type: 0x%x\n", m[i].ddType);
	}
	printf("remainder of block -");
	dump_block(map->sbReserved, sizeof(map->sbReserved));
}

int
get_max_type_string_length(struct partition_map *map)
{
	struct entry *entry;
	int max, length;

	max = 0;

	LIST_FOREACH(entry, &map->disk_order, disk_entry) {
		length = strnlen(entry->dpme_type, DPISTRLEN);
		if (length > max)
			max = length;
	}

	return max;
}

int
get_max_name_string_length(struct partition_map *map)
{
	struct entry *entry;
	int max, length;

	max = 0;

	LIST_FOREACH(entry, &map->disk_order, disk_entry) {
		length = strnlen(entry->dpme_name, DPISTRLEN);
		if (length > max)
			max = length;
	}

	return max;
}

int
get_max_base_or_length(struct partition_map *map)
{
	struct entry *entry;
	int max;

	max = 0;

	LIST_FOREACH(entry, &map->disk_order, disk_entry) {
		if (entry->dpme_pblock_start > max)
			max = entry->dpme_pblock_start;
		if (entry->dpme_pblocks > max)
			max = entry->dpme_pblocks;
	}

	return max;
}