boot.S 4.4 KB

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