| 1 | /* ----------------------------------------------------------------------- * |
|---|
| 2 | * |
|---|
| 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> |
|---|
| 6 | * |
|---|
| 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. |
|---|
| 12 | * |
|---|
| 13 | * ----------------------------------------------------------------------- */ |
|---|
| 14 | |
|---|
| 15 | /* |
|---|
| 16 | * fs.c - Generic sanity check for FAT/NTFS-based installers |
|---|
| 17 | */ |
|---|
| 18 | |
|---|
| 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> |
|---|
| 26 | |
|---|
| 27 | #include "syslinux.h" |
|---|
| 28 | #include "syslxint.h" |
|---|
| 29 | #include "syslxcom.h" |
|---|
| 30 | #include "syslxfs.h" |
|---|
| 31 | |
|---|
| 32 | void syslinux_make_bootsect(void *bs, int fs_type) |
|---|
| 33 | { |
|---|
| 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; |
|---|
| 38 | |
|---|
| 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; |
|---|
| 45 | |
|---|
| 46 | memcpy(&bootsect->NTFS_bsHead, &sbs->NTFS_bsHead, NTFS_bsHeadLen); |
|---|
| 47 | memcpy(&bootsect->NTFS_bsCode, &sbs->NTFS_bsCode, NTFS_bsCodeLen); |
|---|
| 48 | } |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | static const char *check_fat_bootsect(const void *bs, int *fs_type) |
|---|
| 52 | { |
|---|
| 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; |
|---|
| 58 | |
|---|
| 59 | sectorsize = get_16(§buf->bsBytesPerSec); |
|---|
| 60 | |
|---|
| 61 | clustersize = get_8(§buf->bsSecPerClust); |
|---|
| 62 | if (clustersize == 0 || (clustersize & (clustersize - 1))) |
|---|
| 63 | return "impossible cluster size on an FAT volume"; |
|---|
| 64 | |
|---|
| 65 | sectors = get_16(§buf->bsSectors); |
|---|
| 66 | sectors = sectors ? sectors : get_32(§buf->bsHugeSectors); |
|---|
| 67 | |
|---|
| 68 | dsectors = sectors - get_16(§buf->bsResSectors); |
|---|
| 69 | |
|---|
| 70 | fatsectors = get_16(§buf->bsFATsecs); |
|---|
| 71 | fatsectors = fatsectors ? fatsectors : get_32(§buf->bs32.FATSz32); |
|---|
| 72 | fatsectors *= get_8(§buf->bsFATs); |
|---|
| 73 | dsectors -= fatsectors; |
|---|
| 74 | |
|---|
| 75 | rootdirents = get_16(§buf->bsRootDirEnts); |
|---|
| 76 | dsectors -= (rootdirents + sectorsize / 32 - 1) / sectorsize; |
|---|
| 77 | |
|---|
| 78 | if (dsectors < 0) |
|---|
| 79 | return "negative number of data sectors on an FAT volume"; |
|---|
| 80 | |
|---|
| 81 | clusters = dsectors / clustersize; |
|---|
| 82 | |
|---|
| 83 | fatsectors = get_16(§buf->bsFATsecs); |
|---|
| 84 | fatsectors = fatsectors ? fatsectors : get_32(§buf->bs32.FATSz32); |
|---|
| 85 | fatsectors *= get_8(§buf->bsFATs); |
|---|
| 86 | |
|---|
| 87 | if (!fatsectors) |
|---|
| 88 | return "zero FAT sectors"; |
|---|
| 89 | |
|---|
| 90 | if (clusters < 0xFFF5) { |
|---|
| 91 | /* FAT12 or FAT16 */ |
|---|
| 92 | if (!get_16(§buf->bsFATsecs)) |
|---|
| 93 | return "zero FAT sectors (FAT12/16)"; |
|---|
| 94 | |
|---|
| 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 " |
|---|
| 106 | "supported"; |
|---|
| 107 | memcpy(fserr + 17, §buf->bs16.FileSysType, 8); |
|---|
| 108 | return fserr; |
|---|
| 109 | } |
|---|
| 110 | } |
|---|
| 111 | } else if (clusters < 0x0FFFFFF5) { |
|---|
| 112 | /* |
|---|
| 113 | * FAT32... |
|---|
| 114 | * |
|---|
| 115 | * Moving the FileSysType and BootSignature was a lovely stroke |
|---|
| 116 | * of M$ idiocy... |
|---|
| 117 | */ |
|---|
| 118 | if (get_8(§buf->bs32.BootSignature) != 0x29 || |
|---|
| 119 | memcmp(§buf->bs32.FileSysType, "FAT32 ", 8)) |
|---|
| 120 | return "missing FAT32 signature"; |
|---|
| 121 | } else { |
|---|
| 122 | return "impossibly large number of clusters on an FAT volume"; |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | if (fs_type) |
|---|
| 126 | *fs_type = VFAT; |
|---|
| 127 | |
|---|
| 128 | return NULL; |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | static const char *check_ntfs_bootsect(const void *bs, int *fs_type) |
|---|
| 132 | { |
|---|
| 133 | const struct ntfs_boot_sector *sectbuf = bs; |
|---|
| 134 | |
|---|
| 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"; |
|---|
| 139 | |
|---|
| 140 | if (fs_type) |
|---|
| 141 | *fs_type = NTFS; |
|---|
| 142 | |
|---|
| 143 | return NULL; |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | const char *syslinux_check_bootsect(const void *bs, int *fs_type) |
|---|
| 147 | { |
|---|
| 148 | uint8_t media_sig; |
|---|
| 149 | int sectorsize; |
|---|
| 150 | const struct fat_boot_sector *sectbuf = bs; |
|---|
| 151 | const char *retval; |
|---|
| 152 | |
|---|
| 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?)"; |
|---|
| 157 | |
|---|
| 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"; |
|---|
| 163 | else |
|---|
| 164 | return "impossible sector size"; |
|---|
| 165 | |
|---|
| 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); |
|---|
| 170 | |
|---|
| 171 | return retval; |
|---|
| 172 | } |
|---|