/** @file
  This driver produces Print9 protocol layered on top of the PrintLib from the MdePkg.

Copyright (c) 2009, Intel Corporation. All rights reserved.<BR>
This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution.  The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php

THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.

**/

#include <PiDxe.h>
#include <Library/UefiLib.h>

#include <Library/PrintLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/DebugLib.h>
#include <Library/UefiDriverEntryPoint.h>
#include <Library/MemoryAllocationLib.h>
#include "Print9.h"

#include <Protocol/Cpu.h>
#include <Library/CpuLib.h>

extern EFI_SYSTEM_TABLE         *gST;

EFI_GUID gEfiPrint9ProtocolGuid =
                { 0xf05976ef, 0x83f1, 0x4f3d, 
                  { 0x86, 0x19, 0xf7, 0x59,0x5d, 0x41, 0xe5, 0x61 } };

EFI_GUID gEfiCpuArchProtocolGuid = 
                { 0x26BACCB1, 0x6F42, 0x11D4, 
                  { 0xBC, 0xE7, 0x00, 0x80, 0xC7, 0x3C, 0x88, 0x81 }};

EFI_PRINT9_PRIVATE_DATA         *Image;
EFI_HANDLE  mPrintThunkHandle   = NULL;

//Copied from \MdeModulePkg\Library\DxePrintLibPrint2Protocol\PrintLib.c
UINTN
EFIAPI
MyPrint ()
{
  CHAR16  *Buffer=L"1 2 3 4 5 6 7 8 9 0 A B C E D F ";
  
  UnicodeSPrint(Buffer,16,L"%d\r\n",Image->Var2);
  gST->ConOut->OutputString(gST->ConOut,Buffer); 
  
  return 0;
}

VOID
EFIAPI
MyInterruptHandler (
  IN EFI_EXCEPTION_TYPE   InterruptType,
  IN EFI_SYSTEM_CONTEXT   SystemContext
  )
{
       Image->Var2++; 
}  
/**
  The user Entry Point for Print module.

  This is the entry point for Print DXE Driver. It installs the Print2 Protocol.

  @param[in] ImageHandle    The firmware allocated handle for the EFI image.
  @param[in] SystemTable    A pointer to the EFI System Table.

  @retval EFI_SUCCESS       The entry point is executed successfully.
  @retval Others            Some error occurs when executing this entry point.

**/
EFI_STATUS
EFIAPI
PrintEntryPoint (
  IN EFI_HANDLE           ImageHandle,
  IN EFI_SYSTEM_TABLE     *SystemTable
  )
{
        EFI_STATUS              Status;
        EFI_CPU_ARCH_PROTOCOL  *Cpu;
        
        //
        // Allocate a new image structure
        //
        Image = AllocateZeroPool (sizeof(EFI_PRINT9_PRIVATE_DATA));
        if (Image == NULL) {
                Status = EFI_OUT_OF_RESOURCES;
                ASSERT_EFI_ERROR (Status);
        }

        Image->Signature         = PRINT9_PRIVATE_DATA_SIGNATURE;
  
        Image->PRINT9.UnicodeBSPrint=UnicodeBSPrint;
        Image->PRINT9.UnicodeSPrint=UnicodeSPrint;
        Image->PRINT9.UnicodeBSPrintAsciiFormat=UnicodeBSPrintAsciiFormat;
        Image->PRINT9.UnicodeSPrintAsciiFormat=MyPrint;
        //Image->PRINT9.UnicodeValueToString=UnicodeValueToString;
        Image->PRINT9.AsciiBSPrint=AsciiBSPrint;
        Image->PRINT9.AsciiSPrint=AsciiSPrint;        
        Image->PRINT9.AsciiBSPrintUnicodeFormat=AsciiBSPrintUnicodeFormat;
        Image->PRINT9.AsciiSPrintUnicodeFormat=AsciiSPrintUnicodeFormat;
        //Image->PRINT9.AsciiValueToString=AsciiValueToString;

        Image->Var2=1984;
        
        Status = gBS->InstallMultipleProtocolInterfaces (
                  &mPrintThunkHandle,
                  &gEfiPrint9ProtocolGuid, 
                  &Image->PRINT9,
                  NULL
                );
        ASSERT_EFI_ERROR (Status);

        //
        // Locate the Cpu Arch Protocol.
        //
        Status = gBS->LocateProtocol (&gEfiCpuArchProtocolGuid, NULL, &Cpu);
        ASSERT_EFI_ERROR (Status);

        Status = Cpu->RegisterInterruptHandler (Cpu, 0x41, MyInterruptHandler);
        ASSERT_EFI_ERROR (Status);
        
  return Status;
}
