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 | /* |
| 12 | * arch/i386/boot/edd.c |
| 13 | * |
| 14 | * Get EDD BIOS disk information |
| 15 | */ |
| 16 | |
| 17 | #include "boot.h" |
| 18 | #include <linux/edd.h> |
| 19 | |
| 20 | #if defined(CONFIG_EDD) || defined(CONFIG_EDD_MODULE) |
| 21 | |
H. Peter Anvin | d13444a | 2007-07-11 12:18:48 -0700 | [diff] [blame] | 22 | /* |
| 23 | * Read the MBR (first sector) from a specific device. |
| 24 | */ |
| 25 | static int read_mbr(u8 devno, void *buf) |
| 26 | { |
H. Peter Anvin | c1a6e2b | 2007-08-13 16:27:42 -0700 | [diff] [blame] | 27 | u16 ax, bx, cx, dx; |
H. Peter Anvin | d13444a | 2007-07-11 12:18:48 -0700 | [diff] [blame] | 28 | |
| 29 | ax = 0x0201; /* Legacy Read, one sector */ |
| 30 | cx = 0x0001; /* Sector 0-0-1 */ |
| 31 | dx = devno; |
| 32 | bx = (size_t)buf; |
H. Peter Anvin | b015124 | 2007-08-22 16:28:01 -0700 | [diff] [blame] | 33 | asm volatile("pushfl; stc; int $0x13; setc %%al; popfl" |
| 34 | : "+a" (ax), "+c" (cx), "+d" (dx), "+b" (bx) |
| 35 | : : "esi", "edi", "memory"); |
H. Peter Anvin | d13444a | 2007-07-11 12:18:48 -0700 | [diff] [blame] | 36 | |
| 37 | return -(u8)ax; /* 0 or -1 */ |
| 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[]; |
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 | |
| 63 | if (read_mbr(devno, mbrbuf_ptr)) |
H. Peter Anvin | 9a5f35d | 2007-08-14 17:36:00 -0700 | [diff] [blame] | 64 | return -1; |
H. Peter Anvin | d13444a | 2007-07-11 12:18:48 -0700 | [diff] [blame] | 65 | |
H. Peter Anvin | 9a5f35d | 2007-08-14 17:36:00 -0700 | [diff] [blame] | 66 | *mbrsig = *(u32 *)&mbrbuf_ptr[EDD_MBR_SIG_OFFSET]; |
| 67 | return 0; |
H. Peter Anvin | d13444a | 2007-07-11 12:18:48 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | static int get_edd_info(u8 devno, struct edd_info *ei) |
| 71 | { |
| 72 | u16 ax, bx, cx, dx, di; |
| 73 | |
| 74 | memset(ei, 0, sizeof *ei); |
| 75 | |
| 76 | /* Check Extensions Present */ |
| 77 | |
| 78 | ax = 0x4100; |
| 79 | bx = EDDMAGIC1; |
| 80 | dx = devno; |
| 81 | asm("pushfl; stc; int $0x13; setc %%al; popfl" |
| 82 | : "+a" (ax), "+b" (bx), "=c" (cx), "+d" (dx) |
| 83 | : : "esi", "edi"); |
| 84 | |
| 85 | if ((u8)ax) |
| 86 | return -1; /* No extended information */ |
| 87 | |
| 88 | if (bx != EDDMAGIC2) |
| 89 | return -1; |
| 90 | |
| 91 | ei->device = devno; |
| 92 | ei->version = ax >> 8; /* EDD version number */ |
| 93 | ei->interface_support = cx; /* EDD functionality subsets */ |
| 94 | |
| 95 | /* Extended Get Device Parameters */ |
| 96 | |
| 97 | ei->params.length = sizeof(ei->params); |
| 98 | ax = 0x4800; |
| 99 | dx = devno; |
| 100 | asm("pushfl; int $0x13; popfl" |
H. Peter Anvin | 463c9a9 | 2007-08-02 13:45:49 -0400 | [diff] [blame] | 101 | : "+a" (ax), "+d" (dx), "=m" (ei->params) |
H. Peter Anvin | d13444a | 2007-07-11 12:18:48 -0700 | [diff] [blame] | 102 | : "S" (&ei->params) |
| 103 | : "ebx", "ecx", "edi"); |
| 104 | |
| 105 | /* Get legacy CHS parameters */ |
| 106 | |
| 107 | /* Ralf Brown recommends setting ES:DI to 0:0 */ |
| 108 | ax = 0x0800; |
| 109 | dx = devno; |
| 110 | di = 0; |
| 111 | asm("pushw %%es; " |
| 112 | "movw %%di,%%es; " |
| 113 | "pushfl; stc; int $0x13; setc %%al; popfl; " |
| 114 | "popw %%es" |
| 115 | : "+a" (ax), "=b" (bx), "=c" (cx), "+d" (dx), "+D" (di) |
| 116 | : : "esi"); |
| 117 | |
| 118 | if ((u8)ax == 0) { |
| 119 | ei->legacy_max_cylinder = (cx >> 8) + ((cx & 0xc0) << 2); |
| 120 | ei->legacy_max_head = dx >> 8; |
| 121 | ei->legacy_sectors_per_track = cx & 0x3f; |
| 122 | } |
| 123 | |
| 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | void query_edd(void) |
| 128 | { |
| 129 | char eddarg[8]; |
| 130 | int do_mbr = 1; |
| 131 | int do_edd = 1; |
devzero@web.de | e479c83 | 2008-01-30 13:33:03 +0100 | [diff] [blame] | 132 | int be_quiet; |
H. Peter Anvin | d13444a | 2007-07-11 12:18:48 -0700 | [diff] [blame] | 133 | int devno; |
| 134 | struct edd_info ei, *edp; |
H. Peter Anvin | 9a5f35d | 2007-08-14 17:36:00 -0700 | [diff] [blame] | 135 | u32 *mbrptr; |
H. Peter Anvin | d13444a | 2007-07-11 12:18:48 -0700 | [diff] [blame] | 136 | |
| 137 | if (cmdline_find_option("edd", eddarg, sizeof eddarg) > 0) { |
| 138 | if (!strcmp(eddarg, "skipmbr") || !strcmp(eddarg, "skip")) |
| 139 | do_mbr = 0; |
| 140 | else if (!strcmp(eddarg, "off")) |
| 141 | do_edd = 0; |
| 142 | } |
| 143 | |
devzero@web.de | e479c83 | 2008-01-30 13:33:03 +0100 | [diff] [blame] | 144 | be_quiet = cmdline_find_option_bool("quiet"); |
| 145 | |
H. Peter Anvin | 9a5f35d | 2007-08-14 17:36:00 -0700 | [diff] [blame] | 146 | edp = boot_params.eddbuf; |
| 147 | mbrptr = boot_params.edd_mbr_sig_buffer; |
H. Peter Anvin | d13444a | 2007-07-11 12:18:48 -0700 | [diff] [blame] | 148 | |
| 149 | if (!do_edd) |
| 150 | return; |
| 151 | |
devzero@web.de | e479c83 | 2008-01-30 13:33:03 +0100 | [diff] [blame] | 152 | /* Bugs in OnBoard or AddOnCards Bios may hang the EDD probe, |
| 153 | * so give a hint if this happens. |
| 154 | */ |
| 155 | |
| 156 | if (!be_quiet) |
H. Peter Anvin | f777501 | 2008-01-30 13:33:03 +0100 | [diff] [blame^] | 157 | printf("Probing EDD... "); |
devzero@web.de | e479c83 | 2008-01-30 13:33:03 +0100 | [diff] [blame] | 158 | |
H. Peter Anvin | d13444a | 2007-07-11 12:18:48 -0700 | [diff] [blame] | 159 | for (devno = 0x80; devno < 0x80+EDD_MBR_SIG_MAX; devno++) { |
| 160 | /* |
| 161 | * Scan the BIOS-supported hard disks and query EDD |
| 162 | * information... |
| 163 | */ |
| 164 | get_edd_info(devno, &ei); |
| 165 | |
| 166 | if (boot_params.eddbuf_entries < EDDMAXNR) { |
| 167 | memcpy(edp, &ei, sizeof ei); |
| 168 | edp++; |
| 169 | boot_params.eddbuf_entries++; |
| 170 | } |
| 171 | |
H. Peter Anvin | 9a5f35d | 2007-08-14 17:36:00 -0700 | [diff] [blame] | 172 | if (do_mbr && !read_mbr_sig(devno, &ei, mbrptr++)) |
| 173 | boot_params.edd_mbr_sig_buf_entries = devno-0x80+1; |
H. Peter Anvin | d13444a | 2007-07-11 12:18:48 -0700 | [diff] [blame] | 174 | } |
devzero@web.de | e479c83 | 2008-01-30 13:33:03 +0100 | [diff] [blame] | 175 | |
| 176 | if (!be_quiet) |
H. Peter Anvin | f777501 | 2008-01-30 13:33:03 +0100 | [diff] [blame^] | 177 | printf("ok\n"); |
H. Peter Anvin | d13444a | 2007-07-11 12:18:48 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | #endif |