mirror of
				https://github.com/FDOS/kernel.git
				synced 2025-11-04 05:05:11 +01:00 
			
		
		
		
	Use dos/sys compression if the uncompressed kernel is less than 64K. That
makes the compressed kernel about 2K smaller. git-svn-id: https://svn.code.sf.net/p/freedos/svn/kernel/trunk@1329 6ac86273-5f31-0410-b378-82cca8765d1b
This commit is contained in:
		
							parent
							
								
									ad58440395
								
							
						
					
					
						commit
						29d6eb79f1
					
				@ -38,9 +38,10 @@ production:     ..\bin\$(TARGET).sys
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
kernel.sys:	kernel.exe ..\utils\exeflat.exe
 | 
					kernel.sys:	kernel.exe ..\utils\exeflat.exe
 | 
				
			||||||
		..\utils\exeflat kernel.exe kernelf.exe 0x60 -S0x10 -S0x74 -E
 | 
							..\utils\exeflat kernel.exe kernelf.exe 0x60 -S0x10 -S0x74 -E
 | 
				
			||||||
		$(XUPX) kernelf.exe
 | 
							if exist kernelf.exe $(XUPX) kernelf.exe
 | 
				
			||||||
 | 
							if exist kernelf.sys $(XUPX) kernelf.sys
 | 
				
			||||||
		..\utils\exeflat kernelf.exe kernel.sys 0x60 $(UPXOPT)
 | 
							..\utils\exeflat kernelf.exe kernel.sys 0x60 $(UPXOPT)
 | 
				
			||||||
		-$(RM) kernelf.exe
 | 
							-$(RM) kernelf.exe kernelf.sys
 | 
				
			||||||
 | 
					
 | 
				
			||||||
kernel.exe:	$(TARGET).lnk $(OBJS) $(LIBS)
 | 
					kernel.exe:	$(TARGET).lnk $(OBJS) $(LIBS)
 | 
				
			||||||
		$(LINK) @$(TARGET).lnk;
 | 
							$(LINK) @$(TARGET).lnk;
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										123
									
								
								utils/exeflat.c
									
									
									
									
									
								
							
							
						
						
									
										123
									
								
								utils/exeflat.c
									
									
									
									
									
								
							@ -48,10 +48,13 @@ large portions copied from task.c
 | 
				
			|||||||
#include "exe.h"
 | 
					#include "exe.h"
 | 
				
			||||||
#include <stdio.h>
 | 
					#include <stdio.h>
 | 
				
			||||||
#include <stdlib.h>
 | 
					#include <stdlib.h>
 | 
				
			||||||
 | 
					#include <string.h>
 | 
				
			||||||
#include <ctype.h>
 | 
					#include <ctype.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#define BUFSIZE 32768u
 | 
					#define BUFSIZE 32768u
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#define KERNEL_START 0x10 /* the kernel code really starts here at 60:10 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
typedef struct {
 | 
					typedef struct {
 | 
				
			||||||
  UWORD off, seg;
 | 
					  UWORD off, seg;
 | 
				
			||||||
} farptr;
 | 
					} farptr;
 | 
				
			||||||
@ -82,7 +85,7 @@ void usage(void)
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
int main(int argc, char **argv)
 | 
					int main(int argc, char **argv)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
  exe_header header;
 | 
					  static exe_header header; /* must be initialized to zero */
 | 
				
			||||||
  int i, j;
 | 
					  int i, j;
 | 
				
			||||||
  size_t bufsize;
 | 
					  size_t bufsize;
 | 
				
			||||||
  farptr *reloc;
 | 
					  farptr *reloc;
 | 
				
			||||||
@ -92,7 +95,7 @@ int main(int argc, char **argv)
 | 
				
			|||||||
  UBYTE **curbuf;
 | 
					  UBYTE **curbuf;
 | 
				
			||||||
  FILE *src, *dest;
 | 
					  FILE *src, *dest;
 | 
				
			||||||
  short silentSegments[20], silentcount = 0, silentdone = 0;
 | 
					  short silentSegments[20], silentcount = 0, silentdone = 0;
 | 
				
			||||||
  int UPX = FALSE, flat_exe = FALSE;
 | 
					  int UPX = FALSE, flat_exe = FALSE, compress_sys_file = FALSE;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /* if no arguments provided, show usage and exit */
 | 
					  /* if no arguments provided, show usage and exit */
 | 
				
			||||||
  if (argc < 4) usage();
 | 
					  if (argc < 4) usage();
 | 
				
			||||||
@ -136,37 +139,45 @@ int main(int argc, char **argv)
 | 
				
			|||||||
  
 | 
					  
 | 
				
			||||||
  if ((src = fopen(argv[1], "rb")) == NULL)
 | 
					  if ((src = fopen(argv[1], "rb")) == NULL)
 | 
				
			||||||
  {
 | 
					  {
 | 
				
			||||||
    printf("Source file %s could not be opened\n", argv[1]);
 | 
					    if (UPX && strlen(argv[1]) > 3)
 | 
				
			||||||
    return 1;
 | 
					    {
 | 
				
			||||||
 | 
					      strcpy(argv[1] + strlen(argv[1]) - 3, "sys");
 | 
				
			||||||
 | 
					      if ((src = fopen(argv[1], "rb")) == NULL)
 | 
				
			||||||
 | 
					      {
 | 
				
			||||||
 | 
					        printf("Source file %s could not be opened\n", argv[1]);
 | 
				
			||||||
 | 
					        return 1;
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					      compress_sys_file = TRUE;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
  if (fread(&header, sizeof(header), 1, src) != 1)
 | 
					  if (compress_sys_file) {
 | 
				
			||||||
  {
 | 
					    fseek(src, 0, SEEK_END);
 | 
				
			||||||
    printf("Error reading header from %s\n", argv[1]);
 | 
					    size = ftell(src);
 | 
				
			||||||
    fclose(src);
 | 
					  } else {
 | 
				
			||||||
    return 1;
 | 
					    if (fread(&header, sizeof(header), 1, src) != 1)
 | 
				
			||||||
  }
 | 
					    {
 | 
				
			||||||
  if (header.exSignature != MAGIC)
 | 
					      printf("Error reading header from %s\n", argv[1]);
 | 
				
			||||||
  {
 | 
					      fclose(src);
 | 
				
			||||||
    printf("Source file %s is not a valid .EXE\n", argv[1]);
 | 
					      return 1;
 | 
				
			||||||
    fclose(src);
 | 
					    }
 | 
				
			||||||
    return 1;
 | 
					    if (header.exSignature != MAGIC)
 | 
				
			||||||
  }
 | 
					    {
 | 
				
			||||||
  if ((dest = fopen(argv[2], "wb+")) == NULL)
 | 
					      printf("Source file %s is not a valid .EXE\n", argv[1]);
 | 
				
			||||||
  {
 | 
					      fclose(src);
 | 
				
			||||||
    printf("Destination file %s could not be created\n", argv[2]);
 | 
					      return 1;
 | 
				
			||||||
    return 1;
 | 
					    }
 | 
				
			||||||
  }
 | 
					    start_seg = (UWORD)strtol(argv[3], NULL, 0);
 | 
				
			||||||
  start_seg = (UWORD)strtol(argv[3], NULL, 0);
 | 
					    if (header.exExtraBytes == 0)
 | 
				
			||||||
  if (header.exExtraBytes == 0)
 | 
					      header.exExtraBytes = 0x200;
 | 
				
			||||||
    header.exExtraBytes = 0x200;
 | 
					    printf("header len = %lu = 0x%lx\n", header.exHeaderSize * 16UL,
 | 
				
			||||||
  printf("header len = %lu = 0x%lx\n", header.exHeaderSize * 16UL,
 | 
					           header.exHeaderSize * 16UL);
 | 
				
			||||||
         header.exHeaderSize * 16UL);
 | 
					    size =
 | 
				
			||||||
  size =
 | 
					 | 
				
			||||||
      ((DWORD) (header.exPages - 1) << 9) + header.exExtraBytes -
 | 
					      ((DWORD) (header.exPages - 1) << 9) + header.exExtraBytes -
 | 
				
			||||||
      header.exHeaderSize * 16UL;
 | 
					      header.exHeaderSize * 16UL;
 | 
				
			||||||
  printf("image size (less header) = %lu = 0x%lx\n", size, size);
 | 
					    printf("image size (less header) = %lu = 0x%lx\n", size, size);
 | 
				
			||||||
  printf("first relocation offset = %u = 0x%u\n", header.exOverlay,
 | 
					    printf("first relocation offset = %u = 0x%u\n", header.exOverlay,
 | 
				
			||||||
         header.exOverlay);
 | 
					           header.exOverlay);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /* first read file into memory chunks */
 | 
					  /* first read file into memory chunks */
 | 
				
			||||||
  fseek(src, header.exHeaderSize * 16UL, SEEK_SET);
 | 
					  fseek(src, header.exHeaderSize * 16UL, SEEK_SET);
 | 
				
			||||||
@ -194,18 +205,21 @@ int main(int argc, char **argv)
 | 
				
			|||||||
      return 1;
 | 
					      return 1;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
  fseek(src, header.exRelocTable, SEEK_SET);
 | 
					  if (header.exRelocTable && header.exRelocItems)
 | 
				
			||||||
  reloc = malloc(header.exRelocItems * sizeof(farptr));
 | 
					 | 
				
			||||||
  if (reloc == NULL)
 | 
					 | 
				
			||||||
  {
 | 
					  {
 | 
				
			||||||
    printf("Allocation error\n");
 | 
					    fseek(src, header.exRelocTable, SEEK_SET);
 | 
				
			||||||
    return 1;
 | 
					    reloc = malloc(header.exRelocItems * sizeof(farptr));
 | 
				
			||||||
  }
 | 
					    if (reloc == NULL)
 | 
				
			||||||
  if (fread(reloc, sizeof(farptr), header.exRelocItems, src) !=
 | 
					    {
 | 
				
			||||||
      header.exRelocItems)
 | 
					      printf("Allocation error\n");
 | 
				
			||||||
  {
 | 
					      return 1;
 | 
				
			||||||
    printf("Source file read error\n");
 | 
					    }
 | 
				
			||||||
    return 1;
 | 
					    if (fread(reloc, sizeof(farptr), header.exRelocItems, src) !=
 | 
				
			||||||
 | 
					        header.exRelocItems)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					      printf("Source file read error\n");
 | 
				
			||||||
 | 
					      return 1;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
  fclose(src);
 | 
					  fclose(src);
 | 
				
			||||||
  qsort(reloc, header.exRelocItems, sizeof(reloc[0]), compReloc);
 | 
					  qsort(reloc, header.exRelocItems, sizeof(reloc[0]), compReloc);
 | 
				
			||||||
@ -233,6 +247,17 @@ int main(int argc, char **argv)
 | 
				
			|||||||
    *spot1 = segment >> 8;
 | 
					    *spot1 = segment >> 8;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  if (flat_exe) {
 | 
				
			||||||
 | 
					    compress_sys_file = size < 0x10000L;
 | 
				
			||||||
 | 
					    if (compress_sys_file && strlen(argv[2]) > 3)
 | 
				
			||||||
 | 
					      strcpy(argv[2] + strlen(argv[2]) - 3, "sys");
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  if ((dest = fopen(argv[2], "wb+")) == NULL)
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    printf("Destination file %s could not be created\n", argv[2]);
 | 
				
			||||||
 | 
					    return 1;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
  if (UPX)
 | 
					  if (UPX)
 | 
				
			||||||
  {
 | 
					  {
 | 
				
			||||||
    /* UPX HEADER jump $+2+size */
 | 
					    /* UPX HEADER jump $+2+size */
 | 
				
			||||||
@ -268,7 +293,7 @@ int main(int argc, char **argv)
 | 
				
			|||||||
    fwrite(JumpBehindCode, 1, 0x20, dest);
 | 
					    fwrite(JumpBehindCode, 1, 0x20, dest);
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  if (flat_exe) {
 | 
					  if (flat_exe && !compress_sys_file) {
 | 
				
			||||||
    /* write header without relocations to file */
 | 
					    /* write header without relocations to file */
 | 
				
			||||||
    exe_header nheader = header;
 | 
					    exe_header nheader = header;
 | 
				
			||||||
    nheader.exRelocItems = 0;
 | 
					    nheader.exRelocItems = 0;
 | 
				
			||||||
@ -335,8 +360,22 @@ int main(int argc, char **argv)
 | 
				
			|||||||
    *(short *)&trailer[1] = (short)size + 0x20;
 | 
					    *(short *)&trailer[1] = (short)size + 0x20;
 | 
				
			||||||
    *(short *)&trailer[23] = header.exInitSS;
 | 
					    *(short *)&trailer[23] = header.exInitSS;
 | 
				
			||||||
    *(short *)&trailer[28] = header.exInitSP;
 | 
					    *(short *)&trailer[28] = header.exInitSP;
 | 
				
			||||||
 | 
					    if (compress_sys_file)
 | 
				
			||||||
 | 
					      /* replace by jmp word ptr [6]: ff 26 06 00
 | 
				
			||||||
 | 
					         (the .SYS strategy handler which will unpack) */
 | 
				
			||||||
 | 
					      *(long *)&trailer[30] = 0x000626ffL;
 | 
				
			||||||
    fwrite(trailer, 1, sizeof trailer, dest);
 | 
					    fwrite(trailer, 1, sizeof trailer, dest);
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					  if (flat_exe && compress_sys_file) {
 | 
				
			||||||
 | 
					    /* overwrite first 8 bytes with SYS header */
 | 
				
			||||||
 | 
					    UWORD dhdr[4];
 | 
				
			||||||
 | 
					    fseek(dest, 0, SEEK_SET);
 | 
				
			||||||
 | 
					    for (i = 0; i < 3; i++)
 | 
				
			||||||
 | 
					      dhdr[i] = 0xffff;
 | 
				
			||||||
 | 
					    /* strategy will jump to us, interrupt never called */
 | 
				
			||||||
 | 
					    dhdr[3] = KERNEL_START;
 | 
				
			||||||
 | 
					    fwrite(dhdr, sizeof(dhdr), 1, dest);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
  fclose(dest);
 | 
					  fclose(dest);
 | 
				
			||||||
  printf("\nProcessed %d relocations, %d not shown\n",
 | 
					  printf("\nProcessed %d relocations, %d not shown\n",
 | 
				
			||||||
         header.exRelocItems, silentdone);
 | 
					         header.exRelocItems, silentdone);
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user