1 | /* ----------------------------------------------------------------------- * |
---|
2 | * |
---|
3 | * Copyright 1998-2011 H. Peter Anvin - All Rights Reserved |
---|
4 | * Copyright 2009-2011 Intel Corporation; author H. Peter Anvin |
---|
5 | * Copyright 2011 Paulo Alcantara <pcacjr@gmail.com> |
---|
6 | * |
---|
7 | * This program is free software; you can redistribute it and/or modify |
---|
8 | * it under the terms of the GNU General Public License as published by |
---|
9 | * the Free Software Foundation, Inc., 53 Temple Place Ste 330, |
---|
10 | * Boston MA 02111-1307, USA; either version 2 of the License, or |
---|
11 | * (at your option) any later version; incorporated herein by reference. |
---|
12 | * |
---|
13 | * ----------------------------------------------------------------------- */ |
---|
14 | |
---|
15 | /* |
---|
16 | * fs.c - Generic sanity check for FAT/NTFS-based installers |
---|
17 | */ |
---|
18 | |
---|
19 | #define _XOPEN_SOURCE 500 /* Required on glibc 2.x */ |
---|
20 | #define _BSD_SOURCE |
---|
21 | /* glibc 2.20 deprecates _BSD_SOURCE in favour of _DEFAULT_SOURCE */ |
---|
22 | #define _DEFAULT_SOURCE 1 |
---|
23 | #include <stdio.h> |
---|
24 | #include <inttypes.h> |
---|
25 | #include <string.h> |
---|
26 | #include <stddef.h> |
---|
27 | #include <stdlib.h> |
---|
28 | |
---|
29 | #include "syslinux.h" |
---|
30 | #include "syslxint.h" |
---|
31 | #include "syslxcom.h" |
---|
32 | #include "syslxfs.h" |
---|
33 | |
---|
34 | void syslinux_make_bootsect(void *bs, int fs_type) |
---|
35 | { |
---|
36 | if (fs_type == VFAT) { |
---|
37 | struct fat_boot_sector *bootsect = bs; |
---|
38 | const struct fat_boot_sector *sbs = |
---|
39 | (const struct fat_boot_sector *)boot_sector; |
---|
40 | |
---|
41 | memcpy(&bootsect->FAT_bsHead, &sbs->FAT_bsHead, FAT_bsHeadLen); |
---|
42 | memcpy(&bootsect->FAT_bsCode, &sbs->FAT_bsCode, FAT_bsCodeLen); |
---|
43 | } else if (fs_type == NTFS) { |
---|
44 | struct ntfs_boot_sector *bootsect = bs; |
---|
45 | const struct ntfs_boot_sector *sbs = |
---|
46 | (const struct ntfs_boot_sector *)boot_sector; |
---|
47 | |
---|
48 | memcpy(&bootsect->NTFS_bsHead, &sbs->NTFS_bsHead, NTFS_bsHeadLen); |
---|
49 | memcpy(&bootsect->NTFS_bsCode, &sbs->NTFS_bsCode, NTFS_bsCodeLen); |
---|
50 | } |
---|
51 | } |
---|
52 | |
---|
53 | static const char *check_fat_bootsect(const void *bs, int *fs_type) |
---|
54 | { |
---|
55 | int sectorsize; |
---|
56 | const struct fat_boot_sector *sectbuf = bs; |
---|
57 | long long sectors, fatsectors, dsectors; |
---|
58 | long long clusters; |
---|
59 | int rootdirents, clustersize; |
---|
60 | |
---|
61 | sectorsize = get_16(§buf->bsBytesPerSec); |
---|
62 | |
---|
63 | clustersize = get_8(§buf->bsSecPerClust); |
---|
64 | if (clustersize == 0 || (clustersize & (clustersize - 1))) |
---|
65 | return "impossible cluster size on an FAT volume"; |
---|
66 | |
---|
67 | sectors = get_16(§buf->bsSectors); |
---|
68 | sectors = sectors ? sectors : get_32(§buf->bsHugeSectors); |
---|
69 | |
---|
70 | dsectors = sectors - get_16(§buf->bsResSectors); |
---|
71 | |
---|
72 | fatsectors = get_16(§buf->bsFATsecs); |
---|
73 | fatsectors = fatsectors ? fatsectors : get_32(§buf->bs32.FATSz32); |
---|
74 | fatsectors *= get_8(§buf->bsFATs); |
---|
75 | dsectors -= fatsectors; |
---|
76 | |
---|
77 | rootdirents = get_16(§buf->bsRootDirEnts); |
---|
78 | dsectors -= (rootdirents + sectorsize / 32 - 1) / sectorsize; |
---|
79 | |
---|
80 | if (dsectors < 0) |
---|
81 | return "negative number of data sectors on an FAT volume"; |
---|
82 | |
---|
83 | clusters = dsectors / clustersize; |
---|
84 | |
---|
85 | fatsectors = get_16(§buf->bsFATsecs); |
---|
86 | fatsectors = fatsectors ? fatsectors : get_32(§buf->bs32.FATSz32); |
---|
87 | fatsectors *= get_8(§buf->bsFATs); |
---|
88 | |
---|
89 | if (!fatsectors) |
---|
90 | return "zero FAT sectors"; |
---|
91 | |
---|
92 | if (clusters < 0xFFF5) { |
---|
93 | /* FAT12 or FAT16 */ |
---|
94 | if (!get_16(§buf->bsFATsecs)) |
---|
95 | return "zero FAT sectors (FAT12/16)"; |
---|
96 | |
---|
97 | if (get_8(§buf->bs16.BootSignature) == 0x29) { |
---|
98 | if (!memcmp(§buf->bs16.FileSysType, "FAT12 ", 8)) { |
---|
99 | if (clusters >= 0xFF5) |
---|
100 | return "more than 4084 clusters but claims FAT12"; |
---|
101 | } else if (!memcmp(§buf->bs16.FileSysType, "FAT16 ", 8)) { |
---|
102 | if (clusters < 0xFF5) |
---|
103 | return "less than 4084 clusters but claims FAT16"; |
---|
104 | } else if (!memcmp(§buf->bs16.FileSysType, "FAT32 ", 8)) { |
---|
105 | return "less than 65525 clusters but claims FAT32"; |
---|
106 | } else if (memcmp(§buf->bs16.FileSysType, "FAT ", 8)) { |
---|
107 | static char fserr[] = "filesystem type \"????????\" not " |
---|
108 | "supported"; |
---|
109 | memcpy(fserr + 17, §buf->bs16.FileSysType, 8); |
---|
110 | return fserr; |
---|
111 | } |
---|
112 | } |
---|
113 | } else if (clusters < 0x0FFFFFF5) { |
---|
114 | /* |
---|
115 | * FAT32... |
---|
116 | * |
---|
117 | * Moving the FileSysType and BootSignature was a lovely stroke |
---|
118 | * of M$ idiocy... |
---|
119 | */ |
---|
120 | if (get_8(§buf->bs32.BootSignature) != 0x29 || |
---|
121 | memcmp(§buf->bs32.FileSysType, "FAT32 ", 8)) |
---|
122 | return "missing FAT32 signature"; |
---|
123 | } else { |
---|
124 | return "impossibly large number of clusters on an FAT volume"; |
---|
125 | } |
---|
126 | |
---|
127 | if (fs_type) |
---|
128 | *fs_type = VFAT; |
---|
129 | |
---|
130 | return NULL; |
---|
131 | } |
---|
132 | |
---|
133 | static const char *check_ntfs_bootsect(const void *bs, int *fs_type) |
---|
134 | { |
---|
135 | const struct ntfs_boot_sector *sectbuf = bs; |
---|
136 | |
---|
137 | if (memcmp(§buf->bsOemName, "NTFS ", 8) && |
---|
138 | memcmp(§buf->bsOemName, "MSWIN4.0", 8) && |
---|
139 | memcmp(§buf->bsOemName, "MSWIN4.1", 8)) |
---|
140 | return "unknown OEM name but claims NTFS"; |
---|
141 | |
---|
142 | if (fs_type) |
---|
143 | *fs_type = NTFS; |
---|
144 | |
---|
145 | return NULL; |
---|
146 | } |
---|
147 | |
---|
148 | const char *syslinux_check_bootsect(const void *bs, int *fs_type) |
---|
149 | { |
---|
150 | uint8_t media_sig; |
---|
151 | int sectorsize; |
---|
152 | const struct fat_boot_sector *sectbuf = bs; |
---|
153 | const char *retval; |
---|
154 | |
---|
155 | media_sig = get_8(§buf->bsMedia); |
---|
156 | /* Must be 0xF0 or 0xF8-0xFF for FAT/NTFS volumes */ |
---|
157 | if (media_sig != 0xF0 && media_sig < 0xF8) |
---|
158 | return "invalid media signature (not an FAT/NTFS volume?)"; |
---|
159 | |
---|
160 | sectorsize = get_16(§buf->bsBytesPerSec); |
---|
161 | if (sectorsize == SECTOR_SIZE) ; /* ok */ |
---|
162 | else if (sectorsize >= 512 && sectorsize <= 4096 && |
---|
163 | (sectorsize & (sectorsize - 1)) == 0) |
---|
164 | return "unsupported sectors size"; |
---|
165 | else |
---|
166 | return "impossible sector size"; |
---|
167 | |
---|
168 | if (ntfs_check_zero_fields((struct ntfs_boot_sector *)bs)) |
---|
169 | retval = check_ntfs_bootsect(bs, fs_type); |
---|
170 | else |
---|
171 | retval = check_fat_bootsect(bs, fs_type); |
---|
172 | |
---|
173 | return retval; |
---|
174 | } |
---|