boot.S 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /* Boot sector: load contiguous kernel sectors 1..N to 0x7E00, then PM. No FAT. */
  2. .code16
  3. .section .text
  4. .globl _start
  5. _start:
  6. .byte 0xEB, 0x3E, 0x90
  7. .space 0x3E - 3, 0
  8. .org 0x3E
  9. kernel_sectors:
  10. .word 1
  11. .org 0x40
  12. loader_start:
  13. xorw %ax, %ax
  14. movw %ax, %ds
  15. movw %ax, %ss
  16. movw $0x7c00, %sp
  17. movb %dl, boot_drive
  18. movw $0x0003, %ax
  19. int $0x10
  20. /* VESA: try fixed mode list, use 0x6000 for mode info (256 bytes). */
  21. xorw %ax, %ax
  22. movw %ax, %es
  23. movw $0x6000, %di
  24. /* Try modes: 0x11C, 0x11B, 0x118, 0x114 (descending res, >= 16bpp). */
  25. movw $0x11C, %cx
  26. 1: movw $0x4F01, %ax
  27. int $0x10
  28. cmpw $0x004F, %ax
  29. jnz 2f
  30. cmpb $16, 0x19(%di) /* BitsPerPixel >= 16 */
  31. jb 2f
  32. testw $0x80, (%di) /* ModeAttributes bit 7 = LFB */
  33. jz 2f
  34. movw %cx, %bx
  35. orw $0x4000, %bx /* Set mode with LFB */
  36. movw $0x4F02, %ax
  37. int $0x10
  38. cmpw $0x004F, %ax
  39. jz vesa_ok
  40. 2: cmpw $0x114, %cx
  41. jz vesa_fail
  42. subw $1, %cx /* 0x11C -> 0x11B -> 0x118 -> 0x114 */
  43. jmp 1b
  44. vesa_fail:
  45. xorw %ax, %ax
  46. movw $0x600, %bx
  47. movb %al, (%bx) /* valid = 0 */
  48. jmp load_kernel
  49. vesa_ok:
  50. movw $0x6000, %di /* Reload: int 0x10/4F02 may trash DI */
  51. movw $0x600, %bx
  52. movb $1, (%bx) /* valid = 1 */
  53. movw 0x12(%di), %ax
  54. movw %ax, 1(%bx) /* width */
  55. movw 0x14(%di), %ax
  56. movw %ax, 3(%bx) /* height */
  57. movw 0x10(%di), %ax
  58. movw %ax, 5(%bx) /* pitch */
  59. movb 0x19(%di), %al
  60. movb %al, 7(%bx) /* bpp */
  61. movw 0x28(%di), %ax
  62. movw %ax, 8(%bx) /* lfb_base lo */
  63. movw 0x2a(%di), %ax
  64. movw %ax, 10(%bx) /* lfb_base hi */
  65. load_kernel:
  66. /* Load kernel: sectors 1..kernel_sectors to 0x7E00. */
  67. movw kernel_sectors, %cx
  68. movw $1, %si
  69. movw $0x07e0, %ax
  70. movw %ax, %es
  71. xorw %bx, %bx
  72. load_loop:
  73. cmpw $0, %cx
  74. jz load_done
  75. pushw %cx
  76. pushw %si
  77. /* CHS from LBA in SI */
  78. movw %si, %ax
  79. xorw %dx, %dx
  80. movw $18, %di
  81. divw %di
  82. movb %dl, %cl
  83. incb %cl
  84. xorw %dx, %dx
  85. movw $2, %di
  86. divw %di
  87. movb %dl, %dh
  88. movb %al, %ch
  89. movb boot_drive, %dl
  90. movb $0x02, %ah
  91. movb $1, %al
  92. int $0x13
  93. popw %si
  94. popw %cx
  95. jc halt
  96. addw $0x200, %bx
  97. jnc 1f
  98. movw %es, %ax
  99. addw $0x1000, %ax
  100. movw %ax, %es
  101. 1: incw %si
  102. decw %cx
  103. jnz load_loop
  104. load_done:
  105. movw $0x7DFC, %bx
  106. movb boot_drive, %al
  107. movb %al, (%bx)
  108. cli
  109. lgdt gdtr
  110. movl %cr0, %eax
  111. orl $1, %eax
  112. movl %eax, %cr0
  113. .byte 0x66, 0xea
  114. .long pm_start
  115. .word 0x08
  116. .align 4
  117. boot_drive:
  118. .byte 0
  119. .align 8
  120. gdt:
  121. .quad 0
  122. .word 0xffff, 0x0000
  123. .byte 0x00, 0x9a, 0xcf, 0x00
  124. .word 0xffff, 0x0000
  125. .byte 0x00, 0x92, 0xcf, 0x00
  126. gdt_end:
  127. gdtr:
  128. .word gdt_end - gdt - 1
  129. .long gdt
  130. halt:
  131. cli
  132. 1: hlt
  133. jmp 1b
  134. .code32
  135. pm_start:
  136. movw $0x10, %ax
  137. movw %ax, %ds
  138. movw %ax, %es
  139. movw %ax, %fs
  140. movw %ax, %gs
  141. movw %ax, %ss
  142. /* Stack above DMA buffer 0x20000-0x20200 so DMA does not overwrite stack */
  143. movl $0x20800, %esp
  144. movl $0x7e00, %eax
  145. jmp *%eax
  146. .section .bootsig
  147. .byte 0x55, 0xaa