9 #include "include/fs.h"
11 typedef int (*dfunc
)(int);
14 get_le_short(void *from
)
16 unsigned char *p
= from
;
17 return ((unsigned short)(p
[1]) << 8) +
21 static unsigned int get_le_long(void *from
)
23 unsigned char *p
= from
;
24 return ((unsigned int)(p
[3]) << 24) +
25 ((unsigned int)(p
[2]) << 16) +
26 ((unsigned int)(p
[1]) << 8) +
30 static unsigned short get_be_short(void *from
)
32 unsigned char *p
= from
;
33 return ((unsigned short)(p
[0]) << 8) +
37 static unsigned int get_be_long(void *from
)
39 unsigned char *p
= from
;
40 return ((unsigned int)(p
[0]) << 24) +
41 ((unsigned int)(p
[1]) << 16) +
42 ((unsigned int)(p
[2]) << 8) +
46 static int get_buffer(int fd
, unsigned char *b
, int offset
, int len
)
48 if(lseek(fd
, offset
, SEEK_SET
) != offset
)
50 if(read(fd
, b
, len
) != len
)
55 #define MBR_BUF_SIZE 512
56 static int detect_mbr(int fd
)
59 unsigned char *buffer
= (unsigned char*)malloc(MBR_BUF_SIZE
);
60 if(get_buffer(fd
, buffer
, 0, MBR_BUF_SIZE
) != 0)
62 if((buffer
[510] == 0x55) && (buffer
[511] == 0xAA))
69 #define EFI_BUF_OFFSET 512
70 #define EFI_BUF_SIZE 512
71 static int detect_efi(int fd
)
74 unsigned char *buffer
= (unsigned char*)malloc(EFI_BUF_SIZE
);
75 if(get_buffer(fd
, buffer
, EFI_BUF_OFFSET
, EFI_BUF_SIZE
) != 0)
77 if(!memcmp(buffer
, "EFI PART", 8))
84 #define EXT2_BUF_SIZE 1024
85 static int detect_ext23(int fd
)
88 unsigned char *buffer
= (unsigned char*)malloc(EXT2_BUF_SIZE
);
89 if(get_buffer(fd
, buffer
, 1024, EXT2_BUF_SIZE
) != 0)
91 if(get_le_short(buffer
+ 56) == 0xEF53)
93 if(get_le_long(buffer
+ 92) & 0x0004)
95 if ((get_le_long(buffer
+ 96) < 0x0000040)
96 && (get_le_long(buffer
+ 100) < 0x0000008))
109 #define FAT_BUF_SIZE 512
110 static int detect_fat(int fd
)
113 unsigned char *buffer
= (unsigned char*)malloc(FAT_BUF_SIZE
);
114 if(get_buffer(fd
, buffer
, 0, FAT_BUF_SIZE
) != 0)
117 if (((((buffer
[0] & 0xff) == 0xEB) && ((buffer
[2] & 0xff) == 0x90)) || ((buffer
[0] & 0xff) == 0xE9))
118 && ((buffer
[510] & 0xff) == 0x55) /*&& ((buffer[511] & 0xff == 0xAA))*/
119 && (memcmp(buffer
+ 3, "NTFS ", 8)))
126 static int detect_exfat(int fd
)
129 unsigned char *buffer
= (unsigned char*)malloc(FAT_BUF_SIZE
);
130 if(get_buffer(fd
, buffer
, 0, FAT_BUF_SIZE
) != 0)
133 if (!memcmp(buffer
+ 3, "EXFAT ", 7))
140 #define HFSPLUS_VOL_JOURNALED (1 << 13)
141 #define HFSPLUS_BUF_SIZE 512
142 static int detect_hfsplus(int fd
)
145 unsigned short magic
;
146 unsigned int journal
;
147 unsigned char *buffer
= (unsigned char*)malloc(HFSPLUS_BUF_SIZE
);
148 if(get_buffer(fd
, buffer
, 1024, HFSPLUS_BUF_SIZE
) != 0)
150 magic
= get_be_short(buffer
);
151 journal
= get_be_long(buffer
+ 4) & HFSPLUS_VOL_JOURNALED
;
157 ret
= HFSPLUSJOURNAL
;
164 #define NTFS_BUF_SIZE 512
165 static int detect_ntfs(int fd
)
168 unsigned char *buffer
= (unsigned char*)malloc(NTFS_BUF_SIZE
);
169 if(get_buffer(fd
, buffer
, 0, NTFS_BUF_SIZE
) != 0)
171 if(!memcmp(buffer
+ 3, "NTFS ", 8))
178 #define EXTENDED_BUF_SIZE 512
179 static int detect_extended(int fd
)
182 unsigned char *buffer
= (unsigned char*)malloc(MBR_BUF_SIZE
);
183 if(get_buffer(fd
, buffer
, 0, 512) != 0)
185 if((((buffer
[0] & 0xff) == 0xEB) && ((buffer
[2] & 0xff) == 0x90)) || ((buffer
[0] & 0xff) == 0xE9))
187 if(((buffer
[510] & 0xff) == 0x55) && ((buffer
[511] & 0xff) == 0xAA))
194 static dfunc funcs
[] = {
205 int detect_fs(char *device
)
211 fd
= open(device
, O_RDONLY
);
215 while((i
< 6) && (ret
== NONE
))
216 ret
= funcs
[i
++](fd
);