fat.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /*
  2. * volume_id - reads filesystem label and uuid
  3. *
  4. * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #include "volume_id_internal.h"
  21. #define FAT12_MAX 0xff5
  22. #define FAT16_MAX 0xfff5
  23. #define FAT_ATTR_VOLUME_ID 0x08
  24. #define FAT_ATTR_DIR 0x10
  25. #define FAT_ATTR_LONG_NAME 0x0f
  26. #define FAT_ATTR_MASK 0x3f
  27. #define FAT_ENTRY_FREE 0xe5
  28. struct vfat_super_block {
  29. uint8_t boot_jump[3];
  30. uint8_t sysid[8];
  31. uint16_t sector_size;
  32. uint8_t sectors_per_cluster;
  33. uint16_t reserved;
  34. uint8_t fats;
  35. uint16_t dir_entries;
  36. uint16_t sectors;
  37. uint8_t media;
  38. uint16_t fat_length;
  39. uint16_t secs_track;
  40. uint16_t heads;
  41. uint32_t hidden;
  42. uint32_t total_sect;
  43. union {
  44. struct fat_super_block {
  45. uint8_t unknown[3];
  46. uint8_t serno[4];
  47. uint8_t label[11];
  48. uint8_t magic[8];
  49. uint8_t dummy2[192];
  50. uint8_t pmagic[2];
  51. } __attribute__((__packed__)) fat;
  52. struct fat32_super_block {
  53. uint32_t fat32_length;
  54. uint16_t flags;
  55. uint8_t version[2];
  56. uint32_t root_cluster;
  57. uint16_t insfo_sector;
  58. uint16_t backup_boot;
  59. uint16_t reserved2[6];
  60. uint8_t unknown[3];
  61. uint8_t serno[4];
  62. uint8_t label[11];
  63. uint8_t magic[8];
  64. uint8_t dummy2[164];
  65. uint8_t pmagic[2];
  66. } __attribute__((__packed__)) fat32;
  67. } __attribute__((__packed__)) type;
  68. } __attribute__((__packed__));
  69. struct vfat_dir_entry {
  70. uint8_t name[11];
  71. uint8_t attr;
  72. uint16_t time_creat;
  73. uint16_t date_creat;
  74. uint16_t time_acc;
  75. uint16_t date_acc;
  76. uint16_t cluster_high;
  77. uint16_t time_write;
  78. uint16_t date_write;
  79. uint16_t cluster_low;
  80. uint32_t size;
  81. } __attribute__((__packed__));
  82. static uint8_t *get_attr_volume_id(struct vfat_dir_entry *dir, unsigned count)
  83. {
  84. unsigned i;
  85. for (i = 0; i < count; i++) {
  86. /* end marker */
  87. if (dir[i].name[0] == 0x00) {
  88. dbg("end of dir");
  89. break;
  90. }
  91. /* empty entry */
  92. if (dir[i].name[0] == FAT_ENTRY_FREE)
  93. continue;
  94. /* long name */
  95. if ((dir[i].attr & FAT_ATTR_MASK) == FAT_ATTR_LONG_NAME)
  96. continue;
  97. if ((dir[i].attr & (FAT_ATTR_VOLUME_ID | FAT_ATTR_DIR)) == FAT_ATTR_VOLUME_ID) {
  98. /* labels do not have file data */
  99. if (dir[i].cluster_high != 0 || dir[i].cluster_low != 0)
  100. continue;
  101. dbg("found ATTR_VOLUME_ID id in root dir");
  102. return dir[i].name;
  103. }
  104. dbg("skip dir entry");
  105. }
  106. return NULL;
  107. }
  108. int volume_id_probe_vfat(struct volume_id *id, uint64_t off)
  109. {
  110. struct vfat_super_block *vs;
  111. struct vfat_dir_entry *dir;
  112. uint16_t sector_size;
  113. uint16_t dir_entries;
  114. uint32_t sect_count;
  115. uint16_t reserved;
  116. uint32_t fat_size;
  117. uint32_t root_cluster;
  118. uint32_t dir_size;
  119. uint32_t cluster_count;
  120. uint32_t fat_length;
  121. uint64_t root_start;
  122. uint32_t start_data_sect;
  123. uint16_t root_dir_entries;
  124. uint8_t *buf;
  125. uint32_t buf_size;
  126. uint8_t *label = NULL;
  127. uint32_t next;
  128. int maxloop;
  129. dbg("probing at offset 0x%llx", (unsigned long long) off);
  130. vs = volume_id_get_buffer(id, off, 0x200);
  131. if (vs == NULL)
  132. return -1;
  133. /* believe only that's fat, don't trust the version
  134. * the cluster_count will tell us
  135. */
  136. if (memcmp(vs->sysid, "NTFS", 4) == 0)
  137. return -1;
  138. if (memcmp(vs->type.fat32.magic, "MSWIN", 5) == 0)
  139. goto valid;
  140. if (memcmp(vs->type.fat32.magic, "FAT32 ", 8) == 0)
  141. goto valid;
  142. if (memcmp(vs->type.fat.magic, "FAT16 ", 8) == 0)
  143. goto valid;
  144. if (memcmp(vs->type.fat.magic, "MSDOS", 5) == 0)
  145. goto valid;
  146. if (memcmp(vs->type.fat.magic, "FAT12 ", 8) == 0)
  147. goto valid;
  148. /*
  149. * There are old floppies out there without a magic, so we check
  150. * for well known values and guess if it's a fat volume
  151. */
  152. /* boot jump address check */
  153. if ((vs->boot_jump[0] != 0xeb || vs->boot_jump[2] != 0x90) &&
  154. vs->boot_jump[0] != 0xe9)
  155. return -1;
  156. /* heads check */
  157. if (vs->heads == 0)
  158. return -1;
  159. /* cluster size check */
  160. if (vs->sectors_per_cluster == 0 ||
  161. (vs->sectors_per_cluster & (vs->sectors_per_cluster-1)))
  162. return -1;
  163. /* media check */
  164. if (vs->media < 0xf8 && vs->media != 0xf0)
  165. return -1;
  166. /* fat count*/
  167. if (vs->fats != 2)
  168. return -1;
  169. valid:
  170. /* sector size check */
  171. sector_size = le16_to_cpu(vs->sector_size);
  172. if (sector_size != 0x200 && sector_size != 0x400 &&
  173. sector_size != 0x800 && sector_size != 0x1000)
  174. return -1;
  175. dbg("sector_size 0x%x", sector_size);
  176. dbg("sectors_per_cluster 0x%x", vs->sectors_per_cluster);
  177. dir_entries = le16_to_cpu(vs->dir_entries);
  178. reserved = le16_to_cpu(vs->reserved);
  179. dbg("reserved 0x%x", reserved);
  180. sect_count = le16_to_cpu(vs->sectors);
  181. if (sect_count == 0)
  182. sect_count = le32_to_cpu(vs->total_sect);
  183. dbg("sect_count 0x%x", sect_count);
  184. fat_length = le16_to_cpu(vs->fat_length);
  185. if (fat_length == 0)
  186. fat_length = le32_to_cpu(vs->type.fat32.fat32_length);
  187. dbg("fat_length 0x%x", fat_length);
  188. fat_size = fat_length * vs->fats;
  189. dir_size = ((dir_entries * sizeof(struct vfat_dir_entry)) +
  190. (sector_size-1)) / sector_size;
  191. dbg("dir_size 0x%x", dir_size);
  192. cluster_count = sect_count - (reserved + fat_size + dir_size);
  193. cluster_count /= vs->sectors_per_cluster;
  194. dbg("cluster_count 0x%x", cluster_count);
  195. // if (cluster_count < FAT12_MAX) {
  196. // strcpy(id->type_version, "FAT12");
  197. // } else if (cluster_count < FAT16_MAX) {
  198. // strcpy(id->type_version, "FAT16");
  199. // } else {
  200. // strcpy(id->type_version, "FAT32");
  201. // goto fat32;
  202. // }
  203. if (cluster_count >= FAT16_MAX)
  204. goto fat32;
  205. /* the label may be an attribute in the root directory */
  206. root_start = (reserved + fat_size) * sector_size;
  207. dbg("root dir start 0x%llx", (unsigned long long) root_start);
  208. root_dir_entries = le16_to_cpu(vs->dir_entries);
  209. dbg("expected entries 0x%x", root_dir_entries);
  210. buf_size = root_dir_entries * sizeof(struct vfat_dir_entry);
  211. buf = volume_id_get_buffer(id, off + root_start, buf_size);
  212. if (buf == NULL)
  213. goto found;
  214. dir = (struct vfat_dir_entry*) buf;
  215. label = get_attr_volume_id(dir, root_dir_entries);
  216. vs = volume_id_get_buffer(id, off, 0x200);
  217. if (vs == NULL)
  218. return -1;
  219. if (label != NULL && memcmp(label, "NO NAME ", 11) != 0) {
  220. // volume_id_set_label_raw(id, label, 11);
  221. volume_id_set_label_string(id, label, 11);
  222. } else if (memcmp(vs->type.fat.label, "NO NAME ", 11) != 0) {
  223. // volume_id_set_label_raw(id, vs->type.fat.label, 11);
  224. volume_id_set_label_string(id, vs->type.fat.label, 11);
  225. }
  226. volume_id_set_uuid(id, vs->type.fat.serno, UUID_DOS);
  227. goto found;
  228. fat32:
  229. /* FAT32 root dir is a cluster chain like any other directory */
  230. buf_size = vs->sectors_per_cluster * sector_size;
  231. root_cluster = le32_to_cpu(vs->type.fat32.root_cluster);
  232. dbg("root dir cluster %u", root_cluster);
  233. start_data_sect = reserved + fat_size;
  234. next = root_cluster;
  235. maxloop = 100;
  236. while (--maxloop) {
  237. uint32_t next_sect_off;
  238. uint64_t next_off;
  239. uint64_t fat_entry_off;
  240. int count;
  241. dbg("next cluster %u", next);
  242. next_sect_off = (next - 2) * vs->sectors_per_cluster;
  243. next_off = (start_data_sect + next_sect_off) * sector_size;
  244. dbg("cluster offset 0x%llx", (unsigned long long) next_off);
  245. /* get cluster */
  246. buf = volume_id_get_buffer(id, off + next_off, buf_size);
  247. if (buf == NULL)
  248. goto found;
  249. dir = (struct vfat_dir_entry*) buf;
  250. count = buf_size / sizeof(struct vfat_dir_entry);
  251. dbg("expected entries 0x%x", count);
  252. label = get_attr_volume_id(dir, count);
  253. if (label)
  254. break;
  255. /* get FAT entry */
  256. fat_entry_off = (reserved * sector_size) + (next * sizeof(uint32_t));
  257. buf = volume_id_get_buffer(id, off + fat_entry_off, buf_size);
  258. if (buf == NULL)
  259. goto found;
  260. /* set next cluster */
  261. next = le32_to_cpu(*((uint32_t *) buf) & 0x0fffffff);
  262. if (next == 0)
  263. break;
  264. }
  265. if (maxloop == 0)
  266. dbg("reached maximum follow count of root cluster chain, give up");
  267. vs = volume_id_get_buffer(id, off, 0x200);
  268. if (vs == NULL)
  269. return -1;
  270. if (label != NULL && memcmp(label, "NO NAME ", 11) != 0) {
  271. // volume_id_set_label_raw(id, label, 11);
  272. volume_id_set_label_string(id, label, 11);
  273. } else if (memcmp(vs->type.fat32.label, "NO NAME ", 11) != 0) {
  274. // volume_id_set_label_raw(id, vs->type.fat32.label, 11);
  275. volume_id_set_label_string(id, vs->type.fat32.label, 11);
  276. }
  277. volume_id_set_uuid(id, vs->type.fat32.serno, UUID_DOS);
  278. found:
  279. // volume_id_set_usage(id, VOLUME_ID_FILESYSTEM);
  280. // id->type = "vfat";
  281. return 0;
  282. }