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