Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

bootsector.c 3.4KB

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