summaryrefslogtreecommitdiff
path: root/ami.c
blob: 018eb3d708714116015d60500e52ba0d5d64f56f (plain)
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
 * Decompression utility for AMI BIOSes.
 *
 * Copyright 2009      Luc Verhaegen <libv@skynet.be>
 * Copyright 2000-2006 Anthony Borisow
 *
 * 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.
 */

#include <stdio.h>
#include <inttypes.h>
#include <errno.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <endian.h>

#include "bios_extract.h"
#include "lh5_extract.h"

struct AMI95ModuleName
{
    uint8_t Id;
    char *Name;
};

static struct AMI95ModuleName
AMI95ModuleNames[] = {
    {0x00, "POST"},
    {0x01, "Setup Server"},
    {0x02, "RunTime"},
    {0x03, "DIM"},
    {0x04, "Setup Client"},
    {0x05, "Remote Server"},
    {0x06, "DMI Data"},
    {0x07, "Green PC"},
    {0x08, "Interface"},
    {0x09, "MP"},
    {0x0A, "Notebook"},
    {0x0B, "Int-10"},
    {0x0C, "ROM-ID"},
    {0x0D, "Int-13"},
    {0x0E, "OEM Logo"},
    {0x0F, "ACPI Table"},
    {0x10, "ACPI AML"},
    {0x11, "P6 Microcode"},
    {0x12, "Configuration"},
    {0x13, "DMI Code"},
    {0x14, "System Health"},
    {0x15, "Memory Sizing"},
    {0x16, "Memory Test"},
    {0x17, "Debug"},
    {0x18, "ADM (Display MGR)"},
    {0x19, "ADM Font"},
    {0x1A, "Small Logo"},
    {0x1B, "SLAB"},
    {0x20, "PCI AddOn ROM"},
    {0x21, "Multilanguage"},
    {0x22, "UserDefined"},
    {0x23, "ASCII Font"},
    {0x24, "BIG5 Font"},
    {0x25, "OEM Logo"},
    {0x2A, "User ROM"},
    {0x2B, "PXE Code"},
    {0x2C, "AMI Font"},
    {0x2E, "User ROM"},
    {0x2D, "Battery Refresh"},
    {0x30, "Font Database"},
    {0x31, "OEM Logo Data"},
    {0x32, "Graphic Logo Code"},
    {0x33, "Graphic Logo Data"},
    {0x34, "Action Logo Code"},
    {0x35, "Action Logo Data"},
    {0x36, "Virus"},
    {0x37, "Online Menu"},
    {0x38, "Lang1 as ROM"},
    {0x39, "Lang2 as ROM"},
    {0x3A, "Lang3 as ROM"},
    {0x40, "AMD CIM-X NB binary"},
    {0x60, "AMD CIM-X SB binary"},
    {0x70, "OSD Bitmaps"},
    {0xf0, "Asrock Backup Util"},
    {0xf9, "Asrock AMD AHCI DLL"},
    {0xfa, "Asrock LOGO GIF"},
    {0xfb, "Asrock LOGO JPG"},
    {0xfc, "Asrock LOGO JPG"},
    {0xfd, "Asrock LOGO PCX - Instant boot"},
    {0, NULL}
};

static char *
AMI95ModuleNameGet(uint8_t ID)
{
    int i;

    for (i = 0; AMI95ModuleNames[i].Name; i++)
	if (AMI95ModuleNames[i].Id == ID)
	    return AMI95ModuleNames[i].Name;

    return NULL;
}

/*
 *
 */
Bool
AMI95Extract(unsigned char *BIOSImage, int BIOSLength, int BIOSOffset,
	     uint32_t AMIBOffset, uint32_t ABCOffset)
{
    Bool Compressed;
    uint32_t Offset;
    char Date[9];
    int i;

    struct abc {
	const char AMIBIOSC[8];
	const char Version[4];
	const uint16_t CRCLen;
	const uint32_t CRC32;
	const uint16_t BeginLo;
	const uint16_t BeginHi;
    } *abc;

    struct bigpart {
	const uint32_t CSize;
	const uint32_t Unknown;
    } *bigpart;

    struct part {
	/* When Previous Part Address is 0xFFFFFFFF, then this is the last part. */
	const uint16_t PrePartLo; /* Previous part low word */
	const uint16_t PrePartHi; /* Previous part high word */
	const uint16_t CSize; /* Header length */
	const uint8_t PartID; /* ID for this header */
	const uint8_t IsComprs; /* 0x80 -> compressed */
	const uint32_t RealCS; /* Old BIOSes: 
	                             Real Address in RAM where to expand to
	                          Now:
	                             Type 0x20 PCI ID of device for this ROM
	                             Type 0x21 Language ID (ascii) */
	const uint32_t ROMSize; /* Compressed Length */
	const uint32_t ExpSize; /* Expanded Length */
    } *part;

    if (!ABCOffset) {
	if ((BIOSImage[8] == '1') && (BIOSImage[9] == '0') &&
	    (BIOSImage[11] == '1') && (BIOSImage[12] == '0'))
	    fprintf(stderr, "Error: This is an AMI '94 (1010) BIOS Image.\n");
	else
	    fprintf(stderr, "Error: This is an AMI '94 BIOS Image.\n");
	return FALSE;
    }

    /* now the individual modules */
    abc = (struct abc *) (BIOSImage + ABCOffset);

    /* Get Date */
    memcpy(Date, BIOSImage + BIOSLength - 11, 8);
    Date[8] = 0;

    printf("AMI95 Version\t: %.4s (%s)\n", abc->Version, Date);

    /* First, the boot rom */
    {
	uint32_t BootOffset;
	int fd;

	BootOffset = AMIBOffset & 0xFFFF0000;

	printf("0x%05X (%6d bytes) -> amiboot.rom\n", BootOffset, BIOSLength - BootOffset);

	fd = open("amiboot.rom", O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
	if (fd < 0) {
	    fprintf(stderr, "Error: unable to open %s: %s\n\n",
		    "amiboot.rom", strerror(errno));
	    return FALSE;
	}

	write(fd, BIOSImage + BootOffset, BIOSLength - BootOffset);
	close(fd);
    }

    /* now dump the individual modules */
    if (BIOSLength > 0x100000)
	Offset = (le16toh(abc->BeginHi) << 16) + le16toh(abc->BeginLo);
    else
	Offset = (le16toh(abc->BeginHi) << 4) + le16toh(abc->BeginLo);

    for (i = 0; i < 0x80; i++) {
	char filename[64], *ModuleName;
	unsigned char *Buffer;
	int BufferSize;

	part = (struct part *) (BIOSImage + (Offset - BIOSOffset));

	if (part->IsComprs & 0x80)
	    Compressed = FALSE;
	else
	    Compressed = TRUE;

	/* even they claim they are compressed they arent */
	if ((part->PartID == 0x40) || (part->PartID == 0x60))
	    Compressed = FALSE;

	if (part->PartID == 0x20) {
	    uint16_t vid = le32toh(part->RealCS) & 0xFFFF;
	    uint16_t pid = le32toh(part->RealCS) >> 16;
	    sprintf(filename, "amipci_%04X_%04X.rom", vid, pid);
        } else if (part->PartID == 0x21) {
            sprintf(filename, "amilang_%c%c.rom", (le32toh(part->RealCS) >> 8) & 0xFF,
                                                   le32toh(part->RealCS) & 0xFF);
        } else
	    sprintf(filename, "amibody_%02x.rom", part->PartID);

	if (Compressed)
	    printf("0x%05X (%6d bytes)", Offset - BIOSOffset + 0x14, le32toh(part->ROMSize));
	else
	    printf("0x%05X (%6d bytes)", Offset - BIOSOffset + 0x0C, le16toh(part->CSize));

	printf(" -> %-20s", filename);

	if (Compressed)
	    printf(" (%6d bytes)", le32toh(part->ExpSize));
	else
	    printf("               ");

	ModuleName = AMI95ModuleNameGet(part->PartID);
	if (ModuleName)
	    printf("  \"%s\"\n", ModuleName);
	else
	    printf("\n");

	if (Compressed)
	    BufferSize = le32toh(part->ExpSize);
	else
	    BufferSize = le16toh(part->CSize);

	if (((BufferSize == 0xFFFF) || !BufferSize) && !Compressed)  {
	    bigpart = (struct bigpart *) (BIOSImage + (Offset - BIOSOffset) - sizeof(struct bigpart));
	    BufferSize = bigpart->CSize;
	}

	Buffer = MMapOutputFile(filename, BufferSize);
	if (!Buffer)
	    return FALSE;

	if (Compressed)
	    LH5Decode(BIOSImage + (Offset - BIOSOffset) + 0x14, le32toh(part->ROMSize),
		      Buffer, BufferSize);
	else
	    memcpy(Buffer, BIOSImage + (Offset - BIOSOffset) + 0x0C, BufferSize);

	munmap(Buffer, BufferSize);

	if ((le16toh(part->PrePartHi) == 0xFFFF) || (le16toh(part->PrePartLo) == 0xFFFF))
	    break;

	if (BIOSLength > 0x100000)
	    Offset = (le16toh(part->PrePartHi) << 16) + le16toh(part->PrePartLo);
	else
	    Offset = (le16toh(part->PrePartHi) << 4) + le16toh(part->PrePartLo);
    }

    return TRUE;
}