edd.c 3.8 KB

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