Exemplo n.º 1
0
void interface_list(disk_t *disk, const int verbose, const int saveheader, const int backup)
{
  list_part_t *list_part;
  list_part_t *parts;
  log_info("\nAnalyse ");
  log_info("%s\n", disk->description(disk));
  printf("%s\n", disk->description(disk));
  printf(msg_PART_HEADER_LONG);
  list_part=disk->arch->read_part(disk,verbose,saveheader);

  for(parts=list_part; parts!=NULL; parts=parts->next)
  {
    const char *msg;
    const partition_t *partition=parts->part;
    msg=aff_part_aux(AFF_PART_ORDER|AFF_PART_STATUS, disk, partition);
    printf("%s\n", msg);
    if(partition->info[0]!='\0')
      printf("     %s\n", partition->info);
  }
  if(backup>0)
  {
    partition_save(disk, list_part, verbose);
  }
  part_free_list(list_part);
}
Exemplo n.º 2
0
QPhotorec::~QPhotorec()
{
//  session_save(list_search_space, params, options);
  part_free_list(list_part);
  delete_list_disk(list_disk);
  free(options);
  free(params);
}
Exemplo n.º 3
0
static int menu_disk_cli(disk_t *disk_car, const int verbose,int dump_ind, const int saveheader, char **current_cmd)
{
  int align=1;
  int ask_part_order=0;
  unsigned int expert=0;
  while(1)
  {
    while(*current_cmd[0]==',')
      (*current_cmd)++;
    if(strncmp(*current_cmd,"analyze",7)==0 || strncmp(*current_cmd,"analyse",7)==0)
    {
      (*current_cmd)+=7;
      {
	list_part_t *list_part;
	list_part=interface_analyse(disk_car, verbose, saveheader, current_cmd);
	interface_recovery(disk_car, list_part, verbose, dump_ind, align, ask_part_order, expert, current_cmd);
	part_free_list(list_part);
      }
    }
    else if(strncmp(*current_cmd,"geometry,",9)==0)
    {
      (*current_cmd)+=9;
      change_geometry_cli(disk_car, current_cmd);
    }
    else if(strncmp(*current_cmd,"advanced",8)==0)
    {
      (*current_cmd)+=8;
      interface_adv(disk_car, verbose, dump_ind, expert,current_cmd);
    }
    else if(strncmp(*current_cmd,"options,",8)==0)
    {
      (*current_cmd)+=8;
      interface_options(&dump_ind, &align, &expert,current_cmd);
    }
    else if(strncmp(*current_cmd,"delete",6)==0)
    {
      (*current_cmd)+=6;
      write_clean_table(disk_car);
    }
    else if(strncmp(*current_cmd,"mbr_code",8)==0)
    {
      (*current_cmd)+=8;
      write_MBR_code(disk_car);
    }
    else
    {
      return 0;
    }
  }
}
Exemplo n.º 4
0
void QPhotorec::select_disk(disk_t *disk)
{
  if(disk==NULL)
    return ;
  selected_disk=disk;
  selected_partition=NULL;
  autodetect_arch(selected_disk, &arch_none);
  log_info("%s\n", selected_disk->description_short(selected_disk));
  part_free_list(list_part);
  list_part=init_list_part(selected_disk, NULL);
  /* If only whole disk is listed, select it */
  /* If there is the whole disk and only one partition, select the partition */
  if(list_part!=NULL)
  {
    if(list_part->next==NULL)
      selected_partition=list_part->part;
    else if(list_part->next->next==NULL)
      selected_partition=list_part->next->part;
  }
  log_all_partitions(selected_disk, list_part);
}
Exemplo n.º 5
0
void menu_photorec(struct ph_param *params, struct ph_options *options, alloc_data_t*list_search_space)
{
  list_part_t *list_part;
#ifdef HAVE_NCURSES
  list_part_t *current_element;
  unsigned int current_element_num;
  int done=0;
  int command;
  unsigned int offset=0;
  unsigned int menu=0;
  static const struct MenuItem menuMain[]=
  {
	{'S',"Search","Start file recovery"},
	{'O',"Options","Modify options"},
	{'F',"File Opt","Modify file options"},
	{'G',"Geometry", "Change disk geometry" },
	{'Q',"Quit","Return to disk selection"},
	{0,NULL,NULL}
  };
#endif
  params->blocksize=0;
  list_part=init_list_part(params->disk, options);
  if(list_part==NULL)
    return;
  log_all_partitions(params->disk, list_part);
  if(params->cmd_run!=NULL)
  {
    if(menu_photorec_cli(list_part, params, options, list_search_space) > 0)
    {
      if(params->recup_dir==NULL)
      {
	char *res;
#ifdef HAVE_NCURSES
	res=ask_location("Please select a destination to save the recovered files.\nDo not choose to write the files to the same partition they were stored on.", "", NULL);
#else
	res=get_default_location();
#endif
	if(res!=NULL)
	{
	  params->recup_dir=(char *)MALLOC(strlen(res)+1+strlen(DEFAULT_RECUP_DIR)+1);
	  strcpy(params->recup_dir,res);
	  strcat(params->recup_dir,"/");
	  strcat(params->recup_dir,DEFAULT_RECUP_DIR);
	  free(res);
	}
      }
      if(params->recup_dir!=NULL)
	photorec(params, options, list_search_space);
    }
  }
  if(params->cmd_run!=NULL)
  {
    part_free_list(list_part);
    return;
  }
#ifdef HAVE_NCURSES
  if(list_part->next!=NULL)
  {
    current_element_num=1;
    current_element=list_part->next;
  }
  else
  {
    current_element_num=0;
    current_element=list_part;
  }
  while(done==0)
  { /* ncurses interface */
    list_part_t *element;
    unsigned int i;
    aff_copy(stdscr);
    wmove(stdscr,4,0);
    wprintw(stdscr,"%s",params->disk->description_short(params->disk));
    mvwaddstr(stdscr,6,0,msg_PART_HEADER_LONG);
#if defined(KEY_MOUSE) && defined(ENABLE_MOUSE)
    mousemask(ALL_MOUSE_EVENTS, NULL);
#endif
    for(i=0,element=list_part; element!=NULL && i<offset+INTER_SELECT;element=element->next,i++)
    {
      if(i<offset)
	continue;
      wmove(stdscr,7+i-offset,0);
      wclrtoeol(stdscr);	/* before addstr for BSD compatibility */
      if(element==current_element)
      {
	wattrset(stdscr, A_REVERSE);
	waddstr(stdscr, ">");
	aff_part(stdscr,AFF_PART_ORDER|AFF_PART_STATUS,params->disk,element->part);
	wattroff(stdscr, A_REVERSE);
      } else
      {
	waddstr(stdscr, " ");
	aff_part(stdscr,AFF_PART_ORDER|AFF_PART_STATUS,params->disk,element->part);
      }
    }
    wmove(stdscr,7+INTER_SELECT,5);
    wclrtoeol(stdscr);
    if(element!=NULL)
      wprintw(stdscr, "Next");
    command = wmenuSelect(stdscr, INTER_SELECT_Y+1, INTER_SELECT_Y, INTER_SELECT_X, menuMain, 8,
	(options->expert==0?"SOFQ":"SOFGQ"), MENU_HORIZ | MENU_BUTTON | MENU_ACCEPT_OTHERS, menu);
#if defined(KEY_MOUSE) && defined(ENABLE_MOUSE)
    if(command == KEY_MOUSE)
    {
      MEVENT event;
      if(getmouse(&event) == OK)
      {	/* When the user clicks left mouse button */
	if((event.bstate & BUTTON1_CLICKED) || (event.bstate & BUTTON1_DOUBLE_CLICKED))
	{
	  if(event.y >=7 && event.y<7+INTER_SELECT)
	  {
	    /* Disk selection */
	    while(current_element_num > event.y-(7-offset) && current_element->prev!=NULL)
	    {
	      current_element=current_element->prev;
	      current_element_num--;
	    }
	    while(current_element_num < event.y-(7-offset) && current_element->next!=NULL)
	    {
	      current_element=current_element->next;
	      current_element_num++;
	    }
	    if(event.bstate & BUTTON1_DOUBLE_CLICKED)
	      command='S';
	  }
	  else
	    command = menu_to_command(INTER_SELECT_Y+1, INTER_SELECT_Y, INTER_SELECT_X, menuMain, 8,
		(options->expert==0?"SOFQ":"SOFGQ"), MENU_HORIZ | MENU_BUTTON | MENU_ACCEPT_OTHERS, event.y, event.x);
	}
      }
    }
#endif
    switch(command)
    {
      case KEY_UP:
	if(current_element!=NULL && current_element->prev!=NULL)
	{
	  current_element=current_element->prev;
	  current_element_num--;
	}
	break;
      case KEY_PPAGE:
	for(i=0; (signed)i<INTER_SELECT && current_element->prev!=NULL; i++)
	{
	  current_element=current_element->prev;
	  current_element_num--;
	}
	break;
      case KEY_DOWN:
	if(current_element->next!=NULL)
	{
	  current_element=current_element->next;
	  current_element_num++;
	}
	break;
      case KEY_NPAGE:
	for(i=0; (signed)i<INTER_SELECT && current_element->next!=NULL; i++)
	{
	  current_element=current_element->next;
	  current_element_num++;
	}
	break;
      case 's':
      case 'S':
	if(current_element!=NULL)
	{
	  params->partition=current_element->part;
	  ask_mode_ext2(params->disk, params->partition, &options->mode_ext2, &params->carve_free_space_only);
	  menu=0;
	  if(params->recup_dir==NULL)
	  {
	    char *res;
	    res=ask_location("Please select a destination to save the recovered files.\nDo not choose to write the files to the same partition they were stored on.", "", NULL);
	    if(res!=NULL)
	    {
	      params->recup_dir=(char *)MALLOC(strlen(res)+1+strlen(DEFAULT_RECUP_DIR)+1);
	      strcpy(params->recup_dir,res);
	      strcat(params->recup_dir,"/");
	      strcat(params->recup_dir,DEFAULT_RECUP_DIR);
	      free(res);
	    }
	  }
	  if(params->recup_dir!=NULL)
	  {
	    if(td_list_empty(&list_search_space->list))
	    {
	      init_search_space(list_search_space, params->disk, params->partition);
	    }
	    if(params->carve_free_space_only>0)
	    {
	      aff_copy(stdscr);
	      wmove(stdscr,5,0);
	      wprintw(stdscr, "Filesystem analysis, please wait...\n");
	      wrefresh(stdscr);
	      params->blocksize=remove_used_space(params->disk, params->partition, list_search_space);
	      /* Only free space is carved, list_search_space is modified.
	       * To carve the whole space, need to quit and reselect the params->partition */
	      done = 1;
	    }
	    photorec(params, options, list_search_space);
	  }
	}
	break;
      case 'o':
      case 'O':
	{
	  interface_options_photorec_ncurses(options);
	  menu=1;
	}
	break;
      case 'f':
      case 'F':
	interface_file_select_ncurses(options->list_file_format);
	menu=2;
	break;
      case 'g':
      case 'G':
	if(options->expert!=0)
	  if(change_geometry_ncurses(params->disk))
	    done=1;
	break;
      case 'a':
      case 'A':
	if(params->disk->arch != &arch_none)
	{
	  list_part=add_partition_ncurses(params->disk, list_part);
	  current_element=list_part;
	  current_element_num=0;
	}
	break;
      case 'q':
      case 'Q':
	done = 1;
	break;
    }
    if(current_element_num<offset)
      offset=current_element_num;
    if(current_element_num>=offset+INTER_SELECT)
      offset=current_element_num-INTER_SELECT+1;
  }
#endif
  log_info("\n");
  part_free_list(list_part);
}
Exemplo n.º 6
0
void autodetect_arch(disk_t *disk, const arch_fnct_t *arch)
{
  list_part_t *list_part=NULL;
#ifdef DEBUG_PARTAUTO
  const int verbose=2;
#else
  const int verbose=0;
  unsigned int old_levels;
  old_levels=log_set_levels(0);
#endif
  {
    disk->arch=&arch_none;
    list_part=disk->arch->read_part(disk,verbose,0);
    if(list_part!=NULL && list_part->part!=NULL && list_part->part->upart_type==UP_UNK)
    {
      part_free_list(list_part);
      list_part=NULL;
    }
  }
  if(list_part==NULL)
  {
    disk->arch=&arch_xbox;
    list_part=disk->arch->read_part(disk,verbose,0);
  }
  if(list_part==NULL)
  {
    disk->arch=&arch_gpt;
    list_part=disk->arch->read_part(disk,verbose,0);
  }
  if(list_part==NULL)
  {
    disk->arch=&arch_humax;
    list_part=disk->arch->read_part(disk,verbose,0);
  }
  if(list_part==NULL)
  {
    disk->arch=&arch_i386;
    list_part=disk->arch->read_part(disk,verbose,0);
  }
  if(list_part==NULL)
  {
    disk->arch=&arch_sun;
    list_part=disk->arch->read_part(disk,verbose,0);
  }
  if(list_part==NULL)
  {
    disk->arch=&arch_mac;
    list_part=disk->arch->read_part(disk,verbose,0);
  }
#ifndef DEBUG_PARTAUTO
  log_set_levels(old_levels);
#endif
  if(list_part!=NULL)
  {
    disk->arch_autodetected=disk->arch;
    log_info("Partition table type (auto): %s\n", disk->arch->part_name);
    part_free_list(list_part);
    return ;
  }
  disk->arch_autodetected=NULL;
  if(arch!=NULL)
  {
    disk->arch=arch;
  }
  else
  {
#ifdef TARGET_SOLARIS
    disk->arch=&arch_sun;
#elif defined __APPLE__
#ifdef TESTDISK_LSB
    disk->arch=&arch_gpt;
#else
    disk->arch=&arch_mac;
#endif
#else
#if defined(__CYGWIN__) || defined(__MINGW32__)
    if(disk->device[0]=='\\' && disk->device[1]=='\\' && disk->device[2]=='.' && disk->device[3]=='\\' && disk->device[5]==':')
      disk->arch=&arch_none;
    else
#endif
    /* PC/Intel partition table is limited to 2 TB, 2^32 512-bytes sectors */
    if(disk->disk_size < ((uint64_t)1<<(32+9)))
      disk->arch=&arch_i386;
    else
      disk->arch=&arch_gpt;
#endif
  }
  log_info("Partition table type default to %s\n", disk->arch->part_name);
}
Exemplo n.º 7
0
void menu_photorec(struct ph_param *params, struct ph_options *options, alloc_data_t*list_search_space)
{
  list_part_t *list_part;
  list_part_t *current_element;
  unsigned int current_element_num;
  unsigned int user_blocksize=0;
  int done=0;
  init_mode_t mode_init_space=(td_list_empty(&list_search_space->list)?INIT_SPACE_WHOLE:INIT_SPACE_PREINIT);
#ifdef HAVE_NCURSES
  int command;
  unsigned int offset=0;
  unsigned int menu=0;
  static const struct MenuItem menuMain[]=
  {
	{'S',"Search","Start file recovery"},
	{'O',"Options","Modify options"},
	{'F',"File Opt","Modify file options"},
	{'G',"Geometry", "Change disk geometry" },
	{'Q',"Quit","Return to disk selection"},
	{0,NULL,NULL}
  };
#endif
  params->blocksize=0;
  list_part=init_list_part(params->disk, options);
  if(list_part==NULL)
    return;
  log_all_partitions(params->disk, list_part);
  if(list_part->next!=NULL)
  {
    current_element_num=1;
    current_element=list_part->next;
  }
  else
  {
    current_element_num=0;
    current_element=list_part;
  }
  while(done==0)
  {
    if(params->cmd_run!=NULL)
    {
      while(params->cmd_run[0]==',')
	params->cmd_run++;
      if(params->cmd_run[0]=='\0')
      {
	part_free_list(list_part);
	return;
      }
      if(strncmp(params->cmd_run,"search",6)==0)
      {
	params->cmd_run+=6;
	if(params->recup_dir==NULL)
	{
	  char *res;
#ifdef HAVE_NCURSES
	  res=ask_location("Please select a destination to save the recovered files.\nDo not choose to write the files to the same partition they were stored on.", "", NULL);
#else
	  res=get_default_location();
#endif
	  if(res!=NULL)
	  {
	    params->recup_dir=(char *)MALLOC(strlen(res)+1+strlen(DEFAULT_RECUP_DIR)+1);
	    strcpy(params->recup_dir,res);
	    strcat(params->recup_dir,"/");
	    strcat(params->recup_dir,DEFAULT_RECUP_DIR);
	    free(res);
	  }
	}
	if(params->recup_dir!=NULL)
	{
	  params->partition=current_element->part;
	  if(mode_init_space==INIT_SPACE_EXT2_GROUP)
	  {
	    params->blocksize=ext2_fix_group(list_search_space, params->disk, params->partition);
	    if(params->blocksize==0)
	      display_message("Not a valid ext2/ext3/ext4 filesystem");
	  }
	  else if(mode_init_space==INIT_SPACE_EXT2_INODE)
	  {
	    params->blocksize=ext2_fix_inode(list_search_space, params->disk, params->partition);
	    if(params->blocksize==0)
	      display_message("Not a valid ext2/ext3/ext4 filesystem");
	  }
	  if(td_list_empty(&list_search_space->list))
	  {
	    init_search_space(list_search_space, params->disk, params->partition);
	  }
	  if(params->carve_free_space_only>0)
	  {
	    params->blocksize=remove_used_space(params->disk, params->partition, list_search_space);
	  }
	  if(user_blocksize > 0)
	    params->blocksize=user_blocksize;
	  photorec(params, options, list_search_space);
	}
      }
      else if(strncmp(params->cmd_run,"options",7)==0)
      {
	params->cmd_run+=7;
	interface_options_photorec_cli(options, &params->cmd_run);
      }
      else if(strncmp(params->cmd_run,"fileopt",7)==0)
      {
	params->cmd_run+=7;
	interface_file_select(options->list_file_format, &params->cmd_run);
      }
      else if(strncmp(params->cmd_run,"blocksize,",10)==0)
      {
	params->cmd_run+=10;
	user_blocksize=atoi(params->cmd_run);
	while(params->cmd_run[0]!=',' && params->cmd_run[0]!='\0')
	  params->cmd_run++;
      }
      else if(strncmp(params->cmd_run,"geometry,",9)==0)
      {
	params->cmd_run+=9;
	change_geometry_cli(params->disk, &params->cmd_run);
      }
      else if(strncmp(params->cmd_run,"inter",5)==0)
      {	/* Start interactive mode */
	params->cmd_run=NULL;
      }
      else if(strncmp(params->cmd_run,"wholespace",10)==0)
      {
	params->cmd_run+=10;
	params->carve_free_space_only=0;
      }
      else if(strncmp(params->cmd_run,"freespace",9)==0)
      {
	params->cmd_run+=9;
	params->carve_free_space_only=1;
      }
      else if(strncmp(params->cmd_run,"ext2_group,",11)==0)
      {
	unsigned int groupnr;
	params->cmd_run+=11;
	options->mode_ext2=1;
	groupnr=atoi(params->cmd_run);
	while(params->cmd_run[0]!=',' && params->cmd_run[0]!='\0')
	  params->cmd_run++;
	if(mode_init_space==INIT_SPACE_WHOLE)
	  mode_init_space=INIT_SPACE_EXT2_GROUP;
	if(mode_init_space==INIT_SPACE_EXT2_GROUP)
	{
          alloc_data_t *new_free_space;
          new_free_space=(alloc_data_t*)MALLOC(sizeof(*new_free_space));
          /* Temporary storage, values need to be multiplied by group size and aligned */
          new_free_space->start=groupnr;
          new_free_space->end=groupnr;
          new_free_space->file_stat=NULL;
	  new_free_space->data=1;
          if(td_list_add_sorted_uniq(&new_free_space->list, &list_search_space->list, spacerange_cmp))
	    free(new_free_space);
        }
      }
      else if(strncmp(params->cmd_run,"ext2_inode,",11)==0)
      {
	unsigned int inodenr;
	params->cmd_run+=11;
	options->mode_ext2=1;
	inodenr=atoi(params->cmd_run);
	while(params->cmd_run[0]!=',' && params->cmd_run[0]!='\0')
	  params->cmd_run++;
	if(mode_init_space==INIT_SPACE_WHOLE)
	  mode_init_space=INIT_SPACE_EXT2_INODE;
	if(mode_init_space==INIT_SPACE_EXT2_INODE)
	{
          alloc_data_t *new_free_space;
          new_free_space=(alloc_data_t*)MALLOC(sizeof(*new_free_space));
          /* Temporary storage, values need to be multiplied by group size and aligned */
          new_free_space->start=inodenr;
          new_free_space->end=inodenr;
          new_free_space->file_stat=NULL;
	  new_free_space->data=1;
          if(td_list_add_sorted_uniq(&new_free_space->list, &list_search_space->list, spacerange_cmp))
	    free(new_free_space);
        }
      }
      else if(isdigit(params->cmd_run[0]))
      {
	list_part_t *element;
	unsigned int order;
	order= atoi(params->cmd_run);
	while(params->cmd_run[0]!=',' && params->cmd_run[0]!='\0')
	  params->cmd_run++;
	for(element=list_part;element!=NULL && element->part->order!=order;element=element->next);
	if(element!=NULL)
	  current_element=element;
      }
      else
      {
	log_critical("Syntax error in command line: %s\n", params->cmd_run);
	part_free_list(list_part);
	return;
      }
    }
#ifdef HAVE_NCURSES
    else
    { /* ncurses interface */
      list_part_t *element;
      unsigned int i;
      aff_copy(stdscr);
      wmove(stdscr,4,0);
      wprintw(stdscr,"%s",params->disk->description_short(params->disk));
      mvwaddstr(stdscr,6,0,msg_PART_HEADER_LONG);
#if defined(KEY_MOUSE) && defined(ENABLE_MOUSE)
      mousemask(ALL_MOUSE_EVENTS, NULL);
#endif
      for(i=0,element=list_part; element!=NULL && i<offset+INTER_SELECT;element=element->next,i++)
      {
	if(i<offset)
	  continue;
	wmove(stdscr,7+i-offset,0);
	wclrtoeol(stdscr);	/* before addstr for BSD compatibility */
	if(element==current_element)
	{
	  wattrset(stdscr, A_REVERSE);
	  waddstr(stdscr, ">");
	  aff_part(stdscr,AFF_PART_ORDER|AFF_PART_STATUS,params->disk,element->part);
	  wattroff(stdscr, A_REVERSE);
	} else
	{
	  waddstr(stdscr, " ");
	  aff_part(stdscr,AFF_PART_ORDER|AFF_PART_STATUS,params->disk,element->part);
	}
      }
      wmove(stdscr,7+INTER_SELECT,5);
      wclrtoeol(stdscr);
      if(element!=NULL)
	wprintw(stdscr, "Next");
      command = wmenuSelect(stdscr, INTER_SELECT_Y+1, INTER_SELECT_Y, INTER_SELECT_X, menuMain, 8,
	  (options->expert==0?"SOFQ":"SOFGQ"), MENU_HORIZ | MENU_BUTTON | MENU_ACCEPT_OTHERS, menu);
#if defined(KEY_MOUSE) && defined(ENABLE_MOUSE)
      if(command == KEY_MOUSE)
      {
	MEVENT event;
	if(getmouse(&event) == OK)
	{	/* When the user clicks left mouse button */
	  if((event.bstate & BUTTON1_CLICKED) || (event.bstate & BUTTON1_DOUBLE_CLICKED))
	  {
	    if(event.y >=7 && event.y<7+INTER_SELECT)
	    {
	      /* Disk selection */
	      while(current_element_num > event.y-(7-offset) && current_element->prev!=NULL)
	      {
		current_element=current_element->prev;
		current_element_num--;
	      }
	      while(current_element_num < event.y-(7-offset) && current_element->next!=NULL)
	      {
		current_element=current_element->next;
		current_element_num++;
	      }
	      if(event.bstate & BUTTON1_DOUBLE_CLICKED)
		command='S';
	    }
	    else
	      command = menu_to_command(INTER_SELECT_Y+1, INTER_SELECT_Y, INTER_SELECT_X, menuMain, 8,
		  (options->expert==0?"SOFQ":"SOFGQ"), MENU_HORIZ | MENU_BUTTON | MENU_ACCEPT_OTHERS, event.y, event.x);
	  }
	}
      }
#endif
      switch(command)
      {
	case KEY_UP:
	  if(current_element!=NULL && current_element->prev!=NULL)
	  {
	    current_element=current_element->prev;
	    current_element_num--;
	  }
	  break;
	case KEY_PPAGE:
	  for(i=0; (signed)i<INTER_SELECT && current_element->prev!=NULL; i++)
	  {
	    current_element=current_element->prev;
	    current_element_num--;
	  }
	  break;
	case KEY_DOWN:
	  if(current_element->next!=NULL)
	  {
	    current_element=current_element->next;
	    current_element_num++;
	  }
	  break;
	case KEY_NPAGE:
	  for(i=0; (signed)i<INTER_SELECT && current_element->next!=NULL; i++)
	  {
	    current_element=current_element->next;
	    current_element_num++;
	  }
	  break;
	case 's':
	case 'S':
	  if(current_element!=NULL)
	  {
	    params->partition=current_element->part;
	    ask_mode_ext2(params->disk, params->partition, &options->mode_ext2, &params->carve_free_space_only);
	    menu=0;
	    if(params->recup_dir==NULL)
	    {
	      char *res;
	      res=ask_location("Please select a destination to save the recovered files.\nDo not choose to write the files to the same partition they were stored on.", "", NULL);
	      if(res!=NULL)
	      {
		params->recup_dir=(char *)MALLOC(strlen(res)+1+strlen(DEFAULT_RECUP_DIR)+1);
		strcpy(params->recup_dir,res);
		strcat(params->recup_dir,"/");
		strcat(params->recup_dir,DEFAULT_RECUP_DIR);
		free(res);
	      }
	    }
	    if(params->recup_dir!=NULL)
	    {
	      if(td_list_empty(&list_search_space->list))
	      {
		init_search_space(list_search_space, params->disk, params->partition);
	      }
	      if(params->carve_free_space_only>0)
	      {
		aff_copy(stdscr);
		wmove(stdscr,5,0);
		wprintw(stdscr, "Filesystem analysis, please wait...\n");
		wrefresh(stdscr);
		params->blocksize=remove_used_space(params->disk, params->partition, list_search_space);
		/* Only free space is carved, list_search_space is modified.
		 * To carve the whole space, need to quit and reselect the params->partition */
		done = 1;
	      }
	      photorec(params, options, list_search_space);
	    }
	  }
	  break;
	case 'o':
	case 'O':
	  {
	    interface_options_photorec_ncurses(options);
	    menu=1;
	  }
	  break;
	case 'f':
	case 'F':
	  interface_file_select(options->list_file_format, &params->cmd_run);
	  menu=2;
	  break;
	case 'g':
	case 'G':
	  if(options->expert!=0)
	    if(change_geometry_ncurses(params->disk))
	      done=1;
	  break;
      case 'a':
      case 'A':
	if(params->disk->arch != &arch_none)
	{
	  list_part=add_partition_ncurses(params->disk, list_part);
	  current_element=list_part;
	  current_element_num=0;
	}
	break;
	case 'q':
	case 'Q':
	  done = 1;
	  break;
      }
      if(current_element_num<offset)
	offset=current_element_num;
      if(current_element_num>=offset+INTER_SELECT)
	offset=current_element_num-INTER_SELECT+1;
    }
#endif
  }
  log_info("\n");
  part_free_list(list_part);
}
Exemplo n.º 8
0
static int write_part_gpt_i386(disk_t *disk_car, const list_part_t *list_part)
{
  /* The Protective MBR has the same format as a legacy MBR. */
  const list_part_t *element;
  list_part_t *list_part_i386=NULL;
  uint64_t efi_psize=disk_car->disk_size;
  partition_t *part_mac=NULL;
  partition_t *part_linux=NULL;
  partition_t *part_windows=NULL;
  for(element=list_part;element!=NULL;element=element->next)
  {
    if(part_mac==NULL && element->part->part_type_i386==P_HFS)
      part_mac=element->part;
    else if(part_linux==NULL && element->part->part_type_i386==P_LINUX)
      part_linux=element->part;
    else if(part_windows==NULL && element->part->part_type_i386==P_NTFS)
      part_windows=element->part;
  }
  if(part_mac!=NULL && (part_linux!=NULL || part_windows!=NULL))
  { /* For bootcamp, the layout should be
     * 1 EFI
     * 2 MacOS X
     * 3 Linux if any
     * 4 Windows
     */
    {
      int insert_error=0;
      partition_t *new_partition=partition_new(NULL);
      dup_partition_t(new_partition, part_mac);
      new_partition->arch=&arch_i386;
      new_partition->status=STATUS_PRIM;
      new_partition->order=2;
      list_part_i386=insert_new_partition(list_part_i386, new_partition, 0, &insert_error);
      if(insert_error>0)
        free(new_partition);
      else if(efi_psize > new_partition->part_offset)
        efi_psize=new_partition->part_offset;
    }
    if(part_linux!=NULL)
    {
      int insert_error=0;
      partition_t *new_partition=partition_new(NULL);
      dup_partition_t(new_partition, part_linux);
      new_partition->arch=&arch_i386;
      new_partition->status=STATUS_PRIM;
      new_partition->order=3;
      list_part_i386=insert_new_partition(list_part_i386, new_partition, 0, &insert_error);
      if(insert_error>0)
        free(new_partition);
      else if(efi_psize > new_partition->part_offset)
        efi_psize=new_partition->part_offset;
    }
    if(part_windows!=NULL)
    {
      int insert_error=0;
      partition_t *new_partition=partition_new(NULL);
      dup_partition_t(new_partition, part_windows);
      new_partition->arch=&arch_i386;
      new_partition->status=STATUS_PRIM;
      new_partition->order=4;
      list_part_i386=insert_new_partition(list_part_i386, new_partition, 0, &insert_error);
      if(insert_error>0)
        free(new_partition);
      else if(efi_psize > new_partition->part_offset)
        efi_psize=new_partition->part_offset;
    }
    {
      int insert_error=0;
      partition_t *new_partition=partition_new(&arch_i386);
      new_partition->status=STATUS_PRIM;
      new_partition->order=1;
      new_partition->part_type_i386=0xee;
      new_partition->part_offset=disk_car->sector_size;
      new_partition->part_size=efi_psize - new_partition->part_offset;
      list_part_i386=insert_new_partition(list_part_i386, new_partition, 0, &insert_error);
      if(insert_error>0)
        free(new_partition);
    }
  }
  else
  { /* The Protective MBR contains one partition entry of OS type 0xEE and
     * reserves the entire space used on the disk by the GPT partitions,
     * including all headers.
     */
    int insert_error=0;
    partition_t *new_partition=partition_new(&arch_i386);
    new_partition->status=STATUS_PRIM;
    new_partition->order=1;
    new_partition->part_type_i386=0xee;
    new_partition->part_offset=disk_car->sector_size;
    new_partition->part_size=disk_car->disk_size - new_partition->part_offset;
    list_part_i386=insert_new_partition(list_part_i386, new_partition, 0, &insert_error);
    if(insert_error>0)
      free(new_partition);
  }
  arch_i386.write_part(disk_car, list_part_i386, 0, 0);
  part_free_list(list_part_i386);
  return 0;
}
Exemplo n.º 9
0
static list_part_t *search_part(disk_t *disk_car, const list_part_t *list_part_org, const int verbose, const int dump_ind, const int fast_mode, char **current_cmd)
{
  unsigned char *buffer_disk;
  unsigned char *buffer_disk0;
  /* TODO use circular buffer for try_offset and try_offset_raid */
  uint64_t try_offset[MAX_SEARCH_LOCATION];
  uint64_t try_offset_raid[MAX_SEARCH_LOCATION];
  const uint64_t min_location=get_min_location(disk_car);
  uint64_t search_location;
  unsigned int try_offset_nbr=0;
  unsigned int try_offset_raid_nbr=0;
#ifdef HAVE_NCURSES
  unsigned int old_cylinder=0;
#endif
  const unsigned int location_boundary=get_location_boundary(disk_car);
  indstop_t ind_stop=INDSTOP_CONTINUE;
  list_part_t *list_part=NULL;
  list_part_t *list_part_bad=NULL;
  partition_t *partition;
  /* It's not a problem to read a little bit more than necessary */
  const uint64_t search_location_max=td_max((disk_car->disk_size /
      ((uint64_t) disk_car->geom.heads_per_cylinder * disk_car->geom.sectors_per_head * disk_car->sector_size) + 1 ) *
      ((uint64_t) disk_car->geom.heads_per_cylinder * disk_car->geom.sectors_per_head * disk_car->sector_size),
      disk_car->disk_real_size);
  assert(disk_car->sector_size>0);
  partition=partition_new(disk_car->arch);
  buffer_disk=(unsigned char*)MALLOC(16*DEFAULT_SECTOR_SIZE);
  buffer_disk0=(unsigned char*)MALLOC(16*DEFAULT_SECTOR_SIZE);
  {
    /* Will search for partition at current known partition location */
    const list_part_t *element;
    for(element=list_part_org;element!=NULL;element=element->next)
    {
      hint_insert(try_offset, element->part->part_offset, &try_offset_nbr);
    }
  }

#ifdef HAVE_NCURSES
  wmove(stdscr,22,0);
  wattrset(stdscr, A_REVERSE);
  waddstr(stdscr,"  Stop  ");
  wattroff(stdscr, A_REVERSE);
#endif
  screen_buffer_reset();
  log_info("\nsearch_part()\n");
  log_info("%s\n",disk_car->description(disk_car));
  search_location=min_location;
  search_add_hints(disk_car, try_offset, &try_offset_nbr);
  /* Not every sector will be examined */
  search_location_init(disk_car, location_boundary, fast_mode);
  /* Scan the disk */
  while(ind_stop!=INDSTOP_QUIT && search_location < search_location_max)
  {
    CHS_t start;
    offset2CHS_inline(disk_car,search_location,&start);
#ifdef HAVE_NCURSES
    if(disk_car->geom.heads_per_cylinder>1)
    {
      if(old_cylinder!=start.cylinder)
      {
	old_cylinder=start.cylinder;
	wmove(stdscr,ANALYSE_Y,ANALYSE_X);
	wclrtoeol(stdscr);
	wprintw(stdscr,"Analyse cylinder %5u/%u: %02u%%",
	    start.cylinder, disk_car->geom.cylinders-1,
	    (unsigned int)(search_location*100/disk_car->disk_size));
	wrefresh(stdscr);
	switch(check_enter_key_or_s(stdscr))
	{
	  case 1:
	    ind_stop=INDSTOP_STOP;
	    break;
	  case 2:
	    ind_stop=INDSTOP_SKIP;
	    break;
	}
      }
    }
    else if((start.cylinder & 0x7FFF)==0)
    {
      wmove(stdscr,ANALYSE_Y,ANALYSE_X);
      wclrtoeol(stdscr);
      wprintw(stdscr,"Analyse sector %11llu/%lu: %02u%%",
	  search_location / disk_car->sector_size,
	  (disk_car->disk_size-1)/disk_car->sector_size,
	    (unsigned int)(search_location*100/disk_car->disk_size));
      wrefresh(stdscr);
      switch(check_enter_key_or_s(stdscr))
      {
	case 1:
	  ind_stop=INDSTOP_STOP;
	  break;
	case 2:
	  ind_stop=INDSTOP_SKIP;
	  break;
      }
    }
#endif
    {
      unsigned int sector_inc=0;
      int test_nbr=0;
      int search_now=0;
      int search_now_raid=0;
      while(try_offset_nbr>0 && try_offset[0]<=search_location)
      {
        unsigned int j;
        if(try_offset[0]==search_location)
          search_now=1;
        for(j=0;j<try_offset_nbr-1;j++)
          try_offset[j]=try_offset[j+1];
        try_offset_nbr--;
      }
      /* PC x/0/1 x/1/1 x/2/1 */
      /* PC Vista 2048 sectors unit */
      if(disk_car->arch==&arch_i386)
        search_now|= (start.sector==1 && fast_mode>1) ||
          (start.sector==1 && start.head<=2) ||
          search_location%(2048*512)==0;
      else
        search_now|= (search_location%location_boundary==0);
      while(try_offset_raid_nbr>0 && try_offset_raid[0]<=search_location)
      {
        unsigned int j;
        if(try_offset_raid[0]==search_location)
          search_now_raid=1;
        for(j=0;j<try_offset_raid_nbr-1;j++)
          try_offset_raid[j]=try_offset_raid[j+1];
        try_offset_raid_nbr--;
      }
      do
      {
        int res=0;
        partition->part_size=(uint64_t)0;
        partition->part_offset=search_location;
        if(res<=0 && test_nbr==0)
        {
          if(search_now_raid>0 || fast_mode>1)
          { /* Search Linux software RAID */
	    if(disk_car->pread(disk_car, buffer_disk, 8 * DEFAULT_SECTOR_SIZE, search_location) == 8 *DEFAULT_SECTOR_SIZE)
            {
              if(recover_MD(disk_car, (const struct mdp_superblock_s*)buffer_disk, partition, verbose, dump_ind)==0)
              {
                const struct mdp_superblock_1 *sb1=(const struct mdp_superblock_1 *)buffer_disk;
		if(le32(sb1->md_magic)==(unsigned int)MD_SB_MAGIC)
		{
		  if(le32(sb1->major_version)==0)
		    partition->part_offset-=(uint64_t)MD_NEW_SIZE_SECTORS(partition->part_size/512)*512;
		  else
		    partition->part_offset-=le64(sb1->super_offset)*512;
		}
		else
		{
		  if(be32(sb1->major_version)==0)
		    partition->part_offset-=(uint64_t)MD_NEW_SIZE_SECTORS(partition->part_size/512)*512;
		  else
		    partition->part_offset-=be64(sb1->super_offset)*512;
		}
                res=1;
              }
              else
                res=0;
            }
          }
          test_nbr++;
        }
        if(res<=0 && test_nbr==1)
	{
	  if((disk_car->arch==&arch_i386 &&
		((start.sector==7 && (start.head<=2 || fast_mode>1)) ||
		 search_location%(2048*512)==(7-1)*512)) ||
	      (disk_car->arch!=&arch_i386 && (search_location%location_boundary==(7-1)*512)) ||
	      (disk_car->arch==&arch_gpt&& (search_location%(2048*512)==(7-1)*512)) ||
	      (disk_car->arch==&arch_none && search_location==(7-1)*512))
	    res=search_FAT_backup(buffer_disk,disk_car,partition,verbose,dump_ind);
	  test_nbr++;
        }
        if(res<=0 && test_nbr==2)
	{
	  if((disk_car->arch==&arch_i386 &&
		((start.sector==13 && (start.head<=2 || fast_mode>1)) ||
		 search_location%(2048*512)==(13-1)*disk_car->sector_size)) ||
	      (disk_car->arch==&arch_gpt&& (search_location%(2048*512)==(13-1)*512)) ||
	      (disk_car->arch!=&arch_i386 && (search_location%location_boundary==(13-1)*disk_car->sector_size)))
	    res=search_EXFAT_backup(buffer_disk, disk_car, partition);
	  test_nbr++;
	}
        if(res<=0 && test_nbr==3)
        {
          if((disk_car->arch==&arch_i386 &&
                ((start.sector==disk_car->geom.sectors_per_head &&
		  (start.head==disk_car->geom.heads_per_cylinder-1 || fast_mode>1)) ||
                 search_location%(2048*512)==(2048-1)*512)) ||
	      (disk_car->arch==&arch_gpt&& (search_location%(2048*512)==(2048-1)*512)) ||
              (disk_car->arch!=&arch_i386 && search_location%location_boundary==(location_boundary-512) &&
               search_location>0))
            res=search_NTFS_backup(buffer_disk,disk_car,partition,verbose,dump_ind);
          test_nbr++;
        }
        if(res<=0 && test_nbr==4)
        {
          if((disk_car->arch==&arch_i386 &&
                ((start.sector==disk_car->geom.sectors_per_head &&
		  (start.head==disk_car->geom.heads_per_cylinder-1 || fast_mode>1)) ||
		 search_location%(2048*512)==(2048-1)*512)) ||
              (disk_car->arch!=&arch_i386 && search_location%location_boundary==(location_boundary-512) &&
               search_location>0))
            res=search_HFS_backup(buffer_disk,disk_car,partition,verbose,dump_ind);
          test_nbr++;
        }
        if(res<=0 && test_nbr==5)
        {
          int s_log_block_size;
          /* try backup superblock */
          /* It must be in fast_mode>0 because it can hide otherwise other partition type */
          /* Block size: 1024, 2048 or 4096 bytes (8192 bytes on Alpha systems) */
          /* From e2fsprogs-1.34/lib/ext2fs/initialize.c: set_field(s_first_data_block, super->s_log_block_size ? 0 : 1); */
          /* Assumes that TestDisk is not running under Alpha and s_blocks_per_group=8 * block size */
          for(s_log_block_size=0;(s_log_block_size<=2)&&(res<=0);s_log_block_size++)
          {
            /* sparse superblock feature: The groups chosen are 0, 1 and powers of 3, 5 and 7. */
            /* Checking group 3 */
            const uint64_t hd_offset=3*(EXT2_MIN_BLOCK_SIZE<<s_log_block_size)*8*(EXT2_MIN_BLOCK_SIZE<<s_log_block_size)+(s_log_block_size==0?2*DEFAULT_SECTOR_SIZE:0);
            if(search_location>=hd_offset)
            {
              CHS_t start_ext2;
              offset2CHS_inline(disk_car,search_location-hd_offset,&start_ext2);
              if((disk_car->arch==&arch_i386 && start_ext2.sector==1 &&  (start_ext2.head<=2 || fast_mode>1)) ||
		  (disk_car->arch==&arch_i386 && (search_location-hd_offset)%(2048*512)==0) ||
		  (disk_car->arch!=&arch_i386 && (search_location-hd_offset)%location_boundary==0))
	      {
		if(disk_car->pread(disk_car, buffer_disk, 1024, search_location)==1024)
		{
		  const struct ext2_super_block *sb=(const struct ext2_super_block*)buffer_disk;
		  if(le16(sb->s_magic)==EXT2_SUPER_MAGIC && le16(sb->s_block_group_nr)>0 &&
		      recover_EXT2(disk_car, sb, partition, verbose, dump_ind)==0)
		    res=1;
		}
	      }
            }
          }
          test_nbr++;
        }
        if(res<=0 && test_nbr==6)
        {
	  if(search_now==0)
            test_nbr=14;
	  else
	  {
	    if(disk_car->pread(disk_car, buffer_disk0, 16 * DEFAULT_SECTOR_SIZE, partition->part_offset) == 16 * DEFAULT_SECTOR_SIZE)
	      res=search_type_2(buffer_disk0,disk_car,partition,verbose,dump_ind);
	    else
	      res=-1;
	    test_nbr++;
	  }
        }
        if(res<=0 && test_nbr==7)
        {
	  if(res==0)
	    res=search_type_1(buffer_disk0, disk_car,partition,verbose,dump_ind);
	  test_nbr++;
        }
        if(res<=0 && test_nbr==8)
        {
	  if(res==0)
	    res=search_type_0(buffer_disk0,disk_car,partition,verbose,dump_ind);
          test_nbr++;
        }
        if(res<=0 && test_nbr==9)
        {
          res=search_type_8(buffer_disk,disk_car,partition,verbose,dump_ind);
          test_nbr++;
        }
        if(res<=0 && test_nbr==10)
        {
          /* Try to catch disklabel before BSD FFS partition */
          res=search_type_16(buffer_disk,disk_car,partition,verbose,dump_ind);
          test_nbr++;
        }
        if(res<=0 && test_nbr==11)
        {
          res=search_type_64(buffer_disk,disk_car,partition,verbose,dump_ind);
          test_nbr++;
        }
        if(res<=0 && test_nbr==12)
        {
          /* read to fill the cache */
          disk_car->pread(disk_car, buffer_disk, 8 * DEFAULT_SECTOR_SIZE,
	      partition->part_offset + (63 + 16) * 512);
          /* Try to catch disklabel before BSD FFS partition */
          res=search_type_128(buffer_disk,disk_car,partition,verbose,dump_ind);
          test_nbr++;
        }
	if(res<=0 && test_nbr==13)
	{
          res=search_type_2048(buffer_disk,disk_car,partition,verbose,dump_ind);
          test_nbr++;
	}
        if(test_nbr>=14)
        {
          sector_inc=1;
          test_nbr=0;
        }
        if(res<0)
        {
#ifdef HAVE_NCURSES
	  wmove(stdscr,ANALYSE_Y+1,ANALYSE_X);
	  wclrtoeol(stdscr);
	  wprintw(stdscr,msg_READ_ERROR_AT, start.cylinder,start.head,start.sector,(unsigned long)(partition->part_offset/disk_car->sector_size));
#endif
	  /* Stop reading after the end of the disk */
	  if(search_location >= disk_car->disk_real_size)
	    search_location = search_location_max;
        }
        else if(res>0)
        {
          partition->status=STATUS_DELETED;
          log_partition(disk_car,partition);
          aff_part_buffer(AFF_PART_BASE, disk_car,partition);
#ifdef HAVE_NCURSES
	  screen_buffer_to_interface();
#endif
          if(disk_car->arch->is_part_known(partition)!=0 &&
              partition->part_size>1 &&
              partition->part_offset>=min_location)
          {
            const uint64_t pos_fin=partition->part_offset+partition->part_size-1;
            if(partition->upart_type!=UP_MD && partition->upart_type!=UP_MD1 &&
	      ind_stop==INDSTOP_CONTINUE)
            { /* Detect Linux md 0.9 software raid */
              unsigned int disk_factor;
              for(disk_factor=6; disk_factor>=1;disk_factor--)
              { /* disk_factor=1, detect Raid 0/1 */
                /* disk_factor>1, detect Raid 5 */
		unsigned int help_factor;
                for(help_factor=0; help_factor<=MD_MAX_CHUNK_SIZE/MD_RESERVED_BYTES+3; help_factor++)
                {
                  const uint64_t offset=(uint64_t)MD_NEW_SIZE_SECTORS((partition->part_size/disk_factor+help_factor*MD_RESERVED_BYTES-1)/MD_RESERVED_BYTES*MD_RESERVED_BYTES/512)*512;
                  hint_insert(try_offset_raid, partition->part_offset+offset, &try_offset_raid_nbr);
                }
              }
              /* TODO: Detect Linux md 1.0 software raid */
            }
            /* */
            if(pos_fin <= search_location_max)
            {
              {
                int insert_error=0;
                partition_t *new_partition=partition_new(NULL);
                dup_partition_t(new_partition,partition);
                list_part=insert_new_partition(list_part, new_partition, 0, &insert_error);
                if(insert_error>0)
                  free(new_partition);
              }
              {
                const uint64_t next_part_offset=partition->part_offset+partition->part_size-1+1;
                const uint64_t head_size=disk_car->geom.sectors_per_head * disk_car->sector_size;
                hint_insert(try_offset, next_part_offset, &try_offset_nbr);
                hint_insert(try_offset, next_part_offset+head_size, &try_offset_nbr);
                if(next_part_offset%head_size!=0)
                {
                  hint_insert(try_offset, (next_part_offset+head_size-1)/head_size*head_size, &try_offset_nbr);
                  hint_insert(try_offset, (next_part_offset+head_size-1)/head_size*head_size+head_size, &try_offset_nbr);
                }
              }
              if((fast_mode==0) && (partition->part_offset+partition->part_size-disk_car->sector_size > search_location))
              {
                search_location=partition->part_offset+partition->part_size-disk_car->sector_size;
                test_nbr=0;
                sector_inc=1;
              }
            }
            else
            {
              {
                int insert_error=0;
                partition_t *new_partition=partition_new(NULL);
                dup_partition_t(new_partition,partition);
                list_part_bad=insert_new_partition(list_part_bad, new_partition, 0, &insert_error);
                if(insert_error>0)
                  free(new_partition);
              }
              if(verbose>0)
                log_warning("This partition ends after the disk limits. (start=%llu, size=%llu, end=%llu, disk end=%llu)\n",
                    (unsigned long long)(partition->part_offset/disk_car->sector_size),
                    (unsigned long long)(partition->part_size/disk_car->sector_size),
                    (unsigned long long)(pos_fin/disk_car->sector_size),
                    (unsigned long long)(disk_car->disk_size/disk_car->sector_size));
              else
                log_warning("This partition ends after the disk limits.\n");
            }
          }
          else
          {
            if(verbose>0)
            {
              log_warning("Partition not added.\n");
            }
          }
          partition_reset(partition, disk_car->arch);
        }
      }
      while(sector_inc==0);
    }
    if(ind_stop==INDSTOP_SKIP)
    {
      ind_stop=INDSTOP_CONTINUE;
      if(try_offset_nbr>0 && search_location < try_offset[0])
	search_location=try_offset[0];
    }
    else if(ind_stop==INDSTOP_STOP)
    {
      if(try_offset_nbr>0 && search_location < try_offset[0])
	search_location=try_offset[0];
      else
	ind_stop=INDSTOP_QUIT;
    }
    else
    { /* Optimized "search_location+=disk_car->sector_size;" */
      uint64_t min=search_location_update(search_location);
      if(try_offset_nbr>0 && min>try_offset[0])
        min=try_offset[0];
      if(try_offset_raid_nbr>0 && min>try_offset_raid[0])
        min=try_offset_raid[0];
      if(min==(uint64_t)-1 || min<=search_location)
        search_location+=disk_car->sector_size;
      else
        search_location=min;
    }
  }
  /* Search for NTFS partition near the supposed partition beginning
     given by the NTFS backup boot sector */
  if(fast_mode>0)
    search_NTFS_from_backup(disk_car, list_part, verbose, dump_ind, min_location, search_location_max);
  free(partition);
  if(ind_stop!=INDSTOP_CONTINUE)
    log_info("Search for partition aborted\n");
  if(list_part_bad!=NULL)
  {
    interface_part_bad_log(disk_car,list_part_bad);
#ifdef HAVE_NCURSES
    if(*current_cmd==NULL)
      interface_part_bad_ncurses(disk_car,list_part_bad);
#endif
  }
  part_free_list(list_part_bad);
  free(buffer_disk0);
  free(buffer_disk);
  return list_part;
}
Exemplo n.º 10
0
int interface_recovery(disk_t *disk_car, const list_part_t * list_part_org, const int verbose, const int dump_ind, const int align, const int ask_part_order, const unsigned int expert, char **current_cmd)
{
  int res_interface_write;
  int fast_mode=0;
  do
  {
    list_part_t *list_part;
    const list_part_t *element;
    unsigned int menu=0;
    if(fast_mode==0)
      menu=3;	/* Search! */
#ifdef HAVE_NCURSES
    aff_copy(stdscr);
    wmove(stdscr,4,0);
    wprintw(stdscr,"%s",disk_car->description(disk_car));
    wmove(stdscr,5,0);
#endif
    res_interface_write=0;
    list_part=search_part(disk_car, list_part_org, verbose, dump_ind, fast_mode, current_cmd);
    if(list_part!=NULL && (disk_car->arch==&arch_i386 || disk_car->arch==&arch_sun))
    { /* Correct disk geometry is necessary for successfull Intel and Sun partition recovery */
      const unsigned int heads_per_cylinder=get_geometry_from_list_part(disk_car, list_part, verbose);
      if(disk_car->geom.heads_per_cylinder!=heads_per_cylinder)
      {
	log_warning("Warning: the current number of heads per cylinder is %u but the correct value may be %u.\n",
	    disk_car->geom.heads_per_cylinder, heads_per_cylinder);
#ifdef HAVE_NCURSES
	if(*current_cmd==NULL)
	{
	  warning_geometry_ncurses(disk_car, heads_per_cylinder);
	}
#endif
      }
    }
    align_structure(list_part, disk_car, align);

    disk_car->arch->init_structure(disk_car,list_part,verbose);
    if(verbose>0)
    {
#ifdef TARGET_LINUX
      unsigned int i=0;
#endif
      /* Write found partitions in the log file */
      log_info("\nResults\n");
      for(element=list_part;element!=NULL;element=element->next)
	log_partition(disk_car,element->part);
#ifdef TARGET_LINUX
      if(list_part!=NULL)
	log_info("\nHint for advanced users: dmsetup may be used if you prefer to avoid rewriting the partition table for the moment:\n");
      for(element=list_part;element!=NULL;element=element->next)
      {
	const partition_t *partition=element->part;
	log_info("echo \"0 %llu linear %s %llu\" | dmsetup create test%u\n",
	    (long long unsigned)(partition->part_size/512),
	    disk_car->device,
	    (long long unsigned)(partition->part_offset/512),
	    i++);
      }
#endif
    }
    do
    {
      list_part=ask_structure(disk_car,list_part,verbose,current_cmd);
    } while(fast_mode!=0 && list_part!=NULL && is_structure_empty(list_part)
#ifdef HAVE_NCURSES
	&& ask_confirmation("Discard the results, confirm ? (Y/N)")==0
#endif
    );
    if(disk_car->arch->test_structure(list_part)==0)
    {
      int do_again=0;
      int max_ext=0;
      int can_ask_minmax_ext=0;
      int no_confirm=0;
      list_part=reduce_structure(list_part);
      /* sort list_part */
      list_part=sort_partition_list(list_part);
      /* Create PC/Intel Extended partition */
      /* if(disk_car->arch==&arch_i386) */
      {
	list_part_t *parts;
	uint64_t partext_offset=0;
	uint64_t partext_size=0;
	list_part=add_ext_part_i386(disk_car, list_part, !max_ext, verbose);
	for(parts=list_part;parts!=NULL;parts=parts->next)
	  if(parts->part->status==STATUS_EXT)
	  {
	    partext_offset=parts->part->part_offset;
	    partext_size=parts->part->part_size;
	  }
	if(partext_offset>0)
	{
	  list_part=add_ext_part_i386(disk_car, list_part, max_ext, verbose);
	  for(parts=list_part;parts!=NULL;parts=parts->next)
	    if(parts->part->status==STATUS_EXT)
	    {
	      if(partext_offset!=parts->part->part_offset || partext_size!=parts->part->part_size)
		can_ask_minmax_ext=1;
	    }
	}
      }
      list_part=disk_car->arch->init_part_order(disk_car,list_part);
      if(ask_part_order!=0)
      {
	/* Demande l'ordre des entrees dans le MBR */
#ifdef HAVE_NCURSES
	ask_mbr_order_i386(disk_car,list_part);
#endif
	/* Demande l'ordre des partitions etendues */
      }
      do
      {
	do_again=0;
	res_interface_write=interface_write(disk_car,list_part,(fast_mode<1),can_ask_minmax_ext, &no_confirm, current_cmd,&menu);
	switch(res_interface_write)
	{
	  case 'W':
	    if(disk_car->arch == &arch_mac)
	    {
#ifdef HAVE_NCURSES
	      write_part_mac_warning_ncurses();
#endif
	    }
	    else if(disk_car->arch == &arch_sun)
	    {
#ifdef HAVE_NCURSES
	      not_implemented("write_part_sun");
#endif
	    }
	    else if(disk_car->arch == &arch_xbox)
	    {
#ifdef HAVE_NCURSES
	      not_implemented("write_part_xbox");
#endif
	    }
	    else if(disk_car->arch->write_part!=NULL)
	    {
	      if(no_confirm!=0
#ifdef HAVE_NCURSES
		  || ask_confirmation("Write partition table, confirm ? (Y/N)")!=0
#endif
		)
	      {
		log_info("write!\n");
		if(disk_car->arch->write_part(disk_car, list_part, RW, verbose))
		{
		  display_message(msg_PART_WR_ERR);
		}
		else
		{
		  use_backup(disk_car,list_part,verbose,dump_ind,expert,current_cmd);
		  if(no_confirm==0)
		    display_message("You will have to reboot for the change to take effect.\n");
		}
	      }
	      else
		log_info("Don't write, no confirmation\n");
	    }
	    break;
	  case 0:
	    if(disk_car->arch->write_part!=NULL)
	    {
	      log_info("simulate write!\n");
	      disk_car->arch->write_part(disk_car, list_part, RO, verbose);
	    }
	    break;
	  case 'S':
	    if(fast_mode<2)
	      fast_mode++;
	    break;
	  case 'E':
	    max_ext=!max_ext;
	    list_part=add_ext_part_i386(disk_car, list_part, max_ext, verbose);
	    do_again=1;
	    break;
	}
      }
      while(do_again==1);
    }
    else
    {
      display_message("Invalid partition structure.\n");
    }
    part_free_list(list_part);
  } while(res_interface_write=='S');
  return 0;
}
Exemplo n.º 11
0
static int menu_disk_ncurses(disk_t *disk, const int verbose,int dump_ind, const int saveheader, char **current_cmd)
{
  int align=1;
  int ask_part_order=0;
  unsigned int expert=0;
  char options[16];
  static const struct MenuItem menuMain[]=
  {
	{'A',"Analyse","Analyse current partition structure and search for lost partitions"},
	{'T',"Advanced","Filesystem Utils"},
	{'G',"Geometry", "Change disk geometry" },
	{'O',"Options","Modify options"},
	{'C',"MBR Code","Write TestDisk MBR code to first sector"},
	{'D',"Delete","Delete all data in the partition table"},
	{'Q',"Quit","Return to disk selection"},
	{'E',"Editor","Basic disk editor"},
	{0,NULL,NULL}
  };
  unsigned int menu=(disk->arch == &arch_none ? 1 : 0);
  strcpy(options, "AGOPTQ");
  if(disk->arch->write_MBR_code!=NULL)
    strcat(options,"C");
  if(disk->arch->erase_list_part!=NULL)
    strcat(options,"D");
  while(1)
  {
    int real_key;
    int command;
    aff_copy(stdscr);
    wmove(stdscr,5,0);
    wprintw(stdscr, "%s\n", disk->description_short(disk));
    wmove(stdscr,6,0);
    if(disk->geom.heads_per_cylinder == 1 && disk->geom.sectors_per_head == 1)
      wprintw(stdscr, "     %llu sectors", (long long unsigned)(disk->disk_size / disk->sector_size));
    else
      wprintw(stdscr, "     CHS %lu %u %u",
	  disk->geom.cylinders, disk->geom.heads_per_cylinder, disk->geom.sectors_per_head);
    wprintw(stdscr, " - sector size=%u", disk->sector_size);
    wmove(stdscr,20,0);
    wprintw(stdscr,"Note: Correct disk geometry is required for a successful recovery. 'Analyse'");
    wmove(stdscr,21,0);
    wprintw(stdscr,"process may give some warnings if it thinks the logical geometry is mismatched.");
    command = wmenuSelect_ext(stdscr, 23, INTER_DISK_Y, INTER_DISK_X, menuMain, 10,
	options, MENU_VERT | MENU_VERT_WARN | MENU_BUTTON | MENU_ACCEPT_OTHERS, &menu,&real_key);
    /* e for editor will be added when the editor will be better */
    switch(command)
    {
      case 'a':
      case 'A':
	{
	  list_part_t *list_part;
	  list_part=interface_analyse(disk, verbose, saveheader, current_cmd);
	  interface_recovery(disk, list_part, verbose, dump_ind, align, ask_part_order, expert, current_cmd);
	  part_free_list(list_part);
	}
	break;
      case 'd':
      case 'D':
	write_clean_table(disk);
	break;
      case 'c':
      case 'C':
	write_MBR_code(disk);
	break;
      case 'g':
      case 'G':
	change_geometry_ncurses(disk);
	break;
      case 'o':
      case 'O':
	{
	  interface_options(&dump_ind, &align, &expert, current_cmd);
	}
	break;
      case 't':
      case 'T':
	interface_adv(disk, verbose, dump_ind, expert, current_cmd);
	break;
      case 'e':
      case 'E':
	interface_editor(disk);
	break;
      case 'q':
      case 'Q':
	return 0;
    }
  }
}
Exemplo n.º 12
0
void interface_adv(disk_t *disk_car, const int verbose,const int dump_ind, const unsigned int expert, char**current_cmd)
{
#ifdef HAVE_NCURSES
    int offset=0;
    int current_element_num=0;
    int old_LINES=LINES;
#endif
    int rewrite=1;
    unsigned int menu=0;
    list_part_t *element;
    list_part_t *list_part;
    list_part_t *current_element;
    log_info("\nInterface Advanced\n");
    list_part=disk_car->arch->read_part(disk_car,verbose,0);
    current_element=list_part;
    log_all_partitions(disk_car, list_part);
    while(1)
    {
        const char *options;
        static struct MenuItem menuAdv[]=
        {
            {'t',"Type","Change type, this setting will not be saved on disk"},
            {'b',"Boot","Boot sector recovery"},
            {'s',"Superblock",NULL},
            {'l',"List", "List and copy files"},
            {'u',"Undelete", "File undelete"},
            {'c',"Image Creation", "Create an image"},
//      {'a',"Add", "Add temporary partition (Expert only)"},
            {'q',"Quit","Return to main menu"},
            {0,NULL,NULL}
        };
        int command;
#ifdef HAVE_NCURSES
        int i;
        if(old_LINES!=LINES)
        {
            old_LINES=LINES;
            rewrite=1;
        }
        if(rewrite!=0)
        {
            aff_copy(stdscr);
            wmove(stdscr,4,0);
            wprintw(stdscr,"%s",disk_car->description(disk_car));
            if(list_part!=NULL)
                mvwaddstr(stdscr,6,0,msg_PART_HEADER_LONG);
            rewrite=0;
        }
        for(i=0,element=list_part; element!=NULL && i<offset+INTER_ADV; element=element->next,i++)
        {
            if(i<offset)
                continue;
            wmove(stdscr,7+i-offset,0);
            wclrtoeol(stdscr);	/* before addstr for BSD compatibility */
            if(element==current_element)
            {
                wattrset(stdscr, A_REVERSE);
                waddstr(stdscr, ">");
                aff_part(stdscr,AFF_PART_ORDER|AFF_PART_STATUS,disk_car,element->part);
                wattroff(stdscr, A_REVERSE);
            } else
            {
                waddstr(stdscr, " ");
                aff_part(stdscr,AFF_PART_ORDER|AFF_PART_STATUS,disk_car,element->part);
            }
        }
        wmove(stdscr,7+INTER_ADV,5);
        wclrtoeol(stdscr);
        if(element!=NULL)
            wprintw(stdscr, "Next");
#endif
        if(current_element==NULL)
        {
            options="q";
#ifdef HAVE_NCURSES
            wmove(stdscr,7,0);
            wattrset(stdscr, A_REVERSE);
            wprintw(stdscr,"No partition available.");
            wattroff(stdscr, A_REVERSE);
#endif
        }
        else
        {
            const partition_t *partition=current_element->part;
            if(menu==0 && (disk_car->arch!=&arch_none || partition->upart_type!=UP_UNK))
                menu=1;
            if(is_part_fat(partition))
            {
                options="tubcq";
                menu=(partition->upart_type==UP_UNK?1:4);
            }
            else if(is_part_ntfs(partition))
                options="tlubcq";
            else if(is_part_linux(partition))
            {
                if(partition->upart_type==UP_EXT2)
                    options="tuscq";
                else
                    options="tlscq";
                menuAdv[2].desc="Locate ext2/ext3/ext4 backup superblock";
            }
            else if(is_part_hfs(partition) || is_part_hfsp(partition))
            {
                options="tscq";
                menuAdv[2].desc="Locate HFS/HFS+ backup volume header";
            }
            else if(is_fat(partition))
                options="tubcq";
            else if(is_ntfs(partition))
                options="tlubcq";
            else if(is_exfat(partition))
                options="tlubcq";
            else if(is_linux(partition))
            {
                if(partition->upart_type==UP_EXT2)
                    options="tluscq";
                else
                    options="tlscq";
                menuAdv[2].desc="Locate ext2/ext3/ext4 backup superblock";
            }
            else if(is_hfs(partition) || is_hfsp(partition))
            {
                options="tscq";
                menuAdv[2].desc="Locate HFS/HFS+ backup volume header";
            }
            else
                options="tcq";
        }
        if(*current_cmd!=NULL)
        {
            int keep_asking;
            command='q';
            do
            {
                keep_asking=0;
                while(*current_cmd[0]==',')
                    (*current_cmd)++;
                if(strncmp(*current_cmd,"type",4)==0)
                {
                    (*current_cmd)+=4;
                    command='t';
                }
                else if(strncmp(*current_cmd,"addpart",7)==0)
                {
                    (*current_cmd)+=7;
                    command='a';
                }
                else if(strncmp(*current_cmd,"boot",4)==0)
                {
                    (*current_cmd)+=4;
                    command='b';
                }
                else if(strncmp(*current_cmd,"copy",4)==0)
                {
                    (*current_cmd)+=4;
                    command='c';
                }
                else if(strncmp(*current_cmd,"list",4)==0)
                {
                    (*current_cmd)+=4;
                    command='l';
                }
                else if(strncmp(*current_cmd,"undelete",8)==0)
                {
                    (*current_cmd)+=8;
                    command='u';
                }
                else if(strncmp(*current_cmd,"superblock",10)==0)
                {
                    (*current_cmd)+=10;
                    command='s';
                }
                else if(isdigit(*current_cmd[0]))
                {
                    const unsigned int order= atoi(*current_cmd);
                    while(*current_cmd[0]!=',' && *current_cmd[0]!='\0')
                        (*current_cmd)++;
                    for(element=list_part; element!=NULL && element->part->order!=order; element=element->next);
                    if(element!=NULL)
                    {
                        current_element=element;
                        keep_asking=1;
                    }
                }
            } while(keep_asking>0);
        }
        else
        {
#ifdef HAVE_NCURSES
            command = wmenuSelect_ext(stdscr, INTER_ADV_Y+1, INTER_ADV_Y, INTER_ADV_X, menuAdv, 8, options,
                                      MENU_HORIZ | MENU_BUTTON | MENU_ACCEPT_OTHERS, &menu, NULL);
#else
            command = 'q';
#endif
        }
        switch(command)
        {
        case 'q':
        case 'Q':
            part_free_list(list_part);
            return;
#ifdef HAVE_NCURSES
        case 'a':
        case 'A':
            if(disk_car->arch!=&arch_none)
            {
                if(*current_cmd!=NULL)
                    list_part=add_partition_cli(disk_car, list_part, current_cmd);
                else
                    list_part=add_partition_ncurses(disk_car, list_part);
                current_element=list_part;
                rewrite=1;
            }
            break;
#endif
        }
#ifdef HAVE_NCURSES
        if(current_element!=NULL)
        {
            switch(command)
            {
            case 'p':
            case 'P':
            case KEY_UP:
                if(current_element->prev!=NULL)
                {
                    current_element=current_element->prev;
                    current_element_num--;
                }
                break;
            case 'n':
            case 'N':
            case KEY_DOWN:
                if(current_element->next!=NULL)
                {
                    current_element=current_element->next;
                    current_element_num++;
                }
                break;
            case KEY_PPAGE:
                for(i=0; i<INTER_ADV-1 && current_element->prev!=NULL; i++)
                {
                    current_element=current_element->prev;
                    current_element_num--;
                }
                break;
            case KEY_NPAGE:
                for(i=0; i<INTER_ADV-1 && current_element->next!=NULL; i++)
                {
                    current_element=current_element->next;
                    current_element_num++;
                }
                break;
            case 'b':
            case 'B':
            {
                partition_t *partition=current_element->part;
                if(is_part_fat32(partition))
                {
                    fat32_boot_sector(disk_car, partition, verbose, dump_ind, expert,current_cmd);
                    rewrite=1;
                }
                else if(is_part_fat12(partition) || is_part_fat16(partition))
                {
                    fat1x_boot_sector(disk_car, partition, verbose, dump_ind,expert,current_cmd);
                    rewrite=1;
                }
                else if(is_part_ntfs(partition))
                {
                    if(partition->upart_type==UP_EXFAT)
                        exFAT_boot_sector(disk_car, partition, verbose, current_cmd);
                    else
                        ntfs_boot_sector(disk_car, partition, verbose, expert, current_cmd);
                    rewrite=1;
                }
                else if(partition->upart_type==UP_FAT32)
                {
                    fat32_boot_sector(disk_car, partition, verbose, dump_ind, expert,current_cmd);
                    rewrite=1;
                }
                else if(partition->upart_type==UP_FAT12 || partition->upart_type==UP_FAT16)
                {
                    fat1x_boot_sector(disk_car, partition, verbose, dump_ind,expert,current_cmd);
                    rewrite=1;
                }
                else if(partition->upart_type==UP_NTFS)
                {
                    ntfs_boot_sector(disk_car, partition, verbose, expert, current_cmd);
                    rewrite=1;
                }
                else if(partition->upart_type==UP_EXFAT)
                {
                    exFAT_boot_sector(disk_car, partition, verbose, current_cmd);
                    rewrite=1;
                }
            }
            break;
            case 'c':
            case 'C':
            {
                char *dst_path;
#ifdef HAVE_NCURSES
                if(*current_cmd!=NULL)
                    dst_path=get_default_location();
                else
                {
                    char msg[256];
                    snprintf(msg, sizeof(msg),
                             "Please select where to store the file image.dd (%u MB), an image of the partition",
                             (unsigned int)(current_element->part->part_size/1000/1000));
                    dst_path=ask_location(msg, "", NULL);
                }
#else
                dst_path=get_default_location();
#endif
                if(dst_path!=NULL)
                {
                    char *filename=(char *)MALLOC(strlen(dst_path) + 1 + strlen(DEFAULT_IMAGE_NAME) + 1);
                    strcpy(filename, dst_path);
                    strcat(filename, "/");
                    strcat(filename, DEFAULT_IMAGE_NAME);
                    disk_image(disk_car, current_element->part, filename);
                    free(filename);
                    free(dst_path);
                }
            }
            rewrite=1;
            break;
            case 'u':
            case 'U':
            {
                partition_t *partition=current_element->part;
                if(partition->sb_offset!=0 && partition->sb_size>0)
                {
                    io_redir_add_redir(disk_car,
                                       partition->part_offset+partition->sborg_offset,
                                       partition->sb_size,
                                       partition->part_offset+partition->sb_offset,
                                       NULL);
                    if(partition->upart_type==UP_NTFS ||
                            (is_part_ntfs(partition) && partition->upart_type!=UP_EXFAT))
                        ntfs_undelete_part(disk_car, partition, verbose, current_cmd);
                    else
                        dir_partition(disk_car, partition, 0, current_cmd);
                    io_redir_del_redir(disk_car, partition->part_offset+partition->sborg_offset);
                }
                else
                {
                    if(partition->upart_type==UP_NTFS ||
                            (is_part_ntfs(partition) && partition->upart_type!=UP_EXFAT))
                        ntfs_undelete_part(disk_car, partition, verbose, current_cmd);
                    else
                        dir_partition(disk_car, partition, 0, current_cmd);
                }
            }
            rewrite=1;
            break;
            case 'l':
            case 'L':
            {
                partition_t *partition=current_element->part;
                if(partition->sb_offset!=0 && partition->sb_size>0)
                {
                    io_redir_add_redir(disk_car,
                                       partition->part_offset+partition->sborg_offset,
                                       partition->sb_size,
                                       partition->part_offset+partition->sb_offset,
                                       NULL);
                    dir_partition(disk_car,partition,verbose, current_cmd);
                    io_redir_del_redir(disk_car, partition->part_offset+partition->sborg_offset);
                }
                else
                    dir_partition(disk_car,partition,verbose, current_cmd);
            }
            rewrite=1;
            break;
            case 's':
            case 'S':
            {
                if(is_linux(current_element->part))
                {
                    list_part_t *list_sb=search_superblock(disk_car,current_element->part,verbose,dump_ind,1);
                    interface_superblock(disk_car,list_sb,current_cmd);
                    part_free_list(list_sb);
                }
                if(is_hfs(current_element->part) || is_hfsp(current_element->part))
                {
                    HFS_HFSP_boot_sector(disk_car, current_element->part, verbose, current_cmd);
                }
                rewrite=1;
            }
            break;
            case 't':
            case 'T':
                if(*current_cmd!=NULL)
                    change_part_type_cli(disk_car, current_element->part, current_cmd);
                else
                    change_part_type_ncurses(disk_car, current_element->part);
                rewrite=1;
                break;
            }
            if(current_element_num<offset)
                offset=current_element_num;
            if(current_element_num>=offset+INTER_ADV)
                offset=current_element_num-INTER_ADV+1;
        }
#endif
    }
}