boot.S 5.7 KB

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