Re: accessing the contents of video memory

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Tue, Jan 18, 2011 at 3:40 PM, Prabhu nath <gprabhunath@xxxxxxxxx> wrote:
> If you are working on a desktop machine, then the following idea works.
> Usually, the VGA compatible controller memory will be mapped to the physical
> address space which can be viewed with the help of "lspci -vv" command. Then
> open the device file "/dev/mem" and mmap with the offset equal to the
> physical address of the Graphics card. Here is a small snippet which works
> on my machine
> Now open the device mem as
> ÂÂÂÂÂÂÂÂÂ fd = open = ("/dev/mem", O_RDWR)
> and do an mmap as
> ÂÂÂÂÂÂÂÂÂ vgamem = mmap (NULL, 0x10000000, PROT_READ | PROT_WRITE,
> ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ MAP_SHARED, fd, 0xd0000000)
> 0x10000000 = 256MB of graphics card memory
> 0xd0000000 = base of the physical address of graphics card memory
>
> vgamem will be the user virtual address which is mapped to the physical
> address of Graphics card memory.
>
> To verify the mapping, write some bit patterns to 256MB of memory starting
> from vgamem.
> If you see distortion on your desktop, then you have written on to your
> graphics card
> memory.

Just gave it a shot, for the fun of it (program attached - needs root
rights). Indeed after running it my screen was stripped with little
black dots, as one could expect. However it also screwed my fonts and
after a few seconds crashed my system. ;) Indeed, the video memory
does not only contain the screen framebuffer, it also hosts other data
related to 3d management, compositing, and probably data structures
that X rely on. But at least this validates your approach to easily
access video memory.

Now all that is needed for Elvis to get the screen contents is to know
the boundaries of the screen's buffer, as well as its format.
Following your idea, he could probably access the memory-mapped
registers of his video card, but this is highly vendor-dependant.
Cannot think of a more generic way though.

Alex.
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>

#define VMEM_START 0xe0000000
#define VMEM_SIZE 0x10000000
#define BOMB_STEP 0x1000

int main(int argc, char *argv[])
{
	int fd = open("/dev/mem", O_RDWR);
	if (fd == -1) {
		perror("Cannot open /dev/mem");
		return 1;
	}
	unsigned int *vgamem = mmap(NULL, VMEM_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, VMEM_START);
	if (vgamem == MAP_FAILED) {
		perror("Cannot map video memory");
		return 1;
	}

	// Bomb the video memory...
	unsigned int i;
	for (i = 0; i < VMEM_SIZE / sizeof(unsigned int); i += BOMB_STEP) {
		vgamem[i] = 0;
	}

	munmap(vgamem, VMEM_SIZE);
	close(fd);
	return 0;
}
_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@xxxxxxxxxxxxxxxxx
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux