fat.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /* fat.c - Read/write access to the FAT */
  2. /* Written 1993 by Werner Almesberger */
  3. /* FAT32, VFAT, Atari format support, and various fixes additions May 1998
  4. * by Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de> */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <unistd.h>
  9. #include "common.h"
  10. #include "dosfsck.h"
  11. #include "io.h"
  12. #include "check.h"
  13. #include "fat.h"
  14. static void get_fat(FAT_ENTRY *entry,void *fat,unsigned long cluster,DOS_FS *fs)
  15. {
  16. unsigned char *ptr;
  17. switch(fs->fat_bits) {
  18. case 12:
  19. ptr = &((unsigned char *) fat)[cluster*3/2];
  20. entry->value = 0xfff & (cluster & 1 ? (ptr[0] >> 4) | (ptr[1] << 4) :
  21. (ptr[0] | ptr[1] << 8));
  22. break;
  23. case 16:
  24. entry->value = CF_LE_W(((unsigned short *) fat)[cluster]);
  25. break;
  26. case 32:
  27. /* According to M$, the high 4 bits of a FAT32 entry are reserved and
  28. * are not part of the cluster number. So we cut them off. */
  29. {
  30. unsigned long e = CF_LE_L(((unsigned int *) fat)[cluster]);
  31. entry->value = e & 0xfffffff;
  32. entry->reserved = e >> 28;
  33. }
  34. break;
  35. default:
  36. die("Bad FAT entry size: %d bits.",fs->fat_bits);
  37. }
  38. entry->owner = NULL;
  39. }
  40. void read_fat(DOS_FS *fs)
  41. {
  42. int eff_size;
  43. unsigned long i;
  44. void *first,*second = NULL;
  45. int first_ok,second_ok;
  46. eff_size = ((fs->clusters+2ULL)*fs->fat_bits+7)/8ULL;
  47. first = alloc(eff_size);
  48. fs_read(fs->fat_start,eff_size,first);
  49. if (fs->nfats > 1) {
  50. second = alloc(eff_size);
  51. fs_read(fs->fat_start+fs->fat_size,eff_size,second);
  52. }
  53. if (second && memcmp(first,second,eff_size) != 0) {
  54. FAT_ENTRY first_media, second_media;
  55. get_fat(&first_media,first,0,fs);
  56. get_fat(&second_media,second,0,fs);
  57. first_ok = (first_media.value & FAT_EXTD(fs)) == FAT_EXTD(fs);
  58. second_ok = (second_media.value & FAT_EXTD(fs)) == FAT_EXTD(fs);
  59. if (first_ok && !second_ok) {
  60. printf("FATs differ - using first FAT.\n");
  61. fs_write(fs->fat_start+fs->fat_size,eff_size,first);
  62. }
  63. if (!first_ok && second_ok) {
  64. printf("FATs differ - using second FAT.\n");
  65. fs_write(fs->fat_start,eff_size,second);
  66. memcpy(first,second,eff_size);
  67. }
  68. if (first_ok && second_ok) {
  69. if (interactive) {
  70. printf("FATs differ but appear to be intact. Use which FAT ?\n"
  71. "1) Use first FAT\n2) Use second FAT\n");
  72. if (get_key("12","?") == '1') {
  73. fs_write(fs->fat_start+fs->fat_size,eff_size,first);
  74. } else {
  75. fs_write(fs->fat_start,eff_size,second);
  76. memcpy(first,second,eff_size);
  77. }
  78. }
  79. else {
  80. printf("FATs differ but appear to be intact. Using first "
  81. "FAT.\n");
  82. fs_write(fs->fat_start+fs->fat_size,eff_size,first);
  83. }
  84. }
  85. if (!first_ok && !second_ok) {
  86. printf("Both FATs appear to be corrupt. Giving up.\n");
  87. exit(1);
  88. }
  89. }
  90. if (second) {
  91. free(second);
  92. }
  93. fs->fat = qalloc(&mem_queue,sizeof(FAT_ENTRY)*(fs->clusters+2ULL));
  94. for (i = 2; i < fs->clusters+2; i++) get_fat(&fs->fat[i],first,i,fs);
  95. for (i = 2; i < fs->clusters+2; i++)
  96. if (fs->fat[i].value >= fs->clusters+2 &&
  97. (fs->fat[i].value < FAT_MIN_BAD(fs))) {
  98. printf("Cluster %ld out of range (%ld > %ld). Setting to EOF.\n",
  99. i-2,fs->fat[i].value,fs->clusters+2-1);
  100. set_fat(fs,i,-1);
  101. }
  102. free(first);
  103. }
  104. void set_fat(DOS_FS *fs,unsigned long cluster,unsigned long new)
  105. {
  106. unsigned char data[4];
  107. int size;
  108. loff_t offs;
  109. if ((long)new == -1)
  110. new = FAT_EOF(fs);
  111. else if ((long)new == -2)
  112. new = FAT_BAD(fs);
  113. switch( fs->fat_bits ) {
  114. case 12:
  115. offs = fs->fat_start+cluster*3/2;
  116. if (cluster & 1) {
  117. data[0] = ((new & 0xf) << 4) | (fs->fat[cluster-1].value >> 8);
  118. data[1] = new >> 4;
  119. }
  120. else {
  121. data[0] = new & 0xff;
  122. data[1] = (new >> 8) | (cluster == fs->clusters-1 ? 0 :
  123. (0xff & fs->fat[cluster+1].value) << 4);
  124. }
  125. size = 2;
  126. break;
  127. case 16:
  128. offs = fs->fat_start+cluster*2;
  129. *(unsigned short *) data = CT_LE_W(new);
  130. size = 2;
  131. break;
  132. case 32:
  133. offs = fs->fat_start+cluster*4;
  134. /* According to M$, the high 4 bits of a FAT32 entry are reserved and
  135. * are not part of the cluster number. So we never touch them. */
  136. *(unsigned long *) data = CT_LE_L( (new & 0xfffffff) |
  137. (fs->fat[cluster].reserved << 28) );
  138. size = 4;
  139. break;
  140. default:
  141. die("Bad FAT entry size: %d bits.",fs->fat_bits);
  142. }
  143. fs->fat[cluster].value = new;
  144. fs_write(offs,size,&data);
  145. fs_write(offs+fs->fat_size,size,&data);
  146. }
  147. int bad_cluster(DOS_FS *fs,unsigned long cluster)
  148. {
  149. return FAT_IS_BAD(fs,fs->fat[cluster].value);
  150. }
  151. unsigned long next_cluster(DOS_FS *fs,unsigned long cluster)
  152. {
  153. unsigned long value;
  154. value = fs->fat[cluster].value;
  155. if (FAT_IS_BAD(fs,value))
  156. die("Internal error: next_cluster on bad cluster");
  157. return FAT_IS_EOF(fs,value) ? -1 : value;
  158. }
  159. loff_t cluster_start(DOS_FS *fs,unsigned long cluster)
  160. {
  161. return fs->data_start+((loff_t)cluster-2)*(unsigned long long)fs->cluster_size;
  162. }
  163. void set_owner(DOS_FS *fs,unsigned long cluster,DOS_FILE *owner)
  164. {
  165. if (owner && fs->fat[cluster].owner)
  166. die("Internal error: attempt to change file owner");
  167. fs->fat[cluster].owner = owner;
  168. }
  169. DOS_FILE *get_owner(DOS_FS *fs,unsigned long cluster)
  170. {
  171. return fs->fat[cluster].owner;
  172. }
  173. void fix_bad(DOS_FS *fs)
  174. {
  175. unsigned long i;
  176. if (verbose)
  177. printf("Checking for bad clusters.\n");
  178. for (i = 2; i < fs->clusters+2; i++)
  179. if (!get_owner(fs,i) && !FAT_IS_BAD(fs,fs->fat[i].value))
  180. if (!fs_test(cluster_start(fs,i),fs->cluster_size)) {
  181. printf("Cluster %lu is unreadable.\n",i);
  182. set_fat(fs,i,-2);
  183. }
  184. }
  185. void reclaim_free(DOS_FS *fs)
  186. {
  187. int reclaimed;
  188. unsigned long i;
  189. if (verbose)
  190. printf("Checking for unused clusters.\n");
  191. reclaimed = 0;
  192. for (i = 2; i < fs->clusters+2; i++)
  193. if (!get_owner(fs,i) && fs->fat[i].value &&
  194. !FAT_IS_BAD(fs,fs->fat[i].value)) {
  195. set_fat(fs,i,0);
  196. reclaimed++;
  197. }
  198. if (reclaimed)
  199. printf("Reclaimed %d unused cluster%s (%llu bytes).\n",reclaimed,
  200. reclaimed == 1 ? "" : "s",(unsigned long long)reclaimed*fs->cluster_size);
  201. }
  202. static void tag_free(DOS_FS *fs,DOS_FILE *ptr)
  203. {
  204. DOS_FILE *owner;
  205. int prev;
  206. unsigned long i,walk;
  207. for (i = 2; i < fs->clusters+2; i++)
  208. if (fs->fat[i].value && !FAT_IS_BAD(fs,fs->fat[i].value) &&
  209. !get_owner(fs,i) && !fs->fat[i].prev) {
  210. prev = 0;
  211. for (walk = i; walk > 0 && walk != -1;
  212. walk = next_cluster(fs,walk)) {
  213. if (!(owner = get_owner(fs,walk))) set_owner(fs,walk,ptr);
  214. else if (owner != ptr)
  215. die("Internal error: free chain collides with file");
  216. else {
  217. set_fat(fs,prev,-1);
  218. break;
  219. }
  220. prev = walk;
  221. }
  222. }
  223. }
  224. void reclaim_file(DOS_FS *fs)
  225. {
  226. DOS_FILE dummy;
  227. int reclaimed,files,changed;
  228. unsigned long i,next,walk;
  229. if (verbose)
  230. printf("Reclaiming unconnected clusters.\n");
  231. for (i = 2; i < fs->clusters+2; i++) fs->fat[i].prev = 0;
  232. for (i = 2; i < fs->clusters+2; i++) {
  233. next = fs->fat[i].value;
  234. if (!get_owner(fs,i) && next && next < fs->clusters+2) {
  235. if (get_owner(fs,next) || !fs->fat[next].value ||
  236. FAT_IS_BAD(fs,fs->fat[next].value)) set_fat(fs,i,-1);
  237. else fs->fat[next].prev++;
  238. }
  239. }
  240. do {
  241. tag_free(fs,&dummy);
  242. changed = 0;
  243. for (i = 2; i < fs->clusters+2; i++)
  244. if (fs->fat[i].value && !FAT_IS_BAD(fs,fs->fat[i].value) &&
  245. !get_owner(fs, i)) {
  246. if (!fs->fat[fs->fat[i].value].prev--)
  247. die("Internal error: prev going below zero");
  248. set_fat(fs,i,-1);
  249. changed = 1;
  250. printf("Broke cycle at cluster %lu in free chain.\n",i);
  251. break;
  252. }
  253. }
  254. while (changed);
  255. files = reclaimed = 0;
  256. for (i = 2; i < fs->clusters+2; i++)
  257. if (get_owner(fs,i) == &dummy && !fs->fat[i].prev) {
  258. DIR_ENT de;
  259. loff_t offset;
  260. files++;
  261. offset = alloc_rootdir_entry(fs,&de,"FSCK%04dREC");
  262. de.start = CT_LE_W(i&0xffff);
  263. if (fs->fat_bits == 32)
  264. de.starthi = CT_LE_W(i>>16);
  265. for (walk = i; walk > 0 && walk != -1;
  266. walk = next_cluster(fs,walk)) {
  267. de.size = CT_LE_L(CF_LE_L(de.size)+fs->cluster_size);
  268. reclaimed++;
  269. }
  270. fs_write(offset,sizeof(DIR_ENT),&de);
  271. }
  272. if (reclaimed)
  273. printf("Reclaimed %d unused cluster%s (%llu bytes) in %d chain%s.\n",
  274. reclaimed,reclaimed == 1 ? "" : "s",(unsigned long long)reclaimed*fs->cluster_size,files,
  275. files == 1 ? "" : "s");
  276. }
  277. unsigned long update_free(DOS_FS *fs)
  278. {
  279. unsigned long i;
  280. unsigned long free = 0;
  281. int do_set = 0;
  282. for (i = 2; i < fs->clusters+2; i++)
  283. if (!get_owner(fs,i) && !FAT_IS_BAD(fs,fs->fat[i].value))
  284. ++free;
  285. if (!fs->fsinfo_start)
  286. return free;
  287. if (verbose)
  288. printf("Checking free cluster summary.\n");
  289. if (fs->free_clusters >= 0) {
  290. if (free != fs->free_clusters) {
  291. printf( "Free cluster summary wrong (%ld vs. really %ld)\n",
  292. fs->free_clusters,free);
  293. if (interactive)
  294. printf( "1) Correct\n2) Don't correct\n" );
  295. else printf( " Auto-correcting.\n" );
  296. if (!interactive || get_key("12","?") == '1')
  297. do_set = 1;
  298. }
  299. }
  300. else {
  301. printf( "Free cluster summary uninitialized (should be %ld)\n", free );
  302. if (interactive)
  303. printf( "1) Set it\n2) Leave it uninitialized\n" );
  304. else printf( " Auto-setting.\n" );
  305. if (!interactive || get_key("12","?") == '1')
  306. do_set = 1;
  307. }
  308. if (do_set) {
  309. fs->free_clusters = free;
  310. free = CT_LE_L(free);
  311. fs_write(fs->fsinfo_start+offsetof(struct info_sector,free_clusters),
  312. sizeof(free),&free);
  313. }
  314. return free;
  315. }
  316. /* Local Variables: */
  317. /* tab-width: 8 */
  318. /* End: */