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
|
/*
File: partmac.c
Copyright (C) 1998-2005 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
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#include <ctype.h> /* tolower */
#include "types.h"
#include "common.h"
#include "testdisk.h"
#include "fnctdsk.h"
//#include "analyse.h"
#include "lang.h"
#include "intrf.h"
#include "chgtype.h"
#include "partmac.h"
#include "savehdr.h"
#include "cramfs.h"
#include "ext2.h"
#include "hfs.h"
#include "hfsp.h"
#include "jfs_superblock.h"
#include "jfs.h"
#include "rfs.h"
#include "xfs.h"
static int check_part_mac(t_param_disk *disk_car, const int debug,t_partition *partition,const int saveheader);
static int get_geometry_from_macmbr(const unsigned char *buffer, const int debug, t_CHS *geometry);
static t_list_part *read_part_mac(t_param_disk *disk_car, const int debug, const int saveheader);
static int write_part_mac(t_param_disk *disk_car, const t_list_part *list_part, const int ro , const int debug, const int align);
static t_list_part *init_part_order_mac(const t_param_disk *disk_car, t_list_part *list_part);
static t_list_part *add_partition_mac(t_param_disk *disk_car,t_list_part *list_part, const int debug, char **current_cmd);
static void set_next_status_mac(const t_param_disk *disk_car, t_partition *partition);
static int test_structure_mac(t_list_part *list_part);
static int set_part_type_mac(t_partition *partition, unsigned int part_type_mac);
static int is_part_known_mac(const t_partition *partition);
static void init_structure_mac(const t_param_disk *disk_car,t_list_part *list_part, const int debug);
static const unsigned char *get_partition_name_mac(const t_partition *partition);
static const unsigned char *get_partition_name_mac_aux(const unsigned int part_type_mac);
static unsigned int get_part_type_mac(const t_partition *partition);
static const struct systypes mac_sys_types[] = {
{ PMAC_DRIVER43, "Driver43" },
{ PMAC_DRIVERATA, "Driver_ATA" },
{ PMAC_DRIVERIO, "Driver_IOKit" },
{ PMAC_FREE, "Free" },
{ PMAC_FWDRIVER, "FWDriver" },
{ PMAC_SWAP, "Swap" },
{ PMAC_LINUX, "Linux" },
{ PMAC_HFS, "HFS" },
{ PMAC_MAP, "partition_map" },
{ PMAC_PATCHES, "Patches" },
{ PMAC_UNK, "Unknown" },
{ PMAC_NewWorld, "NewWorld" },
{ PMAC_DRIVER, "Driver" },
{ PMAC_MFS, "MFS" },
{ PMAC_PRODOS, "ProDOS" },
{ PMAC_UNK, NULL }
};
t_arch_fnct arch_mac=
{
.part_name="Mac",
.part_name_option="partition_mac",
.msg_part_type=" P=Primary D=Deleted",
.read_part=read_part_mac,
.write_part=write_part_mac,
.init_part_order=init_part_order_mac,
.get_geometry_from_mbr=get_geometry_from_macmbr,
.check_part=check_part_mac,
.write_MBR_code=NULL,
.add_partition=add_partition_mac,
.set_prev_status=set_next_status_mac,
.set_next_status=set_next_status_mac,
.test_structure=test_structure_mac,
.set_part_type=set_part_type_mac,
.is_part_known=is_part_known_mac,
.init_structure=init_structure_mac,
.erase_list_part=NULL,
.get_partition_name=get_partition_name_mac,
.get_part_type=get_part_type_mac
};
static unsigned int get_part_type_mac(const t_partition *partition)
{
return partition->part_type_mac;
}
int get_geometry_from_macmbr(const unsigned char *buffer, const int debug, t_CHS *geometry)
{
const mac_Block0 *maclabel=(const mac_Block0*)buffer;
if(debug>1)
{
ecrit_rapport("get_geometry_from_macmbr\n");
}
geometry->cylinder=0;
geometry->head=0;
geometry->sector=1;
if(geometry->sector>0)
{
ecrit_rapport("Geometry from MAC MBR: head=%u sector=%u\n",geometry->head+1,geometry->sector);
}
return 0;
}
t_list_part *read_part_mac(t_param_disk *disk_car, const int debug, const int saveheader)
{
unsigned char buffer[DEFAULT_SECTOR_SIZE];
t_list_part *new_list_part=NULL;
unsigned int i;
unsigned int limit=1;
aff_buffer(BUFFER_RESET,"Q");
if(disk_car->read(disk_car,sizeof(buffer), &buffer, 0)!=0)
return new_list_part;
{
mac_Block0 *maclabel=(mac_Block0*)&buffer;
if (be16(maclabel->sbSig) != BLOCK0_SIGNATURE)
{
aff_buffer(BUFFER_ADD,"\nBad MAC partition, invalid block0 signature\n");
return NULL;
}
}
for(i=1;i<=limit;i++)
{
mac_DPME *dpme=(mac_DPME *)buffer;
if(disk_car->read(disk_car,sizeof(buffer), &buffer, (uint64_t)i*PBLOCK_SIZE)!=0)
return new_list_part;
if(be16(dpme->dpme_signature) != DPME_SIGNATURE)
{
aff_buffer(BUFFER_ADD,"\nread_part_mac: bad DPME signature");
return new_list_part;
}
{
t_partition *new_partition=partition_new();
partition_reset(new_partition);
new_partition->order=i;
if (strcmp(dpme->dpme_type, "Apple_UNIX_SVR2")==0)
{
if (!strcmp(dpme->dpme_name, "Swap") || !strcmp(dpme->dpme_name, "swap"))
new_partition->part_type_mac=PMAC_SWAP;
else
new_partition->part_type_mac=PMAC_LINUX;
}
else if (strcmp(dpme->dpme_type, "Apple_Bootstrap")==0)
new_partition->part_type_mac=PMAC_NewWorld;
else if (strcmp(dpme->dpme_type, "Apple_Scratch")==0)
new_partition->part_type_mac=PMAC_SWAP;
else if(strcmp(dpme->dpme_type,"Apple_Driver")==0)
new_partition->part_type_mac=PMAC_DRIVER;
else if(strcmp(dpme->dpme_type,"Apple_Driver43")==0)
new_partition->part_type_mac=PMAC_DRIVER43;
else if(strcmp(dpme->dpme_type,"Apple_Driver_ATA")==0)
new_partition->part_type_mac=PMAC_DRIVERATA;
else if(strcmp(dpme->dpme_type,"Apple_Driver_IOKit")==0)
new_partition->part_type_mac=PMAC_DRIVERIO;
else if(strcmp(dpme->dpme_type,"Apple_Free")==0)
new_partition->part_type_mac=PMAC_FREE;
else if(strcmp(dpme->dpme_type,"Apple_FWDriver")==0)
new_partition->part_type_mac=PMAC_FWDRIVER;
else if(strcmp(dpme->dpme_type,"Apple_partition_map")==0)
new_partition->part_type_mac=PMAC_MAP;
else if(strcmp(dpme->dpme_type,"Apple_Patches")==0)
new_partition->part_type_mac=PMAC_PATCHES;
else if(strcmp(dpme->dpme_type,"Apple_HFS")==0)
new_partition->part_type_mac=PMAC_HFS;
else if(strcmp(dpme->dpme_type,"Apple_MFS")==0)
new_partition->part_type_mac=PMAC_MFS;
else if(strcmp(dpme->dpme_type,"Apple_PRODOS")==0)
new_partition->part_type_mac=PMAC_PRODOS;
else
{
new_partition->part_type_mac=PMAC_UNK;
ecrit_rapport("%s\n",dpme->dpme_type);
}
new_partition->part_offset=(uint64_t)be32(dpme->dpme_pblock_start)*PBLOCK_SIZE;
new_partition->part_size=(uint64_t)be32(dpme->dpme_pblocks)*PBLOCK_SIZE;
new_partition->status=STATUS_PRIM;
disk_car->arch->check_part(disk_car,debug,new_partition,saveheader);
new_list_part=insert_new_partition(new_list_part,new_partition);
aff_part_buffer(AFF_PART_ORDER,disk_car,new_partition);
}
if(i==1)
{
limit=be32(dpme->dpme_map_entries);
}
}
return new_list_part;
}
static int write_part_mac(t_param_disk *disk_car, const t_list_part *list_part, const int ro, const int debug, const int align)
{ /* TODO: Implement it */
if(ro==0)
{
/* not_implemented("write_part_mac"); */
WINDOW *window=newwin(0,0,0,0); /* full screen */
aff_copy(window);
wmove(window,7,0);
wdoprintf(window,"Function write_part_mac not implemented");
ecrit_rapport("Function write_part_mac not implemented\n");
wmove(window,8,0);
wdoprintf(window,"Use pdisk to recreate the missing partition");
wmove(window,9,0);
wdoprintf(window,"using values displayed by TestDisk");
wmove(window,22,0);
wattrset(window,A_STANDOUT);
wdoprintf(window,"[ Abort ]");
wattroff(window,A_STANDOUT);
wrefresh(window);
while(wgetch(window)==ERR);
delwin(window);
(void) clearok(stdscr, TRUE);
#ifdef HAVE_TOUCHWIN
touchwin(stdscr);
#endif
}
return 0;
}
static t_list_part *init_part_order_mac(const t_param_disk *disk_car, t_list_part *list_part)
{
return list_part;
}
static t_list_part *add_partition_mac(t_param_disk *disk_car,t_list_part *list_part, const int debug, char **current_cmd)
{
t_CHS start,end;
t_partition *new_partition=partition_new();
int position=0;
start.cylinder=0;
start.head=0;
start.sector=1;
end.cylinder=disk_car->CHS.cylinder;
end.head=disk_car->CHS.head;
end.sector=disk_car->CHS.sector;
{
int done = FALSE;
while (done==FALSE) {
int command;
static struct MenuItem menuGeometry[]=
{
{ 'c', "Cylinders", "Change starting cylinder" },
{ 'C', "Cylinders", "Change ending cylinder" },
{ 'T' ,"Type", "Change partition type"},
{ 'd', "Done", "" },
{ 0, NULL, NULL }
};
aff_copy(stdscr);
wmove(stdscr,4,0);
wdoprintf(stdscr,"%s",disk_car->description(disk_car));
new_partition->part_offset=CHS2offset(disk_car,&start);
new_partition->part_size=CHS2offset(disk_car,&end) - new_partition->part_offset + disk_car->sector_size;
wmove(stdscr,10, 0);
wclrtoeol(stdscr);
aff_part(stdscr,AFF_PART_SHORT,disk_car,new_partition);
wmove(stdscr,COMMAND_LINE_Y, COMMAND_LINE_X);
wclrtoeol(stdscr);
wrefresh(stdscr);
command=wmenuSimple(stdscr,menuGeometry, position);
switch (command) {
case 'c':
start.cylinder=ask_number(start.cylinder,0,disk_car->CHS.cylinder,"Enter the starting cylinder ");
position=1;
break;
case 'C':
end.cylinder=ask_number(end.cylinder,start.cylinder,disk_car->CHS.cylinder,"Enter the ending cylinder ");
position=2;
break;
case 'T':
case 't':
change_part_type(disk_car,new_partition,current_cmd);
position=3;
break;
case key_ESC:
case 'd':
case 'D':
case 'q':
case 'Q':
done = TRUE;
break;
}
}
}
if((CHS2offset(disk_car,&end)>new_partition->part_offset)&& new_partition->part_type_mac>0)
{
t_list_part *new_list_part=insert_new_partition(list_part, new_partition);
new_partition->status=STATUS_PRIM;
if(test_structure_mac(list_part)!=0)
{
new_partition->status=STATUS_DELETED;
}
return new_list_part;
}
free(new_partition);
return list_part;
}
static void set_next_status_mac(const t_param_disk *disk_car, t_partition *partition)
{
if(partition->status==STATUS_DELETED)
partition->status=STATUS_PRIM;
else
partition->status=STATUS_DELETED;
}
static int test_structure_mac(t_list_part *list_part)
{ /* Return 1 if bad*/
t_list_part *new_list_part=NULL;
t_list_part *element;
t_list_part *new_element;
int res=0;
/* Sort list_part in new_list_part */
for(element=list_part;element!=NULL;element=element->next)
{
if(element->part->status!=STATUS_DELETED)
new_list_part=insert_new_partition(new_list_part,element->part);
}
/* Test overlapping */
for(element=new_list_part;element!=NULL;element=element->next)
{
if(((element->prev!=NULL) && (element->part->part_offset<=element->prev->part->part_offset+element->prev->part->part_size-1)) ||
((element->next!=NULL) && (element->part->part_offset+element->part->part_size-1>=element->next->part->part_offset)))
{
res=1;
}
}
for(element=new_list_part;element!=NULL;element=new_element)
{
new_element=element->next;
free(element);
}
return res;
}
static int set_part_type_mac(t_partition *partition, unsigned int part_type_mac)
{
if(part_type_mac>0 && part_type_mac <= 255)
{
partition->part_type_mac=part_type_mac;
return 0;
}
return 1;
}
static int is_part_known_mac(const t_partition *partition)
{
return (partition->part_type_mac!=PMAC_UNK);
}
static void init_structure_mac(const t_param_disk *disk_car,t_list_part *list_part, const int debug)
{
t_list_part *element;
t_list_part *new_list_part=NULL;
/* Create new list */
for(element=list_part;element!=NULL;element=element->next)
element->to_be_removed=0;
for(element=list_part;element!=NULL;element=element->next)
{
int to_be_removed=0;
t_list_part *element2;
for(element2=element->next;element2!=NULL;element2=element2->next)
if(element->part->part_offset+element->part->part_size-1 >= element2->part->part_offset)
{
to_be_removed=1;
element2->to_be_removed=1;
}
if(to_be_removed)
element->to_be_removed=1;
if(element->to_be_removed==0)
new_list_part=insert_new_partition_aux(new_list_part,element_new(element->part),1);
}
#ifdef DEBUG
check_list_part(new_list_part);
#endif
for(element=new_list_part;element!=NULL;element=element->next)
element->part->status=STATUS_PRIM;
if(disk_car->arch->test_structure(new_list_part))
{
for(element=new_list_part;element!=NULL;element=element->next)
element->part->status=STATUS_DELETED;
}
{
/* free */
t_list_part *new_element;
for(element=new_list_part;element!=NULL;element=new_element)
{
new_element=element->next;
free(element);
}
}
#ifdef DEBUG
check_list_part(list_part);
#endif
}
static int check_part_mac(t_param_disk *disk_car,const int debug,t_partition *partition, const int saveheader)
{
int ret=0;
switch(partition->part_type_mac)
{
case PMAC_DRIVER43:
case PMAC_DRIVERATA:
case PMAC_DRIVERIO:
case PMAC_FREE:
case PMAC_FWDRIVER:
case PMAC_SWAP:
case PMAC_MAP:
case PMAC_PATCHES:
case PMAC_UNK:
case PMAC_NewWorld:
case PMAC_DRIVER:
case PMAC_MFS:
case PMAC_PRODOS:
break;
case PMAC_LINUX:
ret=check_JFS(disk_car,partition,debug);
if(ret!=0)
{
ret=check_rfs(disk_car,partition,debug);
}
if(ret!=0)
{
ret=check_EXT2(disk_car,partition,debug);
}
if(ret!=0)
{
ret=check_cramfs(disk_car,partition,debug);
}
if(ret!=0)
{
ret=check_xfs(disk_car,partition,debug);
}
if(ret!=0)
{ aff_buffer(BUFFER_ADD,"No EXT2, JFS, Reiser, cramfs or XFS marker\n"); }
break;
case PMAC_HFS:
ret=check_HFS(disk_car,partition,debug);
if(ret!=0)
{
ret=check_HFSP(disk_car,partition,debug);
}
break;
default:
if(debug>0)
{
ecrit_rapport("check_part_mac %u type %02X: no test\n",partition->order,partition->part_type_mac);
}
break;
}
if(ret!=0)
{
ecrit_rapport("check_part_mac failed for partition type %02X\n", partition->part_type_mac);
aff_part_buffer(AFF_PART_ORDER,disk_car,partition);
if(saveheader>0)
{
save_header(disk_car,partition,debug);
}
}
return ret;
}
static const unsigned char *get_partition_name_mac_aux(const unsigned int part_type_mac)
{
int i;
for (i=0; mac_sys_types[i].name!=NULL; i++)
if (mac_sys_types[i].part_type == part_type_mac)
return mac_sys_types[i].name;
return NULL;
}
static const unsigned char *get_partition_name_mac(const t_partition *partition)
{
return get_partition_name_mac_aux(partition->part_type_mac);
}
|