summaryrefslogtreecommitdiffstats
path: root/debian/grub-extras/ntldr-img/utils.c
blob: d1f3bfaafbddf13ab808cff72099e340237674d2 (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
/*
 *  GRUB Utilities --  Utilities for GRUB Legacy, GRUB2 and GRUB for DOS
 *  Copyright (C) 2007 Bean (bean123@126.com)
 *
 *  This program 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 to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#ifdef LINUX

#define _FILE_OFFSET_BITS 64	// This is required to enable 64-bit off_t
#include <unistd.h>

#endif

#include <stdio.h>
#include <string.h>
#include <fcntl.h>

#include "utils.h"

static unsigned char ebuf[512];

#if defined(WIN32)

#ifdef __GNUC__				// Mingw or Cygwin

#define u_off_t		off64_t
#define u_lseek		lseek64

#else

#define u_off_t		__int64
#define u_lseek		_lseeki64

#endif

#else

#define u_off_t		off_t		// In FreeBSD, off_t is 64-bit !
#define u_lseek		lseek

#endif

int go_sect(int hd,unsigned long sec)
{
  // Test if 64-bit seek is supported
  if (sizeof(u_off_t)>=8)
    {
      u_off_t bs,rs;

      bs=sec;
      bs<<=9;
      rs=u_lseek(hd,bs,SEEK_SET);
      return (bs!=rs);
    }
  else
    {
      unsigned long bs[2];

      bs[0]=sec<<9;
      bs[1]=sec>>23;
      if (bs[1])
        return 1;
      return (lseek(hd,bs[0],SEEK_SET)!=(off_t)bs[0]);
    }
}

// Partition enumerator
// xe->cur is the current partition number, before the first call to xd_enum,
// it should be set to 0xFF
// xe->nxt is the target partition number, if it equals 0xFF, it means enumerate
// all partitions, otherwise, it means jump to the specific partition.
int xd_enum(int hd,xde_t* xe)
{
  int nn=512,kk=1,cc;

  for (cc=xe->cur;;)
    {
      if (cc==0xFF)
        {
          unsigned long pt[4][2];
          int i,j,np;

          if (go_sect(hd,0))
            return 1;
          if (read(hd,ebuf,nn)!=nn)
            return 1;
          if (get16(ebuf,0x1FE)!=0xAA55)
            return 1;
          np=0;
          for (i=0x1BE;i<0x1FE;i+=16)
            if (ebuf[i+4])
              {
                if ((pt[np][1]=get32(ebuf,i+12))==0)
                  return 1;
                pt[np++][0]=get32(ebuf,i+8);
              }
          if (np==0)
            return 1;
          // Sort partition table base on start address
          for (i=0;i<np-1;i++)
            {
              int k=i;
              for (j=i+1;j<np;j++)
                if (pt[k][0]>pt[j][0]) k=j;
              if (k!=i)
                {
                  unsigned long tt;

                  tt=pt[i][0];
                  pt[i][0]=pt[k][0];
                  pt[k][0]=tt;
                  tt=pt[i][1];
                  pt[i][1]=pt[k][1];
                  pt[k][1]=tt;
                }
            }
          // Should have space for MBR
          if (pt[0][0]==0)
            return 1;
          // Check for partition overlap
          for (i=0;i<np-1;i++)
            if (pt[i][0]+pt[i][1]>pt[i+1][0])
              return 1;
          cc=0;
        }
      else if (kk)
        cc++;
      if ((unsigned char)cc>xe->nxt)
        return 1;
      if (cc<4)
        {
          if (xe->nxt<4)
            {
              // Empty partition
              if (! ebuf[xe->nxt*16+4+0x1BE])
                return 1;
              xe->cur=xe->nxt;
              xe->dfs=ebuf[xe->nxt*16+4+0x1BE];
              xe->bse=get32(ebuf,xe->nxt*16+8+0x1BE);
              xe->len=get32(ebuf,xe->nxt*16+12+0x1BE);
              return 0;
            }
          else if (xe->nxt!=0xFF)
            cc=4;
          else while (cc<4)
            {
              if (ebuf[cc*16+4+0x1BE])
                {
                  xe->cur=cc;
                  xe->dfs=ebuf[cc*16+4+0x1BE];
                  xe->bse=get32(ebuf,cc*16+8+0x1BE);
                  xe->len=get32(ebuf,cc*16+12+0x1BE);
                  return 0;
                }
              cc++;
            }
        }
      if ((cc==4) && (kk))
        {
          int i;

          // Scan for extended partition
          for (i=0;i<4;i++)
            if ((ebuf[i*16+4+0x1BE]==5) || (ebuf[i*16+4+0x1BE]==0xF)) break;
          if (i==4)
            return 1;
          xe->ebs=xe->bse=get32(ebuf,i*16+8+0x1BE);
        }
      else
        {
          // Is end of extended partition chain ?
          if (((ebuf[4+0x1CE]!=0x5) && (ebuf[4+0x1CE]!=0xF)) ||
              (get32(ebuf,8+0x1CE)==0))
            return 1;
          xe->bse=xe->ebs+get32(ebuf,8+0x1CE);
        }
      {
        while (1)
          {
            if (go_sect(hd,xe->bse))
              return 1;

            if (read(hd,ebuf,nn)!=nn)
              return 1;

            if (get16(ebuf,0x1FE)!=0xAA55)
              return 1;

            if ((ebuf[4+0x1BE]==5) || (ebuf[4+0x1BE]==0xF))
	      {
		if (get32(ebuf,8+0x1BE)==0)
		  return 1;
		else
		  {
		    xe->bse=xe->ebs+get32(ebuf,8+0x1BE);
		    continue;
		  }
	      }
            break;
          }
        kk=(ebuf[4+0x1BE]!=0);
        if ((kk) && ((xe->nxt==0xFF) || (cc==xe->nxt)))
          {
            xe->cur=cc;
            xe->dfs=ebuf[4+0x1BE];
            xe->bse+=get32(ebuf,8+0x1BE);
            xe->len=get32(ebuf,12+0x1BE);
            return 0;
          }
      }
    }
}

#define EXT2_SUPER_MAGIC      0xEF53

int mbr_nhd, mbr_spt;

static void split_chs(unsigned char* chs,unsigned long* c,unsigned long* h,unsigned long* s)
{
  *h=chs[0];
  *s=(chs[1] & 0x3F)-1;
  *c=((unsigned long)(chs[1]>>6))*256+chs[2];
}

static int chk_chs(unsigned long nhd,unsigned long spt,unsigned long lba,unsigned char* chs)
{
  unsigned long c,h,s;

  split_chs(chs,&c,&h,&s);
  if (c==0x3FF)
    return ((nhd==h+1) && (spt==s+1));
  else
    return (c*nhd*spt+h*spt+s==lba);
}

static int chk_mbr(unsigned char* buf)
{
  unsigned long nhd,spt,a1,a2,c2,h2,s2;
  int i;

  i=0x1BE;
  while ((i<0x1FE) && (buf[i+4]==0))
    i+=16;
  if (i>=0x1FE)
    return 0;
  a1=get32(&buf[i],8);
  a2=a1+get32(&buf[i],12)-1;
  if (a1>=a2)
    return 0;
  split_chs(buf+i+5,&c2,&h2,&s2);
  if (c2==0x3FF)
    {
      nhd=h2+1;
      spt=s2+1;
      if (! chk_chs(nhd,spt,a1,buf+i+1))
        return 0;
    }
  else
    {
      unsigned long c1,h1,s1;
      long n1,n2;

      split_chs(buf+i+1,&c1,&h1,&s1);
      if ((c1==0x3FF) || (c1>c2))
        return 0;
      n1=(long)(c1*a2)-(long)(c2*a1)-(long)(c1*s2)+(long)(c2*s1);
      n2=(long)(c1*h2)-(long)(c2*h1);
      if (n2<0)
        {
          n2=-n2;
          n1=-n1;
        }
      if ((n2==0) || (n1<=0) || (n1 % n2))
        return 0;
      spt=(unsigned long)(n1/n2);
      if (c2)
        {
          n1=(long)a2-(long)s2-(long)(h2*spt);
          n2=(long)(c2*spt);
          if ((n2==0) || (n1<=0) || (n1 % n2))
            return 0;
          nhd=(unsigned long)(n1/n2);
        }
      else
        nhd=h2+1;
    }
  if ((nhd==0) || (nhd>255) || (spt==0) || (spt>63))
    return 0;
  i+=16;
  while (i<0x1FE)
    {
      if (buf[i+4])
        {
          if ((! chk_chs(nhd,spt,get32(&buf[i],8),buf+i+1)) ||
              (! chk_chs(nhd,spt,get32(&buf[i],8)+get32(&buf[i],12)-1,buf+i+5)))
            return 0;
        }
      i+=16;
    }
  mbr_nhd=(int)nhd;
  mbr_spt=(int)spt;
  return 1;
}

int get_fstype(unsigned char* buf)
{
  if (chk_mbr(buf))
    return FST_MBR;

  // The first sector of EXT2 might not contain the 0xAA55 signature
  if (get16(&buf[1024],56)==EXT2_SUPER_MAGIC)
    return FST_EXT2;
  if (get16(&buf[0],0x1FE)!=0xAA55)
    return FST_OTHER;
  if (! memcmp(&buf[0x36],"FAT",3))
    return ((buf[0x26]==0x28) || (buf[0x26]==0x29))?FST_FAT16:FST_OTHER;
  if (! memcmp(&buf[0x52],"FAT32",5))
    return ((buf[0x42]==0x28) || (buf[0x42]==0x29))?FST_FAT32:FST_OTHER;
  if (! memcmp(&buf[0x3],"NTFS",4))
    return ((buf[0]==0xEB) && (buf[1]==0x52))?FST_NTFS:FST_OTHER;
  return FST_OTHER;
}

const char* fst2str(int fs)
{
  switch (fs) {
  case FST_OTHER:
    return "Other";
  case FST_MBR:
    return "MBR";
  case FST_FAT16:
    return "FAT12/FAT16";
  case FST_FAT32:
    return "FAT32";
  case FST_NTFS:
    return "NTFS";
  case FST_EXT2:
    return "EXT2/EXT3";
  default:
    return "Unknown";
  }
}

typedef struct {
  int id;
  const char* str;
} fstab_t;

static fstab_t fstab[]= {
  {0x1,"FAT12"},
  {0x4,"FAT16"},
  {0x5,"Extended"},
  {0x6,"FAT16B"},
  {0x7,"NTFS"},
  {0xB,"FAT32"},
  {0xC,"FAT32X"},
  {0xE,"FAT16X"},
  {0xF,"ExtendedX"},
  {0x11,"(H)FAT12"},
  {0x14,"(H)FAT16"},
  {0x16,"(H)FAT16B"},
  {0x17,"(H)NTFS"},
  {0x1B,"(H)FAT32"},
  {0x1C,"(H)FAT32X"},
  {0x1E,"(H)FAT16X"},
  {0x82,"Swap"},
  {0x83,"Ext2"},
  {0xA5,"FBSD"},
  {0,"Other"}};

const char* dfs2str(int fs)
{
  int i;

  for (i=0;fstab[i].id;i++)
    if (fs==fstab[i].id)
      return fstab[i].str;
  return fstab[i].str;
}