boot32.S 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /* arch/x86/boot/boot32.S
  2. *
  3. * Copyright (C) 2021, 2022 intirain.
  4. *
  5. * This file is part of evx.
  6. *
  7. * evx is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * evx is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with evx. If not, see <https://www.gnu.org/licenses/>
  19. */
  20. .code16
  21. /* load system to 0x8000, and boot */
  22. .globl _start
  23. _start: xorw %ax, %ax
  24. movw %ax, %ds
  25. movw %ax, %es
  26. movw %ax, %ss
  27. movw $0x7c00, %sp
  28. /* move self to 0x7e00 */
  29. movw $0x7e00, %di
  30. movw $0x7c00, %si
  31. movw $512, %cx
  32. cld
  33. rep
  34. movsb
  35. ljmp $0x0000, $real_start
  36. real_start:
  37. leaw loading_msg, %si
  38. call print
  39. call load_sys
  40. call setup_e820
  41. ljmp $0, $0x8000
  42. die: hlt
  43. jmp die
  44. load_sys:
  45. /* load the system to 0x8000, and jump */
  46. movw $0x0800, %ax
  47. movw %ax, %es
  48. xorw %bx, %bx
  49. movl $1, %eax
  50. lloop: movw $16, %cx
  51. call read_lba
  52. movw %es, %cx
  53. addw $512, %cx
  54. movw %cx, %es
  55. addl $16, %eax
  56. cmpl $64, %eax
  57. jb cont
  58. ret
  59. cont: pushal
  60. /* put a '.' on screen */
  61. movb $'.', %al
  62. movb $0x0e, %ah
  63. movw $0x000f, %bx
  64. int $0x10
  65. popal
  66. jmp lloop
  67. read_lba:
  68. pushaw
  69. pushl $0
  70. pushl %eax
  71. pushw %es
  72. pushw %bx
  73. pushw %cx
  74. pushw $16
  75. movb $0x42, %ah
  76. /* dx didn't change */
  77. movw %sp, %si
  78. int $0x13
  79. jc read_failed
  80. addw $16, %sp
  81. popaw
  82. ret
  83. read_failed:
  84. leaw read_fail_msg, %si
  85. call print
  86. jmp die
  87. setup_e820:
  88. movl $0x9000, %ebx
  89. movl %ebx, %es
  90. xorl %edi, %edi
  91. xorl %ebx, %ebx
  92. sel: movl $0xe820, %eax
  93. movl $20, %ecx
  94. movl $0x534d4150, %edx
  95. int $0x15
  96. jc e820_failed
  97. addl $20, %edi
  98. cmpl $0, %ebx
  99. jne sel
  100. ret
  101. e820_failed:
  102. leaw e820_fail_msg, %si
  103. pushw $die
  104. print: cld
  105. lodsb
  106. testb %al, %al
  107. jz pret
  108. movb $0x0e, %ah
  109. movw $0x000f, %bx
  110. int $0x10
  111. jmp print
  112. pret: ret
  113. read_fail_msg:
  114. .ascii "read /dev/sda failed. "
  115. .byte 0x0d, 0x0a, 0x00
  116. loading_msg:
  117. .asciz "Loading System"
  118. e820_fail_msg:
  119. .asciz "get e820 meminfo failed. "
  120. .org 0x1be
  121. part0:
  122. .org 510
  123. .word 0xaa55