diff --git a/src/fat.c b/src/fat.c index d26be0cb..e2d273a8 100644 --- a/src/fat.c +++ b/src/fat.c @@ -180,9 +180,12 @@ int check_FAT(disk_t *disk_car,partition_t *partition,const int verbose) static void set_FAT_info(disk_t *disk_car, const struct fat_boot_sector *fat_header, partition_t *partition) { - uint64_t start_fat1,start_data,part_size; - unsigned long int no_of_cluster,fat_length; - const char*buffer=(const char*)fat_header; + uint64_t start_fat1; + uint64_t start_data; + uint64_t part_size; + unsigned long int no_of_cluster; + unsigned long int fat_length; + const char *buffer=(const char*)fat_header; partition->fsname[0]='\0'; partition->blocksize=fat_sector_size(fat_header)* fat_header->sectors_per_cluster; fat_length=le16(fat_header->fat_length)>0?le16(fat_header->fat_length):le32(fat_header->fat32_length); @@ -223,76 +226,87 @@ static void set_FAT_info(disk_t *disk_car, const struct fat_boot_sector *fat_hea } } -unsigned int get_next_cluster(disk_t *disk_car,const partition_t *partition, const upart_type_t upart_type,const int offset, const unsigned int cluster) +static unsigned int get_next_cluster_fat12(disk_t *disk, const partition_t *partition, const int offset, const unsigned int cluster) +{ + unsigned int next_cluster; + unsigned long int offset_s; + unsigned long int offset_o; + unsigned char *buffer=(unsigned char*)MALLOC(2*disk_car->sector_size); + offset_s=(cluster+cluster/2)/disk_car->sector_size; + offset_o=(cluster+cluster/2)%disk_car->sector_size; + if((unsigned)disk_car->pread(disk_car, buffer, 2 * disk_car->sector_size, + partition->part_offset + (uint64_t)(offset + offset_s) * disk_car->sector_size) != 2 * disk_car->sector_size) + { + log_error("get_next_cluster_fat12 read error\n"); + free(buffer); + return 0; + } + if((cluster&1)!=0) + next_cluster=le16((*((uint16_t*)&buffer[offset_o])))>>4; + else + next_cluster=le16(*((uint16_t*)&buffer[offset_o]))&0x0FFF; + free(buffer); + return next_cluster; +} + +static unsigned int get_next_cluster_fat16(disk_t *disk, const partition_t *partition, const int offset, const unsigned int cluster) +{ + unsigned int next_cluster; + unsigned long int offset_s; + unsigned long int offset_o; + unsigned char *buffer=(unsigned char*)MALLOC(disk->sector_size); + const uint16_t *p16=(const uint16_t*)buffer; + offset_s=cluster/(disk->sector_size/2); + offset_o=cluster%(disk->sector_size/2); + if((unsigned)disk->pread(disk, buffer, disk->sector_size, + partition->part_offset + (uint64_t)(offset + offset_s) * disk->sector_size) != disk->sector_size) + { + log_error("get_next_cluster_fat16 read error\n"); + free(buffer); + return 0; + } + next_cluster=le16(p16[offset_o]); + free(buffer); + return next_cluster; +} + +static unsigned int get_next_cluster_fat32(disk_t *disk, const partition_t *partition, const int offset, const unsigned int cluster) +{ + unsigned int next_cluster; + unsigned long int offset_s; + unsigned long int offset_o; + unsigned char *buffer=(unsigned char*)MALLOC(disk->sector_size); + const uint32_t *p32=(const uint32_t*)buffer; + offset_s=cluster/(disk->sector_size/4); + offset_o=cluster%(disk->sector_size/4); + if((unsigned)disk->pread(disk, buffer, disk->sector_size, + partition->part_offset + (uint64_t)(offset + offset_s) * disk->sector_size) != disk->sector_size) + { + log_error("get_next_cluster_fat32 read error\n"); + free(buffer); + return 0; + } + /* FAT32 used 28 bits, the 4 high bits are reserved + * 0x00000000: free cluster + * 0x0FFFFFF7: bad cluster + * 0x0FFFFFF8+: EOC End of cluster + * */ + next_cluster=le32(p32[offset_o])&0xFFFFFFF; + free(buffer); + return next_cluster; +} + +unsigned int get_next_cluster(disk_t *disk,const partition_t *partition, const upart_type_t upart_type,const int offset, const unsigned int cluster) { /* Offset can be offset to FAT1 or to FAT2 */ - /* log_trace("get_next_cluster(upart_type=%u,offset=%u,cluster=%u\n",upart_type,offset,cluster); */ switch(upart_type) { case UP_FAT12: - { - unsigned int next_cluster; - unsigned long int offset_s,offset_o; - unsigned char *buffer=(unsigned char*)MALLOC(2*disk_car->sector_size); - offset_s=(cluster+cluster/2)/disk_car->sector_size; - offset_o=(cluster+cluster/2)%disk_car->sector_size; - if((unsigned)disk_car->pread(disk_car, buffer, 2 * disk_car->sector_size, - partition->part_offset + (uint64_t)(offset + offset_s) * disk_car->sector_size) != 2 * disk_car->sector_size) - { - log_error("get_next_cluster read error\n"); - free(buffer); - return 0; - } - if((cluster&1)!=0) - next_cluster=le16((*((uint16_t*)&buffer[offset_o])))>>4; - else - next_cluster=le16(*((uint16_t*)&buffer[offset_o]))&0x0FFF; - free(buffer); - return next_cluster; - } + return get_next_cluster_fat12(disk, partition, offset, cluster); case UP_FAT16: - { - unsigned int next_cluster; - unsigned long int offset_s,offset_o; - unsigned char *buffer=(unsigned char*)MALLOC(disk_car->sector_size); - const uint16_t *p16=(const uint16_t*)buffer; - offset_s=cluster/(disk_car->sector_size/2); - offset_o=cluster%(disk_car->sector_size/2); - if((unsigned)disk_car->pread(disk_car, buffer, disk_car->sector_size, - partition->part_offset + (uint64_t)(offset + offset_s) * disk_car->sector_size) != disk_car->sector_size) - { - log_error("get_next_cluster read error\n"); - free(buffer); - return 0; - } - next_cluster=le16(p16[offset_o]); - free(buffer); - return next_cluster; - } + return get_next_cluster_fat16(disk, partition, offset, cluster); case UP_FAT32: - { - unsigned int next_cluster; - unsigned long int offset_s,offset_o; - unsigned char *buffer=(unsigned char*)MALLOC(disk_car->sector_size); - const uint32_t *p32=(const uint32_t*)buffer; - offset_s=cluster/(disk_car->sector_size/4); - offset_o=cluster%(disk_car->sector_size/4); - if((unsigned)disk_car->pread(disk_car, buffer, disk_car->sector_size, - partition->part_offset + (uint64_t)(offset + offset_s) * disk_car->sector_size) != disk_car->sector_size) - { - log_error("get_next_cluster read error\n"); - free(buffer); - return 0; - } - /* FAT32 used 28 bits, the 4 high bits are reserved - * 0x00000000: free cluster - * 0x0FFFFFF7: bad cluster - * 0x0FFFFFF8+: EOC End of cluster - * */ - next_cluster=le32(p32[offset_o])&0xFFFFFFF; - free(buffer); - return next_cluster; - } + return get_next_cluster_fat32(disk, partition, offset, cluster); default: log_critical("fat.c get_next_cluster unknown fat type\n"); return 0; diff --git a/src/fat.h b/src/fat.h index 2ec58c77..11d0871d 100644 --- a/src/fat.h +++ b/src/fat.h @@ -86,7 +86,8 @@ struct fat_fsinfo { } __attribute__ ((gcc_struct, __packed__)); struct msdos_dir_entry { - int8_t name[8],ext[3]; /* 00 name and extension */ + int8_t name[8]; /* 00 name and extension */ + int8_t ext[3]; uint8_t attr; /* 0B attribute bits */ uint8_t lcase; /* 0C Case for base and extension */ uint8_t ctime_ms; /* 0D Creation time, milliseconds */ diff --git a/src/fat_adv.c b/src/fat_adv.c index 0772aa37..b5f3452f 100644 --- a/src/fat_adv.c +++ b/src/fat_adv.c @@ -591,7 +591,8 @@ static void fat_date_unix2dos(int unix_date,unsigned short *mstime, unsigned sho (((unix_date/3600) % 24) << 11)); day = unix_date/86400-3652; year = day/365; - if ((year+3)/4+365*year > day) year--; + if ((year+3)/4+365*year > day) + year--; day -= (year+3)/4+365*year; if (day == 59 && !(year & 3)) { nl_day = day; @@ -600,7 +601,8 @@ static void fat_date_unix2dos(int unix_date,unsigned short *mstime, unsigned sho else { nl_day = (year & 3) || day <= 59 ? day : day-1; for (month = 1; month < 12; month++) - if (day_n[month] > nl_day) break; + if (day_n[month] > nl_day) + break; } *msdate = le16(nl_day-day_n[month-1]+1+(month << 5)+(year << 9)); } @@ -609,7 +611,9 @@ static int file2entry(struct msdos_dir_entry *de, const file_info_t *current_fil { unsigned int i,j; /* Name */ - for(i=0;(i<8)&&(current_file->name[i]!='.')&&(current_file->name[i]!='\0');i++) + for(i=0; + i<8 && current_file->name[i]!='.' && current_file->name[i]!='\0'; + i++) { de->name[i]=current_file->name[i]; } @@ -618,8 +622,12 @@ static int file2entry(struct msdos_dir_entry *de, const file_info_t *current_fil de->name[j]=' '; } /* Extension */ - for(;(current_file->name[i]!='.')&&(current_file->name[i]!='\0');i++); - for(j=0;(j<3)&&(current_file->name[i]!='\0');j++) + for(; + current_file->name[i]!='.' && current_file->name[i]!='\0'; + i++); + for(j=0; + j<3 && current_file->name[i]!='\0'; + j++) { de->ext[j]=current_file->name[i]; } @@ -1708,11 +1716,17 @@ static upart_type_t fat_find_info(disk_t *disk_car,unsigned int*reserved, unsign } switch(info_offset[offset_for_max_nbr].fat_type) { - case 12: upart_type=UP_FAT12; break; - case 16: upart_type=UP_FAT16; break; - case 32: upart_type=UP_FAT32; break; + case 12: + upart_type=UP_FAT12; + break; + case 16: + upart_type=UP_FAT16; + break; + case 32: + upart_type=UP_FAT32; + break; } - for(i=0;i