/*
 *               Copyright (C) 2006, 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 <ioslibc.h>
#include <iop.h>
#include <sys.h>
#include <boot.h>
#include <addr_map.h>

void
relocateImg(u32 dst, u32 ldr) 
{
    u32 src = ldr - sizeof(BootOSImageHeader);
    BootOSImageHeader   *hdr = (BootOSImageHeader *) src;
    void(*loader)(void) = (void (*)(void)) (OSLAUNCH_SP_START+ hdr->hdrSize);
    u32 imgSize = hdr->hdrSize + hdr->loaderSize + hdr->imageSize;

    /* Move coarse-grained boundary to include new relaunch space + stack. */
    setupDDR_PPCCGBoundary(OSLAUNCH_SP_START - 4096, DDR_BASE + (DDR_ADDRSPACE_SIZE - 1));
    /* Relocate */
    memcpy((char *)dst, (char *)src, imgSize);
    /* Erase old image */
    memset((u8 *)(src + hdr->loaderSize + hdr->hdrSize), 0, hdr->imageSize);
    /* Run relocated loader */
    loader();
}
