vesa_stage.S 14 KB

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