/*
    File        : blackhole.c
    Date        : 19-Sep-02
    Description : Automatically patch the Singularity mode of BlackHole for the
                  PsiFS module.

    Copyright  1998, 1999, 2000, 2001, 2002 Alexander Thoukydides

    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.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/

// Include header file for this module
#include "blackhole.h"

// Include clib header files
#include <stdio.h>

// Include oslib header files
#include "oslib/fileswitch.h"
#include "oslib/osmodule.h"

// Include project header files
#include "debug.h"
#include "err.h"
#include "psifs.h"

// Name of the BlackHole module
#define BLACKHOLE_MODULE "BlackHole"

// Has the patch been applied
static bool blackhole_patched = FALSE;

/*
    Parameters  : start         - The start address of the module.
                  end           - The first byte after the end of the module.
                  patch         - Variable to receive the address of the
                                  instruction to patch, or NULL if no patch is
                                  required.
    Returns     : os_error *    - Pointer to a corresponding error block, or
                                  NULL if no error.
    Description : Patch the BlackHole module at the specified address.
*/
os_error *blackhole_search(bits *start, bits *end, bits **patch)
{
    os_error *err = NULL;

    // Just test the single instruction to be patched
    *patch = start[0x324 / 4] == 0xe20830ff ? start + (0x324 / 4) : NULL;

    // Return any error produced
    return err;
}

/*
    Parameters  : patch         - Pointer to the instruction to patch.
    Returns     : os_error *    - Pointer to a corresponding error block, or
                                  NULL if no error.
    Description : Patch the BlackHole module at the specified address.
*/
os_error *blackhole_patch(bits *patch)
{
    os_error *err = NULL;
    typedef struct
    {
        bits original;
        bits teq;
        bits moveq;
        bits movs;
    } code_block;
    code_block *code;

    DEBUG_PRINTF(("Applying BlackHole patch"))

    // Allocate a block of memory (in the RMA) for the extra code
    err = xosmodule_alloc(sizeof(code_block), (void **) &code);
    if (!err && !code) err = &err_buffer;

    // Patch the module if successful
    if (!err)
    {
        // Record the patch
        blackhole_patched = TRUE;

        // AND r3, r8, #&ff
        code->original = 0xe20830ff;

        // TEQ r3, #psifs_FS_NUMBER_PSIFS
        code->teq = 0xe3330000 | psifs_FS_NUMBER_PSIFS;

        // MOVEQ r3, #FileSwitch_FSNumberDEVICEFS
        code->moveq = 0x03a03000 | fileswitch_FS_NUMBER_DEVICEFS;

        // MOVS pc, r14
        code->movs = 0xe1b0f00e;

        // Modify the module to use the new code
        *patch = 0xeb000000 | (((int) (((bits *) code) - patch) - 2) & 0xffffff);

        // Synchronize StrongARM caches
        xos_synchronise_code_areas(1, patch, patch);
        xos_synchronise_code_areas(1, code, ((byte *) code) + sizeof(code) - 4);
    }

    // Return any error produced
    return err;
}

/*
    Parameters  : void
    Returns     : os_error *    - Pointer to a corresponding error block, or
                                  NULL if no error.
    Description : Check whether a copy of BlackHole is loaded, and patch it if
                  appropriate.
*/
os_error *blackhole_check(void)
{
    os_error *err = NULL;
    byte *module;

    DEBUG_PRINTF(("Checking for BlackHole"))

    // Attempt to find the module
    if (!xosmodule_lookup(BLACKHOLE_MODULE, NULL, NULL, &module, NULL, NULL)
        && module)
    {
        bits *patch = NULL;

        // Check the module data
        err = blackhole_search((bits *) module,
                               (bits *) (module + ((bits *) module)[-1]),
                               &patch);

        // Patch the module if necessary
        if (!err && patch) err = blackhole_patch(patch);
    }

    // Return any error produced
    return err;
}

/*
    Parameters  : void
    Returns     : os_error *    - Pointer to a corresponding error block, or
                                  NULL if no error.
    Description : Display the current status of the BlackHole patch.
*/
os_error *blackhole_status(void)
{
    os_error *err = NULL;

    DEBUG_PRINTF(("Displaying BlackHole patch status"))

    // No action unless the patch applied
    if (blackhole_patched)
    {
        printf("Singularity mode of %s module patched.\n", BLACKHOLE_MODULE);
    }

    // Return any error produced
    return err;
}
