blob: 223e42527077d26c818d7e0ec7259d98717dbd61 [file] [log] [blame]
H. Peter Anvind13444a2007-07-11 12:18:48 -07001/* -*- linux-c -*- ------------------------------------------------------- *
2 *
3 * Copyright (C) 1991, 1992 Linus Torvalds
4 * Copyright 2007 rPath, Inc. - All Rights Reserved
H. Peter Anvin3435d342009-04-01 18:17:17 -07005 * Copyright 2009 Intel Corporation; author H. Peter Anvin
H. Peter Anvind13444a2007-07-11 12:18:48 -07006 *
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 Anvind13444a2007-07-11 12:18:48 -070013 * Get EDD BIOS disk information
14 */
15
16#include "boot.h"
17#include <linux/edd.h>
Vivek Goyalc041b5a2014-03-18 15:26:37 -040018#include "string.h"
H. Peter Anvind13444a2007-07-11 12:18:48 -070019
20#if defined(CONFIG_EDD) || defined(CONFIG_EDD_MODULE)
21
H. Peter Anvind13444a2007-07-11 12:18:48 -070022/*
23 * Read the MBR (first sector) from a specific device.
24 */
25static int read_mbr(u8 devno, void *buf)
26{
H. Peter Anvin3435d342009-04-01 18:17:17 -070027 struct biosregs ireg, oreg;
H. Peter Anvind13444a2007-07-11 12:18:48 -070028
H. Peter Anvin3435d342009-04-01 18:17:17 -070029 initregs(&ireg);
30 ireg.ax = 0x0201; /* Legacy Read, one sector */
31 ireg.cx = 0x0001; /* Sector 0-0-1 */
32 ireg.dl = devno;
33 ireg.bx = (size_t)buf;
H. Peter Anvind13444a2007-07-11 12:18:48 -070034
H. Peter Anvin3435d342009-04-01 18:17:17 -070035 intcall(0x13, &ireg, &oreg);
36
37 return -(oreg.eflags & X86_EFLAGS_CF); /* 0 or -1 */
H. Peter Anvind13444a2007-07-11 12:18:48 -070038}
39
H. Peter Anvin9a5f35d2007-08-14 17:36:00 -070040static u32 read_mbr_sig(u8 devno, struct edd_info *ei, u32 *mbrsig)
H. Peter Anvind13444a2007-07-11 12:18:48 -070041{
42 int sector_size;
43 char *mbrbuf_ptr, *mbrbuf_end;
H. Peter Anvind13444a2007-07-11 12:18:48 -070044 u32 buf_base, mbr_base;
45 extern char _end[];
Andrey Borzenkov6cdcdb92008-10-03 21:08:49 +040046 u16 mbr_magic;
H. Peter Anvind13444a2007-07-11 12:18:48 -070047
48 sector_size = ei->params.bytes_per_sector;
49 if (!sector_size)
50 sector_size = 512; /* Best available guess */
51
H. Peter Anvinff659d12007-07-31 16:17:13 -040052 /* Produce a naturally aligned buffer on the heap */
H. Peter Anvind13444a2007-07-11 12:18:48 -070053 buf_base = (ds() << 4) + (u32)&_end;
54 mbr_base = (buf_base+sector_size-1) & ~(sector_size-1);
H. Peter Anvinff659d12007-07-31 16:17:13 -040055 mbrbuf_ptr = _end + (mbr_base-buf_base);
H. Peter Anvind13444a2007-07-11 12:18:48 -070056 mbrbuf_end = mbrbuf_ptr + sector_size;
57
H. Peter Anvinff659d12007-07-31 16:17:13 -040058 /* Make sure we actually have space on the heap... */
H. Peter Anvind13444a2007-07-11 12:18:48 -070059 if (!(boot_params.hdr.loadflags & CAN_USE_HEAP))
H. Peter Anvin9a5f35d2007-08-14 17:36:00 -070060 return -1;
H. Peter Anvind13444a2007-07-11 12:18:48 -070061 if (mbrbuf_end > (char *)(size_t)boot_params.hdr.heap_end_ptr)
H. Peter Anvin9a5f35d2007-08-14 17:36:00 -070062 return -1;
H. Peter Anvind13444a2007-07-11 12:18:48 -070063
Andrey Borzenkov6cdcdb92008-10-03 21:08:49 +040064 memset(mbrbuf_ptr, 0, sector_size);
H. Peter Anvind13444a2007-07-11 12:18:48 -070065 if (read_mbr(devno, mbrbuf_ptr))
H. Peter Anvin9a5f35d2007-08-14 17:36:00 -070066 return -1;
H. Peter Anvind13444a2007-07-11 12:18:48 -070067
H. Peter Anvin9a5f35d2007-08-14 17:36:00 -070068 *mbrsig = *(u32 *)&mbrbuf_ptr[EDD_MBR_SIG_OFFSET];
Andrey Borzenkov6cdcdb92008-10-03 21:08:49 +040069 mbr_magic = *(u16 *)&mbrbuf_ptr[510];
70
71 /* check for valid MBR magic */
72 return mbr_magic == 0xAA55 ? 0 : -1;
H. Peter Anvind13444a2007-07-11 12:18:48 -070073}
74
75static int get_edd_info(u8 devno, struct edd_info *ei)
76{
H. Peter Anvin3435d342009-04-01 18:17:17 -070077 struct biosregs ireg, oreg;
H. Peter Anvind13444a2007-07-11 12:18:48 -070078
79 memset(ei, 0, sizeof *ei);
80
81 /* Check Extensions Present */
82
H. Peter Anvin3435d342009-04-01 18:17:17 -070083 initregs(&ireg);
84 ireg.ah = 0x41;
85 ireg.bx = EDDMAGIC1;
86 ireg.dl = devno;
87 intcall(0x13, &ireg, &oreg);
H. Peter Anvind13444a2007-07-11 12:18:48 -070088
H. Peter Anvin3435d342009-04-01 18:17:17 -070089 if (oreg.eflags & X86_EFLAGS_CF)
H. Peter Anvind13444a2007-07-11 12:18:48 -070090 return -1; /* No extended information */
91
H. Peter Anvin3435d342009-04-01 18:17:17 -070092 if (oreg.bx != EDDMAGIC2)
H. Peter Anvind13444a2007-07-11 12:18:48 -070093 return -1;
94
95 ei->device = devno;
H. Peter Anvin3435d342009-04-01 18:17:17 -070096 ei->version = oreg.ah; /* EDD version number */
97 ei->interface_support = oreg.cx; /* EDD functionality subsets */
H. Peter Anvind13444a2007-07-11 12:18:48 -070098
99 /* Extended Get Device Parameters */
100
101 ei->params.length = sizeof(ei->params);
H. Peter Anvin3435d342009-04-01 18:17:17 -0700102 ireg.ah = 0x48;
103 ireg.si = (size_t)&ei->params;
104 intcall(0x13, &ireg, &oreg);
H. Peter Anvind13444a2007-07-11 12:18:48 -0700105
106 /* Get legacy CHS parameters */
107
108 /* Ralf Brown recommends setting ES:DI to 0:0 */
H. Peter Anvin3435d342009-04-01 18:17:17 -0700109 ireg.ah = 0x08;
110 ireg.es = 0;
111 intcall(0x13, &ireg, &oreg);
H. Peter Anvind13444a2007-07-11 12:18:48 -0700112
H. Peter Anvin3435d342009-04-01 18:17:17 -0700113 if (!(oreg.eflags & X86_EFLAGS_CF)) {
114 ei->legacy_max_cylinder = oreg.ch + ((oreg.cl & 0xc0) << 2);
115 ei->legacy_max_head = oreg.dh;
116 ei->legacy_sectors_per_track = oreg.cl & 0x3f;
H. Peter Anvind13444a2007-07-11 12:18:48 -0700117 }
118
119 return 0;
120}
121
122void query_edd(void)
123{
124 char eddarg[8];
125 int do_mbr = 1;
Tim Gardner8c4dd602008-04-29 01:02:45 -0700126#ifdef CONFIG_EDD_OFF
127 int do_edd = 0;
128#else
H. Peter Anvind13444a2007-07-11 12:18:48 -0700129 int do_edd = 1;
Tim Gardner8c4dd602008-04-29 01:02:45 -0700130#endif
devzero@web.dee479c832008-01-30 13:33:03 +0100131 int be_quiet;
H. Peter Anvind13444a2007-07-11 12:18:48 -0700132 int devno;
133 struct edd_info ei, *edp;
H. Peter Anvin9a5f35d2007-08-14 17:36:00 -0700134 u32 *mbrptr;
H. Peter Anvind13444a2007-07-11 12:18:48 -0700135
136 if (cmdline_find_option("edd", eddarg, sizeof eddarg) > 0) {
Tim Gardner8c4dd602008-04-29 01:02:45 -0700137 if (!strcmp(eddarg, "skipmbr") || !strcmp(eddarg, "skip")) {
138 do_edd = 1;
H. Peter Anvind13444a2007-07-11 12:18:48 -0700139 do_mbr = 0;
Tim Gardner8c4dd602008-04-29 01:02:45 -0700140 }
H. Peter Anvind13444a2007-07-11 12:18:48 -0700141 else if (!strcmp(eddarg, "off"))
142 do_edd = 0;
Tim Gardner8c4dd602008-04-29 01:02:45 -0700143 else if (!strcmp(eddarg, "on"))
144 do_edd = 1;
H. Peter Anvind13444a2007-07-11 12:18:48 -0700145 }
146
devzero@web.dee479c832008-01-30 13:33:03 +0100147 be_quiet = cmdline_find_option_bool("quiet");
148
H. Peter Anvin9a5f35d2007-08-14 17:36:00 -0700149 edp = boot_params.eddbuf;
150 mbrptr = boot_params.edd_mbr_sig_buffer;
H. Peter Anvind13444a2007-07-11 12:18:48 -0700151
152 if (!do_edd)
153 return;
154
devzero@web.dee479c832008-01-30 13:33:03 +0100155 /* Bugs in OnBoard or AddOnCards Bios may hang the EDD probe,
156 * so give a hint if this happens.
157 */
158
159 if (!be_quiet)
H. Peter Anvinb7107632008-01-30 13:33:03 +0100160 printf("Probing EDD (edd=off to disable)... ");
devzero@web.dee479c832008-01-30 13:33:03 +0100161
H. Peter Anvind13444a2007-07-11 12:18:48 -0700162 for (devno = 0x80; devno < 0x80+EDD_MBR_SIG_MAX; devno++) {
163 /*
164 * Scan the BIOS-supported hard disks and query EDD
165 * information...
166 */
Jan Beulichf2ba9392008-07-18 13:35:37 +0100167 if (!get_edd_info(devno, &ei)
168 && boot_params.eddbuf_entries < EDDMAXNR) {
H. Peter Anvind13444a2007-07-11 12:18:48 -0700169 memcpy(edp, &ei, sizeof ei);
170 edp++;
171 boot_params.eddbuf_entries++;
172 }
173
H. Peter Anvin9a5f35d2007-08-14 17:36:00 -0700174 if (do_mbr && !read_mbr_sig(devno, &ei, mbrptr++))
175 boot_params.edd_mbr_sig_buf_entries = devno-0x80+1;
H. Peter Anvind13444a2007-07-11 12:18:48 -0700176 }
devzero@web.dee479c832008-01-30 13:33:03 +0100177
178 if (!be_quiet)
H. Peter Anvinf7775012008-01-30 13:33:03 +0100179 printf("ok\n");
H. Peter Anvind13444a2007-07-11 12:18:48 -0700180}
181
182#endif