boot.S 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. /* Short jump over the BPB and the fixed header slots to loader_start.
  8. * The displacement is computed so the header layout below can change
  9. * without leaving the jump inside a header field (a hardcoded 0x40
  10. * landed in pack_lba, where 0x0E decoded as a harmless push cs and
  11. * 0x0F as a faulting larw). */
  12. .byte 0xEB
  13. .byte loader_start - _start - 2
  14. .byte 0x90
  15. /* ---- BPB (BIOS Parameter Block) ---- */
  16. .ascii "NEBOS " /* OEM name */
  17. .word 512 /* bytes per sector */
  18. .byte 4 /* sectors per cluster */
  19. .word 1 /* reserved sectors */
  20. .byte 2 /* number of FATs */
  21. .word 512 /* root entries */
  22. .word 0 /* total sectors 16-bit (use 32-bit) */
  23. .byte 0xF8 /* media descriptor (hard disk) */
  24. .word 128 /* sectors per FAT */
  25. .word 63 /* sectors per track */
  26. .word 128 /* number of heads */
  27. .long 0 /* hidden sectors */
  28. .long 131072 /* total sectors 32-bit */
  29. /* ---- Extended BPB ---- */
  30. .byte 0x80 /* drive number */
  31. .byte 0x00 /* reserved */
  32. .byte 0x29 /* boot signature */
  33. .long 0x12345678 /* volume ID */
  34. .ascii "NEBOS " /* volume label */
  35. .ascii "FAT16 " /* filesystem type */
  36. .org 0x3E
  37. kernel_sectors:
  38. .word 1
  39. /* First kernel sector (LBA). Fixed header slot so the image builders
  40. * can patch it without depending on the code layout: the floppy image
  41. * keeps 1, the USB/HDD image patches the low byte (0x40 = LBA 64). */
  42. .org 0x40
  43. kernel_lba:
  44. .word 1
  45. /* Rootfs image LBA (see rootfs.h). Patched by the image builders:
  46. * floppy/final.bin point it just past the padded kernel, usb.img at
  47. * its rootfs partition. */
  48. .org 0x42
  49. pack_lba:
  50. .long 1
  51. .org 0x46
  52. loader_start:
  53. xorw %ax, %ax
  54. movw %ax, %ds
  55. movw %ax, %ss
  56. movw $0x7c00, %sp
  57. movb %dl, boot_drive
  58. cld
  59. movw $0x0003, %ax
  60. int $0x10
  61. /* First visible sign of life. This is before VBE, paging and the
  62. * kernel, and it is the only output a machine without serial has:
  63. * if the panel shows this line, the BIOS reached our boot sector. */
  64. movw $boot_msg, %si
  65. call print_string
  66. /* The kernel load can take a while on a BIOS USB stack; say what is
  67. * happening instead of leaving the operator with a blank screen. */
  68. movw $loading_msg, %si
  69. call print_string
  70. load_kernel:
  71. movw kernel_sectors, %cx
  72. movw kernel_lba, %si
  73. movw $0x07e0, %ax
  74. movw %ax, %es
  75. xorw %bx, %bx
  76. load_loop:
  77. cmpw $0, %cx
  78. jz load_done
  79. pushw %cx
  80. pushw %si
  81. movb boot_drive, %dl
  82. cmpb $0x80, %dl
  83. jb read_chs
  84. xorw %ax, %ax
  85. movw %ax, 0x50a
  86. movw %ax, 0x50c
  87. movw %ax, 0x50e
  88. movb $0x10, 0x500
  89. movb %al, 0x501
  90. incw %ax
  91. movw %ax, 0x502
  92. movw %bx, 0x504
  93. movw %es, 0x506
  94. movw %si, 0x508
  95. movw $0x500, %si
  96. movb $0x42, %ah
  97. jmp do_int13
  98. read_chs:
  99. movw %si, %ax
  100. xorw %dx, %dx
  101. movw $18, %di
  102. divw %di
  103. movb %dl, %cl
  104. incb %cl
  105. xorw %dx, %dx
  106. movw $2, %di
  107. divw %di
  108. movb %dl, %dh
  109. movb %al, %ch
  110. movb boot_drive, %dl
  111. movb $0x02, %ah
  112. movb $1, %al
  113. do_int13:
  114. int $0x13
  115. popw %si
  116. popw %cx
  117. jc read_error
  118. addw $0x200, %bx
  119. jnc 1f
  120. movw %es, %ax
  121. addw $0x1000, %ax
  122. movw %ax, %es
  123. 1: incw %si
  124. decw %cx
  125. jnz load_loop
  126. load_done:
  127. /* Second marker: the whole kernel image was read without an INT 13h
  128. * error. It stays on screen if the VESA stage fails or hangs before
  129. * it switches modes. */
  130. movw $loaded_msg, %si
  131. call print_string
  132. /* Jump to VESA stage at 0x7E00 (real mode). */
  133. ljmp $0x0000, $0x7E00
  134. boot_drive:
  135. .byte 0
  136. /* INT 13h error: say so instead of halting silently. */
  137. read_error:
  138. movw $disk_err_msg, %si
  139. call print_string
  140. halt:
  141. cli
  142. 1: hlt
  143. jmp 1b
  144. /* Print the NUL-terminated string at DS:SI with the BIOS teletype service. */
  145. print_string:
  146. pushw %ax
  147. pushw %bx
  148. movb $0x0E, %ah
  149. xorw %bx, %bx
  150. 1: lodsb
  151. testb %al, %al
  152. jz 2f
  153. int $0x10
  154. jmp 1b
  155. 2: popw %bx
  156. popw %ax
  157. ret
  158. boot_msg:
  159. .asciz "Nebos starting...\r\n"
  160. loading_msg:
  161. .asciz "Loading kernel...\r\n"
  162. loaded_msg:
  163. .asciz "Kernel loaded, starting...\r\n"
  164. disk_err_msg:
  165. .asciz "Disk read error\r\n"
  166. .org 0x1BE
  167. /* MBR partition table: one active partition at LBA 0 (USB/HDD). */
  168. .byte 0x80 /* bootable */
  169. .byte 0x00, 0x01, 0x00 /* CHS start: C=0, H=0, S=1 */
  170. .byte 0x06 /* type: FAT16 LBA (matches BPB) */
  171. .byte 0xFF, 0xFF, 0xFF /* CHS end: max values */
  172. .long 0 /* LBA start */
  173. .long 131072 /* sector count (64MB) */
  174. .space 48, 0 /* 3 empty partition entries */
  175. .org 0x1FE
  176. .word 0xAA55