MCPcopy Create free account
hub / github.com/cgsecurity/testdisk / create_fat_boot_sector

Function create_fat_boot_sector

src/fat_adv.c:1062–1261  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1060}
1061
1062static void create_fat_boot_sector(disk_t *disk_car, partition_t *partition, const unsigned int reserved, const int verbose, const unsigned int dir_entries, const unsigned long int root_cluster, const unsigned int sectors_per_cluster, const unsigned int fat_length, const upart_type_t upart_type, const unsigned int fats, char **current_cmd)
1063{
1064 unsigned char *orgboot;
1065 unsigned char *newboot;
1066 const struct fat_boot_sector *org_fat_header;
1067 struct fat_boot_sector *fat_header;
1068 int error=0;
1069 unsigned long int part_size=0;
1070 orgboot=(unsigned char *)MALLOC(3*disk_car->sector_size);
1071 newboot=(unsigned char *)MALLOC(3*disk_car->sector_size);
1072 org_fat_header=(const struct fat_boot_sector *)orgboot;
1073 fat_header=(struct fat_boot_sector *)newboot;
1074 if((unsigned)disk_car->pread(disk_car, orgboot, 3 * disk_car->sector_size, partition->part_offset) != 3 * disk_car->sector_size)
1075 {
1076 log_error("create_fat_boot_sector: Can't read old boot sector\n");
1077 memset(orgboot,0,3*disk_car->sector_size);
1078 }
1079 memcpy(newboot,orgboot,3*disk_car->sector_size);
1080 if(3*disk_car->sector_size >= DEFAULT_SECTOR_SIZE &&
1081 (le16(fat_header->marker)!=0xAA55 ||
1082 !((fat_header->ignored[0]==0xeb && fat_header->ignored[2]==0x90)||fat_header->ignored[0]==0xe9)))
1083 {
1084 write_FAT_boot_code_aux(newboot);
1085 }
1086 fat_header->sector_size[0]=disk_car->sector_size & 0xFF;
1087 fat_header->sector_size[1]=disk_car->sector_size >>8;
1088 fat_header->fats=fats;
1089 fat_header->secs_track=le16(disk_car->geom.sectors_per_head);
1090 fat_header->heads=le16(disk_car->geom.heads_per_cylinder);
1091 fat_header->marker=le16(0xAA55);
1092 if(!((fat_header->ignored[0]==0xeb&&fat_header->ignored[2]==0x90)||fat_header->ignored[0]==0xe9))
1093 {
1094 fat_header->ignored[0]=0xeb;
1095 fat_header->ignored[2]=0x90;
1096 }
1097
1098 /* I have seen a FAT32 partition that Win98 2nd edition was unable to read
1099 * because this name was missing! */
1100 if(memcmp(fat_header->system_id,"MSDOS5.0",8) &&
1101 memcmp(fat_header->system_id,"MSWIN4.0",8) &&
1102 memcmp(fat_header->system_id,"MSWIN4.1",8))
1103 memcpy(fat_header->system_id,"MSWIN4.1",8);
1104 /* FIXME, need to know where the extended or logical partition start */
1105#if 0
1106 if(partition->status==STATUS_LOG)
1107 fat_header->hidden=le32(disk_car->geom.sectors_per_head);
1108 else
1109#endif
1110 fat_header->hidden=le32((partition->part_offset/disk_car->sector_size));
1111 fat_header->sectors_per_cluster=sectors_per_cluster;
1112 fat_header->reserved=le16(reserved);
1113 /* The filesystem size can be smaller than the partition size */
1114 switch(upart_type)
1115 {
1116 case UP_FAT12:
1117 part_size=le16(fat_header->reserved)+fats*fat_length+dir_entries*32/disk_car->sector_size+sectors_per_cluster*(fat_length*disk_car->sector_size*2/3-2);
1118 break;
1119 case UP_FAT16:

Callers 1

rebuild_FAT_BSFunction · 0.85

Calls 6

MALLOCFunction · 0.85
write_FAT_boot_code_auxFunction · 0.85
log_closeFunction · 0.85
check_VFAT_volume_nameFunction · 0.85
log_fat2_infoFunction · 0.85

Tested by

no test coverage detected