chiark / gitweb /
add "Persistent Device Naming" rules file for disks
[elogind.git] / extras / volume_id / volume_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 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 #ifndef _GNU_SOURCE
22 #define _GNU_SOURCE 1
23 #endif
24
25 #ifdef HAVE_CONFIG_H
26 #  include <config.h>
27 #endif
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <string.h>
33 #include <errno.h>
34 #include <ctype.h>
35 #include <asm/types.h>
36
37 #include "volume_id.h"
38 #include "logging.h"
39 #include "util.h"
40 #include "mac.h"
41
42 struct mac_driver_desc {
43         uint8_t         signature[2];
44         uint16_t        block_size;
45         uint32_t        block_count;
46 } __attribute__((__packed__));
47
48 struct mac_partition {
49         uint8_t         signature[2];
50         uint16_t        res1;
51         uint32_t        map_count;
52         uint32_t        start_block;
53         uint32_t        block_count;
54         uint8_t         name[32];
55         uint8_t         type[32];
56 } __attribute__((__packed__));
57
58 int volume_id_probe_mac_partition_map(struct volume_id *id, uint64_t off)
59 {
60         const uint8_t *buf;
61         struct mac_driver_desc *driver;
62         struct mac_partition *part;
63
64         dbg("probing at offset 0x%llx", (unsigned long long) off);
65
66         buf = volume_id_get_buffer(id, off, 0x200);
67         if (buf == NULL)
68                 return -1;
69
70         part = (struct mac_partition *) buf;
71         if ((memcmp(part->signature, "PM", 2) == 0) &&
72             (memcmp(part->type, "Apple_partition_map", 19) == 0)) {
73                 /* linux creates an own subdevice for the map
74                  * just return the type if the drive header is missing */
75                 volume_id_set_usage(id, VOLUME_ID_PARTITIONTABLE);
76                 id->type = "mac_partition_map";
77                 return 0;
78         }
79
80         driver = (struct mac_driver_desc *) buf;
81         if (memcmp(driver->signature, "ER", 2) == 0) {
82                 /* we are on a main device, like a CD
83                  * just try to probe the first partition from the map */
84                 unsigned int bsize = be16_to_cpu(driver->block_size);
85                 int part_count;
86                 int i;
87
88                 /* get first entry of partition table */
89                 buf = volume_id_get_buffer(id, off +  bsize, 0x200);
90                 if (buf == NULL)
91                         return -1;
92
93                 part = (struct mac_partition *) buf;
94                 if (memcmp(part->signature, "PM", 2) != 0)
95                         return -1;
96
97                 part_count = be32_to_cpu(part->map_count);
98                 dbg("expecting %d partition entries", part_count);
99
100                 if (id->partitions != NULL)
101                         free(id->partitions);
102                 id->partitions =
103                         malloc(part_count * sizeof(struct volume_id_partition));
104                 if (id->partitions == NULL)
105                         return -1;
106                 memset(id->partitions, 0x00, sizeof(struct volume_id_partition));
107
108                 id->partition_count = part_count;
109
110                 for (i = 0; i < part_count; i++) {
111                         uint64_t poff;
112                         uint64_t plen;
113
114                         buf = volume_id_get_buffer(id, off + ((i+1) * bsize), 0x200);
115                         if (buf == NULL)
116                                 return -1;
117
118                         part = (struct mac_partition *) buf;
119                         if (memcmp(part->signature, "PM", 2) != 0)
120                                 return -1;
121
122                         poff = be32_to_cpu(part->start_block) * bsize;
123                         plen = be32_to_cpu(part->block_count) * bsize;
124                         dbg("found '%s' partition entry at 0x%llx, len 0x%llx",
125                             part->type, (unsigned long long) poff, (unsigned long long) plen);
126
127                         id->partitions[i].off = poff;
128                         id->partitions[i].len = plen;
129
130                         if (memcmp(part->type, "Apple_Free", 10) == 0) {
131                                 volume_id_set_usage_part(&id->partitions[i], VOLUME_ID_UNUSED);
132                         } else if (memcmp(part->type, "Apple_partition_map", 19) == 0) {
133                                 volume_id_set_usage_part(&id->partitions[i], VOLUME_ID_PARTITIONTABLE);
134                         } else {
135                                 volume_id_set_usage_part(&id->partitions[i], VOLUME_ID_UNPROBED);
136                         }
137                 }
138                 volume_id_set_usage(id, VOLUME_ID_PARTITIONTABLE);
139                 id->type = "mac_partition_map";
140                 return 0;
141         }
142
143         return -1;
144 }