loader.S 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 1999,2000,2001,2002,2003,2005,2006,2007,2008 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /*
  19. * Note: These functions defined in this file may be called from C.
  20. * Be careful of that you must not modify some registers. Quote
  21. * from gcc-2.95.2/gcc/config/i386/i386.h:
  22. 1 for registers not available across function calls.
  23. These must include the FIXED_REGISTERS and also any
  24. registers that can be used without being saved.
  25. The latter must include the registers where values are returned
  26. and the register where structure-value addresses are passed.
  27. Aside from that, you can include as many other registers as you like.
  28. ax,dx,cx,bx,si,di,bp,sp,st,st1,st2,st3,st4,st5,st6,st7,arg
  29. { 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }
  30. */
  31. /*
  32. * Note: GRUB is compiled with the options -mrtd and -mregparm=3.
  33. * So the first three arguments are passed in %eax, %edx, and %ecx,
  34. * respectively, and if a function has a fixed number of arguments
  35. * and the number if greater than three, the function must return
  36. * with "ret $N" where N is ((the number of arguments) - 3) * 4.
  37. */
  38. /*
  39. * This is the area for all of the special variables.
  40. */
  41. GRUB_EXPORT_START
  42. GRUB_EXPORT(grub_linux_prot_size);
  43. GRUB_EXPORT(grub_linux_tmp_addr);
  44. GRUB_EXPORT(grub_linux_real_addr);
  45. GRUB_EXPORT(grub_linux_is_bzimage);
  46. GRUB_EXPORT(grub_linux16_real_boot);
  47. GRUB_EXPORT_END
  48. .p2align 2 /* force 4-byte alignment */
  49. /*
  50. * void grub_linux_boot_zimage (void)
  51. */
  52. VARIABLE(grub_linux_prot_size)
  53. .long 0
  54. VARIABLE(grub_linux_tmp_addr)
  55. .long 0
  56. VARIABLE(grub_linux_real_addr)
  57. .long 0
  58. VARIABLE(grub_linux_is_bzimage)
  59. .long 0
  60. FUNCTION(grub_linux16_real_boot)
  61. /* Must be done before zImage copy. */
  62. call EXT_C(grub_dl_unload_all)
  63. movl EXT_C(grub_linux_is_bzimage), %ebx
  64. test %ebx, %ebx
  65. jne bzimage
  66. /* copy the kernel */
  67. movl EXT_C(grub_linux_prot_size), %ecx
  68. addl $3, %ecx
  69. shrl $2, %ecx
  70. movl $GRUB_LINUX_BZIMAGE_ADDR, %esi
  71. movl $GRUB_LINUX_ZIMAGE_ADDR, %edi
  72. cld
  73. rep
  74. movsl
  75. bzimage:
  76. movl EXT_C(grub_linux_real_addr), %ebx
  77. /* copy the real mode code */
  78. movl EXT_C(grub_linux_tmp_addr), %esi
  79. movl %ebx, %edi
  80. movl $GRUB_LINUX_SETUP_MOVE_SIZE, %ecx
  81. cld
  82. rep
  83. movsb
  84. /* change %ebx to the segment address */
  85. shrl $4, %ebx
  86. movl %ebx, %eax
  87. addl $0x20, %eax
  88. movw %ax, linux_setup_seg
  89. /* XXX new stack pointer in safe area for calling functions */
  90. movl $0x4000, %esp
  91. call EXT_C(grub_stop_floppy)
  92. /* final setup for linux boot */
  93. call prot_to_real
  94. .code16
  95. cli
  96. movw %bx, %ss
  97. movw $GRUB_LINUX_SETUP_STACK, %sp
  98. movw %bx, %ds
  99. movw %bx, %es
  100. movw %bx, %fs
  101. movw %bx, %gs
  102. /* ljmp */
  103. .byte 0xea
  104. .word 0
  105. linux_setup_seg:
  106. .word 0
  107. .code32