1 /* common.c - miscellaneous shared variables and routines */ 2 /* 3 * GRUB -- GRand Unified Bootloader 4 * Copyright (C) 1999,2000,2001,2002,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 #include <shared.h> 22 23 #ifdef SUPPORT_NETBOOT 24 #include <grub.h> 25 #include <bootp.h> 26 #endif 27 28 /* 29 * Shared BIOS/boot data. 30 */ 31 32 struct multiboot_info mbi; 33 unsigned long saved_drive; 34 unsigned long saved_partition; 35 unsigned long cdrom_drive; 36 #ifndef STAGE1_5 37 #ifdef SOLARIS_NETBOOT 38 unsigned long dhcpack_length; 39 unsigned long dhcpack_buf; 40 #endif /* SOLARIS_NETBOOT */ 41 unsigned long saved_mem_upper; 42 43 /* This saves the maximum size of extended memory (in KB). */ 44 unsigned long extended_memory; 45 #endif 46 47 /* 48 * Error code stuff. 49 */ 50 51 grub_error_t errnum = ERR_NONE; 52 53 #ifndef STAGE1_5 54 55 char *err_list[] = 56 { 57 [ERR_NONE] = 0, 58 [ERR_BAD_ARGUMENT] = "Invalid argument", 59 [ERR_BAD_FILENAME] = 60 "Filename must be either an absolute pathname or blocklist", 61 [ERR_BAD_FILETYPE] = "Bad file or directory type", 62 [ERR_BAD_GZIP_DATA] = "Bad or corrupt data while decompressing file", 63 [ERR_BAD_GZIP_HEADER] = "Bad or incompatible header in compressed file", 64 [ERR_BAD_PART_TABLE] = "Partition table invalid or corrupt", 65 [ERR_BAD_VERSION] = "Mismatched or corrupt version of stage1/stage2", 66 [ERR_BELOW_1MB] = "Loading below 1MB is not supported", 67 [ERR_BOOT_COMMAND] = "Kernel must be loaded before booting", 68 [ERR_BOOT_FAILURE] = "Unknown boot failure", 69 [ERR_BOOT_FEATURES] = "Unsupported Multiboot features requested", 70 [ERR_DEV_FORMAT] = "Unrecognized device string", 71 [ERR_DEV_NEED_INIT] = "Device not initialized yet", 72 [ERR_DEV_VALUES] = "Invalid device requested", 73 [ERR_EXEC_FORMAT] = "Invalid or unsupported executable format", 74 [ERR_FILELENGTH] = 75 "Filesystem compatibility error, cannot read whole file", 76 [ERR_FILE_NOT_FOUND] = "File not found", 77 [ERR_FSYS_CORRUPT] = "Inconsistent filesystem structure", 78 [ERR_FSYS_MOUNT] = "Cannot mount selected partition", 79 [ERR_GEOM] = "Selected cylinder exceeds maximum supported by BIOS", 80 [ERR_NEED_LX_KERNEL] = "Linux kernel must be loaded before initrd", 81 [ERR_NEED_MB_KERNEL] = "Multiboot kernel must be loaded before modules", 82 [ERR_NO_DISK] = "Selected disk does not exist", 83 [ERR_NO_DISK_SPACE] = "No spare sectors on the disk", 84 [ERR_NO_PART] = "No such partition", 85 [ERR_NUMBER_OVERFLOW] = "Overflow while parsing number", 86 [ERR_NUMBER_PARSING] = "Error while parsing number", 87 [ERR_OUTSIDE_PART] = "Attempt to access block outside partition", 88 [ERR_PRIVILEGED] = "Must be authenticated", 89 [ERR_READ] = "Disk read error", 90 [ERR_SYMLINK_LOOP] = "Too many symbolic links", 91 [ERR_UNALIGNED] = "File is not sector aligned", 92 [ERR_UNRECOGNIZED] = "Unrecognized command", 93 [ERR_WONT_FIT] = "Selected item cannot fit into memory", 94 [ERR_WRITE] = "Disk write error", 95 [ERR_BAD_GZIP_CRC] = "Incorrect gunzip CRC checksum", 96 [ERR_FILESYSTEM_NOT_FOUND] = "File System not found", 97 /* this zfs file system is not found in the pool of the device */ 98 [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.", 99 [ERR_NEWER_VERSION] = "Newer on-disk pool version", 100 [ERR_NOTXPM] = "Image not in XPM graphics format", 101 [ERR_TOOMANYCOLORS] = "Image cannot use more than 14 colors", 102 [ERR_CORRUPTXPM] = "File contains corrupt XPM image data" 103 }; 104 105 106 /* static for BIOS memory map fakery */ 107 static struct AddrRangeDesc fakemap[3] = 108 { 109 {20, 0, 0, MB_ARD_MEMORY}, 110 {20, 0x100000, 0, MB_ARD_MEMORY}, 111 {20, 0x1000000, 0, MB_ARD_MEMORY} 112 }; 113 114 /* A big problem is that the memory areas aren't guaranteed to be: 115 (1) contiguous, (2) sorted in ascending order, or (3) non-overlapping. 116 Thus this kludge. */ 117 static unsigned long 118 mmap_avail_at (unsigned long bottom) 119 { 120 unsigned long long top; 121 unsigned long addr; 122 int cont; 123 124 top = bottom; 125 do 126 { 127 for (cont = 0, addr = mbi.mmap_addr; 128 addr < mbi.mmap_addr + mbi.mmap_length; 129 addr += *((unsigned long *) addr) + 4) 130 { 131 struct AddrRangeDesc *desc = (struct AddrRangeDesc *) addr; 132 133 if (desc->Type == MB_ARD_MEMORY 134 && desc->BaseAddr <= top 135 && desc->BaseAddr + desc->Length > top) 136 { 137 top = desc->BaseAddr + desc->Length; 138 cont++; 139 } 140 } 141 } 142 while (cont); 143 144 /* For now, GRUB assumes 32bits addresses, so... */ 145 if (top > 0xFFFFFFFF) 146 top = 0xFFFFFFFF; 147 148 return (unsigned long) top - bottom; 149 } 150 #endif /* ! STAGE1_5 */ 151 152 /* This queries for BIOS information. */ 153 void 154 init_bios_info (void) 155 { 156 #ifndef STAGE1_5 157 unsigned long cont, memtmp, addr; 158 int drive; 159 #endif 160 161 /* 162 * Get information from BIOS on installed RAM. 163 */ 164 165 mbi.mem_lower = get_memsize (0); 166 mbi.mem_upper = get_memsize (1); 167 168 #ifndef STAGE1_5 169 /* 170 * We need to call this somewhere before trying to put data 171 * above 1 MB, since without calling it, address line 20 will be wired 172 * to 0. Not too desirable. 173 */ 174 175 gateA20 (1); 176 177 /* Store the size of extended memory in EXTENDED_MEMORY, in order to 178 tell it to non-Multiboot OSes. */ 179 extended_memory = mbi.mem_upper; 180 181 /* 182 * The "mbi.mem_upper" variable only recognizes upper memory in the 183 * first memory region. If there are multiple memory regions, 184 * the rest are reported to a Multiboot-compliant OS, but otherwise 185 * unused by GRUB. 186 */ 187 188 addr = get_code_end (); 189 mbi.mmap_addr = addr; 190 mbi.mmap_length = 0; 191 cont = 0; 192 193 do 194 { 195 cont = get_mmap_entry ((void *) addr, cont); 196 197 /* If the returned buffer's length is zero, quit. */ 198 if (! *((unsigned long *) addr)) 199 break; 200 201 mbi.mmap_length += *((unsigned long *) addr) + 4; 202 addr += *((unsigned long *) addr) + 4; 203 } 204 while (cont); 205 206 if (mbi.mmap_length) 207 { 208 unsigned long long max_addr; 209 210 /* 211 * This is to get the lower memory, and upper memory (up to the 212 * first memory hole), into the "mbi.mem_{lower,upper}" 213 * elements. This is for OS's that don't care about the memory 214 * map, but might care about total RAM available. 215 */ 216 mbi.mem_lower = mmap_avail_at (0) >> 10; 217 mbi.mem_upper = mmap_avail_at (0x100000) >> 10; 218 219 /* Find the maximum available address. Ignore any memory holes. */ 220 for (max_addr = 0, addr = mbi.mmap_addr; 221 addr < mbi.mmap_addr + mbi.mmap_length; 222 addr += *((unsigned long *) addr) + 4) 223 { 224 struct AddrRangeDesc *desc = (struct AddrRangeDesc *) addr; 225 226 if (desc->Type == MB_ARD_MEMORY && desc->Length > 0 227 && desc->BaseAddr + desc->Length > max_addr) 228 max_addr = desc->BaseAddr + desc->Length; 229 } 230 231 extended_memory = (max_addr - 0x100000) >> 10; 232 } 233 else if ((memtmp = get_eisamemsize ()) != -1) 234 { 235 cont = memtmp & ~0xFFFF; 236 memtmp = memtmp & 0xFFFF; 237 238 if (cont != 0) 239 extended_memory = (cont >> 10) + 0x3c00; 240 else 241 extended_memory = memtmp; 242 243 if (!cont || (memtmp == 0x3c00)) 244 memtmp += (cont >> 10); 245 else 246 { 247 /* XXX should I do this at all ??? */ 248 249 mbi.mmap_addr = (unsigned long) fakemap; 250 mbi.mmap_length = sizeof (fakemap); 251 fakemap[0].Length = (mbi.mem_lower << 10); 252 fakemap[1].Length = (memtmp << 10); 253 fakemap[2].Length = cont; 254 } 255 256 mbi.mem_upper = memtmp; 257 } 258 259 saved_mem_upper = mbi.mem_upper; 260 261 #ifdef SUPPORT_NETBOOT 262 #ifdef SOLARIS_NETBOOT 263 /* leave room for dhcpack_buf */ 264 dhcpack_buf = addr; 265 addr += sizeof (struct dhcp_t); 266 #endif 267 #endif 268 269 /* Get the drive info. */ 270 /* FIXME: This should be postponed until a Multiboot kernel actually 271 requires it, because this could slow down the start-up 272 unreasonably. */ 273 mbi.drives_length = 0; 274 mbi.drives_addr = addr; 275 276 /* For now, GRUB doesn't probe floppies, since it is trivial to map 277 floppy drives to BIOS drives. */ 278 for (drive = 0x80; drive < 0x88; drive++) 279 { 280 struct geometry geom; 281 struct drive_info *info = (struct drive_info *) addr; 282 unsigned short *port; 283 284 /* Get the geometry. This ensures that the drive is present. */ 285 if (get_diskinfo (drive, &geom)) 286 break; 287 288 /* Clean out the I/O map. */ 289 grub_memset ((char *) io_map, 0, 290 IO_MAP_SIZE * sizeof (unsigned short)); 291 292 /* Disable to probe I/O ports temporarily, because this doesn't 293 work with some BIOSes (maybe they are too buggy). */ 294 #if 0 295 /* Track the int13 handler. */ 296 track_int13 (drive); 297 #endif 298 299 /* Set the information. */ 300 info->drive_number = drive; 301 info->drive_mode = ((geom.flags & BIOSDISK_FLAG_LBA_EXTENSION) 302 ? MB_DI_LBA_MODE : MB_DI_CHS_MODE); 303 info->drive_cylinders = geom.cylinders; 304 info->drive_heads = geom.heads; 305 info->drive_sectors = geom.sectors; 306 307 addr += sizeof (struct drive_info); 308 for (port = io_map; *port; port++, addr += sizeof (unsigned short)) 309 *((unsigned short *) addr) = *port; 310 311 info->size = addr - (unsigned long) info; 312 mbi.drives_length += info->size; 313 } 314 315 /* Get the ROM configuration table by INT 15, AH=C0h. */ 316 mbi.config_table = get_rom_config_table (); 317 318 /* Set the boot loader name. */ 319 mbi.boot_loader_name = (unsigned long) "GNU GRUB " VERSION; 320 321 /* Get the APM BIOS table. */ 322 get_apm_info (); 323 if (apm_bios_info.version) 324 mbi.apm_table = (unsigned long) &apm_bios_info; 325 326 /* 327 * Initialize other Multiboot Info flags. 328 */ 329 330 mbi.flags = (MB_INFO_MEMORY | MB_INFO_CMDLINE | MB_INFO_BOOTDEV 331 | MB_INFO_DRIVE_INFO | MB_INFO_CONFIG_TABLE 332 | MB_INFO_BOOT_LOADER_NAME); 333 334 if (apm_bios_info.version) 335 mbi.flags |= MB_INFO_APM_TABLE; 336 337 #endif /* STAGE1_5 */ 338 339 /* Set boot drive and partition. */ 340 saved_drive = boot_drive; 341 saved_partition = install_partition; 342 343 /* Set cdrom drive. */ 344 { 345 struct geometry geom; 346 347 /* Get the geometry. */ 348 if (get_diskinfo (boot_drive, &geom) 349 || ! (geom.flags & BIOSDISK_FLAG_CDROM)) 350 cdrom_drive = GRUB_INVALID_DRIVE; 351 else 352 cdrom_drive = boot_drive; 353 } 354 355 /* Start main routine here. */ 356 cmain (); 357 } 358