edd.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. #if defined(CONFIG_EDD) || defined(CONFIG_EDD_MODULE)
  17. /*
  18. * Read the MBR (first sector) from a specific device.
  19. */
  20. static int read_mbr(u8 devno, void *buf)
  21. {
  22. struct biosregs ireg, oreg;
  23. initregs(&ireg);
  24. ireg.ax = 0x0201; /* Legacy Read, one sector */
  25. ireg.cx = 0x0001; /* Sector 0-0-1 */
  26. ireg.dl = devno;
  27. ireg.bx = (size_t)buf;
  28. intcall(0x13, &ireg, &oreg);
  29. return -(oreg.eflags & X86_EFLAGS_CF); /* 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. struct biosregs ireg, oreg;
  62. memset(ei, 0, sizeof *ei);
  63. /* Check Extensions Present */
  64. initregs(&ireg);
  65. ireg.ah = 0x41;
  66. ireg.bx = EDDMAGIC1;
  67. ireg.dl = devno;
  68. intcall(0x13, &ireg, &oreg);
  69. if (oreg.eflags & X86_EFLAGS_CF)
  70. return -1; /* No extended information */
  71. if (oreg.bx != EDDMAGIC2)
  72. return -1;
  73. ei->device = devno;
  74. ei->version = oreg.ah; /* EDD version number */
  75. ei->interface_support = oreg.cx; /* EDD functionality subsets */
  76. /* Extended Get Device Parameters */
  77. ei->params.length = sizeof(ei->params);
  78. ireg.ah = 0x48;
  79. ireg.si = (size_t)&ei->params;
  80. intcall(0x13, &ireg, &oreg);
  81. /* Get legacy CHS parameters */
  82. /* Ralf Brown recommends setting ES:DI to 0:0 */
  83. ireg.ah = 0x08;
  84. ireg.es = 0;
  85. intcall(0x13, &ireg, &oreg);
  86. if (!(oreg.eflags & X86_EFLAGS_CF)) {
  87. ei->legacy_max_cylinder = oreg.ch + ((oreg.cl & 0xc0) << 2);
  88. ei->legacy_max_head = oreg.dh;
  89. ei->legacy_sectors_per_track = oreg.cl & 0x3f;
  90. }
  91. return 0;
  92. }
  93. void query_edd(void)
  94. {
  95. char eddarg[8];
  96. int do_mbr = 1;
  97. #ifdef CONFIG_EDD_OFF
  98. int do_edd = 0;
  99. #else
  100. int do_edd = 1;
  101. #endif
  102. int be_quiet;
  103. int devno;
  104. struct edd_info ei, *edp;
  105. u32 *mbrptr;
  106. if (cmdline_find_option("edd", eddarg, sizeof eddarg) > 0) {
  107. if (!strcmp(eddarg, "skipmbr") || !strcmp(eddarg, "skip")) {
  108. do_edd = 1;
  109. do_mbr = 0;
  110. }
  111. else if (!strcmp(eddarg, "off"))
  112. do_edd = 0;
  113. else if (!strcmp(eddarg, "on"))
  114. do_edd = 1;
  115. }
  116. be_quiet = cmdline_find_option_bool("quiet");
  117. edp = boot_params.eddbuf;
  118. mbrptr = boot_params.edd_mbr_sig_buffer;
  119. if (!do_edd)
  120. return;
  121. /* Bugs in OnBoard or AddOnCards Bios may hang the EDD probe,
  122. * so give a hint if this happens.
  123. */
  124. if (!be_quiet)
  125. printf("Probing EDD (edd=off to disable)... ");
  126. for (devno = 0x80; devno < 0x80+EDD_MBR_SIG_MAX; devno++) {
  127. /*
  128. * Scan the BIOS-supported hard disks and query EDD
  129. * information...
  130. */
  131. if (!get_edd_info(devno, &ei)
  132. && boot_params.eddbuf_entries < EDDMAXNR) {
  133. memcpy(edp, &ei, sizeof ei);
  134. edp++;
  135. boot_params.eddbuf_entries++;
  136. }
  137. if (do_mbr && !read_mbr_sig(devno, &ei, mbrptr++))
  138. boot_params.edd_mbr_sig_buf_entries = devno-0x80+1;
  139. }
  140. if (!be_quiet)
  141. printf("ok\n");
  142. }
  143. #endif