1N/A/* common.c - miscellaneous shared variables and routines */
1N/A/*
1N/A * GRUB -- GRand Unified Bootloader
1N/A * Copyright (C) 1999,2000,2001,2002,2004 Free Software Foundation, Inc.
1N/A *
1N/A * This program is free software; you can redistribute it and/or modify
1N/A * it under the terms of the GNU General Public License as published by
1N/A * the Free Software Foundation; either version 2 of the License, or
1N/A * (at your option) any later version.
1N/A *
1N/A * This program is distributed in the hope that it will be useful,
1N/A * but WITHOUT ANY WARRANTY; without even the implied warranty of
1N/A * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1N/A * GNU General Public License for more details.
1N/A *
1N/A * You should have received a copy of the GNU General Public License
1N/A * along with this program; if not, write to the Free Software
1N/A * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
1N/A */
1N/A
1N/A#include <shared.h>
1N/A
1N/A#ifdef SUPPORT_NETBOOT
1N/A#include <grub.h>
1N/A#include <bootp.h>
1N/A#endif
1N/A
1N/A/*
1N/A * Shared BIOS/boot data.
1N/A */
1N/A
1N/Astruct multiboot_info mbi;
1N/Astruct vbe_controller mbi_vbe_controller;
1N/Astruct vbe_mode mbi_vbe_mode_info;
1N/Aunsigned long saved_drive;
1N/Aunsigned long saved_partition;
1N/Aunsigned long cdrom_drive;
1N/A#ifndef STAGE1_5
1N/A#ifdef SOLARIS_NETBOOT
1N/Aunsigned long dhcpack_length;
1N/Aunsigned long dhcpack_buf;
1N/A#endif /* SOLARIS_NETBOOT */
1N/Aunsigned long saved_mem_upper;
1N/A
1N/A/* This saves the maximum size of extended memory (in KB). */
1N/Aunsigned long extended_memory;
1N/A#endif
1N/A
1N/A/*
1N/A * Error code stuff.
1N/A */
1N/A
1N/Agrub_error_t errnum = ERR_NONE;
1N/A
1N/A#ifndef STAGE1_5
1N/A
1N/Achar *err_list[] =
1N/A{
1N/A [ERR_NONE] = 0,
1N/A [ERR_BAD_ARGUMENT] = "Invalid argument",
1N/A [ERR_BAD_FILENAME] =
1N/A "Filename must be either an absolute pathname or blocklist",
1N/A [ERR_BAD_FILETYPE] = "Bad file or directory type",
1N/A [ERR_BAD_GZIP_DATA] = "Bad or corrupt data while decompressing file",
1N/A [ERR_BAD_GZIP_HEADER] = "Bad or incompatible header in compressed file",
1N/A [ERR_BAD_PART_TABLE] = "Partition table invalid or corrupt",
1N/A [ERR_BAD_VERSION] = "Mismatched or corrupt version of stage1/stage2",
1N/A [ERR_BELOW_1MB] = "Loading below 1MB is not supported",
1N/A [ERR_BOOT_COMMAND] = "Kernel must be loaded before booting",
1N/A [ERR_BOOT_FAILURE] = "Unknown boot failure",
1N/A [ERR_BOOT_FEATURES] = "Unsupported Multiboot features requested",
1N/A [ERR_DEV_FORMAT] = "Unrecognized device string",
1N/A [ERR_DEV_NEED_INIT] = "Device not initialized yet",
1N/A [ERR_DEV_VALUES] = "Invalid device requested",
1N/A [ERR_EXEC_FORMAT] = "Invalid or unsupported executable format",
1N/A [ERR_FILELENGTH] =
1N/A "Filesystem compatibility error, cannot read whole file",
1N/A [ERR_FILE_NOT_FOUND] = "File not found",
1N/A [ERR_FSYS_CORRUPT] = "Inconsistent filesystem structure",
1N/A [ERR_FSYS_MOUNT] = "Cannot mount selected partition",
1N/A [ERR_GEOM] = "Selected cylinder exceeds maximum supported by BIOS",
1N/A [ERR_NEED_LX_KERNEL] = "Linux kernel must be loaded before initrd",
1N/A [ERR_NEED_MB_KERNEL] = "Multiboot kernel must be loaded before modules",
1N/A [ERR_NO_DISK] = "Selected disk does not exist",
1N/A [ERR_NO_DISK_SPACE] = "No spare sectors on the disk",
1N/A [ERR_NO_PART] = "No such partition",
1N/A [ERR_NUMBER_OVERFLOW] = "Overflow while parsing number",
1N/A [ERR_NUMBER_PARSING] = "Error while parsing number",
1N/A [ERR_OUTSIDE_PART] = "Attempt to access block outside partition",
1N/A [ERR_PRIVILEGED] = "Must be authenticated",
1N/A [ERR_READ] = "Disk read error",
1N/A [ERR_SYMLINK_LOOP] = "Too many symbolic links",
1N/A [ERR_UNALIGNED] = "File is not sector aligned",
1N/A [ERR_UNRECOGNIZED] = "Unrecognized command",
1N/A [ERR_WONT_FIT] = "Selected item cannot fit into memory",
1N/A [ERR_WRITE] = "Disk write error",
1N/A [ERR_BAD_GZIP_CRC] = "Incorrect gunzip CRC checksum",
1N/A [ERR_FILESYSTEM_NOT_FOUND] = "File System not found",
1N/A /* this zfs file system is not found in the pool of the device */
1N/A [ERR_NO_BOOTPATH] = "No valid boot path found in the zfs label. This may be caused by attempting to boot from an off-lined device.",
1N/A [ERR_NEWER_VERSION] = "Newer on-disk pool version",
1N/A [ERR_NOTXPM] = "Image not in XPM graphics format",
1N/A [ERR_TOOMANYCOLORS] = "Image cannot use more than 14 colors",
1N/A [ERR_CORRUPTXPM] = "File contains corrupt XPM image data"
1N/A};
1N/A
1N/A
1N/A/* static for BIOS memory map fakery */
1N/Astatic struct AddrRangeDesc fakemap[3] =
1N/A{
1N/A {20, 0, 0, MB_ARD_MEMORY},
1N/A {20, 0x100000, 0, MB_ARD_MEMORY},
1N/A {20, 0x1000000, 0, MB_ARD_MEMORY}
1N/A};
1N/A
1N/A/* A big problem is that the memory areas aren't guaranteed to be:
1N/A (1) contiguous, (2) sorted in ascending order, or (3) non-overlapping.
1N/A Thus this kludge. */
1N/Astatic unsigned long
1N/Ammap_avail_at (unsigned long bottom)
1N/A{
1N/A unsigned long long top;
1N/A unsigned long addr;
1N/A int cont;
1N/A
1N/A top = bottom;
1N/A do
1N/A {
1N/A for (cont = 0, addr = mbi.mmap_addr;
1N/A addr < mbi.mmap_addr + mbi.mmap_length;
1N/A addr += *((unsigned long *) addr) + 4)
1N/A {
1N/A struct AddrRangeDesc *desc = (struct AddrRangeDesc *) addr;
1N/A
1N/A if (desc->Type == MB_ARD_MEMORY
1N/A && desc->BaseAddr <= top
1N/A && desc->BaseAddr + desc->Length > top)
1N/A {
1N/A top = desc->BaseAddr + desc->Length;
1N/A cont++;
1N/A }
1N/A }
1N/A }
1N/A while (cont);
1N/A
1N/A /* For now, GRUB assumes 32bits addresses, so... */
1N/A if (top > 0xFFFFFFFF)
1N/A top = 0xFFFFFFFF;
1N/A
1N/A return (unsigned long) top - bottom;
1N/A}
1N/A#endif /* ! STAGE1_5 */
1N/A
1N/A/* This queries for BIOS information. */
1N/Avoid
1N/Ainit_bios_info (void)
1N/A{
1N/A#ifndef STAGE1_5
1N/A unsigned long cont, memtmp, addr;
1N/A int drive;
1N/A#endif
1N/A
1N/A /*
1N/A * Get information from BIOS on installed RAM.
1N/A */
1N/A
1N/A mbi.mem_lower = get_memsize (0);
1N/A mbi.mem_upper = get_memsize (1);
1N/A
1N/A#ifndef STAGE1_5
1N/A /*
1N/A * We need to call this somewhere before trying to put data
1N/A * above 1 MB, since without calling it, address line 20 will be wired
1N/A * to 0. Not too desirable.
1N/A */
1N/A
1N/A gateA20 (1);
1N/A
1N/A /* Store the size of extended memory in EXTENDED_MEMORY, in order to
1N/A tell it to non-Multiboot OSes. */
1N/A extended_memory = mbi.mem_upper;
1N/A
1N/A /*
1N/A * The "mbi.mem_upper" variable only recognizes upper memory in the
1N/A * first memory region. If there are multiple memory regions,
1N/A * the rest are reported to a Multiboot-compliant OS, but otherwise
1N/A * unused by GRUB.
1N/A */
1N/A
1N/A addr = get_code_end ();
1N/A mbi.mmap_addr = addr;
1N/A mbi.mmap_length = 0;
1N/A cont = 0;
1N/A
1N/A do
1N/A {
1N/A cont = get_mmap_entry ((void *) addr, cont);
1N/A
1N/A /* If the returned buffer's length is zero, quit. */
1N/A if (! *((unsigned long *) addr))
1N/A break;
1N/A
1N/A mbi.mmap_length += *((unsigned long *) addr) + 4;
1N/A addr += *((unsigned long *) addr) + 4;
1N/A }
1N/A while (cont);
1N/A
1N/A if (mbi.mmap_length)
1N/A {
1N/A unsigned long long max_addr;
1N/A
1N/A /*
1N/A * This is to get the lower memory, and upper memory (up to the
1N/A * first memory hole), into the "mbi.mem_{lower,upper}"
1N/A * elements. This is for OS's that don't care about the memory
1N/A * map, but might care about total RAM available.
1N/A */
1N/A mbi.mem_lower = mmap_avail_at (0) >> 10;
1N/A mbi.mem_upper = mmap_avail_at (0x100000) >> 10;
1N/A
1N/A /* Find the maximum available address. Ignore any memory holes. */
1N/A for (max_addr = 0, addr = mbi.mmap_addr;
1N/A addr < mbi.mmap_addr + mbi.mmap_length;
1N/A addr += *((unsigned long *) addr) + 4)
1N/A {
1N/A struct AddrRangeDesc *desc = (struct AddrRangeDesc *) addr;
1N/A
1N/A if (desc->Type == MB_ARD_MEMORY && desc->Length > 0
1N/A && desc->BaseAddr + desc->Length > max_addr)
1N/A max_addr = desc->BaseAddr + desc->Length;
1N/A }
1N/A
1N/A extended_memory = (max_addr - 0x100000) >> 10;
1N/A }
1N/A else if ((memtmp = get_eisamemsize ()) != -1)
1N/A {
1N/A cont = memtmp & ~0xFFFF;
1N/A memtmp = memtmp & 0xFFFF;
1N/A
1N/A if (cont != 0)
1N/A extended_memory = (cont >> 10) + 0x3c00;
1N/A else
1N/A extended_memory = memtmp;
1N/A
1N/A if (!cont || (memtmp == 0x3c00))
1N/A memtmp += (cont >> 10);
1N/A else
1N/A {
1N/A /* XXX should I do this at all ??? */
1N/A
1N/A mbi.mmap_addr = (unsigned long) fakemap;
1N/A mbi.mmap_length = sizeof (fakemap);
1N/A fakemap[0].Length = (mbi.mem_lower << 10);
1N/A fakemap[1].Length = (memtmp << 10);
1N/A fakemap[2].Length = cont;
1N/A }
1N/A
1N/A mbi.mem_upper = memtmp;
1N/A }
1N/A
1N/A saved_mem_upper = mbi.mem_upper;
1N/A
1N/A#ifdef SUPPORT_NETBOOT
1N/A#ifdef SOLARIS_NETBOOT
1N/A /* leave room for dhcpack_buf */
1N/A dhcpack_buf = addr;
1N/A addr += sizeof (struct dhcp_t);
1N/A#endif
1N/A#endif
1N/A
1N/A /* Get the drive info. */
1N/A /* FIXME: This should be postponed until a Multiboot kernel actually
1N/A requires it, because this could slow down the start-up
1N/A unreasonably. */
1N/A mbi.drives_length = 0;
1N/A mbi.drives_addr = addr;
1N/A
1N/A /* For now, GRUB doesn't probe floppies, since it is trivial to map
1N/A floppy drives to BIOS drives. */
1N/A for (drive = 0x80; drive < 0x88; drive++)
1N/A {
1N/A struct geometry geom;
1N/A struct drive_info *info = (struct drive_info *) addr;
1N/A unsigned short *port;
1N/A
1N/A /* Get the geometry. This ensures that the drive is present. */
1N/A if (get_diskinfo (drive, &geom))
1N/A break;
1N/A
1N/A /* Clean out the I/O map. */
1N/A grub_memset ((char *) io_map, 0,
1N/A IO_MAP_SIZE * sizeof (unsigned short));
1N/A
1N/A /* Disable to probe I/O ports temporarily, because this doesn't
1N/A work with some BIOSes (maybe they are too buggy). */
1N/A#if 0
1N/A /* Track the int13 handler. */
1N/A track_int13 (drive);
1N/A#endif
1N/A
1N/A /* Set the information. */
1N/A info->drive_number = drive;
1N/A info->drive_mode = ((geom.flags & BIOSDISK_FLAG_LBA_EXTENSION)
1N/A ? MB_DI_LBA_MODE : MB_DI_CHS_MODE);
1N/A info->drive_cylinders = geom.cylinders;
1N/A info->drive_heads = geom.heads;
1N/A info->drive_sectors = geom.sectors;
1N/A
1N/A addr += sizeof (struct drive_info);
1N/A for (port = io_map; *port; port++, addr += sizeof (unsigned short))
1N/A *((unsigned short *) addr) = *port;
1N/A
1N/A info->size = addr - (unsigned long) info;
1N/A mbi.drives_length += info->size;
1N/A }
1N/A
1N/A /* Get the ROM configuration table by INT 15, AH=C0h. */
1N/A mbi.config_table = get_rom_config_table ();
1N/A
1N/A /* Set the boot loader name. */
1N/A mbi.boot_loader_name = (unsigned long) "GNU GRUB " VERSION;
1N/A
1N/A /* Get the APM BIOS table. */
1N/A get_apm_info ();
1N/A if (apm_bios_info.version)
1N/A mbi.apm_table = (unsigned long) &apm_bios_info;
1N/A
1N/A /*
1N/A * Initialize other Multiboot Info flags.
1N/A */
1N/A
1N/A mbi.flags = (MB_INFO_MEMORY | MB_INFO_CMDLINE | MB_INFO_BOOTDEV
1N/A | MB_INFO_DRIVE_INFO | MB_INFO_CONFIG_TABLE
1N/A | MB_INFO_BOOT_LOADER_NAME);
1N/A
1N/A if (apm_bios_info.version)
1N/A mbi.flags |= MB_INFO_APM_TABLE;
1N/A
1N/A#endif /* STAGE1_5 */
1N/A
1N/A /* Set boot drive and partition. */
1N/A saved_drive = boot_drive;
1N/A saved_partition = install_partition;
1N/A
1N/A /* Set cdrom drive. */
1N/A {
1N/A struct geometry geom;
1N/A
1N/A /* Get the geometry. */
1N/A if (get_diskinfo (boot_drive, &geom)
1N/A || ! (geom.flags & BIOSDISK_FLAG_CDROM))
1N/A cdrom_drive = GRUB_INVALID_DRIVE;
1N/A else
1N/A cdrom_drive = boot_drive;
1N/A }
1N/A
1N/A /* Start main routine here. */
1N/A cmain ();
1N/A}