rosh: rosh_argcat now has length. if DO_DEBUG, show startup command
[syslinux.git] / libinstaller / fat.c
blobe2101353e4e4dc74a7d57ca7a2ee07a975ff7edb
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 veryold;
46 int sectorsize;
47 long long sectors, fatsectors, dsectors;
48 long long clusters;
49 int rootdirents, clustersize;
50 const struct boot_sector *sectbuf = bs;
52 veryold = 0;
54 /* Must be 0xF0 or 0xF8..0xFF */
55 if (get_8(&sectbuf->bsMedia) != 0xF0 && get_8(&sectbuf->bsMedia) < 0xF8)
56 return "invalid media signature (not a FAT filesystem?)";
58 sectorsize = get_16(&sectbuf->bsBytesPerSec);
59 if (sectorsize == SECTOR_SIZE)
60 ; /* ok */
61 else if (sectorsize >= 512 && sectorsize <= 4096 &&
62 (sectorsize & (sectorsize - 1)) == 0)
63 return "unsupported sectors size";
64 else
65 return "impossible sector size";
67 clustersize = get_8(&sectbuf->bsSecPerClust);
68 if (clustersize == 0 || (clustersize & (clustersize - 1)))
69 return "impossible cluster size";
71 sectors = get_16(&sectbuf->bsSectors);
72 sectors = sectors ? sectors : get_32(&sectbuf->bsHugeSectors);
74 dsectors = sectors - get_16(&sectbuf->bsResSectors);
76 fatsectors = get_16(&sectbuf->bsFATsecs);
77 fatsectors = fatsectors ? fatsectors : get_32(&sectbuf->bs32.FATSz32);
78 fatsectors *= get_8(&sectbuf->bsFATs);
79 dsectors -= fatsectors;
81 rootdirents = get_16(&sectbuf->bsRootDirEnts);
82 dsectors -= (rootdirents + sectorsize / 32 - 1) / sectorsize;
84 if (dsectors < 0)
85 return "negative number of data sectors";
87 if (fatsectors == 0)
88 return "zero FAT sectors";
90 clusters = dsectors / clustersize;
92 if (clusters < 0xFFF5) {
93 /* FAT12 or FAT16 */
95 if (!get_16(&sectbuf->bsFATsecs))
96 return "zero FAT sectors (FAT12/16)";
98 if (get_8(&sectbuf->bs16.BootSignature) == 0x29) {
99 if (!memcmp(&sectbuf->bs16.FileSysType, "FAT12 ", 8)) {
100 if (clusters >= 0xFF5)
101 return "more than 4084 clusters but claims FAT12";
102 } else if (!memcmp(&sectbuf->bs16.FileSysType, "FAT16 ", 8)) {
103 if (clusters < 0xFF5)
104 return "less than 4084 clusters but claims FAT16";
105 } else if (!memcmp(&sectbuf->bs16.FileSysType, "FAT32 ", 8)) {
106 return "less than 65525 clusters but claims FAT32";
107 } else if (memcmp(&sectbuf->bs16.FileSysType, "FAT ", 8)) {
108 static char fserr[] =
109 "filesystem type \"????????\" not supported";
110 memcpy(fserr + 17, &sectbuf->bs16.FileSysType, 8);
111 return fserr;
114 } else if (clusters < 0x0FFFFFF5) {
116 * FAT32...
118 * Moving the FileSysType and BootSignature was a lovely stroke
119 * of M$ idiocy...
121 if (get_8(&sectbuf->bs32.BootSignature) != 0x29 ||
122 memcmp(&sectbuf->bs32.FileSysType, "FAT32 ", 8))
123 return "missing FAT32 signature";
124 } else {
125 return "impossibly large number of clusters";
128 return NULL;