edd.c 4.2 KB

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