1 /* GRUB compatibility header
2  *
3  * taken from filo and grub.
4  */
5 
6 /*
7  *  GRUB  --  GRand Unified Bootloader
8  *  Copyright (C) 1999,2000,2001,2003   Free Software Foundation, Inc.
9  *
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 2 of the License, or
13  *  (at your option) any later version.
14  *
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License
21  *  along with this program; if not, write to the Free Software
22  *  Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
23  *  MA 02110-1301, USA.
24  */
25 
26 /* This disables some portion of code */
27 #define STAGE1_5 1
28 
29 #if defined CONFIG_X86
30 /*
31  * ffz = Find First Zero in word. Undefined if no zero exists,
32  * so code should check against ~0UL first..
33  */
34 static __inline__ unsigned int
ffz(unsigned int word)35 ffz (unsigned int word)
36 {
37 	__asm__ ("bsfl %1,%0"
38 	  : "=r" (word)
39 	  : "r" (~word));
40 	return word;
41 }
42 
43 static __inline__ unsigned int
log2(unsigned int word)44 log2 (unsigned int word)
45 {
46 	__asm__ ("bsfl %1,%0"
47 	  : "=r" (word)
48 	  : "r" (word));
49 	return word;
50 }
51 
52 #elif defined (CONFIG_PPC)
53 static __inline__ unsigned long
__ilog2(unsigned long x)54  __ilog2(unsigned long x)
55 {
56 	unsigned long lz;
57 
58 	asm ("cntlzw %0,%1" : "=r" (lz) : "r" (x));
59 	return 31 - lz;
60 }
61 
62 static __inline__ unsigned long
ffz(unsigned long x)63 ffz(unsigned long x)
64 {
65 	if ((x = ~x) == 0)
66 		return 32;
67 
68 	return __ilog2(x & -x);
69 }
70 
71 #define log2(n) ffz(~(n))
72 
73 #else
74 
log2(unsigned int word)75 static __inline__ unsigned int log2(unsigned int word)
76 {
77 	/* assume 8 bits per byte. */
78 	unsigned int i = 1 << (sizeof(word)*8 - 1);
79 	unsigned int pow = sizeof(word) * 8 - 1;
80 
81 	if (! word) {
82 		/* invalid parameter */
83 		return -1;
84 	}
85 	for(; i > word; i >>= 1, pow--) ;
86 
87         return pow;
88 }
89 
90 #define ffz(n) log2(~(n))
91 
92 #endif
93 
94 static inline int
substring(const char * s1,const char * s2)95 substring (const char *s1, const char *s2)
96 {
97   while (*s1 == *s2)
98     {
99       /* The strings match exactly. */
100       if (! *(s1++))
101 	return 0;
102       s2 ++;
103     }
104 
105   /* S1 is a substring of S2. */
106   if (*s1 == 0)
107     return -1;
108 
109   /* S1 isn't a substring. */
110   return 1;
111 }
112 
113 #define grub_memmove memmove
114 #define grub_strcmp strcmp
115 
116 #define MAXINT 0x7fffffff
117 
118 /* This is only used by fsys_* to determine if it's hard disk. If it is,
119  * they try to guess filesystem type by partition type. I guess it is
120  * not necessory, so hardcoded to 0 (first floppy) --ts1 */
121 #define current_drive 0
122 #define current_slice 0
123 #define current_partition 0
124 
125 /* we fake this for now, assuming that the filesystem is not corrupt */
126 #define part_length -1
127 extern int filepos;
128 extern int filemax;
129 extern int fsmax;
130 
131 /* Error codes (descriptions are in common.c) */
132 typedef enum
133 {
134   ERR_NONE = 0,
135   ERR_BAD_FILENAME,
136   ERR_BAD_FILETYPE,
137   ERR_BAD_GZIP_DATA,
138   ERR_BAD_GZIP_HEADER,
139   ERR_BAD_PART_TABLE,
140   ERR_BAD_VERSION,
141   ERR_BELOW_1MB,
142   ERR_BOOT_COMMAND,
143   ERR_BOOT_FAILURE,
144   ERR_BOOT_FEATURES,
145   ERR_DEV_FORMAT,
146   ERR_DEV_VALUES,
147   ERR_EXEC_FORMAT,
148   ERR_FILELENGTH,
149   ERR_FILE_NOT_FOUND,
150   ERR_FSYS_CORRUPT,
151   ERR_FSYS_MOUNT,
152   ERR_GEOM,
153   ERR_NEED_LX_KERNEL,
154   ERR_NEED_MB_KERNEL,
155   ERR_NO_DISK,
156   ERR_NO_PART,
157   ERR_NUMBER_PARSING,
158   ERR_OUTSIDE_PART,
159   ERR_READ,
160   ERR_SYMLINK_LOOP,
161   ERR_UNRECOGNIZED,
162   ERR_WONT_FIT,
163   ERR_WRITE,
164   ERR_BAD_ARGUMENT,
165   ERR_UNALIGNED,
166   ERR_PRIVILEGED,
167   ERR_DEV_NEED_INIT,
168   ERR_NO_DISK_SPACE,
169   ERR_NUMBER_OVERFLOW,
170 
171   MAX_ERR_NUM
172 } grub_error_t;
173 
174 extern grub_error_t errnum;
175 
176 #define grub_open file_open
177 #define grub_read file_read
178 #define grub_seek file_seek
179 #define grub_close file_close
180 
181 /* instrumentation variables */
182 /* (Not used in FILO) */
183 extern void (*disk_read_hook) (int, int, int);
184 extern void (*disk_read_func) (int, int, int);
185 
186 #define FSYS_BUFLEN 0x8000
187 extern char FSYS_BUF[FSYS_BUFLEN];
188 
189 #define print_possibilities 0
190 
191 #define SECTOR_SIZE 512
192 #define SECTOR_BITS 9
193 
194 #ifdef CONFIG_FSYS_FAT
195 int fat_mount (void);
196 int fat_read (char *buf, int len);
197 int fat_dir (char *dirname);
198 #endif
199 
200 #ifdef CONFIG_FSYS_EXT2FS
201 int ext2fs_mount (void);
202 int ext2fs_read (char *buf, int len);
203 int ext2fs_dir (char *dirname);
204 #endif
205 
206 #ifdef CONFIG_FSYS_MINIX
207 int minix_mount (void);
208 int minix_read (char *buf, int len);
209 int minix_dir (char *dirname);
210 #endif
211 
212 #ifdef CONFIG_FSYS_REISERFS
213 int reiserfs_mount (void);
214 int reiserfs_read (char *buf, int len);
215 int reiserfs_dir (char *dirname);
216 int reiserfs_embed (int *start_sector, int needed_sectors);
217 #endif
218 
219 #ifdef CONFIG_FSYS_JFS
220 int jfs_mount (void);
221 int jfs_read (char *buf, int len);
222 int jfs_dir (char *dirname);
223 int jfs_embed (int *start_sector, int needed_sectors);
224 #endif
225 
226 #ifdef CONFIG_FSYS_XFS
227 int xfs_mount (void);
228 int xfs_read (char *buf, int len);
229 int xfs_dir (char *dirname);
230 #endif
231 
232 #ifdef CONFIG_FSYS_UFS
233 int ufs_mount (void);
234 int ufs_read (char *buf, int len);
235 int ufs_dir (char *dirname);
236 int ufs_embed (int *start_sector, int needed_sectors);
237 #endif
238 
239 #ifdef CONFIG_FSYS_ISO9660
240 int iso9660_mount (void);
241 int iso9660_read (char *buf, int len);
242 int iso9660_dir (char *dirname);
243 #endif
244 
245 /* This is not a flag actually, but used as if it were a flag.  */
246 #define PC_SLICE_TYPE_HIDDEN_FLAG	0x10
247 
248 #define PC_SLICE_TYPE_NONE         	0
249 #define PC_SLICE_TYPE_FAT12        	1
250 #define PC_SLICE_TYPE_FAT16_LT32M  	4
251 #define PC_SLICE_TYPE_EXTENDED     	5
252 #define PC_SLICE_TYPE_FAT16_GT32M  	6
253 #define PC_SLICE_TYPE_FAT32		0xb
254 #define PC_SLICE_TYPE_FAT32_LBA		0xc
255 #define PC_SLICE_TYPE_FAT16_LBA		0xe
256 #define PC_SLICE_TYPE_WIN95_EXTENDED	0xf
257 #define PC_SLICE_TYPE_EZD        	0x55
258 #define PC_SLICE_TYPE_MINIX		0x80
259 #define PC_SLICE_TYPE_LINUX_MINIX	0x81
260 #define PC_SLICE_TYPE_EXT2FS       	0x83
261 #define PC_SLICE_TYPE_LINUX_EXTENDED	0x85
262 #define PC_SLICE_TYPE_VSTAFS		0x9e
263 #define PC_SLICE_TYPE_DELL_UTIL		0xde
264 #define PC_SLICE_TYPE_LINUX_RAID	0xfd
265 
266 /* For convinience.  */
267 /* Check if TYPE is a FAT partition type. Clear the hidden flag before
268    the check, to allow the user to mount a hidden partition in GRUB.  */
269 #define IS_PC_SLICE_TYPE_FAT(type)	\
270   ({ int _type = (type) & ~PC_SLICE_TYPE_HIDDEN_FLAG; \
271      _type == PC_SLICE_TYPE_FAT12 \
272      || _type == PC_SLICE_TYPE_FAT16_LT32M \
273      || _type == PC_SLICE_TYPE_FAT16_GT32M \
274      || _type == PC_SLICE_TYPE_FAT16_LBA \
275      || _type == PC_SLICE_TYPE_FAT32 \
276      || _type == PC_SLICE_TYPE_FAT32_LBA \
277      || _type == PC_SLICE_TYPE_DELL_UTIL; })
278 
279 #define IS_PC_SLICE_TYPE_MINIX(type) \
280   (((type) == PC_SLICE_TYPE_MINIX)	\
281    || ((type) == PC_SLICE_TYPE_LINUX_MINIX))
282 
283 #define IS_PC_SLICE_TYPE_BSD_WITH_FS(type,fs) 0
284 
285 /* possible values for the *BSD-style partition type */
286 #define	FS_UNUSED	0	/* unused */
287 #define	FS_SWAP		1	/* swap */
288 #define	FS_V6		2	/* Sixth Edition */
289 #define	FS_V7		3	/* Seventh Edition */
290 #define	FS_SYSV		4	/* System V */
291 #define	FS_V71K		5	/* V7 with 1K blocks (4.1, 2.9) */
292 #define	FS_V8		6	/* Eighth Edition, 4K blocks */
293 #define	FS_BSDFFS	7	/* 4.2BSD fast file system */
294 #define	FS_MSDOS	8	/* MSDOS file system */
295 #define	FS_BSDLFS	9	/* 4.4BSD log-structured file system */
296 #define	FS_OTHER	10	/* in use, but unknown/unsupported */
297 #define	FS_HPFS		11	/* OS/2 high-performance file system */
298 #define	FS_ISO9660	12	/* ISO 9660, normally CD-ROM */
299 #define	FS_BOOT		13	/* partition contains bootstrap */
300 #define	FS_ADOS		14	/* AmigaDOS fast file system */
301 #define	FS_HFS		15	/* Macintosh HFS */
302 #define	FS_FILECORE	16	/* Acorn Filecore Filing System */
303 #define	FS_EXT2FS	17	/* Linux Extended 2 file system */
304