edd.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. sector_size = ei->params.bytes_per_sector;
  65. if (!sector_size)
  66. sector_size = 512; /* Best available guess */
  67. /* Produce a naturally aligned buffer on the heap */
  68. buf_base = (ds() << 4) + (u32)&_end;
  69. mbr_base = (buf_base+sector_size-1) & ~(sector_size-1);
  70. mbrbuf_ptr = _end + (mbr_base-buf_base);
  71. mbrbuf_end = mbrbuf_ptr + sector_size;
  72. /* Make sure we actually have space on the heap... */
  73. if (!(boot_params.hdr.loadflags & CAN_USE_HEAP))
  74. return 0;
  75. if (mbrbuf_end > (char *)(size_t)boot_params.hdr.heap_end_ptr)
  76. return 0;
  77. if (read_mbr(devno, mbrbuf_ptr))
  78. return 0;
  79. mbrsig = *(u32 *)&mbrbuf_ptr[EDD_MBR_SIG_OFFSET];
  80. return mbrsig;
  81. }
  82. static int get_edd_info(u8 devno, struct edd_info *ei)
  83. {
  84. u16 ax, bx, cx, dx, di;
  85. memset(ei, 0, sizeof *ei);
  86. /* Check Extensions Present */
  87. ax = 0x4100;
  88. bx = EDDMAGIC1;
  89. dx = devno;
  90. asm("pushfl; stc; int $0x13; setc %%al; popfl"
  91. : "+a" (ax), "+b" (bx), "=c" (cx), "+d" (dx)
  92. : : "esi", "edi");
  93. if ((u8)ax)
  94. return -1; /* No extended information */
  95. if (bx != EDDMAGIC2)
  96. return -1;
  97. ei->device = devno;
  98. ei->version = ax >> 8; /* EDD version number */
  99. ei->interface_support = cx; /* EDD functionality subsets */
  100. /* Extended Get Device Parameters */
  101. ei->params.length = sizeof(ei->params);
  102. ax = 0x4800;
  103. dx = devno;
  104. asm("pushfl; int $0x13; popfl"
  105. : "+a" (ax), "+d" (dx), "=m" (ei->params)
  106. : "S" (&ei->params)
  107. : "ebx", "ecx", "edi");
  108. /* Get legacy CHS parameters */
  109. /* Ralf Brown recommends setting ES:DI to 0:0 */
  110. ax = 0x0800;
  111. dx = devno;
  112. di = 0;
  113. asm("pushw %%es; "
  114. "movw %%di,%%es; "
  115. "pushfl; stc; int $0x13; setc %%al; popfl; "
  116. "popw %%es"
  117. : "+a" (ax), "=b" (bx), "=c" (cx), "+d" (dx), "+D" (di)
  118. : : "esi");
  119. if ((u8)ax == 0) {
  120. ei->legacy_max_cylinder = (cx >> 8) + ((cx & 0xc0) << 2);
  121. ei->legacy_max_head = dx >> 8;
  122. ei->legacy_sectors_per_track = cx & 0x3f;
  123. }
  124. return 0;
  125. }
  126. void query_edd(void)
  127. {
  128. char eddarg[8];
  129. int do_mbr = 1;
  130. int do_edd = 1;
  131. int devno;
  132. struct edd_info ei, *edp;
  133. if (cmdline_find_option("edd", eddarg, sizeof eddarg) > 0) {
  134. if (!strcmp(eddarg, "skipmbr") || !strcmp(eddarg, "skip"))
  135. do_mbr = 0;
  136. else if (!strcmp(eddarg, "off"))
  137. do_edd = 0;
  138. }
  139. edp = (struct edd_info *)boot_params.eddbuf;
  140. if (!do_edd)
  141. return;
  142. for (devno = 0x80; devno < 0x80+EDD_MBR_SIG_MAX; devno++) {
  143. /*
  144. * Scan the BIOS-supported hard disks and query EDD
  145. * information...
  146. */
  147. get_edd_info(devno, &ei);
  148. if (boot_params.eddbuf_entries < EDDMAXNR) {
  149. memcpy(edp, &ei, sizeof ei);
  150. edp++;
  151. boot_params.eddbuf_entries++;
  152. }
  153. if (do_mbr) {
  154. u32 mbr_sig;
  155. mbr_sig = read_mbr_sig(devno, &ei);
  156. boot_params.edd_mbr_sig_buffer[devno-0x80] = mbr_sig;
  157. }
  158. }
  159. }
  160. #endif