NEWS: Document MENU HIDDENKEY
[syslinux.git] / libinstaller / fat.c
blob9cde00c290b10f617fbfdea101c544793f734bda
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 */
19 #define _BSD_SOURCE
20 #include <stdio.h>
21 #include <inttypes.h>
22 #include <string.h>
23 #include <stddef.h>
24 #include <stdlib.h>
26 #include "syslinux.h"
27 #include "syslxint.h"
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)
45 int sectorsize;
46 long long sectors, fatsectors, dsectors;
47 long long clusters;
48 int rootdirents, clustersize;
49 const struct boot_sector *sectbuf = bs;
51 /* Must be 0xF0 or 0xF8..0xFF */
52 if (get_8(&sectbuf->bsMedia) != 0xF0 && get_8(&sectbuf->bsMedia) < 0xF8)
53 return "invalid media signature (not a FAT filesystem?)";
55 sectorsize = get_16(&sectbuf->bsBytesPerSec);
56 if (sectorsize == SECTOR_SIZE)
57 ; /* ok */
58 else if (sectorsize >= 512 && sectorsize <= 4096 &&
59 (sectorsize & (sectorsize - 1)) == 0)
60 return "unsupported sectors size";
61 else
62 return "impossible sector size";
64 clustersize = get_8(&sectbuf->bsSecPerClust);
65 if (clustersize == 0 || (clustersize & (clustersize - 1)))
66 return "impossible cluster size";
68 sectors = get_16(&sectbuf->bsSectors);
69 sectors = sectors ? sectors : get_32(&sectbuf->bsHugeSectors);
71 dsectors = sectors - get_16(&sectbuf->bsResSectors);
73 fatsectors = get_16(&sectbuf->bsFATsecs);
74 fatsectors = fatsectors ? fatsectors : get_32(&sectbuf->bs32.FATSz32);
75 fatsectors *= get_8(&sectbuf->bsFATs);
76 dsectors -= fatsectors;
78 rootdirents = get_16(&sectbuf->bsRootDirEnts);
79 dsectors -= (rootdirents + sectorsize / 32 - 1) / sectorsize;
81 if (dsectors < 0)
82 return "negative number of data sectors";
84 if (fatsectors == 0)
85 return "zero FAT sectors";
87 clusters = dsectors / clustersize;
89 if (clusters < 0xFFF5) {
90 /* FAT12 or FAT16 */
92 if (!get_16(&sectbuf->bsFATsecs))
93 return "zero FAT sectors (FAT12/16)";
95 if (get_8(&sectbuf->bs16.BootSignature) == 0x29) {
96 if (!memcmp(&sectbuf->bs16.FileSysType, "FAT12 ", 8)) {
97 if (clusters >= 0xFF5)
98 return "more than 4084 clusters but claims FAT12";
99 } else if (!memcmp(&sectbuf->bs16.FileSysType, "FAT16 ", 8)) {
100 if (clusters < 0xFF5)
101 return "less than 4084 clusters but claims FAT16";
102 } else if (!memcmp(&sectbuf->bs16.FileSysType, "FAT32 ", 8)) {
103 return "less than 65525 clusters but claims FAT32";
104 } else if (memcmp(&sectbuf->bs16.FileSysType, "FAT ", 8)) {
105 static char fserr[] =
106 "filesystem type \"????????\" not supported";
107 memcpy(fserr + 17, &sectbuf->bs16.FileSysType, 8);
108 return fserr;
111 } else if (clusters < 0x0FFFFFF5) {
113 * FAT32...
115 * Moving the FileSysType and BootSignature was a lovely stroke
116 * of M$ idiocy...
118 if (get_8(&sectbuf->bs32.BootSignature) != 0x29 ||
119 memcmp(&sectbuf->bs32.FileSysType, "FAT32 ", 8))
120 return "missing FAT32 signature";
121 } else {
122 return "impossibly large number of clusters";
125 return NULL;