summaryrefslogtreecommitdiffstats
path: root/src/thfs.c
diff options
context:
space:
mode:
authorChristophe Grenier <grenier@cgsecurity.org>2017-04-14 18:19:19 +0200
committerChristophe Grenier <grenier@cgsecurity.org>2017-04-14 18:19:19 +0200
commitf7a840127cd5e741c29528542e02267c95df693e (patch)
treee28a57e869ada65ffaf5e9496996470459428cd2 /src/thfs.c
parent34b37516965b499429a401ca2c2cdf2e439ee77d (diff)
Rewrote HFS_HFSP_boot_sector()
Diffstat (limited to 'src/thfs.c')
-rw-r--r--src/thfs.c206
1 files changed, 104 insertions, 102 deletions
diff --git a/src/thfs.c b/src/thfs.c
index f7635b5..090288f 100644
--- a/src/thfs.c
+++ b/src/thfs.c
@@ -74,11 +74,104 @@ static void hfs_dump(disk_t *disk_car, const partition_t *partition, const unsig
}
}
-int HFS_HFSP_boot_sector(disk_t *disk_car, partition_t *partition, const int verbose, char **current_cmd)
+static int HFS_HFSP_boot_sector_command(char **current_cmd, const char *options)
+{
+ while(*current_cmd[0]==',')
+ (*current_cmd)++;
+ if(strncmp(*current_cmd,"dump",4)==0)
+ {
+ (*current_cmd)+=4;
+ return 'D';
+ }
+ else if(strncmp(*current_cmd,"originalhfsp",11)==0)
+ {
+ (*current_cmd)+=11;
+ if(strchr(options,'O')!=NULL)
+ return 'O';
+ }
+ else if(strncmp(*current_cmd,"backuphfsp",9)==0)
+ {
+ (*current_cmd)+=9;
+ if(strchr(options,'B')!=NULL)
+ return 'B';
+ }
+ return 0;
+}
+
+static const char *HFS_HFSP_boot_sector_rescan(disk_t *disk_car, partition_t *partition, unsigned char *buffer_bs, unsigned char *buffer_backup_bs, const int verbose)
+{
+ int opt_B=0;
+ int opt_O=0;
+#ifdef HAVE_NCURSES
+ aff_copy(stdscr);
+ wmove(stdscr,4,0);
+ wprintw(stdscr,"%s",disk_car->description(disk_car));
+ mvwaddstr(stdscr,5,0,msg_PART_HEADER_LONG);
+ wmove(stdscr,6,0);
+ aff_part(stdscr,AFF_PART_ORDER|AFF_PART_STATUS,disk_car,partition);
+#endif
+ log_info("\nHFS_HFSP_boot_sector\n");
+ log_partition(disk_car,partition);
+ screen_buffer_add("Volume header\n");
+ if(disk_car->pread(disk_car, buffer_bs, HFSP_BOOT_SECTOR_SIZE, partition->part_offset + 0x400) != HFSP_BOOT_SECTOR_SIZE)
+ {
+ screen_buffer_add("Bad: can't read HFS/HFS+ volume header.\n");
+ memset(buffer_bs,0,HFSP_BOOT_SECTOR_SIZE);
+ }
+ else if(test_HFSP(disk_car,(const struct hfsp_vh*)buffer_bs,partition,verbose,0)==0)
+ {
+ screen_buffer_add("HFS+ OK\n");
+ opt_O=1;
+ }
+ else if(test_HFS(disk_car,(const hfs_mdb_t*)buffer_bs,partition,verbose,0)==0)
+ {
+ screen_buffer_add("HFS Ok\n");
+ opt_O=1;
+ }
+ else
+ screen_buffer_add("Bad\n");
+ screen_buffer_add("\nBackup volume header\n");
+ if(disk_car->pread(disk_car, buffer_backup_bs, HFSP_BOOT_SECTOR_SIZE, partition->part_offset + partition->part_size - 0x400) != HFSP_BOOT_SECTOR_SIZE)
+ {
+ screen_buffer_add("Bad: can't read HFS/HFS+ backup volume header.\n");
+ memset(buffer_backup_bs,0,HFSP_BOOT_SECTOR_SIZE);
+ }
+ else if(test_HFSP(disk_car,(const struct hfsp_vh*)buffer_backup_bs,partition,verbose,0)==0)
+ {
+ screen_buffer_add("HFS+ OK\n");
+ opt_B=1;
+ }
+ else if(test_HFS(disk_car,(const hfs_mdb_t*)buffer_backup_bs,partition,verbose,0)==0)
+ {
+ screen_buffer_add("HFS Ok\n");
+ opt_B=1;
+ }
+ else
+ screen_buffer_add("Bad\n");
+ screen_buffer_add("\n");
+ if(memcmp(buffer_bs,buffer_backup_bs,HFSP_BOOT_SECTOR_SIZE)==0)
+ {
+ screen_buffer_add("Sectors are identical.\n");
+ return "D";
+ }
+ else
+ {
+ screen_buffer_add("Sectors are not identical.\n");
+ }
+ if(opt_B!=0 && opt_O!=0)
+ return "DOB";
+ else if(opt_B!=0)
+ return "DB";
+ else if(opt_O!=0)
+ return "DO";
+ return "D";
+}
+
+int HFS_HFSP_boot_sector(disk_t *disk, partition_t *partition, const int verbose, char **current_cmd)
{
unsigned char *buffer_bs;
unsigned char *buffer_backup_bs;
- const char *options="";
+ const char *options;
int rescan=1;
#ifdef HAVE_NCURSES
const struct MenuItem menu_hfsp[]=
@@ -104,104 +197,13 @@ int HFS_HFSP_boot_sector(disk_t *disk_car, partition_t *partition, const int ver
screen_buffer_reset();
if(rescan==1)
{
- int opt_over=0;
- int opt_B=0;
- int opt_O=0;
- options="D";
-#ifdef HAVE_NCURSES
- aff_copy(stdscr);
- wmove(stdscr,4,0);
- wprintw(stdscr,"%s",disk_car->description(disk_car));
- mvwaddstr(stdscr,5,0,msg_PART_HEADER_LONG);
- wmove(stdscr,6,0);
- aff_part(stdscr,AFF_PART_ORDER|AFF_PART_STATUS,disk_car,partition);
-#endif
- log_info("\nHFS_HFSP_boot_sector\n");
- log_partition(disk_car,partition);
- screen_buffer_add("Volume header\n");
- if(disk_car->pread(disk_car, buffer_bs, HFSP_BOOT_SECTOR_SIZE, partition->part_offset + 0x400) != HFSP_BOOT_SECTOR_SIZE)
- {
- screen_buffer_add("Bad: can't read HFS/HFS+ volume header.\n");
- memset(buffer_bs,0,HFSP_BOOT_SECTOR_SIZE);
- }
- else if(test_HFSP(disk_car,(const struct hfsp_vh*)buffer_bs,partition,verbose,0)==0)
- {
- screen_buffer_add("HFS+ OK\n");
- opt_O=1;
- opt_over=1;
- }
- else if(test_HFS(disk_car,(const hfs_mdb_t*)buffer_bs,partition,verbose,0)==0)
- {
- screen_buffer_add("HFS Ok\n");
- opt_O=1;
- opt_over=1;
- }
- else
- screen_buffer_add("Bad\n");
- screen_buffer_add("\nBackup volume header\n");
- if(disk_car->pread(disk_car, buffer_backup_bs, HFSP_BOOT_SECTOR_SIZE, partition->part_offset + partition->part_size - 0x400) != HFSP_BOOT_SECTOR_SIZE)
- {
- screen_buffer_add("Bad: can't read HFS/HFS+ backup volume header.\n");
- memset(buffer_backup_bs,0,HFSP_BOOT_SECTOR_SIZE);
- }
- else if(test_HFSP(disk_car,(const struct hfsp_vh*)buffer_backup_bs,partition,verbose,0)==0)
- {
- screen_buffer_add("HFS+ OK\n");
- opt_B=1;
- opt_over=1;
- }
- else if(test_HFS(disk_car,(const hfs_mdb_t*)buffer_backup_bs,partition,verbose,0)==0)
- {
- screen_buffer_add("HFS Ok\n");
- opt_B=1;
- opt_over=1;
- }
- else
- screen_buffer_add("Bad\n");
- screen_buffer_add("\n");
- if(memcmp(buffer_bs,buffer_backup_bs,HFSP_BOOT_SECTOR_SIZE)==0)
- {
- screen_buffer_add("Sectors are identical.\n");
- opt_over=0;
- }
- else
- {
- screen_buffer_add("Sectors are not identical.\n");
- }
- if(opt_over!=0)
- {
- if(opt_B!=0 && opt_O!=0)
- options="DOB";
- else if(opt_B!=0)
- options="DB";
- else if(opt_O!=0)
- options="DO";
- }
+ options=HFS_HFSP_boot_sector_rescan(disk, partition, buffer_bs, buffer_backup_bs, verbose);
rescan=0;
}
screen_buffer_to_log();
if(*current_cmd!=NULL)
{
- command=0;
- while(*current_cmd[0]==',')
- (*current_cmd)++;
- if(strncmp(*current_cmd,"dump",4)==0)
- {
- (*current_cmd)+=4;
- command='D';
- }
- else if(strncmp(*current_cmd,"originalhfsp",11)==0)
- {
- (*current_cmd)+=11;
- if(strchr(options,'O')!=NULL)
- command='O';
- }
- else if(strncmp(*current_cmd,"backuphfsp",9)==0)
- {
- (*current_cmd)+=9;
- if(strchr(options,'B')!=NULL)
- command='B';
- }
+ command=HFS_HFSP_boot_sector_command(current_cmd, options);
}
else
{
@@ -222,35 +224,35 @@ int HFS_HFSP_boot_sector(disk_t *disk_car, partition_t *partition, const int ver
case 'O': /* O : copy original superblock over backup boot */
#ifdef HAVE_NCURSES
if(ask_confirmation("Copy original HFS/HFS+ volume header over backup, confirm ? (Y/N)")!=0)
+#endif
{
log_info("copy original superblock over backup boot\n");
- if(disk_car->pwrite(disk_car, buffer_bs, HFSP_BOOT_SECTOR_SIZE, partition->part_offset + partition->part_size - 0x400) != HFSP_BOOT_SECTOR_SIZE)
+ if(disk->pwrite(disk, buffer_bs, HFSP_BOOT_SECTOR_SIZE, partition->part_offset + partition->part_size - 0x400) != HFSP_BOOT_SECTOR_SIZE)
{
display_message("Write error: Can't overwrite HFS/HFS+ backup volume header\n");
}
- disk_car->sync(disk_car);
+ disk->sync(disk);
rescan=1;
}
-#endif
break;
case 'B': /* B : copy backup superblock over main superblock */
#ifdef HAVE_NCURSES
if(ask_confirmation("Copy backup HFS/HFS+ volume header over main volume header, confirm ? (Y/N)")!=0)
+#endif
{
log_info("copy backup superblock over main superblock\n");
/* Reset information about backup boot sector */
partition->sb_offset=0;
- if(disk_car->pwrite(disk_car, buffer_backup_bs, HFSP_BOOT_SECTOR_SIZE, partition->part_offset + 0x400) != HFSP_BOOT_SECTOR_SIZE)
+ if(disk->pwrite(disk, buffer_backup_bs, HFSP_BOOT_SECTOR_SIZE, partition->part_offset + 0x400) != HFSP_BOOT_SECTOR_SIZE)
{
display_message("Write error: Can't overwrite HFS/HFS+ main volume header\n");
}
- disk_car->sync(disk_car);
+ disk->sync(disk);
rescan=1;
}
-#endif
break;
case 'D':
- hfs_dump(disk_car, partition, buffer_bs, buffer_backup_bs, current_cmd);
+ hfs_dump(disk, partition, buffer_bs, buffer_backup_bs, current_cmd);
break;
}
}