boot.S 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. /* Load kernel: sectors 1..kernel_sectors to 0x7E00. */
  21. movw kernel_sectors, %cx
  22. movw $1, %si
  23. movw $0x07e0, %ax
  24. movw %ax, %es
  25. xorw %bx, %bx
  26. load_loop:
  27. cmpw $0, %cx
  28. jz load_done
  29. pushw %cx
  30. pushw %si
  31. /* CHS from LBA in SI */
  32. movw %si, %ax
  33. xorw %dx, %dx
  34. movw $18, %di
  35. divw %di
  36. movb %dl, %cl
  37. incb %cl
  38. xorw %dx, %dx
  39. movw $2, %di
  40. divw %di
  41. movb %dl, %dh
  42. movb %al, %ch
  43. movb boot_drive, %dl
  44. movb $0x02, %ah
  45. movb $1, %al
  46. int $0x13
  47. popw %si
  48. popw %cx
  49. jc halt
  50. addw $0x200, %bx
  51. jnc 1f
  52. movw %es, %ax
  53. addw $0x1000, %ax
  54. movw %ax, %es
  55. 1: incw %si
  56. decw %cx
  57. jnz load_loop
  58. load_done:
  59. movw $0x7DFC, %bx
  60. movb boot_drive, %al
  61. movb %al, (%bx)
  62. cli
  63. lgdt gdtr
  64. movl %cr0, %eax
  65. orl $1, %eax
  66. movl %eax, %cr0
  67. .byte 0x66, 0xea
  68. .long pm_start
  69. .word 0x08
  70. .align 4
  71. boot_drive:
  72. .byte 0
  73. .align 8
  74. gdt:
  75. .quad 0
  76. .word 0xffff, 0x0000
  77. .byte 0x00, 0x9a, 0xcf, 0x00
  78. .word 0xffff, 0x0000
  79. .byte 0x00, 0x92, 0xcf, 0x00
  80. gdt_end:
  81. gdtr:
  82. .word gdt_end - gdt - 1
  83. .long gdt
  84. halt:
  85. cli
  86. 1: hlt
  87. jmp 1b
  88. .code32
  89. pm_start:
  90. movw $0x10, %ax
  91. movw %ax, %ds
  92. movw %ax, %es
  93. movw %ax, %fs
  94. movw %ax, %gs
  95. movw %ax, %ss
  96. movl $0x10000, %esp
  97. movl $0x7e00, %eax
  98. jmp *%eax
  99. .section .bootsig
  100. .byte 0x55, 0xaa