summaryrefslogtreecommitdiffstats
path: root/src/adv.c
blob: 510b48fee25e6162d152ad75b3a669f125c1cd5e (plain)
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
/*

    File: adv.c

    Copyright (C) 1998-2009 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 the Free Software Foundation, Inc., 51
    Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

 */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_STDARG_H
#include <stdarg.h>
#endif
#include <ctype.h>
#include <assert.h>
#include "types.h"
#include "common.h"
#include "lang.h"
#include "intrf.h"
#include "intrfn.h"
#include "fnctdsk.h"
#include "chgtype.h"
#include "chgtypen.h"
#include "dirpart.h"
#include "fat.h"
#include "ntfs.h"
#include "adv.h"
#include "log.h"
#include "log_part.h"
#include "guid_cmp.h"
#include "dimage.h"
#include "ntfs_udl.h"
#include "ext2_sb.h"
#include "ext2_sbn.h"
#include "fat1x.h"
#include "fat32.h"
#include "texfat.h"
#include "tntfs.h"
#include "thfs.h"
#include "askloc.h"
#include "addpart.h"
#include "addpartn.h"
#include "io_redir.h"

extern const arch_fnct_t arch_gpt;
extern const arch_fnct_t arch_i386;
extern const arch_fnct_t arch_mac;
extern const arch_fnct_t arch_none;
extern const arch_fnct_t arch_sun;
extern const arch_fnct_t arch_xbox;

#ifdef HAVE_NCURSES
#define INTER_ADV_X	0
#define INTER_ADV_Y	(LINES-2)
#define INTER_ADV	(LINES-2-7-1)
#endif

#define DEFAULT_IMAGE_NAME "image.dd"

static int is_part_hfs(const partition_t *partition)
{
  if( partition->part_type_i386 == P_HFS ||
      partition->part_type_mac  == PMAC_HFS)
    return 1;
  if(guid_cmp(partition->part_type_gpt,GPT_ENT_TYPE_MAC_HFS)==0)
    return 1;
  return 0;
}

static int is_part_hfsp(const partition_t *partition)
{
  if( partition->part_type_i386 == P_HFSP ||
      partition->part_type_mac  == PMAC_HFS )
      return 1;
  if(guid_cmp(partition->part_type_gpt,GPT_ENT_TYPE_MAC_HFS)==0)
    return 1;
  return 0;
}

int is_part_linux(const partition_t *partition)
{
  if(partition->arch==&arch_i386 && partition->part_type_i386==P_LINUX)
      return 1;
  if(partition->arch==&arch_sun  && partition->part_type_sun==PSUN_LINUX)
      return 1;
  if(partition->arch==&arch_mac  && partition->part_type_mac==PMAC_LINUX)
      return 1;
  if(partition->arch==&arch_gpt &&
      (
       guid_cmp(partition->part_type_gpt,GPT_ENT_TYPE_LINUX_DATA)==0 ||
       guid_cmp(partition->part_type_gpt,GPT_ENT_TYPE_LINUX_HOME)==0 ||
       guid_cmp(partition->part_type_gpt,GPT_ENT_TYPE_LINUX_SRV)==0
      ))
      return 1;
  return 0;
}

static int is_exfat(const partition_t *partition)
{
  return (is_part_ntfs(partition) || partition->upart_type==UP_EXFAT);
}

static int is_hfs(const partition_t *partition)
{
  return (is_part_hfs(partition) || partition->upart_type==UP_HFS);
}

static int is_hfsp(const partition_t *partition)
{
  return (is_part_hfsp(partition) || partition->upart_type==UP_HFSP || partition->upart_type==UP_HFSX);
}

static int is_linux(const partition_t *partition)
{
  if(is_part_linux(partition))
    return 1;
  switch(partition->upart_type)
  {
    case UP_CRAMFS:
    case UP_EXT2:
    case UP_EXT3:
    case UP_EXT4:
    case UP_JFS:
    case UP_RFS:
    case UP_RFS2:
    case UP_RFS3:
    case UP_RFS4:
    case UP_XFS:
    case UP_XFS2:
    case UP_XFS3:
    case UP_XFS4:
    case UP_XFS5:
      return 1;
    default:
      break;
  }
  return 0;
}

#ifdef HAVE_NCURSES
static void interface_adv_ncurses(disk_t *disk, const int rewrite, list_part_t *list_part, const list_part_t *current_element, const int offset)
{
  list_part_t *element;
  int i;
  if(rewrite!=0)
  {
    aff_copy(stdscr);
    wmove(stdscr,4,0);
    wprintw(stdscr,"%s",disk->description(disk));
    if(list_part!=NULL)
      mvwaddstr(stdscr,6,0,msg_PART_HEADER_LONG);
  }
  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,element->part);
      wattroff(stdscr, A_REVERSE);
    } else
    {
      waddstr(stdscr, " ");
      aff_part(stdscr,AFF_PART_ORDER|AFF_PART_STATUS,disk,element->part);
    }
  }
  wmove(stdscr,7+INTER_ADV,5);
  wclrtoeol(stdscr);
  if(element!=NULL)
    wprintw(stdscr, "Next");
  if(current_element==NULL)
  {
    wmove(stdscr,7,0);
    wattrset(stdscr, A_REVERSE);
    wprintw(stdscr,"No partition available.");
    wattroff(stdscr, A_REVERSE);
  }
}
#endif

static int adv_string_to_command(char**current_cmd, list_part_t **current_element, list_part_t *list_part)
{
  int keep_asking;
  int command='q';
  assert(current_cmd!=NULL);
  do
  {
    keep_asking=0;
    skip_comma_in_command(current_cmd);
    if(check_command(current_cmd,"type",4)==0)
    {
      command='t';
    }
    else if(check_command(current_cmd,"addpart",7)==0)
    {
      command='a';
    }
    else if(check_command(current_cmd,"boot",4)==0)
    {
      command='b';
    }
    else if(check_command(current_cmd,"copy",4)==0)
    {
      command='c';
    }
    else if(check_command(current_cmd,"list",4)==0)
    {
      command='l';
    }
    else if(check_command(current_cmd,"undelete",8)==0)
    {
      command='u';
    }
    else if(check_command(current_cmd,"superblock",10)==0)
    {
      command='s';
    }
    else if(isdigit(*current_cmd[0]))
    {
      list_part_t *element;
      const unsigned int order= get_int_from_command(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);
  return command;
}

#ifdef HAVE_NCURSES
static const char *adv_get_boot_description(const partition_t *partition)
{
  assert(partition!=NULL);
  if(is_part_linux(partition))
  {
    return "Locate ext2/ext3/ext4 backup superblock";
  }
  else if(is_part_hfs(partition) || is_part_hfsp(partition))
  {
    return "Locate HFS/HFS+ backup volume header";
  }
  else if(is_linux(partition))
  {
    return "Locate ext2/ext3/ext4 backup superblock";
  }
  else if(is_hfs(partition) || is_hfsp(partition))
  {
    return "Locate HFS/HFS+ backup volume header";
  }
  return "Boot sector recovery";
}

static const char *adv_get_options_for_partition(const partition_t *partition)
{
  if(is_part_fat(partition))
  {
    return "tubcq";
  }
  else if(is_part_ntfs(partition))
    return "tlubcq";
  else if(is_part_linux(partition))
  {
    if(partition->upart_type==UP_EXT2)
      return "tuscq";
    else
      return "tlscq";
  }
  else if(is_part_hfs(partition) || is_part_hfsp(partition))
  {
    return "tscq";
  }
  else if(is_fat(partition))
    return "tubcq";
  else if(is_ntfs(partition) || is_exfat(partition))
    return "tlubcq";
  else if(is_linux(partition))
  {
    if(partition->upart_type==UP_EXT2)
      return "tluscq";
    else
      return "tlscq";
  }
  else if(is_hfs(partition) || is_hfsp(partition))
  {
    return "tscq";
  }
  return "tcq";
}
#endif

static int adv_menu_boot_selected(disk_t *disk, partition_t *partition, const int verbose,const int dump_ind, const unsigned int expert, char**current_cmd)
{
  if(is_part_fat32(partition))
  {
    fat32_boot_sector(disk, partition, verbose, dump_ind, expert,current_cmd);
    return 1;
  }
  else if(is_part_fat12(partition) || is_part_fat16(partition))
  {
    fat1x_boot_sector(disk, partition, verbose, dump_ind,expert,current_cmd);
    return 1;
  }
  else if(is_part_ntfs(partition))
  {
    if(partition->upart_type==UP_EXFAT)
      exFAT_boot_sector(disk, partition, current_cmd);
    else
      ntfs_boot_sector(disk, partition, verbose, expert, current_cmd);
    return 1;
  }
  else if(partition->upart_type==UP_FAT32)
  {
    fat32_boot_sector(disk, partition, verbose, dump_ind, expert,current_cmd);
    return 1;
  }
  else if(partition->upart_type==UP_FAT12 || partition->upart_type==UP_FAT16)
  {
    fat1x_boot_sector(disk, partition, verbose, dump_ind,expert,current_cmd);
    return 1;
  }
  else if(partition->upart_type==UP_NTFS)
  {
    ntfs_boot_sector(disk, partition, verbose, expert, current_cmd);
    return 1;
  }
  else if(partition->upart_type==UP_EXFAT)
  {
    exFAT_boot_sector(disk, partition, current_cmd);
    return 1;
  }
  return 0;
}

static void adv_menu_image_selected(disk_t *disk, const partition_t *partition, char **current_cmd)
{
  char dst_path[4096];
  dst_path[0]='\0';
#ifdef HAVE_NCURSES
  if(*current_cmd!=NULL)
    td_getcwd(dst_path, sizeof(dst_path));
  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)(partition->part_size/1000/1000));
    ask_location(dst_path, sizeof(dst_path), msg, "");
  }
#else
  td_getcwd(&dst_path, sizeof(dst_path));
#endif
  if(dst_path[0]!='\0')
  {
    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, partition, filename);
    free(filename);
  }
}

static void adv_menu_undelete_selected(disk_t *disk, const partition_t *partition, const int verbose, char **current_cmd)
{
  if(partition->sb_offset!=0 && partition->sb_size>0)
  {
    io_redir_add_redir(disk,
	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, partition, verbose, current_cmd);
    else
      dir_partition(disk, partition, 0, 0, current_cmd);
    io_redir_del_redir(disk, 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, partition, verbose, current_cmd);
    else
      dir_partition(disk, partition, 0, 0, current_cmd);
  }
}

static void adv_menu_list_selected(disk_t *disk, const partition_t *partition, const int verbose, const int expert, char **current_cmd)
{
  if(partition->sb_offset!=0 && partition->sb_size>0)
  {
    io_redir_add_redir(disk,
	partition->part_offset+partition->sborg_offset,
	partition->sb_size,
	partition->part_offset+partition->sb_offset,
	NULL);
    dir_partition(disk,partition, verbose, expert, current_cmd);
    io_redir_del_redir(disk, partition->part_offset+partition->sborg_offset);
  }
  else
    dir_partition(disk, partition, verbose, expert, current_cmd);
}

static void adv_menu_superblock_selected(disk_t *disk, partition_t *partition, const int verbose,const int dump_ind, char**current_cmd)
{
  if(is_linux(partition))
  {
    list_part_t *list_sb=search_superblock(disk,partition,verbose,dump_ind);
    interface_superblock(disk, list_sb, current_cmd);
    part_free_list(list_sb);
  }
  if(is_hfs(partition) || is_hfsp(partition))
  {
    HFS_HFSP_boot_sector(disk, partition, verbose, current_cmd);
  }
}

void interface_adv(disk_t *disk_car, const int verbose,const int dump_ind, const unsigned int expert, char**current_cmd)
{
  int current_element_num=0;
#ifdef HAVE_NCURSES
  int offset=0;
#endif
  int rewrite=1;
  unsigned int menu=0;
  list_part_t *list_part;
  list_part_t *current_element;
  assert(current_cmd!=NULL);
  log_info("\nInterface Advanced\n");
  list_part=disk_car->arch->read_part(disk_car,verbose,0);
  /*@ assert valid_list_part(list_part); */
  current_element=list_part;
  log_all_partitions(disk_car, list_part);
  while(1)
  {
    int command;
#ifdef HAVE_NCURSES
    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}
    };
    const char *options;
    int old_LINES=LINES;
    interface_adv_ncurses(disk_car, rewrite, list_part, current_element, offset);
#endif
    rewrite=0;
    if(current_element==NULL)
    {
#ifdef HAVE_NCURSES
      options="q";
#endif
    }
    else
    {
      if(menu==0 && (disk_car->arch!=&arch_none || current_element->part->upart_type!=UP_UNK))
	menu=1;
#ifdef HAVE_NCURSES
      options=adv_get_options_for_partition(current_element->part);
      menuAdv[2].desc=adv_get_boot_description(current_element->part);
#endif
    }
    if(*current_cmd!=NULL)
    {
      command=adv_string_to_command(current_cmd, &current_element, list_part);
    }
    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;
      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);
#ifdef HAVE_NCURSES
	  else
	    list_part=add_partition_ncurses(disk_car, list_part);
#endif
	  current_element=list_part;
	  rewrite=1;
	}
	break;
    }
    if(current_element!=NULL)
    {
      switch(command)
      {
	case 'p':
	case 'P':
#ifdef KEY_UP
	case KEY_UP:
#endif
	  if(current_element->prev!=NULL)
	  {
	    current_element=current_element->prev;
	    current_element_num--;
	  }
	  break;
	case 'n':
	case 'N':
#ifdef KEY_DOWN
	case KEY_DOWN:
#endif
	  if(current_element->next!=NULL)
	  {
	    current_element=current_element->next;
	    current_element_num++;
	  }
	  break;
#ifdef KEY_PPAGE
	case KEY_PPAGE:
	  {
	    int i;
	    for(i=0;i<INTER_ADV-1 && current_element->prev!=NULL;i++)
	    {
	      current_element=current_element->prev;
	      current_element_num--;
	    }
	  }
	  break;
#endif
#ifdef KEY_NPAGE
	case KEY_NPAGE:
	  {
	    int i;
	    for(i=0;i<INTER_ADV-1 && current_element->next!=NULL;i++)
	    {
	      current_element=current_element->next;
	      current_element_num++;
	    }
	  }
	  break;
#endif
	case 'b':
	case 'B':
	  rewrite=adv_menu_boot_selected(disk_car, current_element->part, verbose, dump_ind, expert, current_cmd);
	  break;
	case 'c':
	case 'C':
	  adv_menu_image_selected(disk_car, current_element->part, current_cmd);
	  rewrite=1;
	  break;
	case 'u':
	case 'U':
	  adv_menu_undelete_selected(disk_car, current_element->part, verbose, current_cmd);
	  rewrite=1;
	  break;
	case 'l':
	case 'L':
	  adv_menu_list_selected(disk_car, current_element->part, verbose, expert, current_cmd);
	  rewrite=1;
	  break;
	case 's':
	case 'S':
	  adv_menu_superblock_selected(disk_car, current_element->part, verbose, dump_ind, current_cmd);
	  rewrite=1;
	  break;
	case 't':
	case 'T':
	  if(*current_cmd!=NULL)
	    change_part_type_cli(disk_car, current_element->part, current_cmd);
#ifdef HAVE_NCURSES
	  else
	    change_part_type_ncurses(disk_car, current_element->part);
#endif
	  rewrite=1;
	  break;
      }
#ifdef HAVE_NCURSES
      if(current_element_num<offset)
	offset=current_element_num;
      if(current_element_num>=offset+INTER_ADV)
	offset=current_element_num-INTER_ADV+1;
#endif
    }
#ifdef HAVE_NCURSES
    if(old_LINES!=LINES)
      rewrite=1;
#endif
  }
}