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 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. /** Vector for storing original INT 18 handler
  31. *
  32. * We do not chain to this vector, so there is no need to place it in
  33. * .text16.
  34. */
  35. static struct segoff int18_vector;
  36. /** Vector for storing original INT 19 handler
  37. *
  38. * We do not chain to this vector, so there is no need to place it in
  39. * .text16.
  40. */
  41. static struct segoff int19_vector;
  42. /** Restart point for INT 18 or 19 */
  43. extern void bootsector_exec_fail ( void );
  44. /**
  45. * Jump to preloaded bootsector
  46. *
  47. * @v segment Real-mode segment
  48. * @v offset Real-mode offset
  49. * @v drive Drive number to pass to boot sector
  50. * @ret rc Return status code
  51. */
  52. int call_bootsector ( unsigned int segment, unsigned int offset,
  53. unsigned int drive ) {
  54. int discard_b, discard_D, discard_d;
  55. DBG ( "Booting from boot sector at %04x:%04x\n", segment, offset );
  56. /* Hook INTs 18 and 19 to capture failure paths */
  57. hook_bios_interrupt ( 0x18, ( unsigned int ) bootsector_exec_fail,
  58. &int18_vector );
  59. hook_bios_interrupt ( 0x19, ( unsigned int ) bootsector_exec_fail,
  60. &int19_vector );
  61. /* Boot the loaded sector
  62. *
  63. * We assume that the boot sector may completely destroy our
  64. * real-mode stack, so we preserve everything we need in
  65. * static storage.
  66. */
  67. __asm__ __volatile__ ( REAL_CODE ( /* Save return address off-stack */
  68. "popw %%cs:saved_retaddr\n\t"
  69. /* Save stack pointer */
  70. "movw %%ss, %%ax\n\t"
  71. "movw %%ax, %%cs:saved_ss\n\t"
  72. "movw %%sp, %%cs:saved_sp\n\t"
  73. /* Jump to boot sector */
  74. "pushw %%bx\n\t"
  75. "pushw %%di\n\t"
  76. "sti\n\t"
  77. "lret\n\t"
  78. /* Preserved variables */
  79. "\nsaved_ss: .word 0\n\t"
  80. "\nsaved_sp: .word 0\n\t"
  81. "\nsaved_retaddr: .word 0\n\t"
  82. /* Boot failure return point */
  83. "\nbootsector_exec_fail:\n\t"
  84. /* Restore stack pointer */
  85. "movw %%cs:saved_ss, %%ax\n\t"
  86. "movw %%ax, %%ss\n\t"
  87. "movw %%cs:saved_sp, %%sp\n\t"
  88. /* Return via saved address */
  89. "jmp *%%cs:saved_retaddr\n\t" )
  90. : "=b" ( discard_b ), "=D" ( discard_D ),
  91. "=d" ( discard_d )
  92. : "b" ( segment ), "D" ( offset ),
  93. "d" ( drive )
  94. : "eax", "ecx", "esi", "ebp" );
  95. DBG ( "Booted disk returned via INT 18 or 19\n" );
  96. /* Unhook INTs 18 and 19 */
  97. unhook_bios_interrupt ( 0x18, ( unsigned int ) bootsector_exec_fail,
  98. &int18_vector );
  99. unhook_bios_interrupt ( 0x19, ( unsigned int ) bootsector_exec_fail,
  100. &int19_vector );
  101. return -ECANCELED;
  102. }