
#include <stdlib.h>
#include <stdio.h>

void loadelf(char *filename, void *buf)
{
    FILE *f;
    void *bufp;
    f = fopen(filename, "rb");
    bufp = buf;
    for (;;) {
	int count = fread(bufp, 1, 8192, f);
	if (count == 0)
	    break;
	if (feof(f))
	    break;
	bufp += count;
    }
    fclose(f);
}

int main(int argc, void* argv)
{

void* buf, *tempbuf;
FILE *f;
int codesize, datasize, bsssize, count, j, i;

    printf("Kernel ELF strip utility for Corel Netwinder...\n");

    buf = malloc (12*1024*1024);	//allocate 1.2 megs

    loadelf("vmlinux",buf);
    f = fopen("vm.elf", "wb");	//create empty file

    codesize = *(unsigned int*)((char*)(buf+0x44));
    datasize = *(unsigned int*)((char*)(buf+0x64));
    bsssize = *(unsigned int*)((char*)(buf+0x68));

    printf("Text = 0x%X bytes, data = 0x%X bytes, bss = 0x%X.\n",
	codesize, datasize, bsssize);

    tempbuf = malloc (0x8000);

    for (i=0; i<0x8000; i++)
	*(unsigned char *)(tempbuf+i) = 0;


    codesize -= 0x4000;//16k header - will load at C000, not at 8000
    count = fwrite((char*)(buf+0x4000), 1, codesize, f);

    count = fwrite((char*)(tempbuf), 1, 0x8000, f);

    count = fwrite((char*)(buf + 0x4000 + codesize), 1, datasize, f);

    j = (bsssize)/(0x8000) +1;

    for (i=0; i<j; i++)
        count = fwrite((char*)(tempbuf), 1, 0x8000, f);

    fclose(f);

    free (tempbuf);
    free (buf);
    exit(0);
}

