1 // SPDX-License-Identifier: GPL-2.0-only
2 /* -*- linux-c -*- ------------------------------------------------------- *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 * Copyright 2007 rPath, Inc. - All Rights Reserved
6 * Copyright 2009 Intel Corporation; author H. Peter Anvin
8 * ----------------------------------------------------------------------- */
11 * Get EDD BIOS disk information
15 #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
)
25 struct biosregs ireg
, oreg
;
28 ireg
.ax
= 0x0201; /* Legacy Read, one sector */
29 ireg
.cx
= 0x0001; /* Sector 0-0-1 */
31 ireg
.bx
= (size_t)buf
;
33 intcall(0x13, &ireg
, &oreg
);
35 return -(oreg
.eflags
& X86_EFLAGS_CF
); /* 0 or -1 */
38 static u32
read_mbr_sig(u8 devno
, struct edd_info
*ei
, u32
*mbrsig
)
41 char *mbrbuf_ptr
, *mbrbuf_end
;
42 u32 buf_base
, mbr_base
;
46 sector_size
= ei
->params
.bytes_per_sector
;
48 sector_size
= 512; /* Best available guess */
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
;
56 /* Make sure we actually have space on the heap... */
57 if (!(boot_params
.hdr
.loadflags
& CAN_USE_HEAP
))
59 if (mbrbuf_end
> (char *)(size_t)boot_params
.hdr
.heap_end_ptr
)
62 memset(mbrbuf_ptr
, 0, sector_size
);
63 if (read_mbr(devno
, mbrbuf_ptr
))
66 *mbrsig
= *(u32
*)&mbrbuf_ptr
[EDD_MBR_SIG_OFFSET
];
67 mbr_magic
= *(u16
*)&mbrbuf_ptr
[510];
69 /* check for valid MBR magic */
70 return mbr_magic
== 0xAA55 ? 0 : -1;
73 static int get_edd_info(u8 devno
, struct edd_info
*ei
)
75 struct biosregs ireg
, oreg
;
77 memset(ei
, 0, sizeof(*ei
));
79 /* Check Extensions Present */
85 intcall(0x13, &ireg
, &oreg
);
87 if (oreg
.eflags
& X86_EFLAGS_CF
)
88 return -1; /* No extended information */
90 if (oreg
.bx
!= EDDMAGIC2
)
94 ei
->version
= oreg
.ah
; /* EDD version number */
95 ei
->interface_support
= oreg
.cx
; /* EDD functionality subsets */
97 /* Extended Get Device Parameters */
99 ei
->params
.length
= sizeof(ei
->params
);
101 ireg
.si
= (size_t)&ei
->params
;
102 intcall(0x13, &ireg
, &oreg
);
104 /* Get legacy CHS parameters */
106 /* Ralf Brown recommends setting ES:DI to 0:0 */
109 intcall(0x13, &ireg
, &oreg
);
111 if (!(oreg
.eflags
& X86_EFLAGS_CF
)) {
112 ei
->legacy_max_cylinder
= oreg
.ch
+ ((oreg
.cl
& 0xc0) << 2);
113 ei
->legacy_max_head
= oreg
.dh
;
114 ei
->legacy_sectors_per_track
= oreg
.cl
& 0x3f;
124 #ifdef CONFIG_EDD_OFF
131 struct edd_info ei
, *edp
;
134 if (cmdline_find_option("edd", eddarg
, sizeof(eddarg
)) > 0) {
135 if (!strcmp(eddarg
, "skipmbr") || !strcmp(eddarg
, "skip")) {
139 else if (!strcmp(eddarg
, "off"))
141 else if (!strcmp(eddarg
, "on"))
145 be_quiet
= cmdline_find_option_bool("quiet");
147 edp
= boot_params
.eddbuf
;
148 mbrptr
= boot_params
.edd_mbr_sig_buffer
;
153 /* Bugs in OnBoard or AddOnCards Bios may hang the EDD probe,
154 * so give a hint if this happens.
158 printf("Probing EDD (edd=off to disable)... ");
160 for (devno
= 0x80; devno
< 0x80+EDD_MBR_SIG_MAX
; devno
++) {
162 * Scan the BIOS-supported hard disks and query EDD
165 if (!get_edd_info(devno
, &ei
)
166 && boot_params
.eddbuf_entries
< EDDMAXNR
) {
167 memcpy(edp
, &ei
, sizeof(ei
));
169 boot_params
.eddbuf_entries
++;
172 if (do_mbr
&& !read_mbr_sig(devno
, &ei
, mbrptr
++))
173 boot_params
.edd_mbr_sig_buf_entries
= devno
-0x80+1;