Revert "ldlinux: Allow a held shift key to override UI as well as DEFAULT"
[syslinux/sherbszt.git] / libinstaller / fs.c
blob179629e914f2c48b3f51204e6fb9be24949e238e
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 */
20 #define _BSD_SOURCE
21 #include <stdio.h>
22 #include <inttypes.h>
23 #include <string.h>
24 #include <stddef.h>
25 #include <stdlib.h>
27 #include "syslinux.h"
28 #include "syslxint.h"
29 #include "syslxcom.h"
30 #include "syslxfs.h"
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)
53 int sectorsize;
54 const struct fat_boot_sector *sectbuf = bs;
55 long long sectors, fatsectors, dsectors;
56 long long clusters;
57 int rootdirents, clustersize;
59 sectorsize = get_16(&sectbuf->bsBytesPerSec);
61 clustersize = get_8(&sectbuf->bsSecPerClust);
62 if (clustersize == 0 || (clustersize & (clustersize - 1)))
63 return "impossible cluster size on an FAT volume";
65 sectors = get_16(&sectbuf->bsSectors);
66 sectors = sectors ? sectors : get_32(&sectbuf->bsHugeSectors);
68 dsectors = sectors - get_16(&sectbuf->bsResSectors);
70 fatsectors = get_16(&sectbuf->bsFATsecs);
71 fatsectors = fatsectors ? fatsectors : get_32(&sectbuf->bs32.FATSz32);
72 fatsectors *= get_8(&sectbuf->bsFATs);
73 dsectors -= fatsectors;
75 rootdirents = get_16(&sectbuf->bsRootDirEnts);
76 dsectors -= (rootdirents + sectorsize / 32 - 1) / sectorsize;
78 if (dsectors < 0)
79 return "negative number of data sectors on an FAT volume";
81 clusters = dsectors / clustersize;
83 fatsectors = get_16(&sectbuf->bsFATsecs);
84 fatsectors = fatsectors ? fatsectors : get_32(&sectbuf->bs32.FATSz32);
85 fatsectors *= get_8(&sectbuf->bsFATs);
87 if (!fatsectors)
88 return "zero FAT sectors";
90 if (clusters < 0xFFF5) {
91 /* 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[] = "filesystem type \"????????\" not "
106 "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 on an FAT volume";
125 if (fs_type)
126 *fs_type = VFAT;
128 return NULL;
131 static const char *check_ntfs_bootsect(const void *bs, int *fs_type)
133 const struct ntfs_boot_sector *sectbuf = bs;
135 if (memcmp(&sectbuf->bsOemName, "NTFS ", 8) &&
136 memcmp(&sectbuf->bsOemName, "MSWIN4.0", 8) &&
137 memcmp(&sectbuf->bsOemName, "MSWIN4.1", 8))
138 return "unknown OEM name but claims NTFS";
140 if (fs_type)
141 *fs_type = NTFS;
143 return NULL;
146 const char *syslinux_check_bootsect(const void *bs, int *fs_type)
148 uint8_t media_sig;
149 int sectorsize;
150 const struct fat_boot_sector *sectbuf = bs;
151 const char *retval;
153 media_sig = get_8(&sectbuf->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(&sectbuf->bsBytesPerSec);
159 if (sectorsize == SECTOR_SIZE) ; /* ok */
160 else if (sectorsize >= 512 && sectorsize <= 4096 &&
161 (sectorsize & (sectorsize - 1)) == 0)
162 return "unsupported sectors size";
163 else
164 return "impossible sector size";
166 if (ntfs_check_zero_fields((struct ntfs_boot_sector *)bs))
167 retval = check_ntfs_bootsect(bs, fs_type);
168 else
169 retval = check_fat_bootsect(bs, fs_type);
171 return retval;