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