summaryrefslogtreecommitdiffstats
path: root/src/exfat.c
blob: 8aef785411f2194395f109a81b70fc55231ec266 (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
/*

    File: exfat.c

    Copyright (C) 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
 
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#include <stdio.h>
#include "types.h"
#include "common.h"
#include "exfat.h"
#include "log.h"

extern const arch_fnct_t arch_none;

uint64_t exfat_cluster_to_offset(const struct exfat_super_block *exfat_header, const unsigned int cluster)
{
  return ((uint64_t)(((cluster-2) << exfat_header->block_per_clus_bits) + le32(exfat_header->clus_blocknr))) << exfat_header->blocksize_bits;
}

int exfat_read_cluster(disk_t *disk, const partition_t *partition, const struct exfat_super_block*exfat_header, void *buffer, const unsigned int cluster)
{
  return disk->pread(disk,
      buffer,
      1 << (exfat_header->block_per_clus_bits + exfat_header->blocksize_bits),
      partition->part_offset + exfat_cluster_to_offset(exfat_header, cluster));
}

/*@
  @ requires \valid(partition);
  @ requires \valid_read(exfat_header);
  @ requires \separated(partition, exfat_header);
  @*/
static void set_exFAT_info(partition_t *partition, const struct exfat_super_block*exfat_header)
{
  partition->upart_type=UP_EXFAT;
  partition->blocksize=1<<(exfat_header->block_per_clus_bits + exfat_header->blocksize_bits);
  partition->fsname[0]='\0';
  if(partition->sb_offset==0)
    snprintf(partition->info, sizeof(partition->info), "exFAT, blocksize=%u", partition->blocksize);
  else
    snprintf(partition->info, sizeof(partition->info), "exFAT found using backup sector, blocksize=%u", partition->blocksize);
}

int check_exFAT(disk_t *disk, partition_t *partition)
{
  unsigned char *buffer=(unsigned char*)MALLOC(EXFAT_BS_SIZE);
  if(disk->pread(disk, buffer, EXFAT_BS_SIZE, partition->part_offset) != EXFAT_BS_SIZE)
  {
    free(buffer);
    return 1;
  }
  if(test_exFAT((struct exfat_super_block*)buffer)!=0)
  {
    free(buffer);
    return 1;
  }
  set_exFAT_info(partition, (struct exfat_super_block*)buffer);
  free(buffer);
  return 0;
}

int test_exFAT(const struct exfat_super_block *exfat_header)
{
  if(le16(exfat_header->signature)!=0xAA55)
    return 1;
  if(memcmp(exfat_header->oem_id, "EXFAT   ", sizeof(exfat_header->oem_id))!=0)
    return 1;
  return 0;
}

int recover_exFAT(const disk_t *disk, const struct exfat_super_block *exfat_header, partition_t *partition)
{
  if(test_exFAT(exfat_header)!=0)
    return 1;
  partition->sborg_offset=0;
  partition->sb_size=12 << exfat_header->blocksize_bits;
  partition->part_type_i386=P_EXFAT;
  partition->part_type_gpt=GPT_ENT_TYPE_MS_BASIC_DATA;
  partition->part_size=(uint64_t)le64(exfat_header->nr_sectors) * disk->sector_size;
#ifdef DEBUG_exFAT
  log_info("recover_exFAT:\n");
  log_info("start_sector=%llu\n", (long long unsigned)le64(exfat_header->start_sector));
  log_info("blocksize=%u\n", (12<<exfat_header->blocksize_bits));
  log_info("part_offset=%llu\n", partition->part_offset);
#endif
  if((le64(exfat_header->start_sector) * disk ->sector_size +
      (12 << exfat_header->blocksize_bits) == partition->part_offset) ||
    (disk->arch==&arch_none && ((uint64_t)12 << exfat_header->blocksize_bits) == partition->part_offset))
  {
    partition->sb_offset=12 << exfat_header->blocksize_bits;
    partition->part_offset-=partition->sb_offset;
  }
  set_exFAT_info(partition, exfat_header);
  return 0;
}