/*
 *               Copyright (C) 2005, BroadOn Communications Corp.
 *
 *  These coded instructions, statements, and computer programs contain
 *  unpublished proprietary information of BroadOn Communications Corp.,
 *  and are protected by Federal copyright law. They may not be disclosed
 *  to third parties or copied or duplicated in any form, in whole or in
 *  part, without the prior written consent of BroadOn Communications Corp.
 *
 */

#include <sc/sc.h>
#include <sc/addr_map.h>

/*
 * Boot1
 *
 * Boot1 is the second stage of boot. Its purpose is to load, decrypt,
 * and verify boot2.
 *
 * The purpose of the assembler portion of the boot1 code is to set the
 * stack pointer and set the memory permissions on the stack. Once the
 * stack has been setup, the assembler code jumps to the C code to
 * performan the rest of boot1. After boot1 is done, the assembler code
 * jumps to boot2.
 *
 */

     .text
     .code 32
     .align 0
     .global _start

#define CACHE_LINE_SIZE     16
#define BOOT1_STACK_SIZE    8192

_start:
        /* Relocate to avoid conflict with the SK */
        mov     r0, pc
        ldr     r1, =BOOT1_SP_START
        cmp     r0, r1
        bhi     copied

        /* Set the boot1 space to read/write */
        ldr     r0, =(DAX_START+BOOT1_SP_START)
        ldr     r1, =(DAX_START+BOOT1_SP_END)
        ldr     r2, =(PT_ACCESS_RW << PT_SC_PROT_SHIFT)
        ldr     r3, =16
        bl      word_set

        /* Copy over the entire boot1 */
        ldr     r0, =(__text_start-BOOT1_SP_START)
        ldr     r1, =(__data_end-BOOT1_SP_START)
        ldr     r2, =BOOT1_SP_START
        ldr     r3, =4
        bl      word_copy

        /* Set the first 256 bytes of boot1 to read/execute */
        ldr     r0, =(DAX_START+BOOT1_SP_START)
        ldr     r1, =(DAX_START+BOOT1_SP_START+256)
        ldr     r2, =(PT_ACCESS_EXEC << PT_SC_PROT_SHIFT)
        ldr     r3, =16
        bl      word_set

        /* Branch to the new location */
        ldr     pc, =BOOT1_SP_START

word_set:
        cmp     r0, r1
        bcs     1f
        str     r2, [r0], r3
        b       word_set
1:
        bx      lr

word_copy:
        cmp     r0, r1
        bcs     2f
        ldr     r4, [r0], r3
        str     r4, [r2], r3
        b       word_copy
2:
        bx      lr

copied:
        /* Make stack read/writable */
        ldr     r0, =DAX_START+BOOT1_SP_END
        ldr     r1, =(PT_ACCESS_RW << PT_SC_PROT_SHIFT)
        ldr     r2, =BOOT1_STACK_SIZE/CACHE_LINE_SIZE
loop:
        subs    r2, r2, #1
        str     r1, [r0, #-CACHE_LINE_SIZE]!
        bne     loop

        /* Mark text segment as read/execute */
        ldr     r0, =(DAX_START+__text_start)
        ldr     r1, =(DAX_START+__text_end)
        ldr     r2, =(PT_ACCESS_EXEC << PT_SC_PROT_SHIFT)
        ldr     r3, =16
        bl      word_set

        /* Mark rodata segment as read-only */
        ldr     r0, =(DAX_START+__rodata_start)
        ldr     r1, =(DAX_START+__rodata_end)
        ldr     r2, =(PT_ACCESS_READ << PT_SC_PROT_SHIFT)
        bl      word_set

        /* Mark data and bss segments as read/write */
        ldr     r0, =(DAX_START+__data_start)
        ldr     r1, =(DAX_START+__data_end)
        ldr     r2, =(PT_ACCESS_RW << PT_SC_PROT_SHIFT)
        bl      word_set
        ldr     r0, =(DAX_START+__bss_start)
        ldr     r1, =(DAX_START+__bss_end)
        ldr     r2, =(PT_ACCESS_RW << PT_SC_PROT_SHIFT)
        bl      word_set

        /* Zero the bss */
        ldr     r0, =__bss_start
        ldr     r1, =__bss_end
        ldr     r2, =0
        ldr     r3, =4
        bl      word_set

        ldr     sp, =BOOT1_SP_END      @ Set the stack pointer
        ldr     r0, =bootc
        mov     lr, pc
        bx      r0                  @ and jump to C initialization code

        mov     pc, #0              @ Jump to the reset vector

    .end

