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