From 8d637d3cd473f4af939753f644de145ea47db2f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C3=ADas=20Israelson?= <57065102+israpps@users.noreply.github.com> Date: Tue, 16 Apr 2024 16:20:37 -0300 Subject: [PATCH] add HDDLOAD module Co-Authored-By: Liu Woon Yung --- iop/hdd/Makefile | 3 +- iop/hdd/hddload/Makefile | 15 ++ iop/hdd/hddload/src/hddload.c | 248 ++++++++++++++++++++++++++++++ iop/hdd/hddload/src/imports.lst | 52 +++++++ iop/hdd/hddload/src/irx_imports.h | 19 +++ 5 files changed, 336 insertions(+), 1 deletion(-) create mode 100644 iop/hdd/hddload/Makefile create mode 100644 iop/hdd/hddload/src/hddload.c create mode 100644 iop/hdd/hddload/src/imports.lst create mode 100644 iop/hdd/hddload/src/irx_imports.h diff --git a/iop/hdd/Makefile b/iop/hdd/Makefile index 118fccff972..f36aadca504 100644 --- a/iop/hdd/Makefile +++ b/iop/hdd/Makefile @@ -19,7 +19,8 @@ SUBDIRS = \ hdck \ hdsk \ fsck \ - fssk + fssk \ + hddload include $(PS2SDKSRC)/Defs.make include $(PS2SDKSRC)/Rules.make diff --git a/iop/hdd/hddload/Makefile b/iop/hdd/hddload/Makefile new file mode 100644 index 00000000000..154daf57c22 --- /dev/null +++ b/iop/hdd/hddload/Makefile @@ -0,0 +1,15 @@ +# _____ ___ ____ ___ ____ +# ____| | ____| | | |____| +# | ___| |____ ___| ____| | \ PS2DEV Open Source Project. +#----------------------------------------------------------------------- +# Copyright 2001-2024, ps2dev - http://www.ps2dev.org +# Licenced under Academic Free License version 2.0 +# Review ps2sdk README & LICENSE files for further details. + +IOP_BIN ?= hddload.irx +IOP_OBJS = hddload.o imports.o exports.o + +include $(PS2SDKSRC)/Defs.make +include $(PS2SDKSRC)/iop/Rules.bin.make +include $(PS2SDKSRC)/iop/Rules.make +include $(PS2SDKSRC)/iop/Rules.release \ No newline at end of file diff --git a/iop/hdd/hddload/src/hddload.c b/iop/hdd/hddload/src/hddload.c new file mode 100644 index 00000000000..1ee21c85e15 --- /dev/null +++ b/iop/hdd/hddload/src/hddload.c @@ -0,0 +1,248 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static int DMATxBuffer[4]; +static int DMATransferID; +static u32 OSDAddr; +static u32 OSDStatAddr; +static u32 SectorBuffer[128]; + +//Function prototypes +static void SendResultCode(int result); +static void HDDBootError(int result); +static void FinishHDDLoad(int result); +static int ReadMBR(unsigned char *buffer, unsigned int lba, unsigned int count); +static void HDDLOADThread(void *arg); + +int _start(int argc, char *argv[]) +{ + int i, result, ThreadID; + iop_thread_t ThreadData; + + FlushDcache(); + OSDStatAddr = 0; + OSDAddr = 0; + + if(argc>0) + { + i=0; + + do{ + printf("arg %d %s %s\n", i, argv[0], argv[1]); + + if(strcmp(argv[0], "-osd") == 0) + { + i++; + argv++; + OSDAddr=strtol(*argv, NULL, 0); + printf("osd addr %lx\n", OSDAddr); + } + else if(strcmp(argv[0], "-stat") == 0) + { + i++; + argv++; + OSDStatAddr=strtol(*argv, NULL, 0); + printf("stat addr %lx\n", OSDStatAddr); + } + + i++; + argv++; + }while(i 0) + { + StartThread(ThreadID, NULL); + result = MODULE_RESIDENT_END; + } + else + { + dev9Shutdown(); + result = MODULE_NO_RESIDENT_END; + } + } + + return result; +} + +static void SendResultCode(int result) +{ + int OldState; + SifDmaTransfer_t dmat; + + while(sceSifDmaStat(DMATransferID) >= 0){}; + + DMATxBuffer[0] = result; + + dmat.src=DMATxBuffer; + dmat.size=0x10; + dmat.attr=0; + dmat.dest=(void*)OSDStatAddr; + + CpuSuspendIntr(&OldState); + DMATransferID = sceSifSetDma(&dmat, 1); + CpuResumeIntr(OldState); +} + +static void HDDBootError(int result) +{ + dev9Shutdown(); + SendResultCode(result); + SleepThread(); +} + +static void FinishHDDLoad(int result) +{ + u8 value; + + value=*(*(vu8 **)0x000003c0); //Access the system configuration storage, which is set up by EECONF. + + if(!(value&2)) dev9Shutdown(); //If HDD support is disabled, switch off the DEV9 interface. + SendResultCode(result); + SleepThread(); +} + +static int ReadMBR(unsigned char *buffer, unsigned int lba, unsigned int count) +{ + int NumSectorsToRead, BufferOffset; + + BufferOffset=0; + while(count>0) + { + NumSectorsToRead=(count<0x81)?count:0x80; + + if(ata_device_sector_io(0, buffer+BufferOffset, lba, NumSectorsToRead, ATA_DIR_READ)!=0) + { + printf("cannot read sector 0\n"); + return -1; + } + + count-=NumSectorsToRead; + lba+=NumSectorsToRead; + buffer+=(NumSectorsToRead * 512); + } + + return 0; +} + +static void HDDLOADThread(void *arg) +{ + ata_devinfo_t *devinfo; + int i, OldState, BufferSize; + u32 stat; + u8 iLinkID[0x20]; + u8 *buffer; + void *DecryptedKELF; + + printf("HDL:hdd load thread start.(01/06/10)\n"); + + SendResultCode(0); + + printf("HDL:init ata\n"); + if((devinfo = ata_get_devinfo(0)) == NULL){ + printf("HDL:ATA initialization failed.\n"); + HDDBootError(-1); + } + + for(i=0x1F; i>=0; i--) iLinkID[i] = 0; + + sceCdRI(iLinkID, &stat); + + if(ata_device_sce_sec_unlock(0, iLinkID) != 0) + { + printf("cannot unlock password\n"); + HDDBootError(-2); + } + + printf("HDL:read 0 sec\n"); + if(ata_device_sector_io(0, SectorBuffer, 0, 1, ATA_DIR_READ) != 0) + { + printf("cannot read sector 0\n"); + HDDBootError(-3); + } + + printf("HDL:osdstart %lx osdsize %lx\n", SectorBuffer[76], SectorBuffer[77]); + + BufferSize=SectorBuffer[77]<<9; + + CpuSuspendIntr(&OldState); + buffer=AllocSysMemory(ALLOC_LAST, BufferSize, NULL); + CpuResumeIntr(OldState); + + if(buffer==NULL) + { + printf("cannot alloc memory\n"); + HDDBootError(-6); + } + + if(ReadMBR(buffer, SectorBuffer[76], SectorBuffer[77]) < 0) + { + printf("cannot read data \n"); + CpuSuspendIntr(&OldState); + FreeSysMemory(buffer); + CpuResumeIntr(OldState); + HDDBootError(-4); + } + + if((buffer[0] == 1) && (buffer[3] & 0x04)) + { + printf("HDL:securiyt decript.\n"); + + if((DecryptedKELF=SecrDiskBootFile(buffer))==NULL) + { + printf("cannot secur data \n"); + CpuSuspendIntr(&OldState); + FreeSysMemory(buffer); + CpuResumeIntr(OldState); + HDDBootError(-5); + } + + SifDmaTransfer_t dmat; + + dmat.src=DecryptedKELF; + dmat.size=BufferSize; + dmat.attr=0; + dmat.dest=(void*)OSDAddr; + + CpuSuspendIntr(&OldState); + sceSifSetDma(&dmat, 1); + CpuResumeIntr(OldState); + + CpuSuspendIntr(&OldState); + FreeSysMemory(buffer); + CpuResumeIntr(OldState); + + printf("HDL:END ver.1.00\n"); + + FinishHDDLoad(1); + } + else{ + printf("HDL:security contents error.\n"); + CpuSuspendIntr(&OldState); + FreeSysMemory(buffer); + CpuResumeIntr(OldState); + HDDBootError(-5); + } +} diff --git a/iop/hdd/hddload/src/imports.lst b/iop/hdd/hddload/src/imports.lst new file mode 100644 index 00000000000..36cc1416acf --- /dev/null +++ b/iop/hdd/hddload/src/imports.lst @@ -0,0 +1,52 @@ +dev9_IMPORTS_start +I_dev9Shutdown +dev9_IMPORTS_end + +atad_IMPORTS_start +I_ata_get_devinfo +I_ata_device_sector_io +I_ata_device_sce_sec_unlock +atad_IMPORTS_end + +cdvdman_IMPORTS_start +I_sceCdRI +cdvdman_IMPORTS_end + +intrman_IMPORTS_start +I_CpuSuspendIntr +I_CpuResumeIntr +I_CpuEnableIntr +intrman_IMPORTS_end + +loadcore_IMPORTS_start +I_FlushDcache +loadcore_IMPORTS_end + +sifman_IMPORTS_start +I_sceSifSetDma +I_sceSifDmaStat +sifman_IMPORTS_end + +stdio_IMPORTS_start +I_printf +stdio_IMPORTS_end + +sysclib_IMPORTS_start +I_strcmp +I_strtol +sysclib_IMPORTS_end + +secrman_IMPORTS_start +I_SecrDiskBootFile +secrman_IMPORTS_end + +sysmem_IMPORTS_start +I_AllocSysMemory +I_FreeSysMemory +sysmem_IMPORTS_end + +thbase_IMPORTS_start +I_SleepThread +I_CreateThread +I_StartThread +thbase_IMPORTS_end diff --git a/iop/hdd/hddload/src/irx_imports.h b/iop/hdd/hddload/src/irx_imports.h new file mode 100644 index 00000000000..1e5068fd1d7 --- /dev/null +++ b/iop/hdd/hddload/src/irx_imports.h @@ -0,0 +1,19 @@ +#ifndef IOP_IRX_IMPORTS_H +#define IOP_IRX_IMPORTS_H + +#include + +/* Please keep these in an alphabetical order! */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#endif /* IOP_IRX_IMPORTS_H */