Add default Smack manifest for util-linux-ng.spec
[framework/base/util-linux-ng.git] / shlibs / blkid / src / probers / swap.c
1 /*
2  * Copyright (C) 1999 by Andries Brouwer
3  * Copyright (C) 1999, 2000, 2003 by Theodore Ts'o
4  * Copyright (C) 2001 by Andreas Dilger
5  * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
6  * Copyright (C) 2008 Karel Zak <kzak@redhat.com>
7  *
8  * This file may be redistributed under the terms of the
9  * GNU Lesser General Public License.
10  */
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <unistd.h>
14 #include <string.h>
15 #include <errno.h>
16 #include <ctype.h>
17 #include <stdint.h>
18
19 #include "blkidP.h"
20
21 /* linux-2.6/include/linux/swap.h */
22 struct swap_header_v1_2 {
23      /* char            bootbits[1024]; */ /* Space for disklabel etc. */
24         uint32_t        version;
25         uint32_t        lastpage;
26         uint32_t        nr_badpages;
27         unsigned char   uuid[16];
28         unsigned char   volume[16];
29         uint32_t        padding[117];
30         uint32_t        badpages[1];
31 } __attribute__((packed));
32
33 #define PAGESIZE_MIN    0xff6   /* 4086 (arm, i386, ...) */
34 #define PAGESIZE_MAX    0xfff6  /* 65526 (ia64) */
35
36
37 static int swap_set_info(blkid_probe pr, const char *version)
38 {
39         struct swap_header_v1_2 *hdr;
40
41         /* Swap header always located at offset of 1024 bytes */
42         hdr = (struct swap_header_v1_2 *) blkid_probe_get_buffer(pr, 1024,
43                                 sizeof(struct swap_header_v1_2));
44         if (!hdr)
45                 return -1;
46
47         /* SWAPSPACE2 - check for wrong version or zeroed pagecount */
48         if (strcmp(version, "2") == 0 &&
49             (hdr->version != 1 || hdr->lastpage == 0))
50                 return -1;
51
52         /* arbitrary sanity check.. is there any garbage down there? */
53         if (hdr->padding[32] == 0 && hdr->padding[33] == 0) {
54                 if (hdr->volume[0] && blkid_probe_set_label(pr, hdr->volume,
55                                 sizeof(hdr->volume)) < 0)
56                         return -1;
57                 if (hdr->uuid[0] && blkid_probe_set_uuid(pr, hdr->uuid) < 0)
58                         return -1;
59         }
60
61         blkid_probe_set_version(pr, version);
62         return 0;
63 }
64
65 static int probe_swap(blkid_probe pr, const struct blkid_idmag *mag)
66 {
67         if (!mag)
68                 return -1;
69
70         if (!memcmp(mag->magic, "SWAP-SPACE", mag->len)) {
71                 /* swap v0 doesn't support LABEL or UUID */
72                 blkid_probe_set_version(pr, "1");
73                 return 0;
74
75         } else if (!memcmp(mag->magic, "SWAPSPACE2", mag->len))
76                 return swap_set_info(pr, "2");
77
78         return -1;
79 }
80
81 static int probe_swsuspend(blkid_probe pr, const struct blkid_idmag *mag)
82 {
83         if (!mag)
84                 return -1;
85         if (!memcmp(mag->magic, "S1SUSPEND", mag->len))
86                 return swap_set_info(pr, "s1suspend");
87         if (!memcmp(mag->magic, "S2SUSPEND", mag->len))
88                 return swap_set_info(pr, "s2suspend");
89         if (!memcmp(mag->magic, "ULSUSPEND", mag->len))
90                 return swap_set_info(pr, "ulsuspend");
91         if (!memcmp(mag->magic, "\xed\xc3\x02\xe9\x98\x56\xe5\x0c", mag->len))
92                 return swap_set_info(pr, "tuxonice");
93
94         return -1;      /* no signature detected */
95 }
96
97 const struct blkid_idinfo swap_idinfo =
98 {
99         .name           = "swap",
100         .usage          = BLKID_USAGE_OTHER,
101         .probefunc      = probe_swap,
102         .magics         =
103         {
104                 { "SWAP-SPACE", 10, 0,  0xff6 },
105                 { "SWAPSPACE2", 10, 0,  0xff6 },
106                 { "SWAP-SPACE", 10, 0, 0x1ff6 },
107                 { "SWAPSPACE2", 10, 0, 0x1ff6 },
108                 { "SWAP-SPACE", 10, 0, 0x3ff6 },
109                 { "SWAPSPACE2", 10, 0, 0x3ff6 },
110                 { "SWAP-SPACE", 10, 0, 0x7ff6 },
111                 { "SWAPSPACE2", 10, 0, 0x7ff6 },
112                 { "SWAP-SPACE", 10, 0, 0xfff6 },
113                 { "SWAPSPACE2", 10, 0, 0xfff6 },
114                 { NULL }
115         }
116 };
117
118
119 const struct blkid_idinfo swsuspend_idinfo =
120 {
121         .name           = "swsuspend",
122         .usage          = BLKID_USAGE_OTHER,
123         .probefunc      = probe_swsuspend,
124         .magics         =
125         {
126                 { "S1SUSPEND", 9, 0, 0xff6 },
127                 { "S2SUSPEND", 9, 0, 0xff6 },
128                 { "ULSUSPEND", 9, 0, 0xff6 },
129                 { "\xed\xc3\x02\xe9\x98\x56\xe5\x0c", 8, 0, 0xff6 },
130                 { "S1SUSPEND", 9, 0, 0x1ff6 },
131                 { "S2SUSPEND", 9, 0, 0x1ff6 },
132                 { "ULSUSPEND", 9, 0, 0x1ff6 },
133                 { "\xed\xc3\x02\xe9\x98\x56\xe5\x0c", 8, 0, 0x1ff6 },
134                 { "S1SUSPEND", 9, 0, 0x3ff6 },
135                 { "S2SUSPEND", 9, 0, 0x3ff6 },
136                 { "ULSUSPEND", 9, 0, 0x3ff6 },
137                 { "\xed\xc3\x02\xe9\x98\x56\xe5\x0c", 8, 0, 0x3ff6 },
138                 { "S1SUSPEND", 9, 0, 0x7ff6 },
139                 { "S2SUSPEND", 9, 0, 0x7ff6 },
140                 { "ULSUSPEND", 9, 0, 0x7ff6 },
141                 { "\xed\xc3\x02\xe9\x98\x56\xe5\x0c", 8, 0, 0x7ff6 },
142                 { "S1SUSPEND", 9, 0, 0xfff6 },
143                 { "S2SUSPEND", 9, 0, 0xfff6 },
144                 { "ULSUSPEND", 9, 0, 0xfff6 },
145                 { "\xed\xc3\x02\xe9\x98\x56\xe5\x0c", 8, 0, 0xfff6 },
146                 { NULL }
147         }
148 };
149