• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
usage(void)43 static void __attribute__((noreturn)) usage(void)
44 {
45 	fprintf(stderr, "usage: bios_dumper <filename>\n");
46 	exit(1);
47 }
48 
main(int argc,char ** argv)49 int main(int argc, char **argv)
50 {
51 	struct pci_device *dev;
52 	void *bios;
53 	int error, fd;
54 
55 	if (argc != 2)
56 		usage();
57 
58 	error = pci_system_init();
59 	if (error != 0) {
60 		fprintf(stderr, "Couldn't initialize PCI system: %s\n",
61 			strerror(error));
62 		exit(1);
63 	}
64 
65 	/* Grab the graphics card */
66 	dev = pci_device_find_by_slot(0, 0, 2, 0);
67 	if (dev == NULL)
68 		errx(1, "Couldn't find graphics card");
69 
70 	error = pci_device_probe(dev);
71 	if (error != 0) {
72 		fprintf(stderr, "Couldn't probe graphics card: %s\n",
73 			strerror(error));
74 		exit(1);
75 	}
76 
77 	if (dev->vendor_id != 0x8086)
78 		errx(1, "Graphics card is non-intel");
79 
80 	/* Some versions of libpciaccess correct this automatically, but some
81 	 * don't. */
82 	if (dev->rom_size == 0)
83 		dev->rom_size = 64 * 1024;
84 
85 	bios = malloc(dev->rom_size);
86 	if (bios == NULL)
87 		errx(1, "Couldn't allocate memory for BIOS data\n");
88 
89 	error = pci_device_read_rom(dev, bios);
90 	if (error != 0) {
91 		fprintf(stderr, "Couldn't read graphics card ROM: %s\n",
92 			strerror(error));
93 		exit(1);
94 	}
95 
96 	fd = open(argv[1], O_RDWR | O_CREAT | O_TRUNC, DEFFILEMODE);
97 	if (fd < 0) {
98 		fprintf(stderr, "Couldn't open output: %s\n", strerror(errno));
99 		exit(1);
100 	}
101 
102 	if (write(fd, bios, dev->rom_size) < dev->rom_size) {
103 		fprintf(stderr, "Couldn't write BIOS data: %s\n",
104 			strerror(errno));
105 		exit(1);
106 	}
107 
108 	close(fd);
109 	pci_system_cleanup();
110 
111 	return 0;
112 }
113