/*****************************************************************************

       Copyright © 1995, 1996 Digital Equipment Corporation,
                       Maynard, Massachusetts.

                        All Rights Reserved

Permission to use, copy, modify, and distribute this software and its 
documentation for any purpose and without fee is hereby granted, provided  
that the copyright notice and this permission notice appear in all copies  
of software and supporting documentation, and that the name of Digital not  
be used in advertising or publicity pertaining to distribution of the software 
without specific, written prior permission. Digital grants this permission 
provided that you prominently mark, as not part of the original, any 
modifications made to this software or documentation.

Digital Equipment Corporation disclaims all warranties and/or guarantees  
with regard to this software, including all implied warranties of fitness for 
a particular purpose and merchantability, and makes no representations 
regarding the use of, or the results of the use of, the software and 
documentation in terms of correctness, accuracy, reliability, currentness or
otherwise; and you rely on the software, documentation and results solely at 
your own risk. 

******************************************************************************/
/*
 *  Entry point for the Milo stub code.  This code does a few initialising
 *  jobs, unzips Milo and then jumps to it.
 *
 *  david.rusling@reo.mts.dec.com
 */
#include <linux/kernel.h>
#include <linux/config.h>
#include <linux/string.h>
#include <linux/mm.h>
#include <linux/kernel_stat.h>
#include <linux/pci.h>
#include <linux/bios32.h>
#include <linux/version.h>

#include <asm/system.h>
#include <asm/io.h>
#include <asm/console.h>
#include <asm/hwrpb.h>
#include <asm/page.h>
#include <asm/pgtable.h>

#include <stdarg.h>

#include "milo.h"
#include "osfboot.h"
#include "impure.h"
#include "uart.h"
#include "fs.h"
#include "zip/gzip.h"
#include "video.h"
#include "smc.h"

#if 0
#define DEBUG_STUB 1
#endif

/*****************************************************************************
 *  Macros.                                                                  *
 *****************************************************************************/
struct hae hae = {
  0, 
  (unsigned long *) HAE_ADDRESS
  };

struct bootvideo *milo_video = NULL;

extern char flash_image[];
extern unsigned int   flash_image_size;

extern char *dest_addr;
extern long bytes_to_copy;

unsigned char *inbuf;
unsigned char *window;
unsigned outcnt;
unsigned insize;
unsigned inptr;
long bytes_out;
int method;

static char *dest_addr;

extern unsigned long long _end ;
char *free_memory = NULL ;
#ifdef MALLOC_DEBUG
void *__kmalloc(unsigned int size, int priority, const char *file, const int line)
#else
void *kmalloc(unsigned int size, int priority)
#endif
{
    char *result;
    
    size += 8 - (size & 0x7);		/* align to an 8 byte boundary */
    result = free_memory ;
    free_memory += size ;

#ifdef DEBUG_STUB
    printk("kmalloc: returning 0x%x\n", result) ;
#endif

    return (void *) (result) ;
}

void free(void *where)
{
}

void video_putchar(unsigned char c)
{
}

void boot_main_cont(void) 
{
}

/*****************************************************************************
 *   stubs to the unzipping code                                             *
 *****************************************************************************/
/*
 * Clear input and output buffers
 */
void clear_bufs(void)
{
	outcnt = 0;
	insize = inptr = 0;
	bytes_out = 0;
}


void
error(char *x)
{
	printk("%s -- halting.\n", x);
	while (1) ;
}


/*
 * Check the magic number of the input file and update ofname if an
 * original name was given and to_stdout is not set.
 * Return the compression method, -1 for error, -2 for warning.
 * Set inptr to the offset of the next byte to be processed.
 * This function may be called repeatedly for an input file consisting
 * of several contiguous gzip'ed members.
 * IN assertions: there is at least one remaining compressed member.
 *   If the member is a zip file, it must be the only one.
 */
static int get_method(void)
{
	unsigned char flags;
	char magic[2]; /* magic header */

	magic[0] = get_byte();
	magic[1] = get_byte();

	method = -1;                 /* unknown yet */
	if (memcmp(magic, GZIP_MAGIC, 2) == 0
	    || memcmp(magic, OLD_GZIP_MAGIC, 2) == 0) {

		method = get_byte();
		flags  = get_byte();
		if ((flags & ENCRYPTED) != 0)
		  error("Input is encrypted\n");
		if ((flags & CONTINUATION) != 0)
		  error("Multi part input\n");
		if ((flags & RESERVED) != 0)
		  error("Input has invalid flags\n");
		get_byte();	/* skip over timestamp */
		get_byte();
		get_byte();
		get_byte();

		get_byte();	/* skip extra flags */
		get_byte();	/* skip OS type */

		if ((flags & EXTRA_FIELD) != 0) {
			unsigned len = get_byte();
			len |= get_byte() << 8;
			while (len--) get_byte();
		}

		/* Get original file name if it was truncated */
		if ((flags & ORIG_NAME) != 0) {
			/* skip name */
			while (get_byte() != 0) /* null */ ;
		}

		/* Discard file comment if any */
		if ((flags & COMMENT) != 0) {
			while (get_byte() != 0) /* null */ ;
		}
	} else {
		error("unknown compression method");
	}
	return method;
}


/*
 * Fill the input buffer and return the first byte in it. This is called
 * only when the buffer is empty and at least one byte is really needed.
 */
static int block_number = 0;
int fill_inbuf(void)
{

	if ((block_number % 5) == 0)
	  printk("#");

	memcpy(inbuf, flash_image + (block_number * INBUFSIZ),
	       INBUFSIZ);
	block_number++;
	insize = INBUFSIZ;

	inptr = 1;
	return inbuf[0];
}


/*
 * Write the output window window[0..outcnt-1] holding uncompressed
 * data and update crc.
 */
void flush_window(void)
{
#ifdef DEBUG_STUB
    printk("flush_window: called, dest_addr = 0x%x\n", dest_addr);
#endif

	if (!outcnt) {
		return;
	}

	bytes_out += outcnt;

	if (dest_addr) {
		/*
		 * Simply copy adjacent to previous block:
		 */
		memcpy((char*)dest_addr, window, outcnt);
		dest_addr += outcnt;
	} 
}

void uncompress_milo(void *where)
{
  printk("Allocating memory for unzip\n");
  inbuf = (unsigned char*) kmalloc(INBUFSIZ, 0);
  window = (unsigned char*) kmalloc(WSIZE, 0);

#ifdef DEBUG_STUB
  printk("inbuf @ 0x%x, window @ 0x%x\n", inbuf, window) ;
#endif

  dest_addr = (char *)where;
  
  clear_bufs();

  method = get_method();
  unzip(0, 0);

  printk("done.\n");
}

/*****************************************************************************
 *   Main entry point for system primary bootstrap code                      *
 *****************************************************************************/
void __main(void) 
{
    void (*milo_address)(void) ;

/*****************************************************************************
 *  Initialize.                                                              *
 *****************************************************************************/
/*
 *  First initialize the Evaulation Board environment.
 */

    /* let's get explicit about some variables we care about. */
    block_number = 0 ;
    milo_address = (void (*)(void))LOADER_AT ;
    free_memory = (char *)0x600000 ;
    milo_video = NULL ;

#ifdef CONFIG_ALPHA_PC164
/*
 *  The PC164 employs the SMC FDC37C93X Ultra I/O Controller.
 *  After reset all of the devices are disabled. SMCInit() will
 *  autodetect the FDC37C93X and enable each of the devices.
 */
    SMCInit();
#endif

    /* we can only output to the serial port *after* we initialise it */
    uart_init();

    /* announce ourselves */
    printk("Milo Stub: V1.1\n") ;

    /* unzip milo into the appropriate place in memory */
    printk("Unzipping Milo into position @ 0x%Lx\n", LOADER_AT) ;
    uncompress_milo((void *)LOADER_AT) ;

    /* now jump to it */
    printk("Jumping to Milo...\n") ;
    (milo_address)() ;
}

