summaryrefslogtreecommitdiff
path: root/sbin/pdisk/file_media.c
blob: 99c57db322c21ba35675fd6d6c57d3ab201e5862 (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
/*	$OpenBSD: file_media.c,v 1.42 2016/01/26 16:13:09 krw Exp $	*/

/*
 * file_media.c -
 *
 * Written by Eryk Vershen
 */

/*
 * Copyright 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/param.h>		/* DEV_BSIZE */

#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "dpme.h"
#include "file_media.h"

struct ddmap_ondisk {
    uint8_t	ddBlock[4];
    uint8_t	ddSize[2];
    uint8_t	ddType[2];
};

struct block0_ondisk {
    uint8_t	sbSig[2];
    uint8_t	sbBlkSize[2];
    uint8_t	sbBlkCount[4];
    uint8_t	sbDevType[2];
    uint8_t	sbDevId[2];
    uint8_t	sbData[4];
    uint8_t	sbDrvrCount[2];
    uint8_t	sbDDMap[64];	/* ddmap_ondisk[8] */
    uint8_t	reserved[430];
};

struct dpme_ondisk {
    uint8_t	dpme_signature[2];
    uint8_t	dpme_reserved_1[2];
    uint8_t	dpme_map_entries[4];
    uint8_t	dpme_pblock_start[4];
    uint8_t	dpme_pblocks[4];
    uint8_t	dpme_name[DPISTRLEN];
    uint8_t	dpme_type[DPISTRLEN];
    uint8_t	dpme_lblock_start[4];
    uint8_t	dpme_lblocks[4];
    uint8_t	dpme_flags[4];
    uint8_t	dpme_boot_block[4];
    uint8_t	dpme_boot_bytes[4];
    uint8_t	dpme_load_addr[4];
    uint8_t	dpme_reserved_2[4];
    uint8_t	dpme_goto_addr[4];
    uint8_t	dpme_reserved_3[4];
    uint8_t	dpme_checksum[4];
    uint8_t	dpme_processor_id[16];
    uint8_t	dpme_reserved_4[376];
};

static int	read_block(int, uint64_t, void *);
static int	write_block(int, uint64_t, void *);

static int
read_block(int fd, uint64_t sector, void *address)
{
	ssize_t off;

	off = pread(fd, address, DEV_BSIZE, sector * DEV_BSIZE);
	if (off == DEV_BSIZE)
		return (1);

	if (off == 0)
		fprintf(stderr, "end of file encountered");
	else if (off == -1)
		warn("reading file failed");
	else
		fprintf(stderr, "short read");

	return (0);
}

static int
write_block(int fd, uint64_t sector, void *address)
{
	ssize_t off;

	off = pwrite(fd, address, DEV_BSIZE, sector * DEV_BSIZE);
	if (off == DEV_BSIZE)
		return (1);

	warn("writing to file failed");
	return (0);
}

int
read_block0(int fd, struct block0 *block0)
{
	struct block0_ondisk *block0_ondisk;
	struct ddmap_ondisk ddmap_ondisk;
	int i;

	block0_ondisk = malloc(sizeof(struct block0_ondisk));
	if (block0_ondisk == NULL)
		return 0;

	if (read_block(fd, 0, block0_ondisk) == 0)
		return 0;

	memcpy(&block0->sbSig, block0_ondisk->sbSig,
	    sizeof(block0->sbSig));
	block0->sbSig = betoh16(block0->sbSig);
	memcpy(&block0->sbBlkSize, block0_ondisk->sbBlkSize,
	    sizeof(block0->sbBlkSize));
	block0->sbBlkSize = betoh16(block0->sbBlkSize);
	memcpy(&block0->sbBlkCount, block0_ondisk->sbBlkCount,
	    sizeof(block0->sbBlkCount));
	block0->sbBlkCount = betoh32(block0->sbBlkCount);
	memcpy(&block0->sbDevType, block0_ondisk->sbDevType,
	    sizeof(block0->sbDevType));
	block0->sbDevType = betoh16(block0->sbDevType);
	memcpy(&block0->sbDevId, block0_ondisk->sbDevId,
	    sizeof(block0->sbDevId));
	block0->sbDevId = betoh16(block0->sbDevId);
	memcpy(&block0->sbData, block0_ondisk->sbData,
	    sizeof(block0->sbData));
	block0->sbData = betoh32(block0->sbData);
	memcpy(&block0->sbDrvrCount, block0_ondisk->sbDrvrCount,
	    sizeof(block0->sbDrvrCount));
	block0->sbDrvrCount = betoh16(block0->sbDrvrCount);

	for (i = 0; i < 8; i++) {
		memcpy(&ddmap_ondisk,
		    block0->sbDDMap+i*sizeof(struct ddmap_ondisk),
		    sizeof(ddmap_ondisk));
		memcpy(&block0->sbDDMap[i].ddBlock, &ddmap_ondisk.ddBlock,
		    sizeof(block0->sbDDMap[i].ddBlock));
		block0->sbDDMap[i].ddBlock =
		    betoh32(block0->sbDDMap[i].ddBlock);
		memcpy(&block0->sbDDMap[i].ddSize, &ddmap_ondisk.ddSize,
		    sizeof(block0->sbDDMap[i].ddSize));
		block0->sbDDMap[i].ddSize = betoh16(block0->sbDDMap[i].ddSize);
		memcpy(&block0->sbDDMap[i].ddType, &ddmap_ondisk.ddType,
		    sizeof(block0->sbDDMap[i].ddType));
		block0->sbDDMap[i].ddType = betoh32(block0->sbDDMap[i].ddType);
	}

	free(block0_ondisk);
	return 1;
}

int
write_block0(int fd, struct block0 *block0)
{
	struct block0_ondisk *block0_ondisk;
	struct ddmap_ondisk ddmap_ondisk;
	int i, rslt;
	uint32_t tmp32;
	uint16_t tmp16;

	block0_ondisk = malloc(sizeof(struct block0_ondisk));
	if (block0_ondisk == NULL)
		return 0;

	tmp16 = htobe16(block0->sbSig);
	memcpy(block0_ondisk->sbSig, &tmp16,
	    sizeof(block0_ondisk->sbSig));
	tmp16 = htobe16(block0->sbBlkSize);
	memcpy(block0_ondisk->sbBlkSize, &tmp16,
	    sizeof(block0_ondisk->sbBlkSize));
	tmp32 = htobe32(block0->sbBlkCount);
	memcpy(block0_ondisk->sbBlkCount, &tmp32,
	    sizeof(block0_ondisk->sbBlkCount));
	tmp16 = htobe16(block0->sbDevType);
	memcpy(block0_ondisk->sbDevType, &tmp16,
	    sizeof(block0_ondisk->sbDevType));
	tmp16 = htobe16(block0->sbDevId);
	memcpy(block0_ondisk->sbDevId, &tmp16,
	    sizeof(block0_ondisk->sbDevId));
	tmp32 = htobe32(block0->sbData);
	memcpy(block0_ondisk->sbData, &tmp32,
	    sizeof(block0_ondisk->sbData));
	tmp16 = htobe16(block0->sbDrvrCount);
	memcpy(block0_ondisk->sbDrvrCount, &tmp16,
	    sizeof(block0_ondisk->sbDrvrCount));

	for (i = 0; i < 8; i++) {
		tmp32 = htobe32(block0->sbDDMap[i].ddBlock);
		memcpy(ddmap_ondisk.ddBlock, &tmp32,
		    sizeof(ddmap_ondisk.ddBlock));
		tmp16 = htobe16(block0->sbDDMap[i].ddSize);
		memcpy(&ddmap_ondisk.ddSize, &tmp16,
		    sizeof(ddmap_ondisk.ddSize));
		tmp16 = betoh32(block0->sbDDMap[i].ddType);
		memcpy(&ddmap_ondisk.ddType, &tmp16,
		    sizeof(ddmap_ondisk.ddType));
		memcpy(block0->sbDDMap+i*sizeof(struct ddmap_ondisk),
		    &ddmap_ondisk, sizeof(ddmap_ondisk));
	}

	rslt = write_block(fd, 0, block0_ondisk);
	free(block0_ondisk);
	return rslt;
}

int
read_dpme(int fd, uint64_t sector, struct dpme *dpme)
{
	struct dpme_ondisk *dpme_ondisk;

	dpme_ondisk = malloc(sizeof(struct dpme_ondisk));
	if (dpme_ondisk == NULL)
		return 0;

	if (read_block(fd, sector, dpme_ondisk) == 0)
		return 0;

	memcpy(&dpme->dpme_signature, dpme_ondisk->dpme_signature,
	    sizeof(dpme->dpme_signature));
	memcpy(&dpme->dpme_map_entries, dpme_ondisk->dpme_map_entries,
	    sizeof(dpme->dpme_map_entries));
	memcpy(&dpme->dpme_pblock_start, dpme_ondisk->dpme_pblock_start,
	    sizeof(dpme->dpme_pblock_start));
	memcpy(&dpme->dpme_pblocks, dpme_ondisk->dpme_pblocks,
	    sizeof(dpme->dpme_pblocks));
	memcpy(&dpme->dpme_lblock_start, dpme_ondisk->dpme_lblock_start,
	    sizeof(dpme->dpme_lblock_start));
	memcpy(&dpme->dpme_lblocks, dpme_ondisk->dpme_lblocks,
	    sizeof(dpme->dpme_lblocks));
	memcpy(&dpme->dpme_flags, dpme_ondisk->dpme_flags,
	    sizeof(dpme->dpme_flags));
	memcpy(&dpme->dpme_boot_block, dpme_ondisk->dpme_boot_block,
	    sizeof(dpme->dpme_boot_block));
	memcpy(&dpme->dpme_boot_bytes, dpme_ondisk->dpme_boot_bytes,
	    sizeof(dpme->dpme_boot_bytes));
	memcpy(&dpme->dpme_load_addr, dpme_ondisk->dpme_load_addr,
	    sizeof(dpme->dpme_load_addr));
	memcpy(&dpme->dpme_goto_addr, dpme_ondisk->dpme_goto_addr,
	    sizeof(dpme->dpme_goto_addr));
	memcpy(&dpme->dpme_checksum, dpme_ondisk->dpme_checksum,
	    sizeof(dpme->dpme_checksum));

	dpme->dpme_signature = betoh16(dpme->dpme_signature);
	dpme->dpme_map_entries = betoh32(dpme->dpme_map_entries);
	dpme->dpme_pblock_start = betoh32(dpme->dpme_pblock_start);
	dpme->dpme_pblocks = betoh32(dpme->dpme_pblocks);
	dpme->dpme_lblock_start = betoh32(dpme->dpme_lblock_start);
	dpme->dpme_lblocks = betoh32(dpme->dpme_lblocks);
	dpme->dpme_flags = betoh32(dpme->dpme_flags);
	dpme->dpme_boot_block = betoh32(dpme->dpme_boot_block);
	dpme->dpme_boot_bytes = betoh32(dpme->dpme_boot_bytes);
	dpme->dpme_load_addr = betoh32(dpme->dpme_load_addr);
	dpme->dpme_goto_addr = betoh32(dpme->dpme_goto_addr);
	dpme->dpme_checksum = betoh32(dpme->dpme_checksum);

	memcpy(dpme->dpme_reserved_1, dpme_ondisk->dpme_reserved_1,
	    sizeof(dpme->dpme_reserved_1));
	memcpy(dpme->dpme_reserved_2, dpme_ondisk->dpme_reserved_2,
	    sizeof(dpme->dpme_reserved_2));
	memcpy(dpme->dpme_reserved_3, dpme_ondisk->dpme_reserved_3,
	    sizeof(dpme->dpme_reserved_3));
	memcpy(dpme->dpme_reserved_4, dpme_ondisk->dpme_reserved_4,
	    sizeof(dpme->dpme_reserved_4));

	memcpy(dpme->dpme_name, dpme_ondisk->dpme_name,
	    sizeof(dpme->dpme_name));
	memcpy(dpme->dpme_type, dpme_ondisk->dpme_type,
	    sizeof(dpme->dpme_type));
	memcpy(dpme->dpme_processor_id, dpme_ondisk->dpme_processor_id,
	    sizeof(dpme->dpme_processor_id));

	free(dpme_ondisk);
	return 1;
}

int
write_dpme(int fd, uint64_t sector, struct dpme *dpme)
{
	struct dpme_ondisk *dpme_ondisk;
	int rslt;
	uint32_t tmp32;
	uint16_t tmp16;

	dpme_ondisk = malloc(sizeof(struct dpme_ondisk));
	if (dpme_ondisk == NULL)
		return 0;

	memcpy(dpme_ondisk->dpme_name, dpme->dpme_name,
	    sizeof(dpme_ondisk->dpme_name));
	memcpy(dpme_ondisk->dpme_type, dpme->dpme_type,
	    sizeof(dpme_ondisk->dpme_type));
	memcpy(dpme_ondisk->dpme_processor_id, dpme->dpme_processor_id,
	    sizeof(dpme_ondisk->dpme_processor_id));

	memcpy(dpme_ondisk->dpme_reserved_1, dpme->dpme_reserved_1,
	    sizeof(dpme_ondisk->dpme_reserved_1));
	memcpy(dpme_ondisk->dpme_reserved_2, dpme->dpme_reserved_2,
	    sizeof(dpme_ondisk->dpme_reserved_2));
	memcpy(dpme_ondisk->dpme_reserved_3, dpme->dpme_reserved_3,
	    sizeof(dpme_ondisk->dpme_reserved_3));
	memcpy(dpme_ondisk->dpme_reserved_4, dpme->dpme_reserved_4,
	    sizeof(dpme_ondisk->dpme_reserved_4));

	tmp16 = htobe16(dpme->dpme_signature);
	memcpy(dpme_ondisk->dpme_signature, &tmp16,
	    sizeof(dpme_ondisk->dpme_signature));
	tmp32 = htobe32(dpme->dpme_map_entries);
	memcpy(dpme_ondisk->dpme_map_entries, &tmp32,
	    sizeof(dpme_ondisk->dpme_map_entries));
	tmp32 = htobe32(dpme->dpme_pblock_start);
	memcpy(dpme_ondisk->dpme_pblock_start, &tmp32,
	    sizeof(dpme_ondisk->dpme_pblock_start));
	tmp32 = htobe32(dpme->dpme_pblocks);
	memcpy(dpme_ondisk->dpme_pblocks, &tmp32,
	    sizeof(dpme_ondisk->dpme_pblocks));
	tmp32 = htobe32(dpme->dpme_lblock_start);
	memcpy(dpme_ondisk->dpme_lblock_start, &tmp32,
	    sizeof(dpme_ondisk->dpme_lblock_start));
	tmp32 = betoh32(dpme->dpme_lblocks);
	memcpy(dpme_ondisk->dpme_lblocks, &tmp32,
	    sizeof(dpme_ondisk->dpme_lblocks));
	tmp32 = betoh32(dpme->dpme_flags);
	memcpy(dpme_ondisk->dpme_flags, &tmp32,
	    sizeof(dpme_ondisk->dpme_flags));
	tmp32 = htobe32(dpme->dpme_boot_block);
	memcpy(dpme_ondisk->dpme_boot_block, &tmp32,
	    sizeof(dpme_ondisk->dpme_boot_block));
	tmp32 = htobe32(dpme->dpme_boot_bytes);
	memcpy(dpme_ondisk->dpme_boot_bytes, &tmp32,
	    sizeof(dpme_ondisk->dpme_boot_bytes));
	tmp32 = betoh32(dpme->dpme_load_addr);
	memcpy(dpme_ondisk->dpme_load_addr, &tmp32,
	    sizeof(dpme_ondisk->dpme_load_addr));
	tmp32 = betoh32(dpme->dpme_goto_addr);
	memcpy(dpme_ondisk->dpme_goto_addr, &tmp32,
	    sizeof(dpme_ondisk->dpme_goto_addr));
	tmp32 = betoh32(dpme->dpme_checksum);
	memcpy(dpme_ondisk->dpme_checksum, &tmp32,
	    sizeof(dpme_ondisk->dpme_checksum));

	rslt = write_block(fd, sector, dpme_ondisk);
	free(dpme_ondisk);
	return rslt;
}