edd.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. * arch/i386/boot/edd.c
  12. *
  13. * Get EDD BIOS disk information
  14. */
  15. #include "boot.h"
  16. #include <linux/edd.h>
  17. #if defined(CONFIG_EDD) || defined(CONFIG_EDD_MODULE)
  18. struct edd_dapa {
  19. u8 pkt_size;
  20. u8 rsvd;
  21. u16 sector_cnt;
  22. u16 buf_off, buf_seg;
  23. u64 lba;
  24. u64 buf_lin_addr;
  25. };
  26. /*
  27. * Read the MBR (first sector) from a specific device.
  28. */
  29. static int read_mbr(u8 devno, void *buf)
  30. {
  31. struct edd_dapa dapa;
  32. u16 ax, bx, cx, dx, si;
  33. memset(&dapa, 0, sizeof dapa);
  34. dapa.pkt_size = sizeof(dapa);
  35. dapa.sector_cnt = 1;
  36. dapa.buf_off = (size_t)buf;
  37. dapa.buf_seg = ds();
  38. /* dapa.lba = 0; */
  39. ax = 0x4200; /* Extended Read */
  40. si = (size_t)&dapa;
  41. dx = devno;
  42. asm("pushfl; stc; int $0x13; setc %%al; popfl"
  43. : "+a" (ax), "+S" (si), "+d" (dx)
  44. : "m" (dapa)
  45. : "ebx", "ecx", "edi", "memory");
  46. if (!(u8)ax)
  47. return 0; /* OK */
  48. ax = 0x0201; /* Legacy Read, one sector */
  49. cx = 0x0001; /* Sector 0-0-1 */
  50. dx = devno;
  51. bx = (size_t)buf;
  52. asm("pushfl; stc; int $0x13; setc %%al; popfl"
  53. : "+a" (ax), "+c" (cx), "+d" (dx), "+b" (bx)
  54. : : "esi", "edi", "memory");
  55. return -(u8)ax; /* 0 or -1 */
  56. }
  57. static u32 read_mbr_sig(u8 devno, struct edd_info *ei)
  58. {
  59. int sector_size;
  60. char *mbrbuf_ptr, *mbrbuf_end;
  61. u32 mbrsig;
  62. u32 buf_base, mbr_base;
  63. extern char _end[];
  64. static char mbr_buf[1024];
  65. sector_size = ei->params.bytes_per_sector;
  66. if (!sector_size)
  67. sector_size = 512; /* Best available guess */
  68. buf_base = (ds() << 4) + (u32)&_end;
  69. mbr_base = (buf_base+sector_size-1) & ~(sector_size-1);
  70. mbrbuf_ptr = mbr_buf + (mbr_base-buf_base);
  71. mbrbuf_end = mbrbuf_ptr + sector_size;
  72. if (!(boot_params.hdr.loadflags & CAN_USE_HEAP))
  73. return 0;
  74. if (mbrbuf_end > (char *)(size_t)boot_params.hdr.heap_end_ptr)
  75. return 0;
  76. if (read_mbr(devno, mbrbuf_ptr))
  77. return 0;
  78. mbrsig = *(u32 *)&mbrbuf_ptr[EDD_MBR_SIG_OFFSET];
  79. return mbrsig;
  80. }
  81. static int get_edd_info(u8 devno, struct edd_info *ei)
  82. {
  83. u16 ax, bx, cx, dx, di;
  84. memset(ei, 0, sizeof *ei);
  85. /* Check Extensions Present */
  86. ax = 0x4100;
  87. bx = EDDMAGIC1;
  88. dx = devno;
  89. asm("pushfl; stc; int $0x13; setc %%al; popfl"
  90. : "+a" (ax), "+b" (bx), "=c" (cx), "+d" (dx)
  91. : : "esi", "edi");
  92. if ((u8)ax)
  93. return -1; /* No extended information */
  94. if (bx != EDDMAGIC2)
  95. return -1;
  96. ei->device = devno;
  97. ei->version = ax >> 8; /* EDD version number */
  98. ei->interface_support = cx; /* EDD functionality subsets */
  99. /* Extended Get Device Parameters */
  100. ei->params.length = sizeof(ei->params);
  101. ax = 0x4800;
  102. dx = devno;
  103. asm("pushfl; int $0x13; popfl"
  104. : "+a" (ax), "+d" (dx)
  105. : "S" (&ei->params)
  106. : "ebx", "ecx", "edi");
  107. /* Get legacy CHS parameters */
  108. /* Ralf Brown recommends setting ES:DI to 0:0 */
  109. ax = 0x0800;
  110. dx = devno;
  111. di = 0;
  112. asm("pushw %%es; "
  113. "movw %%di,%%es; "
  114. "pushfl; stc; int $0x13; setc %%al; popfl; "
  115. "popw %%es"
  116. : "+a" (ax), "=b" (bx), "=c" (cx), "+d" (dx), "+D" (di)
  117. : : "esi");
  118. if ((u8)ax == 0) {
  119. ei->legacy_max_cylinder = (cx >> 8) + ((cx & 0xc0) << 2);
  120. ei->legacy_max_head = dx >> 8;
  121. ei->legacy_sectors_per_track = cx & 0x3f;
  122. }
  123. return 0;
  124. }
  125. void query_edd(void)
  126. {
  127. char eddarg[8];
  128. int do_mbr = 1;
  129. int do_edd = 1;
  130. int devno;
  131. struct edd_info ei, *edp;
  132. if (cmdline_find_option("edd", eddarg, sizeof eddarg) > 0) {
  133. if (!strcmp(eddarg, "skipmbr") || !strcmp(eddarg, "skip"))
  134. do_mbr = 0;
  135. else if (!strcmp(eddarg, "off"))
  136. do_edd = 0;
  137. }
  138. edp = (struct edd_info *)boot_params.eddbuf;
  139. if (!do_edd)
  140. return;
  141. for (devno = 0x80; devno < 0x80+EDD_MBR_SIG_MAX; devno++) {
  142. /*
  143. * Scan the BIOS-supported hard disks and query EDD
  144. * information...
  145. */
  146. get_edd_info(devno, &ei);
  147. if (boot_params.eddbuf_entries < EDDMAXNR) {
  148. memcpy(edp, &ei, sizeof ei);
  149. edp++;
  150. boot_params.eddbuf_entries++;
  151. }
  152. if (do_mbr) {
  153. u32 mbr_sig;
  154. mbr_sig = read_mbr_sig(devno, &ei);
  155. boot_params.edd_mbr_sig_buffer[devno-0x80] = mbr_sig;
  156. }
  157. }
  158. }
  159. #endif