10 #include "include/fs.h"
12 typedef int (*dfunc)(int);
15 get_le_short(void *from)
17 unsigned char *p = from;
18 return ((unsigned short)(p[1]) << 8) +
22 unsigned int get_le_long(void *from)
24 unsigned char *p = from;
25 return ((unsigned int)(p[3]) << 24) +
26 ((unsigned int)(p[2]) << 16) +
27 ((unsigned int)(p[1]) << 8) +
31 unsigned short get_be_short(void *from)
33 unsigned char *p = from;
34 return ((unsigned short)(p[0]) << 8) +
38 unsigned int get_be_long(void *from)
40 unsigned char *p = from;
41 return ((unsigned int)(p[0]) << 24) +
42 ((unsigned int)(p[1]) << 16) +
43 ((unsigned int)(p[2]) << 8) +
47 int get_buffer(int fd, unsigned char *b, int offset, int len)
49 if(lseek(fd, offset, SEEK_SET) != offset)
51 if(read(fd, b, len) != len)
56 #define MBR_BUF_SIZE 512
57 int detect_mbr(int fd)
60 unsigned char *buffer = (unsigned char*)malloc(MBR_BUF_SIZE);
61 if(get_buffer(fd, buffer, 0, MBR_BUF_SIZE) != 0)
63 if((buffer[510] == 0x55) && (buffer[511] == 0xAA))
70 #define EFI_BUF_OFFSET 512
71 #define EFI_BUF_SIZE 512
72 int detect_efi(int fd)
75 unsigned char *buffer = (unsigned char*)malloc(EFI_BUF_SIZE);
76 if(get_buffer(fd, buffer, EFI_BUF_OFFSET, EFI_BUF_SIZE) != 0)
78 if(!memcmp(buffer, "EFI PART", 8))
85 #define EXT2_BUF_SIZE 1024
86 int detect_ext23(int fd)
89 unsigned char *buffer = (unsigned char*)malloc(EXT2_BUF_SIZE);
90 if(get_buffer(fd, buffer, 1024, EXT2_BUF_SIZE) != 0)
92 if(get_le_short(buffer + 56) == 0xEF53)
94 if((get_le_long(buffer + 96) & 0x0008)
95 || (get_le_long(buffer + 92) & 0x0004))
105 #define FAT_BUF_SIZE 512
106 int detect_fat(int fd)
109 unsigned char *buffer = (unsigned char*)malloc(FAT_BUF_SIZE);
110 if(get_buffer(fd, buffer, 0, FAT_BUF_SIZE) != 0)
113 if (((((buffer[0] & 0xff) == 0xEB) && ((buffer[2] & 0xff) == 0x90)) || ((buffer[0] & 0xff) == 0xE9))
114 && ((buffer[510] & 0xff) == 0x55) /*&& ((buffer[511] & 0xff == 0xAA))*/
115 && (memcmp(buffer + 3, "NTFS ", 8)))
122 #define HFSPLUS_VOL_JOURNALED (1 << 13)
123 #define HFSPLUS_BUF_SIZE 512
124 int detect_hfsplus(int fd)
127 unsigned short magic;
128 unsigned int journal;
129 unsigned char *buffer = (unsigned char*)malloc(HFSPLUS_BUF_SIZE);
130 if(get_buffer(fd, buffer, 1024, HFSPLUS_BUF_SIZE) != 0)
132 magic = get_be_short(buffer);
133 journal = get_be_long(buffer + 4) & HFSPLUS_VOL_JOURNALED;
139 // ret = HFSPLUSJOURNAL;
146 #define NTFS_BUF_SIZE 512
147 int detect_ntfs(int fd)
150 unsigned char *buffer = (unsigned char*)malloc(NTFS_BUF_SIZE);
151 if(get_buffer(fd, buffer, 0, NTFS_BUF_SIZE) != 0)
153 if(!memcmp(buffer + 3, "NTFS ", 8))
160 #define EXTENDED_BUF_SIZE 512
161 int detect_extended(int fd)
164 unsigned char *buffer = (unsigned char*)malloc(MBR_BUF_SIZE);
165 if(get_buffer(fd, buffer, 0, 512) != 0)
167 if((((buffer[0] & 0xff) == 0xEB) && ((buffer[2] & 0xff) == 0x90)) || ((buffer[0] & 0xff) == 0xE9))
169 if(((buffer[510] & 0xff) == 0x55) && ((buffer[511] & 0xff) == 0xAA))
186 int detect_fs(char *device)
192 fd = open(device, O_RDONLY);
196 while((i < 6) && (ret == NONE))
197 ret = funcs[i++](fd);