1 /* shared.h - definitions used in all GRUB-specific code */
2 /*
3  *  GRUB  --  GRand Unified Bootloader
4  *  Copyright (C) 1999,2000,2001,2002,2003,2004  Free Software Foundation, Inc.
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */
20 /*
21  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
22  * Use is subject to license terms.
23  */
24 
25 /*
26  *  Generic defines to use anywhere
27  */
28 
29 #ifndef GRUB_SHARED_HEADER
30 #define GRUB_SHARED_HEADER	1
31 
32 #include <config.h>
33 
34 /* Add an underscore to a C symbol in assembler code if needed. */
35 #ifdef HAVE_ASM_USCORE
36 # define EXT_C(sym) _ ## sym
37 #else
38 # define EXT_C(sym) sym
39 #endif
40 
41 /* Maybe redirect memory requests through grub_scratch_mem. */
42 #ifdef GRUB_UTIL
43 extern char *grub_scratch_mem;
44 # define RAW_ADDR(x) ((x) + (int) grub_scratch_mem)
45 # define RAW_SEG(x) (RAW_ADDR ((x) << 4) >> 4)
46 #else
47 # define RAW_ADDR(x) (x)
48 # define RAW_SEG(x) (x)
49 #endif
50 
51 /* ZFS will use the top 4 Meg of physical memory (below 4Gig) for sratch */
52 #define ZFS_SCRATCH_SIZE 0x400000
53 
54 #ifndef MAXPATHLEN
55 #define	MAXPATHLEN	1024
56 #endif
57 
58 #define	MAXNAMELEN	256
59 #define MIN(x, y) ((x) < (y) ? (x) : (y))
60 
61 /* Boot signature related defines for the findroot command */
62 #define	BOOTSIGN_DIR	"/boot/grub/bootsign"
63 #define	BOOTSIGN_ARGLEN	(MAXNAMELEN + 10)	/* (<sign>,0,d) */
64 #define	BOOTSIGN_LEN	(sizeof (BOOTSIGN_DIR) + 1 + BOOTSIGN_ARGLEN)
65 #define	BOOTSIGN_BACKUP	"/etc/bootsign"
66 
67 /*
68  *  Integer sizes
69  */
70 
71 #define MAXINT     0x7FFFFFFF
72 
73 /* Maximum command line size. Before you blindly increase this value,
74    see the comment in char_io.c (get_cmdline).  */
75 #define MAX_CMDLINE 1600
76 #define NEW_HEAPSIZE 1500
77 
78 /* 512-byte scratch area */
79 #define SCRATCHADDR  RAW_ADDR (0x77e00)
80 #define SCRATCHSEG   RAW_SEG (0x77e0)
81 
82 /*
83  *  This is the location of the raw device buffer.  It is 31.5K
84  *  in size.
85  */
86 
87 #define BUFFERLEN   0x7e00
88 #define BUFFERADDR  RAW_ADDR (0x70000)
89 #define BUFFERSEG   RAW_SEG (0x7000)
90 
91 #define BOOT_PART_TABLE	RAW_ADDR (0x07be)
92 
93 /*
94  *  BIOS disk defines
95  */
96 #define BIOSDISK_READ			0x0
97 #define BIOSDISK_WRITE			0x1
98 #define BIOSDISK_ERROR_GEOMETRY		0x100
99 #define BIOSDISK_ERROR_SHORT_IO		0x101
100 #define BIOSDISK_FLAG_LBA_EXTENSION	0x1
101 #define BIOSDISK_FLAG_CDROM		0x2
102 
103 /*
104  *  This is the filesystem (not raw device) buffer.
105  *  It is 32K in size, do not overrun!
106  */
107 
108 #define FSYS_BUFLEN  0x8000
109 #define FSYS_BUF RAW_ADDR (0x68000)
110 
111 /* Command-line buffer for Multiboot kernels and modules. This area
112    includes the area into which Stage 1.5 and Stage 1 are loaded, but
113    that's no problem.  */
114 #define MB_CMDLINE_BUF		RAW_ADDR (0x2000)
115 #define MB_CMDLINE_BUFLEN	0x6000
116 
117 /* The buffer for the password.  */
118 #define PASSWORD_BUF		RAW_ADDR (0x78000)
119 #define PASSWORD_BUFLEN		0x200
120 
121 /* THe buffer for the filename of "/boot/grub/default".  */
122 #define DEFAULT_FILE_BUF	(PASSWORD_BUF + PASSWORD_BUFLEN)
123 #define DEFAULT_FILE_BUFLEN	0x60
124 
125 /* The buffer for the command-line.  */
126 #define CMDLINE_BUF		(DEFAULT_FILE_BUF + DEFAULT_FILE_BUFLEN)
127 #define CMDLINE_BUFLEN		MAX_CMDLINE
128 
129 /* The kill buffer for the command-line.  */
130 #define KILL_BUF		(CMDLINE_BUF + CMDLINE_BUFLEN)
131 #define KILL_BUFLEN		MAX_CMDLINE
132 
133 /* The history buffer for the command-line.  */
134 #define HISTORY_BUF		(KILL_BUF + KILL_BUFLEN)
135 #define HISTORY_SIZE		5
136 #define HISTORY_BUFLEN		(MAX_CMDLINE * HISTORY_SIZE)
137 
138 /* The buffer for the completion.  */
139 #define COMPLETION_BUF		(HISTORY_BUF + HISTORY_BUFLEN)
140 #define COMPLETION_BUFLEN	MAX_CMDLINE
141 
142 /* The buffer for the unique string.  */
143 #define UNIQUE_BUF		(COMPLETION_BUF + COMPLETION_BUFLEN)
144 #define UNIQUE_BUFLEN		MAX_CMDLINE
145 
146 /* The buffer for the menu entries.  */
147 #define MENU_BUF		(UNIQUE_BUF + UNIQUE_BUFLEN)
148 #define MENU_BUFLEN		(0x8000 + PASSWORD_BUF - MENU_BUF)
149 
150 /* The size of the drive map.  */
151 #define DRIVE_MAP_SIZE		8
152 
153 /* The size of the key map.  */
154 #define KEY_MAP_SIZE		128
155 
156 /* The size of the io map.  */
157 #define IO_MAP_SIZE		128
158 
159 /*
160  *  Linux setup parameters
161  */
162 
163 #define LINUX_MAGIC_SIGNATURE		0x53726448	/* "HdrS" */
164 #define LINUX_DEFAULT_SETUP_SECTS	4
165 #define LINUX_FLAG_CAN_USE_HEAP		0x80
166 #define LINUX_INITRD_MAX_ADDRESS	0x38000000
167 #define LINUX_MAX_SETUP_SECTS		64
168 #define LINUX_BOOT_LOADER_TYPE		0x71
169 #define LINUX_HEAP_END_OFFSET		(0x9000 - 0x200)
170 
171 #define LINUX_BZIMAGE_ADDR		RAW_ADDR (0x100000)
172 #define LINUX_ZIMAGE_ADDR		RAW_ADDR (0x10000)
173 #define LINUX_OLD_REAL_MODE_ADDR	RAW_ADDR (0x90000)
174 #define LINUX_SETUP_STACK		0x9000
175 
176 #define LINUX_FLAG_BIG_KERNEL		0x1
177 
178 /* Linux's video mode selection support. Actually I hate it!  */
179 #define LINUX_VID_MODE_NORMAL		0xFFFF
180 #define LINUX_VID_MODE_EXTENDED		0xFFFE
181 #define LINUX_VID_MODE_ASK		0xFFFD
182 
183 #define LINUX_CL_OFFSET			0x9000
184 #define LINUX_CL_END_OFFSET		0x90FF
185 #define LINUX_SETUP_MOVE_SIZE		0x9100
186 #define LINUX_CL_MAGIC			0xA33F
187 
188 /*
189  *  General disk stuff
190  */
191 
192 #define SECTOR_SIZE		0x200
193 #define SECTOR_BITS		9
194 #define BIOS_FLAG_FIXED_DISK	0x80
195 
196 #define BOOTSEC_LOCATION		RAW_ADDR (0x7C00)
197 #define BOOTSEC_SIGNATURE		0xAA55
198 #define BOOTSEC_BPB_OFFSET		0x3
199 #define BOOTSEC_BPB_LENGTH		0x3B
200 #define BOOTSEC_BPB_SYSTEM_ID		0x3
201 #define BOOTSEC_BPB_HIDDEN_SECTORS	0x1C
202 #define BOOTSEC_PART_OFFSET		0x1BE
203 #define BOOTSEC_PART_LENGTH		0x40
204 #define BOOTSEC_SIG_OFFSET		0x1FE
205 #define BOOTSEC_LISTSIZE		8
206 
207 /* Not bad, perhaps.  */
208 #define NETWORK_DRIVE	0x20
209 
210 /*
211  *  GRUB specific information
212  *    (in LSB order)
213  */
214 
215 #include <stage1.h>
216 
217 #define STAGE2_VER_MAJ_OFFS	0x6
218 #define STAGE2_INSTALLPART	0x8
219 #define STAGE2_SAVED_ENTRYNO	0xc
220 #define STAGE2_STAGE2_ID	0x10
221 #define STAGE2_FORCE_LBA	0x11
222 #define STAGE2_VER_STR_OFFS	0x12
223 
224 /* Stage 2 identifiers */
225 #define STAGE2_ID_STAGE2		0
226 #define STAGE2_ID_FFS_STAGE1_5		1
227 #define STAGE2_ID_E2FS_STAGE1_5		2
228 #define STAGE2_ID_FAT_STAGE1_5		3
229 #define STAGE2_ID_MINIX_STAGE1_5	4
230 #define STAGE2_ID_REISERFS_STAGE1_5	5
231 #define STAGE2_ID_VSTAFS_STAGE1_5	6
232 #define STAGE2_ID_JFS_STAGE1_5		7
233 #define STAGE2_ID_XFS_STAGE1_5		8
234 #define STAGE2_ID_ISO9660_STAGE1_5	9
235 #define STAGE2_ID_UFS2_STAGE1_5		10
236 #define STAGE2_ID_UFS_STAGE1_5		11
237 #define STAGE2_ID_ZFS_STAGE1_5		12
238 
239 #ifndef STAGE1_5
240 # define STAGE2_ID	STAGE2_ID_STAGE2
241 #else
242 # if defined(FSYS_FFS)
243 #  define STAGE2_ID	STAGE2_ID_FFS_STAGE1_5
244 # elif defined(FSYS_EXT2FS)
245 #  define STAGE2_ID	STAGE2_ID_E2FS_STAGE1_5
246 # elif defined(FSYS_FAT)
247 #  define STAGE2_ID	STAGE2_ID_FAT_STAGE1_5
248 # elif defined(FSYS_MINIX)
249 #  define STAGE2_ID	STAGE2_ID_MINIX_STAGE1_5
250 # elif defined(FSYS_REISERFS)
251 #  define STAGE2_ID	STAGE2_ID_REISERFS_STAGE1_5
252 # elif defined(FSYS_VSTAFS)
253 #  define STAGE2_ID	STAGE2_ID_VSTAFS_STAGE1_5
254 # elif defined(FSYS_JFS)
255 #  define STAGE2_ID	STAGE2_ID_JFS_STAGE1_5
256 # elif defined(FSYS_XFS)
257 #  define STAGE2_ID	STAGE2_ID_XFS_STAGE1_5
258 # elif defined(FSYS_ISO9660)
259 #  define STAGE2_ID	STAGE2_ID_ISO9660_STAGE1_5
260 # elif defined(FSYS_UFS2)
261 #  define STAGE2_ID	STAGE2_ID_UFS2_STAGE1_5
262 # elif defined(FSYS_UFS)
263 #  define STAGE2_ID	STAGE2_ID_UFS_STAGE1_5
264 # elif defined(FSYS_ZFS)
265 #  define STAGE2_ID	STAGE2_ID_ZFS_STAGE1_5
266 # else
267 #  error "unknown Stage 2"
268 # endif
269 #endif
270 
271 /*
272  *  defines for use when switching between real and protected mode
273  */
274 
275 #define CR0_PE_ON	0x1
276 #define CR0_PE_OFF	0xfffffffe
277 #define PROT_MODE_CSEG	0x8
278 #define PROT_MODE_DSEG  0x10
279 #define PSEUDO_RM_CSEG	0x18
280 #define PSEUDO_RM_DSEG	0x20
281 #define STACKOFF	(0x2000 - 0x10)
282 #define PROTSTACKINIT   (FSYS_BUF - 0x10)
283 
284 
285 /*
286  * Assembly code defines
287  *
288  * "EXT_C" is assumed to be defined in the Makefile by the configure
289  *   command.
290  */
291 
292 #define ENTRY(x) .globl EXT_C(x) ; EXT_C(x):
293 #define VARIABLE(x) ENTRY(x)
294 
295 
296 #define K_RDWR  	0x60	/* keyboard data & cmds (read/write) */
297 #define K_STATUS	0x64	/* keyboard status */
298 #define K_CMD		0x64	/* keybd ctlr command (write-only) */
299 
300 #define K_OBUF_FUL 	0x01	/* output buffer full */
301 #define K_IBUF_FUL 	0x02	/* input buffer full */
302 
303 #define KC_CMD_WIN	0xd0	/* read  output port */
304 #define KC_CMD_WOUT	0xd1	/* write output port */
305 #define KB_OUTPUT_MASK  0xdd	/* enable output buffer full interrupt
306 				   enable data line
307 				   enable clock line */
308 #define KB_A20_ENABLE   0x02
309 
310 /* Codes for getchar. */
311 #define ASCII_CHAR(x)   ((x) & 0xFF)
312 #if !defined(GRUB_UTIL) || !defined(HAVE_LIBCURSES)
313 # define KEY_LEFT        0x4B00
314 # define KEY_RIGHT       0x4D00
315 # define KEY_UP          0x4800
316 # define KEY_DOWN        0x5000
317 # define KEY_IC          0x5200	/* insert char */
318 # define KEY_DC          0x5300	/* delete char */
319 # define KEY_BACKSPACE   0x0008
320 # define KEY_HOME        0x4700
321 # define KEY_END         0x4F00
322 # define KEY_NPAGE       0x5100
323 # define KEY_PPAGE       0x4900
324 # define A_NORMAL        0x7
325 # define A_REVERSE       0x70
326 #elif defined(HAVE_NCURSES_CURSES_H)
327 # include <ncurses/curses.h>
328 #elif defined(HAVE_NCURSES_H)
329 # include <ncurses.h>
330 #elif defined(HAVE_CURSES_H)
331 # include <curses.h>
332 #endif
333 
334 /* In old BSD curses, A_NORMAL and A_REVERSE are not defined, so we
335    define them here if they are undefined.  */
336 #ifndef A_NORMAL
337 # define A_NORMAL	0
338 #endif /* ! A_NORMAL */
339 #ifndef A_REVERSE
340 # ifdef A_STANDOUT
341 #  define A_REVERSE	A_STANDOUT
342 # else /* ! A_STANDOUT */
343 #  define A_REVERSE	0
344 # endif /* ! A_STANDOUT */
345 #endif /* ! A_REVERSE */
346 
347 /* Define ACS_* ourselves, since the definitions are not consistent among
348    various curses implementations.  */
349 #undef ACS_ULCORNER
350 #undef ACS_URCORNER
351 #undef ACS_LLCORNER
352 #undef ACS_LRCORNER
353 #undef ACS_HLINE
354 #undef ACS_VLINE
355 #undef ACS_LARROW
356 #undef ACS_RARROW
357 #undef ACS_UARROW
358 #undef ACS_DARROW
359 
360 #define ACS_ULCORNER	'+'
361 #define ACS_URCORNER	'+'
362 #define ACS_LLCORNER	'+'
363 #define ACS_LRCORNER	'+'
364 #define ACS_HLINE	'-'
365 #define ACS_VLINE	'|'
366 #define ACS_LARROW	'<'
367 #define ACS_RARROW	'>'
368 #define ACS_UARROW	'^'
369 #define ACS_DARROW	'v'
370 
371 /* Special graphics characters for IBM displays. */
372 #define DISP_UL		218
373 #define DISP_UR		191
374 #define DISP_LL		192
375 #define DISP_LR		217
376 #define DISP_HORIZ	196
377 #define DISP_VERT	179
378 #define DISP_LEFT	0x1b
379 #define DISP_RIGHT	0x1a
380 #define DISP_UP		0x18
381 #define DISP_DOWN	0x19
382 
383 /* Remap some libc-API-compatible function names so that we prevent
384    circularararity. */
385 #ifndef WITHOUT_LIBC_STUBS
386 #define memmove grub_memmove
387 #define memcpy grub_memmove	/* we don't need a separate memcpy */
388 #define memset grub_memset
389 #undef isspace
390 #define isspace grub_isspace
391 #define printf grub_printf
392 #define sprintf grub_sprintf
393 #undef putchar
394 #define putchar grub_putchar
395 #define strncat grub_strncat
396 #define strstr grub_strstr
397 #define memcmp grub_memcmp
398 #define strcmp grub_strcmp
399 #define tolower grub_tolower
400 #define strlen grub_strlen
401 #define strcpy grub_strcpy
402 #endif /* WITHOUT_LIBC_STUBS */
403 
404 #define UNDI_STACK (512 + 64) << 10
405 #define UNDI_STACK_SEG (UNDI_STACK >> 4) /*  PXE load GRUB here */
406 #define UNDI_STACK_OFF (0x10000 - 0x10)
407 
408 #ifndef ASM_FILE
409 /*
410  *  Below this should be ONLY defines and other constructs for C code.
411  */
412 
413 /* multiboot stuff */
414 
415 #include "mb_header.h"
416 #include "mb_info.h"
417 
418 /* For the Linux/i386 boot protocol version 2.03.  */
419 struct linux_kernel_header
420 {
421   char code1[0x0020];
422   unsigned short cl_magic;		/* Magic number 0xA33F */
423   unsigned short cl_offset;		/* The offset of command line */
424   char code2[0x01F1 - 0x0020 - 2 - 2];
425   unsigned char setup_sects;		/* The size of the setup in sectors */
426   unsigned short root_flags;		/* If the root is mounted readonly */
427   unsigned short syssize;		/* obsolete */
428   unsigned short swap_dev;		/* obsolete */
429   unsigned short ram_size;		/* obsolete */
430   unsigned short vid_mode;		/* Video mode control */
431   unsigned short root_dev;		/* Default root device number */
432   unsigned short boot_flag;		/* 0xAA55 magic number */
433   unsigned short jump;			/* Jump instruction */
434   unsigned long header;			/* Magic signature "HdrS" */
435   unsigned short version;		/* Boot protocol version supported */
436   unsigned long realmode_swtch;		/* Boot loader hook */
437   unsigned long start_sys;		/* Points to kernel version string */
438   unsigned char type_of_loader;		/* Boot loader identifier */
439   unsigned char loadflags;		/* Boot protocol option flags */
440   unsigned short setup_move_size;	/* Move to high memory size */
441   unsigned long code32_start;		/* Boot loader hook */
442   unsigned long ramdisk_image;		/* initrd load address */
443   unsigned long ramdisk_size;		/* initrd size */
444   unsigned long bootsect_kludge;	/* obsolete */
445   unsigned short heap_end_ptr;		/* Free memory after setup end */
446   unsigned short pad1;			/* Unused */
447   char *cmd_line_ptr;			/* Points to the kernel command line */
448   unsigned long initrd_addr_max;	/* The highest address of initrd */
449 } __attribute__ ((packed));
450 
451 /* Memory map address range descriptor used by GET_MMAP_ENTRY. */
452 struct mmar_desc
453 {
454   unsigned long desc_len;	/* Size of this descriptor. */
455   unsigned long long addr;	/* Base address. */
456   unsigned long long length;	/* Length in bytes. */
457   unsigned long type;		/* Type of address range. */
458 } __attribute__ ((packed));
459 
460 /* VBE controller information.  */
461 struct vbe_controller
462 {
463   unsigned char signature[4];
464   unsigned short version;
465   unsigned long oem_string;
466   unsigned long capabilities;
467   unsigned long video_mode;
468   unsigned short total_memory;
469   unsigned short oem_software_rev;
470   unsigned long oem_vendor_name;
471   unsigned long oem_product_name;
472   unsigned long oem_product_rev;
473   unsigned char reserved[222];
474   unsigned char oem_data[256];
475 } __attribute__ ((packed));
476 
477 /* VBE mode information.  */
478 struct vbe_mode
479 {
480   unsigned short mode_attributes;
481   unsigned char win_a_attributes;
482   unsigned char win_b_attributes;
483   unsigned short win_granularity;
484   unsigned short win_size;
485   unsigned short win_a_segment;
486   unsigned short win_b_segment;
487   unsigned long win_func;
488   unsigned short bytes_per_scanline;
489 
490   /* >=1.2 */
491   unsigned short x_resolution;
492   unsigned short y_resolution;
493   unsigned char x_char_size;
494   unsigned char y_char_size;
495   unsigned char number_of_planes;
496   unsigned char bits_per_pixel;
497   unsigned char number_of_banks;
498   unsigned char memory_model;
499   unsigned char bank_size;
500   unsigned char number_of_image_pages;
501   unsigned char reserved0;
502 
503   /* direct color */
504   unsigned char red_mask_size;
505   unsigned char red_field_position;
506   unsigned char green_mask_size;
507   unsigned char green_field_position;
508   unsigned char blue_mask_size;
509   unsigned char blue_field_position;
510   unsigned char reserved_mask_size;
511   unsigned char reserved_field_position;
512   unsigned char direct_color_mode_info;
513 
514   /* >=2.0 */
515   unsigned long phys_base;
516   unsigned long reserved1;
517   unsigned short reversed2;
518 
519   /* >=3.0 */
520   unsigned short linear_bytes_per_scanline;
521   unsigned char banked_number_of_image_pages;
522   unsigned char linear_number_of_image_pages;
523   unsigned char linear_red_mask_size;
524   unsigned char linear_red_field_position;
525   unsigned char linear_green_mask_size;
526   unsigned char linear_green_field_position;
527   unsigned char linear_blue_mask_size;
528   unsigned char linear_blue_field_position;
529   unsigned char linear_reserved_mask_size;
530   unsigned char linear_reserved_field_position;
531   unsigned long max_pixel_clock;
532 
533   unsigned char reserved3[189];
534 } __attribute__ ((packed));
535 
536 
537 #undef NULL
538 #define NULL         ((void *) 0)
539 
540 /* Error codes (descriptions are in common.c) */
541 typedef enum
542 {
543   ERR_NONE = 0,
544   ERR_BAD_FILENAME,
545   ERR_BAD_FILETYPE,
546   ERR_BAD_GZIP_DATA,
547   ERR_BAD_GZIP_HEADER,
548   ERR_BAD_PART_TABLE,
549   ERR_BAD_VERSION,
550   ERR_BELOW_1MB,
551   ERR_BOOT_COMMAND,
552   ERR_BOOT_FAILURE,
553   ERR_BOOT_FEATURES,
554   ERR_DEV_FORMAT,
555   ERR_DEV_VALUES,
556   ERR_EXEC_FORMAT,
557   ERR_FILELENGTH,
558   ERR_FILE_NOT_FOUND,
559   ERR_FSYS_CORRUPT,
560   ERR_FSYS_MOUNT,
561   ERR_GEOM,
562   ERR_NEED_LX_KERNEL,
563   ERR_NEED_MB_KERNEL,
564   ERR_NO_DISK,
565   ERR_NO_PART,
566   ERR_NUMBER_PARSING,
567   ERR_OUTSIDE_PART,
568   ERR_READ,
569   ERR_SYMLINK_LOOP,
570   ERR_UNRECOGNIZED,
571   ERR_WONT_FIT,
572   ERR_WRITE,
573   ERR_BAD_ARGUMENT,
574   ERR_UNALIGNED,
575   ERR_PRIVILEGED,
576   ERR_DEV_NEED_INIT,
577   ERR_NO_DISK_SPACE,
578   ERR_NUMBER_OVERFLOW,
579   ERR_BAD_GZIP_CRC,
580   ERR_FILESYSTEM_NOT_FOUND,
581   ERR_NO_BOOTPATH,
582   ERR_NEWER_VERSION,
583   ERR_NOTXPM,
584   ERR_TOOMANYCOLORS,
585   ERR_CORRUPTXPM,
586 
587   MAX_ERR_NUM
588 } grub_error_t;
589 
590 extern unsigned long install_partition;
591 extern unsigned long boot_drive;
592 extern unsigned long install_second_sector;
593 extern struct apm_info apm_bios_info;
594 extern unsigned long boot_part_addr;
595 extern int saved_entryno;
596 extern unsigned char force_lba;
597 extern char version_string[];
598 extern char config_file[];
599 extern unsigned long linux_text_len;
600 extern char *linux_data_tmp_addr;
601 extern char *linux_data_real_addr;
602 
603 #ifdef GRUB_UTIL
604 /* If not using config file, this variable is set to zero,
605    otherwise non-zero.  */
606 extern int use_config_file;
607 /* If using the preset menu, this variable is set to non-zero,
608    otherwise zero.  */
609 extern int use_preset_menu;
610 /* If not using curses, this variable is set to zero, otherwise non-zero.  */
611 extern int use_curses;
612 /* The flag for verbose messages.  */
613 extern int verbose;
614 /* The flag for read-only.  */
615 extern int read_only;
616 /* The number of floppies to be probed.  */
617 extern int floppy_disks;
618 /* The map between BIOS drives and UNIX device file names.  */
619 extern char **device_map;
620 /* The filename which stores the information about a device map.  */
621 extern char *device_map_file;
622 /* The array of geometries.  */
623 extern struct geometry *disks;
624 /* Assign DRIVE to a device name DEVICE.  */
625 extern void assign_device_name (int drive, const char *device);
626 #endif
627 
628 #ifndef STAGE1_5
629 /* GUI interface variables. */
630 # define MAX_FALLBACK_ENTRIES	8
631 extern int fallback_entries[MAX_FALLBACK_ENTRIES];
632 extern int fallback_entryno;
633 extern int default_entry;
634 extern int current_entryno;
635 
636 /* The constants for password types.  */
637 typedef enum
638 {
639   PASSWORD_PLAIN,
640   PASSWORD_MD5,
641   PASSWORD_UNSUPPORTED
642 }
643 password_t;
644 
645 extern char *password;
646 extern password_t password_type;
647 extern int auth;
648 extern char commands[];
649 
650 /* For `more'-like feature.  */
651 extern int max_lines;
652 extern int count_lines;
653 extern int use_pager;
654 #endif
655 
656 #ifndef NO_DECOMPRESSION
657 extern int no_decompression;
658 extern int compressed_file;
659 #endif
660 
661 /* instrumentation variables */
662 extern void (*disk_read_hook) (unsigned int, int, int);
663 extern void (*disk_read_func) (unsigned int, int, int);
664 
665 #ifndef STAGE1_5
666 /* The flag for debug mode.  */
667 extern int debug;
668 #endif /* STAGE1_5 */
669 
670 extern unsigned long current_drive;
671 extern unsigned long current_partition;
672 extern char current_rootpool[MAXNAMELEN];
673 extern char current_bootfs[MAXNAMELEN];
674 extern unsigned long long current_bootfs_obj;
675 extern char current_bootpath[MAXPATHLEN];
676 extern char current_devid[MAXPATHLEN];
677 extern int is_zfs_mount;
678 extern unsigned long best_drive;
679 extern unsigned long best_part;
680 extern int find_best_root;
681 
682 extern int fsys_type;
683 
684 /* The information for a disk geometry. The CHS information is only for
685    DOS/Partition table compatibility, and the real number of sectors is
686    stored in TOTAL_SECTORS.  */
687 struct geometry
688 {
689   /* The number of cylinders */
690   unsigned long cylinders;
691   /* The number of heads */
692   unsigned long heads;
693   /* The number of sectors */
694   unsigned long sectors;
695   /* The total number of sectors */
696   unsigned long total_sectors;
697   /* Device sector size */
698   unsigned long sector_size;
699   /* Flags */
700   unsigned long flags;
701 };
702 
703 extern unsigned long part_start;
704 extern unsigned long part_length;
705 
706 extern int current_slice;
707 
708 extern int buf_drive;
709 #define BUF_CACHE_INVALID 0xffffffff
710 extern unsigned int buf_track;
711 extern struct geometry buf_geom;
712 
713 /* these are the current file position and maximum file position */
714 extern int filepos;
715 extern int filemax;
716 
717 /*
718  *  Common BIOS/boot data.
719  */
720 
721 extern struct multiboot_info mbi;
722 extern unsigned long saved_drive;
723 extern unsigned long saved_partition;
724 extern unsigned long cdrom_drive;
725 #ifndef STAGE1_5
726 #ifdef SOLARIS_NETBOOT
727 extern unsigned long dhcpack_length;
728 extern unsigned long dhcpack_buf;
729 #endif
730 extern unsigned long saved_mem_upper;
731 extern unsigned long extended_memory;
732 #endif
733 
734 /*
735  *  Error variables.
736  */
737 
738 extern grub_error_t errnum;
739 extern char *err_list[];
740 
741 /* don't print geeky noise */
742 typedef enum
743 {
744   SILENT,
745   VERBOSE,
746   DEFER_SILENT,
747   DEFER_VERBOSE
748 } silent_status;
749 
750 /* one screen worth of messages 80x24 = 1920 chars -- more with newlines */
751 #define	SCREENBUF 2000
752 
753 struct silentbuf {
754 	silent_status status;
755 	int looped;
756 	char buffer[SCREENBUF];
757 	char *buffer_start;
758 };
759 
760 extern struct silentbuf silent;
761 extern int reset_term;
762 
763 /* Simplify declaration of entry_addr. */
764 typedef void (*entry_func) (int, int, int, int, int, int)
765      __attribute__ ((noreturn));
766 
767 extern entry_func entry_addr;
768 
769 /* Enter the stage1.5/stage2 C code after the stack is set up. */
770 void cmain (void);
771 
772 /* Halt the processor (called after an unrecoverable error). */
773 void stop (void) __attribute__ ((noreturn));
774 
775 /* Reboot the system.  */
776 void grub_reboot (void) __attribute__ ((noreturn));
777 
778 /* Halt the system, using APM if possible. If NO_APM is true, don't use
779    APM even if it is available.  */
780 void grub_halt (int no_apm) __attribute__ ((noreturn));
781 
782 /* Copy MAP to the drive map and set up int13_handler.  */
783 void set_int13_handler (unsigned short *map);
784 
785 /* Set up int15_handler.  */
786 void set_int15_handler (void);
787 
788 /* Restore the original int15 handler.  */
789 void unset_int15_handler (void);
790 
791 /* Track the int13 handler to probe I/O address space.  */
792 void track_int13 (int drive);
793 
794 /* The key map.  */
795 extern unsigned short bios_key_map[];
796 extern unsigned short ascii_key_map[];
797 extern unsigned short io_map[];
798 
799 /* calls for direct boot-loader chaining */
800 void chain_stage1 (unsigned long segment, unsigned long offset,
801 		   unsigned long part_table_addr)
802      __attribute__ ((noreturn));
803 void chain_stage2 (unsigned long segment, unsigned long offset,
804 		   int second_sector)
805      __attribute__ ((noreturn));
806 
807 /* do some funky stuff, then boot linux */
808 void linux_boot (void) __attribute__ ((noreturn));
809 
810 /* do some funky stuff, then boot bzImage linux */
811 void big_linux_boot (void) __attribute__ ((noreturn));
812 
813 /* booting a multiboot executable */
814 void multi_boot (int start, int mb_info) __attribute__ ((noreturn));
815 
816 /* If LINEAR is nonzero, then set the Intel processor to linear mode.
817    Otherwise, bit 20 of all memory accesses is always forced to zero,
818    causing a wraparound effect for bugwards compatibility with the
819    8086 CPU. */
820 void gateA20 (int linear);
821 
822 /* memory probe routines */
823 int get_memsize (int type);
824 int get_eisamemsize (void);
825 
826 /* Fetch the next entry in the memory map and return the continuation
827    value.  DESC is a pointer to the descriptor buffer, and CONT is the
828    previous continuation value (0 to get the first entry in the
829    map). */
830 int get_mmap_entry (struct mmar_desc *desc, int cont);
831 
832 /* Get the linear address of a ROM configuration table. Return zero,
833    if fails.  */
834 unsigned long get_rom_config_table (void);
835 
836 /* Get APM BIOS information.  */
837 void get_apm_info (void);
838 
839 /* Get VBE controller information.  */
840 int get_vbe_controller_info (struct vbe_controller *controller);
841 
842 /* Get VBE mode information.  */
843 int get_vbe_mode_info (int mode_number, struct vbe_mode *mode);
844 
845 /* Set VBE mode.  */
846 int set_vbe_mode (int mode_number);
847 
848 /* Return the data area immediately following our code. */
849 int get_code_end (void);
850 
851 /* low-level timing info */
852 int getrtsecs (void);
853 int currticks (void);
854 
855 /* Clear the screen. */
856 void cls (void);
857 
858 /* Turn on/off cursor. */
859 int setcursor (int on);
860 
861 /* Get the current cursor position (where 0,0 is the top left hand
862    corner of the screen).  Returns packed values, (RET >> 8) is x,
863    (RET & 0xff) is y. */
864 int getxy (void);
865 
866 /* Set the cursor position. */
867 void gotoxy (int x, int y);
868 
869 /* Displays an ASCII character.  IBM displays will translate some
870    characters to special graphical ones (see the DISP_* constants). */
871 void grub_putchar (int c);
872 
873 /* Wait for a keypress, and return its packed BIOS/ASCII key code.
874    Use ASCII_CHAR(ret) to extract the ASCII code. */
875 int getkey (void);
876 
877 /* Like GETKEY, but doesn't block, and returns -1 if no keystroke is
878    available. */
879 int checkkey (void);
880 
881 /* Low-level disk I/O */
882 int get_diskinfo (int drive, struct geometry *geometry);
883 int biosdisk (int subfunc, int drive, struct geometry *geometry,
884     unsigned int sector, int nsec, int segment);
885 void stop_floppy (void);
886 
887 /* Command-line interface functions. */
888 #ifndef STAGE1_5
889 
890 /* The flags for the builtins.  */
891 #define BUILTIN_CMDLINE		0x1	/* Run in the command-line.  */
892 #define BUILTIN_MENU		0x2	/* Run in the menu.  */
893 #define BUILTIN_TITLE		0x4	/* Only for the command title.  */
894 #define BUILTIN_SCRIPT		0x8	/* Run in the script.  */
895 #define BUILTIN_NO_ECHO		0x10	/* Don't print command on booting. */
896 #define BUILTIN_HELP_LIST	0x20	/* Show help in listing.  */
897 
898 /* The table for a builtin.  */
899 struct builtin
900 {
901   /* The command name.  */
902   char *name;
903   /* The callback function.  */
904   int (*func) (char *, int);
905   /* The combination of the flags defined above.  */
906   int flags;
907   /* The short version of the documentation.  */
908   char *short_doc;
909   /* The long version of the documentation.  */
910   char *long_doc;
911 };
912 
913 /* All the builtins are registered in this.  */
914 extern struct builtin *builtin_table[];
915 
916 /* The constants for kernel types.  */
917 typedef enum
918 {
919   KERNEL_TYPE_NONE,		/* None is loaded.  */
920   KERNEL_TYPE_MULTIBOOT,	/* Multiboot.  */
921   KERNEL_TYPE_LINUX,		/* Linux.  */
922   KERNEL_TYPE_BIG_LINUX,	/* Big Linux.  */
923   KERNEL_TYPE_FREEBSD,		/* FreeBSD.  */
924   KERNEL_TYPE_NETBSD,		/* NetBSD.  */
925   KERNEL_TYPE_CHAINLOADER	/* Chainloader.  */
926 }
927 kernel_t;
928 
929 extern kernel_t kernel_type;
930 extern int show_menu;
931 extern int grub_timeout;
932 
933 void init_builtins (void);
934 void init_config (void);
935 char *skip_to (int after_equal, char *cmdline);
936 struct builtin *find_command (char *command);
937 void print_cmdline_message (int forever);
938 void enter_cmdline (char *heap, int forever);
939 int run_script (char *script, char *heap);
940 #endif
941 
942 /* C library replacement functions with identical semantics. */
943 void grub_printf (const char *format,...);
944 int grub_sprintf (char *buffer, const char *format, ...);
945 int grub_tolower (int c);
946 int grub_isspace (int c);
947 int grub_strncat (char *s1, const char *s2, int n);
948 void grub_memcpy(void *dest, const void *src, int len);
949 void *grub_memmove (void *to, const void *from, int len);
950 void *grub_memset (void *start, int c, int len);
951 int grub_strncat (char *s1, const char *s2, int n);
952 char *grub_strstr (const char *s1, const char *s2);
953 int grub_memcmp (const char *s1, const char *s2, int n);
954 int grub_strcmp (const char *s1, const char *s2);
955 int grub_strlen (const char *str);
956 char *grub_strcpy (char *dest, const char *src);
957 char *grub_strchr (char *str, char c);
958 
959 void noisy_printf (const char *format,...);
960 
961 #ifndef GRUB_UTIL
962 typedef unsigned long grub_jmp_buf[6];
963 #else
964 /* In the grub shell, use the libc jmp_buf instead.  */
965 # include <setjmp.h>
966 # define grub_jmp_buf jmp_buf
967 #endif
968 
969 #ifdef GRUB_UTIL
970 # define grub_setjmp	setjmp
971 # define grub_longjmp	longjmp
972 #else /* ! GRUB_UTIL */
973 int grub_setjmp (grub_jmp_buf env);
974 void grub_longjmp (grub_jmp_buf env, int val);
975 #endif /* ! GRUB_UTIL */
976 
977 /* The environment for restarting Stage 2.  */
978 extern grub_jmp_buf restart_env;
979 /* The environment for restarting the command-line interface.  */
980 extern grub_jmp_buf restart_cmdline_env;
981 
982 /* misc */
983 void init_page (void);
984 void print_error (void);
985 char *convert_to_ascii (char *buf, int c, ...);
986 int get_cmdline (char *prompt, char *cmdline, int maxlen,
987 		 int echo_char, int history);
988 int substring (const char *s1, const char *s2);
989 int nul_terminate (char *str);
990 int get_based_digit (int c, int base);
991 int safe_parse_maxint (char **str_ptr, int *myint_ptr);
992 int memcheck (unsigned long start, unsigned long len);
993 void grub_putstr (const char *str);
994 
995 #ifndef NO_DECOMPRESSION
996 /* Compression support. */
997 int gunzip_test_header (void);
998 int gunzip_read (char *buf, int len);
999 #endif /* NO_DECOMPRESSION */
1000 
1001 int rawread (int drive, unsigned int sector, int byte_offset, int byte_len,
1002 	char *buf);
1003 int devread (unsigned int sector, int byte_offset, int byte_len, char *buf);
1004 int rawwrite (int drive, unsigned int sector, char *buf);
1005 int devwrite (unsigned int sector, int sector_len, char *buf);
1006 
1007 /* Parse a device string and initialize the global parameters. */
1008 char *set_device (char *device);
1009 int open_device (void);
1010 int real_open_partition (int flags);
1011 int open_partition (void);
1012 int next_partition (unsigned long drive, unsigned long dest,
1013 		    unsigned long *partition, int *type,
1014 		    unsigned long *start, unsigned long *len,
1015 		    unsigned long *offset, int *entry,
1016 		    unsigned long *ext_offset, char *buf);
1017 
1018 /* Sets device to the one represented by the SAVED_* parameters. */
1019 int make_saved_active (void);
1020 
1021 /* Set or clear the current root partition's hidden flag.  */
1022 int set_partition_hidden_flag (int hidden);
1023 
1024 /* Open a file or directory on the active device, using GRUB's
1025    internal filesystem support. */
1026 int grub_open (char *filename);
1027 
1028 /* Read LEN bytes into BUF from the file that was opened with
1029    GRUB_OPEN.  If LEN is -1, read all the remaining data in the file.  */
1030 int grub_read (char *buf, int len);
1031 
1032 /* Reposition a file offset.  */
1033 int grub_seek (int offset);
1034 
1035 /* Close a file.  */
1036 void grub_close (void);
1037 
1038 /* List the contents of the directory that was opened with GRUB_OPEN,
1039    printing all completions. */
1040 int dir (char *dirname);
1041 
1042 int set_bootdev (int hdbias);
1043 
1044 /* Display statistics on the current active device. */
1045 void print_fsys_type (void);
1046 
1047 /* Display device and filename completions. */
1048 void print_a_completion (char *filename);
1049 int print_completions (int is_filename, int is_completion);
1050 
1051 /* Copies the current partition data to the desired address. */
1052 void copy_current_part_entry (char *buf);
1053 
1054 #ifndef STAGE1_5
1055 void bsd_boot (kernel_t type, int bootdev, char *arg)
1056      __attribute__ ((noreturn));
1057 
1058 /* Define flags for load_image here.  */
1059 /* Don't pass a Linux's mem option automatically.  */
1060 #define KERNEL_LOAD_NO_MEM_OPTION	(1 << 0)
1061 
1062 kernel_t load_image (char *kernel, char *arg, kernel_t suggested_type,
1063 		     unsigned long load_flags);
1064 
1065 int load_module (char *module, char *arg);
1066 int load_initrd (char *initrd);
1067 
1068 int check_password(char *entered, char* expected, password_t type);
1069 #endif
1070 
1071 void init_bios_info (void);
1072 
1073 #endif /* ASM_FILE */
1074 
1075 #endif /* ! GRUB_SHARED_HEADER */
1076