drivemap_int13h.S 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* drivemap_int13h.S - interrupt handler for the BIOS drive remapper */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2008, 2009 Free Software Foundation, Inc.
  5. *
  6. * GRUB is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * GRUB is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <grub/symbol.h>
  20. #define INT13H_OFFSET(x) ((x) - LOCAL (base))
  21. .code16
  22. /* Copy starts here. When deployed, this code must be segment-aligned. */
  23. /* The replacement int13 handler. Preserve all registers. */
  24. FUNCTION(grub_drivemap_handler)
  25. LOCAL (base):
  26. /* Save %dx for future restore. */
  27. push %dx
  28. /* Push flags. Used to simulate interrupt with original flags. */
  29. pushf
  30. /* Map the drive number (always in DL). */
  31. push %ax
  32. push %bx
  33. movw $INT13H_OFFSET(LOCAL (mapstart)), %bx
  34. more_remaining:
  35. movw %cs:(%bx), %ax
  36. cmpb %ah, %al
  37. jz not_found /* DRV=DST => map end - drive not remapped, keep DL. */
  38. inc %bx
  39. inc %bx
  40. cmpb %dl, %al
  41. jnz more_remaining /* Not found, but more remaining, loop. */
  42. movb %ah, %dl /* Found - drive remapped, modify DL. */
  43. not_found:
  44. pop %bx
  45. pop %ax
  46. /* If the call isn't ah=0x8 or ah=0x15 we must restore %dx. */
  47. cmpb $0x8, %ah
  48. jz norestore
  49. cmpb $0x15, %ah
  50. jz norestore
  51. /* Restore flags. */
  52. popf
  53. pushf
  54. lcall *%cs:INT13H_OFFSET (LOCAL (oldhandler))
  55. push %bp
  56. mov %sp, %bp
  57. tail:
  58. /* Save new flags below %esp so the caller will recieve new flags. */
  59. pushf
  60. pop %dx
  61. mov %dx, 8(%bp)
  62. pop %bp
  63. /* Restore %dx. */
  64. pop %dx
  65. iret
  66. norestore:
  67. /* Restore flags. */
  68. popf
  69. pushf
  70. lcall *%cs:INT13H_OFFSET (LOCAL (oldhandler))
  71. push %bp
  72. mov %sp, %bp
  73. /* Save %dx. So it won't be restored to original value. */
  74. mov %dx, 2(%bp)
  75. jmp tail
  76. /* Far pointer to the old handler. Stored as a CS:IP in the style of real-mode
  77. IVT entries (thus PI:SC in mem). */
  78. VARIABLE(grub_drivemap_oldhandler)
  79. LOCAL (oldhandler):
  80. .word 0x0, 0x0
  81. /* This label MUST be at the end of the copied block, since the installer code
  82. reserves additional space for mappings at runtime and copies them over it. */
  83. .align 2
  84. VARIABLE(grub_drivemap_mapstart)
  85. LOCAL (mapstart):
  86. .byte 0