boot.S 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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, 0x40, 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. /* First kernel sector (LBA). Fixed header slot so the image builders
  33. * can patch it without depending on the code layout: the floppy image
  34. * keeps 1, the USB/HDD image patches the low byte (0x40 = LBA 64). */
  35. .org 0x40
  36. kernel_lba:
  37. .word 1
  38. .org 0x42
  39. loader_start:
  40. xorw %ax, %ax
  41. movw %ax, %ds
  42. movw %ax, %ss
  43. movw $0x7c00, %sp
  44. movb %dl, boot_drive
  45. cld
  46. movw $0x0003, %ax
  47. int $0x10
  48. /* First visible sign of life. This is before VBE, paging and the
  49. * kernel, and it is the only output a machine without serial has:
  50. * if the panel shows this line, the BIOS reached our boot sector. */
  51. movw $boot_msg, %si
  52. call print_string
  53. /* The kernel load can take a while on a BIOS USB stack; say what is
  54. * happening instead of leaving the operator with a blank screen. */
  55. movw $loading_msg, %si
  56. call print_string
  57. load_kernel:
  58. movw kernel_sectors, %cx
  59. movw kernel_lba, %si
  60. movw $0x07e0, %ax
  61. movw %ax, %es
  62. xorw %bx, %bx
  63. load_loop:
  64. cmpw $0, %cx
  65. jz load_done
  66. pushw %cx
  67. pushw %si
  68. movb boot_drive, %dl
  69. cmpb $0x80, %dl
  70. jb read_chs
  71. xorw %ax, %ax
  72. movw %ax, 0x50a
  73. movw %ax, 0x50c
  74. movw %ax, 0x50e
  75. movb $0x10, 0x500
  76. movb %al, 0x501
  77. incw %ax
  78. movw %ax, 0x502
  79. movw %bx, 0x504
  80. movw %es, 0x506
  81. movw %si, 0x508
  82. movw $0x500, %si
  83. movb $0x42, %ah
  84. jmp do_int13
  85. read_chs:
  86. movw %si, %ax
  87. xorw %dx, %dx
  88. movw $18, %di
  89. divw %di
  90. movb %dl, %cl
  91. incb %cl
  92. xorw %dx, %dx
  93. movw $2, %di
  94. divw %di
  95. movb %dl, %dh
  96. movb %al, %ch
  97. movb boot_drive, %dl
  98. movb $0x02, %ah
  99. movb $1, %al
  100. do_int13:
  101. int $0x13
  102. popw %si
  103. popw %cx
  104. jc read_error
  105. addw $0x200, %bx
  106. jnc 1f
  107. movw %es, %ax
  108. addw $0x1000, %ax
  109. movw %ax, %es
  110. 1: incw %si
  111. decw %cx
  112. jnz load_loop
  113. load_done:
  114. /* Second marker: the whole kernel image was read without an INT 13h
  115. * error. It stays on screen if the VESA stage fails or hangs before
  116. * it switches modes. */
  117. movw $loaded_msg, %si
  118. call print_string
  119. /* Jump to VESA stage at 0x7E00 (real mode). */
  120. ljmp $0x0000, $0x7E00
  121. boot_drive:
  122. .byte 0
  123. /* INT 13h error: say so instead of halting silently. */
  124. read_error:
  125. movw $disk_err_msg, %si
  126. call print_string
  127. halt:
  128. cli
  129. 1: hlt
  130. jmp 1b
  131. /* Print the NUL-terminated string at DS:SI with the BIOS teletype service. */
  132. print_string:
  133. pushw %ax
  134. pushw %bx
  135. movb $0x0E, %ah
  136. xorw %bx, %bx
  137. 1: lodsb
  138. testb %al, %al
  139. jz 2f
  140. int $0x10
  141. jmp 1b
  142. 2: popw %bx
  143. popw %ax
  144. ret
  145. boot_msg:
  146. .asciz "Nebos starting...\r\n"
  147. loading_msg:
  148. .asciz "Loading kernel...\r\n"
  149. loaded_msg:
  150. .asciz "Kernel loaded, starting...\r\n"
  151. disk_err_msg:
  152. .asciz "Disk read error\r\n"
  153. .org 0x1BE
  154. /* MBR partition table: one active partition at LBA 0 (USB/HDD). */
  155. .byte 0x80 /* bootable */
  156. .byte 0x00, 0x01, 0x00 /* CHS start: C=0, H=0, S=1 */
  157. .byte 0x06 /* type: FAT16 LBA (matches BPB) */
  158. .byte 0xFF, 0xFF, 0xFF /* CHS end: max values */
  159. .long 0 /* LBA start */
  160. .long 131072 /* sector count (64MB) */
  161. .space 48, 0 /* 3 empty partition entries */
  162. .org 0x1FE
  163. .word 0xAA55