A20 Enabler

Programming, for all ages and all languages.
Post Reply
User avatar
siavoshkc
Member
Member
Posts: 40
Joined: Wed Feb 19, 2014 11:10 am

A20 Enabler

Post by siavoshkc »

[EDIT] This code is wrong. Don't use it.
I wrote this code to enable A20 line

Code: Select all

;See if A20Line is enabled
	push ds
	mov bx, 0xF040
	mov ds, bx
	mov bx, 0xFFFD
	mov word [bx], 0x0000
	mov bx, 0x0000 
	mov ds, bx
	mov bx, 0x03FE
	mov word [bx], 0xFF
	mov bx, 0xF040
	mov ds, bx
	mov bx, 0xFFFD
	mov cx, [bx]
	pop ds
	test cx, 0xFF
	jne	A20_Enabled
	;Enable A20
	in al, 0x92
	or al, 2
	out 0x92, al
	
	A20_Enabled:
	;check again
	push ds
	mov bx, 0xF040
	mov ds, bx
	mov bx, 0xFFFD
	mov word [bx], 0x0000
	mov bx, 0x0000 
	mov ds, bx
	mov bx, 0x03FE
	mov word [bx], 0xFF
	mov bx, 0xF040
	mov ds, bx
	mov bx, 0xFFFD
	mov cx, [bx]
	pop ds
	test cx, 0xFF
	jne endA20
	mov bx, str_A20Failed
	call print_nullterminated_str
.
.
.
       str_A20Failed: db 'Error: Could not enable A20.',0xA,0xd, 0
What do you think?
Last edited by siavoshkc on Sun Feb 23, 2014 10:31 am, edited 1 time in total.
Check out my FSB Data Integrity Tester at http://fsbdit.sourceforge.net/.

Siavosh
User avatar
thepowersgang
Member
Member
Posts: 734
Joined: Tue Dec 25, 2007 6:03 am
Libera.chat IRC: thePowersGang
Location: Perth, Western Australia
Contact:

Re: A20 Enabler

Post by thepowersgang »

... what do I think? I think it's pretty much useless to post here.

The steps you should take to enable the A20 line are covered by the wiki pretty well, and you should only care if you're writing a bootloader.


[edit] After actually looking at the code, my opinion dropped even lower.

1. You check that A20 is actually enabled (good)
2. You then use the Fast A20 Gate (which has the chance of doing strange things if it's not supported)
3. You then re-check it's enabled (even if it was already enabled)
4. You attempt no other techniques of enabling the A20 line (there are three listed on the wiki)

If you're going to use this code for yourself - read the wiki and rewrite your code to use the BIOS first, keyboard controller second then try the Fast A20 method.
If you just posted for people to use... to be quite honest you're not helping people.
Kernel Development, It's the brain surgery of programming.
Acess2 OS (c) | Tifflin OS (rust) | mrustc - Rust compiler
Currently Working on: mrustc
User avatar
siavoshkc
Member
Member
Posts: 40
Joined: Wed Feb 19, 2014 11:10 am

Re: A20 Enabler

Post by siavoshkc »

thepowersgang wrote:... what do I think? I think it's pretty much useless to post here.

The steps you should take to enable the A20 line are covered by the wiki pretty well, and you should only care if you're writing a bootloader.


[edit] After actually looking at the code, my opinion dropped even lower.

1. You check that A20 is actually enabled (good)
2. You then use the Fast A20 Gate (which has the chance of doing strange things if it's not supported)
3. You then re-check it's enabled (even if it was already enabled)
4. You attempt no other techniques of enabling the A20 line (there are three listed on the wiki)

If you're going to use this code for yourself - read the wiki and rewrite your code to use the BIOS first, keyboard controller second then try the Fast A20 method.
If you just posted for people to use... to be quite honest you're not helping people.
I created it from the wiki. I selected the cleanest way to enable A20. Rechecking is a bug of course. Should change the jmp after first check.
To tell the truth it is unable to enable A20 in Bochs. At least it says so! So I will add other methods too.
Check out my FSB Data Integrity Tester at http://fsbdit.sourceforge.net/.

Siavosh
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: A20 Enabler

Post by Brendan »

Hi,
siavoshkc wrote:I created it from the wiki. I selected the cleanest way to enable A20. Rechecking is a bug of course. Should change the jmp after first check.
To tell the truth it is unable to enable A20 in Bochs. At least it says so! So I will add other methods too.
I just added the Recommended Method section to the A20 wiki page. Hopefully this will help to prevent future confusion. :)


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
User avatar
siavoshkc
Member
Member
Posts: 40
Joined: Wed Feb 19, 2014 11:10 am

Re: A20 Enabler

Post by siavoshkc »

The problem is that BIOS itself uses one method right? I don't like Keyboard method because it is slow and I want to eliminate BIOS calls in my code.
Check out my FSB Data Integrity Tester at http://fsbdit.sourceforge.net/.

Siavosh
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: A20 Enabler

Post by bluemoon »

siavoshkc wrote:The problem is that BIOS itself uses one method right? I don't like Keyboard method because it is slow and I want to eliminate BIOS calls in my code.
The BIOS method has a great advantage that it does not explode (I hope) even if it can't enable A20.

A extra step can be used to check if KBC exists before talking to it.
It at least hang on my ancient macbook by polling a non-existing KBC.
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: A20 Enabler

Post by Brendan »

Hi,
siavoshkc wrote:The problem is that BIOS itself uses one method right? I don't like Keyboard method because it is slow and I want to eliminate BIOS calls in my code.
The BIOS uses whichever method is the best method for the specific motherboard (which could be anything, and may be something completely undocumented). The keyboard method (compared to "fast A20") is slower, but it's more likely to work and less likely to cause major problems.

If you want to eliminate BIOS calls, then you're either:
  • using UEFI (where A20 is enabled anyway), or
  • writing code for a specific motherboard (unlikely), or
  • doing something wrong

Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
User avatar
siavoshkc
Member
Member
Posts: 40
Joined: Wed Feb 19, 2014 11:10 am

Re: A20 Enabler

Post by siavoshkc »

My code is wrong. Conditional jumps should be jz not jne.


[EDIT]
I will use BIOS method.
Check out my FSB Data Integrity Tester at http://fsbdit.sourceforge.net/.

Siavosh
freecrac
Member
Member
Posts: 69
Joined: Thu Sep 20, 2012 5:11 am
Location: germany hamburg

Re: A20 Enabler

Post by freecrac »

This is the small subroutine that i use. I see it is not perfect, but i never found a problem with it.
Do you think it is possible that this routine fails on some PCs?

Code: Select all

A20:     call W_8042
	      jnz BACK
	      mov      al, 0D1h
	      out      64h, al
	      call W_8042
         jnz BACK
	      mov      al, 0DFh
	      out      60h, al
;------------------------------------------------------------
W_8042:  xor      cx, cx
STATUS:  in       al, 64h
	      and      al, 2
	      loopnz STATUS
BACK:    ret
Dirk
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: A20 Enabler

Post by Brendan »

Hi,
freecrac wrote:This is the small subroutine that i use. I see it is not perfect, but i never found a problem with it.
Do you think it is possible that this routine fails on some PCs?
Yes. For a start, on Apple 80x86 hardware (where there is no keyboard controller) it will crash the computer.


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
User avatar
ChosenOreo
Member
Member
Posts: 25
Joined: Sun May 29, 2011 5:16 pm
Location: Inside A Computer
Contact:

Re: A20 Enabler

Post by ChosenOreo »

I had this problem a while back and asked here for how I would go about solving it. After some testing I came up with a solution.. it seems to work on all the hardware I have (though I don't have any very ancient computers to test this on).

I'm not going to post the source unless you really want it as it has always seemed more fun and fulfilling to me to figure out something on my own :D
However, I will give you the general idea of what my code does.

My code has a few functions, which are:
  • ToggleA20BIOS - Toggles the A20 line from whatever it was using the BIOS
  • ToggleA20Fast - Toggles the A20 line from whatever it was using the Fast A20 Line
  • ToggleA20Keyboard - Toggles the A20 line from whatever it was using the Keyboard Controller
  • CheckA20Wrap - Checks whether the A20 line is enabled by seeing if memory wraps around
  • A20SupportBIOS - Checks for various methods of changing the A20 line through the BIOS (BIOS, Fast A20, Keyboard Controller, etc..)
  • ToggleA20 - Toggles the A20 line using the above functions
  • EnableA20 - Enables the A20 line using the above functions
  • DisableA20 - Disables the A20 line using the above functions
Basically for the last two methods the procedure is roughly the same.

First, we check if the A20 line is enabled / disabled. Then, we check for BIOS support for the A20 line. If the BIOS support function works, it will let us know whether we can use the other BIOS functions, the Fast A20 function, or the keyboard controller to enable / disable the A20 line. If it doesn't work, we will assume that the BIOS functions are not available and will proceed to first use the keyboard controller, then the Fast A20 if that does not work. In any case, the general procedure is to first do the BIOS function, then the Keyboard Controller, then then Fast A20. If none of these things work we simply return an error code.

On a final note, you may notice that I created Toggle functions instead of Enable / Disable functions for each different way to edit the A20 line. This allows us to write enable / disable code around the toggle code, thus re-using code that we normally would have to rewrite with a few small differences.

I hope this helps!

- Adrian
User avatar
VolTeK
Member
Member
Posts: 815
Joined: Sat Nov 15, 2008 2:37 pm
Location: The Fire Nation

Re: A20 Enabler

Post by VolTeK »

thepowersgang wrote: ... what do I think? I think it's pretty much useless to post here.
Agreed. This helps no one learn anything (No good questions are asked, no good answers are given), it does flare up bad habits and others may try and copy this code rather than learn it.
freecrac
Member
Member
Posts: 69
Joined: Thu Sep 20, 2012 5:11 am
Location: germany hamburg

Re: A20 Enabler

Post by freecrac »

Brendan wrote:Hi,
freecrac wrote:This is the small subroutine that i use. I see it is not perfect, but i never found a problem with it.
Do you think it is possible that this routine fails on some PCs?
Yes. For a start, on Apple 80x86 hardware (where there is no keyboard controller) it will crash the computer.
Cheers,

Brendan
Only on Apple, i think it is OK.
..
Copying of code is no problem for me, because it is all only a small part of our human cultural heritage. And without using our cultural heritage and a known language, that was grown up a long time, nobody can think about to build a cultural grand. So the supreme effort of our human forefathers to build our today cultural comprehension is much more a significant and important factor, as the ignorant thinking that our intellectual was build only by our self and we get an own intellectual ownership. I am sure an individual intellectual ownership does not exist nowhere.

And if somebody do not want to learn how to understand the technical demands behind the code, then we only can hope that he/she get no problem with it. And we have to except that all bad habits are also only a part of our human cultur and the progress of the humanization with a lot of highs and lows step by step. I think we need all of these highs and lows of the humanization and if we want to stepover without going high and low together, then we are going to miss something in the future. I post my code for to copy and paste and only if somebody want to learn about the technic behind by her/him self, then he/she is looking deeper, but not if we want it. The interessest can not be pressed or wakeup by giving only an order for to learn it self. Additional thinking about that humans are learning in different ways, so some of them need a roughly overview first for to understand little parts deep inside of a technik, that other humans do not need for their orientation.

Dirk
Post Reply