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