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