Fix a couple of problems with FreeLDR portability.
[reactos.git] / reactos / boot / freeldr / bootsect / ofwboot.s
index 36e0434..d367382 100644 (file)
@@ -1,41 +1,11 @@
        .section .text
+        .globl  setup_bats
+
 _start:        
        .long   0xe00000 + 12
        .long   0
        .long   0
        
-/*
- * LIFTED FROM arch/macppc/stand/ofwboot/Locore.c
- * Copyright (C) 1995, 1996 Wolfgang Solfrank.
- * Copyright (C) 1995, 1996 TooLs GmbH.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- * 3. All advertising materials mentioning features or use of this software
- *    must display the following acknowledgement:
- *      This product includes software developed by TooLs GmbH.
- * 4. The name of TooLs GmbH may not be used to endorse or promote products
- *    derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES ;  LOSS OF USE, DATA, OR PROFITS;
- * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
- * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
- * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
- * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-       
 _begin:
        sync                    
        isync
@@ -48,23 +18,8 @@ _begin:
        li      %r0,0
        mtmsr   %r0             
        isync                   
-                               
-       mtibatu 0,%r0           
-       mtibatu 1,%r0           
-       mtibatu 2,%r0           
-       mtibatu 3,%r0           
-       mtdbatu 0,%r0           
-       mtdbatu 1,%r0           
-       mtdbatu 2,%r0           
-       mtdbatu 3,%r0           
-       
-       li      %r9,0x12             /* BATL(0, BAT_M, BAT_PP_RW) */
-       mtibatl 0,%r9           
-       mtdbatl 0,%r9           
-       li      %r9,0x1ffe           /* BATU(0, BAT_BL_256M, BAT_Vs) */
-       mtibatu 0,%r9           
-       mtdbatu 0,%r9           
-       isync                   
+
+       bl      setup_bats                                     
 
        li      %r8,0x3030
        mtmsr   %r8
@@ -93,6 +48,8 @@ _begin:
 
        bl      ofw_print_eol
 
+       bl      setup_exc
+
        /* Zero CTR */
        mtcr    %r31
        
@@ -105,7 +62,55 @@ _begin:
        addi    %r3,%r3,call_ofw - _start
        
        b       call_freeldr
-               
+
+/*
+ * lifted from ppc/boot/openfirmware/misc.S
+ * Copyright (C) Paul Mackerras 1997.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation        ;  either version
+ * 2 of the License, or (at your option) any later version.
+ */
+
+/*
+ * Use the BAT2 & 3 registers to map the 1st 16MB of RAM to
+ * the address given as the 1st argument.
+ */
+setup_bats:
+        mfpvr   5
+        rlwinm  5,5,16,16,31            /* r3 = 1 for 601, 4 for 604 */
+        cmpwi   0,5,1
+        li      0,0
+        bne     4f
+        mtibatl 3,0                     /* invalidate BAT first */
+        ori     3,3,4                   /* set up BAT registers for 601 */
+        li      4,0x7f
+        mtibatu 2,3
+        mtibatl 2,4
+        oris    3,3,0x80
+        oris    4,4,0x80
+        mtibatu 3,3
+        mtibatl 3,4
+        b       5f
+4:     mtdbatu 3,0                     /* invalidate BATs first */
+        mtibatu 3,0
+        ori     3,3,0xff                /* set up BAT registers for 604 */
+        li      4,2
+        mtdbatl 2,4
+        mtdbatu 2,3
+        mtibatl 2,4
+        mtibatu 2,3
+        oris    3,3,0x80
+        oris    4,4,0x80
+        mtdbatl 3,4
+        mtdbatu 3,3
+        mtibatl 3,4
+        mtibatu 3,3
+5:     sync
+        isync
+        blr
+       
        .align  4
 call_freeldr:
        /* Get the address of the functions list --
@@ -630,6 +635,53 @@ ofw_finddevice:
        /* Return */
        blr
 
+ofw_open:
+       /* Reserve stack space ...
+        * 20 bytes for the ofw call,
+        * r8, r9, and lr */
+       subi    %r1,%r1,32
+
+       /* Store r8, r9, lr */
+       stw     %r8,20(%r1)
+       stw     %r9,24(%r1)
+       mflr    %r8
+       stw     %r8,28(%r1)
+       
+       /* Get open name */
+       lis     %r8,0xe00000@ha
+       addi    %r9,%r8,ofw_open_name - _start
+       stw     %r9,0(%r1)
+
+       /* 1 Argument and 1 return */
+       li      %r9,1
+       stw     %r9,4(%r1)
+       stw     %r9,8(%r1)
+
+       stw     %r3,12(%r1)
+
+       /* Load up the call address */
+       lwz     %r9,ofw_call_addr - _start(%r8)
+       mtlr    %r9
+
+       /* Set argument */
+       mr      %r3,%r1
+       
+       /* Fire */
+       blrl
+       
+       lwz     %r3,16(%r1)
+       
+       /* Restore registers */
+       lwz     %r8,28(%r1)
+       mtlr    %r8
+       lwz     %r9,24(%r1)
+       lwz     %r8,20(%r1)
+       
+       addi    %r1,%r1,32
+       
+       /* Return */
+       blr
+
 ofw_getprop_hook:
        /* Reserve stack space:
         * 32 bytes for the ofw call
@@ -889,7 +941,224 @@ ofw_exit_loop:
        /* Fire */
        blrl
        /* No return from exit */
+
+ofw_child:
+       /* Reserve stack space ...
+        * 20 bytes for the ofw call,
+        * r8, r9, and lr */
+       subi    %r1,%r1,32
+
+       /* Store r8, r9, lr */
+       stw     %r8,20(%r1)
+       stw     %r9,24(%r1)
+       mflr    %r8
+       stw     %r8,28(%r1)
+       
+       /* Get child name */
+       lis     %r8,0xe00000@ha
+       addi    %r9,%r8,ofw_child_name - _start
+       stw     %r9,0(%r1)
+
+       /* 1 Argument and 1 return */
+       li      %r9,1
+       stw     %r9,4(%r1)
+       stw     %r9,8(%r1)
+
+       stw     %r3,12(%r1)
+
+       /* Load up the call address */
+       lwz     %r9,ofw_call_addr - _start(%r8)
+       mtlr    %r9
+
+       /* Set argument */
+       mr      %r3,%r1
+       
+       /* Fire */
+       blrl
+       
+       lwz     %r3,16(%r1)
+       
+       /* Restore registers */
+       lwz     %r8,28(%r1)
+       mtlr    %r8
+       lwz     %r9,24(%r1)
+       lwz     %r8,20(%r1)
+       
+       addi    %r1,%r1,32
+       
+       /* Return */
+       blr
+       
+ofw_peer:
+       /* Reserve stack space ...
+        * 20 bytes for the ofw call,
+        * r8, r9, and lr */
+       subi    %r1,%r1,32
+
+       /* Store r8, r9, lr */
+       stw     %r8,20(%r1)
+       stw     %r9,24(%r1)
+       mflr    %r8
+       stw     %r8,28(%r1)
+       
+       /* Get peer name */
+       lis     %r8,0xe00000@ha
+       addi    %r9,%r8,ofw_peer_name - _start
+       stw     %r9,0(%r1)
+
+       /* 1 Argument and 1 return */
+       li      %r9,1
+       stw     %r9,4(%r1)
+       stw     %r9,8(%r1)
+
+       stw     %r3,12(%r1)
+
+       /* Load up the call address */
+       lwz     %r9,ofw_call_addr - _start(%r8)
+       mtlr    %r9
+
+       /* Set argument */
+       mr      %r3,%r1
+       
+       /* Fire */
+       blrl
+       
+       lwz     %r3,16(%r1)
+       
+       /* Restore registers */
+       lwz     %r8,28(%r1)
+       mtlr    %r8
+       lwz     %r9,24(%r1)
+       lwz     %r8,20(%r1)
+       
+       addi    %r1,%r1,32
+       
+       /* Return */
+       blr
+                               
+ofw_seek:
+       /* Reserve stack space ...
+        * 20 bytes for the ofw call,
+        * r8, r9, and lr */
+       subi    %r1,%r1,32
+
+       /* Store r8, r9, lr */
+       stw     %r8,20(%r1)
+       stw     %r9,24(%r1)
+       mflr    %r8
+       stw     %r8,28(%r1)
+       
+       /* Get peer name */
+       lis     %r8,0xe00000@ha
+       addi    %r9,%r8,ofw_seek_name - _start
+       stw     %r9,0(%r1)
+
+       /* 3 Arguments and 1 return */
+       li      %r9,3
+       stw     %r9,4(%r1)
+       li      %r9,1
+       stw     %r9,8(%r1)
+
+       stw     %r3,12(%r1)
+       stw     %r4,16(%r1)
+       stw     %r5,20(%r1)
+
+       /* Load up the call address */
+       lwz     %r9,ofw_call_addr - _start(%r8)
+       mtlr    %r9
+
+       /* Set argument */
+       mr      %r3,%r1
+       
+       /* Fire */
+       blrl
+       
+       lwz     %r3,16(%r1)
+       
+       /* Restore registers */
+       lwz     %r8,28(%r1)
+       mtlr    %r8
+       lwz     %r9,24(%r1)
+       lwz     %r8,20(%r1)
+       
+       addi    %r1,%r1,32
+       
+       /* Return */
+       blr
+
+
+setup_exc:
+       subi    %r1,%r1,32
+
+       stw     %r3,0(%r1)
+       mflr    %r3
+       stw     %r3,4(%r1)
+       stw     %r8,8(%r1)
+       stw     %r9,12(%r1)
+       stw     %r10,16(%r1)
+       stw     %r12,20(%r1)
+       
+       lis     %r8,0xe00000@ha
+       xor     %r12,%r12,%r12
+       addi    %r12,%r12,0x300
+       addi    %r9,%r8,dsi_exc - _start
+       addi    %r10,%r8,dsi_end - _start
+
+copy_loop:     
+       cmp     0,0,%r9,%r10
+       beq     ret_setup_exc
+
+       mr      %r3,%r12
+       bl      ofw_print_number
+       bl      ofw_print_space
+       
+       lwz     %r3,0(%r9)
+       stw     %r3,0(%r12)
+
+       bl      ofw_print_number
+       bl      ofw_print_eol
+
+       addi    %r12,%r12,4
+       addi    %r9,%r9,4
+       b       copy_loop
+       
+ret_setup_exc:
+       mfmsr   %r12
+       andi.   %r12,%r12,0xffbf 
+       mtmsr   %r12
+       
+       lwz     %r12,20(%r1)
+       lwz     %r10,16(%r1)
+       lwz     %r9,12(%r1)
+       lwz     %r8,8(%r1)
+
+       lwz     %r3,4(%r1)
+       mtlr    %r3
+       lwz     %r3,0(%r1)
+       
+       blr
+
+dsi_exc:
+       subi    %r1,%r1,16
+       
+       stw     %r0,0(%r1)
+       stw     %r3,4(%r1)
+
+       mfsrr0  %r3
+       addi    %r3,%r3,4
+       mtsrr0  %r3
+
+       /* mfsrr1       %r3 */
+       /* ori  %r3,%r3,2 */
+       /* mtsrr1       %r3 */
                
+       lwz     %r3,4(%r1)
+       lwz     %r0,0(%r1)
+
+       addi    %r1,%r1,16
+       rfi
+dsi_end:       
+                                       
        .org    0x1000
 freeldr_banner:
        .ascii  "ReactOS OpenFirmware Boot Program\r\n\0"
@@ -899,7 +1168,6 @@ freeldr_halt:
 
 freeldr_reg_init:
        .ascii  "r\0"
-
 freeldr_reg_lr:        
        .ascii  "lr \0"
 freeldr_reg_cr:        
@@ -933,6 +1201,18 @@ ofw_read_name:
 ofw_exit_name:
        .ascii  "exit\0"
 
+ofw_open_name:
+       .ascii  "open\0"
+
+ofw_child_name:
+       .ascii  "child\0"
+
+ofw_peer_name:
+       .ascii  "peer\0"
+               
+ofw_seek_name:
+       .ascii  "seek\0"
+       
 ofw_chosen_name:
        .ascii  "/chosen\0"
 
@@ -954,7 +1234,12 @@ ofw_functions:
        .long   ofw_print_regs
        .long   ofw_print_string
        .long   ofw_print_number
+       .long   ofw_open
+       .long   ofw_child
+       .long   ofw_peer
+       .long   ofw_seek
        
        .org    0x2000
 stack:
        .space  0x4000
+