flat assembler
Message board for the users of flat assembler.

Index > Linux > Direct access to text video memory and cursor control

Author
Thread Post new topic Reply to topic
Cas



Joined: 26 Feb 2004
Posts: 82
Location: Argentina
Cas 07 Jun 2008, 23:09
How can I get to directly read/write on the screen memory while in text mode? (to enter control characters on the screen)

Also... how can I change the text cursor size and position or render it invisible?

Is it possible to also show up the mouse cursor in text mode?

I'm looking, if possible, for an all-or-most distros resolution. Thank you very much in advance for your help!

_________________
«Earth is my country; science is my religion» - Christian Huygens
Post 07 Jun 2008, 23:09
View user's profile Send private message Yahoo Messenger MSN Messenger Reply with quote
gunblade



Joined: 19 Feb 2004
Posts: 209
gunblade 07 Jun 2008, 23:53
Hey,
The most common way to write to a terminal (or console) in linux is to use the write syscall

Code:
mov eax, 4
mov ebx, 1
mov ecx, msg
mov edx, msg.len
int 0x80

msg db 'test',10
.len = $ - msg    


If you meant to write to the text buffer directly (DOS style), I dont think its possible, (For protection mainly.. for DOS is fine, it has no memory protection and is single-tasked, but linux is multitasking, what if 2 programs try to write to the buffer at the same time).

On the other hand, there are libraries like svgalib which allow you to write to the console as a SVGA buffer, so you could look at the source code and see how they do it.
Post 07 Jun 2008, 23:53
View user's profile Send private message Reply with quote
System86



Joined: 15 Aug 2007
Posts: 77
System86 08 Jun 2008, 13:27
I've done direct access to text mode video ram once under Linux. You open /dev/mem, mmap with offset 0xB8000, and use the returned value as a pointer to text mode video ram. As for cursor, I have not done that, but it certainly can be done by in/out commands after you do the ioperm/iopl call.

The above can be done, though, without direct hardware access by using various Linux int 80h calls (like write and ioctl).
Post 08 Jun 2008, 13:27
View user's profile Send private message Reply with quote
Cas



Joined: 26 Feb 2004
Posts: 82
Location: Argentina
Cas 09 Jun 2008, 22:30
Thank you, guys!

System86: Can you give me code for what you did to access /dev/mem?
gunblade: Will that int 80h call allow me to write special codes (x < 20h) as characters on the screen?

both: Where can I get a fully detailed list of the int 80h calls with descriptions and that? Something like the DOS interrupt list by Ralf Brown.
Post 09 Jun 2008, 22:30
View user's profile Send private message Yahoo Messenger MSN Messenger Reply with quote
Chewy509



Joined: 19 Jun 2003
Posts: 297
Location: Bris-vegas, Australia
Chewy509 09 Jun 2008, 23:07
Cas, have a look at the 'ncurses' library.

It does everything you need, and is portable across both real and virtual terminals on all *nix systems.

PS. http://en.wikipedia.org/wiki/Ncurses
Post 09 Jun 2008, 23:07
View user's profile Send private message Visit poster's website Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3175
Location: Denmark
f0dder 10 Jun 2008, 01:49
Chewy509: it isn't exactly "direct", but it's probably the best option. Works remotely (SSH, whatever) and is relatively fast on a local console. Other than that, there's some /dev/something you can open (which shouldn't require root as /dev/mem) which is a bit less "cooked" (or "more raw", if you prefer that term). Unfortunately, you can't mmap() it, so you need write() calls (unless mmap capability has been added for that device in the last 3-5 years Wink).
Post 10 Jun 2008, 01:49
View user's profile Send private message Visit poster's website Reply with quote
Chewy509



Joined: 19 Jun 2003
Posts: 297
Location: Bris-vegas, Australia
Chewy509 10 Jun 2008, 02:32
f0dder wrote:
it isn't exactly "direct"

Very true, but when dealing with OS's such as Linux, etc, direct access to hardware is generally a no-no since it can and will break things. And the fact that any application that needs direct access, also requires 'root' privilege levels as well... (eg opening /dev/mem for read/write requires 'root' access).

ncurses is the most appropriate option as it'll work in most environments, be it, the local console, a virtual console, SSH/Telnet session, and will work on any architecture, including some weird and wonderful dumb terminals, and also work as a standard user (and thus less likely to crash his box).

The other thing he'll need to take into account, is if the console isn't 80x25 (mode 03h), but something else, eg 132x50. The framebuffer offset in /dev/mem will be different or act differently due to the increases screen size.

@cas: http://asm.sourceforge.net/syscall.html#0 for the syscall list for Linux
Post 10 Jun 2008, 02:32
View user's profile Send private message Visit poster's website Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3175
Location: Denmark
f0dder 10 Jun 2008, 02:38
Chewy509: I obviously agree pretty much 100% with you, but some people just won't take no for an answer - even if ncurses is "generally fast enough" even on 486 class processors Smile
Post 10 Jun 2008, 02:38
View user's profile Send private message Visit poster's website Reply with quote
Cas



Joined: 26 Feb 2004
Posts: 82
Location: Argentina
Cas 13 Jun 2008, 03:57
I'm concerned about requiring root privileges. Does that mean that, if I make an application that simply displays the full 256 standard ascii characters on the text screen and somebody runs it, Linux will ask them for the root password?
Yes, I would sure love the most direct way possible to access the text video memory, but if there is a way I can do it indirectly that allows me for the same final results (no CR/LF for codes 0ah/0dh, but actual characters, and full text color support and control over the cursor) I prefer one that will allow every user to easily execute the application.
Anyway, your suggestions and examples so far and incredibly helpful and I appreciate it very much! It's much more than I expected to be able to get about my perhaps uncommon concern.
Post 13 Jun 2008, 03:57
View user's profile Send private message Yahoo Messenger MSN Messenger Reply with quote
gunblade



Joined: 19 Feb 2004
Posts: 209
gunblade 15 Jun 2008, 08:44
the int 0x80 method will let you write x < 20 values to the screen (i have tested mainly with 0x0a for new line).

Also, for the root access, you have a bunch of choices.
If the program requires root access (you want to access /dev/mem), then your choices are:

1) login as root
2) use sudo (allows a user to gain root priviledges just for one command.. so your program would be run like: sudo ./program, this has to be setup though, type man sudo to find out more about it)
3) or you could set your binary as suid, this means that when your program is executed, it gains root priveledges, no matter who its executed by. Of course, this is a security risk, if you have any bugs/holes in your program, a user could make an exploit to gain root access through it (its how a lot of exploits were made, suid programs and buffer/stack overflows). (To do this, run: "chmod a+s program" as root)
Post 15 Jun 2008, 08:44
View user's profile Send private message Reply with quote
Endre



Joined: 29 Dec 2003
Posts: 215
Location: Budapest, Hungary
Endre 15 Jun 2008, 11:01
Try using escape sequences (see: man console_codes), the method does not need root privileges or any libraries. Some years ago we discussed this thing already, so you might want to check this out as well:
http://board.flatassembler.net/topic.php?t=3226
Post 15 Jun 2008, 11:01
View user's profile Send private message Reply with quote
Feryno



Joined: 23 Mar 2005
Posts: 509
Location: Czech republic, Slovak republic
Feryno 18 Jun 2008, 16:42
/dev/vcsa0
/dev/vcs0
/dev/vcsa0, /dev/vcsa1,... are like real mode segment B800h (byte for text + byte for color) and the other is just plain text (without any byte for color)
http://www.google.com/search?q=asmutils&lr=
look for window.asm it uses the color scheme (/dev/vcsa0)
the other choice /dev/vcs0 is just plaintext without text color attribute bytes
The last number is for console number, e.g. if you switch console to e.g. 5th (if you have it, Linux has typically 6 text consoles and the 7th is used for x-windows) by pressing ALT+F5 (or CTRL+ALT+F5 from running x-windows) then you need to access /dev/vcs5 and so on. If you need to grab the text + color attributes of console ALT+F5 when running on console ALT+F1 then you must access /dev/vcsa5 again.
Most Linuxes has these /dev/vcs... compiled in kernel. If you don't then you need to recompile your kernel. The zero at the end e.g. vcs0 means current console
here google to know more (but I wrote all in shortcut)
http://www.google.com/search?q=%2Fdev%2Fvcs0&lr=
you can use 'cat' and 'dd' command to directly read (cat) and read/write from those devices
e.g. dd if=/dev/vcs5 of=screen_text_dump.txt bs=2000 count=1
if your console is 25 lines, 80 charactes in 1 line (25*80=2000)
Post 18 Jun 2008, 16:42
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.