chiark / gitweb /
libvolume_id: fat - check for signature at end of sector
[elogind.git] / extras / volume_id / lib / volume_id.c
1 /*
2  * volume_id - reads volume label and uuid
3  *
4  * Copyright (C) 2005 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 #include <fcntl.h>
26 #include <sys/stat.h>
27
28 #include "libvolume_id.h"
29 #include "util.h"
30
31 /* the user can overwrite this log function */
32 static void default_log(int priority, const char *file, int line, const char *format, ...)
33 {
34         return;
35 }
36
37 volume_id_log_fn_t volume_id_log_fn = default_log;
38
39 int volume_id_probe_raid(struct volume_id *id, uint64_t off, uint64_t size)
40 {
41         if (id == NULL)
42                 return -EINVAL;
43
44         info("probing at offset 0x%llx, size 0x%llx",
45             (unsigned long long) off, (unsigned long long) size);
46
47         /* probe for raid first, because fs probes may be successful on raid members */
48         if (size) {
49                 if (volume_id_probe_linux_raid(id, off, size) == 0)
50                         goto found;
51
52                 if (volume_id_probe_intel_software_raid(id, off, size) == 0)
53                         goto found;
54
55                 if (volume_id_probe_lsi_mega_raid(id, off, size) == 0)
56                         goto found;
57
58                 if (volume_id_probe_via_raid(id, off, size) == 0)
59                         goto found;
60
61                 if (volume_id_probe_silicon_medley_raid(id, off, size) == 0)
62                         goto found;
63
64                 if (volume_id_probe_nvidia_raid(id, off, size) == 0)
65                         goto found;
66
67                 if (volume_id_probe_promise_fasttrack_raid(id, off, size) == 0)
68                         goto found;
69
70                 if (volume_id_probe_highpoint_45x_raid(id, off, size) == 0)
71                         goto found;
72         }
73
74         if (volume_id_probe_lvm1(id, off) == 0)
75                 goto found;
76
77         if (volume_id_probe_lvm2(id, off) == 0)
78                 goto found;
79
80         if (volume_id_probe_highpoint_37x_raid(id, off) == 0)
81                 goto found;
82
83         return -1;
84
85 found:
86         /* If recognized, we free the allocated buffers */
87         volume_id_free_buffer(id);
88         return 0;
89 }
90
91 int volume_id_probe_filesystem(struct volume_id *id, uint64_t off, uint64_t size)
92 {
93         if (id == NULL)
94                 return -EINVAL;
95
96         info("probing at offset 0x%llx, size 0x%llx",
97             (unsigned long long) off, (unsigned long long) size);
98
99         if (volume_id_probe_luks(id, off) == 0)
100                 goto found;
101
102         if (volume_id_probe_vfat(id, off) == 0)
103                 goto found;
104
105         if (volume_id_probe_xfs(id, off) == 0)
106                 goto found;
107
108         /* fill buffer with maximum */
109         volume_id_get_buffer(id, 0, SB_BUFFER_SIZE);
110
111         if (volume_id_probe_linux_swap(id, off) == 0)
112                 goto found;
113
114         if (volume_id_probe_ext(id, off) == 0)
115                 goto found;
116
117         if (volume_id_probe_reiserfs(id, off) == 0)
118                 goto found;
119
120         if (volume_id_probe_jfs(id, off) == 0)
121                 goto found;
122
123         if (volume_id_probe_udf(id, off) == 0)
124                 goto found;
125
126         if (volume_id_probe_iso9660(id, off) == 0)
127                 goto found;
128
129         if (volume_id_probe_hfs_hfsplus(id, off) == 0)
130                 goto found;
131
132         if (volume_id_probe_ufs(id, off) == 0)
133                 goto found;
134
135         if (volume_id_probe_ntfs(id, off)  == 0)
136                 goto found;
137
138         if (volume_id_probe_cramfs(id, off) == 0)
139                 goto found;
140
141         if (volume_id_probe_romfs(id, off) == 0)
142                 goto found;
143
144         if (volume_id_probe_hpfs(id, off) == 0)
145                 goto found;
146
147         if (volume_id_probe_sysv(id, off) == 0)
148                 goto found;
149
150         if (volume_id_probe_minix(id, off) == 0)
151                 goto found;
152
153         if (volume_id_probe_ocfs1(id, off) == 0)
154                 goto found;
155
156         if (volume_id_probe_ocfs2(id, off) == 0)
157                 goto found;
158
159         if (volume_id_probe_vxfs(id, off) == 0)
160                 goto found;
161
162         if (volume_id_probe_squashfs(id, off) == 0)
163                 goto found;
164
165         if (volume_id_probe_netware(id, off) == 0)
166                 goto found;
167
168         return -1;
169
170 found:
171         /* If recognized, we free the allocated buffers */
172         volume_id_free_buffer(id);
173         return 0;
174 }
175
176 int volume_id_probe_all(struct volume_id *id, uint64_t off, uint64_t size)
177 {
178         if (id == NULL)
179                 return -EINVAL;
180
181         if (volume_id_probe_raid(id, off, size) == 0)
182                 return 0;
183
184         if (volume_id_probe_filesystem(id, off, size) == 0)
185                 return 0;
186
187         return -1;
188 }
189
190 /* open volume by already open file descriptor */
191 struct volume_id *volume_id_open_fd(int fd)
192 {
193         struct volume_id *id;
194
195         id = malloc(sizeof(struct volume_id));
196         if (id == NULL)
197                 return NULL;
198         memset(id, 0x00, sizeof(struct volume_id));
199
200         id->fd = fd;
201
202         return id;
203 }
204
205 /* open volume by device node */
206 struct volume_id *volume_id_open_node(const char *path)
207 {
208         struct volume_id *id;
209         int fd;
210
211         fd = open(path, O_RDONLY);
212         if (fd < 0) {
213                 dbg("unable to open '%s'", path);
214                 return NULL;
215         }
216
217         id = volume_id_open_fd(fd);
218         if (id == NULL)
219                 return NULL;
220
221         /* close fd on device close */
222         id->fd_close = 1;
223
224         return id;
225 }
226
227 void volume_id_close(struct volume_id *id)
228 {
229         if (id == NULL)
230                 return;
231
232         if (id->fd_close != 0)
233                 close(id->fd);
234
235         volume_id_free_buffer(id);
236
237         free(id);
238 }