Skip to content

Commit 248e1df

Browse files
committed
mbr: implement support for loading OSD and config file paths
1 parent 021ec98 commit 248e1df

9 files changed

Lines changed: 395 additions & 139 deletions

File tree

common/include/defaults.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@
2626
// Partition containing DKWDRV
2727
#define HOSD_SYS_PARTITION "hdd0:__system"
2828

29+
// Path relative to HOSD_SYS_PARTITION root
30+
#ifndef HOSDMBR_CONF_PATH
31+
#define HOSD_PFS_PATH "/osdmenu/hosdmenu.elf"
32+
#endif
33+
2934
// Path relative to HOSD_CONF_PARTITION root
3035
#ifndef HOSD_CONF_PATH
3136
#define HOSD_CONF_PATH "/osdmenu/OSDMENU.CNF"
@@ -41,4 +46,18 @@
4146
#define HOSD_DKWDRV_PATH HOSD_SYS_PARTITION ":pfs:/osdmenu/DKWDRV.ELF"
4247
#endif
4348

49+
//
50+
// HDD-OSD and PSBBN paths, relative to SYSTEM_PARTITION root
51+
//
52+
53+
// System partition
54+
#define SYSTEM_PARTITION "hdd0:__system"
55+
56+
// HDD-OSD
57+
#define HOSDSYS_PFS_PATH_1 "/osd100/hosdsys.elf"
58+
#define HOSDSYS_PFS_PATH_2 "/osd100/OSDSYS_A.XLF"
59+
60+
// PSBBN launcher path
61+
#define OSDBOOT_PFS_PATH "/p2lboot/osdboot.elf"
62+
4463
#endif

examples/OSDMBR.CNF

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
boot_auto = $PSBBN
2+
boot_auto = $HOSDSYS
3+
boot_auto_arg1 =
4+
boot_start = hdd0:__common:pfs:/Apps/APP_NHDDL/nhddl.elf
5+
boot_start = mc0:/APP_NHDDL/nhddl.elf
6+
boot_start_arg1 = -mode=mmce
7+
boot_start_arg2 = -video=480p
8+
boot_triangle = hdd0:__common/Apps/BOOT/BOOT.ELF
9+
boot_triangle_arg1 =
10+
boot_circle = mc?:/BOOT/BOOT.elf
11+
boot_circle_arg1 =
12+
boot_cross = cdrom
13+
boot_cross_arg1 =
14+
boot_square = rom0:OSDSYS
15+
boot_square_arg1 =
16+
cdrom_skip_ps2logo = 1
17+
cdrom_disable_gameid = 0
18+
cdrom_use_dkwdrv = 0
19+
ps1drv_enable_fast = 0
20+
ps1drv_enable_smooth = 0
21+
ps1drv_use_ps1vn = 0
22+
prefer_bbn = 0

mbr/Makefile

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ EE_LINKFILE = linkfile
1010

1111
EE_INCS = -I$(PS2SDK)/ee/include -I$(PS2SDK)/common/include -I$(PS2SDK)/sbv/include -I$(PS2DEV)/gsKit/include -I$(PS2SDK)/ports/include -Iinclude -I../common/include
1212
EE_LIBS = -lpatches -ldma -ldebug -lfileXio -lsecr -lpad -lgskit -ldmakit -lmc
13-
EE_OBJS = main.o init.o config.o mbrboot_crypto.o loader.o hdd.o hdd_start.o common.o disc.o
13+
EE_OBJS = main.o init.o config.o crypto.o loader.o hdd.o patinfo.o common.o disc.o
1414
ELF_FILES = loader.elf ps1vn.elf
1515
IRX_FILES = iomanX.irx fileXio.irx ps2dev9.irx ps2atad.irx ps2hdd-osd.irx ps2fs.irx secrsif.irx
1616

@@ -21,7 +21,7 @@ RES_FILES += icon_A.sys icon_C.sys icon_J.sys
2121
# C compiler flags
2222
EE_CFLAGS := -D_EE -O2 -G0 -Wall $(EE_CFLAGS)
2323
EE_CFLAGS = -D_EE -Os -G0 -Wall -Werror -fdata-sections -ffunction-sections $(EE_INCS)
24-
EE_LDFLAGS += -Wl,-zmax-page-size=128 -Wl,--gc-sections # -s
24+
EE_LDFLAGS += -Wl,-zmax-page-size=128 -Wl,--gc-sections -s
2525
EE_LDFLAGS += -L$(PS2DEV)/gsKit/lib
2626

2727
# Reduce binary size by using newlib-nano
@@ -32,20 +32,10 @@ EE_OBJS_DIR = obj/
3232
EE_ASM_DIR = asm/
3333
EE_SRC_DIR = src/
3434

35-
ifeq ($(PAYLOAD),patcher.elf)
36-
EE_CFLAGS += -DHOSDMENU
37-
endif
38-
3935
ifeq ($(ENABLE_PRINTF), 1)
4036
EE_CFLAGS += -DENABLE_PRINTF
4137
endif
4238

43-
ifneq (,$(findstring ELF,$(PAYLOAD)))
44-
EE_OBJS += $(PAYLOAD:.ELF=_ELF.o)
45-
else
46-
EE_OBJS += $(PAYLOAD:.elf=_elf.o)
47-
endif
48-
4939
EE_OBJS += $(IRX_FILES:.irx=_irx.o)
5040
EE_OBJS += $(ELF_FILES:.elf=_elf.o)
5141
EE_OBJS += $(RES_FILES:.sys=_sys.o)
@@ -66,19 +56,6 @@ clean:
6656
%ps2hdd-osd_irx.c:
6757
$(BIN2C) $(PS2SDK)/iop/irx/ps2hdd-osd.irx $@ $(*:$(EE_SRC_DIR)%=%)ps2hdd_osd_irx
6858

69-
# HOSDMenu payload
70-
%patcher_elf.c:
71-
$(MAKE) -C ../patcher clean
72-
$(MAKE) -C ../patcher HOSD=1
73-
$(BIN2C) ../patcher/patcher.elf $@ payload_elf
74-
75-
# Custom payload
76-
%_elf.c:
77-
$(BIN2C) $(*:$(EE_SRC_DIR)%=%).elf $@ payload_elf
78-
79-
%_ELF.c:
80-
$(BIN2C) $(*:$(EE_SRC_DIR)%=%).ELF $@ payload_elf
81-
8259
# ELF loader
8360
loader.elf:
8461
$(MAKE) -C ../utils/loader/$<

mbr/include/common.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,20 @@ int checkFile(char *path);
77
// Displays the message on the screen
88
void msg(const char *str, ...);
99

10+
// Displays the fatal error message on the screen
11+
void fatalMsg(char *str);
12+
13+
// Starts HOSDMenu or HDD-OSD.
14+
// Assumes the system partition is already mounted and argv has space for the additional -mbrboot argument.
15+
// Will unmount the partition.
16+
int startHOSDSYS(int argc, char *argv[]);
17+
18+
// Attempts to launch PSBBN, HOSDMenu or HOSDSYS. Falls back to OSDSYS
19+
int execOSD(int argc, char *argv[]);
20+
21+
// Shuts down HDD, dev9 and exits to OSD with arguments
22+
void bootFailWithArgs(char *msg, int argc, char *argv[]);
23+
1024
// Shuts down HDD, dev9 and exits to OSDSYS
1125
void bootFail(char *msg);
1226

mbr/include/config.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ typedef enum {
1010
FLAG_PS1DRV_FAST = (1 << 3), // If set, will force PS1DRV fast disc speed
1111
FLAG_PS1DRV_SMOOTH = (1 << 4), // If set, will force PS1DRV texture smoothing
1212
FLAG_PS1DRV_USE_VN = (1 << 5), // If set, run PS1DRV via the PS1DRV Video Mode Negator
13+
FLAG_PREFER_BBN = (1 << 6), // If set, will attempt to run PSBBN instead of HOSDSYS on OSD errors
1314
} Flags;
1415

1516
typedef enum {
@@ -31,12 +32,13 @@ typedef struct launchPath {
3132
TriggerType trigger;
3233
linkedStr *paths;
3334
linkedStr *args;
35+
int argCount;
3436
struct launchPath *next;
3537
} launchPath;
3638

3739
// Settings struct, contains option flags and paths
3840
typedef struct {
39-
uint8_t flags; // Flags
41+
uint8_t flags; // Flags
4042
launchPath *paths; // Launch paths
4143
} Settings;
4244

mbr/src/common.c

Lines changed: 105 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1+
#include "config.h"
2+
#include "crypto.h"
3+
#include "defaults.h"
14
#include "hdd.h"
5+
#include "loader.h"
26
#include <debug.h>
37
#include <fcntl.h>
48
#include <kernel.h>
59
#include <ps2sdkapi.h>
10+
#include <stdlib.h>
11+
#include <string.h>
612

713
static int isScreenInited = 0;
814

@@ -38,22 +44,114 @@ void msg(const char *str, ...) {
3844
va_end(args);
3945
}
4046

41-
// Shuts down HDD, dev9 and exits to OSDSYS
42-
void bootFail(char *msg) {
47+
// Displays the fatal error message on the screen
48+
void fatalMsg(char *msg) {
4349
// Display the error message
4450
initScreen();
4551
scr_printf("\t\nFatal error:\n\t%s\n", msg);
4652

47-
// Shutdown dev9
48-
shutdownDEV9();
49-
5053
// Delay
5154
int ret = 0x20000000;
5255
while (ret--)
5356
asm("nop\nnop\nnop\nnop");
57+
}
58+
59+
// Starts HOSDMenu or HDD-OSD.
60+
// Assumes the system partition is already mounted and argv has space for the additional -mbrboot argument.
61+
// Will unmount the partition.
62+
int startHOSDSYS(int argc, char *argv[]) {
63+
char *hosdsysPath = NULL;
64+
if (checkFile("pfs0:" HOSDSYS_PFS_PATH_1) >= 0)
65+
hosdsysPath = SYSTEM_PARTITION ":pfs:" HOSDSYS_PFS_PATH_1;
66+
else if (checkFile("pfs0:" HOSDSYS_PFS_PATH_2) >= 0)
67+
hosdsysPath = SYSTEM_PARTITION ":pfs:" HOSDSYS_PFS_PATH_2;
68+
69+
umountPFS();
70+
71+
if (!hosdsysPath)
72+
return -ENOENT;
73+
74+
argv[0] = hosdsysPath;
75+
76+
if (!mountPFS(HOSD_SYS_PARTITION)) {
77+
int res = checkFile("pfs0:" HOSD_PFS_PATH);
78+
umountPFS();
79+
if (res >= 0) {
80+
// Boot HOSDMenu
81+
argv[0] = HOSD_SYS_PARTITION ":pfs:" HOSD_PFS_PATH;
82+
83+
argv[argc] = "-mbrboot";
84+
85+
LoadOptions opts = {
86+
.dev9ShutdownType = ShutdownType_None,
87+
.argc = argc + 1,
88+
.argv = argv,
89+
};
90+
loadELF(&opts);
91+
return -ENOENT;
92+
}
93+
}
94+
95+
// Boot HOSDSYS
96+
LoadOptions opts = {
97+
.argc = argc,
98+
.argv = argv,
99+
};
100+
loadELF(&opts);
101+
return -ENOENT;
102+
}
103+
104+
// Attempts to launch PSBBN, HOSDMenu or HOSDSYS. Falls back to OSDSYS
105+
void execOSD(int argc, char *argv[]) {
106+
if (mountPFS(SYSTEM_PARTITION)) {
107+
ExecOSD(argc, argv);
108+
return;
109+
}
110+
111+
// Ensure we always work with initialized argv array
112+
char **nargv = malloc((argc + 2) * sizeof(char *)); // + 2 to store nargv[0] and possible HOSDMenu -mbrboot argument
113+
int nargc = argc + 1;
114+
nargv[0] = NULL;
115+
if (argv && argc > 0)
116+
for (int i = 0; i < argc; i++)
117+
nargv[i + 1] = argv[i];
118+
119+
if ((settings.flags & FLAG_PREFER_BBN) && (checkFile("pfs0:" OSDBOOT_PFS_PATH) >= 0)) {
120+
// Start PSBBN
121+
nargv[0] = SYSTEM_PARTITION ":pfs:" OSDBOOT_PFS_PATH;
122+
if (argc > 1) // Encrypt arguments
123+
nargv = encryptOSDBOOTArgs(&nargc, nargv);
124+
125+
LoadOptions opts = {
126+
.argc = nargc,
127+
.argv = nargv,
128+
};
129+
loadELF(&opts);
130+
return;
131+
}
132+
133+
startHOSDSYS(nargc, nargv);
134+
135+
// Else, exec OSDSYS
136+
// Shutdown dev9
137+
shutdownDEV9();
138+
argc--;
139+
ExecOSD(argc, &argv[1]);
140+
return;
141+
}
142+
143+
// Shuts down HDD, dev9 and exits to OSD with arguments
144+
void bootFailWithArgs(char *msg, int argc, char *argv[]) {
145+
fatalMsg(msg);
54146

55147
// Execute OSDSYS with the "BootError" argument
56-
char *args[] = {"BootError"};
57-
ExecOSD(1, args);
148+
execOSD(argc, argv);
58149
__builtin_trap();
59150
}
151+
152+
// Shuts down HDD, dev9 and exits to OSD
153+
void bootFail(char *msg) {
154+
// Execute OSD with the "BootError" argument
155+
char *args[] = {"BootError"};
156+
bootFailWithArgs(msg, 1, args);
157+
}

mbr/src/config.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ int loadConfig() {
125125
newPath->trigger = btn;
126126
newPath->paths = NULL;
127127
newPath->args = NULL;
128+
newPath->argCount = 0;
128129

129130
// Insert the path into the list
130131
if (currentPath)
@@ -136,10 +137,11 @@ int loadConfig() {
136137
settings.paths = currentPath;
137138

138139
// Check if the option has an _arg suffix
139-
if ((optPtr = strrchr(lineBuffer, '_')) && !strncmp(++optPtr, "arg", 3))
140+
if ((optPtr = strrchr(lineBuffer, '_')) && !strncmp(++optPtr, "arg", 3)) {
140141
// Add the argument
141142
currentPath->args = addStr(currentPath->args, valuePtr);
142-
else
143+
currentPath->argCount++;
144+
} else
143145
// Add the path
144146
currentPath->paths = addStr(currentPath->paths, valuePtr);
145147

@@ -189,6 +191,13 @@ int loadConfig() {
189191
settings.flags &= ~(FLAG_PS1DRV_USE_VN);
190192
continue;
191193
}
194+
if (!strncmp(lineBuffer, "prefer_bbn", 10)) {
195+
if (atoi(valuePtr))
196+
settings.flags |= FLAG_PREFER_BBN;
197+
else
198+
settings.flags &= ~(FLAG_PREFER_BBN);
199+
continue;
200+
}
192201
}
193202
fclose(file);
194203
umountPFS();

0 commit comments

Comments
 (0)