/*
    start.S, Copyright  (c) by Lukas Ruf, lr@lpr.ch,
    Swiss Federal Institute of Technology,
    Computer Engineering and Networks Laboratory.

    TOPSY -- A Teachable Operating System.
             Implementation of a tiny and simple
             micro kernel for teaching purposes.

    For further information, please visit http://www.tik.ee.ethz.ch/~topsy

    This software is provided under the terms of the GNU General Public Licence.

    A full copy of the GNU GPL is provided in the file COPYING found in the 
    development root of Topsy.
*/
/*
    File:                  $Source: /home/ruf/cvs_Topsy/cvs/Topsy/Startup/ia32/start.S,v $
    Author(s):             Lukas Ruf, lr@lpr.ch
    Affiliation:           ETH Zuerich, TIK
    Version:               $Revision: 3039 $
    Creation Date:         
    Last Date of Change:   $Date: 2007-06-12 21:43:31 +0200 (Die, 12 Jun 2007) $      by: $Author: sryffel $
*/
#include "MemoryLayout.h"
#include "asm.h"
#include "headerLayout.h"

#define TRACKMAX        64512

.extern __printstr

.section .init
.align  4,0x9090
.org    0x00000000

// even if all is already defined in CoreLoad.BIN
// we set the defaults for sure

// Topsyheader
        .org ORG_MARKER
marker:
        .asciz  "TOPSYia32"     // MARKER for PATCHING
        .org ORG_TEXT_SIZE
_textsize:
        .org ORG_DATA_SIZE
_datasize:
        .org ORG_BSS__SIZE
_bsssize:
        .org ORG_PLATFORM_EXTENSIONS
_tracktoread:
			  /* make sure there is enough room for information storage
				 * the Topsy Header is 128 bytes long
			   */
	      .org    ORG_CODE_START    
        
        .align  4,0x9090
        
ASM_PROC_FRAME(__start)
        nop             // make sure, the disassembler is able to synchronize...
        nop             // this is only inserted here, as all disassemblers
        nop             // to not really know about -- how could they... 
        nop             // this would not be used for execution but for readability,
        nop             // I'm still human :-)
        nop
        nop
        nop

        movl    $GC_KDSEL,%eax
        movw    %ax,%ds
        movw    %ax,%es
        movw    %ax,%fs
        movw    %ax,%gs
        movw    %ax,%ss

        movl    $BOOTSTACKTOP,%eax      
        movl    %eax,%esp       

/*
  CONCATENATION: PM, Kernel is executing lowest level of startup
  In Assembler only concatenating of Kernel chunk of bytes is performed.
  All other concatenation is done during Initialization running 
  full C-Code.
  Therefore, this code has to be concatenated... I do not like parsing the
  Code segment. So, bruteforce: Concatenate in Assembler and Execute as normal.
 */
store_patched_values:   
        movl    _tracktoread,%eax       // save in register
        movl    %eax,_ltracks           // save in data segment
        
        movl    _textsize,%eax          // save on stack
        movl    %eax,_ltexts            // save in data segment

        movl    _datasize,%eax          // .... and
        movl    %eax,_ldatas            // save in data segment

// now, try to say hello 
        
        movl    $success,%eax
        pushl   %eax
        movl    $0x1F,%eax
        pushl   %eax
        movl    lsuccOfs,%eax
        pushl   %eax
        call    __printstr
        addl    $12,%esp
        
        movl    $copyrights,%eax
        pushl   %eax
        movl    $0x1F,%eax
        pushl   %eax
        movl    lcopyOfs,%eax
        pushl   %eax
        call    __printstr
        addl    $12,%esp
        
        call    __stop_fd               // Turn off floppy drive

        cli                             // disable all interrupts
        
//-LR       break:
//-LR         jmp break
//-LR         
        jmp     main                    // and now GO !!


//****************************************************************************//
//***  Support Functions to report correct "sizes"                         ***//
//****************************************************************************//
        .section .sasm
        .align  4,0x9090
//ulong __getTextSize();        
// return size of .text as pachted by KernelPatch 
ASM_PROC_FRAME(__getTextSize)
        ENTER
        movl    _ltexts,%eax
        LEAVE

//ulong __getDataSize();        
// return size of .data as pachted by KernelPatch 
ASM_PROC_FRAME(__getDataSize)
        ENTER
        movl    _ldatas,%eax
        LEAVE

//ulong __getKernelSize();      
// return size of .data as pachted by KernelPatch 
ASM_PROC_FRAME(__getNoOfTracks)
        ENTER
        movl    _ltracks,%eax
        LEAVE

//stop floppy disk light :-)    
ASM_PROC_FRAME(__stop_fd)
        pushl   %eax
        pushl   %edx
        movl    $0x3F2,%edx     // Floppy Port
        xorb    %al,%al         // Turn of Floppy
        outb    %al,%dx
        popl    %edx
        popl    %eax
        ret

//****************************************************************************//
//***  Startup Messages (just to say "I'm alive"                           ***//
//****************************************************************************//

//-LR         .data
        /* LR 000712 */
        /* Important: before kernel is concatenated, this data should be 
         * accessible to show functionality.  That's why it is located in the
         * init-section.  So, the location within the first memory block is
         * guaranteed.
         */
        .section .init
        .align  4,0x9090
success:
        .asciz  " => successfully completed."
        
.globl copyrights
copyrights:
//-LR         .asciz  "==> Topsy i386, Kernel Startup 0.113 Lukas Ruf June 1998 <=="
        .asciz  "==> Topsy i386, Kernel Startup 0.114 Lukas Ruf July 2000 <=="

.globl __MARKER
__MARKER:
        .asciz "TOPSYi386"

.globl __MARKER_fail
__MARKER_fail:
        .asciz "Marker not found!"
        
lsuccOfs:
        .long   0x00000D56 + 160

lcopyOfs:
        .long   0x00000DC0 + 160

_ltexts:
        .long   0x00000000
_ldatas:
        .long   0X00000000
_ltracks:
        .long   0X00000000
        

