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