blob: 57cc0649a7f1037336a043e2beefc148aecf18d7 [file] [log] [blame]
Luc Verhaegen838fe242009-06-17 00:48:02 +02001/*
2 * Decompression utility for AMI BIOSes.
3 *
4 * Copyright 2009 Luc Verhaegen <libv@skynet.be>
5 * Copyright 2000-2006 Anthony Borisow
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
10 * any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; see the file COPYING. If not, write to
19 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22#include <stdio.h>
23#include <inttypes.h>
24#include <errno.h>
25#include <string.h>
26#include <sys/mman.h>
27#include <sys/stat.h>
28#include <fcntl.h>
29#include <unistd.h>
30
31#include "bios_extract.h"
32#include "lh5_extract.h"
33
34struct AMI95ModuleName
35{
36 uint8_t Id;
37 char *Name;
38};
39
40static struct AMI95ModuleName
41AMI95ModuleNames[] = {
42 {0x00, "POST"},
43 {0x01, "Setup Server"},
44 {0x02, "RunTime"},
45 {0x03, "DIM"},
46 {0x04, "Setup Client"},
47 {0x05, "Remote Server"},
48 {0x06, "DMI Data"},
49 {0x07, "Green PC"},
50 {0x08, "Interface"},
51 {0x09, "MP"},
52 {0x0A, "Notebook"},
53 {0x0B, "Int-10"},
54 {0x0C, "ROM-ID"},
55 {0x0D, "Int-13"},
56 {0x0E, "OEM Logo"},
57 {0x0F, "ACPI Table"},
58 {0x10, "ACPI AML"},
59 {0x11, "P6 Microcode"},
60 {0x12, "Configuration"},
61 {0x13, "DMI Code"},
62 {0x14, "System Health"},
63 {0x15, "Memory Sizing"},
64 {0x16, "Memory Test"},
65 {0x17, "Debug"},
66 {0x18, "ADM (Display MGR)"},
67 {0x19, "ADM Font"},
68 {0x1A, "Small Logo"},
69 {0x1B, "SLAB"},
70 {0x20, "PCI AddOn ROM"},
71 {0x21, "Multilanguage"},
72 {0x22, "UserDefined"},
73 {0x23, "ASCII Font"},
74 {0x24, "BIG5 Font"},
75 {0x25, "OEM Logo"},
76 {0x2A, "User ROM"},
77 {0x2B, "PXE Code"},
78 {0x2C, "AMI Font"},
79 {0x2E, "User ROM"},
80 {0x2D, "Battery Refresh"},
81 {0x30, "Font Database"},
82 {0x31, "OEM Logo Data"},
83 {0x32, "Graphic Logo Code"},
84 {0x33, "Graphic Logo Data"},
85 {0x34, "Action Logo Code"},
86 {0x35, "Action Logo Data"},
87 {0x36, "Virus"},
88 {0x37, "Online Menu"},
89 {0x38, "Lang1 as ROM"},
90 {0x39, "Lang2 as ROM"},
91 {0x3A, "Lang3 as ROM"},
Rudolf Marekd6b4fbb2009-12-21 11:30:05 +010092 {0x40, "AMD CIM-X NB binary"},
93 {0x60, "AMD CIM-X SB binary"},
Luc Verhaegen838fe242009-06-17 00:48:02 +020094 {0x70, "OSD Bitmaps"},
Rudolf Marekd6b4fbb2009-12-21 11:30:05 +010095 {0xf0, "Asrock Backup Util"},
96 {0xf9, "Asrock AMD AHCI DLL"},
97 {0xfa, "Asrock LOGO GIF"},
98 {0xfb, "Asrock LOGO JPG"},
99 {0xfc, "Asrock LOGO JPG"},
100 {0xfd, "Asrock LOGO PCX - Instant boot"},
Luc Verhaegen838fe242009-06-17 00:48:02 +0200101 {0, NULL}
102};
103
104static char *
Luc Verhaegen15587352009-06-26 10:00:29 +0200105AMI95ModuleNameGet(uint8_t ID)
Luc Verhaegen838fe242009-06-17 00:48:02 +0200106{
107 int i;
108
109 for (i = 0; AMI95ModuleNames[i].Name; i++)
110 if (AMI95ModuleNames[i].Id == ID)
111 return AMI95ModuleNames[i].Name;
112
Luc Verhaegen15587352009-06-26 10:00:29 +0200113 return NULL;
Luc Verhaegen838fe242009-06-17 00:48:02 +0200114}
115
116/*
117 *
118 */
119Bool
Luc Verhaegen15587352009-06-26 10:00:29 +0200120AMI95Extract(unsigned char *BIOSImage, int BIOSLength, int BIOSOffset,
Luc Verhaegen838fe242009-06-17 00:48:02 +0200121 uint32_t AMIBOffset, uint32_t ABCOffset)
122{
123 Bool Compressed;
124 uint32_t Offset;
125 char Date[9];
126 int i;
127
128 struct abc {
129 const char AMIBIOSC[8];
130 const char Version[4];
131 const uint16_t CRCLen;
132 const uint32_t CRC32;
133 const uint16_t BeginLo;
134 const uint16_t BeginHi;
135 } *abc;
136
Rudolf Marekd6b4fbb2009-12-21 11:30:05 +0100137 struct bigpart {
138 const uint32_t CSize;
139 const uint32_t Unknown;
140 } *bigpart;
141
Luc Verhaegen838fe242009-06-17 00:48:02 +0200142 struct part {
143 /* When Previous Part Address is 0xFFFFFFFF, then this is the last part. */
144 const uint16_t PrePartLo; /* Previous part low word */
145 const uint16_t PrePartHi; /* Previous part high word */
146 const uint16_t CSize; /* Header length */
147 const uint8_t PartID; /* ID for this header */
148 const uint8_t IsComprs; /* 0x80 -> compressed */
Michael Karcher6d867dc2010-01-14 16:55:36 +0100149 const uint32_t RealCS; /* Old BIOSes:
150 Real Address in RAM where to expand to
151 Now:
152 Type 0x20 PCI ID of device for this ROM
153 Type 0x21 Language ID (ascii) */
Luc Verhaegen838fe242009-06-17 00:48:02 +0200154 const uint32_t ROMSize; /* Compressed Length */
155 const uint32_t ExpSize; /* Expanded Length */
156 } *part;
157
158 if (!ABCOffset) {
159 if ((BIOSImage[8] == '1') && (BIOSImage[9] == '0') &&
160 (BIOSImage[11] == '1') && (BIOSImage[12] == '0'))
161 fprintf(stderr, "Error: This is an AMI '94 (1010) BIOS Image.\n");
162 else
163 fprintf(stderr, "Error: This is an AMI '94 BIOS Image.\n");
164 return FALSE;
165 }
166
Luc Verhaegen15587352009-06-26 10:00:29 +0200167 /* now the individual modules */
168 abc = (struct abc *) (BIOSImage + ABCOffset);
169
170 /* Get Date */
171 memcpy(Date, BIOSImage + BIOSLength - 11, 8);
172 Date[8] = 0;
173
174 printf("AMI95 Version\t: %.4s (%s)\n", abc->Version, Date);
175
Luc Verhaegen838fe242009-06-17 00:48:02 +0200176 /* First, the boot rom */
Luc Verhaegen15587352009-06-26 10:00:29 +0200177 {
178 uint32_t BootOffset;
Luc Verhaegen838fe242009-06-17 00:48:02 +0200179 int fd;
180
Luc Verhaegen15587352009-06-26 10:00:29 +0200181 BootOffset = AMIBOffset & 0xFFFF0000;
182
183 printf("0x%05X (%6d bytes) -> amiboot.rom\n", BootOffset, BIOSLength - BootOffset);
Luc Verhaegen838fe242009-06-17 00:48:02 +0200184
185 fd = open("amiboot.rom", O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
186 if (fd < 0) {
187 fprintf(stderr, "Error: unable to open %s: %s\n\n",
188 "amiboot.rom", strerror(errno));
189 return FALSE;
190 }
191
Luc Verhaegen15587352009-06-26 10:00:29 +0200192 write(fd, BIOSImage + BootOffset, BIOSLength - BootOffset);
Luc Verhaegen838fe242009-06-17 00:48:02 +0200193 close(fd);
194 }
195
Luc Verhaegen15587352009-06-26 10:00:29 +0200196 /* now dump the individual modules */
Luc Verhaegena794e6b2009-12-21 11:37:19 +0100197 if (BIOSLength > 0x100000)
198 Offset = (le16toh(abc->BeginHi) << 16) + le16toh(abc->BeginLo);
199 else
200 Offset = (le16toh(abc->BeginHi) << 4) + le16toh(abc->BeginLo);
Luc Verhaegen838fe242009-06-17 00:48:02 +0200201
202 for (i = 0; i < 0x80; i++) {
Luc Verhaegen15587352009-06-26 10:00:29 +0200203 char filename[64], *ModuleName;
Luc Verhaegen15587352009-06-26 10:00:29 +0200204 unsigned char *Buffer;
Luc Verhaegenf5571312010-03-27 13:19:53 +0100205 int BufferSize, ROMSize;
Luc Verhaegen15587352009-06-26 10:00:29 +0200206
Luc Verhaegen838fe242009-06-17 00:48:02 +0200207 part = (struct part *) (BIOSImage + (Offset - BIOSOffset));
208
Michael Karcherb75b76a2010-01-14 16:56:32 +0100209 if (part->IsComprs & 0x80)
Luc Verhaegen838fe242009-06-17 00:48:02 +0200210 Compressed = FALSE;
211 else
212 Compressed = TRUE;
213
Rudolf Marekd6b4fbb2009-12-21 11:30:05 +0100214 /* even they claim they are compressed they arent */
215 if ((part->PartID == 0x40) || (part->PartID == 0x60))
216 Compressed = FALSE;
217
Michael Karcher6d867dc2010-01-14 16:55:36 +0100218 if (part->PartID == 0x20) {
219 uint16_t vid = le32toh(part->RealCS) & 0xFFFF;
220 uint16_t pid = le32toh(part->RealCS) >> 16;
221 sprintf(filename, "amipci_%04X_%04X.rom", vid, pid);
222 } else if (part->PartID == 0x21) {
223 sprintf(filename, "amilang_%c%c.rom", (le32toh(part->RealCS) >> 8) & 0xFF,
224 le32toh(part->RealCS) & 0xFF);
225 } else
Luc Verhaegen15587352009-06-26 10:00:29 +0200226 sprintf(filename, "amibody_%02x.rom", part->PartID);
Luc Verhaegen838fe242009-06-17 00:48:02 +0200227
Luc Verhaegenf5571312010-03-27 13:19:53 +0100228 if (Compressed) {
229 ROMSize = le32toh(part->ROMSize);
230 BufferSize = le32toh(part->ExpSize);
231 } else {
232 BufferSize = le16toh(part->CSize);
233 if (!BufferSize || (BufferSize == 0xFFFF)) {
234 bigpart = (struct bigpart *) (BIOSImage + (Offset - BIOSOffset) - sizeof(struct bigpart));
235 BufferSize = le32toh(bigpart->CSize);
236 }
237 ROMSize = BufferSize;
238 }
239
Luc Verhaegen15587352009-06-26 10:00:29 +0200240 if (Compressed)
Luc Verhaegenf5571312010-03-27 13:19:53 +0100241 printf("0x%05X (%6d bytes)", Offset - BIOSOffset + 0x14, ROMSize);
Luc Verhaegen15587352009-06-26 10:00:29 +0200242 else
Luc Verhaegenf5571312010-03-27 13:19:53 +0100243 printf("0x%05X (%6d bytes)", Offset - BIOSOffset + 0x0C, ROMSize);
Luc Verhaegen838fe242009-06-17 00:48:02 +0200244
Michael Karcher6d867dc2010-01-14 16:55:36 +0100245 printf(" -> %-20s", filename);
Rudolf Marekd6b4fbb2009-12-21 11:30:05 +0100246
Luc Verhaegen15587352009-06-26 10:00:29 +0200247 if (Compressed)
Luc Verhaegenf5571312010-03-27 13:19:53 +0100248 printf(" (%6d bytes)", BufferSize);
Luc Verhaegen15587352009-06-26 10:00:29 +0200249 else
Michael Karcher6d867dc2010-01-14 16:55:36 +0100250 printf(" ");
Luc Verhaegen838fe242009-06-17 00:48:02 +0200251
Luc Verhaegen15587352009-06-26 10:00:29 +0200252 ModuleName = AMI95ModuleNameGet(part->PartID);
253 if (ModuleName)
254 printf(" \"%s\"\n", ModuleName);
255 else
256 printf("\n");
Luc Verhaegen838fe242009-06-17 00:48:02 +0200257
Luc Verhaegen15587352009-06-26 10:00:29 +0200258 Buffer = MMapOutputFile(filename, BufferSize);
259 if (!Buffer)
260 return FALSE;
Luc Verhaegen838fe242009-06-17 00:48:02 +0200261
Luc Verhaegen15587352009-06-26 10:00:29 +0200262 if (Compressed)
Luc Verhaegenf5571312010-03-27 13:19:53 +0100263 LH5Decode(BIOSImage + (Offset - BIOSOffset) + 0x14, ROMSize, Buffer, BufferSize);
Luc Verhaegen15587352009-06-26 10:00:29 +0200264 else
265 memcpy(Buffer, BIOSImage + (Offset - BIOSOffset) + 0x0C, BufferSize);
266
267 munmap(Buffer, BufferSize);
Luc Verhaegen838fe242009-06-17 00:48:02 +0200268
Peter Lemenkovb1567252009-08-01 21:38:34 +0400269 if ((le16toh(part->PrePartHi) == 0xFFFF) || (le16toh(part->PrePartLo) == 0xFFFF))
Luc Verhaegen838fe242009-06-17 00:48:02 +0200270 break;
Luc Verhaegena794e6b2009-12-21 11:37:19 +0100271
272 if (BIOSLength > 0x100000)
273 Offset = (le16toh(part->PrePartHi) << 16) + le16toh(part->PrePartLo);
274 else
275 Offset = (le16toh(part->PrePartHi) << 4) + le16toh(part->PrePartLo);
Luc Verhaegen838fe242009-06-17 00:48:02 +0200276 }
277
278 return TRUE;
279}