Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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