1fa225cbcSrjs/*
2fa225cbcSrjs * Copyright © 2007 Intel Corporation
3fa225cbcSrjs *
4fa225cbcSrjs * Permission is hereby granted, free of charge, to any person obtaining a
5fa225cbcSrjs * copy of this software and associated documentation files (the "Software"),
6fa225cbcSrjs * to deal in the Software without restriction, including without limitation
7fa225cbcSrjs * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8fa225cbcSrjs * and/or sell copies of the Software, and to permit persons to whom the
9fa225cbcSrjs * Software is furnished to do so, subject to the following conditions:
10fa225cbcSrjs *
11fa225cbcSrjs * The above copyright notice and this permission notice (including the next
12fa225cbcSrjs * paragraph) shall be included in all copies or substantial portions of the
13fa225cbcSrjs * Software.
14fa225cbcSrjs *
15fa225cbcSrjs * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16fa225cbcSrjs * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17fa225cbcSrjs * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18fa225cbcSrjs * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19fa225cbcSrjs * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20fa225cbcSrjs * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21fa225cbcSrjs * DEALINGS IN THE SOFTWARE.
22fa225cbcSrjs *
23fa225cbcSrjs * Authors:
24fa225cbcSrjs *    Eric Anholt <eric@anholt.net>
25fa225cbcSrjs *
26fa225cbcSrjs */
27fa225cbcSrjs
28fa225cbcSrjs#include <stdio.h>
29fa225cbcSrjs#include <stdlib.h>
30fa225cbcSrjs#include <string.h>
31fa225cbcSrjs#include <stdarg.h>
32fa225cbcSrjs#include <sys/stat.h>
33fa225cbcSrjs#include <fcntl.h>
34fa225cbcSrjs#include <unistd.h>
35fa225cbcSrjs#include <errno.h>
36fa225cbcSrjs#include <pciaccess.h>
37fa225cbcSrjs#include <err.h>
38fa225cbcSrjs
39fa225cbcSrjs#ifndef DEFFILEMODE
40fa225cbcSrjs#define DEFFILEMODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH) /* 0666*/
41fa225cbcSrjs#endif
42fa225cbcSrjs
43fa225cbcSrjsstatic void usage(void)
44fa225cbcSrjs{
45fa225cbcSrjs    fprintf(stderr, "usage: bios_dumper <filename>\n");
46fa225cbcSrjs    exit(1);
47fa225cbcSrjs}
48fa225cbcSrjs
49fa225cbcSrjsint main(int argc, char **argv)
50fa225cbcSrjs{
51fa225cbcSrjs    struct pci_device *dev;
52fa225cbcSrjs    void *bios;
53fa225cbcSrjs    int err, fd;
54fa225cbcSrjs
55fa225cbcSrjs    if (argc != 2)
56fa225cbcSrjs	usage();
57fa225cbcSrjs
58fa225cbcSrjs    err = pci_system_init();
59fa225cbcSrjs    if (err != 0) {
60fa225cbcSrjs	fprintf(stderr, "Couldn't initialize PCI system: %s\n", strerror(err));
61fa225cbcSrjs	exit(1);
62fa225cbcSrjs    }
63fa225cbcSrjs
64fa225cbcSrjs    /* Grab the graphics card */
65fa225cbcSrjs    dev = pci_device_find_by_slot(0, 0, 2, 0);
66fa225cbcSrjs    if (dev == NULL)
67fa225cbcSrjs	errx(1, "Couldn't find graphics card");
68fa225cbcSrjs
69fa225cbcSrjs    err = pci_device_probe(dev);
70fa225cbcSrjs    if (err != 0) {
71fa225cbcSrjs	fprintf(stderr, "Couldn't probe graphics card: %s\n", strerror(err));
72fa225cbcSrjs	exit(1);
73fa225cbcSrjs    }
74fa225cbcSrjs
75fa225cbcSrjs    if (dev->vendor_id != 0x8086)
76fa225cbcSrjs	errx(1, "Graphics card is non-intel");
77fa225cbcSrjs
78fa225cbcSrjs    bios = malloc(dev->rom_size);
79fa225cbcSrjs    if (bios == NULL)
80fa225cbcSrjs	errx(1, "Couldn't allocate memory for BIOS data\n");
81fa225cbcSrjs
82fa225cbcSrjs    err = pci_device_read_rom(dev, bios);
83fa225cbcSrjs    if (err != 0) {
84fa225cbcSrjs	fprintf(stderr, "Couldn't read graphics card ROM: %s\n",
85fa225cbcSrjs		strerror(err));
86fa225cbcSrjs	exit(1);
87fa225cbcSrjs    }
88fa225cbcSrjs
89fa225cbcSrjs    fd = open(argv[1], O_RDWR | O_CREAT | O_TRUNC, DEFFILEMODE);
90fa225cbcSrjs    if (fd < 0) {
91fa225cbcSrjs	fprintf(stderr, "Couldn't open output: %s\n", strerror(errno));
92fa225cbcSrjs	exit(1);
93fa225cbcSrjs    }
94fa225cbcSrjs
95fa225cbcSrjs    if (write(fd, bios, dev->rom_size) < dev->rom_size) {
96fa225cbcSrjs	fprintf(stderr, "Couldn't write BIOS data: %s\n", strerror(errno));
97fa225cbcSrjs	exit(1);
98fa225cbcSrjs    }
99fa225cbcSrjs
100fa225cbcSrjs    close(fd);
101fa225cbcSrjs    pci_system_cleanup();
102fa225cbcSrjs
103fa225cbcSrjs    return 0;
104fa225cbcSrjs}
105