Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

bootsector.c 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. */
  19. FILE_LICENCE ( GPL2_OR_LATER );
  20. /**
  21. * @file
  22. *
  23. * x86 bootsector image format
  24. *
  25. */
  26. #include <errno.h>
  27. #include <realmode.h>
  28. #include <biosint.h>
  29. #include <bootsector.h>
  30. #include <ipxe/console.h>
  31. /** Vector for storing original INT 18 handler
  32. *
  33. * We do not chain to this vector, so there is no need to place it in
  34. * .text16.
  35. */
  36. static struct segoff int18_vector;
  37. /** Vector for storing original INT 19 handler
  38. *
  39. * We do not chain to this vector, so there is no need to place it in
  40. * .text16.
  41. */
  42. static struct segoff int19_vector;
  43. /** Restart point for INT 18 or 19 */
  44. extern void bootsector_exec_fail ( void );
  45. /**
  46. * Jump to preloaded bootsector
  47. *
  48. * @v segment Real-mode segment
  49. * @v offset Real-mode offset
  50. * @v drive Drive number to pass to boot sector
  51. * @ret rc Return status code
  52. */
  53. int call_bootsector ( unsigned int segment, unsigned int offset,
  54. unsigned int drive ) {
  55. int discard_b, discard_D, discard_d;
  56. /* Reset console, since boot sector will probably use it */
  57. console_reset();
  58. DBG ( "Booting from boot sector at %04x:%04x\n", segment, offset );
  59. /* Hook INTs 18 and 19 to capture failure paths */
  60. hook_bios_interrupt ( 0x18, ( unsigned int ) bootsector_exec_fail,
  61. &int18_vector );
  62. hook_bios_interrupt ( 0x19, ( unsigned int ) bootsector_exec_fail,
  63. &int19_vector );
  64. /* Boot the loaded sector
  65. *
  66. * We assume that the boot sector may completely destroy our
  67. * real-mode stack, so we preserve everything we need in
  68. * static storage.
  69. */
  70. __asm__ __volatile__ ( REAL_CODE ( /* Save return address off-stack */
  71. "popw %%cs:saved_retaddr\n\t"
  72. /* Save stack pointer */
  73. "movw %%ss, %%ax\n\t"
  74. "movw %%ax, %%cs:saved_ss\n\t"
  75. "movw %%sp, %%cs:saved_sp\n\t"
  76. /* Save frame pointer (gcc bug) */
  77. "movl %%ebp, %%cs:saved_ebp\n\t"
  78. /* Prepare jump to boot sector */
  79. "pushw %%bx\n\t"
  80. "pushw %%di\n\t"
  81. /* Clear all registers */
  82. "xorl %%eax, %%eax\n\t"
  83. "xorl %%ebx, %%ebx\n\t"
  84. "xorl %%ecx, %%ecx\n\t"
  85. /* %edx contains drive number */
  86. "xorl %%esi, %%esi\n\t"
  87. "xorl %%edi, %%edi\n\t"
  88. "xorl %%ebp, %%ebp\n\t"
  89. "movw %%ax, %%ds\n\t"
  90. "movw %%ax, %%es\n\t"
  91. "movw %%ax, %%fs\n\t"
  92. "movw %%ax, %%gs\n\t"
  93. /* Jump to boot sector */
  94. "sti\n\t"
  95. "lret\n\t"
  96. /* Preserved variables */
  97. "\nsaved_ebp: .long 0\n\t"
  98. "\nsaved_ss: .word 0\n\t"
  99. "\nsaved_sp: .word 0\n\t"
  100. "\nsaved_retaddr: .word 0\n\t"
  101. /* Boot failure return point */
  102. "\nbootsector_exec_fail:\n\t"
  103. /* Restore frame pointer (gcc bug) */
  104. "movl %%cs:saved_ebp, %%ebp\n\t"
  105. /* Restore stack pointer */
  106. "movw %%cs:saved_ss, %%ax\n\t"
  107. "movw %%ax, %%ss\n\t"
  108. "movw %%cs:saved_sp, %%sp\n\t"
  109. /* Return via saved address */
  110. "jmp *%%cs:saved_retaddr\n\t" )
  111. : "=b" ( discard_b ), "=D" ( discard_D ),
  112. "=d" ( discard_d )
  113. : "b" ( segment ), "D" ( offset ),
  114. "d" ( drive )
  115. : "eax", "ecx", "esi" );
  116. DBG ( "Booted disk returned via INT 18 or 19\n" );
  117. /* Unhook INTs 18 and 19 */
  118. unhook_bios_interrupt ( 0x18, ( unsigned int ) bootsector_exec_fail,
  119. &int18_vector );
  120. unhook_bios_interrupt ( 0x19, ( unsigned int ) bootsector_exec_fail,
  121. &int19_vector );
  122. return -ECANCELED;
  123. }