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
|
/* $OpenBSD: file_media.c,v 1.48 2016/01/30 17:21:10 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 <sys/queue.h>
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "partition_map.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 partition_map *map)
{
struct block0_ondisk *block0_ondisk;
struct ddmap_ondisk ddmap_ondisk;
int i;
block0_ondisk = malloc(sizeof(struct block0_ondisk));
if (block0_ondisk == NULL)
errx(1, "No memory to read block0");
if (read_block(fd, 0, block0_ondisk) == 0)
return 0;
memcpy(&map->sbSig, block0_ondisk->sbSig,
sizeof(map->sbSig));
map->sbSig = betoh16(map->sbSig);
memcpy(&map->sbBlkSize, block0_ondisk->sbBlkSize,
sizeof(map->sbBlkSize));
map->sbBlkSize = betoh16(map->sbBlkSize);
memcpy(&map->sbBlkCount, block0_ondisk->sbBlkCount,
sizeof(map->sbBlkCount));
map->sbBlkCount = betoh32(map->sbBlkCount);
memcpy(&map->sbDevType, block0_ondisk->sbDevType,
sizeof(map->sbDevType));
map->sbDevType = betoh16(map->sbDevType);
memcpy(&map->sbDevId, block0_ondisk->sbDevId,
sizeof(map->sbDevId));
map->sbDevId = betoh16(map->sbDevId);
memcpy(&map->sbData, block0_ondisk->sbData,
sizeof(map->sbData));
map->sbData = betoh32(map->sbData);
memcpy(&map->sbDrvrCount, block0_ondisk->sbDrvrCount,
sizeof(map->sbDrvrCount));
map->sbDrvrCount = betoh16(map->sbDrvrCount);
for (i = 0; i < 8; i++) {
memcpy(&ddmap_ondisk,
map->sbDDMap+i*sizeof(struct ddmap_ondisk),
sizeof(ddmap_ondisk));
memcpy(&map->sbDDMap[i].ddBlock, &ddmap_ondisk.ddBlock,
sizeof(map->sbDDMap[i].ddBlock));
map->sbDDMap[i].ddBlock =
betoh32(map->sbDDMap[i].ddBlock);
memcpy(&map->sbDDMap[i].ddSize, &ddmap_ondisk.ddSize,
sizeof(map->sbDDMap[i].ddSize));
map->sbDDMap[i].ddSize = betoh16(map->sbDDMap[i].ddSize);
memcpy(&map->sbDDMap[i].ddType, &ddmap_ondisk.ddType,
sizeof(map->sbDDMap[i].ddType));
map->sbDDMap[i].ddType = betoh32(map->sbDDMap[i].ddType);
}
free(block0_ondisk);
return 1;
}
int
write_block0(int fd, struct partition_map *map)
{
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)
errx(1, "No memory to write block 0");
tmp16 = htobe16(map->sbSig);
memcpy(block0_ondisk->sbSig, &tmp16,
sizeof(block0_ondisk->sbSig));
tmp16 = htobe16(map->sbBlkSize);
memcpy(block0_ondisk->sbBlkSize, &tmp16,
sizeof(block0_ondisk->sbBlkSize));
tmp32 = htobe32(map->sbBlkCount);
memcpy(block0_ondisk->sbBlkCount, &tmp32,
sizeof(block0_ondisk->sbBlkCount));
tmp16 = htobe16(map->sbDevType);
memcpy(block0_ondisk->sbDevType, &tmp16,
sizeof(block0_ondisk->sbDevType));
tmp16 = htobe16(map->sbDevId);
memcpy(block0_ondisk->sbDevId, &tmp16,
sizeof(block0_ondisk->sbDevId));
tmp32 = htobe32(map->sbData);
memcpy(block0_ondisk->sbData, &tmp32,
sizeof(block0_ondisk->sbData));
tmp16 = htobe16(map->sbDrvrCount);
memcpy(block0_ondisk->sbDrvrCount, &tmp16,
sizeof(block0_ondisk->sbDrvrCount));
for (i = 0; i < 8; i++) {
tmp32 = htobe32(map->sbDDMap[i].ddBlock);
memcpy(ddmap_ondisk.ddBlock, &tmp32,
sizeof(ddmap_ondisk.ddBlock));
tmp16 = htobe16(map->sbDDMap[i].ddSize);
memcpy(&ddmap_ondisk.ddSize, &tmp16,
sizeof(ddmap_ondisk.ddSize));
tmp16 = betoh32(map->sbDDMap[i].ddType);
memcpy(&ddmap_ondisk.ddType, &tmp16,
sizeof(ddmap_ondisk.ddType));
memcpy(map->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 entry *entry)
{
struct dpme_ondisk *dpme_ondisk;
dpme_ondisk = malloc(sizeof(struct dpme_ondisk));
if (dpme_ondisk == NULL)
errx(1, "No memory to read dpme");
if (read_block(fd, sector, dpme_ondisk) == 0)
return 0;
memcpy(&entry->dpme_signature, dpme_ondisk->dpme_signature,
sizeof(entry->dpme_signature));
memcpy(&entry->dpme_map_entries, dpme_ondisk->dpme_map_entries,
sizeof(entry->dpme_map_entries));
memcpy(&entry->dpme_pblock_start, dpme_ondisk->dpme_pblock_start,
sizeof(entry->dpme_pblock_start));
memcpy(&entry->dpme_pblocks, dpme_ondisk->dpme_pblocks,
sizeof(entry->dpme_pblocks));
memcpy(&entry->dpme_lblock_start, dpme_ondisk->dpme_lblock_start,
sizeof(entry->dpme_lblock_start));
memcpy(&entry->dpme_lblocks, dpme_ondisk->dpme_lblocks,
sizeof(entry->dpme_lblocks));
memcpy(&entry->dpme_flags, dpme_ondisk->dpme_flags,
sizeof(entry->dpme_flags));
memcpy(&entry->dpme_boot_block, dpme_ondisk->dpme_boot_block,
sizeof(entry->dpme_boot_block));
memcpy(&entry->dpme_boot_bytes, dpme_ondisk->dpme_boot_bytes,
sizeof(entry->dpme_boot_bytes));
memcpy(&entry->dpme_load_addr, dpme_ondisk->dpme_load_addr,
sizeof(entry->dpme_load_addr));
memcpy(&entry->dpme_goto_addr, dpme_ondisk->dpme_goto_addr,
sizeof(entry->dpme_goto_addr));
memcpy(&entry->dpme_checksum, dpme_ondisk->dpme_checksum,
sizeof(entry->dpme_checksum));
entry->dpme_signature = betoh16(entry->dpme_signature);
entry->dpme_map_entries = betoh32(entry->dpme_map_entries);
entry->dpme_pblock_start = betoh32(entry->dpme_pblock_start);
entry->dpme_pblocks = betoh32(entry->dpme_pblocks);
entry->dpme_lblock_start = betoh32(entry->dpme_lblock_start);
entry->dpme_lblocks = betoh32(entry->dpme_lblocks);
entry->dpme_flags = betoh32(entry->dpme_flags);
entry->dpme_boot_block = betoh32(entry->dpme_boot_block);
entry->dpme_boot_bytes = betoh32(entry->dpme_boot_bytes);
entry->dpme_load_addr = betoh32(entry->dpme_load_addr);
entry->dpme_goto_addr = betoh32(entry->dpme_goto_addr);
entry->dpme_checksum = betoh32(entry->dpme_checksum);
memcpy(entry->dpme_reserved_1, dpme_ondisk->dpme_reserved_1,
sizeof(entry->dpme_reserved_1));
memcpy(entry->dpme_reserved_2, dpme_ondisk->dpme_reserved_2,
sizeof(entry->dpme_reserved_2));
memcpy(entry->dpme_reserved_3, dpme_ondisk->dpme_reserved_3,
sizeof(entry->dpme_reserved_3));
memcpy(entry->dpme_reserved_4, dpme_ondisk->dpme_reserved_4,
sizeof(entry->dpme_reserved_4));
strlcpy(entry->dpme_name, dpme_ondisk->dpme_name,
sizeof(entry->dpme_name));
strlcpy(entry->dpme_type, dpme_ondisk->dpme_type,
sizeof(entry->dpme_type));
strlcpy(entry->dpme_processor_id, dpme_ondisk->dpme_processor_id,
sizeof(entry->dpme_processor_id));
free(dpme_ondisk);
return 1;
}
int
write_dpme(int fd, uint64_t sector, struct entry *entry)
{
struct dpme_ondisk *dpme_ondisk;
int rslt;
uint32_t tmp32;
uint16_t tmp16;
dpme_ondisk = malloc(sizeof(struct dpme_ondisk));
if (dpme_ondisk == NULL)
errx(1, "No memory to write dpme");
memcpy(dpme_ondisk->dpme_name, entry->dpme_name,
sizeof(dpme_ondisk->dpme_name));
memcpy(dpme_ondisk->dpme_type, entry->dpme_type,
sizeof(dpme_ondisk->dpme_type));
memcpy(dpme_ondisk->dpme_processor_id, entry->dpme_processor_id,
sizeof(dpme_ondisk->dpme_processor_id));
memcpy(dpme_ondisk->dpme_reserved_1, entry->dpme_reserved_1,
sizeof(dpme_ondisk->dpme_reserved_1));
memcpy(dpme_ondisk->dpme_reserved_2, entry->dpme_reserved_2,
sizeof(dpme_ondisk->dpme_reserved_2));
memcpy(dpme_ondisk->dpme_reserved_3, entry->dpme_reserved_3,
sizeof(dpme_ondisk->dpme_reserved_3));
memcpy(dpme_ondisk->dpme_reserved_4, entry->dpme_reserved_4,
sizeof(dpme_ondisk->dpme_reserved_4));
tmp16 = htobe16(entry->dpme_signature);
memcpy(dpme_ondisk->dpme_signature, &tmp16,
sizeof(dpme_ondisk->dpme_signature));
tmp32 = htobe32(entry->dpme_map_entries);
memcpy(dpme_ondisk->dpme_map_entries, &tmp32,
sizeof(dpme_ondisk->dpme_map_entries));
tmp32 = htobe32(entry->dpme_pblock_start);
memcpy(dpme_ondisk->dpme_pblock_start, &tmp32,
sizeof(dpme_ondisk->dpme_pblock_start));
tmp32 = htobe32(entry->dpme_pblocks);
memcpy(dpme_ondisk->dpme_pblocks, &tmp32,
sizeof(dpme_ondisk->dpme_pblocks));
tmp32 = htobe32(entry->dpme_lblock_start);
memcpy(dpme_ondisk->dpme_lblock_start, &tmp32,
sizeof(dpme_ondisk->dpme_lblock_start));
tmp32 = betoh32(entry->dpme_lblocks);
memcpy(dpme_ondisk->dpme_lblocks, &tmp32,
sizeof(dpme_ondisk->dpme_lblocks));
tmp32 = betoh32(entry->dpme_flags);
memcpy(dpme_ondisk->dpme_flags, &tmp32,
sizeof(dpme_ondisk->dpme_flags));
tmp32 = htobe32(entry->dpme_boot_block);
memcpy(dpme_ondisk->dpme_boot_block, &tmp32,
sizeof(dpme_ondisk->dpme_boot_block));
tmp32 = htobe32(entry->dpme_boot_bytes);
memcpy(dpme_ondisk->dpme_boot_bytes, &tmp32,
sizeof(dpme_ondisk->dpme_boot_bytes));
tmp32 = betoh32(entry->dpme_load_addr);
memcpy(dpme_ondisk->dpme_load_addr, &tmp32,
sizeof(dpme_ondisk->dpme_load_addr));
tmp32 = betoh32(entry->dpme_goto_addr);
memcpy(dpme_ondisk->dpme_goto_addr, &tmp32,
sizeof(dpme_ondisk->dpme_goto_addr));
tmp32 = betoh32(entry->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;
}
|