vesa_stage.S 14 KB

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