edd.c 4.3 KB

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