1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21#include "volume_id_internal.h"
22
23struct mac_driver_desc {
24 uint8_t signature[2];
25 uint16_t block_size;
26 uint32_t block_count;
27} PACKED;
28
29struct mac_partition {
30 uint8_t signature[2];
31 uint16_t res1;
32 uint32_t map_count;
33 uint32_t start_block;
34 uint32_t block_count;
35 uint8_t name[32];
36 uint8_t type[32];
37} PACKED;
38
39int FAST_FUNC volume_id_probe_mac_partition_map(struct volume_id *id, uint64_t off)
40{
41 const uint8_t *buf;
42 struct mac_driver_desc *driver;
43 struct mac_partition *part;
44
45 dbg("probing at offset 0x%llx", (unsigned long long) off);
46
47 buf = volume_id_get_buffer(id, off, 0x200);
48 if (buf == NULL)
49 return -1;
50
51 part = (struct mac_partition *) buf;
52 if (part->signature[0] == 'P' && part->signature[1] == 'M'
53 && (memcmp(part->type, "Apple_partition_map", 19) == 0)
54 ) {
55
56
57
58
59 return 0;
60 }
61
62 driver = (struct mac_driver_desc *) buf;
63 if (driver->signature[0] == 'E' && driver->signature[1] == 'R') {
64
65
66 unsigned bsize = be16_to_cpu(driver->block_size);
67 int part_count;
68 int i;
69
70
71 buf = volume_id_get_buffer(id, off + bsize, 0x200);
72 if (buf == NULL)
73 return -1;
74
75 part = (struct mac_partition *) buf;
76 if (part->signature[0] != 'P' || part->signature[1] != 'M')
77 return -1;
78
79 part_count = be32_to_cpu(part->map_count);
80 dbg("expecting %d partition entries", part_count);
81
82 if (id->partitions != NULL)
83 free(id->partitions);
84 id->partitions = xzalloc(part_count * sizeof(struct volume_id_partition));
85
86 id->partition_count = part_count;
87
88 for (i = 0; i < part_count; i++) {
89 uint64_t poff;
90 uint64_t plen;
91
92 buf = volume_id_get_buffer(id, off + ((i+1) * bsize), 0x200);
93 if (buf == NULL)
94 return -1;
95
96 part = (struct mac_partition *) buf;
97 if (part->signature[0] != 'P' || part->signature[1] != 'M')
98 return -1;
99
100 poff = be32_to_cpu(part->start_block) * bsize;
101 plen = be32_to_cpu(part->block_count) * bsize;
102 dbg("found '%s' partition entry at 0x%llx, len 0x%llx",
103 part->type, (unsigned long long) poff,
104 (unsigned long long) plen);
105
106
107
108
109
110
111
112
113
114
115
116 }
117
118
119 return 0;
120 }
121
122 return -1;
123}
124