1 /* -*- linux-c -*- ------------------------------------------------------- *
3 * Copyright (C) 1991, 1992 Linus Torvalds
4 * Copyright 2007 rPath, Inc. - All Rights Reserved
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.
9 * ----------------------------------------------------------------------- */
12 * Get EDD BIOS disk information
16 #include <linux/edd.h>
18 #if defined(CONFIG_EDD) || defined(CONFIG_EDD_MODULE)
21 * Read the MBR (first sector) from a specific device.
23 static int read_mbr(u8 devno, void *buf)
27 ax = 0x0201; /* Legacy Read, one sector */
28 cx = 0x0001; /* Sector 0-0-1 */
31 asm volatile("pushfl; stc; int $0x13; setc %%al; popfl"
32 : "+a" (ax), "+c" (cx), "+d" (dx), "+b" (bx)
33 : : "esi", "edi", "memory");
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 */
40 static u32 read_mbr_sig(u8 devno, struct edd_info *ei, u32 *mbrsig)
43 char *mbrbuf_ptr, *mbrbuf_end;
44 u32 buf_base, mbr_base;
48 sector_size = ei->params.bytes_per_sector;
50 sector_size = 512; /* Best available guess */
52 /* Produce a naturally aligned buffer on the heap */
53 buf_base = (ds() << 4) + (u32)&_end;
54 mbr_base = (buf_base+sector_size-1) & ~(sector_size-1);
55 mbrbuf_ptr = _end + (mbr_base-buf_base);
56 mbrbuf_end = mbrbuf_ptr + sector_size;
58 /* Make sure we actually have space on the heap... */
59 if (!(boot_params.hdr.loadflags & CAN_USE_HEAP))
61 if (mbrbuf_end > (char *)(size_t)boot_params.hdr.heap_end_ptr)
64 memset(mbrbuf_ptr, 0, sector_size);
65 if (read_mbr(devno, mbrbuf_ptr))
68 *mbrsig = *(u32 *)&mbrbuf_ptr[EDD_MBR_SIG_OFFSET];
69 mbr_magic = *(u16 *)&mbrbuf_ptr[510];
71 /* check for valid MBR magic */
72 return mbr_magic == 0xAA55 ? 0 : -1;
75 static int get_edd_info(u8 devno, struct edd_info *ei)
77 u16 ax, bx, cx, dx, di;
79 memset(ei, 0, sizeof *ei);
81 /* Check Extensions Present */
86 asm("pushfl; stc; int $0x13; setc %%al; popfl"
87 : "+a" (ax), "+b" (bx), "=c" (cx), "+d" (dx)
91 return -1; /* No extended information */
97 ei->version = ax >> 8; /* EDD version number */
98 ei->interface_support = cx; /* EDD functionality subsets */
100 /* Extended Get Device Parameters */
102 ei->params.length = sizeof(ei->params);
105 asm("pushfl; int $0x13; popfl"
106 : "+a" (ax), "+d" (dx), "=m" (ei->params)
108 : "ebx", "ecx", "edi");
110 /* Get legacy CHS parameters */
112 /* Ralf Brown recommends setting ES:DI to 0:0 */
118 "pushfl; stc; int $0x13; setc %%al; popfl; "
120 : "+a" (ax), "=b" (bx), "=c" (cx), "+d" (dx), "+D" (di)
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;
136 #ifdef CONFIG_EDD_OFF
143 struct edd_info ei, *edp;
146 if (cmdline_find_option("edd", eddarg, sizeof eddarg) > 0) {
147 if (!strcmp(eddarg, "skipmbr") || !strcmp(eddarg, "skip")) {
151 else if (!strcmp(eddarg, "off"))
153 else if (!strcmp(eddarg, "on"))
157 be_quiet = cmdline_find_option_bool("quiet");
159 edp = boot_params.eddbuf;
160 mbrptr = boot_params.edd_mbr_sig_buffer;
165 /* Bugs in OnBoard or AddOnCards Bios may hang the EDD probe,
166 * so give a hint if this happens.
170 printf("Probing EDD (edd=off to disable)... ");
172 for (devno = 0x80; devno < 0x80+EDD_MBR_SIG_MAX; devno++) {
174 * Scan the BIOS-supported hard disks and query EDD
177 if (!get_edd_info(devno, &ei)
178 && boot_params.eddbuf_entries < EDDMAXNR) {
179 memcpy(edp, &ei, sizeof ei);
181 boot_params.eddbuf_entries++;
184 if (do_mbr && !read_mbr_sig(devno, &ei, mbrptr++))
185 boot_params.edd_mbr_sig_buf_entries = devno-0x80+1;