getfb.c revision 6b2c0e5854625cd0422c12c331dd22a3e3021079
1// Copyright (c) 2012 The Chromium OS Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5#include <fcntl.h> 6#include <linux/fb.h> 7#include <stdint.h> 8#include <stdio.h> 9#include <stdlib.h> 10#include <sys/mman.h> 11#include <sys/ioctl.h> 12#include <unistd.h> 13 14int main(int argc, char *argv[]) 15{ 16 const char* device_name = "/dev/fb0"; 17 struct fb_var_screeninfo info; 18 uint32_t screen_size = 0; 19 void *ptr = NULL; 20 int fd = -1; 21 FILE *file = NULL; 22 int ret = -1; 23 24 if (argc < 2) { 25 printf("Usage: getfb [filename]\n"); 26 printf("Writes the active framebuffer to output file [filename].\n"); 27 return 0; 28 } 29 30 // Open the file for reading and writing 31 fd = open(device_name, O_RDONLY); 32 if (fd == -1) { 33 fprintf(stderr, "Cannot open framebuffer device %s\n", device_name); 34 goto exit; 35 } 36 printf("The framebuffer device was opened successfully.\n"); 37 38 // Get fixed screen information 39 if (ioctl(fd, FBIOGET_VSCREENINFO, &info) == -1) { 40 fprintf(stderr, "Error reading variable screen information.\n"); 41 goto exit; 42 } 43 44 printf("%dx%d, %dbpp\n", info.xres, info.yres, info.bits_per_pixel); 45 46 // Figure out the size of the screen in bytes 47 screen_size = info.xres * info.yres * info.bits_per_pixel / 8; 48 49 // Map the device to memory 50 ptr = mmap(0, screen_size, PROT_READ, MAP_SHARED, fd, 0); 51 if (ptr == MAP_FAILED) { 52 fprintf(stderr, "Error: failed to map framebuffer device to memory.\n"); 53 goto exit; 54 } 55 printf("The framebuffer device was mapped to memory successfully.\n"); 56 57 // Write it to output file. 58 file = fopen(argv[1], "w"); 59 if (!file) { 60 fprintf(stderr, "Could not open file %s for writing.\n", argv[1]); 61 goto exit; 62 } 63 64 if (fwrite(ptr, screen_size, 1, file) < 1) { 65 fprintf(stderr, "Error while writing framebuffer to file.\n"); 66 goto exit; 67 } 68 69 ret = 0; 70 71exit: 72 if (file) 73 fclose(file); 74 75 if (ptr != MAP_FAILED && ptr) 76 munmap(ptr, screen_size); 77 78 if (fd >= 0) 79 close(fd); 80 81 return ret; 82} 83