1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
|
/*
* Copyright 2009 Luc Verhaegen <libv@skynet.be>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#define _GNU_SOURCE /* memmem is useful */
#include <stdio.h>
#include <inttypes.h>
#include <errno.h>
#include <string.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>
#include "bios_extract.h"
static void
HelpPrint(char *name)
{
printf("\n");
printf("Program to extract compressed modules from BIOS images.\n");
printf("Supports AMI, Award, Asus and Phoenix BIOSes.\n");
printf("\n");
printf("Usage:\n\t%s <filename>\n", name);
}
unsigned char *
MMapOutputFile(char *filename, int size)
{
unsigned char* Buffer;
int fd;
fd = open(filename, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
if (fd < 0) {
fprintf(stderr, "Error: unable to open %s: %s\n\n",
filename, strerror(errno));
return NULL;
}
/* grow file */
if (lseek(fd, size - 1, SEEK_SET) == -1) {
fprintf(stderr, "Error: Failed to grow \"%s\": %s\n",
filename, strerror(errno));
close(fd);
return NULL;
}
if (write(fd, "", 1) != 1) {
fprintf(stderr, "Error: Failed to write to \"%s\": %s\n",
filename, strerror(errno));
close(fd);
return NULL;
}
Buffer = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (Buffer == ((void *) -1)) {
fprintf(stderr, "Error: Failed to mmap %s: %s\n",
filename, strerror(errno));
close(fd);
return NULL;
}
close(fd);
return Buffer;
}
static struct {
char *String1;
char *String2;
Bool (*Handler) (unsigned char *Image, int ImageLength, int ImageOffset,
uint32_t Offset1, uint32_t Offset2);
} BIOSIdentification[] = {
{"AMIBOOT ROM", "AMIBIOSC", AMI95Extract},
{"$ASUSAMI$", "AMIBIOSC", AMI95Extract},
{"AMIEBBLK", "AMIBIOSC", AMI95Extract},
{"Award BootBlock", "= Award Decompression Bios =", AwardExtract},
{"Phoenix FirstBIOS", "BCPSEGMENT", PhoenixExtract},
{"PhoenixBIOS 4.0", "BCPSEGMENT", PhoenixExtract},
{"Phoenix TrustedCore", "BCPSEGMENT", PhoenixTrustedExtract},
{NULL, NULL, NULL},
};
/*
*
*/
int
main(int argc, char *argv[])
{
int FileLength = 0;
uint32_t BIOSOffset = 0;
unsigned char *BIOSImage = NULL;
int fd;
uint32_t Offset1, Offset2;
int i, len;
unsigned char *tmp;
if ((argc != 2) || !strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")){
HelpPrint(argv[0]);
return 1;
}
fd = open(argv[1], O_RDONLY);
if (fd < 0) {
fprintf(stderr, "Error: Failed to open %s: %s\n",
argv[1], strerror(errno));
return 1;
}
FileLength = lseek(fd, 0, SEEK_END);
if (FileLength < 0) {
fprintf(stderr, "Error: Failed to lseek \"%s\": %s\n",
argv[1], strerror(errno));
return 1;
}
BIOSOffset = (0x100000 - FileLength) & 0xFFFFF;
BIOSImage = mmap(NULL, FileLength, PROT_READ, MAP_PRIVATE, fd, 0);
if (BIOSImage < 0) {
fprintf(stderr, "Error: Failed to mmap %s: %s\n",
argv[1], strerror(errno));
return 1;
}
printf("Using file \"%s\" (%ukB)\n", argv[1], FileLength >> 10);
for (i = 0; BIOSIdentification[i].Handler; i++) {
len = strlen(BIOSIdentification[i].String1);
tmp = memmem(BIOSImage, FileLength - len, BIOSIdentification[i].String1, len);
if (!tmp)
continue;
Offset1 = tmp - BIOSImage;
len = strlen(BIOSIdentification[i].String2);
tmp = memmem(BIOSImage, FileLength - len, BIOSIdentification[i].String2, len);
if (!tmp)
continue;
Offset2 = tmp - BIOSImage;
if (BIOSIdentification[i].Handler(BIOSImage, FileLength, BIOSOffset,
Offset1, Offset2))
return 0;
else
return 1;
}
fprintf(stderr, "Error: Unable to detect BIOS Image type.\n");
return 1;
}
|