chiark / gitweb /
53f0761c8a86300a376db2486312f9b807f488d4
[elogind.git] / extras / volume_id / libvolume_id / mac.c
1 /*
2  * volume_id - reads filesystem label and uuid
3  *
4  * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
5  *
6  *      This program is free software; you can redistribute it and/or modify it
7  *      under the terms of the GNU General Public License as published by the
8  *      Free Software Foundation version 2 of the License.
9  */
10
11 #ifndef _GNU_SOURCE
12 #define _GNU_SOURCE 1
13 #endif
14
15 #ifdef HAVE_CONFIG_H
16 #  include <config.h>
17 #endif
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <string.h>
23 #include <errno.h>
24 #include <ctype.h>
25
26 #include "volume_id.h"
27 #include "logging.h"
28 #include "util.h"
29
30 struct mac_driver_desc {
31         uint8_t         signature[2];
32         uint16_t        block_size;
33         uint32_t        block_count;
34 } __attribute__((__packed__));
35
36 struct mac_partition {
37         uint8_t         signature[2];
38         uint16_t        res1;
39         uint32_t        map_count;
40         uint32_t        start_block;
41         uint32_t        block_count;
42         uint8_t         name[32];
43         uint8_t         type[32];
44 } __attribute__((__packed__));
45
46 int volume_id_probe_mac_partition_map(struct volume_id *id, uint64_t off)
47 {
48         const uint8_t *buf;
49         struct mac_driver_desc *driver;
50         struct mac_partition *part;
51
52         dbg("probing at offset 0x%llx", (unsigned long long) off);
53
54         buf = volume_id_get_buffer(id, off, 0x200);
55         if (buf == NULL)
56                 return -1;
57
58         part = (struct mac_partition *) buf;
59         if ((memcmp(part->signature, "PM", 2) == 0) &&
60             (memcmp(part->type, "Apple_partition_map", 19) == 0)) {
61                 /* linux creates an own subdevice for the map
62                  * just return the type if the drive header is missing */
63                 volume_id_set_usage(id, VOLUME_ID_PARTITIONTABLE);
64                 id->type = "mac_partition_map";
65                 return 0;
66         }
67
68         driver = (struct mac_driver_desc *) buf;
69         if (memcmp(driver->signature, "ER", 2) == 0) {
70                 /* we are on a main device, like a CD
71                  * just try to probe the first partition from the map */
72                 unsigned int bsize = be16_to_cpu(driver->block_size);
73                 int part_count;
74                 int i;
75
76                 /* get first entry of partition table */
77                 buf = volume_id_get_buffer(id, off +  bsize, 0x200);
78                 if (buf == NULL)
79                         return -1;
80
81                 part = (struct mac_partition *) buf;
82                 if (memcmp(part->signature, "PM", 2) != 0)
83                         return -1;
84
85                 part_count = be32_to_cpu(part->map_count);
86                 dbg("expecting %d partition entries", part_count);
87                 if (part_count < 1 || part_count > 256)
88                         return -1;
89
90                 if (id->partitions != NULL)
91                         free(id->partitions);
92                 id->partitions = malloc(part_count * sizeof(struct volume_id_partition));
93                 if (id->partitions == NULL)
94                         return -1;
95                 memset(id->partitions, 0x00, part_count * sizeof(struct volume_id_partition));
96
97                 id->partition_count = part_count;
98
99                 for (i = 0; i < part_count; i++) {
100                         uint64_t poff;
101                         uint64_t plen;
102
103                         buf = volume_id_get_buffer(id, off + ((i+1) * bsize), 0x200);
104                         if (buf == NULL)
105                                 return -1;
106
107                         part = (struct mac_partition *) buf;
108                         if (memcmp(part->signature, "PM", 2) != 0)
109                                 return -1;
110
111                         poff = be32_to_cpu(part->start_block) * bsize;
112                         plen = be32_to_cpu(part->block_count) * bsize;
113                         dbg("found '%s' partition entry at 0x%llx, len 0x%llx",
114                             part->type, (unsigned long long) poff, (unsigned long long) plen);
115
116                         id->partitions[i].off = poff;
117                         id->partitions[i].len = plen;
118
119                         if (memcmp(part->type, "Apple_Free", 10) == 0) {
120                                 volume_id_set_usage_part(&id->partitions[i], VOLUME_ID_UNUSED);
121                         } else if (memcmp(part->type, "Apple_partition_map", 19) == 0) {
122                                 volume_id_set_usage_part(&id->partitions[i], VOLUME_ID_PARTITIONTABLE);
123                         } else {
124                                 volume_id_set_usage_part(&id->partitions[i], VOLUME_ID_UNPROBED);
125                         }
126                 }
127                 volume_id_set_usage(id, VOLUME_ID_PARTITIONTABLE);
128                 id->type = "mac_partition_map";
129                 return 0;
130         }
131
132         return -1;
133 }