chiark / gitweb /
volume_id: use PRIu64i, PRIx64 macros
[elogind.git] / extras / volume_id / lib / hpfs.c
1 /*
2  * volume_id - reads filesystem 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
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program 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
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #ifndef _GNU_SOURCE
21 #define _GNU_SOURCE 1
22 #endif
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <ctype.h>
30
31 #include "libvolume_id.h"
32 #include "libvolume_id-private.h"
33
34 struct hpfs_boot_block
35 {
36         uint8_t         jmp[3];
37         uint8_t         oem_id[8];
38         uint8_t         bytes_per_sector[2];
39         uint8_t         sectors_per_cluster;
40         uint8_t         n_reserved_sectors[2];
41         uint8_t         n_fats;
42         uint8_t         n_rootdir_entries[2];
43         uint8_t         n_sectors_s[2];
44         uint8_t         media_byte;
45         uint16_t        sectors_per_fat;
46         uint16_t        sectors_per_track;
47         uint16_t        heads_per_cyl;
48         uint32_t        n_hidden_sectors;
49         uint32_t        n_sectors_l;
50         uint8_t         drive_number;
51         uint8_t         mbz;
52         uint8_t         sig_28h;
53         uint8_t         vol_serno[4];
54         uint8_t         vol_label[11];
55         uint8_t         sig_hpfs[8];
56         uint8_t         pad[448];
57         uint8_t         magic[2];
58 } PACKED;
59
60 struct hpfs_super
61 {
62         uint8_t         magic[4];
63         uint8_t         magic1[4];
64         uint8_t         version;
65 } PACKED;
66
67
68 struct hpfs_spare_super
69 {
70         uint8_t         magic[4];
71         uint8_t         magic1[4];
72 } PACKED;
73
74 #define HPFS_SUPERBLOCK_OFFSET                  0x2000
75 #define HPFS_SUPERBLOCK_SPARE_OFFSET            0x2200
76
77 int volume_id_probe_hpfs(struct volume_id *id, uint64_t off, uint64_t size)
78 {
79         struct hpfs_super *hs;
80         struct hpfs_spare_super *hss;
81         struct hpfs_boot_block *hbb;
82
83         info("probing at offset 0x%" PRIx64 "\n", off);
84
85         hs = (struct hpfs_super *) volume_id_get_buffer(id, off + HPFS_SUPERBLOCK_OFFSET, 0x400);
86         if (hs == NULL)
87                 return -1;
88         if (memcmp(hs->magic, "\x49\xe8\x95\xf9", 4) != 0)
89                 return -1;
90
91         hss = (struct hpfs_spare_super *) volume_id_get_buffer(id, off + HPFS_SUPERBLOCK_SPARE_OFFSET, 0x200);
92         if (hss == NULL)
93                 return -1;
94         if (memcmp(hss->magic, "\x49\x18\x91\xf9", 4) != 0)
95                 return -1;
96
97         sprintf(id->type_version, "%u", hs->version);
98         volume_id_set_usage(id, VOLUME_ID_FILESYSTEM);
99         id->type = "hpfs";
100
101         /* if boot block looks valid, read label and uuid from there */
102         hbb = (struct hpfs_boot_block *) volume_id_get_buffer(id, off, 0x200);
103         if (hs == NULL)
104                 return -1;
105         if (memcmp(hbb->magic, "\x55\xaa", 2) == 0 &&
106             memcmp(hbb->sig_hpfs, "HPFS", 4) == 0 &&
107             hbb->sig_28h == 0x28) {
108                 volume_id_set_label_raw(id, hbb->vol_label, 11);
109                 volume_id_set_label_string(id, hbb->vol_label, 11);
110                 volume_id_set_uuid(id, hbb->vol_serno, 0, UUID_DOS);
111         }
112
113         return 0;
114 }