vesa_stage.S 14 KB

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