boot.S 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. jmp halt
  46. vesa_ok:
  47. movw $0x6000, %di /* Reload: int 0x10/4F02 may trash DI */
  48. movw $0x600, %bx
  49. movb $1, (%bx) /* valid = 1 */
  50. movw 0x12(%di), %ax
  51. movw %ax, 1(%bx) /* width */
  52. movw 0x14(%di), %ax
  53. movw %ax, 3(%bx) /* height */
  54. movw 0x10(%di), %ax
  55. movw %ax, 5(%bx) /* pitch */
  56. movb 0x19(%di), %al
  57. movb %al, 7(%bx) /* bpp */
  58. movw 0x28(%di), %ax
  59. movw %ax, 8(%bx) /* lfb_base lo */
  60. movw 0x2a(%di), %ax
  61. movw %ax, 10(%bx) /* lfb_base hi */
  62. load_kernel:
  63. /* Load kernel: sectors 1..kernel_sectors to 0x7E00. */
  64. movw kernel_sectors, %cx
  65. movw $1, %si
  66. movw $0x07e0, %ax
  67. movw %ax, %es
  68. xorw %bx, %bx
  69. load_loop:
  70. cmpw $0, %cx
  71. jz load_done
  72. pushw %cx
  73. pushw %si
  74. /* CHS from LBA in SI */
  75. movw %si, %ax
  76. xorw %dx, %dx
  77. movw $18, %di
  78. divw %di
  79. movb %dl, %cl
  80. incb %cl
  81. xorw %dx, %dx
  82. movw $2, %di
  83. divw %di
  84. movb %dl, %dh
  85. movb %al, %ch
  86. movb boot_drive, %dl
  87. movb $0x02, %ah
  88. movb $1, %al
  89. int $0x13
  90. popw %si
  91. popw %cx
  92. jc halt
  93. addw $0x200, %bx
  94. jnc 1f
  95. movw %es, %ax
  96. addw $0x1000, %ax
  97. movw %ax, %es
  98. 1: incw %si
  99. decw %cx
  100. jnz load_loop
  101. load_done:
  102. movw $0x7DFC, %bx
  103. movb boot_drive, %al
  104. movb %al, (%bx)
  105. cli
  106. lgdt gdtr
  107. movl %cr0, %eax
  108. orl $1, %eax
  109. movl %eax, %cr0
  110. .byte 0x66, 0xea
  111. .long pm_start
  112. .word 0x08
  113. .align 4
  114. boot_drive:
  115. .byte 0
  116. .align 8
  117. gdt:
  118. .quad 0
  119. .word 0xffff, 0x0000
  120. .byte 0x00, 0x9a, 0xcf, 0x00
  121. .word 0xffff, 0x0000
  122. .byte 0x00, 0x92, 0xcf, 0x00
  123. gdt_end:
  124. gdtr:
  125. .word gdt_end - gdt - 1
  126. .long gdt
  127. halt:
  128. cli
  129. 1: hlt
  130. jmp 1b
  131. .code32
  132. pm_start:
  133. movw $0x10, %ax
  134. movw %ax, %ds
  135. movw %ax, %es
  136. movw %ax, %fs
  137. movw %ax, %gs
  138. movw %ax, %ss
  139. /* Stack above DMA buffer 0x20000-0x20200 so DMA does not overwrite stack */
  140. movl $0x20800, %esp
  141. movl $0x7e00, %eax
  142. jmp *%eax
  143. .section .bootsig
  144. .byte 0x55, 0xaa