Skip to content

Instantly share code, notes, and snippets.

@jklmnn
Last active February 11, 2021 19:15
Show Gist options
  • Save jklmnn/4a83a6e994b209c276e47dda9421a6bd to your computer and use it in GitHub Desktop.
Save jklmnn/4a83a6e994b209c276e47dda9421a6bd to your computer and use it in GitHub Desktop.
use efi framebuffer from linux user space
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <stdint.h>
#include <unistd.h>
#include "genode.h"
#define EFIFB_ADDRESS 0xe0000000
#define FB_WIDTH 1920
#define FB_HEIGHT 0x14
#define FB_BYPP 4
void *get_mmio(off_t phys, size_t length)
{
int fd = open("/dev/genode-mmio", O_RDWR|O_SYNC);
if(fd < 0){
perror("open1 /dev/genode-mmio");
return 0;
}
int fd2 = open("/dev/genode-mmio", O_RDWR|O_SYNC);
if(fd2 < 0){
perror("open2 /dev/genode-mmio");
}
mmio_range_t range = { (unsigned long) phys, length };
if(ioctl(fd, MMIO_SET_RANGE, &range) < 0)
perror("ioctl1");
mmio_range_t range2 = { 42, 42 };
if(ioctl(fd2, MMIO_SET_RANGE, &range2) < 0)
perror("ioctl2-1");
if(ioctl(fd2, MMIO_SET_RANGE, &range2) < 0)
perror("ioctl2-2");
size_t pagesize = sysconf(_SC_PAGE_SIZE);
off_t page_base = (phys / pagesize) * pagesize;
off_t page_offset = phys - page_base;
void *addr = mmap(NULL, page_offset + length, PROT_READ | PROT_WRITE, MAP_SHARED, fd, phys);
if(addr == MAP_FAILED){
perror("mmap1");
return 0;
}
void *addr2 = mmap(NULL, page_offset + length, PROT_READ | PROT_WRITE, MAP_SHARED, fd2, phys);
if(addr2 == MAP_FAILED){
perror("mmap2");
}
close(fd);
close(fd2);
return addr;
}
int main()
{
uint32_t *buffer = (uint32_t*)get_mmio(EFIFB_ADDRESS, FB_WIDTH * FB_HEIGHT * FB_BYPP);
printf("vga buffer: %p\n", buffer);
uint32_t colors[4] = {0x00ffffff, 0x00ff0000, 0x0000ff00, 0x000000ff};
if(buffer){
int color = 0;
while(1){
printf("drawing color %#08x\n", colors[color]);
for(size_t i = 0; i < FB_WIDTH * FB_HEIGHT; ++i){
buffer[i] = colors[color];
}
color = (color + 1) % 4;
sleep(2);
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment