1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
|
/*
File: fat.c
Copyright (C) 1998-2004 Christophe GRENIER <grenier@cgsecurity.org>
This software is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <ctype.h> /* toupper */
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#include <time.h>
#include "types.h"
#include "common.h"
#include "fat.h"
#include "lang.h"
#include "fnctdsk.h"
#include "testdisk.h"
#include "intrf.h"
static int set_FAT_info(t_param_disk *disk_car, const struct fat_boot_sector *fat_header, t_partition *partition,const int debug);
int dump_fat_rapport(const struct fat_boot_sector*fh1, const upart_type_t upart_type, const unsigned int sector_size)
{
ecrit_rapport("sector_size %u\n", fat_sector_size(fh1));
ecrit_rapport("cluster_size %u\n", fh1->cluster_size);
ecrit_rapport("reserved %u\n", le16(fh1->reserved));
ecrit_rapport("fats %u\n", fh1->fats);
ecrit_rapport("dir_entries %u\n", get_dir_entries(fh1));
ecrit_rapport("sectors %u\n", sectors(fh1));
ecrit_rapport("media %02X\n", fh1->media);
ecrit_rapport("fat_length %u\n", le16(fh1->fat_length));
ecrit_rapport("secs_track %u\n", le16(fh1->secs_track));
ecrit_rapport("heads %u\n", le16(fh1->heads));
ecrit_rapport("hidden %u\n", (unsigned int)le32(fh1->hidden));
ecrit_rapport("total_sect %u\n", (unsigned int)le32(fh1->total_sect));
if(upart_type==UP_FAT32)
{
ecrit_rapport("fat32_length %u\n", (unsigned int)le32(fh1->fat32_length));
ecrit_rapport("flags %04X\n", le16(fh1->flags));
ecrit_rapport("version %u.%u\n", fh1->version[0], fh1->version[1]);
ecrit_rapport("root_cluster %u\n", (unsigned int)le32(fh1->root_cluster));
ecrit_rapport("info_sector %u\n", le16(fh1->info_sector));
ecrit_rapport("backup_boot %u\n", le16(fh1->backup_boot));
if(fat32_get_free_count((const unsigned char*)fh1,sector_size)==0xFFFFFFFF)
ecrit_rapport("free_count uninitialised\n");
else
ecrit_rapport("free_count %lu\n",fat32_get_free_count((const unsigned char*)fh1,sector_size));
if(fat32_get_next_free((const unsigned char*)fh1,sector_size)==0xFFFFFFFF)
ecrit_rapport("next_free uninitialised\n");
else
ecrit_rapport("next_free %lu\n",fat32_get_next_free((const unsigned char*)fh1,sector_size));
}
return 0;
}
int dump_fat_info(const struct fat_boot_sector*fh1, const upart_type_t upart_type, const unsigned int sector_size)
{
switch(upart_type)
{
case UP_FAT12:
wdoprintf(stdscr,"FAT : 12\n");
break;
case UP_FAT16:
wdoprintf(stdscr,"FAT : 16\n");
break;
case UP_FAT32:
wdoprintf(stdscr,"FAT : 32\n");
break;
default:
wdoprintf(stdscr,"Not a FAT\n");
return 0;
}
wdoprintf(stdscr,"cluster_size %u\n", fh1->cluster_size);
wdoprintf(stdscr,"reserved %u\n", le16(fh1->reserved));
if(sectors(fh1)!=0)
wdoprintf(stdscr,"sectors %u\n", sectors(fh1));
if(le32(fh1->total_sect)!=0)
wdoprintf(stdscr,"total_sect %u\n", (unsigned int)le32(fh1->total_sect));
if(upart_type==UP_FAT32)
{
wdoprintf(stdscr,"fat32_length %u\n", (unsigned int)le32(fh1->fat32_length));
wdoprintf(stdscr,"root_cluster %u\n", (unsigned int)le32(fh1->root_cluster));
wdoprintf(stdscr,"flags %04X\n", le16(fh1->flags));
wdoprintf(stdscr,"version %u.%u\n", fh1->version[0], fh1->version[1]);
wdoprintf(stdscr,"root_cluster %u\n", (unsigned int)le32(fh1->root_cluster));
wdoprintf(stdscr,"info_sector %u\n", le16(fh1->info_sector));
wdoprintf(stdscr,"backup_boot %u\n", le16(fh1->backup_boot));
if(fat32_get_free_count((const unsigned char*)fh1,sector_size)==0xFFFFFFFF)
wdoprintf(stdscr,"free_count uninitialised\n");
else
wdoprintf(stdscr,"free_count %lu\n",fat32_get_free_count((const unsigned char*)fh1,sector_size));
if(fat32_get_next_free((const unsigned char*)fh1,sector_size)==0xFFFFFFFF)
wdoprintf(stdscr,"next_free uninitialised\n");
else
wdoprintf(stdscr,"next_free %lu\n",fat32_get_next_free((const unsigned char*)fh1,sector_size));
} else {
wdoprintf(stdscr,"fat_length %u\n", le16(fh1->fat_length));
wdoprintf(stdscr,"dir_entries %u\n", get_dir_entries(fh1));
}
return 0;
}
int dump_2fat_info(const struct fat_boot_sector*fh1, const struct fat_boot_sector*fh2, const upart_type_t upart_type, const unsigned int sector_size)
{
switch(upart_type)
{
case UP_FAT12:
wdoprintf(stdscr,"FAT : 12\n");
break;
case UP_FAT16:
wdoprintf(stdscr,"FAT : 16\n");
break;
case UP_FAT32:
wdoprintf(stdscr,"FAT : 32\n");
break;
default:
wdoprintf(stdscr,"Not a FAT\n");
return 0;
}
wdoprintf(stdscr,"cluster_size %u %u\n", fh1->cluster_size, fh2->cluster_size);
wdoprintf(stdscr,"reserved %u %u\n", le16(fh1->reserved),le16(fh2->reserved));
if(sectors(fh1)!=0 || sectors(fh2)!=0)
wdoprintf(stdscr,"sectors %u %u\n", sectors(fh1), sectors(fh2));
if(le32(fh1->total_sect)!=0 || le32(fh2->total_sect)!=0)
wdoprintf(stdscr,"total_sect %u %u\n", (unsigned int)le32(fh1->total_sect), (unsigned int)le32(fh2->total_sect));
if(upart_type==UP_FAT32)
{
wdoprintf(stdscr,"fat32_length %u %u\n", (unsigned int)le16(fh1->fat32_length), (unsigned int)le16(fh2->fat32_length));
wdoprintf(stdscr,"root_cluster %u %u\n", (unsigned int)le32(fh1->root_cluster), (unsigned int)le32(fh2->root_cluster));
wdoprintf(stdscr,"free_count ");
if(fat32_get_free_count((const unsigned char*)fh1,sector_size)==0xFFFFFFFF)
wdoprintf(stdscr,"uninitialised ");
else
wdoprintf(stdscr,"%lu ",fat32_get_free_count((const unsigned char*)fh1,sector_size));
if(fat32_get_free_count((const unsigned char*)fh2,sector_size)==0xFFFFFFFF)
wdoprintf(stdscr,"uninitialised");
else
wdoprintf(stdscr,"%lu",fat32_get_free_count((const unsigned char*)fh2,sector_size));
wdoprintf(stdscr,"\nnext_free ");
if(fat32_get_next_free((const unsigned char*)fh1,sector_size)==0xFFFFFFFF)
wdoprintf(stdscr,"uninitialised ");
else
wdoprintf(stdscr,"%lu ",fat32_get_next_free((const unsigned char*)fh1,sector_size));
if(fat32_get_next_free((const unsigned char*)fh2,sector_size)==0xFFFFFFFF)
wdoprintf(stdscr,"uninitialised\n");
else
wdoprintf(stdscr,"%lu\n",fat32_get_next_free((const unsigned char*)fh2,sector_size));
} else {
wdoprintf(stdscr,"fat_length %u %u\n", le16(fh1->fat_length), le16(fh2->fat_length));
wdoprintf(stdscr,"dir_entries %u %u\n", get_dir_entries(fh1), get_dir_entries(fh2));
}
return 0;
}
int dump_fat_info_rapport(const struct fat_boot_sector*fh1, const upart_type_t upart_type, const unsigned int sector_size)
{
ecrit_rapport("cluster_size %u\n", fh1->cluster_size);
ecrit_rapport("reserved %u\n", le16(fh1->reserved));
if(sectors(fh1)!=0)
ecrit_rapport("sectors %u\n", sectors(fh1));
if(le32(fh1->total_sect)!=0)
ecrit_rapport("total_sect %u\n", (unsigned int)le32(fh1->total_sect));
if(upart_type==UP_FAT32)
{
ecrit_rapport("fat32_length %u\n", (unsigned int)le32(fh1->fat32_length));
ecrit_rapport("root_cluster %u\n", (unsigned int)le32(fh1->root_cluster));
if(fat32_get_free_count((const unsigned char*)fh1,sector_size)==0xFFFFFFFF)
ecrit_rapport("free_count uninitialised\n");
else
ecrit_rapport("free_count %lu\n",fat32_get_free_count((const unsigned char*)fh1,sector_size));
if(fat32_get_next_free((const unsigned char*)fh1,sector_size)==0xFFFFFFFF)
ecrit_rapport("next_free uninitialised\n");
else
ecrit_rapport("next_free %lu\n",fat32_get_next_free((const unsigned char*)fh1,sector_size));
} else {
ecrit_rapport("fat_length %u\n", le16(fh1->fat_length));
ecrit_rapport("dir_entries %u\n", get_dir_entries(fh1));
}
return 0;
}
int dump_2fat_rapport(const struct fat_boot_sector*fh1, const struct fat_boot_sector*fh2, const upart_type_t upart_type, const unsigned int sector_size)
{
switch(upart_type)
{
case UP_FAT12:
ecrit_rapport("\nFAT12\n");
break;
case UP_FAT16:
ecrit_rapport("\nFAT16\n");
break;
case UP_FAT32:
ecrit_rapport("\nFAT32\n");
break;
default:
return 1;
}
ecrit_rapport("sector_size %u %u\n", fat_sector_size(fh1),fat_sector_size(fh2));
ecrit_rapport("cluster_size %u %u\n", fh1->cluster_size,fh2->cluster_size);
ecrit_rapport("reserved %u %u\n", le16(fh1->reserved),le16(fh2->reserved));
ecrit_rapport("fats %u %u\n", fh1->fats,fh2->fats);
ecrit_rapport("dir_entries %u %u\n", get_dir_entries(fh1),get_dir_entries(fh2));
ecrit_rapport("sectors %u %u\n", sectors(fh1),sectors(fh2));
ecrit_rapport("media %02X %02X\n", fh1->media,fh2->media);
ecrit_rapport("fat_length %u %u\n", le16(fh1->fat_length),le16(fh2->fat_length));
ecrit_rapport("secs_track %u %u\n", le16(fh1->secs_track),le16(fh2->secs_track));
ecrit_rapport("heads %u %u\n", le16(fh1->heads),le16(fh2->heads));
ecrit_rapport("hidden %u %u\n", (unsigned int)le32(fh1->hidden),(unsigned int)le32(fh2->hidden));
ecrit_rapport("total_sect %u %u\n", (unsigned int)le32(fh1->total_sect),(unsigned int)le32(fh2->total_sect));
if(upart_type==UP_FAT32)
{
ecrit_rapport("fat32_length %u %u\n", (unsigned int)le32(fh1->fat32_length),(unsigned int)le32(fh2->fat32_length));
ecrit_rapport("flags %04X %04X\n", le16(fh1->flags),le16(fh2->flags));
ecrit_rapport("version %u.%u %u.%u\n", fh1->version[0], fh1->version[1],fh2->version[0], fh2->version[1]);
ecrit_rapport("root_cluster %u %u\n", (unsigned int)le32(fh1->root_cluster),(unsigned int)le32(fh2->root_cluster));
ecrit_rapport("info_sector %u %u\n", le16(fh1->info_sector),le16(fh2->info_sector));
ecrit_rapport("backup_boot %u %u\n", le16(fh1->backup_boot),le16(fh2->backup_boot));
ecrit_rapport("free_count ");
if(fat32_get_free_count((const unsigned char*)fh1,sector_size)==0xFFFFFFFF)
ecrit_rapport("uninitialised ");
else
ecrit_rapport("%lu ",fat32_get_free_count((const unsigned char*)fh1,sector_size));
if(fat32_get_free_count((const unsigned char*)fh2,sector_size)==0xFFFFFFFF)
ecrit_rapport("uninitialised");
else
ecrit_rapport("%lu",fat32_get_free_count((const unsigned char*)fh2,sector_size));
ecrit_rapport("\nnext_free ");
if(fat32_get_next_free((const unsigned char*)fh1,sector_size)==0xFFFFFFFF)
ecrit_rapport("uninitialised ");
else
ecrit_rapport("%lu ",fat32_get_next_free((const unsigned char*)fh1,sector_size));
if(fat32_get_next_free((const unsigned char*)fh2,sector_size)==0xFFFFFFFF)
ecrit_rapport("uninitialised\n");
else
ecrit_rapport("%lu\n",fat32_get_next_free((const unsigned char*)fh2,sector_size));
}
return 0;
}
int check_FAT(t_param_disk *disk_car,t_partition *partition,const int debug)
{
unsigned char buffer[3*disk_car->sector_size];
if(disk_car->read(disk_car,sizeof(buffer), &buffer, partition->part_offset)!=0)
{
aff_buffer(BUFFER_ADD,msg_CHKFAT_RERR);
ecrit_rapport(msg_CHKFAT_RERR);
return 1;
}
if(test_FAT(disk_car,(const struct fat_boot_sector *)buffer,partition,debug,0)!=0)
{
if(debug!=0)
{
ecrit_rapport("\n\ntest_FAT()\n");
aff_part_rapport(disk_car,partition);
dump_fat_rapport((const struct fat_boot_sector*)buffer, partition->upart_type,disk_car->sector_size);
}
return 1;
}
set_FAT_info(disk_car,(const struct fat_boot_sector *)buffer,partition,debug);
/* aff_buffer(BUFFER_ADD,"Ok\n"); */
return 0;
}
static int set_FAT_info(t_param_disk *disk_car, const struct fat_boot_sector *fat_header, t_partition *partition,const int debug)
{
const char*buffer=(const char*)fat_header;
partition->name[0]=0;
switch(partition->upart_type)
{
case UP_FAT12:
snprintf(partition->info,sizeof(partition->info),"FAT12");
if(buffer[38]==0x29) /* BS_BootSig */
{
set_part_name(partition,((const char*)fat_header)+FAT1X_PART_NAME,11);
if(check_volume_name(partition->name,11))
partition->name[0]='\0';
}
break;
case UP_FAT16:
snprintf(partition->info,sizeof(partition->info),"FAT16");
if(buffer[38]==0x29) /* BS_BootSig */
{
set_part_name(partition,((const char*)fat_header)+FAT1X_PART_NAME,11);
if(check_volume_name(partition->name,11))
partition->name[0]='\0';
}
break;
case UP_FAT32:
snprintf(partition->info,sizeof(partition->info),"FAT32");
fat32_set_part_name(disk_car,partition,fat_header);
break;
default:
ecrit_rapport("set_FAT_info unknown upart_type\n");
return 1;
}
return 0;
}
unsigned int get_next_cluster(t_param_disk *disk_car,const t_partition *partition, const upart_type_t upart_type,const int offset, const unsigned int cluster)
{
/* Offset can be offset to FAT1 or to FAT2 */
/* ecrit_rapport("get_next_cluster(upart_type=%u,offset=%u,cluster=%u\n",upart_type,offset,cluster); */
switch(upart_type)
{
case UP_FAT12:
{
unsigned char buffer[0x400];
unsigned long int offset_s,offset_o;
offset_s=(cluster+cluster/2)/disk_car->sector_size;
offset_o=(cluster+cluster/2)%disk_car->sector_size;
if(disk_car->read(disk_car,sizeof(buffer), &buffer, partition->part_offset+(uint64_t)(offset+offset_s)*disk_car->sector_size)!=0)
{
ecrit_rapport("\nget_next_cluster error"); return 0;
}
if((cluster&1)!=0)
return le16((*((uint16_t*)&buffer[offset_o])))>>4;
else
return le16(*((uint16_t*)&buffer[offset_o]))&0x0FFF;
}
case UP_FAT16:
{
unsigned char buffer[disk_car->sector_size];
uint16_t *p16=(uint16_t*)&buffer;
unsigned long int offset_s,offset_o;
offset_s=cluster/(disk_car->sector_size/2);
offset_o=cluster%(disk_car->sector_size/2);
if(disk_car->read(disk_car,sizeof(buffer), &buffer, partition->part_offset+(uint64_t)(offset+offset_s)*disk_car->sector_size)!=0)
{
ecrit_rapport("\nget_next_cluster error"); return 0;
}
return le16(p16[offset_o]);
}
case UP_FAT32:
{
unsigned char buffer[disk_car->sector_size];
uint32_t *p32=(uint32_t*)&buffer;
unsigned long int offset_s,offset_o;
offset_s=cluster/(disk_car->sector_size/4);
offset_o=cluster%(disk_car->sector_size/4);
if(disk_car->read(disk_car,sizeof(buffer), &buffer, partition->part_offset+(uint64_t)(offset+offset_s)*disk_car->sector_size)!=0)
{
ecrit_rapport("\nget_next_cluster error"); return 0;
}
/* FAT32 used 28 bits, the 4 high bits are reserved
* 0x00000000: free cluster
* 0x0FFFFFF7: bad cluster
* 0x0FFFFFF8+: EOC End of cluster
* */
return le32(p32[offset_o])&0xFFFFFFF;
}
default:
ecrit_rapport("fat.c get_next_cluster unknown fat type\n");
return 0;
}
}
int set_next_cluster(t_param_disk *disk_car,const t_partition *partition, const upart_type_t upart_type,const int offset, const unsigned int cluster, const unsigned int next_cluster)
{
unsigned char buffer[0x400];
unsigned long int offset_s,offset_o;
/* Offset can be offset to FAT1 or to FAT2 */
/* ecrit_rapport("set_next_cluster(upart_type=%u,offset=%u,cluster=%u,next_cluster=%u)\n",upart_type,offset,cluster,next_cluster); */
switch(upart_type)
{
case UP_FAT12:
{
offset_s=(cluster+cluster/2)/disk_car->sector_size;
offset_o=(cluster+cluster/2)%disk_car->sector_size;
if(disk_car->read(disk_car,sizeof(buffer), &buffer, partition->part_offset+(uint64_t)(offset+offset_s)*disk_car->sector_size)!=0)
{
ecrit_rapport("\nset_next_cluster read error"); return 1;
}
if((cluster&1)!=0)
(*((uint16_t*)&buffer[offset_o]))=le16((next_cluster<<4) | (le16(*((uint16_t*)&buffer[offset_o]))&0xF));
else
(*((uint16_t*)&buffer[offset_o]))=le16((next_cluster) | (le16(*((uint16_t*)&buffer[offset_o]))&0xF000));
}
break;
case UP_FAT16:
{
uint16_t *p16=(uint16_t*)&buffer;
offset_s=cluster/(disk_car->sector_size/2);
offset_o=cluster%(disk_car->sector_size/2);
if(disk_car->read(disk_car,sizeof(buffer), &buffer, partition->part_offset+(uint64_t)(offset+offset_s)*disk_car->sector_size)!=0)
{
ecrit_rapport("\nset_next_cluster read error"); return 1;
}
p16[offset_o]=le16(next_cluster);
}
break;
case UP_FAT32:
{
uint32_t *p32=(uint32_t*)&buffer;
offset_s=cluster/(disk_car->sector_size/4);
offset_o=cluster%(disk_car->sector_size/4);
if(disk_car->read(disk_car,sizeof(buffer), &buffer, partition->part_offset+(uint64_t)(offset+offset_s)*disk_car->sector_size)!=0)
{
ecrit_rapport("\nset_next_cluster read error");
return 1;
}
/* FAT32 used 28 bits, the 4 high bits are reserved
* 0x00000000: free cluster
* 0x0FFFFFF7: bad cluster
* 0x0FFFFFF8+: EOC End of cluster
* */
p32[offset_o]=le32(next_cluster);
}
break;
default:
ecrit_rapport("fat.c set_next_cluster unknown fat type\n");
return 1;
}
if(disk_car->write(disk_car,sizeof(buffer), &buffer, partition->part_offset+(uint64_t)(offset+offset_s)*disk_car->sector_size)!=0)
{
ecrit_rapport("\nset_next_cluster write error");
return 1;
}
return 0;
}
unsigned int fat32_get_prev_cluster(t_param_disk *disk_car,const t_partition *partition, const unsigned int fat_offset, const unsigned int cluster, const unsigned int no_of_cluster)
{
unsigned char buffer[disk_car->sector_size];
uint32_t *p32=(uint32_t*)&buffer;
uint64_t hd_offset=partition->part_offset+fat_offset*disk_car->sector_size;
unsigned int prev_cluster;
for(prev_cluster=2;prev_cluster<=no_of_cluster+1;prev_cluster++)
{
unsigned int offset_s,offset_o;
offset_s=prev_cluster/(disk_car->sector_size/4);
offset_o=prev_cluster%(disk_car->sector_size/4);
if((offset_o==0)||(prev_cluster==2))
{
if(disk_car->read(disk_car,sizeof(buffer), &buffer, hd_offset)!=0)
{
ecrit_rapport("fat32_get_prev_cluster error\n"); return 0;
}
hd_offset+=disk_car->sector_size;
}
if((le32(p32[offset_o]) & 0xFFFFFFF) ==cluster)
return prev_cluster;
}
return 0;
}
unsigned int get_prev_cluster(t_param_disk *disk_car,const t_partition *partition, const upart_type_t upart_type,const int offset, const unsigned int cluster, const unsigned int no_of_cluster)
{
unsigned int prev_cluster;
for(prev_cluster=2;prev_cluster<=no_of_cluster+1;prev_cluster++)
{
if(get_next_cluster(disk_car,partition,upart_type,offset, prev_cluster)==cluster)
return prev_cluster;
}
return 0;
}
int test_FAT(t_param_disk *disk_car,const struct fat_boot_sector *fat_header, t_partition *partition,const int debug, const int dump_ind)
{
unsigned long int start_fat1,start_fat2,start_rootdir,start_data,no_of_cluster,fat_length,fat_length_calc,part_size,end_data;
const char *buffer=(const char*)fat_header;
if(debug>1)
{
ecrit_rapport("test_FAT\n");
aff_part_rapport(disk_car,partition);
}
if(le16(fat_header->marker)!=0xAA55)
{
aff_buffer(BUFFER_ADD,"test_FAT : Boot sector doesn't have the endmark 0xAA55\n");
ecrit_rapport("test_FAT : Boot sector doesn't have the endmark 0xAA55\n");
return 1;
}
if(dump_ind!=0)
dump(stdscr,fat_header,DEFAULT_SECTOR_SIZE);
if(!((fat_header->ignored[0]==0xeb && fat_header->ignored[2]==0x90)||fat_header->ignored[0]==0xe9))
{
aff_buffer(BUFFER_ADD,msg_CHKFAT_BAD_JUMP);
ecrit_rapport(msg_CHKFAT_BAD_JUMP);
return 1;
}
if(fat_sector_size(fat_header)!=disk_car->sector_size)
{
aff_buffer(BUFFER_ADD,"check_FAT: Incorrect number of bytes per sector %u (FAT) != %u (HD)\n",fat_sector_size(fat_header),disk_car->sector_size);
ecrit_rapport("check_FAT: Incorrect number of bytes per sector %u (FAT) != %u (HD)\n",fat_sector_size(fat_header),disk_car->sector_size);
return 1;
}
switch(fat_header->cluster_size)
{
case 1:
case 2:
case 4:
case 8:
case 16:
case 32:
case 64:
case 128:
break;
default:
aff_buffer(BUFFER_ADD,msg_CHKFAT_SECT_CLUSTER);
ecrit_rapport(msg_CHKFAT_SECT_CLUSTER);
return 1;
}
if(fat_header->fats!=2)
{
aff_buffer(BUFFER_ADD,msg_CHKFAT_NBRFAT);
ecrit_rapport(msg_CHKFAT_NBRFAT);
return 1;
}
if(fat_header->media!=0xF0 && fat_header->media<0xF8)
{ /* Legal values are 0xF0, 0xF8-0xFF */
aff_buffer(BUFFER_ADD,"check_FAT: Bad media descriptor (0x%2x!=0xF8)\n",fat_header->media);
ecrit_rapport("check_FAT: Bad media descriptor (0x%2x!=0xF8)\n",fat_header->media);
return 1;
}
if(fat_header->media!=0xF8)
{ /* the only value I have ever seen is 0xF8 */
ecrit_rapport("check_FAT: Unusual media descriptor (0x%2x!=0xF8)\n",fat_header->media);
}
fat_length=le16(fat_header->fat_length)>0?le16(fat_header->fat_length):le32(fat_header->fat32_length);
part_size=(sectors(fat_header)>0?sectors(fat_header):fat_header->total_sect);
start_fat1=le16(fat_header->reserved);
start_fat2=start_fat1+fat_length;
start_data=start_fat2+fat_length+(get_dir_entries(fat_header)*32+disk_car->sector_size-1)/disk_car->sector_size;
no_of_cluster=(part_size-start_data)/fat_header->cluster_size;
end_data=start_data+no_of_cluster*fat_header->cluster_size-1;
if(debug>1)
{
ecrit_rapport("number of cluster = %lu\n",no_of_cluster);
}
if(no_of_cluster<4085)
{
if(debug>0)
{
ecrit_rapport("FAT12\n");
}
if(sectors(fat_header)==0)
{
aff_buffer(BUFFER_ADD,msg_CHKFAT_SIZE);
ecrit_rapport(msg_CHKFAT_SIZE);
}
if(le16(fat_header->reserved)!=1)
{
aff_buffer(BUFFER_ADD,"check_FAT: Unusual number of reserved sectors %u (FAT), should be 1.\n",le16(fat_header->reserved));
ecrit_rapport("check_FAT: Unusual number of reserved sectors %u (FAT), should be 1.\n",le16(fat_header->reserved));
}
if((get_dir_entries(fat_header)==0)||(get_dir_entries(fat_header)%16!=0))
{
aff_buffer(BUFFER_ADD,msg_CHKFAT_ENTRY);
ecrit_rapport(msg_CHKFAT_ENTRY);
return 1;
}
if((le16(fat_header->fat_length)>256)||(le16(fat_header->fat_length)==0))
{
aff_buffer(BUFFER_ADD,msg_CHKFAT_SECTPFAT);
ecrit_rapport(msg_CHKFAT_SECTPFAT);
return 1;
}
start_rootdir=start_fat2+fat_length;
fat_length_calc=((no_of_cluster+2+disk_car->sector_size*2/3-1)*3/2/disk_car->sector_size);
partition->upart_type=UP_FAT12;
if(memcmp(buffer+FAT_NAME1,"FAT12 ",8)!=0) /* 2 Mo max */
{
aff_buffer(BUFFER_ADD,"Should be marked as FAT12\n");
ecrit_rapport("Should be marked as FAT12\n");
}
}
else if(no_of_cluster<65525)
{
if(debug>0)
{
ecrit_rapport("FAT16\n");
}
if(le16(fat_header->reserved)!=1)
{
aff_buffer(BUFFER_ADD,"check_FAT: Unusual number of reserved sectors %u (FAT), should be 1.\n",le16(fat_header->reserved));
ecrit_rapport("check_FAT: Unusual number of reserved sectors %u (FAT), should be 1.\n",le16(fat_header->reserved));
}
if(le16(fat_header->fat_length)==0)
{
aff_buffer(BUFFER_ADD,msg_CHKFAT_SECTPFAT);
ecrit_rapport(msg_CHKFAT_SECTPFAT);
return 1;
}
if((get_dir_entries(fat_header)==0)||(get_dir_entries(fat_header)%16!=0))
{
aff_buffer(BUFFER_ADD,msg_CHKFAT_ENTRY);
ecrit_rapport(msg_CHKFAT_ENTRY);
return 1;
}
start_rootdir=start_fat2+fat_length;
fat_length_calc=((no_of_cluster+2+disk_car->sector_size/2-1)*2/disk_car->sector_size);
partition->upart_type=UP_FAT16;
if(memcmp(buffer+FAT_NAME1,"FAT16 ",8)!=0)
{
aff_buffer(BUFFER_ADD,"Should be marked as FAT16\n");
ecrit_rapport("Should be marked as FAT16\n");
}
}
else
{
if(debug>0)
{
ecrit_rapport("FAT32\n");
}
if(sectors(fat_header)!=0)
{
aff_buffer(BUFFER_ADD,msg_CHKFAT_SIZE);
ecrit_rapport(msg_CHKFAT_SIZE);
return 1;
}
if(get_dir_entries(fat_header)!=0)
{
aff_buffer(BUFFER_ADD,msg_CHKFAT_ENTRY);
ecrit_rapport(msg_CHKFAT_ENTRY);
return 1;
}
if((fat_header->version[0]!=0) || (fat_header->version[1]!=0))
{
aff_buffer(BUFFER_ADD,msg_CHKFAT_BADFAT32VERSION);
ecrit_rapport(msg_CHKFAT_BADFAT32VERSION);
}
if((le32(fat_header->root_cluster)<2) ||(le32(fat_header->root_cluster)>=2+no_of_cluster))
{
aff_buffer(BUFFER_ADD,"Bad root_cluster\n");
ecrit_rapport("Bad root_cluster\n");
return 1;
}
start_rootdir=start_data+(le32(fat_header->root_cluster)-2)*fat_header->cluster_size;
fat_length_calc=((no_of_cluster+2+disk_car->sector_size/4-1)*4/disk_car->sector_size);
partition->upart_type=UP_FAT32;
if(memcmp(buffer+FAT_NAME2,"FAT32 ",8)!=0)
{
aff_buffer(BUFFER_ADD,"Should be marked as FAT32\n");
ecrit_rapport("Should be marked as FAT32\n");
}
}
if(partition->part_size>0)
{
if(part_size>partition->part_size/disk_car->sector_size)
{
ecrit_rapport("test_FAT size boot_sector %lu > partition %lu\n",(long unsigned)part_size,(long unsigned)(partition->part_size/disk_car->sector_size));
aff_buffer(BUFFER_ADD,msg_CHKFAT_SIZE);
return 1;
}
else
{
if((debug>0) && (part_size!=partition->part_size))
ecrit_rapport("test_FAT size boot_sector %lu, partition %lu\n",(long unsigned)part_size,(long unsigned)(partition->part_size/disk_car->sector_size));
}
}
if(debug>0)
{
ecrit_rapport("FAT1 : %lu-%lu\n",start_fat1,start_fat1+fat_length-1);
ecrit_rapport("FAT2 : %lu-%lu\n",start_fat2,start_fat2+fat_length-1);
ecrit_rapport("start_rootdir : %lu",start_rootdir);
if(partition->upart_type==UP_FAT32)
ecrit_rapport(" root cluster : %u",(unsigned int)le32(fat_header->root_cluster));
ecrit_rapport("\nData : %lu-%lu\n",start_data,end_data);
ecrit_rapport("sectors : %lu\n",part_size);
ecrit_rapport("cluster_size : %u\n",fat_header->cluster_size);
ecrit_rapport("no_of_cluster : %lu (2 - %lu)\n", no_of_cluster,no_of_cluster+1);
ecrit_rapport("fat_length %lu calculated %lu\n",fat_length,fat_length_calc);
}
if(fat_length<fat_length_calc)
{ aff_buffer(BUFFER_ADD,msg_CHKFAT_SECTPFAT); return 1; }
comp_FAT(disk_car,partition,fat_length,le16(fat_header->reserved));
if(le16(fat_header->secs_track)!=disk_car->CHS.sector)
{
aff_buffer(BUFFER_ADD,"check_FAT: Incorrect number of sectors per track %u (FAT) != %u (HD)\n",le16(fat_header->secs_track),disk_car->CHS.sector);
ecrit_rapport("sect/track %u (FAT) != %u (HD)\n",le16(fat_header->secs_track),disk_car->CHS.sector);
}
if(le16(fat_header->heads)!=disk_car->CHS.head+1)
{
aff_buffer(BUFFER_ADD,"check_FAT: Incorrect number of heads/cylinder %u (FAT) != %u (HD)\n",le16(fat_header->heads),disk_car->CHS.head+1);
ecrit_rapport("heads/cylinder %u (FAT) != %u (HD)\n",le16(fat_header->heads),disk_car->CHS.head+1);
}
return 0;
}
int comp_FAT(t_param_disk *disk_car,const t_partition *partition,const unsigned long int taille,const unsigned long int sect_res)
{
/*
return 0 if FATs match
*/
unsigned int reste=taille;
unsigned int read_size;
unsigned char buffer[NBR_SECT*disk_car->sector_size],buffer2[NBR_SECT*disk_car->sector_size];
uint64_t hd_offset, hd_offset2;
hd_offset=partition->part_offset+(uint64_t)sect_res*disk_car->sector_size;
hd_offset2=hd_offset+(uint64_t)taille*disk_car->sector_size;
if(reste>1000) reste=1000; /* Quick check ! */
while(reste)
{
read_size=reste>NBR_SECT?NBR_SECT:reste;
reste-=read_size;
if(disk_car->read(disk_car,read_size*disk_car->sector_size, &buffer, hd_offset))
{ ecrit_rapport(msg_CHKFAT_RERR);
return 1;}
if(disk_car->read(disk_car,read_size*disk_car->sector_size, &buffer2, hd_offset2))
{ ecrit_rapport(msg_CHKFAT_RERR);
return 1;}
if(memcmp(buffer,buffer2,disk_car->sector_size*read_size)!=0)
{
ecrit_rapport("FAT differs, FAT sectors=%lu-%lu/%lu\n",
(unsigned long) ((hd_offset-partition->part_offset)/disk_car->sector_size-sect_res),
(unsigned long) ((hd_offset-partition->part_offset)/disk_car->sector_size-sect_res+read_size),
taille);
return 1;
}
hd_offset+=read_size*disk_car->sector_size;
hd_offset2+=read_size*disk_car->sector_size;
}
return 0;
}
unsigned int fat_sector_size(const struct fat_boot_sector *fat_header)
{ return (fat_header->sector_size[1]<<8)+fat_header->sector_size[0]; }
unsigned int get_dir_entries(const struct fat_boot_sector *fat_header)
{ return (fat_header->dir_entries[1]<<8)+fat_header->dir_entries[0]; }
unsigned int sectors(const struct fat_boot_sector *fat_header)
{ return (fat_header->sectors[1]<<8)+fat_header->sectors[0]; }
unsigned long int fat32_get_free_count(const unsigned char *boot_fat32, const unsigned int sector_size)
{
return (boot_fat32[sector_size+0x1E8+3]<<24)+(boot_fat32[sector_size+0x1E8+2]<<16)+(boot_fat32[sector_size+0x1E8+1]<<8)+boot_fat32[sector_size+0x1E8];
}
unsigned long int fat32_get_next_free(const unsigned char *boot_fat32, const unsigned int sector_size)
{
return (boot_fat32[sector_size+0x1EC+3]<<24)+(boot_fat32[sector_size+0x1EC+2]<<16)+(boot_fat32[sector_size+0x1EC+1]<<8)+boot_fat32[sector_size+0x1EC];
}
int recover_FAT12(t_param_disk *disk_car,const struct fat_boot_sector*fat_header, t_partition *partition, const int debug, const int dump_ind)
{
const char*buffer=(const char*)fat_header;
if(le16(fat_header->marker)==0xAA55)
{
if(memcmp(buffer+FAT_NAME1,"FAT12",5)==0) /* 2 Mo max */
{
if(debug||dump_ind)
{
ecrit_rapport("\nFAT12 at %u/%u/%u\n", offset2cylinder(disk_car,partition->part_offset),offset2head(disk_car,partition->part_offset),offset2sector(disk_car,partition->part_offset));
}
if(test_FAT(disk_car, fat_header, partition, debug, dump_ind))
return 1;
partition->part_size=(uint64_t)sectors(fat_header)*fat_sector_size(fat_header);
partition->part_type=P_12FAT;
set_FAT_info(disk_car,fat_header,partition,debug);
return 0;
}
} /* fin marqueur de fin :)) */
return 1;
}
int recover_FAT16(t_param_disk *disk_car,const struct fat_boot_sector*fat_header, t_partition *partition, const int debug, const int dump_ind)
{
const char*buffer=(const char*)fat_header;
if(le16(fat_header->marker)==0xAA55)
{
if(memcmp(buffer+FAT_NAME1,"FAT16",5)==0)
{
if(debug||dump_ind)
{
ecrit_rapport("\nFAT16 at %u/%u/%u\n", offset2cylinder(disk_car,partition->part_offset),offset2head(disk_car,partition->part_offset),offset2sector(disk_car,partition->part_offset));
}
if(test_FAT(disk_car, fat_header, partition, debug, dump_ind))
return 1;
if(sectors(fat_header)!=0)
{
partition->part_size=(uint64_t)sectors(fat_header)*fat_sector_size(fat_header);
partition->part_type=P_16FAT;
} else {
partition->part_size=(uint64_t)le32(fat_header->total_sect)*fat_sector_size(fat_header);
if(offset2cylinder(disk_car,partition->part_offset+partition->part_size-1)<=1024)
partition->part_type=P_16FATBD;
else
partition->part_type=P_16FATBD_LBA;
}
set_FAT_info(disk_car,fat_header,partition,debug);
return 0;
}
} /* fin marqueur de fin :)) */
return 1;
}
int recover_FAT32(t_param_disk *disk_car, const struct fat_boot_sector*fat_header, t_partition *partition, const int debug, const int dump_ind,const int backup)
{
const char*buffer=(const char*)fat_header;
if(le16(fat_header->marker)==0xAA55)
{
if(memcmp(buffer+FAT_NAME2,"FAT32",5)==0)
{
if(debug||dump_ind)
{
ecrit_rapport("\nFAT32 at %u/%u/%u\n", offset2cylinder(disk_car,partition->part_offset),offset2head(disk_car,partition->part_offset),offset2sector(disk_car,partition->part_offset));
}
if(test_FAT(disk_car, fat_header, partition, debug, dump_ind))
return 1;
if(sectors(fat_header)!=0)
partition->part_size=(uint64_t)sectors(fat_header)*fat_sector_size(fat_header);
else
partition->part_size=(uint64_t)le32(fat_header->total_sect)*fat_sector_size(fat_header);
if(offset2cylinder(disk_car,partition->part_offset+partition->part_size-1)<=1024)
partition->part_type=P_32FAT;
else
partition->part_type=P_32FAT_LBA;
/* partition->upart_type=UP_FAT32; already done in test_FAT */
if(backup)
{
partition->boot_sector=6;
partition->part_offset-=6*512; /* backup sector ... */
}
set_FAT_info(disk_car,fat_header,partition,debug);
return 0;
}
} /* fin marqueur de fin =:-) */
return 1;
}
int fat32_set_part_name(t_param_disk *disk_car, t_partition *partition, const struct fat_boot_sector*fat_header)
{
partition->name[0]='\0';
if((fat_header->cluster_size>0)&&(fat_header->cluster_size<=128))
{
unsigned char *buffer=(unsigned char*)MALLOC(fat_header->cluster_size*disk_car->sector_size);
if(disk_car->read(disk_car,fat_header->cluster_size*disk_car->sector_size, buffer, partition->part_offset+(uint64_t)(le16(fat_header->reserved)+fat_header->fats*le32(fat_header->fat32_length)+(le32(fat_header->root_cluster)-2)*fat_header->cluster_size)*disk_car->sector_size))
{
ecrit_rapport(msg_ROOT_CLUSTER_RERR);
}
else
{
int i;
int stop=0;
for(i=0;(i<16*fat_header->cluster_size)&&(stop==0);i++)
{ /* Test attribut volume name and check if the volume name is erased or not */
if(((buffer[i*0x20+0xB] & ATTR_EXT) !=ATTR_EXT) && ((buffer[i*0x20+0xB] & ATTR_VOLUME) !=0) && (buffer[i*0x20]!=0xE5))
{
/* dump(stdscr,&buffer[i*0x20],0x20); */
set_part_name(partition,&buffer[i*0x20],11);
if(check_volume_name(partition->name,11))
partition->name[0]='\0';
}
if(buffer[i*0x20]==0)
{
stop=1;
}
}
}
FREE(buffer);
}
if(partition->name[0]=='\0')
{
ecrit_rapport("set_FAT_info: name from BS used\n");
set_part_name(partition,((const char*)fat_header)+FAT32_PART_NAME,11);
if(check_volume_name(partition->name,11))
partition->name[0]='\0';
}
return 0;
}
int recover_HPFS(t_param_disk *disk_car,const struct fat_boot_sector*fat_header, t_partition *partition, const int debug, const int dump_ind)
{
const char*buffer=(const char*)fat_header;
if(le16(fat_header->marker)==0xAA55)
{
if(memcmp(buffer+OS2_NAME,"IBM",3)==0)
{ /* D'apres une analyse de OS2 sur systeme FAT...
FAT_NAME1=FAT
*/
if(debug||dump_ind)
{
ecrit_rapport("\nHPFS maybe at %u/%u/%u\n", offset2cylinder(disk_car,partition->part_offset),offset2head(disk_car,partition->part_offset),offset2sector(disk_car,partition->part_offset));
}
if(dump_ind)
dump(stdscr,buffer,DEFAULT_SECTOR_SIZE);
if(sectors(fat_header)!=0)
partition->part_size=(uint64_t)sectors(fat_header)*fat_sector_size(fat_header);
else
partition->part_size=(uint64_t)le32(fat_header->total_sect)*fat_sector_size(fat_header);
partition->part_type=P_HPFS;
partition->upart_type=UP_HPFS;
return 0;
}
} /* fin marqueur de fin :)) */
return 1;
}
int recover_OS2MB(t_param_disk *disk_car, const struct fat_boot_sector*fat_header, t_partition *partition, const int debug, const int dump_ind)
{
const char*buffer=(const char*)fat_header;
if(le16(fat_header->marker)==0xAA55)
{
if(memcmp(buffer+FAT_NAME1,"FAT ",8)==0)
{ /* Attention a detecter apres OS2 */
if(debug||dump_ind)
{
ecrit_rapport("\nMarker (0xAA55) at %u/%u/%u\n", offset2cylinder(disk_car,partition->part_offset),offset2head(disk_car,partition->part_offset),offset2sector(disk_car,partition->part_offset));
}
if(dump_ind)
dump(stdscr,buffer,DEFAULT_SECTOR_SIZE);
partition->part_size=(uint64_t)(disk_car->CHS.head+1) * disk_car->CHS.sector*disk_car->sector_size; /* 1 cylinder */
partition->part_type=P_OS2MB;
partition->upart_type=UP_OS2MB;
return 0;
}
} /* fin marqueur de fin :)) */
return 1;
}
int is_fat(const int part_type)
{
return (is_fat12(part_type)||is_fat16(part_type)||is_fat32(part_type));
}
int is_fat12(const int part_type)
{
switch(part_type)
{
case P_12FAT:
case P_12FATH:
return 1;
}
return 0;
}
int is_fat16(const int part_type)
{
switch(part_type)
{
case P_16FAT:
case P_16FATH:
case P_16FATBD_LBA:
case P_16FATBD:
case P_16FATBDH:
case P_16FATBD_LBAH:
return 1;
}
return 0;
}
int is_fat32(const int part_type)
{
switch(part_type)
{
case P_32FAT:
case P_32FAT_LBA:
case P_32FATH:
case P_32FAT_LBAH:
return 1;
}
return 0;
}
int fat32_free_info(t_param_disk *disk_car,const t_partition *partition, const unsigned int fat_offset, const unsigned int no_of_cluster, unsigned int *next_free, unsigned int*free_count)
{
unsigned char buffer[disk_car->sector_size];
uint32_t *p32=(uint32_t*)&buffer;
uint64_t hd_offset=partition->part_offset+(uint64_t)fat_offset*disk_car->sector_size;
unsigned int prev_cluster;
*next_free=0;
*free_count=0;
for(prev_cluster=2;prev_cluster<=no_of_cluster+1;prev_cluster++)
{
unsigned long int cluster;
unsigned int offset_s,offset_o;
offset_s=prev_cluster/(disk_car->sector_size/4);
offset_o=prev_cluster%(disk_car->sector_size/4);
if((offset_o==0)||(prev_cluster==2))
{
if(disk_car->read(disk_car,sizeof(buffer), &buffer, hd_offset)!=0)
{
ecrit_rapport("fat32_free_info read error\n");
*next_free=0xFFFFFFFF;
*free_count=0xFFFFFFFF;
return 1;
}
hd_offset+=sizeof(buffer);
}
cluster=le32(p32[offset_o]) & 0xFFFFFFF;
if(cluster==0)
{
(*free_count)++;
if(*next_free==0)
*next_free=prev_cluster;
}
}
ecrit_rapport("next_free %u, free_count %u\n",*next_free,*free_count);
return 0;
}
|