1 /* -*- linux-c -*- ------------------------------------------------------- *
3 * Copyright (C) 1991, 1992 Linus Torvalds
4 * Copyright 2007 rPath, Inc. - All Rights Reserved
5 * Copyright 2009 Intel Corporation; author H. Peter Anvin
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.
10 * ----------------------------------------------------------------------- */
13 * Get EDD BIOS disk information
17 #include <linux/edd.h>
19 #if defined(CONFIG_EDD) || defined(CONFIG_EDD_MODULE)
22 * Read the MBR (first sector) from a specific device.
24 static int read_mbr(u8 devno, void *buf)
26 struct biosregs ireg, oreg;
29 ireg.ax = 0x0201; /* Legacy Read, one sector */
30 ireg.cx = 0x0001; /* Sector 0-0-1 */
32 ireg.bx = (size_t)buf;
34 intcall(0x13, &ireg, &oreg);
36 return -(oreg.eflags & X86_EFLAGS_CF); /* 0 or -1 */
39 static u32 read_mbr_sig(u8 devno, struct edd_info *ei, u32 *mbrsig)
42 char *mbrbuf_ptr, *mbrbuf_end;
43 u32 buf_base, mbr_base;
47 sector_size = ei->params.bytes_per_sector;
49 sector_size = 512; /* Best available guess */
51 /* Produce a naturally aligned buffer on the heap */
52 buf_base = (ds() << 4) + (u32)&_end;
53 mbr_base = (buf_base+sector_size-1) & ~(sector_size-1);
54 mbrbuf_ptr = _end + (mbr_base-buf_base);
55 mbrbuf_end = mbrbuf_ptr + sector_size;
57 /* Make sure we actually have space on the heap... */
58 if (!(boot_params.hdr.loadflags & CAN_USE_HEAP))
60 if (mbrbuf_end > (char *)(size_t)boot_params.hdr.heap_end_ptr)
63 memset(mbrbuf_ptr, 0, sector_size);
64 if (read_mbr(devno, mbrbuf_ptr))
67 *mbrsig = *(u32 *)&mbrbuf_ptr[EDD_MBR_SIG_OFFSET];
68 mbr_magic = *(u16 *)&mbrbuf_ptr[510];
70 /* check for valid MBR magic */
71 return mbr_magic == 0xAA55 ? 0 : -1;
74 static int get_edd_info(u8 devno, struct edd_info *ei)
76 struct biosregs ireg, oreg;
78 memset(ei, 0, sizeof *ei);
80 /* Check Extensions Present */
86 intcall(0x13, &ireg, &oreg);
88 if (oreg.eflags & X86_EFLAGS_CF)
89 return -1; /* No extended information */
91 if (oreg.bx != EDDMAGIC2)
95 ei->version = oreg.ah; /* EDD version number */
96 ei->interface_support = oreg.cx; /* EDD functionality subsets */
98 /* Extended Get Device Parameters */
100 ei->params.length = sizeof(ei->params);
102 ireg.si = (size_t)&ei->params;
103 intcall(0x13, &ireg, &oreg);
105 /* Get legacy CHS parameters */
107 /* Ralf Brown recommends setting ES:DI to 0:0 */
110 intcall(0x13, &ireg, &oreg);
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;
125 #ifdef CONFIG_EDD_OFF
132 struct edd_info ei, *edp;
135 if (cmdline_find_option("edd", eddarg, sizeof eddarg) > 0) {
136 if (!strcmp(eddarg, "skipmbr") || !strcmp(eddarg, "skip")) {
140 else if (!strcmp(eddarg, "off"))
142 else if (!strcmp(eddarg, "on"))
146 be_quiet = cmdline_find_option_bool("quiet");
148 edp = boot_params.eddbuf;
149 mbrptr = boot_params.edd_mbr_sig_buffer;
154 /* Bugs in OnBoard or AddOnCards Bios may hang the EDD probe,
155 * so give a hint if this happens.
159 printf("Probing EDD (edd=off to disable)... ");
161 for (devno = 0x80; devno < 0x80+EDD_MBR_SIG_MAX; devno++) {
163 * Scan the BIOS-supported hard disks and query EDD
166 if (!get_edd_info(devno, &ei)
167 && boot_params.eddbuf_entries < EDDMAXNR) {
168 memcpy(edp, &ei, sizeof ei);
170 boot_params.eddbuf_entries++;
173 if (do_mbr && !read_mbr_sig(devno, &ei, mbrptr++))
174 boot_params.edd_mbr_sig_buf_entries = devno-0x80+1;