summaryrefslogtreecommitdiff
path: root/phnxdeco/phnxdeco.c
blob: 4d9968d19dfab08a4ffa742301e915fae4c6dcd9 (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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/*
 *      Phoenix BIOS Decompress
 *
 *      Copyright (C) 2000, 2002, 2003 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	<stdlib.h>
#include	<memory.h>

#include	"phnxdeco.h"

#define	BLOCK		0x8000

#define	Xtract		0x10
#define	List	   	0x11

static unsigned int FoundAt(FILE * ptx, unsigned char *Buf, char *Pattern, unsigned int BLOCK_LEN)
{

    unsigned int i, Ret;
    unsigned short Len;
    Len = strlen(Pattern);
    for (i = 0; i < BLOCK_LEN - 0x80; i++) {
	if (memcmp(Buf + i, Pattern, Len) == 0) {
	    Ret = ftell(ptx) - (BLOCK_LEN - i);
	    return (Ret);
	}
    }
    return 0;
}
struct PhoenixModuleName
{
    char Id;
    char *Name;
};

static struct PhoenixModuleName
PhoenixModuleNames[] = {
    {'A', "ACPI"},
    {'B', "BIOSCODE"},
    {'C', "UPDATE"},
    {'D', "DISPLAY"},
    {'E', "SETUP"},
    {'F', "FONT"},
    {'G', "DECOMPCODE"},
    {'I', "BOOTBLOCK"},
    {'L', "LOGO"},
    {'M', "MISER"},
    {'N', "ROMPILOTLOAD"},
    {'O', "NETWORK"},
    {'P', "ROMPILOTINIT"},
    {'R', "OPROM"},
    {'S', "STRINGS"},
    {'T', "TEMPLATE"},
    {'U', "USER"},
    {'X', "ROMEXEC"},
    {'W', "WAV"},
    {'H', "TCPA_H"}, /* TCPA (Trusted Computing), USBKCLIB? */
    {'K', "TCPA_K"}, /* TCPA (Trusted Computing), "AUTH"? */
    {'Q', "TCPA_Q"}, /* TCPA (Trusted Computing), "SROM"? */
    {'<', "TCPA_<"},
    {'*', "TCPA_*"},
    {'?', "TCPA_?"},
    {'J', "SmartCardPAS"},
};

static char *
PhoenixModuleNameGet(char Id)
{
    int i;

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

    return "";
}

#define COMPRESSION_NONE    0
#define COMPRESSION_UNKNOWN 1
#define COMPRESSION_LZARI   2
#define COMPRESSION_LZSS    3
#define COMPRESSION_LZHUF   4
#define COMPRESSION_LZINT   5

/*
 * This is nothing but the lzss algo, done rather horribly wrong.
 */
static void decodeM3(FILE *ptx, FILE *bmx, unsigned int RealLen)
{
    unsigned short Index, Index2, DX, Loop, i;
    unsigned char Buffer[0x1000], tmp;
    unsigned int Now;

    memset(Buffer, 0, 0x1000);

    DX = 0;
    Index = 0xFEE;
    Now = 0;

    for (;;) {
	DX >>= 1;
	if ((DX & 0x100) == 0) {
	    if (Now >= RealLen)
		return;
	    fread(&tmp, 1, 1, ptx);
	    DX = (unsigned short) (0xFF) * 0x100 + tmp;
	};

	if ((DX & 0x1) != 0) {
	    if (Now++ >= RealLen)
		return;

	    fread(&tmp, 1, 1, ptx);
	    fwrite(&tmp, 1, 1, bmx);
	    Buffer[Index++] = tmp;
	    Index &= 0xFFF;
	    continue;
	};

	fread(&Index2, 1, 1, ptx);
	Index2 &= 0xFF;

	fread(&tmp, 1, 1, ptx);

	Index2 += (tmp & 0xF0) << 4;
	Loop = (tmp & 0xf) + 3;

	for (i = 0; i < Loop; i++) {
	    tmp = Buffer[Index2++];
	    Index2 &= 0xFFF;
	    fwrite(&tmp, 1, 1, bmx);
	    Buffer[Index++] = tmp;
	    Index &= 0xFFF;
	}

	Now += Loop;
    }

}

typedef struct {
    unsigned int Prev;
    unsigned char Sig[3];
    unsigned char ID_HI;
    unsigned char ID_LO;
    unsigned char HeadLen;
    unsigned char isPacked;
    unsigned short Offset;
    unsigned short Segment;
    unsigned int ExpLen1;
    unsigned int Packed1;
    unsigned int Packed2;
    unsigned int ExpLen2;
} PHOENIXHEAD;

typedef struct {
    unsigned char Name[6];
    unsigned short Flags;
    unsigned short Len;
} PHNXID;

/*
 * Modified Module Detection & Manipulating
 * According to BCPSYS block
 */
static unsigned char TotalSecM(FILE * ptx, unsigned char * Buf, unsigned char Action, unsigned int Start, unsigned int ConstOff, unsigned int SYSOff)
{
    FILE *pto;
    unsigned int i;
    unsigned int blkend = 0, blkstart = 0;
    PHOENIXHEAD phhead;
    unsigned char TotalSec = 0;
    unsigned int Offset = Start;

    phhead.Prev = 0xFFFF0000;
    fseek(ptx, 0, 2);

    if (Action == List)
	printf("\nC I  COMP      START     END    LENGTH   LINK TO   FILEOFFSET");


    while (phhead.Prev != 0x0 && !feof(ptx)) {
	fseek(ptx, Offset, 0);
	TotalSec++;
	Start = Offset;
	if (Start > 0x100000)
	    break;
	fread(&phhead, 1, sizeof(phhead), ptx);
	Offset = phhead.Prev - ConstOff;

	blkend = Start + ConstOff + sizeof(PHOENIXHEAD) - 5;
	blkstart = Start + 0xFFF00000;
	switch (Action) {
	case List:

	    printf("\n%c  %.1X   %1d    %8X  %8X  %5.X    %8X   %5.2Xh %05X", phhead.ID_LO, phhead.ID_HI,
		   phhead.isPacked, blkstart, blkend + phhead.Packed1, phhead.Packed1, phhead.Prev, Start, phhead.Segment << 16 | phhead.Offset);


	    break;

	case Xtract:
	    {
		char *modulename = PhoenixModuleNameGet(phhead.ID_LO);
		char filename[64];

		snprintf(filename, 64, "%s%1.1X.rom", modulename, phhead.ID_HI);
		printf("\n%c %1.1X -> %s", phhead.ID_LO, phhead.ID_HI, filename);


		if ((pto = fopen(filename, "wb")) == NULL) {
		    printf("\nFile %s I/O error..Exit", filename);
		    exit(1);
		}
	    }

	    /* Evidently, isPacked == PackedLevel */
	    if (phhead.isPacked == 0x5) {
		interfacing interface;

		interface.infile = ptx;
		interface.outfile = pto;
		interface.original = phhead.ExpLen1;
		interface.packed = phhead.Packed1;

		decode(interface);
	    } else if (phhead.isPacked == 0x3) {
		fseek(ptx, -4L, 1);
		decodeM3(ptx, pto, phhead.ExpLen1);
	    } else {
		unsigned char tmp[1];

		fseek(ptx, -4L, 1);
		for (i = 0; i < phhead.ExpLen1; i++) {
		    fread(tmp, 1, 1, ptx);
		    fwrite(tmp, 1, 1, pto);
		};
	    }
	    fclose(pto);

	    break;
	}
    }

    return (TotalSec);
}

static unsigned int IsPhoenixBIOS(FILE * ptx, unsigned char * Buf)
{

    unsigned int CurPos = 0;
    char FirstPattern[] = "Phoenix FirstBIOS";
    char PhoenixPattern[] = "PhoenixBIOS 4.0";

    rewind(ptx);
    while (!feof(ptx)) {
	fread(Buf, 1, BLOCK, ptx);
	if ((CurPos = FoundAt(ptx, Buf, PhoenixPattern, BLOCK)) != 0)
	    break;
	/* O'K, we got PhoenixBIOS 4.0 hook */
	CurPos = ftell(ptx) - 0x100;
    }

    if (feof(ptx)) {
	rewind(ptx);
	while (!feof(ptx)) {
	    fread(Buf, 1, BLOCK, ptx);
	    if ((CurPos = FoundAt(ptx, Buf, FirstPattern, BLOCK)) != 0)
		break;
	    /* O'K, we got Phoenix FirstBIOS hook */
	    CurPos = ftell(ptx) - 0x100;
	}

	/* Neither PhoenixBIOS 4.0 nor FirstBIOS */
	if (feof(ptx))
	    return (0);

    }

    return (CurPos);
}

int main(int argc, char *argv[])
{
    FILE *ptx;
    unsigned char Buf[BLOCK];
    unsigned int CurPos, fLen, Start, Offset;
    unsigned int SYSOff;
    char BCPSEGMENT[] = "BCPSEGMENT";
    char phdate[18]; /* "XX/XX/XX\20XX:XX:XX\0" */
    unsigned char TotalSections = 0, Action, Mods = 0;
    PHNXID IDMod;

    memset(Buf, 0, BLOCK);

    if (argc != 3) {
	printf("wrong usage.\n");
	return 1;
    }

    if (!strcmp(argv[1], "x"))
	Action = Xtract;
    else if (!strcmp(argv[1], "l"))
	Action = List;
    else {
	printf("wrong usage.\n");
	return 1;
    }

    if ((ptx = fopen(argv[2], "rb")) == NULL) {
	printf("\nFATAL ERROR: File %s opening error...\n", argv[2]);
	return 0;
    };

    CurPos = 0;
    IDMod.Name[0] = 0xff;
    IDMod.Len = 0xff;
    SYSOff = 0;

    fseek(ptx, 0, 2);
    fLen = ftell(ptx);
    rewind(ptx);
    printf("Filelength\t: %X (%u bytes)\n", fLen, fLen);
    printf("Filename\t: %s\n", argv[2]);

    while (!feof(ptx)) {
	fread(Buf, 1, BLOCK, ptx);
	if ((CurPos = FoundAt(ptx, Buf, BCPSEGMENT, BLOCK)) != 0)
	    break;
	/* O'K, we got PhoenixBIOS BCPSEGMENT hook */
	CurPos = ftell(ptx) - 0x100;

    }

    if (feof(ptx)) {
	printf("Searched through file. Not a PhoenixBIOS\n");
	exit(1);
    }

    printf("PhoenixBIOS hook found at\t: %X\n", CurPos);
    CurPos += 10;
    fseek(ptx, (unsigned int) (CurPos), 0);

    while (IDMod.Name[0] != 0x0 && IDMod.Len != 0x0) {
	fread(&IDMod, 1, sizeof(IDMod), ptx);

	/* Wrong Count w/ ALR and some S/N internal errors ? */
	if (IDMod.Name[0] < 0x41 && IDMod.Name[0] != 0x0) {
	    do {
		fread(Buf, 1, 1, ptx);
	    } while (Buf[0] != 0x42);
	    fseek(ptx, -1L, SEEK_CUR);
	    CurPos = ftell(ptx);
	    fread(&IDMod, 1, sizeof(IDMod), ptx);
	};

	if (memcmp(IDMod.Name, "BCPSYS", 6) == 0)
	    SYSOff = CurPos;
	CurPos += IDMod.Len;
	fseek(ptx, (unsigned int) (CurPos), 0);
	if (IDMod.Name[0] == 0x0 || IDMod.Name[0] < 0x41)
	    break;
	else
	    Mods++;
    }

    printf("System Information at\t\t: %X\n", SYSOff);

    fseek(ptx, SYSOff + 15, 0);
    fread(&phdate, 1, 17, ptx);
    phdate[8] = ' ';
    phdate[17] = 0;

    fseek(ptx, SYSOff + 0x77, 0);
    /* Move to the pointer of 1st module */
    fread(&Start, 1, sizeof(Start), ptx);
    Offset = (0xFFFE0000 - (ftell(ptx) & 0xFFFE0000));
    Start -= Offset;

    /* Move to the DEVEL string */
    fseek(ptx, SYSOff + 0x37, 0);
    fread(Buf, 1, 8, ptx);

    printf("Version\t\t: %8.8s\n", Buf);
    printf("Start\t\t: %X\n", Start);
    printf("Offset\t\t: %X\n", 0xFFFF0000 - Offset);

    printf("BCP Modules\t: %i\n", Mods);

    printf("Released\t: %s\n", phdate);

    CurPos = IsPhoenixBIOS(ptx, Buf);
    fseek(ptx, (unsigned int) (CurPos), 0);

    fread(Buf, 1, 0x100, ptx);

    printf("\t%.64s\n", Buf);

    TotalSections = TotalSecM(ptx, Buf, Action, Start, Offset, SYSOff);

    printf("\n");
    printf("Total Sections: %u\n", TotalSections);

    fclose(ptx);

    printf("\n");
    return 0;
}