1 /* ----------------------------------------------------------------------- *
3 * Copyright 1998-2008 H. Peter Anvin - All Rights Reserved
4 * Copyright 2009-2010 Intel Corporation; author H. Peter Anvin
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, Inc., 53 Temple Place Ste 330,
9 * Boston MA 02111-1307, USA; either version 2 of the License, or
10 * (at your option) any later version; incorporated herein by reference.
12 * ----------------------------------------------------------------------- */
15 * fat.c - Initial sanity check for FAT-based installers
18 #define _XOPEN_SOURCE 500 /* Required on glibc 2.x */
29 void syslinux_make_bootsect(void *bs
)
31 struct boot_sector
*bootsect
= bs
;
32 const struct boot_sector
*sbs
=
33 (const struct boot_sector
*)boot_sector
;
35 memcpy(&bootsect
->bsHead
, &sbs
->bsHead
, bsHeadLen
);
36 memcpy(&bootsect
->bsCode
, &sbs
->bsCode
, bsCodeLen
);
40 * Check to see that what we got was indeed an MS-DOS boot sector/superblock;
41 * Return NULL if OK and otherwise an error message;
43 const char *syslinux_check_bootsect(const void *bs
)
47 long long sectors
, fatsectors
, dsectors
;
49 int rootdirents
, clustersize
;
50 const struct boot_sector
*sectbuf
= bs
;
54 /* Must be 0xF0 or 0xF8..0xFF */
55 if (get_8(§buf
->bsMedia
) != 0xF0 && get_8(§buf
->bsMedia
) < 0xF8)
56 return "invalid media signature (not a FAT filesystem?)";
58 sectorsize
= get_16(§buf
->bsBytesPerSec
);
59 if (sectorsize
== SECTOR_SIZE
)
61 else if (sectorsize
>= 512 && sectorsize
<= 4096 &&
62 (sectorsize
& (sectorsize
- 1)) == 0)
63 return "unsupported sectors size";
65 return "impossible sector size";
67 clustersize
= get_8(§buf
->bsSecPerClust
);
68 if (clustersize
== 0 || (clustersize
& (clustersize
- 1)))
69 return "impossible cluster size";
71 sectors
= get_16(§buf
->bsSectors
);
72 sectors
= sectors
? sectors
: get_32(§buf
->bsHugeSectors
);
74 dsectors
= sectors
- get_16(§buf
->bsResSectors
);
76 fatsectors
= get_16(§buf
->bsFATsecs
);
77 fatsectors
= fatsectors
? fatsectors
: get_32(§buf
->bs32
.FATSz32
);
78 fatsectors
*= get_8(§buf
->bsFATs
);
79 dsectors
-= fatsectors
;
81 rootdirents
= get_16(§buf
->bsRootDirEnts
);
82 dsectors
-= (rootdirents
+ sectorsize
/ 32 - 1) / sectorsize
;
85 return "negative number of data sectors";
88 return "zero FAT sectors";
90 clusters
= dsectors
/ clustersize
;
92 if (clusters
< 0xFFF5) {
95 if (!get_16(§buf
->bsFATsecs
))
96 return "zero FAT sectors (FAT12/16)";
98 if (get_8(§buf
->bs16
.BootSignature
) == 0x29) {
99 if (!memcmp(§buf
->bs16
.FileSysType
, "FAT12 ", 8)) {
100 if (clusters
>= 0xFF5)
101 return "more than 4084 clusters but claims FAT12";
102 } else if (!memcmp(§buf
->bs16
.FileSysType
, "FAT16 ", 8)) {
103 if (clusters
< 0xFF5)
104 return "less than 4084 clusters but claims FAT16";
105 } else if (!memcmp(§buf
->bs16
.FileSysType
, "FAT32 ", 8)) {
106 return "less than 65525 clusters but claims FAT32";
107 } else if (memcmp(§buf
->bs16
.FileSysType
, "FAT ", 8)) {
108 static char fserr
[] =
109 "filesystem type \"????????\" not supported";
110 memcpy(fserr
+ 17, §buf
->bs16
.FileSysType
, 8);
114 } else if (clusters
< 0x0FFFFFF5) {
118 * Moving the FileSysType and BootSignature was a lovely stroke
121 if (get_8(§buf
->bs32
.BootSignature
) != 0x29 ||
122 memcmp(§buf
->bs32
.FileSysType
, "FAT32 ", 8))
123 return "missing FAT32 signature";
125 return "impossibly large number of clusters";