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