summaryrefslogtreecommitdiffstats
path: root/src/next.c
blob: 5cd521281bbcb4bb67f81842728e767938574ff0 (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
/*

    File: next.c

    Copyright (C) 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.

 */
/*
   Instead of incrementing the sector number by one and checking if its value
   is aligned to a cylinder (PC) or similar boundary, return the next
   sector number that will be aligned to such boundary.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
 
#include "types.h"
#include "common.h"
#include "ext2.h"
#include "next.h"
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;

struct search_location
{
  uint64_t offset;
  uint64_t inc;
};
typedef struct search_location search_location_t;
static inline uint64_t CHS_to_offset(const unsigned int C, const int H, const int S,const disk_t *disk_car);

#define SEARCH_LOCATION_MAX 256
static unsigned int search_location_nbr=0;
static search_location_t search_location_info[SEARCH_LOCATION_MAX+1];

static inline uint64_t CHS_to_offset(const unsigned int C, const int H, const int S,const disk_t *disk_car)
{
  return (((uint64_t)C * disk_car->geom.heads_per_cylinder + H) * disk_car->geom.sectors_per_head +(S>0?S-1:S))*disk_car->sector_size;
}

static void update_location(void)
{
  unsigned int i;
  const search_location_t *src=&search_location_info[search_location_nbr];
  if(src->inc==0)
  {
    for(i=0; i<search_location_nbr; i++)
    {
      if(search_location_info[i].offset == src->offset)
	return ;
    }
    if(search_location_nbr < SEARCH_LOCATION_MAX)
      search_location_nbr++;
    return;
  }
  for(i=0; i<search_location_nbr; i++)
  {
    search_location_t *cur=&search_location_info[i];
    if(cur->offset == src->offset &&
	cur->inc >= src->inc &&
	cur->inc % src->inc==0)
    {
      cur->inc=src->inc;
      return ;
    }
    if(cur->inc == 0)
    {
      if(cur->offset == src->offset)
      {
	cur->inc = src->inc;
	return ;
      }
    }
    else
    {
      if(cur->offset == src->offset &&
	  src->inc >= cur->inc && src->inc % cur->inc==0)
	return ;
      if(cur->inc==src->inc &&
	  cur->offset >= src->offset &&
	  (cur->offset - src->offset)%cur->inc==0)
      {
	cur->offset=src->offset;
	return ;
      }
      if(cur->inc==src->inc &&
	  src->offset >= cur->offset &&
	  (src->offset - cur->offset)%cur->inc==0)
	return ;
    }
  }
  if(search_location_nbr < SEARCH_LOCATION_MAX)
    search_location_nbr++;
}

void search_location_init(const disk_t *disk_car, const unsigned int location_boundary, const int fast_mode)
{
  /* test_nbr==1... */
  if(disk_car->arch==&arch_i386)
  {
    if(fast_mode>1)
    {
      search_location_info[search_location_nbr].offset=CHS_to_offset(0,0,1,disk_car);
      search_location_info[search_location_nbr].inc= CHS_to_offset(0,1,0,disk_car);
      update_location();
    }
    else
    {
      //CHS H=0,1,2 S=1
      search_location_info[search_location_nbr].offset=CHS_to_offset(0,0,1,disk_car);
      search_location_info[search_location_nbr].inc=CHS_to_offset(1,0,0,disk_car);
      update_location();
      search_location_info[search_location_nbr].offset=CHS_to_offset(0,1,1,disk_car);
      search_location_info[search_location_nbr].inc=CHS_to_offset(1,0,0,disk_car);
      update_location();
      search_location_info[search_location_nbr].offset=CHS_to_offset(0,2,1,disk_car);
      search_location_info[search_location_nbr].inc=CHS_to_offset(1,0,0,disk_car);
    }
    search_location_info[search_location_nbr].offset=0;
    search_location_info[search_location_nbr].inc=2048*512;
    update_location();
  }
  else
  {
    search_location_info[search_location_nbr].offset=0;
    search_location_info[search_location_nbr].inc=location_boundary;
    update_location();
  }
  if(fast_mode>0)
  {
    /* test_nbr==2 FAT32 backup boot sector */
    if(disk_car->arch==&arch_i386)
    {
      //CHS H=0,1,2 S=7
      search_location_info[search_location_nbr].offset=CHS_to_offset(0,0,7,disk_car);
      search_location_info[search_location_nbr].inc=CHS_to_offset(1,0,0,disk_car);
      update_location();
      search_location_info[search_location_nbr].offset=CHS_to_offset(0,1,7,disk_car);
      search_location_info[search_location_nbr].inc=CHS_to_offset(1,0,0,disk_car);
      update_location();
      search_location_info[search_location_nbr].offset=CHS_to_offset(0,2,7,disk_car);
      search_location_info[search_location_nbr].inc=CHS_to_offset(1,0,0,disk_car);
      update_location();
    }
    else
    {
      search_location_info[search_location_nbr].offset=CHS_to_offset(0,0,7,disk_car);
      search_location_info[search_location_nbr].inc=location_boundary;
      update_location();
    }
    search_location_info[search_location_nbr].offset=CHS_to_offset(0,0,7,disk_car);
    search_location_info[search_location_nbr].inc=2048*512;
    update_location();
    /* test_nbr==3 ou test_nbr==4, NTFS or HFS backup boot sector */
    if(disk_car->arch==&arch_i386)
    {
      search_location_info[search_location_nbr].offset=CHS_to_offset(1,0,-1,disk_car);
      search_location_info[search_location_nbr].inc=CHS_to_offset(1,0,0,disk_car);
      update_location();
    }
    else
    {
      search_location_info[search_location_nbr].offset=location_boundary-512;
      search_location_info[search_location_nbr].inc=location_boundary;
      update_location();
    }
    search_location_info[search_location_nbr].offset=(2048-1)*512;
    search_location_info[search_location_nbr].inc=2048*512;
    update_location();
    /* test_nbr==5*/
    {
      int s_log_block_size;
      for(s_log_block_size=0;s_log_block_size<=2;s_log_block_size++)
      {
	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(disk_car->arch==&arch_i386)
	{
	  search_location_info[search_location_nbr].offset=CHS_to_offset(0,0,1,disk_car)+hd_offset;
	  search_location_info[search_location_nbr].inc=CHS_to_offset(1,0,0,disk_car);
	  update_location();
	  search_location_info[search_location_nbr].offset=CHS_to_offset(0,1,1,disk_car)+hd_offset;
	  search_location_info[search_location_nbr].inc=CHS_to_offset(1,0,0,disk_car);
	  update_location();
	  search_location_info[search_location_nbr].offset=CHS_to_offset(0,2,1,disk_car)+hd_offset;
	  search_location_info[search_location_nbr].inc=CHS_to_offset(1,0,0,disk_car);
	  update_location();
	}
	else
	{
	  search_location_info[search_location_nbr].offset=CHS_to_offset(0,0,1,disk_car)+hd_offset;
	  search_location_info[search_location_nbr].inc=location_boundary;
	  update_location();
	}
      }
    }
  }
}

uint64_t search_location_update(const uint64_t location)
{
  unsigned int i;
  uint64_t min=(uint64_t)-1;
  for(i=0;i<search_location_nbr;i++)
  {
    while(search_location_info[i].offset<=location)
      search_location_info[i].offset+=search_location_info[i].inc;
    if(min>search_location_info[i].offset)
      min=search_location_info[i].offset;
  }
  return min;
}