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