boot.S 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /* Boot sector: load contiguous kernel sectors 1..N to 0x7E00,
  2. then jump to 0x7E00 in real mode (VESA stage handles the rest). */
  3. .code16
  4. .section .text
  5. .globl _start
  6. _start:
  7. .byte 0xEB, 0x3E, 0x90
  8. /* ---- BPB (BIOS Parameter Block) ---- */
  9. .ascii "NEBOS " /* OEM name */
  10. .word 512 /* bytes per sector */
  11. .byte 4 /* sectors per cluster */
  12. .word 1 /* reserved sectors */
  13. .byte 2 /* number of FATs */
  14. .word 512 /* root entries */
  15. .word 0 /* total sectors 16-bit (use 32-bit) */
  16. .byte 0xF8 /* media descriptor (hard disk) */
  17. .word 128 /* sectors per FAT */
  18. .word 63 /* sectors per track */
  19. .word 128 /* number of heads */
  20. .long 0 /* hidden sectors */
  21. .long 131072 /* total sectors 32-bit */
  22. /* ---- Extended BPB ---- */
  23. .byte 0x80 /* drive number */
  24. .byte 0x00 /* reserved */
  25. .byte 0x29 /* boot signature */
  26. .long 0x12345678 /* volume ID */
  27. .ascii "NEBOS " /* volume label */
  28. .ascii "FAT16 " /* filesystem type */
  29. .org 0x3E
  30. kernel_sectors:
  31. .word 1
  32. .org 0x40
  33. loader_start:
  34. xorw %ax, %ax
  35. movw %ax, %ds
  36. movw %ax, %ss
  37. movw $0x7c00, %sp
  38. movb %dl, boot_drive
  39. movw $0x0003, %ax
  40. int $0x10
  41. load_kernel:
  42. movw kernel_sectors, %cx
  43. movw $1, %si
  44. movw $0x07e0, %ax
  45. movw %ax, %es
  46. xorw %bx, %bx
  47. load_loop:
  48. cmpw $0, %cx
  49. jz load_done
  50. pushw %cx
  51. pushw %si
  52. movb boot_drive, %dl
  53. cmpb $0x80, %dl
  54. jb read_chs
  55. xorw %ax, %ax
  56. movw %ax, 0x50a
  57. movw %ax, 0x50c
  58. movw %ax, 0x50e
  59. movb $0x10, 0x500
  60. movb %al, 0x501
  61. incw %ax
  62. movw %ax, 0x502
  63. movw %bx, 0x504
  64. movw %es, 0x506
  65. movw %si, 0x508
  66. movw $0x500, %si
  67. movb $0x42, %ah
  68. jmp do_int13
  69. read_chs:
  70. movw %si, %ax
  71. xorw %dx, %dx
  72. movw $18, %di
  73. divw %di
  74. movb %dl, %cl
  75. incb %cl
  76. xorw %dx, %dx
  77. movw $2, %di
  78. divw %di
  79. movb %dl, %dh
  80. movb %al, %ch
  81. movb boot_drive, %dl
  82. movb $0x02, %ah
  83. movb $1, %al
  84. do_int13:
  85. int $0x13
  86. popw %si
  87. popw %cx
  88. jc halt
  89. addw $0x200, %bx
  90. jnc 1f
  91. movw %es, %ax
  92. addw $0x1000, %ax
  93. movw %ax, %es
  94. 1: incw %si
  95. decw %cx
  96. jnz load_loop
  97. load_done:
  98. /* Jump to VESA stage at 0x7E00 (real mode). */
  99. ljmp $0x0000, $0x7E00
  100. boot_drive:
  101. .byte 0
  102. halt:
  103. cli
  104. 1: hlt
  105. jmp 1b
  106. .org 0x1BE
  107. /* MBR partition table: one active partition at LBA 0 (USB/HDD). */
  108. .byte 0x80 /* bootable */
  109. .byte 0x00, 0x01, 0x00 /* CHS start: C=0, H=0, S=1 */
  110. .byte 0x06 /* type: FAT16 LBA (matches BPB) */
  111. .byte 0xFF, 0xFF, 0xFF /* CHS end: max values */
  112. .long 0 /* LBA start */
  113. .long 131072 /* sector count (64MB) */
  114. .space 48, 0 /* 3 empty partition entries */
  115. .org 0x1FE
  116. .word 0xAA55