blob: 2152160360d970d73a0c0261ec351618068193f8 [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
Lubomir Rintelc707d3e2017-06-25 23:39:52 +020022#define _GNU_SOURCE
23
Luc Verhaegen838fe242009-06-17 00:48:02 +020024#include <stdio.h>
25#include <inttypes.h>
26#include <errno.h>
27#include <string.h>
28#include <sys/mman.h>
Luc Verhaegen838fe242009-06-17 00:48:02 +020029#include <fcntl.h>
30#include <unistd.h>
Lubomir Rintelc707d3e2017-06-25 23:39:52 +020031#include <string.h>
Luc Verhaegen838fe242009-06-17 00:48:02 +020032
33#include "bios_extract.h"
Idwer Vollering0ab44552012-09-23 01:22:20 +020034#include "compat.h"
Luc Verhaegen838fe242009-06-17 00:48:02 +020035#include "lh5_extract.h"
36
Anton Kochkov8f9427f2013-04-07 13:20:30 +040037struct AMI95ModuleName {
38 uint8_t Id;
39 char *Name;
Luc Verhaegen838fe242009-06-17 00:48:02 +020040};
41
Anton Kochkov8f9427f2013-04-07 13:20:30 +040042static struct AMI95ModuleName AMI95ModuleNames[] = {
43 {0x00, "POST"},
44 {0x01, "Setup Server"},
45 {0x02, "RunTime"},
46 {0x03, "DIM"},
47 {0x04, "Setup Client"},
48 {0x05, "Remote Server"},
49 {0x06, "DMI Data"},
50 {0x07, "Green PC"},
51 {0x08, "Interface"},
52 {0x09, "MP"},
53 {0x0A, "Notebook"},
54 {0x0B, "Int-10"},
55 {0x0C, "ROM-ID"},
56 {0x0D, "Int-13"},
57 {0x0E, "OEM Logo"},
58 {0x0F, "ACPI Table"},
59 {0x10, "ACPI AML"},
60 {0x11, "P6 Microcode"},
61 {0x12, "Configuration"},
62 {0x13, "DMI Code"},
63 {0x14, "System Health"},
64 {0x15, "Memory Sizing"},
65 {0x16, "Memory Test"},
66 {0x17, "Debug"},
67 {0x18, "ADM (Display MGR)"},
68 {0x19, "ADM Font"},
69 {0x1A, "Small Logo"},
70 {0x1B, "SLAB"},
71 {0x20, "PCI AddOn ROM"},
72 {0x21, "Multilanguage"},
73 {0x22, "UserDefined"},
74 {0x23, "ASCII Font"},
75 {0x24, "BIG5 Font"},
76 {0x25, "OEM Logo"},
77 {0x2A, "User ROM"},
78 {0x2B, "PXE Code"},
79 {0x2C, "AMI Font"},
80 {0x2E, "User ROM"},
81 {0x2D, "Battery Refresh"},
82 {0x30, "Font Database"},
83 {0x31, "OEM Logo Data"},
84 {0x32, "Graphic Logo Code"},
85 {0x33, "Graphic Logo Data"},
86 {0x34, "Action Logo Code"},
87 {0x35, "Action Logo Data"},
88 {0x36, "Virus"},
89 {0x37, "Online Menu"},
90 {0x38, "Lang1 as ROM"},
91 {0x39, "Lang2 as ROM"},
92 {0x3A, "Lang3 as ROM"},
93 {0x40, "AMD CIM-X NB binary"},
94 {0x60, "AMD CIM-X SB binary"},
95 {0x70, "OSD Bitmaps"},
96 {0xf0, "Asrock Backup Util"},
97 {0xf9, "Asrock AMD AHCI DLL"},
98 {0xfa, "Asrock LOGO GIF"},
99 {0xfb, "Asrock LOGO JPG"},
100 {0xfc, "Asrock LOGO JPG"},
101 {0xfd, "Asrock LOGO PCX - Instant boot"},
102 {0, NULL}
Luc Verhaegen838fe242009-06-17 00:48:02 +0200103};
104
Anton Kochkov8f9427f2013-04-07 13:20:30 +0400105static char *AMI95ModuleNameGet(uint8_t ID)
Luc Verhaegen838fe242009-06-17 00:48:02 +0200106{
Anton Kochkov8f9427f2013-04-07 13:20:30 +0400107 int i;
Luc Verhaegen838fe242009-06-17 00:48:02 +0200108
Anton Kochkov8f9427f2013-04-07 13:20:30 +0400109 for (i = 0; AMI95ModuleNames[i].Name; i++)
110 if (AMI95ModuleNames[i].Id == ID)
111 return AMI95ModuleNames[i].Name;
112 return NULL;
Luc Verhaegen838fe242009-06-17 00:48:02 +0200113}
114
115/*
116 *
117 */
118Bool
Luc Verhaegen15587352009-06-26 10:00:29 +0200119AMI95Extract(unsigned char *BIOSImage, int BIOSLength, int BIOSOffset,
Luc Verhaegen838fe242009-06-17 00:48:02 +0200120 uint32_t AMIBOffset, uint32_t ABCOffset)
121{
Anton Kochkov8f9427f2013-04-07 13:20:30 +0400122 Bool Compressed;
123 uint32_t Offset;
124 char Date[9];
125 int i;
Luc Verhaegen838fe242009-06-17 00:48:02 +0200126
Anton Kochkov8f9427f2013-04-07 13:20:30 +0400127 struct abc {
128 const char AMIBIOSC[8];
129 const char Version[4];
130 const uint16_t CRCLen;
131 const uint32_t CRC32;
132 const uint16_t BeginLo;
133 const uint16_t BeginHi;
134 } *abc;
Luc Verhaegen838fe242009-06-17 00:48:02 +0200135
Anton Kochkov8f9427f2013-04-07 13:20:30 +0400136 struct bigpart {
137 const uint32_t CSize;
138 const uint32_t Unknown;
139 } *bigpart;
Rudolf Marekd6b4fbb2009-12-21 11:30:05 +0100140
Anton Kochkov8f9427f2013-04-07 13:20:30 +0400141 struct part {
142 /* When Previous Part Address is 0xFFFFFFFF, then this is the last part. */
143 const uint16_t PrePartLo; /* Previous part low word */
144 const uint16_t PrePartHi; /* Previous part high word */
145 const uint16_t CSize; /* Header length */
146 const uint8_t PartID; /* ID for this header */
147 const uint8_t IsComprs; /* 0x80 -> compressed */
148 const uint32_t RealCS; /* Old BIOSes:
149 Real Address in RAM where to expand to
150 Now:
151 Type 0x20 PCI ID of device for this ROM
152 Type 0x21 Language ID (ascii) */
153 const uint32_t ROMSize; /* Compressed Length */
154 const uint32_t ExpSize; /* Expanded Length */
155 } *part;
Luc Verhaegen838fe242009-06-17 00:48:02 +0200156
Anton Kochkov8f9427f2013-04-07 13:20:30 +0400157 if (!ABCOffset) {
158 if ((BIOSImage[8] == '1') && (BIOSImage[9] == '0') &&
159 (BIOSImage[11] == '1') && (BIOSImage[12] == '0'))
160 fprintf(stderr,
161 "Error: This is an AMI '94 (1010) BIOS Image.\n");
162 else
163 fprintf(stderr,
164 "Error: This is an AMI '94 BIOS Image.\n");
165 return FALSE;
166 }
Luc Verhaegen838fe242009-06-17 00:48:02 +0200167
Lubomir Rintelc707d3e2017-06-25 23:39:52 +0200168 if (ABCOffset + sizeof (struct abc) < BIOSLength) {
169 abc = (struct abc *)(BIOSImage + ABCOffset);
170 if (memcmp (abc->Version, "AMIN", 4) == 0) {
171 /* Skip to next one if immediately followed by "AMINCBLK"
172 * header in place of a version number. */
173 abc = (struct abc *)memmem (BIOSImage + ABCOffset + 1,
174 BIOSLength - ABCOffset - 1 - sizeof (struct abc),
175 "AMIBIOSC", 8);
176 }
177 } else
178 abc = NULL;
179
180 if (!abc) {
181 fprintf(stderr,
182 "Error: short read after AMIBIOSC signature.\n");
183 return FALSE;
184 }
Luc Verhaegen15587352009-06-26 10:00:29 +0200185
Anton Kochkov8f9427f2013-04-07 13:20:30 +0400186 /* Get Date */
187 memcpy(Date, BIOSImage + BIOSLength - 11, 8);
188 Date[8] = 0;
Luc Verhaegen15587352009-06-26 10:00:29 +0200189
Anton Kochkov8f9427f2013-04-07 13:20:30 +0400190 printf("AMI95 Version\t: %.4s (%s)\n", abc->Version, Date);
Luc Verhaegen15587352009-06-26 10:00:29 +0200191
Anton Kochkov8f9427f2013-04-07 13:20:30 +0400192 /* First, the boot rom */
Luc Verhaegen15587352009-06-26 10:00:29 +0200193 uint32_t BootOffset;
Luc Verhaegen838fe242009-06-17 00:48:02 +0200194 int fd;
195
Luc Verhaegen15587352009-06-26 10:00:29 +0200196 BootOffset = AMIBOffset & 0xFFFF0000;
197
Anton Kochkov8f9427f2013-04-07 13:20:30 +0400198 printf("0x%05X (%6d bytes) -> amiboot.rom\n", BootOffset,
199 BIOSLength - BootOffset);
Luc Verhaegen838fe242009-06-17 00:48:02 +0200200
201 fd = open("amiboot.rom", O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
202 if (fd < 0) {
Anton Kochkov8f9427f2013-04-07 13:20:30 +0400203 fprintf(stderr, "Error: unable to open %s: %s\n\n",
204 "amiboot.rom", strerror(errno));
205 return FALSE;
Luc Verhaegen838fe242009-06-17 00:48:02 +0200206 }
207
Luc Verhaegen15587352009-06-26 10:00:29 +0200208 write(fd, BIOSImage + BootOffset, BIOSLength - BootOffset);
Luc Verhaegen838fe242009-06-17 00:48:02 +0200209 close(fd);
Luc Verhaegen838fe242009-06-17 00:48:02 +0200210
Anton Kochkov8f9427f2013-04-07 13:20:30 +0400211 /* now dump the individual modules */
212 if (BIOSLength > 0x100000)
213 Offset = (le16toh(abc->BeginHi) << 16) + le16toh(abc->BeginLo);
Luc Verhaegen838fe242009-06-17 00:48:02 +0200214 else
Anton Kochkov8f9427f2013-04-07 13:20:30 +0400215 Offset = (le16toh(abc->BeginHi) << 4) + le16toh(abc->BeginLo);
Luc Verhaegen838fe242009-06-17 00:48:02 +0200216
Anton Kochkov8f9427f2013-04-07 13:20:30 +0400217 for (i = 0; i < 0x80; i++) {
218 char filename[64], *ModuleName;
219 unsigned char *Buffer;
220 int BufferSize, ROMSize;
Rudolf Marekd6b4fbb2009-12-21 11:30:05 +0100221
Anton Kochkov8f9427f2013-04-07 13:20:30 +0400222 part = (struct part *)(BIOSImage + (Offset - BIOSOffset));
Luc Verhaegen838fe242009-06-17 00:48:02 +0200223
Anton Kochkov8f9427f2013-04-07 13:20:30 +0400224 if (part->IsComprs & 0x80)
225 Compressed = FALSE;
226 else
227 Compressed = TRUE;
228
229 /* even they claim they are compressed they arent */
230 if ((part->PartID == 0x40) || (part->PartID == 0x60))
231 Compressed = FALSE;
232
233 if (part->PartID == 0x20) {
234 uint16_t vid = le32toh(part->RealCS) & 0xFFFF;
235 uint16_t pid = le32toh(part->RealCS) >> 16;
236 sprintf(filename, "amipci_%04X_%04X.rom", vid, pid);
237 } else if (part->PartID == 0x21) {
238 sprintf(filename, "amilang_%c%c.rom",
239 (le32toh(part->RealCS) >> 8) & 0xFF,
240 le32toh(part->RealCS) & 0xFF);
241 } else
242 sprintf(filename, "amibody_%02x.rom", part->PartID);
243
244 if (Compressed) {
245 ROMSize = le32toh(part->ROMSize);
246 BufferSize = le32toh(part->ExpSize);
247 } else {
248 BufferSize = le16toh(part->CSize);
249 if (!BufferSize || (BufferSize == 0xFFFF)) {
250 bigpart =
251 (struct bigpart *)(BIOSImage +
252 (Offset - BIOSOffset) -
253 sizeof(struct bigpart));
254 BufferSize = le32toh(bigpart->CSize);
255 }
256 ROMSize = BufferSize;
257 }
258
259 if (Compressed)
260 printf("0x%05X (%6d bytes)", Offset - BIOSOffset + 0x14,
261 ROMSize);
262 else
263 printf("0x%05X (%6d bytes)", Offset - BIOSOffset + 0x0C,
264 ROMSize);
265
266 printf(" -> %-20s", filename);
267
268 if (Compressed)
269 printf(" (%6d bytes)", BufferSize);
270 else
271 printf(" ");
272
273 ModuleName = AMI95ModuleNameGet(part->PartID);
274 if (ModuleName)
275 printf(" \"%s\"\n", ModuleName);
276 else
277 printf("\n");
278
279 Buffer = MMapOutputFile(filename, BufferSize);
280 if (!Buffer)
281 return FALSE;
282
283 if (Compressed)
284 LH5Decode(BIOSImage + (Offset - BIOSOffset) + 0x14,
285 ROMSize, Buffer, BufferSize);
286 else
287 memcpy(Buffer, BIOSImage + (Offset - BIOSOffset) + 0x0C,
288 BufferSize);
289
290 munmap(Buffer, BufferSize);
291
292 if ((le16toh(part->PrePartHi) == 0xFFFF)
293 || (le16toh(part->PrePartLo) == 0xFFFF))
294 break;
295
296 if (BIOSLength > 0x100000)
297 Offset =
298 (le16toh(part->PrePartHi) << 16) +
299 le16toh(part->PrePartLo);
300 else
301 Offset =
302 (le16toh(part->PrePartHi) << 4) +
303 le16toh(part->PrePartLo);
Luc Verhaegenf5571312010-03-27 13:19:53 +0100304 }
305
Anton Kochkov8f9427f2013-04-07 13:20:30 +0400306 return TRUE;
Luc Verhaegen838fe242009-06-17 00:48:02 +0200307}