1 /* ----------------------------------------------------------------------- *
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>
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.
13 * ----------------------------------------------------------------------- */
16 * fs.c - Generic sanity check for FAT/NTFS-based installers
19 #define _XOPEN_SOURCE 500 /* Required on glibc 2.x */
21 /* glibc 2.20 deprecates _BSD_SOURCE in favour of _DEFAULT_SOURCE */
22 #define _DEFAULT_SOURCE 1
34 void syslinux_make_bootsect(void *bs
, int fs_type
)
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
;
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
;
48 memcpy(&bootsect
->NTFS_bsHead
, &sbs
->NTFS_bsHead
, NTFS_bsHeadLen
);
49 memcpy(&bootsect
->NTFS_bsCode
, &sbs
->NTFS_bsCode
, NTFS_bsCodeLen
);
53 static const char *check_fat_bootsect(const void *bs
, int *fs_type
)
56 const struct fat_boot_sector
*sectbuf
= bs
;
57 long long sectors
, fatsectors
, dsectors
;
59 int rootdirents
, clustersize
;
61 sectorsize
= get_16(§buf
->bsBytesPerSec
);
63 clustersize
= get_8(§buf
->bsSecPerClust
);
64 if (clustersize
== 0 || (clustersize
& (clustersize
- 1)))
65 return "impossible cluster size on an FAT volume";
67 sectors
= get_16(§buf
->bsSectors
);
68 sectors
= sectors
? sectors
: get_32(§buf
->bsHugeSectors
);
70 dsectors
= sectors
- get_16(§buf
->bsResSectors
);
72 fatsectors
= get_16(§buf
->bsFATsecs
);
73 fatsectors
= fatsectors
? fatsectors
: get_32(§buf
->bs32
.FATSz32
);
74 fatsectors
*= get_8(§buf
->bsFATs
);
75 dsectors
-= fatsectors
;
77 rootdirents
= get_16(§buf
->bsRootDirEnts
);
78 dsectors
-= (rootdirents
+ sectorsize
/ 32 - 1) / sectorsize
;
81 return "negative number of data sectors on an FAT volume";
83 clusters
= dsectors
/ clustersize
;
85 fatsectors
= get_16(§buf
->bsFATsecs
);
86 fatsectors
= fatsectors
? fatsectors
: get_32(§buf
->bs32
.FATSz32
);
87 fatsectors
*= get_8(§buf
->bsFATs
);
90 return "zero FAT sectors";
92 if (clusters
< 0xFFF5) {
94 if (!get_16(§buf
->bsFATsecs
))
95 return "zero FAT sectors (FAT12/16)";
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 "
109 memcpy(fserr
+ 17, §buf
->bs16
.FileSysType
, 8);
113 } else if (clusters
< 0x0FFFFFF5) {
117 * Moving the FileSysType and BootSignature was a lovely stroke
120 if (get_8(§buf
->bs32
.BootSignature
) != 0x29 ||
121 memcmp(§buf
->bs32
.FileSysType
, "FAT32 ", 8))
122 return "missing FAT32 signature";
124 return "impossibly large number of clusters on an FAT volume";
133 static const char *check_ntfs_bootsect(const void *bs
, int *fs_type
)
135 const struct ntfs_boot_sector
*sectbuf
= bs
;
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";
148 const char *syslinux_check_bootsect(const void *bs
, int *fs_type
)
152 const struct fat_boot_sector
*sectbuf
= bs
;
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?)";
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";
166 return "impossible sector size";
168 if (ntfs_check_zero_fields((struct ntfs_boot_sector
*)bs
))
169 retval
= check_ntfs_bootsect(bs
, fs_type
);
171 retval
= check_fat_bootsect(bs
, fs_type
);