From 57741675e985ba6d91a001c609fb3e6190efc60d Mon Sep 17 00:00:00 2001 From: Luc Verhaegen Date: Tue, 16 Jun 2009 23:34:21 +0200 Subject: Add bcpvpd tool. Also rework Makefile a bit and toss something into a README. --- .gitignore | 1 + Makefile | 18 +++++--- README | 14 ++++++ bcpvpd.c | 154 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 180 insertions(+), 7 deletions(-) create mode 100644 README create mode 100644 bcpvpd.c diff --git a/.gitignore b/.gitignore index 459ab7c..9530fa7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ amideco lh5_test +bcpvpd *.o *~ diff --git a/Makefile b/Makefile index db112ce..dc00b18 100644 --- a/Makefile +++ b/Makefile @@ -2,18 +2,22 @@ MAKE = make CFLAGS = -g -fpack-struct -Wall CC = gcc -all: amideco +all: amideco bcpvpd -amideco: lh5_extract.o amideco.c - $(CC) $(CFLAGS) lh5_extract.o amideco.c -o amideco +AMIDECO_OBJS = lh5_extract.o amideco.o +amideco: $(AMIDECO_OBJS) + $(CC) $(CFLAGS) $(AMIDECO_OBJS) -o amideco -lh5_extract.o: lh5_extract.c - $(CC) $(CFLAGS) -c lh5_extract.c -o lh5_extract.o +bcpvpd: bcpvpd.c + $(CC) $(CFLAGS) bcpvpd.c -o bcpvpd -lh5_test: lh5_extract.o lh5_test.c - $(CC) $(CFLAGS) lh5_extract.o lh5_test.c -o lh5_test +# just here to easily verify the functionality of the lh5 routine +LH5_TEST_OBJS = lh5_extract.o lh5_test.o +lh5_test: $(LH5_TEST_OBJS) + $(CC) $(CFLAGS) $(LH5_TEST_OBJS) -o lh5_test clean: rm -f *.o rm -f amideco + rm -f bcpvpd rm -f lh5_test diff --git a/README b/README new file mode 100644 index 0000000..5301bec --- /dev/null +++ b/README @@ -0,0 +1,14 @@ +amideco: +-------- +Tool to extract AMI95 BIOS into its individual submodules, will eventually +be transformed into bios_extract.c and ami/award/phoenix/whatever.c + +bcpvpd: +------- +Extracts IBM compressed BIOS images (BCPVPD and $IBMCOMP) so that bios_extract +can handle it. + +lh5_test: +--------- +Sample program left over from development, but kept to be able to easily +verify lh5 extraction functionality. Will not be built per default. diff --git a/bcpvpd.c b/bcpvpd.c new file mode 100644 index 0000000..e9ead46 --- /dev/null +++ b/bcpvpd.c @@ -0,0 +1,154 @@ +/* + * Copyright 2009 Luc Verhaegen + * + * 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. + */ + +/* + * This code is heavily based on Veit Kannegiesers + * e_bcpvpd.pas program. This software comes with no license, but is freely + * downloadable at http://kannegieser.net/veit/quelle/phoedeco_src.arj + */ +/* + * It should be very straightfoward to add support for the $COMPIBM compressed + * bios images as well. According to Veits code, the id-string is "$COMPIBM", + * and the data starts straight after the (not null-terminated) id-string. + */ + +#define _GNU_SOURCE 1 + +#include +#include +#include +#include +#include +#include + +static inline int +BufferWrite(int fd, unsigned char *Buffer, int *BufferCount, unsigned char value) +{ + Buffer[*BufferCount] = value; + *BufferCount += 1; + + if (*BufferCount == 0x1000) { + if (write(fd, Buffer, 0x1000) != 0x1000) { + fprintf(stderr, "Error writing to output file: %s", + strerror(errno)); + return 1; + } + *BufferCount = 0; + } + + return 0; +} + +static int +BCPVPDExtract(unsigned char *Input, int InputSize, int fd) +{ + unsigned char Buffer[0x1000]; + unsigned short BitBuffer = 0; + int i = 0, k, BitCount = 8, BufferCount = 0; + + while (i < InputSize) { + + if (BitCount == 8) { + BitBuffer = Input[i]; + BitCount = -1; + } else if ((BitBuffer >> BitCount) & 0x01) { + if (BufferWrite(fd, Buffer, &BufferCount, Input[i])) + return 1; + } else if ((i + 1) < InputSize) { + int offset = ((Input[i] | ((Input[i + 1] & 0xF0) << 4)) - 0xFEE) & 0xFFF; + int length = (Input[i + 1] & 0x0F) + 3; + + for (k = 0; k < length; k++) { + if (BufferWrite(fd, Buffer, &BufferCount, Buffer[(offset + k) & 0xFFF])) + return 1; + } + i++; + } else { + fprintf(stderr, "Error: requesting data beyond end of input file.\n"); + return 1; + } + + i++; + BitCount++; + } + + if (BufferCount) { + if (write(fd, Buffer, BufferCount) != BufferCount) { + fprintf(stderr, "Error writing to output file: %s", + strerror(errno)); + return 1; + } + } + + return 0; +} + +int +main(int argc, char *argv[]) +{ + int infd, outfd; + unsigned char *InputBuffer; + int InputBufferSize; + + if (argc != 3) { + printf("usage: %s \n", argv[0]); + return 1; + } + + infd = open(argv[1], O_RDONLY); + if (infd < 0) { + fprintf(stderr, "Error: Failed to open %s: %s\n", + argv[1], strerror(errno)); + return 1; + } + + InputBufferSize = lseek(infd, 0, SEEK_END); + if (InputBufferSize < 0) { + fprintf(stderr, "Error: Failed to lseek \"%s\": %s\n", + argv[1], strerror(errno)); + return 1; + } + + InputBuffer = mmap(NULL, InputBufferSize, PROT_READ, MAP_PRIVATE, infd, 0); + if (InputBuffer < 0) { + fprintf(stderr, "Error: Failed to mmap %s: %s\n", + argv[1], strerror(errno)); + return 1; + } + + if (InputBufferSize < 0x52) { + fprintf(stderr, "Error: \"%s\" is too small tp be a BCPVPD file.\n", + argv[1]); + return 1; + } + + if (strncmp((char *) InputBuffer, "BCPVPD", 7)) { + fprintf(stderr, "Error: unable to find BCPVPD header in \"%s\".\n", + argv[1]); + return 1; + } + + outfd = open(argv[2], O_RDWR | O_TRUNC | O_CREAT, S_IRWXU); + if (outfd == -1) { + fprintf(stderr, "Error: Failed to open \"%s\": %s\n", + argv[2], strerror(errno)); + return 1; + } + + return BCPVPDExtract(InputBuffer + 0x52, InputBufferSize - 0x52, outfd); +} -- cgit v1.2.3