Index: HelenOS.config
===================================================================
--- HelenOS.config	(revision 99d6fd07a4e12eb667d2cfa2d3fdb3b5f63800b2)
+++ HelenOS.config	(revision 421c8338127caee7f13ab5e70216f4ed042e8b22)
@@ -421,30 +421,21 @@
 ! [(CONFIG_HID_OUT=generic|CONFIG_HID_OUT=monitor)&PLATFORM=sparc64&MACHINE=generic] CONFIG_FB (y/n)
 
-% Framebuffer width
-@ "640"
-@ "800"
-@ "1024"
-@ "1152"
-@ "1280"
-@ "1400"
-@ "1440"
-@ "1600"
-@ "2048"
-! [(PLATFORM=ia32|PLATFORM=amd64)&CONFIG_HID_OUT!=none&CONFIG_FB=y] CONFIG_VESA_WIDTH (choice)
-
-% Framebuffer height
-@ "480"
-@ "600"
-@ "768"
-@ "852"
-@ "900"
-@ "960"
-@ "1024"
-@ "1050"
-@ "1200"
-@ "1536"
-! [(PLATFORM=ia32|PLATFORM=amd64)&CONFIG_HID_OUT!=none&CONFIG_FB=y] CONFIG_VESA_HEIGHT (choice)
-
-% Framebuffer depth
+% Default framebuffer mode
+@ "640x480"
+@ "800x600"
+@ "1024x768"
+@ "1152x720"
+@ "1152x864"
+@ "1280x960"
+@ "1280x1024"
+@ "1400x1050"
+@ "1440x900"
+@ "1440x1050"
+@ "1600x1200"
+@ "1920x1080"
+@ "1920x1200"
+! [(PLATFORM=ia32|PLATFORM=amd64)&CONFIG_HID_OUT!=none&CONFIG_FB=y] CONFIG_VESA_MODE (choice)
+
+% Default framebuffer depth
 @ "8"
 @ "16"
Index: defaults/amd64/Makefile.config
===================================================================
--- defaults/amd64/Makefile.config	(revision 99d6fd07a4e12eb667d2cfa2d3fdb3b5f63800b2)
+++ defaults/amd64/Makefile.config	(revision 421c8338127caee7f13ab5e70216f4ed042e8b22)
@@ -50,11 +50,8 @@
 CONFIG_FB = y
 
-# Framebuffer width
-CONFIG_VESA_WIDTH = 800
+# Default framebuffer mode
+CONFIG_VESA_MODE = 800x600
 
-# Framebuffer height
-CONFIG_VESA_HEIGHT = 600
-
-# Framebuffer depth
+# Default framebuffer depth
 CONFIG_VESA_BPP = 16
 
Index: defaults/ia32/Makefile.config
===================================================================
--- defaults/ia32/Makefile.config	(revision 99d6fd07a4e12eb667d2cfa2d3fdb3b5f63800b2)
+++ defaults/ia32/Makefile.config	(revision 421c8338127caee7f13ab5e70216f4ed042e8b22)
@@ -56,11 +56,8 @@
 CONFIG_FB = y
 
-# Framebuffer width
-CONFIG_VESA_WIDTH = 800
+# Default framebuffer mode
+CONFIG_VESA_MODE = 800x600
 
-# Framebuffer height
-CONFIG_VESA_HEIGHT = 600
-
-# Framebuffer depth
+# Default framebuffer depth
 CONFIG_VESA_BPP = 16
 
Index: kernel/arch/amd64/src/boot/boot.S
===================================================================
--- kernel/arch/amd64/src/boot/boot.S	(revision 99d6fd07a4e12eb667d2cfa2d3fdb3b5f63800b2)
+++ kernel/arch/amd64/src/boot/boot.S	(revision 421c8338127caee7f13ab5e70216f4ed042e8b22)
@@ -38,5 +38,5 @@
 
 #define START_STACK	(BOOT_OFFSET - BOOT_STACK_SIZE)
-	
+
 .section K_TEXT_START, "ax"
 
@@ -47,5 +47,5 @@
 	.long MULTIBOOT_HEADER_MAGIC
 	.long MULTIBOOT_HEADER_FLAGS
-	.long -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS)	# checksum
+	.long -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS)  # checksum
 	.long multiboot_header
 	.long unmapped_ktext_start
@@ -56,13 +56,17 @@
 multiboot_image_start:
 	cld
-	movl $START_STACK, %esp			# initialize stack pointer
-	lgdtl bootstrap_gdtr			# initialize Global Descriptor Table register
-
+	movl $START_STACK, %esp             # initialize stack pointer
+	lgdtl bootstrap_gdtr                # initialize Global Descriptor Table register
+	
 	movw $gdtselector(KDATA_DES), %cx
 	movw %cx, %es
-	movw %cx, %ds							# kernel data + stack
+	movw %cx, %ds                       # kernel data + stack
 	movw %cx, %ss
+	
+	#
 	# Simics seems to remove hidden part of GS on entering user mode
-	#  when _visible_ part of GS does not point to user-mode segment
+	# when _visible_ part of GS does not point to user-mode segment.
+	#
+	
 	movw $gdtselector(UDATA_DES), %cx
 	movw %cx, %fs
@@ -72,15 +76,17 @@
 	multiboot_meeting_point:
 	
-	movl %eax, grub_eax						# save parameters from GRUB
+	movl %eax, grub_eax                 # save parameters from GRUB
 	movl %ebx, grub_ebx
 	
+	#
 	# Protected 32-bit. We want to reuse the code-seg descriptor,
-	# the Default operand size must not be 1 when entering long mode
-	
-	movl $(INTEL_CPUID_EXTENDED), %eax  
- 	cpuid
- 	cmp $(INTEL_CPUID_EXTENDED), %eax
+	# the Default operand size must not be 1 when entering long mode.
+	#
+	
+	movl $(INTEL_CPUID_EXTENDED), %eax
+	cpuid
+	cmp $(INTEL_CPUID_EXTENDED), %eax
 	ja extended_cpuid_supported
-		
+	
 		movl $extended_cpuid_msg, %esi
 		jmp error_halt
@@ -91,9 +97,9 @@
 	cpuid
 	bt $(AMD_EXT_LONG_MODE), %edx
- 	jc long_mode_supported
-		
+	jc long_mode_supported
+	
 		movl $long_mode_msg, %esi
 		jmp error_halt
-
+	
 	long_mode_supported:
 	
@@ -109,5 +115,5 @@
 	cpuid
 	bt $(INTEL_FXSAVE), %edx
- 	jc fx_supported
+	jc fx_supported
 	
 		movl $fx_msg, %esi
@@ -117,5 +123,5 @@
 	
 	bt $(INTEL_SSE2), %edx
- 	jc sse2_supported
+	jc sse2_supported
 	
 		movl $sse2_msg, %esi
@@ -123,45 +129,29 @@
 	
 	sse2_supported:
-	
-#ifdef CONFIG_FB
-	mov $vesa_init, %esi
-	mov $VESA_INIT_SEGMENT << 4, %edi
-	mov $e_vesa_init - vesa_init, %ecx
-	rep movsb
-
-	mov $VESA_INIT_SEGMENT << 4, %edi
-	jmpl *%edi
-	
-	vesa_meeting_point:
-	
-	mov %esi, KA2PA(vesa_ph_addr)
-	mov %di, KA2PA(vesa_height)
-	shr $16, %edi
-	mov %di, KA2PA(vesa_width)
-	mov %bx, KA2PA(vesa_scanline)
-	shr $16, %ebx
-	mov %bx, KA2PA(vesa_bpp)
-#endif	
-	
+
+#include "vesa_prot.inc"
+
+	#
 	# Enable 64-bit page translation entries - CR4.PAE = 1.
-	# Paging is not enabled until after long mode is enabled
+	# Paging is not enabled until after long mode is enabled.
+	#
 	
 	movl %cr4, %eax
 	btsl $5, %eax
 	movl %eax, %cr4
-
-	# Set up paging tables
+	
+	# set up paging tables
 	
 	leal ptl_0, %eax
 	movl %eax, %cr3
 	
-	# Enable long mode
-	
-	movl $EFER_MSR_NUM, %ecx			# EFER MSR number
-	rdmsr						# Read EFER
-	btsl $AMD_LME_FLAG, %eax			# Set LME = 1
-	wrmsr						# Write EFER
-	
-	# Enable paging to activate long mode (set CR0.PG = 1)
+	# enable long mode
+	
+	movl $EFER_MSR_NUM, %ecx            # EFER MSR number
+	rdmsr                               # read EFER
+	btsl $AMD_LME_FLAG, %eax            # set LME = 1
+	wrmsr                               # write EFER
+	
+	# enable paging to activate long mode (set CR0.PG = 1)
 	
 	movl %cr0, %eax
@@ -169,5 +159,5 @@
 	movl %eax, %cr0
 	
-	# At this point we are in compatibility mode
+	# at this point we are in compatibility mode
 	
 	jmpl $gdtselector(KTEXT_DES), $start64
@@ -176,6 +166,6 @@
 start64:
 	movq $(PA2KA(START_STACK)), %rsp
-
-	# arch_pre_main(grub_eax, grub_ebx)
+	
+	# call arch_pre_main(grub_eax, grub_ebx)
 	xorq %rdi, %rdi
 	movl grub_eax, %edi
@@ -183,253 +173,20 @@
 	movl grub_ebx, %esi
 	call arch_pre_main
-
+	
 	call main_bsp
-
-	# Not reached.
+	
+	# not reached
 	
 	cli
-	hlt
-
-#ifdef CONFIG_FB
-.code32
-vesa_init:
-	jmp $gdtselector(VESA_INIT_DES), $vesa_init_real - vesa_init
-	
-.code16
-vesa_init_real:
-	
-	mov %cr0, %eax
-	and $~1, %eax
-	mov %eax, %cr0
-	
-	jmp $VESA_INIT_SEGMENT, $vesa_init_real2 - vesa_init
-	
-vesa_init_real2:
-	
-	mov $VESA_INIT_SEGMENT, %bx
-	
-	mov %bx, %es
-	mov %bx, %fs
-	mov %bx, %gs
-	mov %bx, %ds
-	mov %bx, %ss
-	
-	movl $0x0000fffc, %esp
-	movl $0x0000fffc, %ebp
-	
-#define VESA_INFO_SIZE 1024
-
-#define VESA_MODE_ATTRIBUTES_OFFSET 0
-#define VESA_MODE_LIST_PTR_OFFSET 14
-#define VESA_MODE_SCANLINE_OFFSET 16
-#define VESA_MODE_WIDTH_OFFSET 18
-#define VESA_MODE_HEIGHT_OFFSET 20
-#define VESA_MODE_BPP_OFFSET 25
-#define VESA_MODE_PHADDR_OFFSET 40
-
-#define VESA_END_OF_MODES 0xffff
-
-#define VESA_OK 0x4f
-
-#define VESA_GET_INFO 0x4f00
-#define VESA_GET_MODE_INFO 0x4f01
-#define VESA_SET_MODE 0x4f02
-#define VESA_SET_PALETTE 0x4f09
-
-#define CONFIG_VESA_BPP_a 255
-
-#if CONFIG_VESA_BPP == 24
-#define CONFIG_VESA_BPP_VARIANT 32
-#endif
-
-	mov $VESA_GET_INFO, %ax
-	mov $e_vesa_init - vesa_init, %di
-	push %di
-	int $0x10
-	
-	pop %di
-	cmp $VESA_OK, %al
-	jnz 0f
-	
-	mov 2 + VESA_MODE_LIST_PTR_OFFSET(%di), %si
-	mov %si, %gs
-	mov VESA_MODE_LIST_PTR_OFFSET(%di), %si
-	
-	add $VESA_INFO_SIZE, %di
-
-1:# Try next mode
-	mov %gs:(%si), %cx
-	cmp $VESA_END_OF_MODES, %cx
-	jz 0f
-	
-	inc %si
-	inc %si
-	push %cx
-	push %di
-	push %si
-	mov $VESA_GET_MODE_INFO, %ax
-	int $0x10
-	
-	pop %si
-	pop %di
-	pop %cx
-	cmp $VESA_OK, %al
-	jnz 0f
-	
-	mov $CONFIG_VESA_WIDTH, %ax
-	cmp VESA_MODE_WIDTH_OFFSET(%di), %ax
-	jnz 1b
-	
-	mov $CONFIG_VESA_HEIGHT, %ax
-	cmp VESA_MODE_HEIGHT_OFFSET(%di), %ax
-	jnz 1b
-	
-	mov $CONFIG_VESA_BPP, %al
-	cmp VESA_MODE_BPP_OFFSET(%di), %al
-
-#ifdef CONFIG_VESA_BPP_VARIANT
-	jz 2f
-	
-	mov $CONFIG_VESA_BPP_VARIANT, %al
-	cmp VESA_MODE_BPP_OFFSET(%di), %al
-#endif
-	jnz 1b
-	
-2:
-	
-	mov %cx, %bx
-	or $0xc000, %bx
-	push %di
-	mov $VESA_SET_MODE, %ax
-	int $0x10
-	
-	pop %di
-	cmp $VESA_OK, %al
-	jnz 0f
-
-#if CONFIG_VESA_BPP == 8
-	
-	# Set 3:2:3 VGA palette
-	
-	mov VESA_MODE_ATTRIBUTES_OFFSET(%di), %ax
-	push %di
-	mov $vga323 - vesa_init, %di
-	mov $0x100, %ecx
-	
-	bt $5, %ax						# Test if VGA compatible registers are present
-	jnc vga_compat
-		
-		# Try VESA routine to set palette
-		
-		mov $VESA_SET_PALETTE, %ax
-		xor %bl, %bl
-		xor %dx, %dx
-		int $0x10
-		
-		cmp $0x00, %ah
-		je vga_not_compat
-	
-	vga_compat:
-		
-		# Try VGA registers to set palette
-		
-		movw $0x3c6, %dx				# Set palette mask
-		movb $0xff, %al
-		outb %al, %dx
-		
-		movw $0x3c8, %dx				# First index to set
-		xor %al, %al
-		outb %al, %dx
-		
-		movw $0x3c9, %dx				# Data port
-		vga_loop:
-			movb %es:2(%di), %al
-			outb %al, %dx
-			
-			movb %es:1(%di), %al
-			outb %al, %dx
-			
-			movb %es:(%di), %al
-			outb %al, %dx
-			
-			addw $4, %di
-			loop vga_loop
-		
-	vga_not_compat:
-	
-	pop %di
-	
-#endif
-	
-	mov VESA_MODE_PHADDR_OFFSET(%di), %esi
-	mov VESA_MODE_WIDTH_OFFSET(%di), %ax
-	shl $16, %eax
-	mov VESA_MODE_HEIGHT_OFFSET(%di), %ax
-	mov VESA_MODE_BPP_OFFSET(%di), %bl
-	xor %bh, %bh
-	shl $16, %ebx
-	mov VESA_MODE_SCANLINE_OFFSET(%di), %bx
-	mov %eax, %edi
-	
-8:
-	
-	mov %cr0, %eax
-	or $1, %eax
-	mov %eax, %cr0
-	
-	jmp 9f
-9:
-	
-	ljmpl $gdtselector(KTEXT32_DES), $(vesa_init_protect - vesa_init + VESA_INIT_SEGMENT << 4)
-	
-0:# No prefered mode found
-	mov $0x111, %cx
-	push %di
-	push %cx
-	mov $VESA_GET_MODE_INFO, %ax
-	int $0x10
-	
-	pop %cx
-	pop %di
-	cmp $VESA_OK, %al
-	jnz 1f
-	jz 2b						# Force relative jump
-
-1:
-	mov $0x0003, %ax
-	int $0x10
-	mov $0xffffffff, %edi				# EGA text mode used, because of problems with VESA
-	xor %ax, %ax
-	jz 8b						# Force relative jump
-
-vga323:
-#include "vga323.pal"	
-	
-.code32
-vesa_init_protect:
-	movw $gdtselector(KDATA_DES), %cx
-	movw %cx, %es
-	movw %cx, %ds					# kernel data + stack
-	movw %cx, %ss
-	# Simics seems to remove hidden part of GS on entering user mode
-	#  when _visible_ part of GS does not point to user-mode segment
-	movw $gdtselector(UDATA_DES), %cx
-	movw %cx, %fs
-	movw %cx, %gs
-	
-	movl $START_STACK, %esp				# initialize stack pointer
-	
-	jmpl $gdtselector(KTEXT32_DES), $vesa_meeting_point
-	
-.align 4
-e_vesa_init:
-#endif
+	hlt0:
+		hlt
+		jmp hlt0
 
 # Print string from %esi to EGA display (in red) and halt
 error_halt:
-	movl $0xb8000, %edi				# base of EGA text mode memory
+	movl $0xb8000, %edi       # base of EGA text mode memory
 	xorl %eax, %eax
 	
-	movw $0x3d4, %dx				# read bits 8 - 15 of the cursor address
+	movw $0x3d4, %dx          # read bits 8 - 15 of the cursor address
 	movb $0xe, %al
 	outb %al, %dx
@@ -439,5 +196,5 @@
 	shl $8, %ax
 	
-	movw $0x3d4, %dx				# read bits 0 - 7 of the cursor address
+	movw $0x3d4, %dx          # read bits 0 - 7 of the cursor address
 	movb $0xf, %al
 	outb %al, %dx
@@ -448,5 +205,7 @@
 	cmp $1920, %ax
 	jbe cursor_ok
-		movw $1920, %ax				# sanity check for the cursor on the last line
+	
+		movw $1920, %ax       # sanity check for the cursor on the last line
+	
 	cursor_ok:
 	
@@ -455,5 +214,5 @@
 	addl %eax, %edi
 	
-	movw $0x0c00, %ax				# black background, light red foreground
+	movw $0x0c00, %ax         # black background, light red foreground
 	
 	ploop:
@@ -462,9 +221,9 @@
 		je ploop_end
 		stosw
-		inc %bx	
+		inc %bx
 		jmp ploop
 	ploop_end:
 	
-	movw $0x3d4, %dx				# write bits 8 - 15 of the cursor address
+	movw $0x3d4, %dx          # write bits 8 - 15 of the cursor address
 	movb $0xe, %al
 	outb %al, %dx
@@ -474,5 +233,5 @@
 	outb %al, %dx
 	
-	movw $0x3d4, %dx				# write bits 0 - 7 of the cursor address
+	movw $0x3d4, %dx          # write bits 0 - 7 of the cursor address
 	movb $0xf, %al
 	outb %al, %dx
@@ -481,8 +240,11 @@
 	movb %bl, %al
 	outb %al, %dx
-		
+	
 	cli
-	hlt
-				
+	hlt1:
+		hlt
+		jmp hlt1
+
+#include "vesa_real.inc"
 
 .section K_INI_PTLS, "aw", @progbits
@@ -490,6 +252,6 @@
 #
 # Macro for generating initial page table contents.
-# @param cnt	Number of entries to generat. Must be multiple of 8.
-# @param g	Number of GB that will be added to the mapping.
+# @param cnt Number of entries to generat. Must be multiple of 8.
+# @param g   Number of GB that will be added to the mapping.
 #
 .macro ptl2gen cnt g 
@@ -543,5 +305,5 @@
 	.quad ptl_2_0g + (PTL_WRITABLE | PTL_PRESENT)
 	.fill 1, 8, 0
-	
+
 .align 4096
 .global ptl_0
Index: kernel/arch/amd64/src/boot/memmap.c
===================================================================
--- kernel/arch/amd64/src/boot/memmap.c	(revision 99d6fd07a4e12eb667d2cfa2d3fdb3b5f63800b2)
+++ kernel/arch/amd64/src/boot/memmap.c	(revision 421c8338127caee7f13ab5e70216f4ed042e8b22)
@@ -27,5 +27,5 @@
  */
 
-/** @addtogroup amd64mm	
+/** @addtogroup amd64mm
  * @{
  */
Index: kernel/arch/amd64/src/boot/vesa_prot.inc
===================================================================
--- kernel/arch/amd64/src/boot/vesa_prot.inc	(revision 421c8338127caee7f13ab5e70216f4ed042e8b22)
+++ kernel/arch/amd64/src/boot/vesa_prot.inc	(revision 421c8338127caee7f13ab5e70216f4ed042e8b22)
@@ -0,0 +1,1 @@
+../../../ia32/src/boot/vesa_prot.inc
Index: kernel/arch/amd64/src/boot/vesa_real.inc
===================================================================
--- kernel/arch/amd64/src/boot/vesa_real.inc	(revision 421c8338127caee7f13ab5e70216f4ed042e8b22)
+++ kernel/arch/amd64/src/boot/vesa_real.inc	(revision 421c8338127caee7f13ab5e70216f4ed042e8b22)
@@ -0,0 +1,1 @@
+../../../ia32/src/boot/vesa_real.inc
Index: kernel/arch/amd64/src/boot/vesa_ret.inc
===================================================================
--- kernel/arch/amd64/src/boot/vesa_ret.inc	(revision 421c8338127caee7f13ab5e70216f4ed042e8b22)
+++ kernel/arch/amd64/src/boot/vesa_ret.inc	(revision 421c8338127caee7f13ab5e70216f4ed042e8b22)
@@ -0,0 +1,19 @@
+.code32
+vesa_init_protected:
+	movw $gdtselector(KDATA_DES), %cx
+	movw %cx, %es
+	movw %cx, %ds                       # kernel data + stack
+	movw %cx, %ss
+	
+	#
+	# Simics seems to remove hidden part of GS on entering user mode
+	# when _visible_ part of GS does not point to user-mode segment.
+	#
+	
+	movw $gdtselector(UDATA_DES), %cx
+	movw %cx, %fs
+	movw %cx, %gs
+	
+	movl $START_STACK, %esp             # initialize stack pointer
+	
+	jmpl $gdtselector(KTEXT32_DES), $vesa_meeting_point
Index: kernel/arch/ia32/src/boot/boot.S
===================================================================
--- kernel/arch/ia32/src/boot/boot.S	(revision 99d6fd07a4e12eb667d2cfa2d3fdb3b5f63800b2)
+++ kernel/arch/ia32/src/boot/boot.S	(revision 421c8338127caee7f13ab5e70216f4ed042e8b22)
@@ -43,5 +43,5 @@
 	.long MULTIBOOT_HEADER_MAGIC
 	.long MULTIBOOT_HEADER_FLAGS
-	.long -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS)	# checksum
+	.long -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS)  # checksum
 	.long multiboot_header
 	.long unmapped_ktext_start
@@ -49,32 +49,32 @@
 	.long 0
 	.long multiboot_image_start
-	
+
 multiboot_image_start:
 	cld
-	movl $START_STACK, %esp			# initialize stack pointer
-	lgdt KA2PA(bootstrap_gdtr)		# initialize Global Descriptor Table register
-
-	movw $selector(KDATA_DES), %cx
+	movl $START_STACK, %esp     # initialize stack pointer
+	lgdt KA2PA(bootstrap_gdtr)  # initialize Global Descriptor Table register
+	
+	movw $gdtselector(KDATA_DES), %cx
 	movw %cx, %es
 	movw %cx, %fs
 	movw %cx, %gs
-	movw %cx, %ds					# kernel data + stack
+	movw %cx, %ds               # kernel data + stack
 	movw %cx, %ss
 	
-	jmpl $selector(KTEXT_DES), $multiboot_meeting_point
+	jmpl $gdtselector(KTEXT_DES), $multiboot_meeting_point
 	multiboot_meeting_point:
 	
-	movl %eax, grub_eax					# save parameters from GRUB
+	movl %eax, grub_eax         # save parameters from GRUB
 	movl %ebx, grub_ebx
 	
-	xorl %eax, %eax  
- 	cpuid
- 	cmp $0x0, %eax						# any function > 0?
+	xorl %eax, %eax
+	cpuid
+	cmp $0x0, %eax              # any function > 0?
 	jbe pse_unsupported
-	movl $0x1, %eax						# Basic function code 1
+	movl $0x1, %eax             # basic function code 1
 	cpuid
-	bt $3, %edx							# Test if PSE is supported
+	bt $3, %edx                 # test if PSE is supported
 	jc pse_supported
-
+	
 	pse_unsupported:
 		movl $pse_msg, %esi
@@ -82,28 +82,11 @@
 	
 	pse_supported:
-	
-#ifdef CONFIG_FB
-	mov $vesa_init, %esi
-	mov $VESA_INIT_SEGMENT << 4, %edi
-	mov $e_vesa_init - vesa_init, %ecx
-	rep movsb
-
-	mov $VESA_INIT_SEGMENT << 4, %edi
-	jmpl *%edi
-	
-	vesa_meeting_point:
-	
-	mov %esi, KA2PA(vesa_ph_addr)
-	mov %di, KA2PA(vesa_height)
-	shr $16, %edi
-	mov %di, KA2PA(vesa_width)
-	mov %bx, KA2PA(vesa_scanline)
-	shr $16, %ebx
-	mov %bx, KA2PA(vesa_bpp)
-#endif	
-
-	call map_kernel							# map kernel and turn paging on
-
-	# arch_pre_main(grub_eax, grub_ebx)
+
+#include "vesa_prot.inc"
+
+	# map kernel and turn paging on
+	call map_kernel
+	
+	# call arch_pre_main(grub_eax, grub_ebx)
 	pushl grub_ebx
 	pushl grub_eax
@@ -112,8 +95,9 @@
 	call main_bsp
 	
-	# Not reached.
-	
+	# not reached
 	cli
-	hlt
+	hlt0:
+		hlt
+		jmp hlt0
 
 .global map_kernel
@@ -124,6 +108,6 @@
 	#
 	movl %cr4, %ecx
-	orl $(1 << 4), %ecx							# turn PSE on
-	andl $(~(1 << 5)), %ecx						# turn PAE off
+	orl $(1 << 4), %ecx                 # turn PSE on
+	andl $(~(1 << 5)), %ecx             # turn PAE off
 	movl %ecx, %cr4
 	
@@ -132,19 +116,20 @@
 	xorl %ecx, %ecx
 	xorl %ebx, %ebx
-0:
-	movl $((1 << 7) | (1 << 1) | (1 << 0)), %eax
-	orl %ebx, %eax
-	movl %eax, (%esi, %ecx, 4)					# mapping 0x00000000 + %ecx * 4M => 0x00000000 + %ecx * 4M
-	movl %eax, (%edi, %ecx, 4)					# mapping 0x80000000 + %ecx * 4M => 0x00000000 + %ecx * 4M
-	addl $(4 * 1024 * 1024), %ebx
-
-	incl %ecx
-	cmpl $512, %ecx
-	jl 0b
-
+	
+	floop:
+		movl $((1 << 7) | (1 << 1) | (1 << 0)), %eax
+		orl %ebx, %eax
+		movl %eax, (%esi, %ecx, 4)      # mapping 0x00000000 + %ecx * 4M => 0x00000000 + %ecx * 4M
+		movl %eax, (%edi, %ecx, 4)      # mapping 0x80000000 + %ecx * 4M => 0x00000000 + %ecx * 4M
+		addl $(4 * 1024 * 1024), %ebx
+		
+		incl %ecx
+		cmpl $512, %ecx
+		jl floop
+	
 	movl %esi, %cr3
 	
 	movl %cr0, %ebx
-	orl $(1 << 31), %ebx						# turn paging on
+	orl $(1 << 31), %ebx                # turn paging on
 	movl %ebx, %cr0
 	ret
@@ -152,8 +137,8 @@
 # Print string from %esi to EGA display (in red) and halt
 error_halt:
-	movl $0xb8000, %edi						# base of EGA text mode memory
+	movl $0xb8000, %edi         # base of EGA text mode memory
 	xorl %eax, %eax
 	
-	movw $0x3d4, %dx						# read bits 8 - 15 of the cursor address
+	movw $0x3d4, %dx            # read bits 8 - 15 of the cursor address
 	movb $0xe, %al
 	outb %al, %dx
@@ -163,5 +148,5 @@
 	shl $8, %ax
 	
-	movw $0x3d4, %dx						# read bits 0 - 7 of the cursor address
+	movw $0x3d4, %dx            # read bits 0 - 7 of the cursor address
 	movb $0xf, %al
 	outb %al, %dx
@@ -172,5 +157,7 @@
 	cmp $1920, %ax
 	jbe cursor_ok
-		movw $1920, %ax						# sanity check for the cursor on the last line
+	
+		movw $1920, %ax         # sanity check for the cursor on the last line
+	
 	cursor_ok:
 	
@@ -179,5 +166,5 @@
 	addl %eax, %edi
 	
-	movw $0x0c00, %ax						# black background, light red foreground
+	movw $0x0c00, %ax           # black background, light red foreground
 	
 	ploop:
@@ -186,9 +173,9 @@
 		je ploop_end
 		stosw
-		inc %bx	
+		inc %bx
 		jmp ploop
 	ploop_end:
 	
-	movw $0x3d4, %dx						# write bits 8 - 15 of the cursor address
+	movw $0x3d4, %dx            # write bits 8 - 15 of the cursor address
 	movb $0xe, %al
 	outb %al, %dx
@@ -198,5 +185,5 @@
 	outb %al, %dx
 	
-	movw $0x3d4, %dx						# write bits 0 - 7 of the cursor address
+	movw $0x3d4, %dx            # write bits 0 - 7 of the cursor address
 	movb $0xf, %al
 	outb %al, %dx
@@ -205,238 +192,11 @@
 	movb %bl, %al
 	outb %al, %dx
-		
+	
 	cli
-	hlt
-
-#ifdef CONFIG_FB
-vesa_init:
-	jmp $selector(VESA_INIT_DES), $vesa_init_real - vesa_init
-	
-.code16
-vesa_init_real:
-	
-	mov %cr0, %eax
-	and $~1, %eax
-	mov %eax, %cr0
-	
-	jmp $VESA_INIT_SEGMENT, $vesa_init_real2 - vesa_init
-	
-vesa_init_real2:
-	
-	mov $VESA_INIT_SEGMENT, %bx
-	
-	mov %bx, %es
-	mov %bx, %fs
-	mov %bx, %gs
-	mov %bx, %ds
-	mov %bx, %ss
-	
-	movl %esp, %eax
-	movl $0x0000fffc, %esp
-	movl $0x0000fffc, %ebp
-	pushl %eax
-	
-#define VESA_INFO_SIZE 1024
-
-#define VESA_MODE_ATTRIBUTES_OFFSET 0
-#define VESA_MODE_LIST_PTR_OFFSET 14
-#define VESA_MODE_SCANLINE_OFFSET 16
-#define VESA_MODE_WIDTH_OFFSET 18
-#define VESA_MODE_HEIGHT_OFFSET 20
-#define VESA_MODE_BPP_OFFSET 25
-#define VESA_MODE_PHADDR_OFFSET 40
-
-#define VESA_END_OF_MODES 0xffff
-
-#define VESA_OK 0x4f
-
-#define VESA_GET_INFO 0x4f00
-#define VESA_GET_MODE_INFO 0x4f01
-#define VESA_SET_MODE 0x4f02
-#define VESA_SET_PALETTE 0x4f09
-
-#if CONFIG_VESA_BPP == 24
-#define CONFIG_VESA_BPP_VARIANT 32
-#endif
-
-	mov $VESA_GET_INFO, %ax
-	mov $e_vesa_init - vesa_init, %di
-	push %di
-	int $0x10
-	
-	pop %di
-	cmp $VESA_OK, %al
-	jnz 0f
-	
-	mov 2 + VESA_MODE_LIST_PTR_OFFSET(%di), %si
-	mov %si, %gs
-	mov VESA_MODE_LIST_PTR_OFFSET(%di), %si
-	
-	add $VESA_INFO_SIZE, %di
-
-1:# Try next mode
-	mov %gs:(%si), %cx
-	cmp $VESA_END_OF_MODES, %cx
-	jz 0f
-	
-	inc %si
-	inc %si
-	push %cx
-	push %di
-	push %si
-	mov $VESA_GET_MODE_INFO, %ax
-	int $0x10
-	
-	pop %si
-	pop %di
-	pop %cx
-	cmp $VESA_OK, %al
-	jnz 0f
-	
-	mov $CONFIG_VESA_WIDTH, %ax
-	cmp VESA_MODE_WIDTH_OFFSET(%di), %ax
-	jnz 1b
-	
-	mov $CONFIG_VESA_HEIGHT, %ax
-	cmp VESA_MODE_HEIGHT_OFFSET(%di), %ax
-	jnz 1b
-	
-	mov $CONFIG_VESA_BPP, %al
-	cmp VESA_MODE_BPP_OFFSET(%di), %al
-
-#ifdef CONFIG_VESA_BPP_VARIANT
-	jz 2f
-	
-	mov $CONFIG_VESA_BPP_VARIANT, %al
-	cmp VESA_MODE_BPP_OFFSET(%di), %al
-#endif
-	jnz 1b
-
-2:
-	
-	mov %cx, %bx
-	or $0xc000, %bx
-	push %di
-	mov $VESA_SET_MODE, %ax
-	int $0x10
-	
-	pop %di
-	cmp $VESA_OK, %al
-	jnz 0f
-
-#if CONFIG_VESA_BPP == 8
-	
-	# Set 3:2:3 VGA palette
-	
-	mov VESA_MODE_ATTRIBUTES_OFFSET(%di), %ax
-	push %di
-	mov $vga323 - vesa_init, %di
-	mov $0x100, %ecx
-	
-	bt $5, %ax							# Test if VGA compatible registers are present
-	jnc vga_compat
-		
-		# Try VESA routine to set palette
-		
-		mov $VESA_SET_PALETTE, %ax
-		xor %bl, %bl
-		xor %dx, %dx
-		int $0x10
-		
-		cmp $0x00, %ah
-		je vga_not_compat
-	
-	vga_compat:
-		
-		# Try VGA registers to set palette
-		
-		movw $0x3c6, %dx						# Set palette mask
-		movb $0xff, %al
-		outb %al, %dx
-		
-		movw $0x3c8, %dx						# First index to set
-		xor %al, %al
-		outb %al, %dx
-		
-		movw $0x3c9, %dx						# Data port
-		vga_loop:
-			movb %es:2(%di), %al
-			outb %al, %dx
-			
-			movb %es:1(%di), %al
-			outb %al, %dx
-			
-			movb %es:(%di), %al
-			outb %al, %dx
-			
-			addw $4, %di
-			loop vga_loop
-		
-	vga_not_compat:
-	
-	pop %di
-	
-#endif
-	
-	mov VESA_MODE_PHADDR_OFFSET(%di), %esi
-	mov VESA_MODE_WIDTH_OFFSET(%di), %ax
-	shl $16, %eax
-	mov VESA_MODE_HEIGHT_OFFSET(%di), %ax
-	mov VESA_MODE_BPP_OFFSET(%di), %bl
-	xor %bh, %bh
-	shl $16, %ebx
-	mov VESA_MODE_SCANLINE_OFFSET(%di), %bx
-	mov %eax, %edi
-	
-8:	
-	
-	mov %cr0, %eax
-	or $1, %eax
-	mov %eax, %cr0
-	
-	jmp 9f
-9:
-	
-	ljmpl $selector(KTEXT_DES), $(vesa_init_protect - vesa_init + VESA_INIT_SEGMENT << 4)
-
-0:# No prefered mode found
-	mov $0x111, %cx
-	push %di
-	push %cx
-	mov $VESA_GET_MODE_INFO, %ax
-	int $0x10
-	
-	pop %cx
-	pop %di
-	cmp $VESA_OK, %al
-	jnz 1f
-	jz 2b						# Force relative jump
-	
-1:
-	mov $0x0003, %ax
-	int $0x10
-	mov $0xffffffff, %edi		# EGA text mode used, because of problems with VESA
-	xor %ax, %ax
-	jz 8b						# Force relative jump
-
-vga323:
-#include "vga323.pal"
-
-.code32
-vesa_init_protect:
-	movw $selector(KDATA_DES), %cx
-	movw %cx, %es
-	movw %cx, %fs
-	movw %cx, %gs
-	movw %cx, %ds					# kernel data + stack
-	movw %cx, %ss
-	
-	movl $START_STACK, %esp			# initialize stack pointer
-
-	jmpl $selector(KTEXT_DES), $vesa_meeting_point
-
-.align 4
-e_vesa_init:
-#endif	
+	hlt1:
+		hlt
+		jmp hlt1
+
+#include "vesa_real.inc"
 
 .section K_DATA_START, "aw", @progbits
Index: kernel/arch/ia32/src/boot/vesa_prot.inc
===================================================================
--- kernel/arch/ia32/src/boot/vesa_prot.inc	(revision 421c8338127caee7f13ab5e70216f4ed042e8b22)
+++ kernel/arch/ia32/src/boot/vesa_prot.inc	(revision 421c8338127caee7f13ab5e70216f4ed042e8b22)
@@ -0,0 +1,18 @@
+#ifdef CONFIG_FB
+	mov $vesa_init, %esi
+	mov $VESA_INIT_SEGMENT << 4, %edi
+	mov $e_vesa_init - vesa_init, %ecx
+	rep movsb
+	
+	mov $VESA_INIT_SEGMENT << 4, %edi
+	jmpl *%edi
+	
+	vesa_meeting_point:
+		mov %esi, KA2PA(vesa_ph_addr)
+		mov %di, KA2PA(vesa_height)
+		shr $16, %edi
+		mov %di, KA2PA(vesa_width)
+		mov %bx, KA2PA(vesa_scanline)
+		shr $16, %ebx
+		mov %bx, KA2PA(vesa_bpp)
+#endif
Index: kernel/arch/ia32/src/boot/vesa_real.inc
===================================================================
--- kernel/arch/ia32/src/boot/vesa_real.inc	(revision 421c8338127caee7f13ab5e70216f4ed042e8b22)
+++ kernel/arch/ia32/src/boot/vesa_real.inc	(revision 421c8338127caee7f13ab5e70216f4ed042e8b22)
@@ -0,0 +1,334 @@
+#ifdef CONFIG_FB
+
+#include <macros.h>
+
+#define VESA_INFO_SIZE 1024
+
+#define VESA_MODE_ATTRIBUTES_OFFSET 0
+#define VESA_MODE_LIST_PTR_OFFSET 14
+#define VESA_MODE_SCANLINE_OFFSET 16
+#define VESA_MODE_WIDTH_OFFSET 18
+#define VESA_MODE_HEIGHT_OFFSET 20
+#define VESA_MODE_BPP_OFFSET 25
+#define VESA_MODE_PHADDR_OFFSET 40
+
+#define VESA_END_OF_MODES 0xffff
+
+#define VESA_OK 0x4f
+
+#define VESA_GET_INFO 0x4f00
+#define VESA_GET_MODE_INFO 0x4f01
+#define VESA_SET_MODE 0x4f02
+#define VESA_SET_PALETTE 0x4f09
+
+.code32
+vesa_init:
+	jmp $gdtselector(VESA_INIT_DES), $vesa_init_real - vesa_init
+	
+.code16
+vesa_init_real:
+	
+	mov %cr0, %eax
+	and $~1, %eax
+	mov %eax, %cr0
+	
+	jmp $VESA_INIT_SEGMENT, $vesa_init_real2 - vesa_init
+	
+vesa_init_real2:
+	mov $VESA_INIT_SEGMENT, %bx
+	
+	mov %bx, %es
+	mov %bx, %fs
+	mov %bx, %gs
+	mov %bx, %ds
+	mov %bx, %ss
+	
+	movl %esp, %eax
+	movl $0x0000fffc, %esp
+	movl $0x0000fffc, %ebp
+	pushl %eax
+	
+	# parse default mode string
+	
+	mov $default_mode - vesa_init, %di
+	xor %eax, %eax
+	xor %ebx, %ebx
+	
+	mov $8, %ecx
+	parse_width:
+		mov (%di), %al
+		
+		# check for digit
+		
+		cmp $'0', %al
+		jb parse_width_done
+		
+		cmp $'9', %al
+		ja parse_width_done
+		
+		sub $'0', %al
+		
+		# multiply default_width by 10 and add digit
+		
+		mov default_width - vesa_init, %bx
+		lea (%ebx, %ebx, 4), %ebx
+		shl $1, %ebx
+		add %ax, %bx
+		mov %bx, default_width - vesa_init
+		
+		inc %di
+		loop parse_width
+	parse_width_done:
+	
+	mov (%di), %al
+	cmp $0, %al
+	jz parse_done
+	inc %di
+	
+	mov $8, %ecx
+	parse_height:
+		mov (%di), %al
+		
+		# check for digit
+		
+		cmp $'0', %al
+		jb parse_height_done
+		
+		cmp $'9', %al
+		ja parse_height_done
+		
+		sub $'0', %al
+		
+		# multiply default_height by 10 and add digit
+		
+		mov default_height - vesa_init, %bx
+		lea (%ebx, %ebx, 4), %ebx
+		shl $1, %ebx
+		add %ax, %bx
+		mov %bx, default_height - vesa_init
+		
+		inc %di
+		loop parse_height
+	parse_height_done:
+	
+	mov (%di), %al
+	cmp $0, %al
+	jz parse_done
+	inc %di
+	
+	mov $4, %ecx
+	parse_bpp:
+		mov (%di), %al
+		
+		# check for digit
+		
+		cmp $'0', %al
+		jb parse_bpp_done
+		
+		cmp $'9', %al
+		ja parse_bpp_done
+		
+		sub $'0', %al
+		
+		# multiply default_bpp by 10 and add digit
+		
+		mov default_bpp - vesa_init, %bx
+		lea (%ebx, %ebx, 4), %ebx
+		shl $1, %ebx
+		add %ax, %bx
+		mov %bx, default_bpp - vesa_init
+		
+		inc %di
+		loop parse_bpp
+	parse_bpp_done:
+	
+	parse_done:
+	
+	mov $VESA_GET_INFO, %ax
+	mov $e_vesa_init - vesa_init, %di
+	push %di
+	int $0x10
+	
+	pop %di
+	cmp $VESA_OK, %al
+	jnz no_mode
+	
+	mov 2 + VESA_MODE_LIST_PTR_OFFSET(%di), %si
+	mov %si, %gs
+	mov VESA_MODE_LIST_PTR_OFFSET(%di), %si
+	
+	add $VESA_INFO_SIZE, %di
+	
+	next_mode:
+		# try next mode
+		mov %gs:(%si), %cx
+		cmp $VESA_END_OF_MODES, %cx
+		jz no_mode
+		
+		inc %si
+		inc %si
+		push %cx
+		push %di
+		push %si
+		mov $VESA_GET_MODE_INFO, %ax
+		int $0x10
+		
+		pop %si
+		pop %di
+		pop %cx
+		cmp $VESA_OK, %al
+		jnz no_mode
+		
+		mov default_width - vesa_init, %ax
+		cmp VESA_MODE_WIDTH_OFFSET(%di), %ax
+		jnz next_mode
+		
+		mov default_height - vesa_init, %ax
+		cmp VESA_MODE_HEIGHT_OFFSET(%di), %ax
+		jnz next_mode
+		
+		mov default_bpp - vesa_init, %al
+		cmp VESA_MODE_BPP_OFFSET(%di), %al
+		jz set_mode
+		
+		mov $24, %al
+		cmp default_bpp - vesa_init, %al
+		jnz next_mode
+		
+		# for 24 bpp modes accept also 32 bit bpp
+		
+		mov $32, %al
+		cmp VESA_MODE_BPP_OFFSET(%di), %al
+		jnz next_mode
+	
+	set_mode:
+		mov %cx, %bx
+		or $0xc000, %bx
+		push %di
+		mov $VESA_SET_MODE, %ax
+		int $0x10
+		
+		pop %di
+		cmp $VESA_OK, %al
+		jnz no_mode
+		
+		# set 3:2:3 VGA palette
+		
+		mov VESA_MODE_BPP_OFFSET(%di), %al
+		cmp $8, %al
+		jnz vga_not_set
+		
+		mov VESA_MODE_ATTRIBUTES_OFFSET(%di), %ax
+		push %di
+		mov $vga323 - vesa_init, %di
+		mov $0x100, %ecx
+		
+		bt $5, %ax              # test if VGA compatible registers are present
+		jnc vga_compat
+		
+			# try VESA routine to set palette
+			mov $VESA_SET_PALETTE, %ax
+			xor %bl, %bl
+			xor %dx, %dx
+			int $0x10
+			
+			cmp $0x00, %ah
+			je vga_not_compat
+		
+		vga_compat:
+			# try VGA registers to set palette
+			movw $0x3c6, %dx    # set palette mask
+			movb $0xff, %al
+			outb %al, %dx
+			
+			movw $0x3c8, %dx    # first index to set
+			xor %al, %al
+			outb %al, %dx
+			
+			movw $0x3c9, %dx    # data port
+			
+			vga_loop:
+				movb %es:2(%di), %al
+				outb %al, %dx
+				
+				movb %es:1(%di), %al
+				outb %al, %dx
+				
+				movb %es:(%di), %al
+				outb %al, %dx
+				
+				addw $4, %di
+			loop vga_loop
+		
+		vga_not_compat:
+		
+			pop %di
+		
+		vga_not_set:
+		
+		mov VESA_MODE_PHADDR_OFFSET(%di), %esi
+		mov VESA_MODE_WIDTH_OFFSET(%di), %ax
+		shl $16, %eax
+		mov VESA_MODE_HEIGHT_OFFSET(%di), %ax
+		mov VESA_MODE_BPP_OFFSET(%di), %bl
+		xor %bh, %bh
+		shl $16, %ebx
+		mov VESA_MODE_SCANLINE_OFFSET(%di), %bx
+		mov %eax, %edi
+		
+		vesa_leave_real:
+		
+			mov %cr0, %eax
+			or $1, %eax
+			mov %eax, %cr0
+			
+			jmp vesa_leave_real2
+			
+		vesa_leave_real2:
+		
+			ljmpl $gdtselector(KTEXT32_DES), $(vesa_init_protected - vesa_init + VESA_INIT_SEGMENT << 4)
+	
+	no_mode:
+		# no prefered mode found
+		mov $0x111, %cx
+		push %di
+		push %cx
+		mov $VESA_GET_MODE_INFO, %ax
+		int $0x10
+		
+		pop %cx
+		pop %di
+		cmp $VESA_OK, %al
+		jnz text_mode
+		jz set_mode             # force relative jump
+	
+	text_mode:
+		# reset to EGA text mode (because of problems with VESA)
+		mov $0x0003, %ax
+		int $0x10
+		mov $0xffffffff, %edi
+		xor %ax, %ax
+		jz vesa_leave_real      # force relative jump
+
+vga323:
+#include "vga323.pal"
+
+default_width:
+	.word 0
+
+default_height:
+	.word 0
+
+default_bpp:
+	.byte 0
+
+default_mode:
+	.ascii STRING(CONFIG_VESA_MODE)
+	.ascii "-"
+	.asciz STRING(CONFIG_VESA_BPP)
+
+#include "vesa_ret.inc"
+
+.align 4
+e_vesa_init:
+#endif
Index: kernel/arch/ia32/src/boot/vesa_ret.inc
===================================================================
--- kernel/arch/ia32/src/boot/vesa_ret.inc	(revision 421c8338127caee7f13ab5e70216f4ed042e8b22)
+++ kernel/arch/ia32/src/boot/vesa_ret.inc	(revision 421c8338127caee7f13ab5e70216f4ed042e8b22)
@@ -0,0 +1,12 @@
+.code32
+vesa_init_protected:
+	movw $gdtselector(KDATA_DES), %cx
+	movw %cx, %es
+	movw %cx, %fs
+	movw %cx, %gs
+	movw %cx, %ds               # kernel data + stack
+	movw %cx, %ss
+	
+	movl $START_STACK, %esp     # initialize stack pointer
+	
+	jmpl $gdtselector(KTEXT_DES), $vesa_meeting_point
