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 */
32 void syslinux_make_bootsect(void *bs
, int fs_type
)
34 if (fs_type
== VFAT
) {
35 struct fat_boot_sector
*bootsect
= bs
;
36 const struct fat_boot_sector
*sbs
=
37 (const struct fat_boot_sector
*)boot_sector
;
39 memcpy(&bootsect
->FAT_bsHead
, &sbs
->FAT_bsHead
, FAT_bsHeadLen
);
40 memcpy(&bootsect
->FAT_bsCode
, &sbs
->FAT_bsCode
, FAT_bsCodeLen
);
41 } else if (fs_type
== NTFS
) {
42 struct ntfs_boot_sector
*bootsect
= bs
;
43 const struct ntfs_boot_sector
*sbs
=
44 (const struct ntfs_boot_sector
*)boot_sector
;
46 memcpy(&bootsect
->NTFS_bsHead
, &sbs
->NTFS_bsHead
, NTFS_bsHeadLen
);
47 memcpy(&bootsect
->NTFS_bsCode
, &sbs
->NTFS_bsCode
, NTFS_bsCodeLen
);
51 static const char *check_fat_bootsect(const void *bs
, int *fs_type
)
54 const struct fat_boot_sector
*sectbuf
= bs
;
55 long long sectors
, fatsectors
, dsectors
;
57 int rootdirents
, clustersize
;
59 sectorsize
= get_16(§buf
->bsBytesPerSec
);
61 clustersize
= get_8(§buf
->bsSecPerClust
);
62 if (clustersize
== 0 || (clustersize
& (clustersize
- 1)))
63 return "impossible cluster size on an FAT volume";
65 sectors
= get_16(§buf
->bsSectors
);
66 sectors
= sectors
? sectors
: get_32(§buf
->bsHugeSectors
);
68 dsectors
= sectors
- get_16(§buf
->bsResSectors
);
70 fatsectors
= get_16(§buf
->bsFATsecs
);
71 fatsectors
= fatsectors
? fatsectors
: get_32(§buf
->bs32
.FATSz32
);
72 fatsectors
*= get_8(§buf
->bsFATs
);
73 dsectors
-= fatsectors
;
75 rootdirents
= get_16(§buf
->bsRootDirEnts
);
76 dsectors
-= (rootdirents
+ sectorsize
/ 32 - 1) / sectorsize
;
79 return "negative number of data sectors on an FAT volume";
81 clusters
= dsectors
/ clustersize
;
83 fatsectors
= get_16(§buf
->bsFATsecs
);
84 fatsectors
= fatsectors
? fatsectors
: get_32(§buf
->bs32
.FATSz32
);
85 fatsectors
*= get_8(§buf
->bsFATs
);
88 return "zero FAT sectors";
90 if (clusters
< 0xFFF5) {
92 if (!get_16(§buf
->bsFATsecs
))
93 return "zero FAT sectors (FAT12/16)";
95 if (get_8(§buf
->bs16
.BootSignature
) == 0x29) {
96 if (!memcmp(§buf
->bs16
.FileSysType
, "FAT12 ", 8)) {
97 if (clusters
>= 0xFF5)
98 return "more than 4084 clusters but claims FAT12";
99 } else if (!memcmp(§buf
->bs16
.FileSysType
, "FAT16 ", 8)) {
100 if (clusters
< 0xFF5)
101 return "less than 4084 clusters but claims FAT16";
102 } else if (!memcmp(§buf
->bs16
.FileSysType
, "FAT32 ", 8)) {
103 return "less than 65525 clusters but claims FAT32";
104 } else if (memcmp(§buf
->bs16
.FileSysType
, "FAT ", 8)) {
105 static char fserr
[] = "filesystem type \"????????\" not "
107 memcpy(fserr
+ 17, §buf
->bs16
.FileSysType
, 8);
111 } else if (clusters
< 0x0FFFFFF5) {
115 * Moving the FileSysType and BootSignature was a lovely stroke
118 if (get_8(§buf
->bs32
.BootSignature
) != 0x29 ||
119 memcmp(§buf
->bs32
.FileSysType
, "FAT32 ", 8))
120 return "missing FAT32 signature";
122 return "impossibly large number of clusters on an FAT volume";
131 static const char *check_ntfs_bootsect(const void *bs
, int *fs_type
)
133 const struct ntfs_boot_sector
*sectbuf
= bs
;
135 if (memcmp(§buf
->bsOemName
, "NTFS ", 8) &&
136 memcmp(§buf
->bsOemName
, "MSWIN4.0", 8) &&
137 memcmp(§buf
->bsOemName
, "MSWIN4.1", 8))
138 return "unknown OEM name but claims NTFS";
146 const char *syslinux_check_bootsect(const void *bs
, int *fs_type
)
150 const struct fat_boot_sector
*sectbuf
= bs
;
153 media_sig
= get_8(§buf
->bsMedia
);
154 /* Must be 0xF0 or 0xF8-0xFF for FAT/NTFS volumes */
155 if (media_sig
!= 0xF0 && media_sig
< 0xF8)
156 return "invalid media signature (not an FAT/NTFS volume?)";
158 sectorsize
= get_16(§buf
->bsBytesPerSec
);
159 if (sectorsize
== SECTOR_SIZE
) ; /* ok */
160 else if (sectorsize
>= 512 && sectorsize
<= 4096 &&
161 (sectorsize
& (sectorsize
- 1)) == 0)
162 return "unsupported sectors size";
164 return "impossible sector size";
166 if (ntfs_check_zero_fields((struct ntfs_boot_sector
*)bs
))
167 retval
= check_ntfs_bootsect(bs
, fs_type
);
169 retval
= check_fat_bootsect(bs
, fs_type
);