summaryrefslogtreecommitdiffstats
path: root/src/hfsp.c
blob: 040a859dd588cde21df8861ba2b1c7110223df05 (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
/*

    File: hfsp.c

    Copyright (C) 1998-2007 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
#include "types.h"
#include "common.h"
#include "hfsp.h"
#include "fnctdsk.h"
#include "log.h"

static void set_HFSP_info(partition_t *partition, const struct hfsp_vh *vh)
{
  partition->blocksize=be32(vh->blocksize);
  partition->fsname[0]='\0';
  if (be16(vh->version)==4)
  {
    partition->upart_type=UP_HFSP;
    snprintf(partition->info, sizeof(partition->info), "HFS+ blocksize=%u", partition->blocksize);
  }
  else if (be16(vh->version)==5)
  {
    partition->upart_type=UP_HFSX;
    snprintf(partition->info, sizeof(partition->info), "HFSX blocksize=%u", partition->blocksize);
  }
}


int check_HFSP(disk_t *disk_car,partition_t *partition,const int verbose)
{
  unsigned char *buffer=(unsigned char*)MALLOC(HFSP_BOOT_SECTOR_SIZE);
  if(disk_car->pread(disk_car, buffer, HFSP_BOOT_SECTOR_SIZE, partition->part_offset + 0x400) != HFSP_BOOT_SECTOR_SIZE)
  {
    free(buffer);
    return 1;
  }
  if(test_HFSP(disk_car,(struct hfsp_vh *)buffer,partition,verbose,0)!=0)
  {
    free(buffer);
    return 1;
  }
  set_HFSP_info(partition, (const struct hfsp_vh *)buffer);
  if(disk_car->pread(disk_car, buffer, HFSP_BOOT_SECTOR_SIZE, 
	partition->part_offset + partition->part_size - 0x400) == HFSP_BOOT_SECTOR_SIZE &&
      test_HFSP(disk_car,(struct hfsp_vh *)buffer,partition,verbose,0)==0)
  {
    strcat(partition->info, " + Backup");
  }
  free(buffer);
  return 0;
}

int recover_HFSP(disk_t *disk_car, const struct hfsp_vh *vh,partition_t *partition,const int verbose, const int dump_ind, const int backup)
{
  uint64_t part_size;
  if(test_HFSP(disk_car,vh,partition,verbose,dump_ind)!=0)
    return 1;
  part_size=(uint64_t)be32(vh->total_blocks)*be32(vh->blocksize);
  partition->sborg_offset=0x400;
  partition->sb_size=HFSP_BOOT_SECTOR_SIZE;
  if(backup>0)
  {
    if(partition->part_offset+2*disk_car->sector_size<part_size)
      return 1;
    /* backup is at total_blocks-2 */
    partition->sb_offset=part_size-0x400;
    partition->part_offset-=partition->sb_offset;
  }
  partition->part_size=part_size;
  set_HFSP_info(partition, vh);
  if(backup==0)
  {
    unsigned char *buffer=(unsigned char*)MALLOC(HFSP_BOOT_SECTOR_SIZE);
    if(disk_car->pread(disk_car, buffer, HFSP_BOOT_SECTOR_SIZE, 
	  partition->part_offset + partition->part_size - 0x400) == HFSP_BOOT_SECTOR_SIZE &&
	test_HFSP(disk_car,(struct hfsp_vh *)buffer,partition,verbose,0)==0)
    {
      strcat(partition->info, " + Backup");
    }
    free(buffer);
  }
  partition->part_type_i386=P_HFSP;
  partition->part_type_mac=PMAC_HFS;
  partition->part_type_gpt=GPT_ENT_TYPE_MAC_HFS;
  if(verbose>0)
  {
    log_info("part_size %lu\n",(long unsigned)(partition->part_size/disk_car->sector_size));
  }
  return 0;
}

int test_HFSP(disk_t *disk_car, const struct hfsp_vh *vh, const partition_t *partition, const int verbose, const int dump_ind)
{
  if (be32(vh->free_blocks) > be32(vh->total_blocks))
    return 1;
  /* Blocksize must be a multiple of 512 */
  if (be32(vh->blocksize)<512 ||
      ((be32(vh->blocksize)-1) & be32(vh->blocksize))!=0)
    return 1;
  /* http://developer.apple.com/technotes/tn/tn1150.html */
  if (be16(vh->version)==4 && vh->signature==be16(HFSP_VOLHEAD_SIG))
  {
    if(partition==NULL)
      return 0;
    if(verbose>0 || dump_ind!=0)
    {
      log_info("\nHFS+ magic value at %u/%u/%u\n", offset2cylinder(disk_car,partition->part_offset),offset2head(disk_car,partition->part_offset),offset2sector(disk_car,partition->part_offset));
    }
  }
  else if (be16(vh->version)==5 && vh->signature==be16(HFSX_VOLHEAD_SIG))
  {
    if(partition==NULL)
      return 0;
    if(verbose>0 || dump_ind!=0)
    {
      log_info("\nHFSX magic value at %u/%u/%u\n", offset2cylinder(disk_car,partition->part_offset),offset2head(disk_car,partition->part_offset),offset2sector(disk_car,partition->part_offset));
    }
  }
  else
  {
    return 1;
  }
  if(dump_ind!=0)
  {
    /* There is a little offset ... */
    dump_log(vh,DEFAULT_SECTOR_SIZE);
  }
  if(verbose>1)
  {
    log_info("blocksize %u\n",(unsigned) be32(vh->blocksize));
    log_info("total_blocks %u\n",(unsigned) be32(vh->total_blocks));
    log_info("free_blocks  %u\n",(unsigned) be32(vh->free_blocks));
  }
  return 0;
}