3
0

fat.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. //kbuild:lib-$(CONFIG_FEATURE_VOLUMEID_FAT) += fat.o
  21. //config:
  22. //config:config FEATURE_VOLUMEID_FAT
  23. //config: bool "fat filesystem"
  24. //config: default y
  25. //config: depends on VOLUMEID
  26. //config: help
  27. //config: TODO
  28. //config:
  29. #include "volume_id_internal.h"
  30. /* linux/msdos_fs.h says: */
  31. #define FAT12_MAX 0xff4
  32. #define FAT16_MAX 0xfff4
  33. #define FAT32_MAX 0x0ffffff6
  34. #define FAT_ATTR_VOLUME_ID 0x08
  35. #define FAT_ATTR_DIR 0x10
  36. #define FAT_ATTR_LONG_NAME 0x0f
  37. #define FAT_ATTR_MASK 0x3f
  38. #define FAT_ENTRY_FREE 0xe5
  39. struct vfat_super_block {
  40. uint8_t boot_jump[3];
  41. uint8_t sysid[8];
  42. uint16_t sector_size_bytes;
  43. uint8_t sectors_per_cluster;
  44. uint16_t reserved_sct;
  45. uint8_t fats;
  46. uint16_t dir_entries;
  47. uint16_t sectors;
  48. uint8_t media;
  49. uint16_t fat_length;
  50. uint16_t secs_track;
  51. uint16_t heads;
  52. uint32_t hidden;
  53. uint32_t total_sect;
  54. union {
  55. struct fat_super_block {
  56. uint8_t unknown[3];
  57. uint8_t serno[4];
  58. uint8_t label[11];
  59. uint8_t magic[8];
  60. uint8_t dummy2[192];
  61. uint8_t pmagic[2];
  62. } PACKED fat;
  63. struct fat32_super_block {
  64. uint32_t fat32_length;
  65. uint16_t flags;
  66. uint8_t version[2];
  67. uint32_t root_cluster;
  68. uint16_t insfo_sector;
  69. uint16_t backup_boot;
  70. uint16_t reserved2[6];
  71. uint8_t unknown[3];
  72. uint8_t serno[4];
  73. uint8_t label[11];
  74. uint8_t magic[8];
  75. uint8_t dummy2[164];
  76. uint8_t pmagic[2];
  77. } PACKED fat32;
  78. } PACKED type;
  79. } PACKED;
  80. struct vfat_dir_entry {
  81. uint8_t name[11];
  82. uint8_t attr;
  83. uint16_t time_creat;
  84. uint16_t date_creat;
  85. uint16_t time_acc;
  86. uint16_t date_acc;
  87. uint16_t cluster_high;
  88. uint16_t time_write;
  89. uint16_t date_write;
  90. uint16_t cluster_low;
  91. uint32_t size;
  92. } PACKED;
  93. static uint8_t *get_attr_volume_id(struct vfat_dir_entry *dir, int count)
  94. {
  95. for (;--count >= 0; dir++) {
  96. /* end marker */
  97. if (dir->name[0] == 0x00) {
  98. dbg("end of dir");
  99. break;
  100. }
  101. /* empty entry */
  102. if (dir->name[0] == FAT_ENTRY_FREE)
  103. continue;
  104. /* long name */
  105. if ((dir->attr & FAT_ATTR_MASK) == FAT_ATTR_LONG_NAME)
  106. continue;
  107. if ((dir->attr & (FAT_ATTR_VOLUME_ID | FAT_ATTR_DIR)) == FAT_ATTR_VOLUME_ID) {
  108. /* labels do not have file data */
  109. if (dir->cluster_high != 0 || dir->cluster_low != 0)
  110. continue;
  111. dbg("found ATTR_VOLUME_ID id in root dir");
  112. return dir->name;
  113. }
  114. dbg("skip dir entry");
  115. }
  116. return NULL;
  117. }
  118. int FAST_FUNC volume_id_probe_vfat(struct volume_id *id /*,uint64_t fat_partition_off*/)
  119. {
  120. #define fat_partition_off ((uint64_t)0)
  121. struct vfat_super_block *vs;
  122. struct vfat_dir_entry *dir;
  123. uint16_t sector_size_bytes;
  124. uint16_t dir_entries;
  125. uint32_t sect_count;
  126. uint16_t reserved_sct;
  127. uint32_t fat_size_sct;
  128. uint32_t root_cluster;
  129. uint32_t dir_size_sct;
  130. uint32_t cluster_count;
  131. uint64_t root_start_off;
  132. uint32_t start_data_sct;
  133. uint8_t *buf;
  134. uint32_t buf_size;
  135. uint8_t *label = NULL;
  136. uint32_t next_cluster;
  137. int maxloop;
  138. dbg("probing at offset 0x%llx", (unsigned long long) fat_partition_off);
  139. vs = volume_id_get_buffer(id, fat_partition_off, 0x200);
  140. if (vs == NULL)
  141. return -1;
  142. /* believe only that's fat, don't trust the version
  143. * the cluster_count will tell us
  144. */
  145. if (memcmp(vs->sysid, "NTFS", 4) == 0)
  146. return -1;
  147. if (memcmp(vs->type.fat32.magic, "MSWIN", 5) == 0)
  148. goto valid;
  149. if (memcmp(vs->type.fat32.magic, "FAT32 ", 8) == 0)
  150. goto valid;
  151. if (memcmp(vs->type.fat.magic, "FAT16 ", 8) == 0)
  152. goto valid;
  153. if (memcmp(vs->type.fat.magic, "MSDOS", 5) == 0)
  154. goto valid;
  155. if (memcmp(vs->type.fat.magic, "FAT12 ", 8) == 0)
  156. goto valid;
  157. /*
  158. * There are old floppies out there without a magic, so we check
  159. * for well known values and guess if it's a fat volume
  160. */
  161. /* boot jump address check */
  162. if ((vs->boot_jump[0] != 0xeb || vs->boot_jump[2] != 0x90)
  163. && vs->boot_jump[0] != 0xe9
  164. ) {
  165. return -1;
  166. }
  167. /* heads check */
  168. if (vs->heads == 0)
  169. return -1;
  170. /* cluster size check */
  171. if (vs->sectors_per_cluster == 0
  172. || (vs->sectors_per_cluster & (vs->sectors_per_cluster-1))
  173. ) {
  174. return -1;
  175. }
  176. /* media check */
  177. if (vs->media < 0xf8 && vs->media != 0xf0)
  178. return -1;
  179. /* fat count*/
  180. if (vs->fats != 2)
  181. return -1;
  182. valid:
  183. /* sector size check */
  184. sector_size_bytes = le16_to_cpu(vs->sector_size_bytes);
  185. if (sector_size_bytes != 0x200 && sector_size_bytes != 0x400
  186. && sector_size_bytes != 0x800 && sector_size_bytes != 0x1000
  187. ) {
  188. return -1;
  189. }
  190. dbg("sector_size_bytes 0x%x", sector_size_bytes);
  191. dbg("sectors_per_cluster 0x%x", vs->sectors_per_cluster);
  192. reserved_sct = le16_to_cpu(vs->reserved_sct);
  193. dbg("reserved_sct 0x%x", reserved_sct);
  194. sect_count = le16_to_cpu(vs->sectors);
  195. if (sect_count == 0)
  196. sect_count = le32_to_cpu(vs->total_sect);
  197. dbg("sect_count 0x%x", sect_count);
  198. fat_size_sct = le16_to_cpu(vs->fat_length);
  199. if (fat_size_sct == 0)
  200. fat_size_sct = le32_to_cpu(vs->type.fat32.fat32_length);
  201. fat_size_sct *= vs->fats;
  202. dbg("fat_size_sct 0x%x", fat_size_sct);
  203. dir_entries = le16_to_cpu(vs->dir_entries);
  204. dir_size_sct = ((dir_entries * sizeof(struct vfat_dir_entry)) +
  205. (sector_size_bytes-1)) / sector_size_bytes;
  206. dbg("dir_size_sct 0x%x", dir_size_sct);
  207. cluster_count = sect_count - (reserved_sct + fat_size_sct + dir_size_sct);
  208. cluster_count /= vs->sectors_per_cluster;
  209. dbg("cluster_count 0x%x", cluster_count);
  210. // if (cluster_count < FAT12_MAX) {
  211. // strcpy(id->type_version, "FAT12");
  212. // } else if (cluster_count < FAT16_MAX) {
  213. // strcpy(id->type_version, "FAT16");
  214. // } else {
  215. // strcpy(id->type_version, "FAT32");
  216. // goto fat32;
  217. // }
  218. if (cluster_count >= FAT16_MAX)
  219. goto fat32;
  220. /* the label may be an attribute in the root directory */
  221. root_start_off = (reserved_sct + fat_size_sct) * sector_size_bytes;
  222. dbg("root dir start 0x%llx", (unsigned long long) root_start_off);
  223. dbg("expected entries 0x%x", dir_entries);
  224. buf_size = dir_entries * sizeof(struct vfat_dir_entry);
  225. buf = volume_id_get_buffer(id, fat_partition_off + root_start_off, buf_size);
  226. if (buf == NULL)
  227. goto ret;
  228. label = get_attr_volume_id((struct vfat_dir_entry*) buf, dir_entries);
  229. vs = volume_id_get_buffer(id, fat_partition_off, 0x200);
  230. if (vs == NULL)
  231. return -1;
  232. if (label != NULL && memcmp(label, "NO NAME ", 11) != 0) {
  233. // volume_id_set_label_raw(id, label, 11);
  234. volume_id_set_label_string(id, label, 11);
  235. } else if (memcmp(vs->type.fat.label, "NO NAME ", 11) != 0) {
  236. // volume_id_set_label_raw(id, vs->type.fat.label, 11);
  237. volume_id_set_label_string(id, vs->type.fat.label, 11);
  238. }
  239. volume_id_set_uuid(id, vs->type.fat.serno, UUID_DOS);
  240. goto ret;
  241. fat32:
  242. /* FAT32 root dir is a cluster chain like any other directory */
  243. buf_size = vs->sectors_per_cluster * sector_size_bytes;
  244. root_cluster = le32_to_cpu(vs->type.fat32.root_cluster);
  245. start_data_sct = reserved_sct + fat_size_sct;
  246. next_cluster = root_cluster;
  247. maxloop = 100;
  248. while (--maxloop) {
  249. uint64_t next_off_sct;
  250. uint64_t next_off;
  251. uint64_t fat_entry_off;
  252. int count;
  253. dbg("next_cluster 0x%x", (unsigned)next_cluster);
  254. next_off_sct = (uint64_t)(next_cluster - 2) * vs->sectors_per_cluster;
  255. next_off = (start_data_sct + next_off_sct) * sector_size_bytes;
  256. dbg("cluster offset 0x%llx", (unsigned long long) next_off);
  257. /* get cluster */
  258. buf = volume_id_get_buffer(id, fat_partition_off + next_off, buf_size);
  259. if (buf == NULL)
  260. goto ret;
  261. dir = (struct vfat_dir_entry*) buf;
  262. count = buf_size / sizeof(struct vfat_dir_entry);
  263. dbg("expected entries 0x%x", count);
  264. label = get_attr_volume_id(dir, count);
  265. if (label)
  266. break;
  267. /* get FAT entry */
  268. fat_entry_off = (reserved_sct * sector_size_bytes) + (next_cluster * sizeof(uint32_t));
  269. dbg("fat_entry_off 0x%llx", (unsigned long long)fat_entry_off);
  270. buf = volume_id_get_buffer(id, fat_partition_off + fat_entry_off, buf_size);
  271. if (buf == NULL)
  272. goto ret;
  273. /* set next cluster */
  274. next_cluster = le32_to_cpu(*(uint32_t*)buf) & 0x0fffffff;
  275. if (next_cluster < 2 || next_cluster > FAT32_MAX)
  276. break;
  277. }
  278. if (maxloop == 0)
  279. dbg("reached maximum follow count of root cluster chain, give up");
  280. vs = volume_id_get_buffer(id, fat_partition_off, 0x200);
  281. if (vs == NULL)
  282. return -1;
  283. if (label != NULL && memcmp(label, "NO NAME ", 11) != 0) {
  284. // volume_id_set_label_raw(id, label, 11);
  285. volume_id_set_label_string(id, label, 11);
  286. } else if (memcmp(vs->type.fat32.label, "NO NAME ", 11) != 0) {
  287. // volume_id_set_label_raw(id, vs->type.fat32.label, 11);
  288. volume_id_set_label_string(id, vs->type.fat32.label, 11);
  289. }
  290. volume_id_set_uuid(id, vs->type.fat32.serno, UUID_DOS);
  291. ret:
  292. // volume_id_set_usage(id, VOLUME_ID_FILESYSTEM);
  293. IF_FEATURE_BLKID_TYPE(id->type = "vfat";)
  294. return 0;
  295. }