You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

bootsector.c 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. /**
  25. * @file
  26. *
  27. * x86 bootsector image format
  28. *
  29. */
  30. #include <errno.h>
  31. #include <realmode.h>
  32. #include <biosint.h>
  33. #include <bootsector.h>
  34. #include <ipxe/console.h>
  35. /** Vector for storing original INT 18 handler
  36. *
  37. * We do not chain to this vector, so there is no need to place it in
  38. * .text16.
  39. */
  40. static struct segoff int18_vector;
  41. /** Vector for storing original INT 19 handler
  42. *
  43. * We do not chain to this vector, so there is no need to place it in
  44. * .text16.
  45. */
  46. static struct segoff int19_vector;
  47. /** Restart point for INT 18 or 19 */
  48. extern void bootsector_exec_fail ( void );
  49. /**
  50. * Jump to preloaded bootsector
  51. *
  52. * @v segment Real-mode segment
  53. * @v offset Real-mode offset
  54. * @v drive Drive number to pass to boot sector
  55. * @ret rc Return status code
  56. */
  57. int call_bootsector ( unsigned int segment, unsigned int offset,
  58. unsigned int drive ) {
  59. int discard_b, discard_D, discard_d;
  60. /* Reset console, since boot sector will probably use it */
  61. console_reset();
  62. DBG ( "Booting from boot sector at %04x:%04x\n", segment, offset );
  63. /* Hook INTs 18 and 19 to capture failure paths */
  64. hook_bios_interrupt ( 0x18, ( unsigned int ) bootsector_exec_fail,
  65. &int18_vector );
  66. hook_bios_interrupt ( 0x19, ( unsigned int ) bootsector_exec_fail,
  67. &int19_vector );
  68. /* Boot the loaded sector
  69. *
  70. * We assume that the boot sector may completely destroy our
  71. * real-mode stack, so we preserve everything we need in
  72. * static storage.
  73. */
  74. __asm__ __volatile__ ( REAL_CODE ( /* Save return address off-stack */
  75. "popw %%cs:saved_retaddr\n\t"
  76. /* Save stack pointer */
  77. "movw %%ss, %%ax\n\t"
  78. "movw %%ax, %%cs:saved_ss\n\t"
  79. "movw %%sp, %%cs:saved_sp\n\t"
  80. /* Save frame pointer (gcc bug) */
  81. "movl %%ebp, %%cs:saved_ebp\n\t"
  82. /* Prepare jump to boot sector */
  83. "pushw %%bx\n\t"
  84. "pushw %%di\n\t"
  85. /* Clear all registers */
  86. "xorl %%eax, %%eax\n\t"
  87. "xorl %%ebx, %%ebx\n\t"
  88. "xorl %%ecx, %%ecx\n\t"
  89. /* %edx contains drive number */
  90. "xorl %%esi, %%esi\n\t"
  91. "xorl %%edi, %%edi\n\t"
  92. "xorl %%ebp, %%ebp\n\t"
  93. "movw %%ax, %%ds\n\t"
  94. "movw %%ax, %%es\n\t"
  95. "movw %%ax, %%fs\n\t"
  96. "movw %%ax, %%gs\n\t"
  97. /* Jump to boot sector */
  98. "sti\n\t"
  99. "lret\n\t"
  100. /* Preserved variables */
  101. "\nsaved_ebp: .long 0\n\t"
  102. "\nsaved_ss: .word 0\n\t"
  103. "\nsaved_sp: .word 0\n\t"
  104. "\nsaved_retaddr: .word 0\n\t"
  105. /* Boot failure return point */
  106. "\nbootsector_exec_fail:\n\t"
  107. /* Restore frame pointer (gcc bug) */
  108. "movl %%cs:saved_ebp, %%ebp\n\t"
  109. /* Restore stack pointer */
  110. "movw %%cs:saved_ss, %%ax\n\t"
  111. "movw %%ax, %%ss\n\t"
  112. "movw %%cs:saved_sp, %%sp\n\t"
  113. /* Return via saved address */
  114. "jmp *%%cs:saved_retaddr\n\t" )
  115. : "=b" ( discard_b ), "=D" ( discard_D ),
  116. "=d" ( discard_d )
  117. : "b" ( segment ), "D" ( offset ),
  118. "d" ( drive )
  119. : "eax", "ecx", "esi" );
  120. DBG ( "Booted disk returned via INT 18 or 19\n" );
  121. /* Unhook INTs 18 and 19 */
  122. unhook_bios_interrupt ( 0x18, ( unsigned int ) bootsector_exec_fail,
  123. &int18_vector );
  124. unhook_bios_interrupt ( 0x19, ( unsigned int ) bootsector_exec_fail,
  125. &int19_vector );
  126. return -ECANCELED;
  127. }