boot.s 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. .section .text.bootsect
  2. .code16
  3. # mbr
  4. .globl _start
  5. _start:
  6. jmp $0x07c0, $(real_start-_start)
  7. real_start:
  8. movw %cs, %ax
  9. movw %ax, %ds
  10. movw %ax, %es
  11. movw %ax, %ss
  12. # perform a temporary stack
  13. movw $(stack_base-_start), %ax
  14. movw %ax, %bp
  15. movw %ax, %sp
  16. call read_data
  17. ljmp $0x0050, $(loader_start-loader_start)
  18. die:
  19. hlt
  20. jmp die
  21. read_data:
  22. movw $(read_data_pack-_start), %si
  23. mov $0x42, %ah
  24. mov $0x80, %dl
  25. int $0x13
  26. ret
  27. string_hello:
  28. .string "Hello World!"
  29. read_data_pack:
  30. .byte 0x10, 0
  31. .word 16 # block count (read 8k)
  32. .word 0x0000 # offset address
  33. .word 0x0050 # segment address
  34. .long 1 # LBA to read
  35. stack_edge:
  36. .space 128
  37. stack_base:
  38. .space 510 - (.-_start)
  39. .word 0xaa55
  40. .section .text.loader
  41. # loader
  42. loader_start:
  43. # set segment registers
  44. movw %cs, %ax
  45. movw %ax, %ds
  46. # load gdt
  47. cli
  48. lgdt (asm_gdt_descriptor-loader_start)
  49. # enable protection enable (PE) bit
  50. movl %cr0, %eax
  51. orl $1, %eax
  52. movl %eax, %cr0
  53. ljmp $0x08, $0x0500 + (start_32bit-loader_start)
  54. .code32
  55. start_32bit:
  56. movw $16, %ax
  57. movw %ax, %ds
  58. movw %ax, %es
  59. movw %ax, %ss
  60. # set up stack
  61. movl $0x003fffff, %ebp
  62. movl $0x003fffff, %esp
  63. call kernel_main
  64. loader_halt:
  65. hlt
  66. jmp loader_halt
  67. asm_gdt_descriptor:
  68. .word (3 * 8) - 1 # size
  69. .long 0x0500+(asm_gdt_table-loader_start) # address
  70. .globl asm_gdt_descriptor
  71. .type asm_gdt_descriptor @object
  72. .size asm_gdt_descriptor, (.-asm_gdt_descriptor)
  73. asm_gdt_table:
  74. .8byte 0 # null descriptor
  75. # code segment
  76. .word 0x03ff # limit 0 :15
  77. .word 0x0000 # base 0 :15
  78. .byte 0x00 # base 16:23
  79. .byte 0x9a # access
  80. .byte 0b11000000 # flag and limit 16:20
  81. .byte 0x00 # base 24:31
  82. # data segment
  83. .word 0x03ff # limit 0 :15
  84. .word 0x0000 # base 0 :15
  85. .byte 0x40 # base 16:23
  86. .byte 0x92 # access
  87. .byte 0b11000000 # flag and limit 16:20
  88. .byte 0x00 # base 24:31