vesa_stage.S 14 KB

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