diff options
Diffstat (limited to 'util-linux/volume_id/mac.c')
-rw-r--r-- | util-linux/volume_id/mac.c | 125 |
1 files changed, 125 insertions, 0 deletions
diff --git a/util-linux/volume_id/mac.c b/util-linux/volume_id/mac.c new file mode 100644 index 000000000..94a99bee0 --- /dev/null +++ b/util-linux/volume_id/mac.c | |||
@@ -0,0 +1,125 @@ | |||
1 | /* | ||
2 | * volume_id - reads filesystem label and uuid | ||
3 | * | ||
4 | * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org> | ||
5 | * | ||
6 | * This library is free software; you can redistribute it and/or | ||
7 | * modify it under the terms of the GNU Lesser General Public | ||
8 | * License as published by the Free Software Foundation; either | ||
9 | * version 2.1 of the License, or (at your option) any later version. | ||
10 | * | ||
11 | * This library is distributed in the hope that it will be useful, | ||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
14 | * Lesser General Public License for more details. | ||
15 | * | ||
16 | * You should have received a copy of the GNU Lesser General Public | ||
17 | * License along with this library; if not, write to the Free Software | ||
18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
19 | */ | ||
20 | |||
21 | #include "volume_id_internal.h" | ||
22 | |||
23 | struct mac_driver_desc { | ||
24 | uint8_t signature[2]; | ||
25 | uint16_t block_size; | ||
26 | uint32_t block_count; | ||
27 | } __attribute__((__packed__)); | ||
28 | |||
29 | struct 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 | } __attribute__((__packed__)); | ||
38 | |||
39 | int 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 ((memcmp(part->signature, "PM", 2) == 0) && | ||
53 | (memcmp(part->type, "Apple_partition_map", 19) == 0)) { | ||
54 | /* linux creates an own subdevice for the map | ||
55 | * just return the type if the drive header is missing */ | ||
56 | volume_id_set_usage(id, VOLUME_ID_PARTITIONTABLE); | ||
57 | id->type = "mac_partition_map"; | ||
58 | return 0; | ||
59 | } | ||
60 | |||
61 | driver = (struct mac_driver_desc *) buf; | ||
62 | if (memcmp(driver->signature, "ER", 2) == 0) { | ||
63 | /* we are on a main device, like a CD | ||
64 | * just try to probe the first partition from the map */ | ||
65 | unsigned bsize = be16_to_cpu(driver->block_size); | ||
66 | int part_count; | ||
67 | int i; | ||
68 | |||
69 | /* get first entry of partition table */ | ||
70 | buf = volume_id_get_buffer(id, off + bsize, 0x200); | ||
71 | if (buf == NULL) | ||
72 | return -1; | ||
73 | |||
74 | part = (struct mac_partition *) buf; | ||
75 | if (memcmp(part->signature, "PM", 2) != 0) | ||
76 | return -1; | ||
77 | |||
78 | part_count = be32_to_cpu(part->map_count); | ||
79 | dbg("expecting %d partition entries", part_count); | ||
80 | |||
81 | if (id->partitions != NULL) | ||
82 | free(id->partitions); | ||
83 | id->partitions = | ||
84 | malloc(part_count * sizeof(struct volume_id_partition)); | ||
85 | if (id->partitions == NULL) | ||
86 | return -1; | ||
87 | memset(id->partitions, 0x00, sizeof(struct volume_id_partition)); | ||
88 | |||
89 | id->partition_count = part_count; | ||
90 | |||
91 | for (i = 0; i < part_count; i++) { | ||
92 | uint64_t poff; | ||
93 | uint64_t plen; | ||
94 | |||
95 | buf = volume_id_get_buffer(id, off + ((i+1) * bsize), 0x200); | ||
96 | if (buf == NULL) | ||
97 | return -1; | ||
98 | |||
99 | part = (struct mac_partition *) buf; | ||
100 | if (memcmp(part->signature, "PM", 2) != 0) | ||
101 | return -1; | ||
102 | |||
103 | poff = be32_to_cpu(part->start_block) * bsize; | ||
104 | plen = be32_to_cpu(part->block_count) * bsize; | ||
105 | dbg("found '%s' partition entry at 0x%llx, len 0x%llx", | ||
106 | part->type, (unsigned long long) poff, (unsigned long long) plen); | ||
107 | |||
108 | id->partitions[i].off = poff; | ||
109 | id->partitions[i].len = plen; | ||
110 | |||
111 | if (memcmp(part->type, "Apple_Free", 10) == 0) { | ||
112 | volume_id_set_usage_part(&id->partitions[i], VOLUME_ID_UNUSED); | ||
113 | } else if (memcmp(part->type, "Apple_partition_map", 19) == 0) { | ||
114 | volume_id_set_usage_part(&id->partitions[i], VOLUME_ID_PARTITIONTABLE); | ||
115 | } else { | ||
116 | volume_id_set_usage_part(&id->partitions[i], VOLUME_ID_UNPROBED); | ||
117 | } | ||
118 | } | ||
119 | volume_id_set_usage(id, VOLUME_ID_PARTITIONTABLE); | ||
120 | id->type = "mac_partition_map"; | ||
121 | return 0; | ||
122 | } | ||
123 | |||
124 | return -1; | ||
125 | } | ||