edd.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /* -*- linux-c -*- ------------------------------------------------------- *
  2. *
  3. * Copyright (C) 1991, 1992 Linus Torvalds
  4. * Copyright 2007 rPath, Inc. - All Rights Reserved
  5. * Copyright 2009 Intel Corporation; author H. Peter Anvin
  6. *
  7. * This file is part of the Linux kernel, and is made available under
  8. * the terms of the GNU General Public License version 2.
  9. *
  10. * ----------------------------------------------------------------------- */
  11. /*
  12. * Get EDD BIOS disk information
  13. */
  14. #include "boot.h"
  15. #include <linux/edd.h>
  16. #include "string.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. struct biosregs ireg, oreg;
  24. initregs(&ireg);
  25. ireg.ax = 0x0201; /* Legacy Read, one sector */
  26. ireg.cx = 0x0001; /* Sector 0-0-1 */
  27. ireg.dl = devno;
  28. ireg.bx = (size_t)buf;
  29. intcall(0x13, &ireg, &oreg);
  30. return -(oreg.eflags & X86_EFLAGS_CF); /* 0 or -1 */
  31. }
  32. static u32 read_mbr_sig(u8 devno, struct edd_info *ei, u32 *mbrsig)
  33. {
  34. int sector_size;
  35. char *mbrbuf_ptr, *mbrbuf_end;
  36. u32 buf_base, mbr_base;
  37. extern char _end[];
  38. u16 mbr_magic;
  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. memset(mbrbuf_ptr, 0, sector_size);
  53. if (read_mbr(devno, mbrbuf_ptr))
  54. return -1;
  55. *mbrsig = *(u32 *)&mbrbuf_ptr[EDD_MBR_SIG_OFFSET];
  56. mbr_magic = *(u16 *)&mbrbuf_ptr[510];
  57. /* check for valid MBR magic */
  58. return mbr_magic == 0xAA55 ? 0 : -1;
  59. }
  60. static int get_edd_info(u8 devno, struct edd_info *ei)
  61. {
  62. struct biosregs ireg, oreg;
  63. memset(ei, 0, sizeof *ei);
  64. /* Check Extensions Present */
  65. initregs(&ireg);
  66. ireg.ah = 0x41;
  67. ireg.bx = EDDMAGIC1;
  68. ireg.dl = devno;
  69. intcall(0x13, &ireg, &oreg);
  70. if (oreg.eflags & X86_EFLAGS_CF)
  71. return -1; /* No extended information */
  72. if (oreg.bx != EDDMAGIC2)
  73. return -1;
  74. ei->device = devno;
  75. ei->version = oreg.ah; /* EDD version number */
  76. ei->interface_support = oreg.cx; /* EDD functionality subsets */
  77. /* Extended Get Device Parameters */
  78. ei->params.length = sizeof(ei->params);
  79. ireg.ah = 0x48;
  80. ireg.si = (size_t)&ei->params;
  81. intcall(0x13, &ireg, &oreg);
  82. /* Get legacy CHS parameters */
  83. /* Ralf Brown recommends setting ES:DI to 0:0 */
  84. ireg.ah = 0x08;
  85. ireg.es = 0;
  86. intcall(0x13, &ireg, &oreg);
  87. if (!(oreg.eflags & X86_EFLAGS_CF)) {
  88. ei->legacy_max_cylinder = oreg.ch + ((oreg.cl & 0xc0) << 2);
  89. ei->legacy_max_head = oreg.dh;
  90. ei->legacy_sectors_per_track = oreg.cl & 0x3f;
  91. }
  92. return 0;
  93. }
  94. void query_edd(void)
  95. {
  96. char eddarg[8];
  97. int do_mbr = 1;
  98. #ifdef CONFIG_EDD_OFF
  99. int do_edd = 0;
  100. #else
  101. int do_edd = 1;
  102. #endif
  103. int be_quiet;
  104. int devno;
  105. struct edd_info ei, *edp;
  106. u32 *mbrptr;
  107. if (cmdline_find_option("edd", eddarg, sizeof eddarg) > 0) {
  108. if (!strcmp(eddarg, "skipmbr") || !strcmp(eddarg, "skip")) {
  109. do_edd = 1;
  110. do_mbr = 0;
  111. }
  112. else if (!strcmp(eddarg, "off"))
  113. do_edd = 0;
  114. else if (!strcmp(eddarg, "on"))
  115. do_edd = 1;
  116. }
  117. be_quiet = cmdline_find_option_bool("quiet");
  118. edp = boot_params.eddbuf;
  119. mbrptr = boot_params.edd_mbr_sig_buffer;
  120. if (!do_edd)
  121. return;
  122. /* Bugs in OnBoard or AddOnCards Bios may hang the EDD probe,
  123. * so give a hint if this happens.
  124. */
  125. if (!be_quiet)
  126. printf("Probing EDD (edd=off to disable)... ");
  127. for (devno = 0x80; devno < 0x80+EDD_MBR_SIG_MAX; devno++) {
  128. /*
  129. * Scan the BIOS-supported hard disks and query EDD
  130. * information...
  131. */
  132. if (!get_edd_info(devno, &ei)
  133. && boot_params.eddbuf_entries < EDDMAXNR) {
  134. memcpy(edp, &ei, sizeof ei);
  135. edp++;
  136. boot_params.eddbuf_entries++;
  137. }
  138. if (do_mbr && !read_mbr_sig(devno, &ei, mbrptr++))
  139. boot_params.edd_mbr_sig_buf_entries = devno-0x80+1;
  140. }
  141. if (!be_quiet)
  142. printf("ok\n");
  143. }
  144. #endif