|
| 1 | +#include "config.h" |
| 2 | +#include "crypto.h" |
| 3 | +#include "defaults.h" |
1 | 4 | #include "hdd.h" |
| 5 | +#include "loader.h" |
2 | 6 | #include <debug.h> |
3 | 7 | #include <fcntl.h> |
4 | 8 | #include <kernel.h> |
5 | 9 | #include <ps2sdkapi.h> |
| 10 | +#include <stdlib.h> |
| 11 | +#include <string.h> |
6 | 12 |
|
7 | 13 | static int isScreenInited = 0; |
8 | 14 |
|
@@ -38,22 +44,114 @@ void msg(const char *str, ...) { |
38 | 44 | va_end(args); |
39 | 45 | } |
40 | 46 |
|
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) { |
43 | 49 | // Display the error message |
44 | 50 | initScreen(); |
45 | 51 | scr_printf("\t\nFatal error:\n\t%s\n", msg); |
46 | 52 |
|
47 | | - // Shutdown dev9 |
48 | | - shutdownDEV9(); |
49 | | - |
50 | 53 | // Delay |
51 | 54 | int ret = 0x20000000; |
52 | 55 | while (ret--) |
53 | 56 | 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); |
54 | 146 |
|
55 | 147 | // Execute OSDSYS with the "BootError" argument |
56 | | - char *args[] = {"BootError"}; |
57 | | - ExecOSD(1, args); |
| 148 | + execOSD(argc, argv); |
58 | 149 | __builtin_trap(); |
59 | 150 | } |
| 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 | +} |
0 commit comments