vesa_stage.S 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. /*
  2. * VESA real-mode stage: runs at 0x7E00 before protected mode.
  3. *
  4. * Called by the boot sector in real mode (CS=0, IP=0x7E00).
  5. * Probes the E820 map, loads the compressed command-pack rootfs into the
  6. * low gap at 0x60000..0x9FBFF (16 KB staging chunks at 0x3000, BIOS disk
  7. * reads only), walks the VBE mode list and picks the best mode (highest
  8. * resolution, >= 16 bpp, LFB, fits in VRAM), writes the mode info to 0x600
  9. * and the boot info to 0x500, then enables A20, enters protected mode and
  10. * jumps to kernel_entry.
  11. *
  12. * If no VESA mode works, boots in text mode (valid=0 at 0x600).
  13. */
  14. .code16
  15. .section .text.stage2, "ax", @progbits
  16. .globl _vesa_stage
  17. /* E820 map constants (must match mm.h). */
  18. .equ E820_MAGIC, 0xE820E820
  19. .equ E820_BUF_END, 0x1008 + 32*24 /* 32 entries * 24 bytes */
  20. /* Rootfs/boot-info constants (must match rootfs.h / bootinfo.h). */
  21. .equ ROOTFS_VERSION, 1
  22. .equ PACK_LOW_ADDR, 0x60000 /* low gap behind the kernel */
  23. .equ PACK_LOW_MAX, 0x3FBFF /* 0x60000..0x9FBFF */
  24. .equ BOOT_INFO_MAGIC, 0x4E425349
  25. .equ BOOT_INFO_VERSION, 2
  26. .equ BOOT_INFO_WORDS, 12
  27. _vesa_stage:
  28. xorw %ax, %ax
  29. movw %ax, %ds
  30. movw %ax, %es
  31. cld
  32. movb %dl, stage_drive
  33. /* Ask the BIOS whether this drive supports EDD LBA reads. USB sticks
  34. * are sometimes presented as floppies (DL=0); the CHS geometry used
  35. * below cannot reach a rootfs image there. */
  36. movb stage_drive, %dl
  37. movb $0x41, %ah
  38. movw $0x55AA, %bx
  39. int $0x13
  40. jc 1f
  41. cmpw $0xAA55, %bx
  42. jne 1f
  43. testb $0x01, %cl
  44. jz 1f
  45. movb $1, use_lba
  46. 1: xorw %ax, %ax
  47. movw %ax, %ds
  48. movw %ax, %es
  49. /* No UEFI boot-info block on the BIOS path (see bootinfo.h). The boot
  50. * sector used 0x500 as its disk-address packet, so clear the magic;
  51. * write_bootinfo fills in the real block before the PM jump. */
  52. movl $0, 0x500
  53. /* ---- Probe INT 15h E820 memory map into 0:0x1000 ----
  54. * Layout (see mm.h): [magic u32][count u32][24-byte entries...].
  55. * Stored below the kernel (0x7E00) so it survives kexec reloads,
  56. * which re-run kernel_main but not this real-mode stage. */
  57. movl $E820_MAGIC, 0x1000 /* magic */
  58. movl $0, 0x1004 /* count = 0 */
  59. xorl %ebx, %ebx /* continuation value */
  60. movl $0x1008, %edi /* ES:DI -> first entry */
  61. e820_loop:
  62. xorw %ax, %ax
  63. movw %ax, %es /* (re)set ES for the BIOS buffer */
  64. movl $0xE820, %eax
  65. movl $0x534D4150, %edx /* "SMAP" */
  66. movl $24, %ecx
  67. int $0x15
  68. jc e820_done /* CF set -> unsupported / done */
  69. cmpl $0x534D4150, %eax
  70. jne e820_done /* not "SMAP" -> done */
  71. cmpb $20, %cl
  72. jb e820_done /* too short -> stop */
  73. movl $0, 20(%edi) /* zero ACPI attrs (BIOS may write 20 B) */
  74. incl 0x1004 /* count++ */
  75. addl $24, %edi
  76. testl %ebx, %ebx
  77. jz e820_done /* continuation == 0 -> done */
  78. cmpl $E820_BUF_END, %edi
  79. jae e820_done /* buffer full -> stop */
  80. jmp e820_loop
  81. e820_done:
  82. /* ---- Rootfs: load the command pack into the low gap ----
  83. * The pack is a gzip blob behind a one-sector NBROOT1 header (rootfs.h).
  84. * Keep the destination at 0x60000..0x9FBFF: above the kernel BSS/boot
  85. * stack, below the EBDA, and inside the low 1 MB the frame allocator
  86. * already reserves. */
  87. xorw %ax, %ax
  88. movw %ax, %ds
  89. movw %ax, %es
  90. movl 0x7C42, %eax /* boot-sector pack_lba field */
  91. movl %eax, pack_lba
  92. movl $0, pack_addr
  93. movl $0, pack_bytes
  94. movw $msg_pack, %si
  95. call print_str
  96. movzbl stage_drive, %eax
  97. call print_hex32
  98. movw $msg_lba, %si
  99. call print_str
  100. movl pack_lba, %eax
  101. call print_hex32
  102. movw $msg_e, %si
  103. call print_str
  104. movzbl use_lba, %eax
  105. addl $'0', %eax
  106. movb $0x0E, %ah
  107. movb $0x00, %bh
  108. int $0x10
  109. movw $msg_m, %si
  110. call print_str
  111. cmpb $0x80, stage_drive
  112. jb 1f
  113. movb $'L', %al
  114. jmp 2f
  115. 1: movb $'C', %al
  116. 2: movb $0x0E, %ah
  117. movb $0x00, %bh
  118. int $0x10
  119. movw $msg_end, %si
  120. call print_str
  121. xorw %ax, %ax
  122. movw %ax, %ds
  123. movw %ax, %es
  124. /* Read and validate the header, then copy the payload in 16 KB
  125. * chunks (32 sectors) through the 0x3000 staging buffer. */
  126. cmpl $0, pack_lba
  127. je pack_fail
  128. movl pack_lba, %eax
  129. movw $1, %cx
  130. call read_sectors
  131. jc pack_fail
  132. /* Validate the header at 0x3000: "NBROOT1\0", version 1. */
  133. movl $0x3000, %esi
  134. movl 0(%esi), %eax
  135. cmpl $0x4F52424E, %eax /* "NBRO" */
  136. jne pack_fail
  137. movl 4(%esi), %eax
  138. cmpl $0x0031544F, %eax /* "OT1\0" */
  139. jne pack_fail
  140. cmpl $ROOTFS_VERSION, 8(%esi)
  141. jne pack_fail
  142. movl 12(%esi), %eax /* pack_bytes */
  143. testl %eax, %eax
  144. jz pack_fail
  145. cmpl $PACK_LOW_MAX, %eax
  146. ja pack_fail
  147. movl %eax, pack_bytes
  148. movw $msg_dot, %si
  149. call print_str
  150. movl $0, pack_off
  151. load_loop:
  152. movl pack_off, %eax
  153. cmpl pack_bytes, %eax
  154. jae load_ok
  155. movl pack_bytes, %ecx
  156. subl %eax, %ecx
  157. addl $511, %ecx
  158. shrl $9, %ecx
  159. cmpl $32, %ecx
  160. jbe 1f
  161. movl $32, %ecx
  162. 1: movl %ecx, chunk_sectors
  163. movl pack_off, %eax
  164. shrl $9, %eax
  165. addl pack_lba, %eax
  166. incl %eax /* pack data starts after the header */
  167. call read_sectors
  168. jc pack_fail
  169. movl chunk_sectors, %eax
  170. shll $9, %eax
  171. movl %eax, copy_len
  172. movl $PACK_LOW_ADDR, %eax
  173. addl pack_off, %eax
  174. movl %eax, copy_dst
  175. call copy_low
  176. movl chunk_sectors, %eax
  177. shll $9, %eax
  178. addl %eax, pack_off
  179. movw $msg_dot, %si
  180. call print_str
  181. jmp load_loop
  182. load_ok:
  183. movl $PACK_LOW_ADDR, pack_addr
  184. movw $msg_ok, %si
  185. call print_str
  186. jmp pack_done
  187. pack_fail:
  188. movw $msg_fail, %si
  189. call print_str
  190. pack_clear:
  191. movl $0, pack_addr
  192. movl $0, pack_bytes
  193. pack_done:
  194. /* read_sectors leaves ES at the staging segment; the VBE calls below
  195. * expect a normal zero segment. */
  196. xorw %ax, %ax
  197. movw %ax, %ds
  198. movw %ax, %es
  199. /* ---- VBE Controller Info into 0:0x2000 ---- */
  200. movw $0x2000, %di
  201. movw $0x4256, (%di) /* "VB" */
  202. movw $0x3245, 2(%di) /* "E2" */
  203. movw $0x4F00, %ax
  204. int $0x10
  205. cmpw $0x004F, %ax
  206. jnz vesa_no_vbe
  207. /* Save VRAM size (offset 0x12: TotalMemory in 64KB blocks). */
  208. movw 0x12(%di), %ax
  209. testw %ax, %ax
  210. jnz 1f
  211. movw $0xFFFF, %ax
  212. 1: movw %ax, vram_64k
  213. /* Get mode list pointer (offset 0x0E: far pointer seg:off). */
  214. movw 0x200E, %si /* offset (buffer at 0x2000) */
  215. movw 0x2010, %ax /* segment */
  216. movw %ax, %fs /* FS:SI = mode list */
  217. /* ---- Walk mode list, pick best mode ---- */
  218. movw $0, best_mode
  219. movl $0, best_score
  220. walk_modes:
  221. movw %fs:(%si), %cx
  222. cmpw $0xFFFF, %cx
  223. jz walk_done
  224. addw $2, %si
  225. /* Query mode info: VBE 4F01h into 0:0x2200. */
  226. pushw %si
  227. pushw %cx
  228. xorw %ax, %ax
  229. movw %ax, %es
  230. movw $0x2200, %di
  231. movw $0x4F01, %ax
  232. int $0x10
  233. popw %cx
  234. popw %si
  235. cmpw $0x004F, %ax
  236. jnz walk_modes
  237. /* Check: supported (bit 0) + graphics (bit 4) + LFB (bit 7). */
  238. movw 0x2200, %ax
  239. andw $0x0091, %ax
  240. cmpw $0x0091, %ax
  241. jnz walk_modes
  242. /* Check: BitsPerPixel >= 16. */
  243. cmpb $16, 0x2219
  244. jb walk_modes
  245. /* TODO: Remove this width cap when display scaling is improved. */
  246. cmpw $1280, 0x2212
  247. ja walk_modes
  248. /* Check: fits in VRAM. */
  249. movw 0x2210, %ax
  250. mulw 0x2214
  251. testw %ax, %ax
  252. jz 2f
  253. incw %dx
  254. 2: cmpw vram_64k, %dx
  255. ja walk_modes
  256. /* Score = width * height + bpp. */
  257. movzwl 0x2212, %eax
  258. movzwl 0x2214, %edx
  259. imull %edx, %eax
  260. movzbl 0x2219, %edx
  261. addl %edx, %eax
  262. cmpl best_score, %eax
  263. jbe walk_modes
  264. movl %eax, best_score
  265. movw %cx, best_mode
  266. /* Save mode info block to 0:0x2400. */
  267. pushw %si
  268. pushw %cx
  269. movw $0x2200, %si
  270. movw $0x2400, %di
  271. movw $128, %cx
  272. cld
  273. rep movsw
  274. popw %cx
  275. popw %si
  276. jmp walk_modes
  277. walk_done:
  278. /* Did we find a mode? */
  279. movw best_mode, %cx
  280. testw %cx, %cx
  281. jz vesa_no_vbe
  282. /* ---- Set the mode ---- */
  283. movw %cx, %bx
  284. orw $0x4000, %bx /* LFB */
  285. movw $0x4F02, %ax
  286. int $0x10
  287. cmpw $0x004F, %ax
  288. jnz vesa_no_vbe
  289. /* ---- Fill 0x600 boot FB info from saved block at 0x2400 ---- */
  290. movw $0x600, %bx
  291. movb $1, (%bx)
  292. movw 0x2412, %ax
  293. movw %ax, 1(%bx) /* width */
  294. movw 0x2414, %ax
  295. movw %ax, 3(%bx) /* height */
  296. movw 0x2410, %ax
  297. movw %ax, 5(%bx) /* pitch */
  298. movb 0x2419, %al
  299. movb %al, 7(%bx) /* bpp */
  300. movw 0x2428, %ax
  301. movw %ax, 8(%bx) /* lfb_base lo */
  302. movw 0x242A, %ax
  303. movw %ax, 10(%bx) /* lfb_base hi */
  304. movw vram_64k, %ax
  305. movw %ax, 12(%bx) /* vram_64k */
  306. jmp stage_finish
  307. vesa_no_vbe:
  308. /* No VESA: stay in text mode, mark fb info invalid. */
  309. movw $0x600, %bx
  310. movb $0, (%bx)
  311. stage_finish:
  312. /* The pack is loaded; enable A20 and enter protected mode. */
  313. call enable_a20
  314. call write_bootinfo
  315. jmp enter_pm
  316. /* ---- A20 ---------------------------------------------------------------- */
  317. enable_a20:
  318. movw $0x2401, %ax
  319. int $0x15
  320. call a20_wait
  321. movb $0xD1, %al
  322. outb %al, $0x64
  323. call a20_wait
  324. movb $0xDF, %al
  325. outb %al, $0x60
  326. call a20_wait
  327. inb $0x92, %al
  328. testb $0x02, %al
  329. jnz 1f
  330. orb $0x02, %al
  331. andb $0xFE, %al
  332. outb %al, $0x92
  333. 1: ret
  334. a20_wait:
  335. inb $0x64, %al
  336. testb $0x02, %al
  337. jnz a20_wait
  338. ret
  339. /* ---- Read sectors via INT 13h ------------------------------------------
  340. * in: eax = LBA, cx = sector count
  341. * out: data at 0x3000, CF set on failure
  342. * Clobbers all general registers. */
  343. read_sectors:
  344. movl %eax, rd_lba
  345. movw %cx, rd_left
  346. movw $0, rd_off
  347. movw $0, rd_tries
  348. read_retry:
  349. movw rd_off, %bx
  350. xorw %ax, %ax
  351. movw %ax, %ds
  352. movw $0x0300, %ax
  353. movw %ax, %es
  354. movb stage_drive, %dl
  355. cmpb $0x80, %dl
  356. jb read_chs
  357. read_lba:
  358. /* EDD LBA read */
  359. movw $0x10, 0x500
  360. movw $1, 0x502
  361. movw %bx, 0x504
  362. movw $0x0300, 0x506
  363. movl rd_lba, %eax
  364. movl %eax, 0x508
  365. movl $0, 0x50C
  366. movw $0x500, %si
  367. movb $0x42, %ah
  368. jmp read_int
  369. read_chs:
  370. movw rd_lba, %ax
  371. xorw %dx, %dx
  372. movw $18, %di
  373. divw %di
  374. movb %dl, %cl
  375. incb %cl
  376. xorw %dx, %dx
  377. movw $2, %di
  378. divw %di
  379. movb %dl, %dh
  380. movb %al, %ch
  381. movb stage_drive, %dl
  382. movb $0x02, %ah
  383. movb $1, %al
  384. movw rd_off, %bx
  385. read_int:
  386. int $0x13
  387. jnc read_ok
  388. /* Reset and retry the same sector a few times. */
  389. pushw %cx
  390. movb stage_drive, %dl
  391. xorw %ax, %ax
  392. int $0x13
  393. popw %cx
  394. incw rd_tries
  395. cmpw $3, rd_tries
  396. jb read_retry
  397. stc
  398. ret
  399. read_ok:
  400. incl rd_lba
  401. addw $0x200, rd_off
  402. decw rd_left
  403. jnz read_retry
  404. clc
  405. ret
  406. /* ---- Print a NUL-terminated string with the BIOS teletype ------------- */
  407. print_str:
  408. pushw %ax
  409. pushw %bx
  410. movb $0x0E, %ah
  411. xorw %bx, %bx
  412. 1: lodsb
  413. testb %al, %al
  414. jz 2f
  415. int $0x10
  416. jmp 1b
  417. 2: popw %bx
  418. popw %ax
  419. ret
  420. /* ---- Copy the 16 KB staging chunk to the low pack destination --------- */
  421. copy_low:
  422. /* Read the stage variables with DS=0 first; only then switch DS to the
  423. * staging segment. Save both segment registers: the load loop and the
  424. * progress prints keep using DS=0/ES=0. */
  425. movl copy_dst, %eax
  426. movl copy_len, %ecx
  427. movl %eax, %ebx
  428. shrl $4, %eax
  429. pushw %ds
  430. pushw %es
  431. movw %ax, %es
  432. andl $0x0000000F, %ebx
  433. movw %bx, %di
  434. movw $0x0300, %ax
  435. movw %ax, %ds
  436. xorw %si, %si
  437. cld
  438. rep movsb
  439. popw %es
  440. popw %ds
  441. ret
  442. /* ---- Hex print (EAX, 8 digits) ----------------------------------------- */
  443. print_hex32:
  444. movl $8, %ecx
  445. jmp print_hex
  446. print_hex:
  447. pushal
  448. movl %eax, %ebx
  449. 1: roll $4, %ebx
  450. movl %ebx, %eax
  451. andl $0x0F, %eax
  452. cmpl $10, %eax
  453. jb 2f
  454. addl $55, %eax
  455. jmp 3f
  456. 2: addl $48, %eax
  457. 3: pushl %ebx
  458. pushl %ecx
  459. movb $0x0E, %ah
  460. xorw %bx, %bx
  461. int $0x10
  462. popl %ecx
  463. popl %ebx
  464. loop 1b
  465. popal
  466. ret
  467. /* ---- Boot-info block at 0x500 (bootinfo.h) ----------------------------
  468. * twelve dwords: magic, version, checksum, rsdp, flags, pack_addr,
  469. * pack_size, reserved[5]; the checksum makes the sum zero. */
  470. write_bootinfo:
  471. xorw %ax, %ax
  472. movw %ax, %ds
  473. movl $BOOT_INFO_MAGIC, 0x500
  474. movl $BOOT_INFO_VERSION, 0x504
  475. movl $0, 0x508
  476. movl $0, 0x50C
  477. movl $0, 0x510
  478. movl pack_addr, %eax
  479. movl %eax, 0x514
  480. movl pack_bytes, %eax
  481. movl %eax, 0x518
  482. movl $0, 0x51C
  483. movl $0, 0x520
  484. movl $0, 0x524
  485. movl $0, 0x528
  486. movl $0, 0x52C
  487. xorl %eax, %eax
  488. movl $0x500, %esi
  489. movl $BOOT_INFO_WORDS, %ecx
  490. 1: addl (%esi), %eax
  491. addl $4, %esi
  492. loop 1b
  493. negl %eax
  494. movl %eax, 0x508
  495. ret
  496. /* ---- Protected-mode entry ---------------------------------------------- */
  497. enter_pm:
  498. cli
  499. lgdt stage2_gdtr
  500. movl %cr0, %eax
  501. orl $1, %eax
  502. movl %eax, %cr0
  503. .byte 0x66, 0xea
  504. .long stage2_pm
  505. .word 0x08
  506. .code32
  507. stage2_pm:
  508. movw $0x10, %ax
  509. movw %ax, %ds
  510. movw %ax, %es
  511. movw %ax, %fs
  512. movw %ax, %gs
  513. movw %ax, %ss
  514. movl $__boot_stack_top, %esp
  515. jmp kernel_entry
  516. .align 4
  517. stage2_gdt:
  518. .quad 0
  519. .word 0xffff, 0x0000
  520. .byte 0x00, 0x9a, 0xcf, 0x00
  521. .word 0xffff, 0x0000
  522. .byte 0x00, 0x92, 0xcf, 0x00
  523. stage2_gdt_end:
  524. stage2_gdtr:
  525. .word stage2_gdt_end - stage2_gdt - 1
  526. .long stage2_gdt
  527. /* ---- Stage variables --------------------------------------------------- */
  528. .align 4
  529. stage_drive: .byte 0
  530. use_lba: .byte 0
  531. pack_lba: .long 0
  532. pack_bytes: .long 0
  533. pack_addr: .long 0
  534. pack_off: .long 0
  535. chunk_sectors: .long 0
  536. copy_dst: .long 0
  537. copy_len: .long 0
  538. rd_lba: .long 0
  539. rd_off: .word 0
  540. rd_left: .word 0
  541. rd_tries: .word 0
  542. msg_pack: .asciz "Loading rootfs [d="
  543. msg_lba: .asciz " lba="
  544. msg_e: .asciz " e="
  545. msg_m: .asciz " m="
  546. msg_end: .asciz "]... "
  547. msg_dot: .asciz "."
  548. msg_ok: .asciz " ok\r\n"
  549. msg_fail: .asciz " failed\r\n"
  550. .align 2
  551. vram_64k:
  552. .word 0
  553. best_mode:
  554. .word 0
  555. best_score:
  556. .long 0