boot.S 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. /* EDD reads up to 64 sectors per INT 13h into ES:0 and then advances
  62. * ES, so the offset never wraps (one call per sector made the kernel
  63. * load ~700 BIOS calls). CHS (floppy) stays at one sector: a longer
  64. * transfer could cross a 64 KB ISA DMA boundary. */
  65. load_loop:
  66. testw %cx, %cx
  67. jz load_done
  68. pushw %cx
  69. pushw %si
  70. movw $1, %ax
  71. movb boot_drive, %dl
  72. cmpb $0x80, %dl
  73. jb read_chs
  74. movw %cx, %ax
  75. cmpw chunk_max, %ax
  76. jbe 1f
  77. movw chunk_max, %ax
  78. 1: movw %ax, cur_count
  79. movw %ax, 0x502
  80. xorw %ax, %ax
  81. movw %ax, 0x504
  82. movw %ax, 0x50a
  83. movw %ax, 0x50c
  84. movw %ax, 0x50e
  85. movb $0x10, 0x500
  86. movb %al, 0x501
  87. movw %es, 0x506
  88. movw %si, 0x508
  89. movw $0x500, %si
  90. movb $0x42, %ah
  91. jmp do_int13
  92. read_chs:
  93. movw %ax, cur_count
  94. movw %si, %ax
  95. xorw %dx, %dx
  96. movw $18, %di
  97. divw %di
  98. movb %dl, %cl
  99. incb %cl
  100. xorw %dx, %dx
  101. movw $2, %di
  102. divw %di
  103. movb %dl, %dh
  104. movb %al, %ch
  105. movb boot_drive, %dl
  106. movb $0x02, %ah
  107. movb $1, %al
  108. xorw %bx, %bx
  109. do_int13:
  110. int $0x13
  111. popw %si
  112. popw %cx
  113. jnc 2f
  114. /* A BIOS that refuses a multi-sector EDD read: reset the drive and
  115. * continue one sector per call (the old behaviour). */
  116. cmpw $1, cur_count
  117. je read_error
  118. movw $1, chunk_max
  119. movb boot_drive, %dl
  120. xorw %ax, %ax
  121. int $0x13
  122. jmp load_loop
  123. 2: movw cur_count, %ax
  124. addw %ax, %si
  125. subw %ax, %cx
  126. shlw $5, %ax
  127. movw %es, %bx /* not DX: DL carries the boot drive */
  128. addw %ax, %bx /* into the VESA stage */
  129. movw %bx, %es
  130. jmp load_loop
  131. load_done:
  132. /* Second marker: the whole kernel image was read without an INT 13h
  133. * error. It stays on screen if the VESA stage fails or hangs before
  134. * it switches modes. */
  135. movw $loaded_msg, %si
  136. call print_string
  137. /* Jump to VESA stage at 0x7E00 (real mode). */
  138. ljmp $0x0000, $0x7E00
  139. boot_drive:
  140. .byte 0
  141. chunk_max:
  142. .word 64
  143. cur_count:
  144. .word 0
  145. /* INT 13h error: say so instead of halting silently. */
  146. read_error:
  147. movw $disk_err_msg, %si
  148. call print_string
  149. halt:
  150. cli
  151. 1: hlt
  152. jmp 1b
  153. /* Print the NUL-terminated string at DS:SI with the BIOS teletype service. */
  154. print_string:
  155. pushw %ax
  156. pushw %bx
  157. movb $0x0E, %ah
  158. xorw %bx, %bx
  159. 1: lodsb
  160. testb %al, %al
  161. jz 2f
  162. int $0x10
  163. jmp 1b
  164. 2: popw %bx
  165. popw %ax
  166. ret
  167. boot_msg:
  168. .asciz "Nebos starting...\r\n"
  169. loading_msg:
  170. .asciz "Loading kernel...\r\n"
  171. loaded_msg:
  172. .asciz "Kernel loaded, starting...\r\n"
  173. disk_err_msg:
  174. .asciz "Disk read error\r\n"
  175. .org 0x1BE
  176. /* MBR partition table: one active partition at LBA 0 (USB/HDD). */
  177. .byte 0x80 /* bootable */
  178. .byte 0x00, 0x01, 0x00 /* CHS start: C=0, H=0, S=1 */
  179. .byte 0x06 /* type: FAT16 LBA (matches BPB) */
  180. .byte 0xFF, 0xFF, 0xFF /* CHS end: max values */
  181. .long 0 /* LBA start */
  182. .long 131072 /* sector count (64MB) */
  183. .space 48, 0 /* 3 empty partition entries */
  184. .org 0x1FE
  185. .word 0xAA55