H. Peter Anvin | d13444a | 2007-07-11 12:18:48 -0700 | [diff] [blame] | 1 | /* -*- linux-c -*- ------------------------------------------------------- * |
| 2 | * |
| 3 | * Copyright (C) 1991, 1992 Linus Torvalds |
| 4 | * Copyright 2007 rPath, Inc. - All Rights Reserved |
H. Peter Anvin | 3435d34 | 2009-04-01 18:17:17 -0700 | [diff] [blame] | 5 | * Copyright 2009 Intel Corporation; author H. Peter Anvin |
H. Peter Anvin | d13444a | 2007-07-11 12:18:48 -0700 | [diff] [blame] | 6 | * |
| 7 | * This file is part of the Linux kernel, and is made available under |
| 8 | * the terms of the GNU General Public License version 2. |
| 9 | * |
| 10 | * ----------------------------------------------------------------------- */ |
| 11 | |
| 12 | /* |
H. Peter Anvin | d13444a | 2007-07-11 12:18:48 -0700 | [diff] [blame] | 13 | * Get EDD BIOS disk information |
| 14 | */ |
| 15 | |
| 16 | #include "boot.h" |
| 17 | #include <linux/edd.h> |
| 18 | |
| 19 | #if defined(CONFIG_EDD) || defined(CONFIG_EDD_MODULE) |
| 20 | |
H. Peter Anvin | d13444a | 2007-07-11 12:18:48 -0700 | [diff] [blame] | 21 | /* |
| 22 | * Read the MBR (first sector) from a specific device. |
| 23 | */ |
| 24 | static int read_mbr(u8 devno, void *buf) |
| 25 | { |
H. Peter Anvin | 3435d34 | 2009-04-01 18:17:17 -0700 | [diff] [blame] | 26 | struct biosregs ireg, oreg; |
H. Peter Anvin | d13444a | 2007-07-11 12:18:48 -0700 | [diff] [blame] | 27 | |
H. Peter Anvin | 3435d34 | 2009-04-01 18:17:17 -0700 | [diff] [blame] | 28 | initregs(&ireg); |
| 29 | ireg.ax = 0x0201; /* Legacy Read, one sector */ |
| 30 | ireg.cx = 0x0001; /* Sector 0-0-1 */ |
| 31 | ireg.dl = devno; |
| 32 | ireg.bx = (size_t)buf; |
H. Peter Anvin | d13444a | 2007-07-11 12:18:48 -0700 | [diff] [blame] | 33 | |
H. Peter Anvin | 3435d34 | 2009-04-01 18:17:17 -0700 | [diff] [blame] | 34 | intcall(0x13, &ireg, &oreg); |
| 35 | |
| 36 | return -(oreg.eflags & X86_EFLAGS_CF); /* 0 or -1 */ |
H. Peter Anvin | d13444a | 2007-07-11 12:18:48 -0700 | [diff] [blame] | 37 | } |
| 38 | |
H. Peter Anvin | 9a5f35d | 2007-08-14 17:36:00 -0700 | [diff] [blame] | 39 | static u32 read_mbr_sig(u8 devno, struct edd_info *ei, u32 *mbrsig) |
H. Peter Anvin | d13444a | 2007-07-11 12:18:48 -0700 | [diff] [blame] | 40 | { |
| 41 | int sector_size; |
| 42 | char *mbrbuf_ptr, *mbrbuf_end; |
H. Peter Anvin | d13444a | 2007-07-11 12:18:48 -0700 | [diff] [blame] | 43 | u32 buf_base, mbr_base; |
| 44 | extern char _end[]; |
Andrey Borzenkov | 6cdcdb9 | 2008-10-03 21:08:49 +0400 | [diff] [blame] | 45 | u16 mbr_magic; |
H. Peter Anvin | d13444a | 2007-07-11 12:18:48 -0700 | [diff] [blame] | 46 | |
| 47 | sector_size = ei->params.bytes_per_sector; |
| 48 | if (!sector_size) |
| 49 | sector_size = 512; /* Best available guess */ |
| 50 | |
H. Peter Anvin | ff659d1 | 2007-07-31 16:17:13 -0400 | [diff] [blame] | 51 | /* Produce a naturally aligned buffer on the heap */ |
H. Peter Anvin | d13444a | 2007-07-11 12:18:48 -0700 | [diff] [blame] | 52 | buf_base = (ds() << 4) + (u32)&_end; |
| 53 | mbr_base = (buf_base+sector_size-1) & ~(sector_size-1); |
H. Peter Anvin | ff659d1 | 2007-07-31 16:17:13 -0400 | [diff] [blame] | 54 | mbrbuf_ptr = _end + (mbr_base-buf_base); |
H. Peter Anvin | d13444a | 2007-07-11 12:18:48 -0700 | [diff] [blame] | 55 | mbrbuf_end = mbrbuf_ptr + sector_size; |
| 56 | |
H. Peter Anvin | ff659d1 | 2007-07-31 16:17:13 -0400 | [diff] [blame] | 57 | /* Make sure we actually have space on the heap... */ |
H. Peter Anvin | d13444a | 2007-07-11 12:18:48 -0700 | [diff] [blame] | 58 | if (!(boot_params.hdr.loadflags & CAN_USE_HEAP)) |
H. Peter Anvin | 9a5f35d | 2007-08-14 17:36:00 -0700 | [diff] [blame] | 59 | return -1; |
H. Peter Anvin | d13444a | 2007-07-11 12:18:48 -0700 | [diff] [blame] | 60 | if (mbrbuf_end > (char *)(size_t)boot_params.hdr.heap_end_ptr) |
H. Peter Anvin | 9a5f35d | 2007-08-14 17:36:00 -0700 | [diff] [blame] | 61 | return -1; |
H. Peter Anvin | d13444a | 2007-07-11 12:18:48 -0700 | [diff] [blame] | 62 | |
Andrey Borzenkov | 6cdcdb9 | 2008-10-03 21:08:49 +0400 | [diff] [blame] | 63 | memset(mbrbuf_ptr, 0, sector_size); |
H. Peter Anvin | d13444a | 2007-07-11 12:18:48 -0700 | [diff] [blame] | 64 | if (read_mbr(devno, mbrbuf_ptr)) |
H. Peter Anvin | 9a5f35d | 2007-08-14 17:36:00 -0700 | [diff] [blame] | 65 | return -1; |
H. Peter Anvin | d13444a | 2007-07-11 12:18:48 -0700 | [diff] [blame] | 66 | |
H. Peter Anvin | 9a5f35d | 2007-08-14 17:36:00 -0700 | [diff] [blame] | 67 | *mbrsig = *(u32 *)&mbrbuf_ptr[EDD_MBR_SIG_OFFSET]; |
Andrey Borzenkov | 6cdcdb9 | 2008-10-03 21:08:49 +0400 | [diff] [blame] | 68 | mbr_magic = *(u16 *)&mbrbuf_ptr[510]; |
| 69 | |
| 70 | /* check for valid MBR magic */ |
| 71 | return mbr_magic == 0xAA55 ? 0 : -1; |
H. Peter Anvin | d13444a | 2007-07-11 12:18:48 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | static int get_edd_info(u8 devno, struct edd_info *ei) |
| 75 | { |
H. Peter Anvin | 3435d34 | 2009-04-01 18:17:17 -0700 | [diff] [blame] | 76 | struct biosregs ireg, oreg; |
H. Peter Anvin | d13444a | 2007-07-11 12:18:48 -0700 | [diff] [blame] | 77 | |
| 78 | memset(ei, 0, sizeof *ei); |
| 79 | |
| 80 | /* Check Extensions Present */ |
| 81 | |
H. Peter Anvin | 3435d34 | 2009-04-01 18:17:17 -0700 | [diff] [blame] | 82 | initregs(&ireg); |
| 83 | ireg.ah = 0x41; |
| 84 | ireg.bx = EDDMAGIC1; |
| 85 | ireg.dl = devno; |
| 86 | intcall(0x13, &ireg, &oreg); |
H. Peter Anvin | d13444a | 2007-07-11 12:18:48 -0700 | [diff] [blame] | 87 | |
H. Peter Anvin | 3435d34 | 2009-04-01 18:17:17 -0700 | [diff] [blame] | 88 | if (oreg.eflags & X86_EFLAGS_CF) |
H. Peter Anvin | d13444a | 2007-07-11 12:18:48 -0700 | [diff] [blame] | 89 | return -1; /* No extended information */ |
| 90 | |
H. Peter Anvin | 3435d34 | 2009-04-01 18:17:17 -0700 | [diff] [blame] | 91 | if (oreg.bx != EDDMAGIC2) |
H. Peter Anvin | d13444a | 2007-07-11 12:18:48 -0700 | [diff] [blame] | 92 | return -1; |
| 93 | |
| 94 | ei->device = devno; |
H. Peter Anvin | 3435d34 | 2009-04-01 18:17:17 -0700 | [diff] [blame] | 95 | ei->version = oreg.ah; /* EDD version number */ |
| 96 | ei->interface_support = oreg.cx; /* EDD functionality subsets */ |
H. Peter Anvin | d13444a | 2007-07-11 12:18:48 -0700 | [diff] [blame] | 97 | |
| 98 | /* Extended Get Device Parameters */ |
| 99 | |
| 100 | ei->params.length = sizeof(ei->params); |
H. Peter Anvin | 3435d34 | 2009-04-01 18:17:17 -0700 | [diff] [blame] | 101 | ireg.ah = 0x48; |
| 102 | ireg.si = (size_t)&ei->params; |
| 103 | intcall(0x13, &ireg, &oreg); |
H. Peter Anvin | d13444a | 2007-07-11 12:18:48 -0700 | [diff] [blame] | 104 | |
| 105 | /* Get legacy CHS parameters */ |
| 106 | |
| 107 | /* Ralf Brown recommends setting ES:DI to 0:0 */ |
H. Peter Anvin | 3435d34 | 2009-04-01 18:17:17 -0700 | [diff] [blame] | 108 | ireg.ah = 0x08; |
| 109 | ireg.es = 0; |
| 110 | intcall(0x13, &ireg, &oreg); |
H. Peter Anvin | d13444a | 2007-07-11 12:18:48 -0700 | [diff] [blame] | 111 | |
H. Peter Anvin | 3435d34 | 2009-04-01 18:17:17 -0700 | [diff] [blame] | 112 | if (!(oreg.eflags & X86_EFLAGS_CF)) { |
| 113 | ei->legacy_max_cylinder = oreg.ch + ((oreg.cl & 0xc0) << 2); |
| 114 | ei->legacy_max_head = oreg.dh; |
| 115 | ei->legacy_sectors_per_track = oreg.cl & 0x3f; |
H. Peter Anvin | d13444a | 2007-07-11 12:18:48 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | void query_edd(void) |
| 122 | { |
| 123 | char eddarg[8]; |
| 124 | int do_mbr = 1; |
Tim Gardner | 8c4dd60 | 2008-04-29 01:02:45 -0700 | [diff] [blame] | 125 | #ifdef CONFIG_EDD_OFF |
| 126 | int do_edd = 0; |
| 127 | #else |
H. Peter Anvin | d13444a | 2007-07-11 12:18:48 -0700 | [diff] [blame] | 128 | int do_edd = 1; |
Tim Gardner | 8c4dd60 | 2008-04-29 01:02:45 -0700 | [diff] [blame] | 129 | #endif |
devzero@web.de | e479c83 | 2008-01-30 13:33:03 +0100 | [diff] [blame] | 130 | int be_quiet; |
H. Peter Anvin | d13444a | 2007-07-11 12:18:48 -0700 | [diff] [blame] | 131 | int devno; |
| 132 | struct edd_info ei, *edp; |
H. Peter Anvin | 9a5f35d | 2007-08-14 17:36:00 -0700 | [diff] [blame] | 133 | u32 *mbrptr; |
H. Peter Anvin | d13444a | 2007-07-11 12:18:48 -0700 | [diff] [blame] | 134 | |
| 135 | if (cmdline_find_option("edd", eddarg, sizeof eddarg) > 0) { |
Tim Gardner | 8c4dd60 | 2008-04-29 01:02:45 -0700 | [diff] [blame] | 136 | if (!strcmp(eddarg, "skipmbr") || !strcmp(eddarg, "skip")) { |
| 137 | do_edd = 1; |
H. Peter Anvin | d13444a | 2007-07-11 12:18:48 -0700 | [diff] [blame] | 138 | do_mbr = 0; |
Tim Gardner | 8c4dd60 | 2008-04-29 01:02:45 -0700 | [diff] [blame] | 139 | } |
H. Peter Anvin | d13444a | 2007-07-11 12:18:48 -0700 | [diff] [blame] | 140 | else if (!strcmp(eddarg, "off")) |
| 141 | do_edd = 0; |
Tim Gardner | 8c4dd60 | 2008-04-29 01:02:45 -0700 | [diff] [blame] | 142 | else if (!strcmp(eddarg, "on")) |
| 143 | do_edd = 1; |
H. Peter Anvin | d13444a | 2007-07-11 12:18:48 -0700 | [diff] [blame] | 144 | } |
| 145 | |
devzero@web.de | e479c83 | 2008-01-30 13:33:03 +0100 | [diff] [blame] | 146 | be_quiet = cmdline_find_option_bool("quiet"); |
| 147 | |
H. Peter Anvin | 9a5f35d | 2007-08-14 17:36:00 -0700 | [diff] [blame] | 148 | edp = boot_params.eddbuf; |
| 149 | mbrptr = boot_params.edd_mbr_sig_buffer; |
H. Peter Anvin | d13444a | 2007-07-11 12:18:48 -0700 | [diff] [blame] | 150 | |
| 151 | if (!do_edd) |
| 152 | return; |
| 153 | |
devzero@web.de | e479c83 | 2008-01-30 13:33:03 +0100 | [diff] [blame] | 154 | /* Bugs in OnBoard or AddOnCards Bios may hang the EDD probe, |
| 155 | * so give a hint if this happens. |
| 156 | */ |
| 157 | |
| 158 | if (!be_quiet) |
H. Peter Anvin | b710763 | 2008-01-30 13:33:03 +0100 | [diff] [blame] | 159 | printf("Probing EDD (edd=off to disable)... "); |
devzero@web.de | e479c83 | 2008-01-30 13:33:03 +0100 | [diff] [blame] | 160 | |
H. Peter Anvin | d13444a | 2007-07-11 12:18:48 -0700 | [diff] [blame] | 161 | for (devno = 0x80; devno < 0x80+EDD_MBR_SIG_MAX; devno++) { |
| 162 | /* |
| 163 | * Scan the BIOS-supported hard disks and query EDD |
| 164 | * information... |
| 165 | */ |
Jan Beulich | f2ba939 | 2008-07-18 13:35:37 +0100 | [diff] [blame] | 166 | if (!get_edd_info(devno, &ei) |
| 167 | && boot_params.eddbuf_entries < EDDMAXNR) { |
H. Peter Anvin | d13444a | 2007-07-11 12:18:48 -0700 | [diff] [blame] | 168 | memcpy(edp, &ei, sizeof ei); |
| 169 | edp++; |
| 170 | boot_params.eddbuf_entries++; |
| 171 | } |
| 172 | |
H. Peter Anvin | 9a5f35d | 2007-08-14 17:36:00 -0700 | [diff] [blame] | 173 | if (do_mbr && !read_mbr_sig(devno, &ei, mbrptr++)) |
| 174 | boot_params.edd_mbr_sig_buf_entries = devno-0x80+1; |
H. Peter Anvin | d13444a | 2007-07-11 12:18:48 -0700 | [diff] [blame] | 175 | } |
devzero@web.de | e479c83 | 2008-01-30 13:33:03 +0100 | [diff] [blame] | 176 | |
| 177 | if (!be_quiet) |
H. Peter Anvin | f777501 | 2008-01-30 13:33:03 +0100 | [diff] [blame] | 178 | printf("ok\n"); |
H. Peter Anvin | d13444a | 2007-07-11 12:18:48 -0700 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | #endif |