Advertisement
Guest User

Untitled

a guest
Aug 7th, 2014
1,100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 207.71 KB | None | 0 0
  1. /*---------------------------------------------------------------------------*
  2.  
  3. Copyright 2010-2012 Nintendo. All rights reserved.
  4.  
  5. These coded instructions, statements, and computer programs contain
  6. proprietary information of Nintendo of America Inc. and/or Nintendo
  7. Company Ltd., and are protected by Federal copyright law. They may
  8. not be disclosed to third parties or copied or duplicated in any form,
  9. in whole or in part, without the prior written consent of Nintendo.
  10.  
  11. *---------------------------------------------------------------------------*/
  12. using System;
  13. using System.Collections.Generic;
  14. using System.Diagnostics;
  15. using System.IO;
  16. using System.Threading;
  17.  
  18. namespace CafeX
  19. {
  20. partial class Program
  21. {
  22. #region environment variables
  23.  
  24. internal static EnvVar CAFE_ROOT = new EnvVar("CAFE_ROOT");
  25. internal static EnvVar SDIO_BRIDGE_TOOLS = new EnvVar("SDIO_BRIDGE_TOOLS");
  26. internal static EnvVar MION_BRIDGE_TOOLS = new EnvVar("MION_BRIDGE_TOOLS");
  27. internal static EnvVar BRIDGE_CURRENT_NAME = new EnvVar("BRIDGE_CURRENT_NAME");
  28. internal static EnvVar BRIDGE_CURRENT_IP_ADDRESS = new EnvVar("BRIDGE_CURRENT_IP_ADDRESS");
  29.  
  30. internal static EnvVar PLATFORM = new EnvVar("PLATFORM");
  31. internal static EnvVar CAFESTOP_STATUS = new EnvVar("CAFESTOP_STATUS");
  32. internal static EnvVar CAFERUN_OPTION_SOFT_LAUNCH = new EnvVar("CAFERUN_OPTION_SOFT_LAUNCH");
  33. internal static EnvVar HOSTSTOP_RVAL = new EnvVar("HOSTSTOP_RVAL");
  34. internal static EnvVar HOSTSTOP_RETRIES = new EnvVar("HOSTSTOP_RETRIES");
  35. internal static EnvVar HOSTSTOP_RESET_OPTION = new EnvVar("HOSTSTOP_RESET_OPTION");
  36. internal static EnvVar CAFE_HARDWARE = new EnvVar("CAFE_HARDWARE");
  37. internal static EnvVar MION_PWR_SEQ = new EnvVar("MION_PWR_SEQ");
  38. internal static EnvVar BRIDGE_TYPE = new EnvVar("BRIDGE_TYPE");
  39. internal static EnvVar TMP = new EnvVar("TMP");
  40.  
  41. internal static EnvVar SESSION_MANAGER = new EnvVar("SESSION_MANAGER");
  42. internal static EnvVar SESSION_NAME = new EnvVar("SESSION_NAME");
  43. internal static EnvVar SESSION_DEBUG_OUT_PORT = new EnvVar("SESSION_DEBUG_OUT_PORT");
  44. internal static EnvVar SESSION_DEBUG_CONTROL_PORT = new EnvVar("SESSION_DEBUG_CONTROL_PORT");
  45. internal static EnvVar SESSION_HIO_OUT_PORT = new EnvVar("SESSION_HIO_OUT_PORT");
  46. internal static EnvVar SESSION_LAUNCH_CTRL_PORT = new EnvVar("SESSION_LAUNCH_CTRL_PORT");
  47. internal static EnvVar SESSION_PCFS_SATA_PORT = new EnvVar("SESSION_PCFS_SATA_PORT");
  48. internal static EnvVar SESSION_PATH_PREFIX = new EnvVar("SESSION_PATH_PREFIX");
  49.  
  50. internal static EnvVar CAFE_DATA_DIR = new EnvVar("CAFE_DATA_DIR");
  51. internal static EnvVar CAFE_CONTENT_DIR = new EnvVar("CAFE_CONTENT_DIR");
  52. internal static EnvVar CAFE_META_DIR = new EnvVar("CAFE_META_DIR");
  53. internal static EnvVar CAFE_SAVE_DIR = new EnvVar("CAFE_SAVE_DIR");
  54. internal static EnvVar CAFE_SLC_DIR = new EnvVar("CAFE_SLC_DIR");
  55. internal static EnvVar CAFE_MLC_DIR = new EnvVar("CAFE_MLC_DIR");
  56. internal static EnvVar CAFE_DATA_TMP = new EnvVar("CAFE_DATA_TMP");
  57.  
  58. internal static EnvVar LANG = new EnvVar("LANG");
  59. internal static EnvVar QUIET = new EnvVar("QUIET");
  60. internal static EnvVar CAFESTOP_ONLY_IF_FSEMUL = new EnvVar("CAFESTOP_ONLY_IF_FSEMUL");
  61. internal static EnvVar PPC_OS_FLAGS = new EnvVar("PPC_OS_FLAGS");
  62. internal static EnvVar PPC_APP_FLAGS = new EnvVar("PPC_APP_FLAGS");
  63. internal static EnvVar PCFS_SYNC_DATE = new EnvVar("PCFS_SYNC_DATE");
  64. internal static EnvVar MCP_LAUNCH_HINT = new EnvVar("MCP_LAUNCH_HINT");
  65. internal static EnvVar MCP_INFO_OPTION = new EnvVar("MCP_INFO_OPTION");
  66. internal static EnvVar DEBUGGER = new EnvVar("DEBUGGER");
  67. internal static EnvVar CAFE_CONSOLE = new EnvVar("CAFE_CONSOLE");
  68. internal static EnvVar CONSOLE = new EnvVar("CONSOLE");
  69. internal static EnvVar CAFE_DEBUG_PORT = new EnvVar("CAFE_DEBUG_PORT");
  70. internal static EnvVar DEBUG_ELF_FILE = new EnvVar("DEBUG_ELF_FILE");
  71. internal static EnvVar CAFERUN_OPTION_SOFT_RESTART = new EnvVar("CAFERUN_OPTION_SOFT_RESTART");
  72. internal static EnvVar CAFERUN_OPTION_MLC_EMU_LAUNCH = new EnvVar("CAFERUN_OPTION_MLC_EMU_LAUNCH");
  73. internal static EnvVar CAFERUN_OPTION_MLC_EMU_TITLEID = new EnvVar("CAFERUN_OPTION_MLC_EMU_TITLEID");
  74. internal static EnvVar CAFERUN_OS_MAJOR_VERSION_LO = new EnvVar("CAFERUN_OS_MAJOR_VERSION_LO");
  75. internal static EnvVar CAFERUN_OS_MAJOR_VERSION = new EnvVar("CAFERUN_OS_MAJOR_VERSION");
  76. internal static EnvVar CAFERUN_COLDBOOT_OS_VERSION = new EnvVar("CAFERUN_COLDBOOT_OS_VERSION");
  77. internal static EnvVar HEARTBEAT_DISABLE = new EnvVar("HEARTBEAT_DISABLE");
  78. internal static EnvVar PCFS_DIR_MAPPING = new EnvVar("PCFS_DIR_MAPPING");
  79. internal static EnvVar MAPDIR_MLC = new EnvVar("MAPDIR_MLC");
  80. internal static EnvVar MAPDIR_SLC = new EnvVar("MAPDIR_SLC");
  81. internal static EnvVar MAPDIR_CODE = new EnvVar("MAPDIR_CODE");
  82. internal static EnvVar MAPDIR_META = new EnvVar("MAPDIR_META");
  83. internal static EnvVar MAPDIR_CONTENT = new EnvVar("MAPDIR_CONTENT");
  84. internal static EnvVar MAPDIR_SAVE = new EnvVar("MAPDIR_SAVE");
  85. internal static EnvVar PCFS_LOG_TIMESTAMP = new EnvVar("PCFS_LOG_TIMESTAMP");
  86. internal static EnvVar PCFS_SRV_LOG_OUTPUT = new EnvVar("PCFS_SRV_LOG_OUTPUT");
  87. internal static EnvVar RUN_FROM_HDD_BANK = new EnvVar("RUN_FROM_HDD_BANK");
  88. internal static EnvVar BRIDGE_PARAMETERS = new EnvVar("BRIDGE_PARAMETERS");
  89. internal static EnvVar BRIDGE_PARAMETERS_WITH_E = new EnvVar("BRIDGE_PARAMETERS_WITH_E");
  90. internal static EnvVar PCFSSERVER_PARAMETERS = new EnvVar("PCFSSERVER_PARAMETERS");
  91. internal static EnvVar PCFS_OVER_SATA_PORT = new EnvVar("PCFS_OVER_SATA_PORT");
  92. internal static EnvVar PREPHDD_STAT = new EnvVar("PREPHDD_STAT");
  93. internal static EnvVar DISC_EMU_TID = new EnvVar("DISC_EMU_TID");
  94. internal static EnvVar SYSTEM_MODE = new EnvVar("SYSTEM_MODE");
  95. internal static EnvVar RPL_DIRS = new EnvVar("RPL_DIRS");
  96. internal static EnvVar RPL_FILES = new EnvVar("RPL_FILES");
  97. internal static EnvVar CAFERUN_OPTION_NO_DATA_SYNC = new EnvVar("CAFERUN_OPTION_NO_DATA_SYNC");
  98. internal static EnvVar CAFE_BOOT_MODE = new EnvVar("CAFE_BOOT_MODE");
  99. internal static EnvVar CAFERUN_HOSTBRIDGE_VERSION = new EnvVar("CAFERUN_HOSTBRIDGE_VERSION");
  100. internal static EnvVar CAFERUN_FW_VERSION = new EnvVar("CAFERUN_FW_VERSION");
  101. internal static EnvVar CAFE_DEBUG_INIT = new EnvVar("CAFE_DEBUG_INIT");
  102. internal static EnvVar CAFE_DEBUG_PREINIT = new EnvVar("CAFE_DEBUG_PREINIT");
  103. internal static EnvVar CAFE_DEBUG_DONTSTOP = new EnvVar("CAFE_DEBUG_DONTSTOP");
  104. internal static EnvVar CAFE_LAUNCH_DEBUG = new EnvVar("CAFE_LAUNCH_DEBUG");
  105. internal static EnvVar CAFE_SYSLOG_LEVEL = new EnvVar("CAFE_SYSLOG_LEVEL");
  106. internal static EnvVar CAFE_RUN_FORCERESTART = new EnvVar("CAFE_RUN_FORCERESTART");
  107. internal static EnvVar DEBUG_PROC_ID = new EnvVar("DEBUG_PROC_ID");
  108. internal static EnvVar DEBUG_FLAGS = new EnvVar("DEBUG_FLAGS");
  109. internal static EnvVar CATOUTPUT = new EnvVar("CATOUTPUT");
  110. internal static EnvVar CMDOUTPUT = new EnvVar("CMDOUTPUT");
  111. internal static EnvVar CAFE_AUTOTEST = new EnvVar("CAFE_AUTOTEST");
  112.  
  113. internal static EnvVar DASH_A_ARGS = new EnvVar("DASH_A_ARGS");
  114. internal static EnvVar ARGS_WITH_BARS = new EnvVar("ARGS_WITH_BARS");
  115. internal static EnvVar DISC_EMU_TYPE = new EnvVar("DISC_EMU_TYPE");
  116. internal static EnvVar APP_XML_FILE = new EnvVar("APP_XML_FILE");
  117.  
  118. internal static EnvVar USE_PCFS_OVER_SATA = new EnvVar("USE_PCFS_OVER_SATA");
  119. internal static EnvVar CAFERUN_OPTION_CALLED_FROM_CAFEDISCRUN = new EnvVar("CAFERUN_OPTION_CALLED_FROM_CAFEDISCRUN");
  120. internal static EnvVar SYSTEM_XML = new EnvVar("SYSTEM_XML");
  121. internal static EnvVar TEMP = new EnvVar("TEMP");
  122. internal static EnvVar CAFERUN_WORK_DIR = new EnvVar("CAFERUN_WORK_DIR");
  123.  
  124. internal static EnvVar SYNCTOOL_SRC = new EnvVar("SYNCTOOL_SRC");
  125. internal static EnvVar SYNCTOOL_DEST = new EnvVar("SYNCTOOL_DEST");
  126. internal static EnvVar SYNCTOOL_CFG = new EnvVar("SYNCTOOL_CFG");
  127. internal static EnvVar SYNCTOOL_LOG = new EnvVar("SYNCTOOL_LOG");
  128.  
  129. internal static EnvVar CAFE_ELF = new EnvVar("CAFE_ELF");
  130. internal static EnvVar CAFE_ELF_SRC_DIR = new EnvVar("CAFE_ELF_SRC_DIR");
  131. internal static EnvVar CAFE_ELF_DIR = new EnvVar("CAFE_ELF_DIR");
  132. internal static EnvVar CAFE_CODE_DIR = new EnvVar("CAFE_CODE_DIR");
  133. internal static EnvVar COS_XML_FILE = new EnvVar("COS_XML_FILE");
  134. internal static EnvVar META_XML_FILE = new EnvVar("META_XML_FILE");
  135.  
  136. internal static EnvVar PREV_CAFE_ARGSTR = new EnvVar("PREV_CAFE_ARGSTR");
  137. internal static EnvVar CAFERUN_OS_VERSION_PARTIAL_UNIQUE_ID = new EnvVar("CAFERUN_OS_VERSION_PARTIAL_UNIQUE_ID");
  138. internal static EnvVar PCFS_SRV_LOG_DIR = new EnvVar("PCFS_SRV_LOG_DIR");
  139. internal static EnvVar CMD_RESULT = new EnvVar("CMD_RESULT");
  140.  
  141. internal static EnvVar CAFERUN_COLDBOOT_OS_VERSION_HI = new EnvVar("CAFERUN_COLDBOOT_OS_VERSION_HI");
  142. internal static EnvVar CAFERUN_COLDBOOT_OS_VERSION_LO = new EnvVar("CAFERUN_COLDBOOT_OS_VERSION_LO");
  143. internal static EnvVar BOOTRUN_WORK_DIR = new EnvVar("BOOTRUN_WORK_DIR");
  144. internal static EnvVar WUMAD_WORK_DIR = new EnvVar("WUMAD_WORK_DIR");
  145. internal static EnvVar FIRMWARE_FILE = new EnvVar("FIRMWARE_FILE");
  146. internal static EnvVar TOOLCHAIN = new EnvVar("TOOLCHAIN");
  147. internal static EnvVar CAFE_SECURITY = new EnvVar("CAFE_SECURITY");
  148. internal static EnvVar BOOT_DLF_FILE = new EnvVar("BOOT_DLF_FILE");
  149. internal static EnvVar CAFE_WUMAD = new EnvVar("CAFE_WUMAD");
  150.  
  151. internal static EnvVar CAFEMAKEDLF = new EnvVar("CAFEMAKEDLF");
  152. internal static EnvVar APP_TITLE_ID = new EnvVar("APP_TITLE_ID");
  153. internal static EnvVar APP_GROUP_ID = new EnvVar("APP_GROUP_ID");
  154. internal static EnvVar APP_TITLE_VERSION = new EnvVar("APP_TITLE_VERSION");
  155. internal static EnvVar CAFEDEVRUN_FILE = new EnvVar("CAFEDEVRUN_FILE");
  156. internal static EnvVar CAFEDEVRUN_EXT = new EnvVar("CAFEDEVRUN_EXT");
  157. internal static EnvVar CAFEDEVRUN_NAME = new EnvVar("CAFEDEVRUN_NAME");
  158. internal static EnvVar CAFEDEVRUN_FILENAME = new EnvVar("CAFEDEVRUN_FILENAME");
  159. internal static EnvVar CAFEDEVRUN_DIR = new EnvVar("CAFEDEVRUN_DIR");
  160. internal static EnvVar CAFEDEVRUN_WORK_DIR = new EnvVar("CAFEDEVRUN_WORK_DIR");
  161. internal static EnvVar INCLUDE_SYSTEM_DIR = new EnvVar("INCLUDE_SYSTEM_DIR");
  162. internal static EnvVar CAFE_BASEFILE_PATH = new EnvVar("CAFE_BASEFILE_PATH");
  163. internal static EnvVar DEFAULT_DDF = new EnvVar("DEFAULT_DDF");
  164. internal static EnvVar COMMON_DDF_FILE = new EnvVar("COMMON_DDF_FILE");
  165. internal static EnvVar INDIVIDUAL_DDF_FILE = new EnvVar("INDIVIDUAL_DDF_FILE");
  166.  
  167. internal static EnvVar FSEMUL_PARAMS = new EnvVar("FSEMUL_PARAMS");
  168. internal static EnvVar BRIDGE_INSTALL_PATH = new EnvVar("BRIDGE_INSTALL_PATH");
  169. internal static EnvVar CAFE_DETACH_FSEMUL = new EnvVar("CAFE_DETACH_FSEMUL");
  170.  
  171. internal static EnvVar PORT = new EnvVar("PORT");
  172. internal static EnvVar MULTIELF = new EnvVar("MULTIELF");
  173. internal static EnvVar CATTOUCAN_STATUS = new EnvVar("CATTOUCAN_STATUS");
  174. internal static EnvVar MULTI_CONNECT = new EnvVar("MULTI_CONNECT");
  175. internal static EnvVar CAFE_MULTI_INIT = new EnvVar("CAFE_MULTI_INIT");
  176. internal static EnvVar GHS_ROOT = new EnvVar("GHS_ROOT");
  177.  
  178. internal static EnvVar CAFEON_DISABLE_BGD = new EnvVar("CAFEON_DISABLE_BGD");
  179. internal static EnvVar CAFEON_NOBGD_META = new EnvVar("CAFEON_NOBGD_META");
  180. internal static EnvVar CAFEON_OPTION_NO_DATA_SYNC = new EnvVar("CAFEON_OPTION_NO_DATA_SYNC");
  181. internal static EnvVar AUTOTEST_RUNNING = new EnvVar("AUTOTEST_RUNNING");
  182. internal static EnvVar CAFE_RUN_RUNNING = new EnvVar("CAFE_RUN_RUNNING");
  183. internal static EnvVar CATTOUCAN_TERM = new EnvVar("CATTOUCAN_TERM");
  184. internal static EnvVar OS_VERSION_LO = new EnvVar("OS_VERSION_LO");
  185.  
  186. internal static EnvVar SDK_VER = new EnvVar("SDK_VER");
  187. internal static EnvVar SDK_MAJ_VER = new EnvVar("SDK_MAJ_VER");
  188. internal static EnvVar SDK_MIN_VER = new EnvVar("SDK_MIN_VER");
  189. internal static EnvVar SDK_MIC_VER = new EnvVar("SDK_MIC_VER");
  190.  
  191. internal static EnvVar CAFEUPDATE_USE_SYSTEM_UPDATER = new EnvVar("CAFEUPDATE_USE_SYSTEM_UPDATER");
  192. internal static EnvVar CAFEUPDATE_TITLE = new EnvVar("CAFEUPDATE_TITLE");
  193. internal static EnvVar CAFEUPDATE_PACKAGE = new EnvVar("CAFEUPDATE_PACKAGE");
  194. internal static EnvVar CAFEUPDATE_PACKAGE_DEFAULT = new EnvVar("CAFEUPDATE_PACKAGE_DEFAULT");
  195.  
  196. internal static EnvVar SCRIPT_EXE_TIMESTAMP = new EnvVar("SCRIPT_EXE_TIMESTAMP");
  197. internal static EnvVar LOGDIR = new EnvVar("LOGDIR");
  198. internal static EnvVar _CCRSYSCFG1 = new EnvVar("_CCRSYSCFG1");
  199. internal static EnvVar MONITOR_DEFAULT_TIMEOUT = new EnvVar("MONITOR_DEFAULT_TIMEOUT");
  200. internal static EnvVar COMPORTS_SUPPORT = new EnvVar("COMPORTS_SUPPORT");
  201. internal static EnvVar INITIALIZED_COMPORTS = new EnvVar("INITIALIZED_COMPORTS");
  202. internal static EnvVar MONITOR_FOR_OUTPUT_CALLS = new EnvVar("MONITOR_FOR_OUTPUT_CALLS");
  203.  
  204. internal static EnvVar MON_RESULT_PASS = new EnvVar("MON_RESULT_PASS");
  205. internal static EnvVar MON_RESULT_TIMEOUT = new EnvVar("MON_RESULT_TIMEOUT");
  206. internal static EnvVar MON_RESULT_UNDEF = new EnvVar("MON_RESULT_UNDEF");
  207. internal static EnvVar MON_RESULT_MANUAL = new EnvVar("MON_RESULT_MANUAL");
  208. internal static EnvVar MON_RESULT_NOLOG = new EnvVar("MON_RESULT_NOLOG");
  209. internal static EnvVar MON_RESULT_FAIL = new EnvVar("MON_RESULT_FAIL");
  210. internal static EnvVar MON_RESULT_EXPECTED_TERM = new EnvVar("MON_RESULT_EXPECTED_TERM");
  211. internal static EnvVar MON_RESULT_EXEC_WIN_APP = new EnvVar("MON_RESULT_EXEC_WIN_APP");
  212. internal static EnvVar MON_RESULT_NOPPC = new EnvVar("MON_RESULT_NOPPC");
  213. internal static EnvVar MON_RESULT_BOOT1 = new EnvVar("MON_RESULT_BOOT1");
  214. internal static EnvVar MON_RESULT_RESTART = new EnvVar("MON_RESULT_RESTART");
  215. internal static EnvVar MON_RESULT_TEST_AS_RPL = new EnvVar("MON_RESULT_TEST_AS_RPL");
  216. internal static EnvVar MON_RESULT_END_OF_RPL_LOADER = new EnvVar("MON_RESULT_END_OF_RPL_LOADER");
  217. internal static EnvVar MON_RESULT_BRIDGE_COLLISION = new EnvVar("MON_RESULT_BRIDGE_COLLISION");
  218. internal static EnvVar MON_RESULT_TEST_DBG_OUT = new EnvVar("MON_RESULT_TEST_DBG_OUT");
  219. internal static EnvVar MON_RESULT_BRIDGE_OFF = new EnvVar("MON_RESULT_BRIDGE_OFF");
  220. internal static EnvVar MON_RESULT_ERROR = new EnvVar("MON_RESULT_ERROR");
  221. internal static EnvVar MON_RESULT_SKIP = new EnvVar("MON_RESULT_SKIP");
  222. internal static EnvVar MON_RESULT_FATAL_ERROR = new EnvVar("MON_RESULT_FATA_ERROR");
  223. internal static EnvVar MON_RESULT_MISSING = new EnvVar("MON_RESULT_MISSING");
  224. internal static EnvVar MON_RESULT_ABORTED = new EnvVar("MON_RESULT_ABORTED");
  225. internal static EnvVar MON_RESULT_KILL_AUTOTEST = new EnvVar("MON_RESULT_KILL_AUTOTEST");
  226. internal static EnvVar SYNCTOOL_RVAL = new EnvVar("SYNCTOOL_RVAL");
  227.  
  228. internal static EnvVar PATH = new EnvVar("PATH");
  229.  
  230. internal static EnvVar PCFS_HEADLESS_EMUL = new EnvVar("PCFS_HEADLESS_EMUL");
  231. internal static EnvVar TMP_SUB_DIR = new EnvVar("TMP_SUB_DIR");
  232. internal static EnvVar PCFSSERVERSYNC_RVAL = new EnvVar("PCFSSERVERSYNC_RVAL");
  233.  
  234. internal static EnvVar CAFE_RUN_DEBUG = new EnvVar("CAFE_RUN_DEBUG");
  235.  
  236. internal static EnvVar CYGWIN_PATH = new EnvVar("CYGWIN_PATH");
  237.  
  238. internal static EnvVar CAFEX_STDIN_ENCODING = new EnvVar("CAFEX_STDIN_ENCODING");
  239. internal static EnvVar CAFEX_STDOUT_ENCODING = new EnvVar("CAFEX_STDOUT_ENCODING");
  240.  
  241. //disrun in NAND mode
  242. internal static EnvVar ALLOW_DISCRUN_IN_SOFTLAUNCH = new EnvVar("ALLOW_DISCRUN_IN_SOFTLAUNCH");
  243. internal static EnvVar BOOTRUN_ELF_OPT = new EnvVar("BOOTRUN_ELF_OPT");
  244. internal static EnvVar BOOTRUN_ELF_VALUE = new EnvVar("BOOTRUN_ELF_VALUE");
  245. internal static EnvVar ELF_NEST_OPT = new EnvVar("ELF_NEST_OPT");
  246. internal static EnvVar ELF_NEST_VALUE = new EnvVar("ELF_NEST_VALUE");
  247. internal static EnvVar SOFT_LAUNCH_CAFE_ELF = new EnvVar("SOFT_LAUNCH_CAFE_ELF");
  248. internal static EnvVar CAFERUN_WUMAD_TITLE_ID = new EnvVar("CAFERUN_WUMAD_TITLE_ID");
  249. #endregion
  250.  
  251. #region GLOBAL INTIALIZATION
  252. internal static char DEFAULT_DELIMITER = '\t';
  253. internal static string SYSCONFIGTOOL_TITLE_ID = "000500101f700500";
  254.  
  255. internal static CAFEX_ERROR InitialEnvVarSetup()
  256. {
  257. #if DEBUG
  258. Log.WriteLine("InitialEnvVarSetup started.");
  259. #endif
  260.  
  261. // THESE NEED TO BE SET IN CAFEX_ENV.BAT, BUT FOR NOW ARE HERE
  262. CAFE_ROOT.value = PathConverter.Windowsify(CAFE_ROOT.value);
  263. GHS_ROOT.value = PathConverter.Windowsify(GHS_ROOT.value);
  264. CAFE_HARDWARE.value = "catdevmp";
  265. //CAFE_BOOT_MODE.value = "PCFS";
  266. //CAFE_CONTENT_DIR.value = CAFE_ROOT.value + "\\data\\disc\\content";
  267. //CAFE_SAVE_DIR.value = CAFE_ROOT.value + "\\data\\save";
  268. //CAFE_SLC_DIR.value = CAFE_ROOT.value + "\\data\\slc";
  269. //CAFE_MLC_DIR.value = CAFE_ROOT.value + "\\data\\mlc";
  270. //CAFE_META_DIR.value = CAFE_ROOT.value + "\\data\\disc\\meta";
  271. CAFE_CONTENT_DIR.value = PathConverter.Windowsify(CAFE_CONTENT_DIR.value);
  272. CAFE_SAVE_DIR.value = PathConverter.Windowsify(CAFE_SAVE_DIR.value);
  273. CAFE_SLC_DIR.value = PathConverter.Windowsify(CAFE_SLC_DIR.value);
  274. CAFE_MLC_DIR.value = PathConverter.Windowsify(CAFE_MLC_DIR.value);
  275. CAFE_META_DIR.value = PathConverter.Windowsify(CAFE_META_DIR.value);
  276. TOOLCHAIN.value = "ghs";
  277. MONITOR_DEFAULT_TIMEOUT.value = "60";
  278. // carlosk - don't override this setting from environment
  279. //CAFE_CONSOLE.value = "cattoucan";
  280. if (string.IsNullOrEmpty(CAFE_CONSOLE.value))
  281. {
  282. CAFE_CONSOLE.value = "cattoucan";
  283. }
  284. // carlosk - need to remove this or it will always do pcfs over sata
  285. //USE_PCFS_OVER_SATA.value = "1";
  286.  
  287. MON_RESULT_UNDEF.value = "-1";
  288. MON_RESULT_TIMEOUT.value = "0";
  289. MON_RESULT_PASS.value = "1";
  290. MON_RESULT_MANUAL.value = "2";
  291. MON_RESULT_NOLOG.value = "3";
  292. MON_RESULT_FAIL.value = "4";
  293. MON_RESULT_EXPECTED_TERM.value = "5";
  294. MON_RESULT_EXEC_WIN_APP.value = "6";
  295. MON_RESULT_NOPPC.value = "7";
  296. MON_RESULT_BOOT1.value = "8";
  297. MON_RESULT_RESTART.value = "9";
  298. MON_RESULT_TEST_AS_RPL.value = "10";
  299. MON_RESULT_END_OF_RPL_LOADER.value = "11";
  300. MON_RESULT_BRIDGE_COLLISION.value = "12";
  301. MON_RESULT_TEST_DBG_OUT.value = "13";
  302. MON_RESULT_BRIDGE_OFF.value = "14";
  303. MON_RESULT_KILL_AUTOTEST.value = "15";
  304. MON_RESULT_ERROR.value = "128";
  305. MON_RESULT_SKIP.value = "129";
  306. MON_RESULT_FATAL_ERROR.value = "255";
  307. MON_RESULT_MISSING.value = "131";
  308. MON_RESULT_ABORTED.value = "132";
  309.  
  310. ALLOW_DISCRUN_IN_SOFTLAUNCH.value = "0";
  311.  
  312. PATH.AddToVar(";C:\\cygwin\\bin\\", DEFAULT_DELIMITER);
  313.  
  314. string sdk_ver_file = CAFE_ROOT.value + "\\system\\include\\sdk_ver.h";
  315. if (!File.Exists(sdk_ver_file))
  316. {
  317. sdk_ver_file = CAFE_ROOT.value + "\\system\\src\\build\\make\\os_version.mk";
  318. if (!File.Exists(sdk_ver_file))
  319. {
  320. Console.WriteLine("cafex run error: sdk_ver.h and os_version.mk do not exist: " + CAFE_ROOT.value + "\\system\\include\\sdk_ver.h" + sdk_ver_file);
  321. }
  322. else
  323. {
  324. FileStream fs = new FileStream(CAFE_ROOT.value + "\\system\\src\\build\\make\\os_version.mk", FileMode.Open, FileAccess.Read);
  325. StreamReader sr = new StreamReader(fs);
  326. bool found_line = false;
  327. while (!sr.EndOfStream)
  328. {
  329. string line = sr.ReadLine();
  330. if (line.Contains("SM := "))
  331. SDK_VER.value = line.Substring(25,1);
  332. if (line.Contains("Sm := "))
  333. SDK_VER.value = SDK_VER.value + "." + line.Substring(25, 2);
  334. if (line.Contains("Sc := "))
  335. {
  336. string sdk_ver = SDK_VER.value + "." + line.Substring(25, 2);
  337. SDK_VER.value = sdk_ver.Replace(".0", ".");
  338. found_line = true;
  339. break;
  340. }
  341. }
  342. sr.Close();
  343. if (!found_line)
  344. {
  345. Console.WriteLine("cafex run error: couldn't find sdk version in os_version.mk");
  346. return CAFEX_ERROR.RUN_COULDNT_PARSE_SDK_VERSION_HEADER;
  347. }
  348. }
  349. }
  350. else
  351. {
  352. FileStream fs = new FileStream(CAFE_ROOT.value + "\\system\\include\\sdk_ver.h", FileMode.Open, FileAccess.Read);
  353. StreamReader sr = new StreamReader(fs);
  354. bool found_line = false;
  355. while (!sr.EndOfStream)
  356. {
  357. string line = sr.ReadLine();
  358. if (line.Contains("#define CAFE_OS_SDK_VERSION_STRING"))
  359. {
  360. int index = line.IndexOf("#define CAFE_OS_SDK_VERSION_STRING");
  361. string version = line.Substring(61,7);
  362. SDK_VER.value = version.Replace(".0", ".");
  363. found_line = true;
  364. break;
  365. }
  366. }
  367. sr.Close();
  368. if (!found_line)
  369. {
  370. Console.WriteLine("cafex run error: couldn't find sdk version in sdk_ver.h");
  371. return CAFEX_ERROR.RUN_COULDNT_PARSE_SDK_VERSION_HEADER;
  372. }
  373. }
  374.  
  375. SDK_MAJ_VER.value = mionps.Run(BRIDGE_CURRENT_IP_ADDRESS.value + " 3");
  376. SDK_MIN_VER.value = mionps.Run(BRIDGE_CURRENT_IP_ADDRESS.value + " 4");
  377. SDK_MIC_VER.value = mionps.Run(BRIDGE_CURRENT_IP_ADDRESS.value + " 5");
  378.  
  379. #region session setup
  380.  
  381. if (SESSION_MANAGER.value == "1")
  382. {
  383. if (!File.Exists(MION_BRIDGE_TOOLS.value + "\\SessionManager.dll"))
  384. {
  385. Console.WriteLine("ERROR: SessionManager.dll not found and multi-CATDEV is enabled!");
  386. Console.WriteLine(" Please install HostBridge version 3.2.2.3+ or disable multi-CATDEV feature.");
  387. return CAFEX_ERROR.NO_SESSIONMANAGER_DLL;
  388. }
  389.  
  390. string output = string.Empty;
  391. SessionManagerUtil.GetSessionInfo(out output);
  392.  
  393. string[] output_array = output.Split(' ');
  394.  
  395. SESSION_NAME.value = output_array[0];
  396. SESSION_DEBUG_OUT_PORT.value = output_array[1];
  397. SESSION_DEBUG_CONTROL_PORT.value = output_array[2];
  398. SESSION_HIO_OUT_PORT.value = output_array[3];
  399. SESSION_LAUNCH_CTRL_PORT.value = output_array[4];
  400. SESSION_PCFS_SATA_PORT.value = output_array[5];
  401.  
  402. SESSION_PATH_PREFIX.value = SESSION_NAME.value + "_";
  403.  
  404. CAFE_DATA_DIR.value = CAFE_ROOT.value + "\\" + SESSION_PATH_PREFIX.value + "data";
  405.  
  406. // If the content location has been changed from the default, we should not touch and use it.
  407. // Otherwise, should modify by appending the CAFE_DATA_DIR.
  408. string defaultContentDir = CAFE_ROOT.value + "\\data\\disc\\content";
  409. if (CAFE_CONTENT_DIR.value == defaultContentDir)
  410. {
  411. CAFE_CONTENT_DIR.value = CAFE_DATA_DIR.value + "\\disc\\content";
  412. }
  413.  
  414. CAFE_META_DIR.value = CAFE_DATA_DIR.value + "\\disc\\meta";
  415. CAFE_SAVE_DIR.value = CAFE_DATA_DIR.value + "\\save";
  416. CAFE_SLC_DIR.value = CAFE_DATA_DIR.value + "\\slc";
  417. CAFE_MLC_DIR.value = CAFE_DATA_DIR.value + "\\mlc";
  418. CAFE_DATA_TMP.value = CAFE_DATA_DIR.value + "\\tmp";
  419. }
  420. else
  421. {
  422. SESSION_NAME.value = "";
  423. SESSION_DEBUG_OUT_PORT.value = "6001";
  424. SESSION_DEBUG_CONTROL_PORT.value = "6002";
  425. SESSION_HIO_OUT_PORT.value = "6003";
  426. SESSION_LAUNCH_CTRL_PORT.value = "6006";
  427. SESSION_PCFS_SATA_PORT.value = "7500";
  428.  
  429. SESSION_PATH_PREFIX.value = "";
  430. CAFE_DATA_DIR.value = CAFE_ROOT.value + "\\data";
  431. }
  432.  
  433. #if DEBUG
  434. Log.WriteLine("SESSION_NAME = " + SESSION_NAME.value);
  435. Log.WriteLine("SESSION_DEBUG_OUT_PORT = " + SESSION_DEBUG_OUT_PORT.value);
  436. Log.WriteLine("SESSION_DEBUG_CONTROL_PORT = " + SESSION_DEBUG_CONTROL_PORT.value);
  437. Log.WriteLine("SESSION_HIO_OUT_PORT = " + SESSION_HIO_OUT_PORT.value);
  438. Log.WriteLine("SESSION_LAUNCH_CTRL_PORT = " + SESSION_LAUNCH_CTRL_PORT.value);
  439. Log.WriteLine("SESSION_PCFS_SATA_PORT = " + SESSION_PCFS_SATA_PORT.value);
  440. Log.WriteLine("SESSION_PATH_PREFIX = " + SESSION_PATH_PREFIX.value);
  441. Log.WriteLine("CAFE_DATA_DIR = " + CAFE_DATA_DIR.value);
  442. Log.WriteLine("CAFE_CONTENT_DIR = " + CAFE_CONTENT_DIR.value);
  443. Log.WriteLine("CAFEON_OPTION_NO_DATA_SYNC = " + CAFEON_OPTION_NO_DATA_SYNC.value);
  444. Log.WriteLine("CAFERUN_OPTION_NO_DATA_SYNC= " + CAFERUN_OPTION_NO_DATA_SYNC.value);
  445. #endif
  446.  
  447. #endregion
  448.  
  449. return CAFEX_ERROR.OK;
  450. }
  451.  
  452. #endregion
  453.  
  454. #region SCRIPT FUNCTIONS
  455.  
  456. //Script function: cafestop
  457. static void stop(string[] args)
  458. {
  459. #if DEBUG
  460. Log.WriteLine("stop started.");
  461. string argString = null;
  462. if (args != null)
  463. {
  464. foreach (string arg in args)
  465. {
  466. argString += arg + " ";
  467. }
  468. }
  469. Log.WriteLine("Arguments=" + argString);
  470.  
  471. #endif
  472.  
  473. if (args != null && args.Length > 0)
  474. {
  475. // Check for -h and display help
  476. if (args[0].ToLowerInvariant() == "-h")
  477. {
  478. // Display help and return;
  479. stop_usage();
  480. return;
  481. }
  482. // Check for -hostonly setting on first argument
  483. if (args[0].ToLowerInvariant() == "-hostonly")
  484. {
  485. // CafeX doesn't let you execute any function if bridge vars are not set, so there is no
  486. // reason to check them here again like in BASH.
  487. // -----------------------------------------------------------------------------------------
  488. // We want to kill any running processes, but don't want to talk to a (non-existent?) device
  489. ToucanReset.Stop();
  490. PCFSServer.Shutdown();
  491. return;
  492. }
  493. }
  494.  
  495. PLATFORM.value = "cafe";
  496.  
  497. if (CAFERUN_OPTION_SOFT_LAUNCH.value == "1")
  498. {
  499. #if DEBUG
  500. Log.WriteLine("CAFERUN_OPTION_SOFT_LAUNCH.value == 1 -> calling dkt.FlushOutputBuffer()");
  501. #endif
  502. int ret = dkt.FlushOutputBuffer();
  503. CAFESTOP_STATUS.value = ret.ToString();
  504. }
  505. else
  506. {
  507. #if DEBUG
  508. Log.WriteLine("CAFERUN_OPTION_SOFT_LAUNCH.value != 1 -> calling hoststop()");
  509. #endif
  510.  
  511. hoststop(args);
  512.  
  513. int ret = PCFSServer.Shutdown();
  514. CAFESTOP_STATUS.value = ret.ToString();
  515. }
  516.  
  517. if (CAFESTOP_STATUS.value != "0")
  518. {
  519. Console.WriteLine("cafex stop error: CAFESTOP_STATUS=" + CAFESTOP_STATUS.value);
  520. }
  521. }
  522.  
  523. //Script function: hoststop
  524. static void hoststop(string[] args)
  525. {
  526. #if DEBUG
  527. Log.WriteLine("hoststop started.");
  528. string argString = null;
  529. if (args != null)
  530. {
  531. foreach (string arg in args)
  532. {
  533. argString += arg + " ";
  534. }
  535. }
  536. Log.WriteLine("Arguments=" + argString);
  537.  
  538. #endif
  539.  
  540. if (SDIO_BRIDGE_TOOLS.value == null)
  541. {
  542. if (MION_BRIDGE_TOOLS.value == null)
  543. {
  544. Console.WriteLine("cafex hoststop failed: Please install HostBridgeSetup and restart cafex_env.bat");
  545. return;
  546. }
  547. }
  548.  
  549. HOSTSTOP_RVAL.value = "0";
  550. HOSTSTOP_RETRIES.value = "0";
  551.  
  552. getbridgetype();
  553.  
  554. HOSTSTOP_RESET_OPTION.value = "-SMCResetHold";
  555. if (CAFE_HARDWARE.value.StartsWith("ev"))
  556. {
  557. HOSTSTOP_RESET_OPTION.value = "-hold";
  558. MION_PWR_SEQ.value = "FOFF1";
  559. }
  560.  
  561. if (BRIDGE_TYPE.value == "Mion")
  562. {
  563. checkbridgename.CheckName();
  564. if (args != null && args.Length > 0 && args[0] == "-makemine")
  565. {
  566. ToucanReset.Stop();
  567. FSEmul.makemine();
  568. }
  569. if (MION_PWR_SEQ.value == "smcreset")
  570. {
  571. int mionbutton_ret = MionButton.rsthold(args);
  572. HOSTSTOP_RVAL.value = mionbutton_ret.ToString();
  573. }
  574. else
  575. {
  576. if (MION_PWR_SEQ.value == "FOFF1")
  577. {
  578. int mionbutton_ret = MionButton.forceOff1(args);
  579. HOSTSTOP_RVAL.value = mionbutton_ret.ToString();
  580. }
  581. else
  582. {
  583. if (MION_PWR_SEQ.value == "G_SHUTDOWN")
  584. {
  585. int mionbutton_ret = MionButton.init_shutdown_w_FOFF2(args);
  586. HOSTSTOP_RVAL.value = mionbutton_ret.ToString();
  587. }
  588. else
  589. {
  590. HOSTSTOP_RVAL.value = "1";
  591. HOSTSTOP_RETRIES.value = "0";
  592. while (HOSTSTOP_RVAL.value != "0" && HexHandler.GetNumberFromString(HOSTSTOP_RETRIES.value) < 4)
  593. {
  594. if (HexHandler.GetNumberFromString(HOSTSTOP_RETRIES.value) > 0)
  595. {
  596. Console.WriteLine("WARNING: MionButton powerdown failed with err=" + HOSTSTOP_RVAL.value + ", will retry shortly, count=" + HOSTSTOP_RETRIES.value);
  597. Thread.Sleep(new TimeSpan(0,0,30));
  598. }
  599.  
  600. int mionbutton_ret = MionButton.forceOff2_off_single_cgi(args);
  601. HOSTSTOP_RVAL.value = mionbutton_ret.ToString();
  602. HOSTSTOP_RETRIES.value = (HexHandler.GetNumberFromString(HOSTSTOP_RETRIES.value) + 1).ToString();
  603. }
  604. }
  605. }
  606. }
  607.  
  608. if (HexHandler.GetNumberFromString(HOSTSTOP_RVAL.value) != 0)
  609. {
  610. Console.WriteLine("Gathering MION logs via telnet for the mionbutton return of " + HOSTSTOP_RVAL.value + ".");
  611. string logfile = TMP.value + "\\" + SESSION_PATH_PREFIX.value + "cafestop_mion.log";
  612. FileStream fs = new FileStream(logfile, FileMode.Create);
  613. StreamWriter sw = new StreamWriter(fs);
  614. sw.WriteLine("----------------------------------------------------------------");
  615. sw.WriteLine("----- MION telnet debug log @ " + DateTime.Now.ToLocalTime().ToString() + " -----");
  616. sw.WriteLine("----------------------------------------------------------------");
  617. string output = string.Empty;
  618. int miontelnet_ret = miontelnet.Reboot(out output);
  619. sw.Write(output);
  620. sw.Flush();
  621. sw.Close();
  622. Console.WriteLine(" See " + logfile + " for details.");
  623. }
  624. }
  625. else
  626. {
  627. int toucanreset_ret = ToucanReset.Reset();
  628. HOSTSTOP_RVAL.value = toucanreset_ret.ToString();
  629. }
  630.  
  631. ToucanReset.Stop();
  632.  
  633. if (HOSTSTOP_RVAL.value != "0")
  634. {
  635. if (BRIDGE_TYPE.value == "Mion")
  636. {
  637. if (MION_PWR_SEQ.value == "G_SHUTDOWN" && HOSTSTOP_RVAL.value == "1")
  638. {
  639. Console.WriteLine("cafex hoststop warning: Software shutdown failed.");
  640. HOSTSTOP_RVAL.value = "0";
  641. }
  642. else
  643. {
  644. Console.WriteLine("cafex hoststop failed: Could not stop cafe through " + BRIDGE_CURRENT_NAME.value);
  645. checkbridgename.CheckName_IPOnly();
  646. }
  647. }
  648. else
  649. {
  650. Console.WriteLine("cafex hoststop failed: Could not stop cafe through Toucan.");
  651. }
  652. }
  653. }
  654.  
  655. //Script function: getbridgetype
  656. static void getbridgetype()
  657. {
  658. #if DEBUG
  659. Log.WriteLine("getbridgetype started.");
  660. #endif
  661.  
  662. if (CAFE_HARDWARE.value == "ev")
  663. {
  664. BRIDGE_TYPE.value = "Toucan";
  665. }
  666. else if (CAFE_HARDWARE.value == "ev_x4")
  667. {
  668. BRIDGE_TYPE.value = "Mion";
  669. }
  670. else if (CAFE_HARDWARE.value.Substring(6, 2) == "mp")
  671. {
  672. BRIDGE_TYPE.value = "Mion";
  673. }
  674. else if (CAFE_HARDWARE.value.Substring(6) == "1" || CAFE_HARDWARE.value.Substring(6) == "2")
  675. {
  676. BRIDGE_TYPE.value = "Toucan";
  677. }
  678. else
  679. {
  680. BRIDGE_TYPE.value = "Mion";
  681. }
  682. #if DEBUG
  683. Log.WriteLine("BRIDGE_TYPE set to = " + BRIDGE_TYPE.value);
  684. #endif
  685.  
  686. }
  687.  
  688. //Script function: caferun
  689. static CAFEX_ERROR run(string[] args)
  690. {
  691. #if DEBUG
  692. Log.WriteLine("run started.");
  693. string argString = null;
  694. if (args != null)
  695. {
  696. foreach (string arg in args)
  697. {
  698. argString += arg + " ";
  699. }
  700. }
  701. Log.WriteLine("Arguments=" + argString);
  702.  
  703. #endif
  704. #region setup
  705.  
  706. // This tells if the current call contains wumad parameters.
  707. bool isWumadExecution = false;
  708. PLATFORM.value = "cafe";
  709.  
  710. QUIET.value = "-q";
  711. CAFESTOP_ONLY_IF_FSEMUL.value = "0";
  712. PPC_OS_FLAGS.value = "0";
  713. PPC_APP_FLAGS.value = "0";
  714. PCFS_SYNC_DATE.value = "";
  715. MCP_LAUNCH_HINT.value = "";
  716. MCP_INFO_OPTION.value = "";
  717. if (DEBUGGER.value == null)
  718. {
  719. DEBUGGER.value = "none";
  720. }
  721.  
  722. if (CAFE_CONSOLE.value == null)
  723. {
  724. CONSOLE.value = "cattoucan";
  725. }
  726. else
  727. {
  728. CONSOLE.value = CAFE_CONSOLE.value;
  729. }
  730.  
  731. if (CAFE_DEBUG_PORT.value == null)
  732. {
  733. CAFE_DEBUG_PORT.value = "toucan";
  734. }
  735.  
  736. if (DEBUG_ELF_FILE.value == null)
  737. {
  738. DEBUG_ELF_FILE.value = "";
  739. }
  740.  
  741. CAFERUN_OPTION_SOFT_RESTART.value = "0";
  742. CAFERUN_OPTION_MLC_EMU_LAUNCH.value = "0";
  743. CAFERUN_OPTION_MLC_EMU_TITLEID.value = "0xFFFFFFFFFFFFFFFF";
  744. string sdk_ver_file = CAFE_ROOT.value + "\\system\\include\\sdk_ver.h";
  745. if (!File.Exists(sdk_ver_file))
  746. {
  747. Console.WriteLine("cafex run error: sdk_ver.h does not exist: " + sdk_ver_file);
  748. return CAFEX_ERROR.RUN_NO_SDK_VERSION_HEADER;
  749. }
  750.  
  751. FileStream fs = new FileStream(CAFE_ROOT.value + "\\system\\include\\sdk_ver.h", FileMode.Open, FileAccess.Read);
  752. StreamReader sr = new StreamReader(fs);
  753. bool found_line = false;
  754. while (!sr.EndOfStream)
  755. {
  756. string line = sr.ReadLine();
  757. if (line.Contains("00050010"))
  758. {
  759. int index = line.IndexOf("00050010");
  760. string version = line.Substring(index, 16);
  761. CAFERUN_OS_MAJOR_VERSION_LO.value = version.Substring(8, 8);
  762. CAFERUN_OS_MAJOR_VERSION.value = version.Substring(14, 2);
  763. if (CAFERUN_COLDBOOT_OS_VERSION.value == null)
  764. CAFERUN_COLDBOOT_OS_VERSION.value = "0x00050010100040" + CAFERUN_OS_MAJOR_VERSION.value;
  765. else
  766. CAFERUN_COLDBOOT_OS_VERSION.value = "0x" + CAFERUN_COLDBOOT_OS_VERSION.value;
  767. found_line = true;
  768. break;
  769. }
  770. }
  771. sr.Close();
  772. if (!found_line)
  773. {
  774. Console.WriteLine("cafex run error: couldn't find sdk version in sdk_ver.h");
  775. return CAFEX_ERROR.RUN_COULDNT_PARSE_SDK_VERSION_HEADER;
  776. }
  777. #if DEBUG
  778. Log.WriteLine("CAFERUN_COLDBOOT_OS_VERSION set to '" + CAFERUN_COLDBOOT_OS_VERSION.value + "' from file " + CAFE_ROOT.value + "\\system\\include\\sdk_ver.h");
  779. #endif
  780. HEARTBEAT_DISABLE.value = "0";
  781. PCFS_DIR_MAPPING.value = "";
  782. MAPDIR_MLC.value = "";
  783. MAPDIR_SLC.value = "";
  784. MAPDIR_CODE.value = "";
  785. MAPDIR_META.value = "";
  786. MAPDIR_CONTENT.value = "";
  787. MAPDIR_SAVE.value = "";
  788. PCFS_LOG_TIMESTAMP.value = "";
  789. PCFS_SRV_LOG_OUTPUT.value = "";
  790. RUN_FROM_HDD_BANK.value = "";
  791. BRIDGE_PARAMETERS.value = "";
  792. PCFSSERVER_PARAMETERS.value = "";
  793. PCFS_OVER_SATA_PORT.value = "";
  794. if (PCFS_HEADLESS_EMUL.value == null)
  795. {
  796. PCFS_HEADLESS_EMUL.value = "-pcfsenable";
  797. }
  798.  
  799. PREPHDD_STAT.value = "";
  800. DISC_EMU_TID.value = "";
  801. SYSTEM_MODE.value = "";
  802. RPL_DIRS.value = "";
  803. RPL_FILES.value = "";
  804.  
  805. if (CAFERUN_OPTION_NO_DATA_SYNC.value == null)
  806. {
  807. CAFERUN_OPTION_NO_DATA_SYNC.value = "0";
  808. }
  809.  
  810. if (command != "on" && command != "setbootmode" && CAFE_BOOT_MODE.value == "NAND")
  811. {
  812. CAFERUN_OPTION_SOFT_LAUNCH.value = "1";
  813. }
  814. else
  815. {
  816. CAFERUN_OPTION_SOFT_LAUNCH.value = "0";
  817. }
  818. #if DEBUG
  819. Log.WriteLine("CAFERUN_OPTION_SOFT_LAUNCH set to " + CAFERUN_OPTION_SOFT_LAUNCH.value);
  820. #endif
  821.  
  822. CAFERUN_HOSTBRIDGE_VERSION.value = "unknown";
  823. CAFERUN_FW_VERSION.value = "unknown";
  824.  
  825. if (CAFE_DEBUG_INIT.value == null)
  826. {
  827. CAFE_DEBUG_INIT.value = "0";
  828. }
  829.  
  830. if (CAFE_DEBUG_PREINIT.value == null)
  831. {
  832. CAFE_DEBUG_PREINIT.value = "0";
  833. }
  834.  
  835. if (CAFE_DEBUG_DONTSTOP.value == null)
  836. {
  837. CAFE_DEBUG_DONTSTOP.value = "0";
  838. }
  839.  
  840. if (CAFE_LAUNCH_DEBUG.value == null)
  841. {
  842. CAFE_LAUNCH_DEBUG.value = "1";
  843. }
  844.  
  845. if (CAFE_SYSLOG_LEVEL.value == null)
  846. {
  847. CAFE_SYSLOG_LEVEL.value = "1";
  848. }
  849.  
  850. if (CAFE_RUN_FORCERESTART.value == null)
  851. {
  852. CAFE_RUN_FORCERESTART.value = "0";
  853. }
  854.  
  855. if (DEBUG_PROC_ID.value == null)
  856. {
  857. DEBUG_PROC_ID.value = "15";
  858. }
  859.  
  860. DEBUG_FLAGS.value = "0";
  861.  
  862. if (CAFE_DEBUG_INIT.value != "0")
  863. {
  864. DEBUG_FLAGS.value = "16";
  865. }
  866.  
  867. if (CAFE_DEBUG_DONTSTOP.value != "0")
  868. {
  869. DEBUG_FLAGS.value = "32";
  870. }
  871.  
  872. if (CAFE_DEBUG_PREINIT.value != "0")
  873. {
  874. DEBUG_FLAGS.value = "64";
  875. }
  876.  
  877. CATOUTPUT.value = "0";
  878. CMDOUTPUT.value = "0";
  879.  
  880. switch (CONSOLE.value)
  881. {
  882. case "sdio":
  883. {
  884. CONSOLE.value = "toucan";
  885. break;
  886. }
  887. case "cattoucan":
  888. {
  889. CONSOLE.value = "toucan";
  890. DEBUGGER.value = "cattoucan";
  891. CATOUTPUT.value = "1";
  892. break;
  893. }
  894. case "cmdtoucan":
  895. {
  896. CONSOLE.value = "toucan";
  897. DEBUGGER.value = "cattoucan";
  898. CMDOUTPUT.value = "1";
  899. break;
  900. }
  901. }
  902.  
  903. if (CAFE_AUTOTEST.value == "1")
  904. {
  905. DEBUGGER.value = "none";
  906. }
  907.  
  908. #if DEBUG
  909. Log.WriteLine("CONSOLE = " + CONSOLE.value);
  910. Log.WriteLine("DEBUGGER = " + DEBUGGER.value);
  911. Log.WriteLine("CATOUTPUT= " + CATOUTPUT.value);
  912. Log.WriteLine("CMDOUTPUT= " + CMDOUTPUT.value);
  913. #endif
  914. #endregion
  915.  
  916. int rpx_index = args.Length;
  917. for (int i = 0; i < args.Length; ++i)
  918. {
  919. if (args[i].ToLowerInvariant().Contains(".rpx") || args[i].ToLowerInvariant().Contains(".elf"))
  920. {
  921. // Ensure the RPX is not the argument of an option
  922. if ( i > 0 &&
  923. ( args[i - 1].ToLowerInvariant().Trim().Equals("-k") ||
  924. args[i - 1].ToLowerInvariant().Trim().Equals("-l") ||
  925. args[i - 1].ToLowerInvariant().Trim().Equals("-e")))
  926. {
  927. // Exceptions: If ELF/RPX is argument of "-k" or "-l".
  928. continue;
  929. }
  930. else
  931. {
  932. // Default: ELF/RPX found is the one to run.
  933. rpx_index = i;
  934. break;
  935. }
  936. }
  937. }
  938.  
  939. #if DEBUG
  940. Log.WriteLine("Search for RPX index returned index = " + rpx_index.ToString());
  941. #endif
  942.  
  943. #region arguments
  944.  
  945. for (int i = 0; i < rpx_index; ++i)
  946. {
  947. switch (args[i])
  948. {
  949. case "-m":
  950. {
  951. Console.WriteLine("cafex run failed: memmap not supported");
  952. run_usage();
  953. return CAFEX_ERROR.RUN_MEMMAP_NOT_SUPPORTED;
  954. }
  955.  
  956. case "-g":
  957. {
  958. if (!ArgumentChecks.IsArgumentValidSetting(args, i + 1))
  959. {
  960. run_usage();
  961. return CAFEX_ERROR.BAD_ARGUMENT;
  962. }
  963.  
  964. int prev_value = HexHandler.GetNumberFromString(PPC_OS_FLAGS.value);
  965. int arg_value = HexHandler.GetNumberFromString(args[i + 1]);
  966. int new_value = prev_value | arg_value;
  967. PPC_OS_FLAGS.value = new_value.ToString();
  968. ++i;
  969. break;
  970. }
  971.  
  972. case "-p":
  973. {
  974. if (!ArgumentChecks.IsArgumentValidSetting(args, i + 1))
  975. {
  976. run_usage();
  977. return CAFEX_ERROR.BAD_ARGUMENT;
  978. }
  979.  
  980. DEBUG_PROC_ID.value = args[i + 1];
  981. ++i;
  982. break;
  983. }
  984.  
  985. case "-d":
  986. {
  987. if (!ArgumentChecks.IsArgumentValidSetting(args, i + 1))
  988. {
  989. run_usage();
  990. return CAFEX_ERROR.BAD_ARGUMENT;
  991. }
  992.  
  993. DEBUGGER.value = args[i + 1];
  994. int debug_flags = HexHandler.GetNumberFromString(DEBUG_FLAGS.value);
  995. int debug_proc_id = HexHandler.GetNumberFromString(DEBUG_PROC_ID.value);
  996. int new_value = debug_flags | (debug_proc_id & 0xf);
  997. DEBUG_FLAGS.value = new_value.ToString();
  998. CAFE_RUN_FORCERESTART.value = "1";
  999. ++i;
  1000. break;
  1001. }
  1002.  
  1003. case "-k":
  1004. {
  1005. if (!ArgumentChecks.IsArgumentValidSetting(args, i + 1))
  1006. {
  1007. run_usage();
  1008. return CAFEX_ERROR.BAD_ARGUMENT;
  1009. }
  1010.  
  1011. DEBUG_ELF_FILE.value = args[i + 1];
  1012. ++i;
  1013. break;
  1014. }
  1015.  
  1016. case "-w":
  1017. {
  1018. if (!ArgumentChecks.IsArgumentValidSetting(args, i + 1))
  1019. {
  1020. run_usage();
  1021. return CAFEX_ERROR.BAD_ARGUMENT;
  1022. }
  1023.  
  1024. int prev_value = HexHandler.GetNumberFromString(PPC_OS_FLAGS.value);
  1025. int arg_value = 0;
  1026. try
  1027. {
  1028. arg_value = HexHandler.GetNumberFromString(args[i + 1]);
  1029. }
  1030. catch (FormatException)
  1031. {
  1032. Console.WriteLine("cafex : argument for -w option is not a valid number and will be ignored.");
  1033. }
  1034.  
  1035. int new_value = prev_value | arg_value;
  1036. PPC_OS_FLAGS.value = new_value.ToString();
  1037. ++i;
  1038. break;
  1039. }
  1040.  
  1041. case "-a":
  1042. {
  1043. if (!ArgumentChecks.ArgumentExists(args, i + 1))
  1044. {
  1045. run_usage();
  1046. return CAFEX_ERROR.BAD_ARGUMENT;
  1047. }
  1048.  
  1049. DASH_A_ARGS.value = args[i + 1];
  1050. ++i;
  1051. break;
  1052. }
  1053.  
  1054. case "-o":
  1055. {
  1056. if (!ArgumentChecks.IsArgumentValidSetting(args, i + 1))
  1057. {
  1058. run_usage();
  1059. return CAFEX_ERROR.BAD_ARGUMENT;
  1060. }
  1061.  
  1062. CAFERUN_COLDBOOT_OS_VERSION.value = args[i + 1];
  1063. ++i;
  1064. break;
  1065. }
  1066.  
  1067. case "-b":
  1068. {
  1069. CAFERUN_COLDBOOT_OS_VERSION.value = "0x00050010100080" + CAFERUN_OS_MAJOR_VERSION.value;
  1070. break;
  1071. }
  1072.  
  1073. case "-u":
  1074. {
  1075. CAFERUN_COLDBOOT_OS_VERSION.value = "0x000500101000C0" + CAFERUN_OS_MAJOR_VERSION.value;
  1076. break;
  1077. }
  1078.  
  1079. case "-n":
  1080. {
  1081. int prev_value = HexHandler.GetNumberFromString(PPC_OS_FLAGS.value);
  1082. int new_value = prev_value | 0x02000;
  1083. PPC_OS_FLAGS.value = new_value.ToString();
  1084. break;
  1085. }
  1086.  
  1087. case "-i":
  1088. {
  1089. int prev_value = HexHandler.GetNumberFromString(DEBUG_FLAGS.value);
  1090. int new_value = prev_value | 16;
  1091. DEBUG_FLAGS.value = new_value.ToString();
  1092. break;
  1093. }
  1094.  
  1095. case "-R":
  1096. {
  1097. int prev_value = HexHandler.GetNumberFromString(DEBUG_FLAGS.value);
  1098. int new_value = prev_value | 32;
  1099. DEBUG_FLAGS.value = new_value.ToString();
  1100. break;
  1101. }
  1102.  
  1103. case "-j":
  1104. {
  1105. int prev_value = HexHandler.GetNumberFromString(DEBUG_FLAGS.value);
  1106. int new_value = prev_value | 64;
  1107. DEBUG_FLAGS.value = new_value.ToString();
  1108. break;
  1109. }
  1110.  
  1111. case "-q":
  1112. {
  1113. CAFE_SYSLOG_LEVEL.value = "0";
  1114. break;
  1115. }
  1116.  
  1117. case "-v":
  1118. {
  1119. if (!ArgumentChecks.IsArgumentValidSetting(args, i + 1))
  1120. {
  1121. run_usage();
  1122. return CAFEX_ERROR.BAD_ARGUMENT;
  1123. }
  1124.  
  1125. CAFE_SYSLOG_LEVEL.value = args[i + 1];
  1126. ++i;
  1127. break;
  1128. }
  1129.  
  1130. case "-e":
  1131. {
  1132. if (!ArgumentChecks.ArgumentExists(args, i + 1))
  1133. {
  1134. run_usage();
  1135. return CAFEX_ERROR.BAD_ARGUMENT;
  1136. }
  1137.  
  1138. if (args[i + 1].StartsWith("mcp"))
  1139. {
  1140. if (args[i + 1].Length < 4)
  1141. {
  1142. return CAFEX_ERROR.BAD_ARGUMENT;
  1143. }
  1144.  
  1145. string new_val = args[i + 1].Substring(4);
  1146. MCP_INFO_OPTION.value = new_val;
  1147. if (new_val.StartsWith("launch_hint"))
  1148. {
  1149. if (new_val.Length < 12)
  1150. {
  1151. return CAFEX_ERROR.BAD_ARGUMENT;
  1152. }
  1153.  
  1154. MCP_LAUNCH_HINT.value = new_val.Substring(12);
  1155. }
  1156. }
  1157. else if (args[i + 1] == "nopcfs")
  1158. {
  1159. Console.WriteLine("cafex run: Emulating PCFS Headless Mode.");
  1160. PCFS_HEADLESS_EMUL.value = "-pcfsdisable";
  1161. }
  1162. else if (args[i + 1] == "noFFIO")
  1163. {
  1164. // This is the designation for PCFSServer to disable Fast File IO
  1165. Console.WriteLine("cafex run: Disabling PCFS Fast File IO.");
  1166. PCFSSERVER_PARAMETERS.AddToVar("-noFFIO", ' ');
  1167. }
  1168. else if (args[i + 1].ToLowerInvariant().StartsWith("wumad"))
  1169. {
  1170. if (args[i + 1].Length < 6)
  1171. {
  1172. return CAFEX_ERROR.BAD_ARGUMENT;
  1173. }
  1174.  
  1175. CAFE_WUMAD.value = PathConverter.Windowsify(args[i + 1].Substring(6));
  1176. BRIDGE_PARAMETERS.AddToVar("-sata", ' ');
  1177. BRIDGE_PARAMETERS_WITH_E.AddToVar("-e sata", ' ');
  1178. CAFE_RUN_FORCERESTART.value = "1";
  1179. isWumadExecution = true;
  1180. }
  1181. else if (args[i + 1].ToLowerInvariant().StartsWith("soft_launch_cafe_elf"))
  1182. {
  1183. // This is the CAFE_ELF of the top level execution
  1184. // that invoked this run to power on. This is ued
  1185. // to create the disk image.
  1186. CAFERUN_OPTION_CALLED_FROM_CAFEDISCRUN.value = "yes";
  1187. SOFT_LAUNCH_CAFE_ELF.value = args[i + 1].Substring(21);
  1188. }
  1189. else
  1190. {
  1191. BRIDGE_PARAMETERS.AddToVar("-" + args[i + 1], ' ');
  1192. BRIDGE_PARAMETERS_WITH_E.AddToVar("-e " + args[i + 1], ' ');
  1193.  
  1194. if (args[i + 1].StartsWith("h"))
  1195. {
  1196. if (args[i + 1].Length < 2)
  1197. {
  1198. return CAFEX_ERROR.BAD_ARGUMENT;
  1199. }
  1200.  
  1201. RUN_FROM_HDD_BANK.value = args[i + 1].Substring(2);
  1202. }
  1203. if (args[i + 1].StartsWith("pcfsport"))
  1204. {
  1205. if (args[i + 1].Length < 9)
  1206. {
  1207. return CAFEX_ERROR.BAD_ARGUMENT;
  1208. }
  1209.  
  1210. PCFS_OVER_SATA_PORT.value = args[i + 1].Substring(9);
  1211. USE_PCFS_OVER_SATA.value = "1";
  1212. }
  1213. if (args[i + 1].StartsWith("sdio"))
  1214. {
  1215. // If the Host Bridge does not support this option, remove it
  1216. // This option was implemented with HB 3.2.4.4
  1217.  
  1218. CAFEX_ERROR hostcheckversion_ret = hostcheckversion();
  1219. if(hostcheckversion_ret != CAFEX_ERROR.OK)
  1220. return hostcheckversion_ret;
  1221.  
  1222. // 3024004 is the computed flat version of 3.2.4.4.
  1223. // Yes, this is a bad way to compute the flat version.
  1224. if (compute_flat_version(CAFERUN_HOSTBRIDGE_VERSION.value) < 3024004)
  1225. {
  1226. BRIDGE_PARAMETERS.RemoveFromVar("-sdio", ' ');
  1227. BRIDGE_PARAMETERS_WITH_E.RemoveFromVar("-sdio", ' ');
  1228. }
  1229. }
  1230. }
  1231.  
  1232. ++i;
  1233. break;
  1234. }
  1235.  
  1236. case "-c":
  1237. {
  1238. DEBUGGER.value = "cattoucan";
  1239. break;
  1240. }
  1241.  
  1242. case "-f":
  1243. {
  1244. CAFESTOP_ONLY_IF_FSEMUL.value = "1";
  1245. break;
  1246. }
  1247.  
  1248. case "-s":
  1249. {
  1250. CAFERUN_OPTION_SOFT_LAUNCH.value = "1";
  1251. break;
  1252. }
  1253.  
  1254. case "-r":
  1255. {
  1256. CAFERUN_OPTION_SOFT_RESTART.value = "1";
  1257. break;
  1258. }
  1259.  
  1260. case "-F":
  1261. {
  1262. CAFE_RUN_FORCERESTART.value = "1";
  1263. break;
  1264. }
  1265.  
  1266. case "-t":
  1267. {
  1268. if (!ArgumentChecks.IsArgumentValidSetting(args, i + 1))
  1269. {
  1270. run_usage();
  1271. return CAFEX_ERROR.BAD_ARGUMENT;
  1272. }
  1273.  
  1274. if ((args[i + 1].Length == 18 && args[i + 1].StartsWith("0x")) || args[i + 1].Length == 16)
  1275. {
  1276. CAFERUN_OPTION_MLC_EMU_LAUNCH.value = "1";
  1277. CAFERUN_OPTION_MLC_EMU_TITLEID.value = (args[i + 1].Length == 16) ? "0x" + args[i + 1] : args[i + 1];
  1278. }
  1279. else
  1280. {
  1281. Console.WriteLine("Title ID has incorrect length.");
  1282. Console.WriteLine("A correct ID is 18 characters plus 0x in the beginning or 16 without 0x.");
  1283. Console.WriteLine("Ex: 0x1234567812345678 or 1234567812345678");
  1284. return CAFEX_ERROR.RUN_BAD_TITLE_ID;
  1285. }
  1286. ++i;
  1287. break;
  1288. }
  1289.  
  1290. case "-l":
  1291. {
  1292. if (!ArgumentChecks.IsArgumentValidSetting(args, i + 1))
  1293. {
  1294. run_usage();
  1295. return CAFEX_ERROR.BAD_ARGUMENT;
  1296. }
  1297.  
  1298. if (!args[i + 1].EndsWith(".rpl"))
  1299. {
  1300. // CafeX addToVar does not use space as delimiter for the list, so there is no need to replace spaces with "|".
  1301. RPL_DIRS.AddToVar(args[i + 1], DEFAULT_DELIMITER);
  1302. }
  1303. else
  1304. {
  1305. // CafeX addToVar does not use space as delimiter for the list, so there is no need to replace spaces with "|".
  1306. RPL_FILES.AddToVar(args[i + 1], DEFAULT_DELIMITER);
  1307. }
  1308. ++i;
  1309. break;
  1310. }
  1311.  
  1312. case "-h":
  1313. {
  1314. run_usage();
  1315. return CAFEX_ERROR.OK;
  1316. }
  1317.  
  1318. case "-H":
  1319. {
  1320. if (!ArgumentChecks.IsArgumentValidSetting(args, i + 1))
  1321. {
  1322. run_usage();
  1323. return CAFEX_ERROR.BAD_ARGUMENT;
  1324. }
  1325.  
  1326. if (args[i + 1] == "disable")
  1327. {
  1328. HEARTBEAT_DISABLE.value = "0x00020000";
  1329. }
  1330. else if (args[i + 1] == "enable")
  1331. {
  1332. HEARTBEAT_DISABLE.value = "0";
  1333. }
  1334. else
  1335. {
  1336. run_usage();
  1337. return CAFEX_ERROR.RUN_BAD_H_ARGUMENT;
  1338. }
  1339. ++i;
  1340. break;
  1341. }
  1342.  
  1343. case "-x":
  1344. {
  1345. BRIDGE_PARAMETERS.AddToVar("-sata", ' ');
  1346. break;
  1347. }
  1348.  
  1349. case "-z":
  1350. {
  1351. if (!Directory.Exists(CAFE_DATA_DIR.value))
  1352. {
  1353. Console.WriteLine("Session data directory is missing and needs to be sync'd!");
  1354. Console.WriteLine(" Please re-run without the '-z' option.");
  1355. return CAFEX_ERROR.RUN_NO_CAFE_DATA_DIR;
  1356. }
  1357.  
  1358. CAFERUN_OPTION_NO_DATA_SYNC.value = "1";
  1359. break;
  1360. }
  1361.  
  1362. case "-A":
  1363. {
  1364. Console.WriteLine("*** -A option is deprecated and has no effect. ATFS is now the default file system. ***");
  1365. break;
  1366. }
  1367.  
  1368. case "-D":
  1369. {
  1370. Console.WriteLine("cafex run failed: -D option is deprecated. DVDFS is now unsupported.");
  1371. run_usage();
  1372. return CAFEX_ERROR.RUN_D_OPTION_USED;
  1373. }
  1374.  
  1375. case "-M":
  1376. {
  1377. if (!ArgumentChecks.IsArgumentValidSetting(args, i + 1))
  1378. {
  1379. run_usage();
  1380. return CAFEX_ERROR.BAD_ARGUMENT;
  1381. }
  1382.  
  1383. SYSTEM_MODE.value = args[i + 1];
  1384. ++i;
  1385. break;
  1386. }
  1387.  
  1388. default:
  1389. {
  1390. Console.WriteLine("cafex run failed: invalid option: " + args[i]);
  1391. run_usage();
  1392. return CAFEX_ERROR.RUN_INVALID_OPTION;
  1393. }
  1394. }
  1395. }
  1396.  
  1397. #endregion
  1398.  
  1399. // Wumad override: if "-e sata" was not provided and "-e wumad" was, sata bridge parameters should be added.
  1400. if (!string.IsNullOrEmpty(CAFE_WUMAD.value) &&
  1401. (string.IsNullOrEmpty(BRIDGE_PARAMETERS.value) || !BRIDGE_PARAMETERS.value.Contains("-sata")) &&
  1402. (string.IsNullOrEmpty(BRIDGE_PARAMETERS_WITH_E.value) || !BRIDGE_PARAMETERS_WITH_E.value.Contains("-e sata")))
  1403. {
  1404. BRIDGE_PARAMETERS.AddToVar("-sata", ' ');
  1405. BRIDGE_PARAMETERS_WITH_E.AddToVar("-e sata", ' ');
  1406. }
  1407.  
  1408. // Remove arguments before the .rpx
  1409. string[] new_args = new string[args.Length - rpx_index];
  1410. Array.Copy(args, rpx_index, new_args, 0, new_args.Length);
  1411. args = new_args;
  1412.  
  1413. //if (args.Length == 0)
  1414. //{
  1415. // Console.WriteLine("cafex run failed: No arguments specified.");
  1416. // return CAFEX_ERROR.RUN_NO_ARGUMENTS;
  1417. //}
  1418.  
  1419. string catdev_sdk_ver = SDK_MAJ_VER.value + "." + SDK_MIN_VER.value + "." + SDK_MIC_VER.value;
  1420. string sdk_ver = SDK_VER.value;
  1421. if (catdev_sdk_ver != SDK_VER.value)
  1422. {
  1423. Console.WriteLine("The CAT-DEV SDK version is not the same as the SDK you are running.");
  1424. Console.WriteLine("SDK Version: " + sdk_ver + " CAT-DEV SDK Version: " + catdev_sdk_ver);
  1425. Console.WriteLine("Please run cafeupdate to get in sync.");
  1426. }
  1427.  
  1428. if (PCFS_OVER_SATA_PORT.value == null)
  1429. {
  1430. if (USE_PCFS_OVER_SATA.value == "1")
  1431. {
  1432. PCFS_OVER_SATA_PORT.value = SESSION_PCFS_SATA_PORT.value;
  1433. BRIDGE_PARAMETERS.AddToVar("-pcfsport:" + SESSION_PCFS_SATA_PORT.value, ' ');
  1434.  
  1435. Console.WriteLine("cafex run: using PCFS over SATA on port " + PCFS_OVER_SATA_PORT.value);
  1436. }
  1437. else
  1438. {
  1439. Console.WriteLine("cafex run: not using PCFS over SATA");
  1440. }
  1441. }
  1442.  
  1443. if (PCFS_OVER_SATA_PORT.value != null)
  1444. {
  1445. PCFSSERVER_PARAMETERS.AddToVar("-c " + PCFS_OVER_SATA_PORT.value, ' ');
  1446. }
  1447. else
  1448. {
  1449. Console.WriteLine("cafex run: PCFS_OVER_SATA_PORT is not set.");
  1450. }
  1451.  
  1452. if (PCFSSERVER_PARAMETERS.value != null)
  1453. {
  1454. Console.WriteLine("cafex run: PCFSServer parameters " + PCFSSERVER_PARAMETERS.value);
  1455. }
  1456.  
  1457. if (BRIDGE_PARAMETERS.value == null || !BRIDGE_PARAMETERS.value.Contains("-h"))
  1458. {
  1459. if (BRIDGE_PARAMETERS.value == null || !BRIDGE_PARAMETERS.value.Contains("-em"))
  1460. {
  1461. BRIDGE_PARAMETERS.AddToVar("-em", ' ');
  1462. }
  1463. }
  1464.  
  1465. DISC_EMU_TYPE.value = "hfio";
  1466. if (CAFERUN_OPTION_CALLED_FROM_CAFEDISCRUN.value == "yes")
  1467. {
  1468. if (RUN_FROM_HDD_BANK.value != null)
  1469. {
  1470. DISC_EMU_TYPE.value = "hdd";
  1471. Console.WriteLine("cafex run: Will launch HDD bank " + RUN_FROM_HDD_BANK.value);
  1472. }
  1473. else if (!string.IsNullOrEmpty(CAFE_WUMAD.value))
  1474. {
  1475. // wumad emulation started from cafediscrun
  1476. DISC_EMU_TYPE.value = "hdd";
  1477. }
  1478. else if (CAFERUN_OPTION_SOFT_LAUNCH.value == "1")
  1479. {
  1480. ALLOW_DISCRUN_IN_SOFTLAUNCH.value = "1";
  1481. DISC_EMU_TYPE.value = "blockemu";
  1482. BRIDGE_PARAMETERS.AddToVar("-em", ' '); //Tell FSEmul to use block emulation explicitly
  1483. }
  1484. else
  1485. {
  1486. DISC_EMU_TYPE.value = "blockemu";
  1487.  
  1488. if (BRIDGE_PARAMETERS.value == null || !BRIDGE_PARAMETERS.value.Contains("-em"))
  1489. {
  1490. BRIDGE_PARAMETERS.AddToVar("-em", ' ');
  1491. }
  1492. }
  1493. }
  1494. else if (!string.IsNullOrEmpty(CAFE_WUMAD.value))
  1495. {
  1496. // wumad emulation started from caferun
  1497. DISC_EMU_TYPE.value = "hdd";
  1498. }
  1499. else
  1500. {
  1501. // default
  1502. DISC_EMU_TYPE.value = "hfio";
  1503. if (CAFE_BOOT_MODE.value == "NAND")
  1504. {
  1505. if (SYSTEM_MODE.value == "test")
  1506. {
  1507. Console.WriteLine("cafex run failed: Test mode not supported on NAND.");
  1508. return CAFEX_ERROR.RUN_TEST_MODE_WITH_NAND_BOOT;
  1509. }
  1510. Console.WriteLine("cafex run: Will use NAND file system");
  1511. }
  1512. else
  1513. {
  1514. Console.WriteLine("cafex run: Will use PCFS file system emulation");
  1515. }
  1516. if (RUN_FROM_HDD_BANK.value != null)
  1517. {
  1518. Console.WriteLine("cafex run: HDD bank " + RUN_FROM_HDD_BANK.value + " will be attached to odd");
  1519. }
  1520. }
  1521.  
  1522. Console.WriteLine("cafex run: bridge parameters: " + BRIDGE_PARAMETERS.value);
  1523. // Console.WriteLine("cafex run: bridge parameters with -e: " + BRIDGE_PARAMETERS_WITH_E.value);
  1524.  
  1525. long os_version = HexHandler.GetLongNumberFromString(CAFERUN_COLDBOOT_OS_VERSION.value);
  1526. CAFERUN_COLDBOOT_OS_VERSION.value = os_version.ToString("X").PadLeft(16, '0');
  1527.  
  1528. HOSTSTOP_RVAL.value = "0";
  1529. Process[] fsEmulProcesses = Process.GetProcessesByName("FSEmul");
  1530. if (CAFESTOP_ONLY_IF_FSEMUL.value == "0" || (fsEmulProcesses != null && fsEmulProcesses.Length > 0))
  1531. {
  1532. stop(null);
  1533.  
  1534. if (HOSTSTOP_RVAL.value != "0")
  1535. {
  1536. Console.WriteLine("cafex run failed: Please check the device is connected, powered on, and not in use by another host PC.");
  1537. return CAFEX_ERROR.RUN_HOSTSTOP_RVAL_NON_ZERO;
  1538. }
  1539. }
  1540.  
  1541. CAFEX_ERROR ret = hostcheckversion();
  1542. if (ret != CAFEX_ERROR.OK)
  1543. {
  1544. return ret;
  1545. }
  1546.  
  1547. CAFE_SYSLOG_LEVEL.value = (HexHandler.GetNumberFromString(CAFE_SYSLOG_LEVEL.value) & 7).ToString();
  1548. PPC_OS_FLAGS.value = (HexHandler.GetNumberFromString(PPC_OS_FLAGS.value) | HexHandler.GetNumberFromString(HEARTBEAT_DISABLE.value) | HexHandler.GetNumberFromString(DEBUG_FLAGS.value) | (HexHandler.GetNumberFromString(CAFE_SYSLOG_LEVEL.value) << 9)).ToString();
  1549. PPC_APP_FLAGS.value = ((HexHandler.GetNumberFromString(PPC_OS_FLAGS.value) & 0x00000e70) | ((HexHandler.GetNumberFromString(PPC_OS_FLAGS.value) >> 7) & 1) | Convert.ToInt32(((HexHandler.GetNumberFromString(PPC_OS_FLAGS.value) & 0xf) != 0))).ToString();
  1550.  
  1551. SYSTEM_XML.value = CAFE_SLC_DIR.value + "\\sys\\config\\system.xml";
  1552.  
  1553. if (TEMP.value == null)
  1554. {
  1555. Console.WriteLine("cafex run failed: TEMP environment variable is not set.");
  1556. return CAFEX_ERROR.RUN_NO_TEMP_VAR;
  1557. }
  1558. else
  1559. {
  1560. CAFERUN_WORK_DIR.value = TEMP.value + "\\" + SESSION_PATH_PREFIX.value + "caferun";
  1561. }
  1562. create_directory_if_noexist(CAFERUN_WORK_DIR.value);
  1563.  
  1564. if (SESSION_MANAGER.value == "1")
  1565. {
  1566. Console.WriteLine("cafex run: Using CAFE_CONTENT_DIR = " + CAFE_CONTENT_DIR.value);
  1567. if (CAFERUN_OPTION_NO_DATA_SYNC.value == "0" && CAFERUN_OPTION_SOFT_RESTART.value == "0")
  1568. {
  1569. syncsession();
  1570. CAFERUN_OPTION_NO_DATA_SYNC.value = "1";
  1571. }
  1572. }
  1573.  
  1574. if (CAFERUN_OPTION_SOFT_RESTART.value == "0")
  1575. {
  1576. create_directory_if_noexist(CAFE_SLC_DIR.value + "\\sys");
  1577. create_directory_if_noexist(CAFE_MLC_DIR.value);
  1578.  
  1579. if (args.Length != 0 && args[0] != null)
  1580. {
  1581. if (DISC_EMU_TYPE.value == "hdd")
  1582. {
  1583. Console.WriteLine("cafex run: Specified rpx/elf file is used for setting title id from app.xml only");
  1584. }
  1585.  
  1586. CAFE_ELF.value = PathConverter.Windowsify(args[0]);
  1587. CAFE_ELF.value = PathConverter.Root(CAFE_ELF.value);
  1588.  
  1589. if (SESSION_MANAGER.value == "1")
  1590. {
  1591. TMP_SUB_DIR.value = Path.GetFileNameWithoutExtension(args[0]);
  1592. string CAFE_DATA_TMP_DOS = CAFE_DATA_TMP.value + "\\" + TMP_SUB_DIR.value;
  1593. create_directory_if_noexist(CAFE_DATA_TMP_DOS);
  1594.  
  1595. CAFE_ELF_SRC_DIR.value = Path.GetDirectoryName(CAFE_ELF.value);
  1596.  
  1597. if (!string.Equals(CAFE_ELF_SRC_DIR.value, CAFE_DATA_TMP_DOS, StringComparison.InvariantCultureIgnoreCase))
  1598. {
  1599. // Don't sync if we are running from the data/tmp folder as this is most-likely
  1600. // a restart from the Multi debugger. If not, the files are still ok as they were
  1601. // from a previous sync.
  1602. //
  1603. SYNCTOOL_CFG.value = CAFE_ROOT.value + "\\system\\bin\\tool\\synctool.code.xml";
  1604. SYNCTOOL_LOG.value = CAFE_DATA_TMP.value + "\\synctool.log";
  1605. synctool.sync(SYNCTOOL_CFG.value, CAFE_ELF_SRC_DIR.value, CAFE_DATA_TMP_DOS, SYNCTOOL_LOG.value);
  1606. }
  1607. CAFE_ELF.value = CAFE_DATA_TMP.value + "\\" + TMP_SUB_DIR.value + "\\" + Path.GetFileName(args[0]);
  1608. }
  1609.  
  1610. switch (Path.GetExtension(CAFE_ELF.value))
  1611. {
  1612. case ".elf":
  1613. {
  1614. DEBUG_ELF_FILE.value = CAFE_ELF.value;
  1615. string rpx = Path.ChangeExtension(CAFE_ELF.value, "rpx");
  1616. if (File.Exists(rpx))
  1617. {
  1618. CAFE_ELF.value = rpx;
  1619. }
  1620. break;
  1621. }
  1622.  
  1623. case ".rpx":
  1624. {
  1625. if (DEBUG_ELF_FILE.value == null)
  1626. {
  1627. string elf = Path.ChangeExtension(CAFE_ELF.value, "elf");
  1628. if (File.Exists(elf))
  1629. {
  1630. DEBUG_ELF_FILE.value = elf;
  1631. }
  1632. }
  1633. break;
  1634. }
  1635. }
  1636.  
  1637. if (!CAFE_ELF.value.EndsWith(".rpx"))
  1638. {
  1639. Console.WriteLine("cafex run failed: Please provide rpx application as first argument after run_options");
  1640. run_usage();
  1641. return CAFEX_ERROR.RUN_NO_RPX_SPECIFIED;
  1642. }
  1643.  
  1644. if (!File.Exists(CAFE_ELF.value))
  1645. {
  1646. Console.WriteLine("cafex run failed: Cannot find rpx application in " + CAFE_ELF.value);
  1647. return CAFEX_ERROR.RUN_RPX_DOESNT_EXIST;
  1648. }
  1649.  
  1650. CAFE_ELF_DIR.value = Path.GetDirectoryName(CAFE_ELF.value);
  1651.  
  1652. create_directory_if_noexist(CAFE_CONTENT_DIR.value);
  1653. create_directory_if_noexist(CAFE_META_DIR.value);
  1654. create_directory_if_noexist(CAFE_SAVE_DIR.value);
  1655.  
  1656. CAFE_CODE_DIR.value = CAFE_ELF_DIR.value;
  1657.  
  1658. APP_XML_FILE.value = CAFE_CODE_DIR.value + "\\app.xml";
  1659. if (!update_app_xml(APP_XML_FILE.value))
  1660. {
  1661. return CAFEX_ERROR.RUN_UPDATE_APP_XML_FAILED;
  1662. }
  1663.  
  1664. COS_XML_FILE.value = CAFE_CODE_DIR.value + "\\cos.xml";
  1665.  
  1666. if (!update_cos_xml(COS_XML_FILE.value, PPC_APP_FLAGS.value))
  1667. {
  1668. return CAFEX_ERROR.RUN_UPDATE_COS_XML_FAILED;
  1669. }
  1670.  
  1671. META_XML_FILE.value = CAFE_META_DIR.value + "\\meta.xml";
  1672. CAFEX_ERROR update_meta_ret = update_meta_xml(META_XML_FILE.value);
  1673.  
  1674. if (update_meta_ret != CAFEX_ERROR.OK)
  1675. {
  1676. return update_meta_ret;
  1677. }
  1678.  
  1679. if (!update_arglist(COS_XML_FILE.value, CAFE_ELF.value, args))
  1680. {
  1681. return CAFEX_ERROR.RUN_UPDATE_ARGLIST_FAILED;
  1682. }
  1683.  
  1684. update_boot_logos(CAFE_META_DIR.value);
  1685.  
  1686. if (RPL_DIRS.value != null)
  1687. {
  1688. string[] rpl_dirs = RPL_DIRS.value.Split(DEFAULT_DELIMITER);
  1689. foreach (string s in rpl_dirs)
  1690. {
  1691. string dir = PathConverter.Windowsify(s);
  1692. string[] contained_files = Directory.GetFiles(Path.GetDirectoryName(dir), "*.rpl");
  1693. if (contained_files.Length == 0)
  1694. {
  1695. Console.WriteLine("cafex run : WARNING - No files found in directory {0}.", dir);
  1696. }
  1697. else
  1698. {
  1699. foreach (string origin in contained_files)
  1700. {
  1701. string destination = CAFE_CODE_DIR.value + "\\" + Path.GetFileName(origin);
  1702. Console.WriteLine("cafex run: copy {0} to {1}", origin, destination);
  1703. File.Copy(origin, destination, true);
  1704. }
  1705. }
  1706. }
  1707. }
  1708. if (RPL_FILES.value != null)
  1709. {
  1710. string[] rpl_files = RPL_FILES.value.Split(DEFAULT_DELIMITER);
  1711. foreach (string s in rpl_files)
  1712. {
  1713. string file = PathConverter.Windowsify(s);
  1714. if (File.Exists(file))
  1715. {
  1716. string destination = CAFE_CODE_DIR.value + "\\" + Path.GetFileName(s);
  1717. Console.WriteLine("cafex run: copy {0} to {1}", file, destination);
  1718. File.Copy(file, destination, true);
  1719. }
  1720. }
  1721. }
  1722. }
  1723.  
  1724. FileStream previous_info_file = new FileStream(CAFERUN_WORK_DIR.value + "\\" + "previous_launch_info.xml", FileMode.Create);
  1725. StreamWriter writer = new StreamWriter(previous_info_file);
  1726. if (CAFERUN_OPTION_MLC_EMU_LAUNCH.value == "1")
  1727. {
  1728. writer.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
  1729. writer.WriteLine("<prev type=\"complex\" access=\"777\">");
  1730. writer.WriteLine(" <emu_tid type=\"string\" length=\"512\">" + CAFERUN_OPTION_MLC_EMU_TITLEID.value + "</emu_tid>");
  1731. writer.WriteLine("</prev>");
  1732. writer.Flush();
  1733. writer.Close();
  1734. }
  1735. else
  1736. {
  1737. writer.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
  1738. writer.WriteLine("<prev type=\"complex\" access=\"777\">");
  1739. writer.WriteLine(" <code_dir type=\"string\" length=\"512\">" + CAFE_CODE_DIR.value + "</code_dir>");
  1740. writer.WriteLine(" <disc_emu type=\"string\" length=\"8\">" + DISC_EMU_TYPE.value + "</disc_emu>");
  1741. writer.WriteLine(" <hdd_bank type=\"string\" length=\"8\">" + RUN_FROM_HDD_BANK.value + "</hdd_bank>");
  1742. writer.WriteLine("</prev>");
  1743. writer.Flush();
  1744. writer.Close();
  1745. }
  1746.  
  1747. CAFEX_ERROR updateResult = update_system_xml(SYSTEM_XML.value, PPC_OS_FLAGS.value, SYSTEM_MODE.value, MCP_LAUNCH_HINT.value);
  1748. if (updateResult != CAFEX_ERROR.OK)
  1749. {
  1750. return updateResult;
  1751. }
  1752. }
  1753. else
  1754. {
  1755. if (!File.Exists(SYSTEM_XML.value))
  1756. {
  1757. Console.WriteLine("cafex run failed: Please cafex run an rpx application first");
  1758. return CAFEX_ERROR.RUN_DASH_R_WITH_NO_PREVIOUS_LAUNCH_INFO;
  1759. }
  1760.  
  1761. string prev_info_file = CAFERUN_WORK_DIR.value + "\\" + "previous_launch_info.xml";
  1762. bool mlc_launch = XmlHandler.NodeExists(prev_info_file, "emu_tid");
  1763. if (mlc_launch)
  1764. {
  1765. CAFERUN_OPTION_MLC_EMU_TITLEID.value = XmlHandler.GetNodeValue(prev_info_file, "emu_tid");
  1766. CAFERUN_OPTION_MLC_EMU_LAUNCH.value = "1";
  1767. }
  1768. else
  1769. {
  1770. CAFE_CODE_DIR.value = XmlHandler.GetNodeValue(prev_info_file, "code_dir");
  1771. DISC_EMU_TYPE.value = XmlHandler.GetNodeValue(prev_info_file, "disc_emu");
  1772. RUN_FROM_HDD_BANK.value = XmlHandler.GetNodeValue(prev_info_file, "hdd_bank");
  1773.  
  1774. PREV_CAFE_ARGSTR.value = XmlHandler.GetNodeValue(CAFE_CODE_DIR.value + "\\cos.xml", "argstr");
  1775. CAFE_ELF.value = CAFE_CODE_DIR.value + "\\" + PREV_CAFE_ARGSTR.value.Split(' ')[0];
  1776.  
  1777. CAFERUN_COLDBOOT_OS_VERSION.value = XmlHandler.GetNodeValue(SYSTEM_XML.value, "default_os_id");
  1778.  
  1779. if (DISC_EMU_TYPE.value != "hdd")
  1780. {
  1781. if (!File.Exists(CAFE_ELF.value))
  1782. {
  1783. Console.WriteLine("cafex run failed: Could not find " + CAFE_ELF.value);
  1784. return CAFEX_ERROR.RUN_DASH_R_CANT_FIND_APPLICATION;
  1785. }
  1786.  
  1787. Console.WriteLine("cafex run: Restarting " + CAFE_ELF.value);
  1788. }
  1789. }
  1790. }
  1791.  
  1792. if (CAFE_BOOT_MODE.value == "PCFS")
  1793. {
  1794. string os_directory = CAFE_SLC_DIR.value + "\\sys\\title\\" + CAFERUN_COLDBOOT_OS_VERSION.value.Substring(0,8) + "\\" + CAFERUN_COLDBOOT_OS_VERSION.value.Substring(8,8) + "\\code";
  1795. if (!Directory.Exists(os_directory))
  1796. {
  1797. Console.WriteLine("cafex run failed: Cannot find OS in " + os_directory);
  1798. return CAFEX_ERROR.RUN_CANT_FIND_OS_DIRECTORY;
  1799. }
  1800.  
  1801. string[] files = Directory.GetFiles(os_directory);
  1802. bool found_rpl = false;
  1803. foreach (string s in files)
  1804. {
  1805. if (s.EndsWith(".rpl"))
  1806. {
  1807. found_rpl = true;
  1808. break;
  1809. }
  1810. }
  1811.  
  1812. if (!found_rpl)
  1813. {
  1814. CAFERUN_OS_VERSION_PARTIAL_UNIQUE_ID.value = (HexHandler.GetLongNumberFromString("0x" + CAFERUN_COLDBOOT_OS_VERSION.value) >> 8 & 0xff).ToString();
  1815.  
  1816. int value = HexHandler.GetNumberFromString(CAFERUN_OS_VERSION_PARTIAL_UNIQUE_ID.value);
  1817. if (value >= 192)
  1818. {
  1819. Console.WriteLine("cafex run failed: Please build FDEBUG os version " + CAFERUN_COLDBOOT_OS_VERSION.value);
  1820. }
  1821. else if (value >= 128)
  1822. {
  1823. Console.WriteLine("cafex run failed: Please build DEBUG os version " + CAFERUN_COLDBOOT_OS_VERSION.value);
  1824. Console.WriteLine(" or do not use \"cafex run -b\" to use NDEBUG version");
  1825. }
  1826. else if (value >= 64)
  1827. {
  1828. Console.WriteLine("cafex run failed: Please build NDEBUG os version " + CAFERUN_COLDBOOT_OS_VERSION.value);
  1829. Console.WriteLine(" or use \"cafex run -b\" to use the DEBUG version");
  1830. }
  1831. else
  1832. {
  1833. Console.WriteLine("cafex run failed: Non-standard titleId for os version " + CAFERUN_COLDBOOT_OS_VERSION.value);
  1834. }
  1835.  
  1836. return CAFEX_ERROR.RUN_OS_NOT_PRESENT;
  1837. }
  1838. }
  1839.  
  1840. if (PCFS_SRV_LOG_DIR.value == null)
  1841. {
  1842. PCFS_SRV_LOG_OUTPUT.value = "/dev/null";
  1843. }
  1844. else
  1845. {
  1846. PCFS_LOG_TIMESTAMP.value = DateTime.Now.ToString("HHmmss");
  1847. PCFS_SRV_LOG_OUTPUT.value = PCFS_SRV_LOG_DIR.value + "\\" + SESSION_PATH_PREFIX.value + "pcfs_" + PCFS_LOG_TIMESTAMP.value + ".txt";
  1848. Console.WriteLine("PCFS output logged to " + PCFS_SRV_LOG_OUTPUT.value);
  1849. }
  1850.  
  1851. if (DISC_EMU_TYPE.value == "hfio" ||
  1852. DISC_EMU_TYPE.value == "hdd" ||
  1853. ((!string.IsNullOrEmpty(CAFE_ELF.value)) && CAFE_ELF.value.EndsWith(".rpx")))
  1854. {
  1855. if (DISC_EMU_TYPE.value == "hfio")
  1856. {
  1857. MAPDIR_MLC.value = CAFE_MLC_DIR.value + "/";
  1858. MAPDIR_SLC.value = CAFE_SLC_DIR.value + "/";
  1859. MAPDIR_CODE.value = CAFE_CODE_DIR.value + "/";
  1860. MAPDIR_META.value = CAFE_META_DIR.value + "/";
  1861. MAPDIR_CONTENT.value = CAFE_CONTENT_DIR.value + "/";
  1862. MAPDIR_SAVE.value = CAFE_SAVE_DIR.value + "/";
  1863.  
  1864. PCFS_DIR_MAPPING.value = "-m /%MLC_EMU_DIR/ \"" + MAPDIR_MLC.value + "\"" +
  1865. " -m /%SLC_EMU_DIR/ \"" + MAPDIR_SLC.value + "\"" +
  1866. " -m /%DISC_EMU_DIR/code/ \"" + MAPDIR_CODE.value + "\"" +
  1867. " -m /%DISC_EMU_DIR/meta/ \"" + MAPDIR_META.value + "\"" +
  1868. " -m /%DISC_EMU_DIR/content/ \"" + MAPDIR_CONTENT.value + "\"" +
  1869. " -m /%DISC_EMU_DIR/save/ \"" + MAPDIR_SAVE.value + "\"";
  1870. }
  1871. else
  1872. {
  1873. MAPDIR_MLC.value = CAFE_MLC_DIR.value + "/";
  1874. MAPDIR_SLC.value = CAFE_SLC_DIR.value + "/";
  1875. MAPDIR_SAVE.value = CAFE_SAVE_DIR.value + "/";
  1876.  
  1877. PCFS_DIR_MAPPING.value = "-m /%MLC_EMU_DIR/ \"" + MAPDIR_MLC.value + "\"" +
  1878. " -m /%SLC_EMU_DIR/ \"" + MAPDIR_SLC.value + "\"" +
  1879. " -m /%SAVE_EMU_DIR/ \"" + MAPDIR_SAVE.value + "\"";
  1880. }
  1881. }
  1882. else if (CAFERUN_OPTION_MLC_EMU_LAUNCH.value == "1")
  1883. {
  1884. MAPDIR_MLC.value = CAFE_MLC_DIR.value + "/";
  1885. MAPDIR_SLC.value = CAFE_SLC_DIR.value + "/";
  1886.  
  1887. PCFS_DIR_MAPPING.value = "-m /%MLC_EMU_DIR/ \"" + MAPDIR_MLC.value + "\"" +
  1888. " -m /%SLC_EMU_DIR/ \"" + MAPDIR_SLC.value + "\"";
  1889. }
  1890. else
  1891. {
  1892. MAPDIR_MLC.value = CAFE_MLC_DIR.value + "/";
  1893. MAPDIR_SLC.value = CAFE_SLC_DIR.value + "/";
  1894.  
  1895. PCFS_DIR_MAPPING.value = "-m /%MLC_EMU_DIR/ \"" + MAPDIR_MLC.value + "\"" +
  1896. " -m /%SLC_EMU_DIR/ \"" + MAPDIR_SLC.value + "\"";
  1897. }
  1898.  
  1899. ELF_NEST_OPT.value="";
  1900. ELF_NEST_VALUE.value = "";
  1901. if (ALLOW_DISCRUN_IN_SOFTLAUNCH.value == "1")
  1902. {
  1903. // the DLF will not be used now - but it has to be made now,
  1904. // as this is the point where all input environment variables are set correctly
  1905. // for cafedevrun.sh to run
  1906. bootrun(new List<string>() {"-makedlf"} );
  1907. if (!BRIDGE_PARAMETERS_WITH_E.value.Contains("soft_launch_cafe_elf"))
  1908. {
  1909. if (!string.IsNullOrEmpty(CAFE_ELF.value))
  1910. {
  1911. // we didn't get the ELF name from above. So we must to decide what it is.
  1912. ELF_NEST_OPT.value = "-e";
  1913. ELF_NEST_VALUE.value = "soft_launch_cafe_elf:" + CAFE_ELF.value;
  1914. // Since there is no way to communicate new DLF contents into an existing FSEmul,
  1915. // we need a forceful restart for this case - even though it is softlaunch
  1916. CAFE_RUN_FORCERESTART.value = "1";
  1917. }
  1918. }
  1919. }
  1920.  
  1921. if (CAFERUN_OPTION_SOFT_LAUNCH.value == "1")
  1922. {
  1923. // Need to call monitor to flush any data from previous execution that might
  1924. // be still buffered in the FSEmul. Setting a timeout prevents an early
  1925. // start of a new title while a previous one might be still booting up.
  1926. // This is necessary because the .NET process launches immediately after the
  1927. // previous is finished.
  1928. dkt.FlushOutputBuffer(1);
  1929.  
  1930.  
  1931. if (CAFE_RUN_FORCERESTART.value == "1")
  1932. {
  1933. Console.WriteLine("**** FORCE REBOOT ****");
  1934. List<string> onArguments = new List<string>();
  1935. onArguments.Add("-noprompt");
  1936. if (!string.IsNullOrEmpty(BRIDGE_PARAMETERS_WITH_E.value))
  1937. {
  1938. onArguments.AddRange(BRIDGE_PARAMETERS_WITH_E.value.Split(' '));
  1939. }
  1940.  
  1941. // discrun support variables
  1942. onArguments.Add(ELF_NEST_OPT.value);
  1943. onArguments.Add(ELF_NEST_VALUE.value);
  1944.  
  1945. string[] noprompt_args = onArguments.ToArray();
  1946. string wumadRetrievedTitleId = string.Empty;
  1947. EnvVar.BackUpCurrentEnvironment();
  1948. // Saves user input
  1949. string userProvidedTitleId = CAFERUN_OPTION_MLC_EMU_TITLEID.value.StartsWith("0x") ? CAFERUN_OPTION_MLC_EMU_TITLEID.value.Substring(2).ToLowerInvariant() : CAFERUN_OPTION_MLC_EMU_TITLEID.value.ToLowerInvariant();
  1950. // Then assigns to the variable that will be passed in for verification
  1951. CAFERUN_WUMAD_TITLE_ID.value = userProvidedTitleId;
  1952. on(noprompt_args);
  1953. wumadRetrievedTitleId = CAFERUN_WUMAD_TITLE_ID.value;
  1954. EnvVar.RestoreSavedEnvironment();
  1955. // If a value returned and it's different, should take it.
  1956. if (!string.IsNullOrEmpty(wumadRetrievedTitleId) &&
  1957. !wumadRetrievedTitleId.Equals(userProvidedTitleId))
  1958. {
  1959. // This is the auto-discovery action. Reset the wumad title id.
  1960. CAFERUN_WUMAD_TITLE_ID.value = wumadRetrievedTitleId;
  1961. }
  1962. // Make sure the CAT-DEV is in a state that can take commands.
  1963. Thread.Sleep(Program.COS_READY_DELAY); //trying to get connection through devkit help msg. This prevents a bunch of 'help' messages to be issued.
  1964. CMD_RESULT.value = devkitmsg.help(Program.DEVKIT_HELP_TIMEOUT_SECONDS).ToString();
  1965. }
  1966.  
  1967. // try this three times.
  1968. for (int i = 0; i < 3; ++i)
  1969. {
  1970. #if DEBUG
  1971. Log.WriteLine("Iteration #" + (i+1).ToString() + " of 3");
  1972. #endif
  1973.  
  1974. PCFS_SYNC_DATE.value = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss-ffff");
  1975. Console.WriteLine("Launching " + Path.GetFileName(CAFE_ELF.value) + " @ sync date [" + PCFS_SYNC_DATE.value + "]");
  1976. CMD_RESULT.value = PCFSServerSync.sync(PCFS_SYNC_DATE.value, CAFE_ELF.value, PCFS_DIR_MAPPING.value).ToString();
  1977. #if DEBUG
  1978. Log.WriteLine("PCFSServerSync process returned '" + CMD_RESULT.value + "' from execution.");
  1979. #endif
  1980. if (CMD_RESULT.value == "0")
  1981. {
  1982. bool helpMessageSent = false;
  1983. if (CAFERUN_OPTION_SOFT_LAUNCH.value == "1" && CAFE_BOOT_MODE.value == "NAND")
  1984. {
  1985. // Send the help message to find out if the CAT-DEV is can take commands.
  1986. #if DEBUG
  1987. Log.WriteLine("Before softlaunch sleep for '" + Program.COS_READY_DELAY.Seconds.ToString() + "' seconds.");
  1988. #endif
  1989.  
  1990. Thread.Sleep(Program.COS_READY_DELAY); //trying to get connection through devkit help msg. This prevents a bunch of 'help' messages to be issued.
  1991. CMD_RESULT.value = devkitmsg.help(Program.DEVKIT_HELP_TIMEOUT_SECONDS).ToString();
  1992. helpMessageSent = true;
  1993. #if DEBUG
  1994. Log.WriteLine("setting cosflags now.");
  1995. #endif
  1996. CMD_RESULT.value = set_system_xml_cos_flags(PPC_OS_FLAGS.value).ToString();
  1997. }
  1998.  
  1999. if (CMD_RESULT.value == "0")
  2000. {
  2001. if (!helpMessageSent)
  2002. {
  2003. // Send the help message to find out if the CAT-DEV is fully finished booting.
  2004. #if DEBUG
  2005. Log.WriteLine("Before softlaunch sleep for '" + Program.COS_READY_DELAY.Seconds.ToString() + "' seconds.");
  2006. #endif
  2007.  
  2008. Thread.Sleep(Program.COS_READY_DELAY); //trying to get connection through devkit help msg. This prevents a bunch of 'help' messages to be issued.
  2009. CMD_RESULT.value = devkitmsg.help(Program.DEVKIT_HELP_TIMEOUT_SECONDS).ToString();
  2010. }
  2011.  
  2012. #if DEBUG
  2013. Log.WriteLine("trying softlaunch now.");
  2014. #endif
  2015.  
  2016. // discrun support.
  2017. // This is softlaunch, and there is no RPS specified.
  2018. // try to see if we can get it from the user - in case it applies
  2019. if(string.IsNullOrEmpty(DISC_EMU_TID.value))
  2020. {
  2021. if (CAFERUN_OPTION_MLC_EMU_LAUNCH.value == "1")
  2022. {
  2023. DISC_EMU_TID.value = (!string.IsNullOrEmpty(CAFERUN_OPTION_MLC_EMU_TITLEID.value) && CAFERUN_OPTION_MLC_EMU_TITLEID.value.Length == 16) ?
  2024. "0x" + CAFERUN_OPTION_MLC_EMU_TITLEID.value :
  2025. CAFERUN_OPTION_MLC_EMU_TITLEID.value;
  2026.  
  2027. Console.WriteLine("HDD emulation: using command line TID {0}", DISC_EMU_TID.value);
  2028. }
  2029. else if (!string.IsNullOrEmpty(CAFE_WUMAD.value) &&
  2030. !string.IsNullOrEmpty(CAFERUN_WUMAD_TITLE_ID.value))
  2031. {
  2032. // Check if a WUMAD was provided and see if title id is available
  2033. DISC_EMU_TID.value = CAFERUN_WUMAD_TITLE_ID.value;
  2034. Console.WriteLine("HDD WUMAD emulation: using command line TID {0}", DISC_EMU_TID.value);
  2035. }
  2036. }
  2037.  
  2038. // still not set despite our attempts?
  2039. if(string.IsNullOrEmpty(DISC_EMU_TID.value))
  2040. {
  2041. Console.WriteLine("cafex run failed: Unable to determine title ID, and it was not specified in the command line.");
  2042. return CAFEX_ERROR.RUN_DISCRUN_NAND_UNABLE_TO_FIND_APPLICATION;
  2043.  
  2044. }
  2045.  
  2046. // this is to phase in adoption smoothly into autotest,
  2047. // avoiding any changes to tests other than cafediscrun.
  2048. // We may disable this check later.
  2049. if (ALLOW_DISCRUN_IN_SOFTLAUNCH.value == "1")
  2050. {
  2051. CMD_RESULT.value = "0";
  2052. if (!string.IsNullOrEmpty(SYSTEM_MODE.value))
  2053. {
  2054. if (SYSTEM_MODE.value.ToLowerInvariant().Equals("prod"))
  2055. {
  2056. Console.WriteLine("Setting mode to 0");
  2057. devkitmsg.sys_mode_prod();
  2058. }
  2059. else if (SYSTEM_MODE.value.ToLowerInvariant().Equals("dev"))
  2060. {
  2061. Console.WriteLine("Setting mode to 1");
  2062. devkitmsg.sys_mode_dev();
  2063. }
  2064. else if (SYSTEM_MODE.value.ToLowerInvariant().Equals("test"))
  2065. {
  2066. Console.WriteLine("Setting mode to 2");
  2067. devkitmsg.sys_mode_test();
  2068. }
  2069. }
  2070. }
  2071.  
  2072. if (MCP_LAUNCH_HINT.value != null)
  2073. {
  2074. CMD_RESULT.value = devkitmsg.title_softlaunch_with_hint(CAFERUN_COLDBOOT_OS_VERSION.value, DISC_EMU_TID.value, MCP_LAUNCH_HINT.value, SESSION_LAUNCH_CTRL_PORT.value).ToString();
  2075. }
  2076. else
  2077. {
  2078. CMD_RESULT.value = devkitmsg.title_softlaunch(CAFERUN_COLDBOOT_OS_VERSION.value, DISC_EMU_TID.value, SESSION_LAUNCH_CTRL_PORT.value).ToString();
  2079. }
  2080.  
  2081. if (CMD_RESULT.value == "0")
  2082. {
  2083. // Clear the buffer one final time before exiting.
  2084. dkt.FlushOutputBuffer();
  2085. break;
  2086. }
  2087. }
  2088. }
  2089.  
  2090.  
  2091. #if DEBUG
  2092. Log.WriteLine("trying reboot now.");
  2093. #endif
  2094.  
  2095. Console.WriteLine("**** BOOTING CATDEV ****");
  2096. stop(null);
  2097. #if DEBUG
  2098. Log.WriteLine("Before reboot sleep for '" + Program.COS_REBOOT_DELAY.Seconds.ToString() + "' seconds.");
  2099. #endif
  2100.  
  2101. Thread.Sleep(Program.COS_REBOOT_DELAY);
  2102. List<string> onArguments = new List<string>();
  2103. onArguments.Add("-noprompt");
  2104. if (!string.IsNullOrEmpty(BRIDGE_PARAMETERS_WITH_E.value))
  2105. {
  2106. onArguments.AddRange(BRIDGE_PARAMETERS_WITH_E.value.Split(' '));
  2107. }
  2108.  
  2109. // discrun support variables
  2110. onArguments.Add(ELF_NEST_OPT.value);
  2111. onArguments.Add(ELF_NEST_VALUE.value);
  2112.  
  2113. string[] noprompt_args = onArguments.ToArray();
  2114. string wumadRetrievedTitleId = string.Empty;
  2115. EnvVar.BackUpCurrentEnvironment();
  2116. // Saves user input
  2117. string userProvidedTitleId = CAFERUN_OPTION_MLC_EMU_TITLEID.value.StartsWith("0x") ? CAFERUN_OPTION_MLC_EMU_TITLEID.value.Substring(2).ToLowerInvariant() : CAFERUN_OPTION_MLC_EMU_TITLEID.value.ToLowerInvariant();
  2118. // Then assigns to the variable that will be passed in for verification
  2119. CAFERUN_WUMAD_TITLE_ID.value = userProvidedTitleId;
  2120. on(noprompt_args);
  2121. wumadRetrievedTitleId = CAFERUN_WUMAD_TITLE_ID.value;
  2122. EnvVar.RestoreSavedEnvironment();
  2123. // If a value returned and it's different, should take it.
  2124. if (!string.IsNullOrEmpty(wumadRetrievedTitleId) &&
  2125. !wumadRetrievedTitleId.Equals(userProvidedTitleId))
  2126. {
  2127. // This is the auto-discovery action. Reset the wumad title id.
  2128. CAFERUN_WUMAD_TITLE_ID.value = wumadRetrievedTitleId;
  2129. }
  2130. // Send the help message to find out if the CAT-DEV is fully finished booting.
  2131. #if DEBUG
  2132. Log.WriteLine("Before softlaunch sleep for '" + Program.COS_READY_DELAY.Seconds.ToString() + "' seconds.");
  2133. #endif
  2134.  
  2135. Thread.Sleep(Program.COS_READY_DELAY); //15s + 2s trying to get connection through devkit help msg. This prevents a bunch of 'help' messages to be issued.
  2136. CMD_RESULT.value = devkitmsg.help(Program.DEVKIT_HELP_TIMEOUT_SECONDS).ToString();
  2137. }
  2138. }
  2139. else
  2140. {
  2141. PCFS_SYNC_DATE.value = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss-ffff");
  2142. Console.WriteLine("Starting PCFSServer.exe @ sync date [" + PCFS_SYNC_DATE.value + "]");
  2143.  
  2144. PCFSServer.Start(PCFSSERVER_PARAMETERS.value, PCFS_DIR_MAPPING.value, PCFS_SRV_LOG_OUTPUT.value);
  2145.  
  2146. Console.WriteLine("PCFSServer has started");
  2147.  
  2148. if (PCFSSERVER_PARAMETERS.value != null)
  2149. {
  2150. Console.WriteLine("Waiting for PCFSServer initialization...");
  2151. PCFSSERVERSYNC_RVAL.value = PCFSServerSync.wait_sync(PCFS_SYNC_DATE.value).ToString();
  2152. if (PCFSSERVERSYNC_RVAL.value != "0")
  2153. {
  2154. Console.WriteLine("PCFSServerSync exited with value " + PCFSSERVERSYNC_RVAL.value);
  2155. return CAFEX_ERROR.RUN_PCFSSERVER_SYNC_FAILED;
  2156. }
  2157. Console.WriteLine("PCFSServer initialization complete!");
  2158. }
  2159. else
  2160. {
  2161. Console.WriteLine("PCFSServer started without synchronization");
  2162. }
  2163.  
  2164. BOOTRUN_ELF_OPT.value = "";
  2165. BOOTRUN_ELF_VALUE.value = "";
  2166. if (!string.IsNullOrEmpty(SOFT_LAUNCH_CAFE_ELF.value))
  2167. {
  2168. BOOTRUN_ELF_OPT.value = "-usedlf";
  2169. BOOTRUN_ELF_VALUE.value = SOFT_LAUNCH_CAFE_ELF.value;
  2170. }
  2171.  
  2172. // Check if this is a wumad execution, so that bootrun can correctly assign the title Id
  2173. if (isWumadExecution)
  2174. {
  2175. if (!string.IsNullOrEmpty(CAFERUN_OPTION_MLC_EMU_TITLEID.value))
  2176. {
  2177. // Saves user input
  2178. string userProvidedTitleId = CAFERUN_OPTION_MLC_EMU_TITLEID.value.StartsWith("0x") ? CAFERUN_OPTION_MLC_EMU_TITLEID.value.Substring(2).ToLowerInvariant() : CAFERUN_OPTION_MLC_EMU_TITLEID.value.ToLowerInvariant();
  2179. // Then assigns to the variable that will be passed in for verification
  2180. CAFERUN_WUMAD_TITLE_ID.value = userProvidedTitleId;
  2181. }
  2182. else
  2183. {
  2184. // Ensure there is no title, so autodiscovery can run
  2185. CAFERUN_WUMAD_TITLE_ID.value = string.Empty;
  2186. }
  2187. }
  2188.  
  2189. CAFEX_ERROR bootrun_ret = bootrun(new List<string>() {BOOTRUN_ELF_OPT.value, BOOTRUN_ELF_VALUE.value});
  2190. if (bootrun_ret != CAFEX_ERROR.OK)
  2191. return bootrun_ret;
  2192. }
  2193.  
  2194. CAFEX_ERROR connect_serial_or_debugger_ret = connect_serial_or_debugger(quiet_mode);
  2195. if (connect_serial_or_debugger_ret != CAFEX_ERROR.OK)
  2196. {
  2197. return connect_serial_or_debugger_ret;
  2198. }
  2199.  
  2200. return CAFEX_ERROR.OK;
  2201. }
  2202.  
  2203. private struct WumadInfo
  2204. {
  2205. public bool titleFound;
  2206. public string titleId;
  2207. public string appXml;
  2208. public string cosXml;
  2209. public string metaXml;
  2210. }
  2211.  
  2212. private static CAFEX_ERROR TryGetWumadInfo(string wumadTempDir, ref WumadInfo info)
  2213. {
  2214. string[] metaFiles = Directory.GetFiles(wumadTempDir, "*metaxml.bin");
  2215. string[] appFiles = Directory.GetFiles(wumadTempDir, "*appxml.bin");
  2216. string[] cosFiles = Directory.GetFiles(wumadTempDir, "*cosxml.bin");
  2217.  
  2218. // User provided title.
  2219. string titleId = string.Empty;
  2220. if (!string.IsNullOrEmpty(CAFERUN_WUMAD_TITLE_ID.value))
  2221. {
  2222. titleId = CAFERUN_WUMAD_TITLE_ID.value.StartsWith("0x") ? CAFERUN_WUMAD_TITLE_ID.value.Substring(2).ToLowerInvariant() : CAFERUN_WUMAD_TITLE_ID.value.ToLowerInvariant();
  2223. }
  2224.  
  2225. info.titleFound = false;
  2226. if (metaFiles.Length == 1)
  2227. {
  2228. // The wumad has only one title
  2229. string singleTitleId = XmlHandler.GetNodeValue(appFiles[0], "title_id");
  2230. if (string.IsNullOrEmpty(titleId) ||
  2231. titleId.Equals("ffffffffffffffff") ||
  2232. titleId.Equals(singleTitleId) )
  2233. {
  2234. // Empty-> auto discovery *OR*
  2235. // Value-> matches user input
  2236. info.titleFound = true;
  2237. info.titleId = singleTitleId;
  2238. info.metaXml = metaFiles[0];
  2239. info.appXml = appFiles[0];
  2240. info.cosXml = cosFiles[0];
  2241. }
  2242. else
  2243. {
  2244. // We need to respect the title give by the user,
  2245. // but provide a warning that it didn't match.
  2246. if (!IsTitleIdReserved(titleId))
  2247. {
  2248.  
  2249. Console.WriteLine("cafex bootrun: WARNING: Unable to find title {0} in the wumad.", titleId);
  2250. }
  2251.  
  2252. return CAFEX_ERROR.RUN_BAD_TITLE_ID;
  2253. }
  2254. }
  2255. else
  2256. {
  2257. // The wumad has multiple files... need to know the title id
  2258. if (string.IsNullOrEmpty(titleId) ||
  2259. titleId.Equals("ffffffffffffffff"))
  2260. {
  2261. Console.WriteLine("cafex bootrun failed: Multiple titles found in wumad. Either RPX or TitleId is required to select the title to run.");
  2262. return CAFEX_ERROR.RUN_TITLE_ID_REQUIRED;
  2263. }
  2264.  
  2265. // If in NAND/softlaunch mode, we shouldn't validate the title id, since
  2266. // the user may really want to launch a title there is not inside the wumad.
  2267. // However, we should alert the user that the title was not found, and also
  2268. // not search for if we already know it is the sysconfigtool title id.
  2269. if (!IsTitleIdReserved(titleId))
  2270. {
  2271. // Only looking if it's not a system/reserved titleid.
  2272. int targetIndex = 0;
  2273.  
  2274. for (targetIndex = 0; targetIndex < appFiles.Length; ++targetIndex)
  2275. {
  2276. string wumadTitle = XmlHandler.GetNodeValue(appFiles[targetIndex], "title_id").ToLowerInvariant();
  2277. Console.WriteLine("cafex run : wumad title {0} found and comparing to {1}.", wumadTitle, titleId);
  2278. if (wumadTitle.Equals(titleId))
  2279. {
  2280. info.titleId = titleId;
  2281. info.titleFound = true;
  2282. break;
  2283. }
  2284. }
  2285.  
  2286. if (!info.titleFound)
  2287. {
  2288. Console.WriteLine("cafex bootrun: WARNING: Unable to find title {0} in the wumad.", titleId);
  2289. return CAFEX_ERROR.RUN_BAD_TITLE_ID;
  2290. }
  2291.  
  2292. info.metaXml = metaFiles[targetIndex];
  2293. info.appXml = appFiles[targetIndex];
  2294. info.cosXml = cosFiles[targetIndex];
  2295. }
  2296. }
  2297.  
  2298. return CAFEX_ERROR.OK;
  2299. }
  2300.  
  2301. private static bool IsTitleIdReserved(string fullTitleInHex)
  2302. {
  2303. // Reserved range:
  2304. //0x00000 Through 0x000FF -> 0 <= TID <= 255
  2305. //0xF7000 Through 0xF7FFF -> 1011712 <= TID <= 1015807
  2306. int badRangeMaxA = HexHandler.GetNumberFromString("0xff");
  2307. int badRangeMinB = HexHandler.GetNumberFromString("0xf7000");
  2308. int badRangeMaxB = HexHandler.GetNumberFromString("0xf7fff");
  2309.  
  2310. if (string.IsNullOrEmpty(fullTitleInHex))
  2311. {
  2312. // Empty title shouldn't be used, so it is reserved!
  2313. return true;
  2314. }
  2315.  
  2316. string titleId = fullTitleInHex.StartsWith("0x") ? fullTitleInHex.Substring(2).ToLowerInvariant() : fullTitleInHex.ToLowerInvariant();
  2317. // Need to get the following digits of the title: 000500001XXXXX00 The five "X" hex values represent the Unique Id.
  2318. string uniqueId = "0x" + titleId.Substring(9, 5);
  2319. int uniqueIdValue = HexHandler.GetNumberFromString(uniqueId);
  2320.  
  2321. if (uniqueIdValue <= badRangeMaxA ||
  2322. (uniqueIdValue >= badRangeMinB && uniqueIdValue <= badRangeMaxB))
  2323. {
  2324. // is reserved
  2325. return true;
  2326. }
  2327. else
  2328. {
  2329. // is not reserved
  2330. return false;
  2331. }
  2332.  
  2333. }
  2334. //Script function: bootrun
  2335. static CAFEX_ERROR bootrun(List<string> args)
  2336. {
  2337. #if DEBUG
  2338. Log.WriteLine("bootrun started.");
  2339. #endif
  2340.  
  2341. int MAKE_DLF_ONLY = 0;
  2342. int USE_DLF_ONLY = 0;
  2343. string ELF_FILENAME = CAFE_ELF.value;
  2344.  
  2345. if (args != null && args.Count > 0)
  2346. {
  2347. // Check parameters
  2348. while (args.Count != 0)
  2349. {
  2350. string OPT = args[0];
  2351. args.RemoveAt(0);
  2352. switch(OPT)
  2353. {
  2354. case "-makedlf":
  2355. // It will make the DLF, but won't use it
  2356. MAKE_DLF_ONLY = 1;
  2357. break;
  2358.  
  2359. case "-usedlf":
  2360. USE_DLF_ONLY = 1;
  2361. if (args.Count > 0)
  2362. {
  2363. ELF_FILENAME = args[0];
  2364. args.RemoveAt(0);
  2365. }
  2366. break;
  2367.  
  2368. }
  2369. }
  2370. }
  2371.  
  2372. CAFERUN_COLDBOOT_OS_VERSION_HI.value = CAFERUN_COLDBOOT_OS_VERSION.value.Substring(0, 8);
  2373. CAFERUN_COLDBOOT_OS_VERSION_LO.value = CAFERUN_COLDBOOT_OS_VERSION.value.Substring(8, 8);
  2374.  
  2375. if (CAFERUN_OPTION_SOFT_LAUNCH.value == "1" && MAKE_DLF_ONLY == 0)
  2376. {
  2377. devkitmsg.title_softlaunch(CAFERUN_COLDBOOT_OS_VERSION.value, DISC_EMU_TID.value, SESSION_LAUNCH_CTRL_PORT.value);
  2378. }
  2379. else
  2380. {
  2381. if (TEMP.value == null)
  2382. {
  2383. Console.WriteLine("cafex run failed: TEMP environment variable is not set.");
  2384. return CAFEX_ERROR.BOOTRUN_NO_TEMP_VAR;
  2385. }
  2386. else
  2387. {
  2388. BOOTRUN_WORK_DIR.value = TEMP.value + "\\" + SESSION_PATH_PREFIX.value + "caferun";
  2389. WUMAD_WORK_DIR.value = TEMP.value + "\\" + SESSION_PATH_PREFIX.value + "makewumaddlf";
  2390. }
  2391. initialize_work_directory(BOOTRUN_WORK_DIR.value);
  2392.  
  2393. FIRMWARE_FILE.value = CAFE_SLC_DIR.value + "\\sys\\title\\" + CAFERUN_COLDBOOT_OS_VERSION_HI.value + "\\" + CAFERUN_COLDBOOT_OS_VERSION_LO.value + "\\code\\fw.img";
  2394.  
  2395. if (CAFE_HARDWARE.value.StartsWith("ev"))
  2396. {
  2397. FIRMWARE_FILE.value = CAFE_SLC_DIR.value + "\\sys\\title\\" + CAFERUN_COLDBOOT_OS_VERSION_HI.value + "\\" + CAFERUN_COLDBOOT_OS_VERSION_LO.value + "\\code\\fw.bu.img";
  2398. }
  2399.  
  2400. if (CAFE_SECURITY.value == "off")
  2401. {
  2402. FIRMWARE_FILE.value = CAFE_SLC_DIR.value + "\\sys\\title\\" + CAFERUN_COLDBOOT_OS_VERSION_HI.value + "\\" + CAFERUN_COLDBOOT_OS_VERSION_LO.value + "\\code\\fw.bu.img";
  2403. }
  2404. else
  2405. {
  2406. FIRMWARE_FILE.value = CAFE_SLC_DIR.value + "\\sys\\title\\" + CAFERUN_COLDBOOT_OS_VERSION_HI.value + "\\" + CAFERUN_COLDBOOT_OS_VERSION_LO.value + "\\code\\fw.img";
  2407. }
  2408.  
  2409. BOOT_DLF_FILE.value = BOOTRUN_WORK_DIR.value + "\\ppc_boot.dlf";
  2410.  
  2411. CAFEX_ERROR boot1file_ret = make_boot1_system_file(BOOTRUN_WORK_DIR.value + "\\ppc.bsf");
  2412. if (boot1file_ret != CAFEX_ERROR.OK)
  2413. {
  2414. return boot1file_ret;
  2415. }
  2416.  
  2417. string diskid = BOOTRUN_WORK_DIR.value + "\\diskid.bin";
  2418. File.Copy(CAFE_ROOT.value + "\\system\\bin\\" + TOOLCHAIN.value + "\\" + PLATFORM.value + "\\boot\\diskid.bin", diskid, true);
  2419. File.SetAttributes(diskid, FileAttributes.Normal);
  2420.  
  2421. CAFEX_ERROR bootdlf_ret = make_boot_dlf(diskid + "," + BOOTRUN_WORK_DIR.value + "\\ppc.bsf," + FIRMWARE_FILE.value, BOOT_DLF_FILE.value);
  2422. if (bootdlf_ret != CAFEX_ERROR.OK)
  2423. {
  2424. return bootdlf_ret;
  2425. }
  2426.  
  2427. if (DISC_EMU_TYPE.value == "blockemu")
  2428. {
  2429. if (!string.IsNullOrEmpty(ELF_FILENAME) &&
  2430. ELF_FILENAME.EndsWith(".rpx"))
  2431. {
  2432. if (USE_DLF_ONLY == 0)
  2433. {
  2434. CAFEX_ERROR run_ret = devrun(ELF_FILENAME, null, null, null);
  2435. if (run_ret != CAFEX_ERROR.OK)
  2436. {
  2437. return run_ret;
  2438. }
  2439. }
  2440.  
  2441. if (MAKE_DLF_ONLY == 0)
  2442. {
  2443. hostrun(BOOT_DLF_FILE.value + " " + Path.ChangeExtension(ELF_FILENAME, ".dlf"));
  2444. }
  2445.  
  2446. }
  2447. }
  2448. else // HFIO case & HDD case & WUMAD case
  2449. {
  2450. if (!string.IsNullOrEmpty(CAFE_WUMAD.value))
  2451. {
  2452. // WUMAD case
  2453. // Create the DLF from a WUMAD file.
  2454. string wumadTempName = null;
  2455. string wumadTempDir = null;
  2456. string wumadDlfName = null;
  2457.  
  2458. if (CAFE_WUMAD.value.ToLower().EndsWith("wumad"))
  2459. {
  2460. // A WUMAD file was passed instead of a directory.
  2461. Console.WriteLine("Creating DLF from WUMAD file...");
  2462. wumadTempName = Path.GetFileNameWithoutExtension(CAFE_WUMAD.value);
  2463. wumadTempDir = Path.Combine(WUMAD_WORK_DIR.value, wumadTempName);
  2464. wumadDlfName = Path.Combine(wumadTempDir, wumadTempName + ".dlf");
  2465. }
  2466. else
  2467. {
  2468. // A directory was passed in.
  2469. wumadTempName = null;
  2470. wumadTempDir = CAFE_WUMAD.value;
  2471. wumadDlfName = Path.Combine(WUMAD_WORK_DIR.value, "extractedWumad.dlf");
  2472.  
  2473. }
  2474.  
  2475. if (wumadTempName != null)
  2476. {
  2477. wumadTempName = CAFE_WUMAD.value;
  2478. }
  2479.  
  2480. Console.WriteLine("wumadName = " + wumadTempName);
  2481. Console.WriteLine("wumadDir = " + wumadTempDir);
  2482. Console.WriteLine("wumadDlf = " + wumadDlfName);
  2483.  
  2484. CAFEX_ERROR wumad_dlf_ret = make_wumad_dlf(wumadTempName, wumadTempDir, wumadDlfName);
  2485. if (wumad_dlf_ret != CAFEX_ERROR.OK)
  2486. {
  2487. return wumad_dlf_ret;
  2488. }
  2489.  
  2490. WumadInfo info = new WumadInfo();
  2491. CAFEX_ERROR wumadInfoResult = TryGetWumadInfo(wumadTempDir, ref info);
  2492. // If a title id is required because there are more than one in the wumad
  2493. // and none was provided, should stop here.
  2494. if (wumadInfoResult == CAFEX_ERROR.RUN_TITLE_ID_REQUIRED)
  2495. {
  2496. return wumadInfoResult;
  2497. }
  2498.  
  2499. // We always need to set the titleId, if the function
  2500. // returned one. This allows the data to be available upstream
  2501. // in a sequence of calls involving softlaunch.
  2502. if (info.titleFound)
  2503. {
  2504. CAFERUN_WUMAD_TITLE_ID.value = info.titleId;
  2505. }
  2506. else
  2507. {
  2508. CAFERUN_WUMAD_TITLE_ID.value = string.Empty;
  2509. }
  2510.  
  2511. // If no RPX was provided, the xmls not been updated.
  2512. // This is a valid assumption because the code in caferun
  2513. // sets all the xmls besed on an RPX provide prior to call
  2514. // bootrun
  2515. if (string.IsNullOrEmpty(ELF_FILENAME) &&
  2516. info.titleFound)
  2517. {
  2518. string contentMetaXml = CAFE_META_DIR.value + "\\meta.xml";
  2519. file_copy_retry(info.metaXml, contentMetaXml, true, 3);
  2520.  
  2521. // Get the title-id, version, app_type, etc from the WUMAD files
  2522. string title_id = XmlHandler.GetNodeValue(info.appXml, "title_id");
  2523. string title_version = XmlHandler.GetNodeValue(info.appXml, "title_version");
  2524. string os_version = XmlHandler.GetNodeValue(info.appXml, "os_version");
  2525. string app_type = XmlHandler.GetNodeValue(info.appXml, "app_type");
  2526. string app_group_id = XmlHandler.GetNodeValue(info.appXml, "group_id");
  2527.  
  2528. // Save config to meta.xml
  2529. XmlHandler.UpdateNodeValue(contentMetaXml, "title_id", title_id);
  2530. XmlHandler.UpdateNodeValue(contentMetaXml, "group_id", app_group_id);
  2531. XmlHandler.UpdateNodeValue(contentMetaXml, "title_version", title_version);
  2532.  
  2533. // Save config to system.xml
  2534. XmlHandler.UpdateNodeValue(SYSTEM_XML.value, "default_title_id", title_id);
  2535. XmlHandler.UpdateNodeValue(SYSTEM_XML.value, "default_app_type", app_type);
  2536. XmlHandler.UpdateNodeValue(SYSTEM_XML.value, "default_os_id", os_version);
  2537. }
  2538.  
  2539. // Starts the emulation
  2540. if (MAKE_DLF_ONLY == 0)
  2541. {
  2542. hostrun(BOOT_DLF_FILE.value + " " + wumadDlfName);
  2543. }
  2544. }
  2545. else
  2546. {
  2547. // HFIO and HDD cases
  2548. if (MAKE_DLF_ONLY == 0)
  2549. {
  2550. hostrun(BOOT_DLF_FILE.value);
  2551. }
  2552. }
  2553. }
  2554. }
  2555.  
  2556. return CAFEX_ERROR.OK;
  2557. }
  2558.  
  2559. //Script function: cafedevrun
  2560. static CAFEX_ERROR devrun(string elf, string MAKE_PATCH, string CAFEMAKEDLF_OPTION, string KIOSK_DEFAULT_DDF)
  2561. {
  2562. #if DEBUG
  2563. Log.WriteLine("devrun started.");
  2564. #endif
  2565.  
  2566. APP_TITLE_ID.value = "0000000000000000";
  2567. APP_GROUP_ID.value = "00000000";
  2568. APP_TITLE_VERSION.value = "0000";
  2569.  
  2570. if (elf == null)
  2571. {
  2572. Console.WriteLine("cafex cafedevrun failed: image file required!");
  2573. return CAFEX_ERROR.CAFEDEVRUN_NO_IMAGE_FILE;
  2574. }
  2575.  
  2576. CAFEDEVRUN_FILE.value = elf;
  2577. CAFEDEVRUN_EXT.value = Path.GetExtension(elf).Remove(0, 1);
  2578. CAFEDEVRUN_NAME.value = Path.GetDirectoryName(elf) + "\\" + Path.GetFileNameWithoutExtension(elf);
  2579. CAFEDEVRUN_FILENAME.value = Path.GetFileNameWithoutExtension(elf);
  2580. CAFEDEVRUN_DIR.value = Path.GetDirectoryName(elf);
  2581.  
  2582. string GAME_TYPE = null;
  2583. if (MAKE_PATCH != null && MAKE_PATCH == "1")
  2584. {
  2585. GAME_TYPE = "GP";
  2586. }
  2587. else
  2588. {
  2589. GAME_TYPE = "GM";
  2590. }
  2591.  
  2592. // Check temporary directory
  2593. CAFEDEVRUN_WORK_DIR.value = CAFEDEVRUN_NAME.value + "_tmp";
  2594. create_directory_if_noexist(CAFEDEVRUN_WORK_DIR.value);
  2595.  
  2596. if (INCLUDE_SYSTEM_DIR.value == null)
  2597. {
  2598. INCLUDE_SYSTEM_DIR.value = "0";
  2599. }
  2600.  
  2601. CAFE_BASEFILE_PATH.value = CAFE_ROOT.value + "\\system\\bin\\tool\\mastering\\resources";
  2602. if (string.IsNullOrEmpty(KIOSK_DEFAULT_DDF))
  2603. {
  2604. DEFAULT_DDF.value = CAFE_BASEFILE_PATH.value + "\\default.ddf";
  2605. }
  2606. else
  2607. {
  2608. Console.WriteLine("Change default ddf -> " + KIOSK_DEFAULT_DDF);
  2609. DEFAULT_DDF.value = KIOSK_DEFAULT_DDF;
  2610. }
  2611.  
  2612. COMMON_DDF_FILE.value = CAFEDEVRUN_WORK_DIR.value + "\\common.ddf";
  2613.  
  2614. string custom_ddf = Path.ChangeExtension(elf, ".ddf");
  2615. // Use individual ddf file if exists
  2616. if (File.Exists(custom_ddf))
  2617. {
  2618. INDIVIDUAL_DDF_FILE.value = custom_ddf;
  2619. // TODO: Need to replace the game type in the custo_ddf file and save it.
  2620. }
  2621. else
  2622. {
  2623. INDIVIDUAL_DDF_FILE.value = "";
  2624. }
  2625.  
  2626. APP_XML_FILE.value = CAFEDEVRUN_DIR.value + "\\app.xml";
  2627. if (!File.Exists(APP_XML_FILE.value))
  2628. {
  2629. Console.WriteLine("cafex cafedevrun failed: Cannot find " + APP_XML_FILE.value);
  2630. return CAFEX_ERROR.CAFEDEVRUN_CANT_FIND_APP_XML;
  2631. }
  2632.  
  2633. extract_info_from_app_xml(APP_XML_FILE.value);
  2634.  
  2635. FileStream fs = new FileStream(COMMON_DDF_FILE.value, FileMode.Create);
  2636. StreamWriter sw = new StreamWriter(fs);
  2637. sw.WriteLine(";");
  2638. sw.WriteLine("; " + CAFEDEVRUN_NAME.value + "." + CAFEDEVRUN_EXT.value);
  2639. sw.WriteLine(";");
  2640. sw.WriteLine("[Input]");
  2641. sw.WriteLine("DiscHeaderBaseFileName=\"" + CAFE_BASEFILE_PATH.value + "\\discheader_base.bin\"");
  2642. sw.WriteLine("VolumeHeaderBaseFileName=\"" + CAFE_BASEFILE_PATH.value + "\\volheader_base.bin\"");
  2643. sw.WriteLine("[Output]");
  2644. sw.WriteLine("LayoutFileName=\"" + CAFEDEVRUN_NAME.value + ".dlf\"");
  2645. sw.WriteLine("OutputDirectory=\"" + CAFEDEVRUN_DIR.value + "\"");
  2646. sw.WriteLine("AppName=\"" + CAFEDEVRUN_FILENAME.value + "\"");
  2647. sw.WriteLine("Mode=WU");
  2648. sw.WriteLine("[DiscID]");
  2649. sw.WriteLine("GameType=" + GAME_TYPE);
  2650. sw.WriteLine("TitleId=0x" + APP_TITLE_ID.value);
  2651. sw.WriteLine("GroupId=0x" + APP_GROUP_ID.value);
  2652. sw.WriteLine("DiskNumber=0");
  2653. sw.WriteLine("GameVersion=" + (HexHandler.GetNumberFromString("0x" + APP_TITLE_VERSION.value) / 16).ToString());
  2654. sw.WriteLine("[SectionParam]");
  2655. sw.WriteLine("\"CONTENT\"=0x" + APP_TITLE_ID.value + ",0x" + APP_GROUP_ID.value + ",0x02");
  2656. sw.WriteLine("[PathList]");
  2657. sw.WriteLine("CODE_DIR_RPX=\"code\", \"$(CAFE_CODE_DIR)\\" + CAFEDEVRUN_FILENAME.value + ".rpx\"");
  2658.  
  2659. if (INCLUDE_SYSTEM_DIR.value == "1")
  2660. {
  2661. sw.WriteLine("[PathList]");
  2662. sw.WriteLine("SYSTEM_DIR=\"sys\", \"$(CAFE_SYSTEM_DIR)\"");
  2663. }
  2664.  
  2665. sw.Flush();
  2666. sw.Close();
  2667.  
  2668. int cafemakedlf_ret = cafemakedlf.make(DEFAULT_DDF.value, COMMON_DDF_FILE.value, INDIVIDUAL_DDF_FILE.value, CAFEMAKEDLF_OPTION);
  2669.  
  2670. if (cafemakedlf_ret != 0)
  2671. {
  2672. return CAFEX_ERROR.CAFEDEVRUN_CAFEMAKEDLF_FAILED;
  2673. }
  2674.  
  2675. fs = new FileStream(CAFEDEVRUN_NAME.value + ".dlf", FileMode.Append);
  2676. sw = new StreamWriter(fs);
  2677. sw.WriteLine("0x00000005D3A00000,\"\"");
  2678. sw.Flush();
  2679. sw.Close();
  2680.  
  2681. return CAFEX_ERROR.OK;
  2682. }
  2683.  
  2684. //Script function: hostrun
  2685. static void hostrun(string arguments)
  2686. {
  2687. #if DEBUG
  2688. Log.WriteLine("hostrun started. Arguments:" + arguments);
  2689. #endif
  2690.  
  2691. getbridgetype();
  2692.  
  2693. FSEMUL_PARAMS.value = null;
  2694. if (BRIDGE_TYPE.value == "Toucan")
  2695. {
  2696. BRIDGE_INSTALL_PATH.value = SDIO_BRIDGE_TOOLS.value;
  2697. if (CAFE_HARDWARE.value.StartsWith("ev"))
  2698. {
  2699. FSEMUL_PARAMS.value = "-m";
  2700. }
  2701. }
  2702. else
  2703. {
  2704. BRIDGE_INSTALL_PATH.value = MION_BRIDGE_TOOLS.value;
  2705. }
  2706.  
  2707. if (BRIDGE_TYPE.value == "Mion")
  2708. {
  2709. if (BRIDGE_PARAMETERS.value == null)
  2710. {
  2711. FSEMUL_PARAMS.value = "-em -ip " + BRIDGE_CURRENT_IP_ADDRESS.value;
  2712. }
  2713. else
  2714. {
  2715. FSEMUL_PARAMS.value = BRIDGE_PARAMETERS.value + " -ip " + BRIDGE_CURRENT_IP_ADDRESS.value;
  2716. }
  2717. }
  2718.  
  2719. // Need to figure out what the difference between these two options is in the scripts, and if it is possible to replicate here.
  2720. if (CAFE_DETACH_FSEMUL.value == null || CAFE_DETACH_FSEMUL.value != "0")
  2721. {
  2722. FSEmul.start(arguments, FSEMUL_PARAMS.value);
  2723. }
  2724. else
  2725. {
  2726. FSEmul.start(arguments, FSEMUL_PARAMS.value);
  2727. }
  2728. }
  2729.  
  2730. //Script function: cafediscrun
  2731. static CAFEX_ERROR discrun(string[] args)
  2732. {
  2733. #if DEBUG
  2734. Log.WriteLine("disrun started.");
  2735. string argString = null;
  2736. if (args != null)
  2737. {
  2738. foreach (string arg in args)
  2739. {
  2740. argString += arg + " ";
  2741. }
  2742. }
  2743. Log.WriteLine("Arguments=" + argString);
  2744.  
  2745. #endif
  2746.  
  2747. CAFERUN_OPTION_CALLED_FROM_CAFEDISCRUN.value = "yes";
  2748. return run(args);
  2749. }
  2750.  
  2751. //Script function: cafeon
  2752. //Script function: cafeon
  2753. static CAFEX_ERROR on(string[] args)
  2754. {
  2755. #if DEBUG
  2756. Log.WriteLine("on started.");
  2757. string argString = null;
  2758. if (args != null)
  2759. {
  2760. foreach (string arg in args)
  2761. {
  2762. argString += arg + " ";
  2763. }
  2764. }
  2765. Log.WriteLine("Arguments=" + argString);
  2766.  
  2767. #endif
  2768. List<string> argList = new List<string>();
  2769.  
  2770. CAFEON_DISABLE_BGD.value = "0";
  2771. CAFEON_NOBGD_META.value = "";
  2772. BRIDGE_PARAMETERS_WITH_E.value = "";
  2773. CAFE_RUN_DEBUG.value = "";
  2774.  
  2775. CAFEON_OPTION_NO_DATA_SYNC.value = CAFERUN_OPTION_NO_DATA_SYNC.value;
  2776.  
  2777. if (CAFEON_OPTION_NO_DATA_SYNC.value == null)
  2778. {
  2779. CAFEON_OPTION_NO_DATA_SYNC.value = "0";
  2780. }
  2781.  
  2782. if (CAFE_RUN_RUNNING.value == null)
  2783. {
  2784. CAFE_RUN_RUNNING.value = "0";
  2785. }
  2786.  
  2787. if (CAFE_RUN_RUNNING.value != "0")
  2788. {
  2789. Console.WriteLine("****cafex on: RECURSIVE CALL TO cafex on EXITING***");
  2790. return CAFEX_ERROR.ON_RECURSIVE_CALL;
  2791. }
  2792.  
  2793. CAFE_RUN_RUNNING.value = "1";
  2794.  
  2795. if (args != null && args.Length > 0)
  2796. {
  2797. for (int i = 0; i < args.Length; ++i)
  2798. {
  2799. if (args[i] == "-noprompt")
  2800. {
  2801. CATTOUCAN_TERM.value = "source -p -q /vol/content";
  2802. }
  2803. else if (args[i] == "-d")
  2804. {
  2805. CAFE_RUN_DEBUG.value = args[i] + " " + args[i + 1];
  2806. ++i;
  2807. }
  2808. else if (args[i] == "-e")
  2809. {
  2810. BRIDGE_PARAMETERS_WITH_E.AddToVar(args[i] + " " + args[i + 1], ' ');
  2811. ++i;
  2812. }
  2813. else if (args[i] == "-nobgd")
  2814. {
  2815. CAFEON_DISABLE_BGD.value = "1";
  2816. }
  2817. else if (args[i] == "-nosync")
  2818. {
  2819. if (string.IsNullOrEmpty(CAFE_DATA_DIR.value) ||
  2820. !Directory.Exists(CAFE_DATA_DIR.value))
  2821. {
  2822. Console.WriteLine("Session data directory is missing and needs to be sync'd!");
  2823. Console.WriteLine(" Please re-run without the '-nosync' option.");
  2824. return CAFEX_ERROR.ON_DATA_DIR_MISSING;
  2825. }
  2826.  
  2827. CAFEON_OPTION_NO_DATA_SYNC.value = "1";
  2828. }
  2829. else
  2830. {
  2831. // This is an argument that needs to be passed down
  2832. argList.Add(args[i]);
  2833. }
  2834. }
  2835. }
  2836.  
  2837. if (CAFEON_DISABLE_BGD.value == "1")
  2838. {
  2839. if (!string.IsNullOrEmpty(CAFE_META_DIR.value) &&
  2840. Directory.Exists(CAFE_META_DIR.value))
  2841. {
  2842. CAFEON_NOBGD_META.value = TEMP.value + "\\" + SESSION_PATH_PREFIX.value + "tmp_meta";
  2843. Console.WriteLine("Disabling background daemons in tmp dir " + CAFEON_NOBGD_META.value);
  2844. DirectoryInfo originMetaDiretory = new DirectoryInfo(CAFE_META_DIR.value);
  2845. FileSystemInfo[] filesToCopy = originMetaDiretory.GetFileSystemInfos();
  2846. if (filesToCopy.Length > 0)
  2847. {
  2848. create_directory_if_noexist(CAFEON_NOBGD_META.value);
  2849. string[] meta_files = Directory.GetFiles(CAFE_META_DIR.value);
  2850. foreach (FileSystemInfo file in filesToCopy)
  2851. {
  2852. string fileName = file.FullName;
  2853. string destinationFileName = CAFEON_NOBGD_META.value + "\\" + Path.GetFileName(fileName);
  2854. file_copy_retry(fileName, destinationFileName, true, 3);
  2855. }
  2856.  
  2857. XmlHandler.UpdateNodeValue(CAFEON_NOBGD_META.value + "\\meta.xml", "bg_daemon_enable", "0");
  2858.  
  2859. CAFE_META_DIR.value = CAFEON_NOBGD_META.value;
  2860. }
  2861. }
  2862. }
  2863.  
  2864. OS_VERSION_LO.value = XmlHandler.GetNodeValue(CAFE_ROOT.value + "\\data\\mlc\\sys\\title\\00050010\\1f700500\\code\\app.xml", "os_version").Substring(8, 6);
  2865. string total_arglist_with_qs = StringCombiner.MakeDelimitedString(argList.ToArray(), ' ').Replace(' ', '?').Replace("-c?", "-c ").Replace("?-c", " -c");
  2866.  
  2867. if (OS_VERSION_LO.value == null)
  2868. {
  2869. Console.WriteLine("cafex on error: unable to locate System Config Tool app.xml or file is corrupt");
  2870. }
  2871. else
  2872. {
  2873. if (CAFEON_OPTION_NO_DATA_SYNC.value == "0")
  2874. {
  2875. syncsession();
  2876. }
  2877.  
  2878. XmlHandler.UpdateNodeValue(CAFE_DATA_DIR.value + "\\mlc\\sys\\title\\00050010\\1f700500\\code\\cos.xml", "argstr", "system_config_tool.rpx " + total_arglist_with_qs);
  2879.  
  2880. // Keep the current command stored, so when run is called it knows it has last been called by 'on'
  2881. string currentCommand = command;
  2882. command = "on";
  2883. string OS_string = string.Empty;
  2884. if (OS_VERSION_LO.value == "100040")
  2885. {
  2886. // NDEBUG OS
  2887. OS_string = CAFE_ROOT.value + "\\system\\bin\\ghs\\cafe\\app\\system_config_tool\\base\\NDEBUG\\system_config_tool.rpx";
  2888. }
  2889. else if (OS_VERSION_LO.value == "100080")
  2890. {
  2891. // DEBUG OS
  2892. OS_string = "-b" + DEFAULT_DELIMITER + CAFE_ROOT.value + "\\system\\bin\\ghs\\cafe\\app\\system_config_tool\\base\\DEBUG\\system_config_tool.rpx";
  2893. }
  2894.  
  2895. string run_args = (!string.IsNullOrEmpty(CAFE_RUN_DEBUG.value) ? CAFE_RUN_DEBUG.value.Replace(' ', DEFAULT_DELIMITER) : string.Empty) +
  2896. DEFAULT_DELIMITER + (!string.IsNullOrEmpty(BRIDGE_PARAMETERS_WITH_E.value) ? BRIDGE_PARAMETERS_WITH_E.value.Replace(' ', DEFAULT_DELIMITER) : string.Empty) +
  2897. DEFAULT_DELIMITER + "-e" + DEFAULT_DELIMITER + "mcp:launch_hint:hfiomlc" + DEFAULT_DELIMITER + "-z" + DEFAULT_DELIMITER +
  2898. "-t" + DEFAULT_DELIMITER + "0x" + SYSCONFIGTOOL_TITLE_ID + DEFAULT_DELIMITER + OS_string;
  2899.  
  2900. if (argList != null)
  2901. {
  2902. run_args += DEFAULT_DELIMITER + StringCombiner.MakeDelimitedString(argList.ToArray(), DEFAULT_DELIMITER);
  2903. }
  2904.  
  2905. run(run_args.Split(new char[] { DEFAULT_DELIMITER }, StringSplitOptions.RemoveEmptyEntries));
  2906.  
  2907. // Return the current command
  2908. command = currentCommand;
  2909. XmlHandler.UpdateNodeValue(CAFE_DATA_DIR.value + "\\mlc\\sys\\title\\00050010\\1f700500\\code\\cos.xml", "argstr", "system_config_tool.rpx");
  2910. }
  2911.  
  2912. return CAFEX_ERROR.OK;
  2913. }
  2914.  
  2915. //Script function: cafeupdate
  2916. static CAFEX_ERROR update(string[] args)
  2917. {
  2918. #if DEBUG
  2919. Log.WriteLine("update started.");
  2920. string argString = null;
  2921. if (args != null)
  2922. {
  2923. foreach (string arg in args)
  2924. {
  2925. argString += arg + " ";
  2926. }
  2927. }
  2928. Log.WriteLine("Arguments=" + argString);
  2929.  
  2930. #endif
  2931.  
  2932. CAFEUPDATE_USE_SYSTEM_UPDATER.value = "0";
  2933. CAFEUPDATE_TITLE.value = "";
  2934. CAFEUPDATE_PACKAGE_DEFAULT.value = "/vol/storage_hfiomlc01/sys/update/pcfs";
  2935. CAFEUPDATE_PACKAGE.value = CAFEUPDATE_PACKAGE_DEFAULT.value;
  2936.  
  2937. for (int i = 0; i < args.Length; ++i)
  2938. {
  2939. switch (args[i])
  2940. {
  2941. case "-u":
  2942. {
  2943. if (!ArgumentChecks.IsArgumentValidSetting(args, i + 1))
  2944. {
  2945. run_usage();
  2946. return CAFEX_ERROR.BAD_ARGUMENT;
  2947. }
  2948.  
  2949. CAFEUPDATE_PACKAGE.value = args[i + 1];
  2950. ++i;
  2951. break;
  2952. }
  2953.  
  2954. case "-s":
  2955. {
  2956. CAFEUPDATE_USE_SYSTEM_UPDATER.value = "1";
  2957. break;
  2958. }
  2959.  
  2960. case "-t":
  2961. {
  2962. if (!ArgumentChecks.IsArgumentValidSetting(args, i + 1))
  2963. {
  2964. run_usage();
  2965. return CAFEX_ERROR.BAD_ARGUMENT;
  2966. }
  2967.  
  2968. CAFEUPDATE_TITLE.value = args[i + 1];
  2969. ++i;
  2970. break;
  2971. }
  2972.  
  2973. case "-h":
  2974. {
  2975. update_usage();
  2976. return CAFEX_ERROR.OK;
  2977. }
  2978.  
  2979. default:
  2980. {
  2981. Console.WriteLine("cafex update failed: Invalid argument");
  2982. return CAFEX_ERROR.UPDATE_INVALID_OPTION;
  2983. }
  2984. }
  2985. }
  2986.  
  2987. string catdev_sdk_ver = SDK_MAJ_VER.value + "." + SDK_MIN_VER.value + "." + SDK_MIC_VER.value;
  2988. string sdk_ver = SDK_VER.value;
  2989. if (catdev_sdk_ver != SDK_VER.value)
  2990. {
  2991. if (mionps.Run(BRIDGE_CURRENT_IP_ADDRESS.value + " 3 -s " + sdk_ver.Substring(0, 1)) != sdk_ver.Substring(0, 1))
  2992. Console.WriteLine("cafex unable to set the SDK Major version." + sdk_ver.Substring(0, 1));
  2993. if (mionps.Run(BRIDGE_CURRENT_IP_ADDRESS.value + " 4 -s " + sdk_ver.Substring(2, 2).Replace(".", "")) != sdk_ver.Substring(2, 2).Replace(".", ""))
  2994. Console.WriteLine("cafex unable to set the SDK Minor version." + sdk_ver.Substring(2, 2).Replace(".", "") + sdk_ver);
  2995. if (mionps.Run(BRIDGE_CURRENT_IP_ADDRESS.value + " 5 -s " + sdk_ver.Substring(sdk_ver.IndexOf('.', 3)).Replace(".", "")) != sdk_ver.Substring(sdk_ver.IndexOf('.', 3)).Replace(".", ""))
  2996. Console.WriteLine("cafex unable to set the SDK Micro version." + sdk_ver.Substring(sdk_ver.IndexOf('.', 3)).Replace(".", ""));
  2997. }
  2998.  
  2999. if (CAFE_BOOT_MODE.value == "NAND")
  3000. {
  3001. Console.WriteLine("Sourcing script setbootmode NAND...");
  3002. return setbootmode(new string[] { "NAND" });
  3003. }
  3004. else
  3005. {
  3006. if (CAFE_HARDWARE.value.StartsWith("ev") || CAFE_SECURITY.value == "off")
  3007. {
  3008. Console.WriteLine("Sorry, bringup boot1 update not supported yet");
  3009. return CAFEX_ERROR.UPDATE_BRINGUP_BOOT1_UPDATE;
  3010. }
  3011.  
  3012. if (CAFEUPDATE_TITLE.value != null)
  3013. {
  3014. Console.WriteLine("Installing title " + CAFEUPDATE_TITLE.value + " using System Config Tool...");
  3015. CAFEX_ERROR on_ret = on(new string[] { "-c", "install " + CAFEUPDATE_TITLE.value, "-c", "exit" });
  3016. if (on_ret != CAFEX_ERROR.OK)
  3017. return on_ret;
  3018. }
  3019. else
  3020. {
  3021. if (CAFEUPDATE_PACKAGE.value == CAFEUPDATE_PACKAGE_DEFAULT.value)
  3022. {
  3023. Console.WriteLine("Performing system update using System Config Tool...");
  3024. }
  3025. else
  3026. {
  3027. Console.WriteLine("Performing system update using System Config Tool from " + CAFEUPDATE_PACKAGE.value + "...");
  3028. }
  3029.  
  3030. if (CAFEUPDATE_USE_SYSTEM_UPDATER.value == "1")
  3031. {
  3032. Console.WriteLine("Using system updater...");
  3033. CAFEX_ERROR on_ret = on(new string[] { "-c", "update_launch " + CAFEUPDATE_PACKAGE.value });
  3034. if (on_ret != CAFEX_ERROR.OK)
  3035. return on_ret;
  3036. }
  3037. else
  3038. {
  3039. CAFEX_ERROR on_ret = on(new string[] { "-c", "update " + CAFEUPDATE_PACKAGE.value, "-c", "exit" });
  3040. if (on_ret != CAFEX_ERROR.OK)
  3041. return on_ret;
  3042. }
  3043. }
  3044. }
  3045.  
  3046. return CAFEX_ERROR.OK;
  3047. }
  3048.  
  3049. //Script function: setbootmode
  3050. static CAFEX_ERROR setbootmode(string[] args)
  3051. {
  3052. #if DEBUG
  3053. Log.WriteLine("setbootmode started.");
  3054. string argString = null;
  3055. if (args != null)
  3056. {
  3057. foreach (string arg in args)
  3058. {
  3059. argString += arg + " ";
  3060. }
  3061. }
  3062. Log.WriteLine("Arguments=" + argString);
  3063.  
  3064. #endif
  3065.  
  3066. if (!ArgumentChecks.ArgumentExists(args, 0))
  3067. {
  3068. setbootmode_usage();
  3069. return CAFEX_ERROR.BAD_ARGUMENT;
  3070. }
  3071.  
  3072.  
  3073. CAFEX_ERROR returnValue = CAFEX_ERROR.SETBOOTMODE_FAILED;
  3074. int reflash = 1;
  3075. string target = string.Empty;
  3076. if (args[0] == "-noreflash")
  3077. {
  3078. reflash = 0;
  3079. if (!ArgumentChecks.IsArgumentValidSetting(args, 1))
  3080. {
  3081. setbootmode_usage();
  3082. return CAFEX_ERROR.BAD_ARGUMENT;
  3083. }
  3084.  
  3085. target = args[1];
  3086. }
  3087. else
  3088. {
  3089. target = args[0];
  3090. }
  3091.  
  3092. string status_str = "FAIL";
  3093.  
  3094. switch (target)
  3095. {
  3096. case "PCFS":
  3097. {
  3098. Console.WriteLine("Executing cafex nand2pcfs...");
  3099. int ret = nand2pcfs();
  3100. if (ret == 0)
  3101. {
  3102. CAFE_BOOT_MODE.value = "PCFS";
  3103. Console.WriteLine("CAFE_BOOT_MODE=" + CAFE_BOOT_MODE.value);
  3104. status_str = "PASS";
  3105. returnValue = CAFEX_ERROR.OK;
  3106. }
  3107. break;
  3108. }
  3109.  
  3110. case "NAND":
  3111. {
  3112. Console.WriteLine("Executing cafex pcfs2nand...");
  3113. int ret = pcfs2nand(reflash);
  3114. if (ret == 0)
  3115. {
  3116. CAFE_BOOT_MODE.value = "NAND";
  3117. Console.WriteLine("CAFE_BOOT_MODE=" + CAFE_BOOT_MODE.value);
  3118. status_str = "PASS";
  3119. returnValue = CAFEX_ERROR.OK;
  3120. }
  3121. break;
  3122. }
  3123.  
  3124. default:
  3125. {
  3126. Console.WriteLine("cafex setbootmode error: Invalid boot mode");
  3127. status_str = "BAD MODE";
  3128. setbootmode_usage();
  3129. returnValue = CAFEX_ERROR.SETBOOTMODE_BAD_MODE;
  3130. break;
  3131. }
  3132. }
  3133.  
  3134. Console.WriteLine("cafex setbootmode: " + status_str);
  3135.  
  3136. return returnValue;
  3137. }
  3138.  
  3139. //Script function: cafe_nand2pcfs
  3140. static int nand2pcfs()
  3141. {
  3142. #if DEBUG
  3143. Log.WriteLine("nand2pcfs started.");
  3144. #endif
  3145.  
  3146. // Initialization
  3147. SCRIPT_EXE_TIMESTAMP.value = DateTime.Now.ToString("MMMdd_yyyy_HHmmss");
  3148. LOGDIR.value = CAFE_ROOT.value + "\\" + SESSION_PATH_PREFIX.value + "cafe_nand2pcfs\\" + SCRIPT_EXE_TIMESTAMP.value;
  3149. Console.WriteLine("cafe_nand2pcfs");
  3150. Console.WriteLine("log directory is " + LOGDIR.value);
  3151. create_directory_if_noexist(LOGDIR.value);
  3152.  
  3153. File.Delete(TMP.value + "\\" + SESSION_PATH_PREFIX.value + "cafestop_mion.log");
  3154.  
  3155. CAFESTOP_STATUS.value = "0";
  3156. _CCRSYSCFG1.value = "";
  3157. if (initialize_serial_port_and_directory("cafestop") != 0)
  3158. return 1;
  3159.  
  3160. //cafestop
  3161. Console.WriteLine("Executing cafex stop...");
  3162. FileStream fs = new FileStream(LOGDIR.value + "\\cafestop0.txt", FileMode.Create);
  3163. TextWriter old_out = Console.Out;
  3164. TextWriter old_err = Console.Error;
  3165. StreamWriter sw = new StreamWriter(fs);
  3166. Console.SetOut(sw);
  3167. Console.SetError(sw);
  3168.  
  3169. stop(null);
  3170.  
  3171. Console.SetOut(old_out);
  3172. Console.SetError(old_err);
  3173. sw.Flush();
  3174. sw.Close();
  3175.  
  3176. if (File.Exists(TMP.value + "\\" + SESSION_PATH_PREFIX.value + "cafestop_mion.log"))
  3177. {
  3178. File.Move(TMP.value + "\\" + SESSION_PATH_PREFIX.value + "cafestop_mion.log", LOGDIR.value + "\\cafestop0_mion.log");
  3179. }
  3180.  
  3181. if (CAFESTOP_STATUS.value != "0")
  3182. {
  3183. Console.WriteLine("cafex nand2pcfs: cafestop failure.");
  3184. return 1;
  3185. }
  3186.  
  3187. //cafeon
  3188. CAFEX_ERROR hostcheckversion_ret = hostcheckversion();
  3189.  
  3190. if (hostcheckversion_ret != CAFEX_ERROR.OK)
  3191. {
  3192. Console.WriteLine("cafex nand2pcfs: failed running hostcheckversion.");
  3193. return 1;
  3194. }
  3195.  
  3196. int hb_ver_flat = compute_flat_version(CAFERUN_HOSTBRIDGE_VERSION.value);
  3197.  
  3198. Console.WriteLine("Executing cafex on...");
  3199. fs = new FileStream(LOGDIR.value + "\\cafeon.txt", FileMode.Create);
  3200. old_out = Console.Out;
  3201. old_err = Console.Error;
  3202. sw = new StreamWriter(fs);
  3203. Console.SetOut(sw);
  3204. Console.SetError(sw);
  3205.  
  3206. //quiet_mode = true; // This breaks setbootmode in NAND/softlaunch
  3207. Console.WriteLine("at on command");
  3208. if (hb_ver_flat < compute_flat_version("3.2.4.1"))
  3209. {
  3210. on(null);
  3211. }
  3212. else
  3213. {
  3214. on(new string[] { "-e", "nomodecheck" });
  3215. }
  3216.  
  3217. Console.WriteLine("after on command");
  3218.  
  3219. Console.SetOut(old_out);
  3220. Console.SetError(old_err);
  3221. sw.Flush();
  3222. sw.Close();
  3223.  
  3224. if (File.Exists(TMP.value + "\\" + SESSION_PATH_PREFIX.value + "cafestop_mion.log"))
  3225. {
  3226. File.Move(TMP.value + "\\" + SESSION_PATH_PREFIX.value + "cafestop_mion.log", LOGDIR.value + "\\cafeon_mion.log");
  3227. }
  3228.  
  3229. if (monitor_for_output("-k", "source -p -q /vol/content/", MONITOR_DEFAULT_TIMEOUT.value, LOGDIR.value, null, true) != 0)
  3230. {
  3231. Console.WriteLine("cafex nand2pcfs: monitor failed waiting for cafeon success message.");
  3232. return 1;
  3233. }
  3234.  
  3235. //recover command
  3236. Console.WriteLine("Executing devkitmsg recover -v...");
  3237. devkitmsg.recover();
  3238.  
  3239. if (monitor_for_output("-K", "WUD_BCMFWCheck", MONITOR_DEFAULT_TIMEOUT.value, LOGDIR.value, null, true) != 0)
  3240. {
  3241. Console.WriteLine("cafex nand2pcfs: monitor failed waiting for devkitmsg recover success message.");
  3242. return 1;
  3243. }
  3244.  
  3245. //update to PCFS
  3246. Console.WriteLine("Executing devkitmsg 'update /vol/storage_hfiomlc01/sys/update/pcfs' -v...");
  3247. devkitmsg.update_pcfs();
  3248.  
  3249. if (monitor_for_output("-k", "Update Done, err 0", MONITOR_DEFAULT_TIMEOUT.value, LOGDIR.value, null, true) != 0)
  3250. {
  3251. Console.WriteLine("cafex nand2pcfs: monitor failed waiting for devkitmsg 1st update success message.");
  3252. return 1;
  3253. }
  3254.  
  3255. // Set the CAT-DEV mode inside mion...
  3256. if (mionps.Run(BRIDGE_CURRENT_IP_ADDRESS.value + " 2 -s 2") != "2")
  3257. {
  3258. Console.WriteLine("cafex unable to set mode using mionps.");
  3259. return 1;
  3260. }
  3261.  
  3262. //clr_usrdata command
  3263. Console.WriteLine("Executing devkitmsg clr_usrdata -v...");
  3264. devkitmsg.clr_usrdata();
  3265.  
  3266. if (monitor_for_output("-k", "Done, err 0", MONITOR_DEFAULT_TIMEOUT.value, LOGDIR.value, null, true) != 0)
  3267. {
  3268. Console.WriteLine("cafex nand2pcfs: monitor failed waiting for devkitmsg clr_usrdata success message.");
  3269. return 1;
  3270. }
  3271.  
  3272. //cafestop
  3273. Console.WriteLine("Executing cafex stop...");
  3274. fs = new FileStream(LOGDIR.value + "\\cafestop1.txt", FileMode.Create);
  3275. old_out = Console.Out;
  3276. old_err = Console.Error;
  3277. sw = new StreamWriter(fs);
  3278. Console.SetOut(sw);
  3279. Console.SetError(sw);
  3280.  
  3281. stop(null);
  3282.  
  3283. Console.SetOut(old_out);
  3284. Console.SetError(old_err);
  3285. sw.Flush();
  3286. sw.Close();
  3287.  
  3288. if (File.Exists(TMP.value + "\\" + SESSION_PATH_PREFIX.value + "cafestop_mion.log"))
  3289. {
  3290. File.Move(TMP.value + "\\" + SESSION_PATH_PREFIX.value + "cafestop_mion.log", LOGDIR.value + "\\cafestop1_mion.log");
  3291. }
  3292.  
  3293. if (CAFESTOP_STATUS.value != "0")
  3294. {
  3295. Console.WriteLine("cafex nand2pcfs: cafestop failure.");
  3296. return 1;
  3297. }
  3298.  
  3299. //success!
  3300. Console.WriteLine("Removing log directory " + LOGDIR.value + "...");
  3301. Directory.Delete(LOGDIR.value, true);
  3302. Console.WriteLine("cafex nand2pcfs complete. Exiting with code 0");
  3303.  
  3304. return 0;
  3305. }
  3306.  
  3307. //Script function: cafe_pcfs2nand
  3308. static int pcfs2nand(int reflash)
  3309. {
  3310. #if DEBUG
  3311. Log.WriteLine("pcfs2nand started.");
  3312. #endif
  3313.  
  3314. // Initialization
  3315. SCRIPT_EXE_TIMESTAMP.value = DateTime.Now.ToString("MMMdd_yyyy_HHmmss");
  3316. LOGDIR.value = CAFE_ROOT.value + "\\" + SESSION_PATH_PREFIX.value + "cafe_pcfs2nand\\" + SCRIPT_EXE_TIMESTAMP.value;
  3317. Console.WriteLine("cafe_pcfs2nand");
  3318. Console.WriteLine("log directory is " + LOGDIR.value);
  3319. create_directory_if_noexist(LOGDIR.value);
  3320.  
  3321. File.Delete(TMP.value + "\\" + SESSION_PATH_PREFIX.value + "cafestop_mion.log");
  3322.  
  3323. CAFESTOP_STATUS.value = "0";
  3324. _CCRSYSCFG1.value = "";
  3325. if (initialize_serial_port_and_directory("cafestop") != 0)
  3326. return 1;
  3327.  
  3328. //cafestop
  3329. Console.WriteLine("Executing cafex stop...");
  3330. FileStream fs = new FileStream(LOGDIR.value + "\\cafestop0.txt", FileMode.Create);
  3331. TextWriter old_out = Console.Out;
  3332. TextWriter old_err = Console.Error;
  3333. StreamWriter sw = new StreamWriter(fs);
  3334. Console.SetOut(sw);
  3335. Console.SetError(sw);
  3336.  
  3337. stop(null);
  3338.  
  3339. Console.SetOut(old_out);
  3340. Console.SetError(old_err);
  3341. sw.Flush();
  3342. sw.Close();
  3343.  
  3344. if (File.Exists(TMP.value + "\\" + SESSION_PATH_PREFIX.value + "cafestop_mion.log"))
  3345. {
  3346. File.Move(TMP.value + "\\" + SESSION_PATH_PREFIX.value + "cafestop_mion.log", LOGDIR.value + "\\cafestop0_mion.log");
  3347. }
  3348.  
  3349. if (CAFESTOP_STATUS.value != "0")
  3350. {
  3351. Console.WriteLine("cafex nand2pcfs: cafestop failure.");
  3352. return 1;
  3353. }
  3354.  
  3355. //cafeon
  3356. CAFEX_ERROR hostcheckversion_ret = hostcheckversion();
  3357. if (hostcheckversion_ret != CAFEX_ERROR.OK)
  3358. {
  3359. Console.WriteLine("cafex nand2pcfs: failed running hostcheckversion.");
  3360. return 1;
  3361. }
  3362.  
  3363. int hb_ver_flat = compute_flat_version(CAFERUN_HOSTBRIDGE_VERSION.value);
  3364.  
  3365. Console.WriteLine("Executing cafex on...");
  3366. fs = new FileStream(LOGDIR.value + "\\cafeon.txt", FileMode.Create);
  3367. old_out = Console.Out;
  3368. old_err = Console.Error;
  3369. sw = new StreamWriter(fs);
  3370. Console.SetOut(sw);
  3371. Console.SetError(sw);
  3372.  
  3373. //quiet_mode = true; // This breaks setbootmode in NAND/softlaunch
  3374. Console.WriteLine("at on command");
  3375. if (hb_ver_flat < compute_flat_version("3.2.4.1"))
  3376. {
  3377. on(null);
  3378. }
  3379. else
  3380. {
  3381. on(new string[] { "-e", "nomodecheck" });
  3382. }
  3383.  
  3384. Console.WriteLine("after on command");
  3385.  
  3386. Console.SetOut(old_out);
  3387. Console.SetError(old_err);
  3388. sw.Flush();
  3389. sw.Close();
  3390.  
  3391. if (File.Exists(TMP.value + "\\" + SESSION_PATH_PREFIX.value + "cafestop_mion.log"))
  3392. {
  3393. File.Move(TMP.value + "\\" + SESSION_PATH_PREFIX.value + "cafestop_mion.log", LOGDIR.value + "\\cafeon_mion.log");
  3394. }
  3395.  
  3396. if (monitor_for_output("-k", "source -p -q /vol/content/", MONITOR_DEFAULT_TIMEOUT.value, LOGDIR.value, null, true) != 0)
  3397. {
  3398. Console.WriteLine("cafex nand2pcfs: monitor failed waiting for cafeon success message.");
  3399. return 1;
  3400. }
  3401.  
  3402. bool was_pcfs_boot = false;
  3403. fs = new FileStream(LOGDIR.value + "\\monitor_1.txt", FileMode.Open, FileAccess.Read);
  3404. StreamReader sr = new StreamReader(fs);
  3405. string contents = sr.ReadToEnd();
  3406. sr.Close();
  3407. if (contents.Contains("BOOT1: Loaded firmware image from SDIO."))
  3408. {
  3409. was_pcfs_boot = true;
  3410. }
  3411.  
  3412. //recover command
  3413. Console.WriteLine("Executing devkitmsg recover -v...");
  3414. devkitmsg.recover();
  3415.  
  3416. if (monitor_for_output("-K", "WUD_BCMFWCheck", MONITOR_DEFAULT_TIMEOUT.value, LOGDIR.value, null, true) != 0)
  3417. {
  3418. Console.WriteLine("cafex nand2pcfs: monitor failed waiting for devkitmsg recover success message.");
  3419. return 1;
  3420. }
  3421.  
  3422. // if last boot was NAND, first update BOOT1 to PCFS
  3423. if (was_pcfs_boot == false)
  3424. {
  3425. Console.WriteLine("cafex pcfs2nand: NAND boot detected, BOOT1 will be updated before updating to NAND");
  3426. Console.WriteLine("Executing devkitmsg 'update /vol/storage_hfiomlc01/sys/update/pcfs' -v...");
  3427. devkitmsg.update_pcfs();
  3428.  
  3429. if (monitor_for_output("-k", "Update Done, err 0", MONITOR_DEFAULT_TIMEOUT.value, LOGDIR.value, null, true) != 0)
  3430. {
  3431. Console.WriteLine("cafex pcfs2nand: monitor failed waiting for devkitmsg 1st update success message.");
  3432. return 1;
  3433. }
  3434.  
  3435. }
  3436. else
  3437. {
  3438. Console.WriteLine("cafe_pcfs2nand: PCFS boot detected, no BOOT1 update needed");
  3439. }
  3440.  
  3441. //update command
  3442. if (reflash == 1)
  3443. {
  3444. Console.WriteLine("Executing devkitmsg 'reflash /vol/storage_hfiomlc01/sys/update/nand' -v...");
  3445. devkitmsg.update_reflash();
  3446.  
  3447. if (monitor_for_output("-k", "MCP: System update was successful", MONITOR_DEFAULT_TIMEOUT.value, LOGDIR.value, null, true) != 0)
  3448. {
  3449. Console.WriteLine("cafex pcfs2nand: monitor failed waiting for devkitmsg reflash success message.");
  3450. return 1;
  3451. }
  3452. }
  3453. else
  3454. {
  3455. Console.WriteLine("Executing devkitmsg 'update /vol/storage_hfiomlc01/sys/update/nand' -v...");
  3456. devkitmsg.update_nand();
  3457.  
  3458. if (monitor_for_output("-k", "Update Done, err 0", MONITOR_DEFAULT_TIMEOUT.value, LOGDIR.value, null, true) != 0)
  3459. {
  3460. Console.WriteLine("cafex pcfs2nand: monitor failed waiting for devkitmsg update success message.");
  3461. return 1;
  3462. }
  3463. }
  3464.  
  3465. // Set the CAT-DEV mode inside mion...
  3466. if (mionps.Run(BRIDGE_CURRENT_IP_ADDRESS.value + " 2 -s 1") != "1")
  3467. {
  3468. Console.WriteLine("cafex unable to set mode using mionps.");
  3469. return 1;
  3470. }
  3471.  
  3472. // set default boot mode to development
  3473. devkitmsg.sys_mode_dev();
  3474.  
  3475. //cafestop
  3476. Console.WriteLine("Executing cafex stop...");
  3477. fs = new FileStream(LOGDIR.value + "\\cafestop1.txt", FileMode.Create);
  3478. old_out = Console.Out;
  3479. old_err = Console.Error;
  3480. sw = new StreamWriter(fs);
  3481. Console.SetOut(sw);
  3482. Console.SetError(sw);
  3483.  
  3484. stop(null);
  3485.  
  3486. Console.SetOut(old_out);
  3487. Console.SetError(old_err);
  3488. sw.Flush();
  3489. sw.Close();
  3490.  
  3491. if (File.Exists(TMP.value + "\\" + SESSION_PATH_PREFIX.value + "cafestop_mion.log"))
  3492. {
  3493. File.Move(TMP.value + "\\" + SESSION_PATH_PREFIX.value + "cafestop_mion.log", LOGDIR.value + "\\cafestop1_mion.log");
  3494. }
  3495.  
  3496. if (CAFESTOP_STATUS.value != "0")
  3497. {
  3498. Console.WriteLine("cafex nand2pcfs: cafestop failure.");
  3499. return 1;
  3500. }
  3501.  
  3502. //success!
  3503. Console.WriteLine("Removing log directory " + LOGDIR.value + "...");
  3504. Directory.Delete(LOGDIR.value, true);
  3505. Console.WriteLine("cafex nand2pcfs complete. Exiting with code 0");
  3506.  
  3507. return 0;
  3508. }
  3509.  
  3510. //Script function: hostcheckversion
  3511. static CAFEX_ERROR hostcheckversion()
  3512. {
  3513. #if DEBUG
  3514. Log.WriteLine("hostcheckversion started.");
  3515. #endif
  3516.  
  3517. getbridgetype();
  3518.  
  3519. string min_fw_ver = string.Empty;
  3520. string min_sw_ver = string.Empty;
  3521.  
  3522. if (BRIDGE_TYPE.value == "Mion")
  3523. {
  3524. min_fw_ver = "0.0.13.88";
  3525. min_sw_ver = "3.2.2.8";
  3526. }
  3527. else
  3528. {
  3529. min_sw_ver = "2.9.1.5";
  3530. min_fw_ver = "2.8.0.13";
  3531. }
  3532.  
  3533. if (SDIO_BRIDGE_TOOLS.value == null)
  3534. {
  3535. if (MION_BRIDGE_TOOLS.value == null)
  3536. {
  3537. Console.WriteLine("cafex run failed: Please install at least HostBridgeSetup_" + min_sw_ver + ".exe and restart cafex_env.bat");
  3538. return CAFEX_ERROR.HOSTCHECKVERSION_NO_HOSTBRIDGE;
  3539. }
  3540. }
  3541.  
  3542. string fw_ver = string.Empty;
  3543. string sw_ver = string.Empty;
  3544.  
  3545. if (BRIDGE_TYPE.value == "Mion")
  3546. {
  3547. FSEmul.FW_SW_Version(BRIDGE_CURRENT_IP_ADDRESS.value, out fw_ver, out sw_ver);
  3548. }
  3549. else
  3550. {
  3551. FSEmul.FW_SW_Version(null, out fw_ver, out sw_ver);
  3552. }
  3553.  
  3554. if (fw_ver == string.Empty || sw_ver == string.Empty)
  3555. {
  3556. Console.WriteLine("cafex run failed: Cannot check Host Bridge version. Please cafex stop and try again.");
  3557. return CAFEX_ERROR.HOSTCHECKVERSION_FSEMUL_FAILED;
  3558. }
  3559.  
  3560. CAFERUN_HOSTBRIDGE_VERSION.value = sw_ver;
  3561. CAFERUN_FW_VERSION.value = fw_ver;
  3562.  
  3563. int fw_flat = compute_flat_version(fw_ver);
  3564. int sw_flat = compute_flat_version(sw_ver);
  3565. int min_fw_flat = compute_flat_version(min_fw_ver);
  3566. int min_sw_flat = compute_flat_version(min_sw_ver);
  3567.  
  3568. if (sw_flat < min_sw_flat || fw_flat < min_fw_flat)
  3569. {
  3570. Console.WriteLine("cafex run failed: Please install at least HostBridgeSetup_" + min_sw_ver + ".exe and restart cafex_env.bat");
  3571. Console.WriteLine(" Current software version is " + sw_ver + " (need " + min_sw_ver + ")");
  3572. Console.WriteLine(" Current firmware version is " + fw_ver + " (need " + min_fw_ver + ")");
  3573. return CAFEX_ERROR.HOSTCHECKVERSION_OLD_VERSIONS;
  3574. }
  3575.  
  3576. return CAFEX_ERROR.OK;
  3577. }
  3578.  
  3579. //Script function: syncsession
  3580. static void syncsession()
  3581. {
  3582. #if DEBUG
  3583. Log.WriteLine("syncsession started.");
  3584. #endif
  3585.  
  3586. if (SESSION_MANAGER.value == "1")
  3587. {
  3588. stop(null);
  3589.  
  3590. SYNCTOOL_SRC.value = CAFE_ROOT.value + "\\data";
  3591. SYNCTOOL_DEST.value = CAFE_DATA_DIR.value;
  3592. SYNCTOOL_CFG.value = CAFE_ROOT.value + "\\system\\bin\\tool\\synctool.data.xml";
  3593. SYNCTOOL_LOG.value = CAFE_ROOT.value + "\\" + SESSION_PATH_PREFIX.value + "sync.log";
  3594. Console.WriteLine("Synchronizing " + SYNCTOOL_SRC.value + " ===> " + SYNCTOOL_DEST.value + "...");
  3595. SYNCTOOL_RVAL.value = synctool.sync(SYNCTOOL_CFG.value, SYNCTOOL_SRC.value, SYNCTOOL_DEST.value, SYNCTOOL_LOG.value).ToString();
  3596.  
  3597. if (SYNCTOOL_RVAL.value != "0")
  3598. {
  3599. Console.WriteLine(" Session data sync FAILED with exit code " + SYNCTOOL_RVAL.value + "!");
  3600. Console.WriteLine(" See " + SYNCTOOL_LOG.value + " for details.");
  3601. }
  3602. else
  3603. {
  3604. Console.WriteLine(" Session data sync log at " + SYNCTOOL_LOG.value);
  3605. }
  3606. }
  3607. }
  3608.  
  3609. #endregion
  3610.  
  3611. #region SCRIPT HELPERS
  3612.  
  3613. static void initialize_work_directory(string directory)
  3614. {
  3615. #if DEBUG
  3616. Log.WriteLine("initialize_work_diretory started.");
  3617. #endif
  3618.  
  3619. if (!Directory.Exists(directory))
  3620. {
  3621. Directory.CreateDirectory(directory);
  3622. }
  3623.  
  3624. File.Delete(directory + "\\diskid.bin");
  3625. File.Delete(directory + "\\ppc.bsf");
  3626. File.Delete(directory + "\\ppc_boot.dlf");
  3627. }
  3628.  
  3629. static CAFEX_ERROR make_boot1_system_file(string bsf_file)
  3630. {
  3631. #if DEBUG
  3632. Log.WriteLine("make_boot1_system_file started.");
  3633. #endif
  3634.  
  3635. string flags = "0";
  3636.  
  3637. string firmware_filename = Path.GetFileName(FIRMWARE_FILE.value);
  3638. if (firmware_filename.StartsWith("fw"))
  3639. {
  3640. flags = "0x80000000";
  3641. }
  3642.  
  3643. if (makebsf.make(QUIET.value, bsf_file, flags) != 0)
  3644. {
  3645. Console.WriteLine("cafex bootrun failed: Cannot generate bsf file.");
  3646. return CAFEX_ERROR.BOOTRUN_MAKEBSF_FAILED;
  3647. }
  3648.  
  3649. return CAFEX_ERROR.OK;
  3650. }
  3651.  
  3652. static CAFEX_ERROR make_boot_dlf(string file_list, string output_dlf_file)
  3653. {
  3654. #if DEBUG
  3655. Log.WriteLine("make_boot_dlf started.");
  3656. #endif
  3657.  
  3658. string commandResult = string.Empty;
  3659. if (makedlf.make(QUIET.value, file_list, output_dlf_file, out commandResult) != 0)
  3660. {
  3661. Console.WriteLine("cafex bootrun failed: Cannot generate dlf file.");
  3662. Console.WriteLine("Command output: " + commandResult);
  3663. return CAFEX_ERROR.BOOTRUN_MAKEDLF_FAILED;
  3664. }
  3665.  
  3666. return CAFEX_ERROR.OK;
  3667. }
  3668.  
  3669. static CAFEX_ERROR make_wumad_dlf(string wumadFile, string extractionFolder, string output_dlf_file)
  3670. {
  3671. #if DEBUG
  3672. Log.WriteLine("make_wumad_dlf started. Arguments: wumadFile=" + wumadFile + ", extractionFolder=" + extractionFolder + ", output_dlf_file=" + output_dlf_file);
  3673. #endif
  3674.  
  3675. if (makewumaddlf.make(wumadFile, extractionFolder, output_dlf_file) != 0)
  3676. {
  3677. Console.WriteLine("cafex bootrun failed: Cannot generate dlf file from WUMAD.");
  3678. return CAFEX_ERROR.BOOTRUN_MAKEDLF_FAILED;
  3679. }
  3680.  
  3681. return CAFEX_ERROR.OK;
  3682. }
  3683.  
  3684. static void extract_info_from_app_xml(string app_xml_file)
  3685. {
  3686. #if DEBUG
  3687. Log.WriteLine("extract_info_from_app_xml started.");
  3688. #endif
  3689.  
  3690. APP_TITLE_ID.value = XmlHandler.GetNodeValue(app_xml_file, "title_id");
  3691. APP_GROUP_ID.value = XmlHandler.GetNodeValue(app_xml_file, "group_id");
  3692. APP_TITLE_VERSION.value = XmlHandler.GetNodeValue(app_xml_file, "title_version");
  3693.  
  3694. Console.WriteLine("App Title ID : " + APP_TITLE_ID.value);
  3695. Console.WriteLine("App Group ID : " + APP_GROUP_ID.value);
  3696. Console.WriteLine("App Title Version : " + APP_TITLE_VERSION.value);
  3697. }
  3698.  
  3699. static CAFEX_ERROR connect_serial_or_debugger(bool quiet_mode)
  3700. {
  3701. #if DEBUG
  3702. Log.WriteLine("connect_serial_or_debugger started. quiet_mode=" + quiet_mode.ToString() + ", DEBUGGER=" + DEBUGGER.value);
  3703. #endif
  3704.  
  3705. switch (DEBUGGER.value)
  3706. {
  3707. case "multi":
  3708. {
  3709. break;
  3710. }
  3711.  
  3712. case "gdb":
  3713. case "gdbtui":
  3714. {
  3715. break;
  3716. }
  3717.  
  3718. case "cattoucan":
  3719. {
  3720. cattoucan.start(quiet_mode, QUIET.value, SESSION_DEBUG_OUT_PORT.value);
  3721.  
  3722. return CAFEX_ERROR.OK;
  3723. }
  3724.  
  3725. case "none":
  3726. {
  3727. if (CONSOLE.value != "toucan")
  3728. {
  3729. CAFE_DEBUG_PORT.value = "toucan";
  3730. }
  3731. return CAFEX_ERROR.OK;
  3732. }
  3733.  
  3734. default:
  3735. {
  3736. Console.WriteLine("cafex run failed: " + DEBUGGER.value + " unsupported debugger.");
  3737. return CAFEX_ERROR.RUN_BAD_DEBUGGER;
  3738. }
  3739. }
  3740.  
  3741. if (CAFE_DEBUG_PORT.value == "toucan")
  3742. {
  3743. PORT.value = "localhost:" + SESSION_DEBUG_OUT_PORT.value;
  3744. }
  3745. else
  3746. {
  3747. Console.WriteLine("cafex run failed: Unknown debugger port " + CAFE_DEBUG_PORT.value);
  3748. return CAFEX_ERROR.RUN_BAD_DEBUGGER_PORT;
  3749. }
  3750.  
  3751. if (DEBUGGER.value == "multi")
  3752. {
  3753. #if DEBUG
  3754. Log.WriteLine("DEBUGGER = multi");
  3755. #endif
  3756. MULTIELF.value = "";
  3757. if (DEBUG_ELF_FILE.value != null)
  3758. {
  3759. MULTIELF.value = DEBUG_ELF_FILE.value;
  3760. }
  3761.  
  3762. CATTOUCAN_STATUS.value = cattoucan.start(quiet_mode, QUIET.value, PORT.value).ToString();
  3763.  
  3764. if (MULTI_CONNECT.value == null)
  3765. {
  3766. MULTI_CONNECT.value = "\"-connect=cafeserv -iphost localhost -ipport " + SESSION_DEBUG_CONTROL_PORT.value + " -console_ipport " + SESSION_DEBUG_OUT_PORT.value + " -auto_first\"";
  3767. }
  3768.  
  3769. #if DEBUG
  3770. Log.WriteLine("MULTI_CONNECT before calling multi = " + MULTI_CONNECT.value);
  3771. Log.WriteLine("CATTOUCAN_STATUS.value = " + CATTOUCAN_STATUS.value);
  3772. Log.WriteLine("CAFE_LAUNCH_DEBUG.value = " + CAFE_LAUNCH_DEBUG.value);
  3773. #endif
  3774.  
  3775. if (CATTOUCAN_STATUS.value == "42")
  3776. {
  3777. if (CAFE_LAUNCH_DEBUG.value == "1")
  3778. {
  3779. multi.start(MULTI_CONNECT.value, CAFE_MULTI_INIT.value, MULTIELF.value);
  3780. }
  3781. else
  3782. {
  3783. Console.WriteLine("Ready to connect from multi");
  3784. Console.WriteLine(GHS_ROOT.value + "\\multi.exe -cmd prepare_target " + MULTI_CONNECT.value + " " + CAFE_MULTI_INIT.value + " " + MULTIELF.value);
  3785. }
  3786.  
  3787. if (CMDOUTPUT.value == "1")
  3788. {
  3789. #if DEBUG
  3790. Log.WriteLine("CMDOUTPUT = 1 -> calling cattoucan.start...");
  3791. #endif
  3792.  
  3793. cattoucan.start(quiet_mode, QUIET.value, PORT.value);
  3794. }
  3795. }
  3796. }
  3797. else
  3798. {
  3799. Console.WriteLine("cafex run error: Only Multi debugger supported");
  3800. return CAFEX_ERROR.RUN_NON_MULTI_DEBUGGER;
  3801. }
  3802.  
  3803. #if DEBUG
  3804. Log.WriteLine("connect_serial_or_debugger completed.");
  3805. #endif
  3806.  
  3807. return CAFEX_ERROR.OK;
  3808. }
  3809.  
  3810. static int initialize_serial_port_and_directory(string stop_cmd)
  3811. {
  3812. #if DEBUG
  3813. Log.WriteLine("initialize_serial_port_and_directory started.");
  3814. #endif
  3815.  
  3816. if (stop_cmd == null)
  3817. {
  3818. Console.WriteLine("\"Usage: Initialize_serial_port_and_directory stop_cmd\", where stop_cmd is cafestop or a bash function call.");
  3819. return 1;
  3820. }
  3821.  
  3822. create_directory_if_noexist(LOGDIR.value);
  3823.  
  3824. if (stop_cmd == "cafestop")
  3825. {
  3826. stop(null);
  3827. }
  3828.  
  3829. //trap
  3830.  
  3831. if (CAFE_CONSOLE.value == "cattoucan")
  3832. {
  3833. //trap
  3834. CAFE_CONSOLE.value = "toucan";
  3835. }
  3836.  
  3837. if (CAFE_CONSOLE.value == "cmdtoucan")
  3838. {
  3839. //trap
  3840. CAFE_CONSOLE.value = "toucan";
  3841. }
  3842.  
  3843. if (CAFE_CONSOLE.value == "catwaikiki")
  3844. {
  3845. //trap
  3846. CAFE_CONSOLE.value = "waikiki";
  3847. }
  3848.  
  3849. #if DEBUG
  3850. Log.WriteLine("CAFE_CONSOLE=" + CAFE_CONSOLE.value);
  3851. #endif
  3852.  
  3853. if (COMPORTS_SUPPORT.value == "-c")
  3854. {
  3855. int init_flag = 0;
  3856. INITIALIZED_COMPORTS.value = "";
  3857.  
  3858. for (int COMPORT = 1; COMPORT <= 20; ++COMPORT)
  3859. {
  3860. if (cmd.mode(COMPORT) == 0)
  3861. {
  3862. Console.WriteLine("Initialized COM" + COMPORT);
  3863. init_flag = 1;
  3864. INITIALIZED_COMPORTS.AddToVar("COM" + COMPORT + " ", ' ');
  3865. }
  3866. }
  3867.  
  3868. if (init_flag == 0 && CAFE_CONSOLE.value != "toucan")
  3869. {
  3870. Console.WriteLine("ERROR: no serial port is available.");
  3871. Console.WriteLine("Please close other softwares using serial port,");
  3872. Console.WriteLine("such as HyperTerminal.");
  3873. Console.WriteLine();
  3874. Console.WriteLine("This automated test script cannot share the serial port");
  3875. Console.WriteLine("with other softwares.");
  3876. return 1;
  3877. }
  3878. }
  3879.  
  3880. return 0;
  3881. }
  3882.  
  3883. static int monitor_for_output(string str_opt, string test_str, string timeout, string log_dir, string file_name, bool printToScreen)
  3884. {
  3885. #if DEBUG
  3886. Log.WriteLine("monitor_for_output started.");
  3887. #endif
  3888.  
  3889. create_directory_if_noexist(log_dir);
  3890.  
  3891. if (MONITOR_FOR_OUTPUT_CALLS.value == null)
  3892. {
  3893. MONITOR_FOR_OUTPUT_CALLS.value = "0";
  3894. }
  3895.  
  3896. MONITOR_FOR_OUTPUT_CALLS.value = (HexHandler.GetNumberFromString(MONITOR_FOR_OUTPUT_CALLS.value) + 1).ToString();
  3897. string call_no = MONITOR_FOR_OUTPUT_CALLS.value;
  3898. string mon_rc_file = log_dir + "\\monitor_return_code" + call_no + ".txt";
  3899.  
  3900. if (file_name == null)
  3901. {
  3902. file_name = "monitor_" + call_no + ".txt";
  3903. }
  3904.  
  3905. string log_file = log_dir + "\\" + file_name;
  3906.  
  3907. // Write to the log file all the output and print to the screen, if printToScreen is true.
  3908. FileStream fs = new FileStream(log_file, FileMode.Create);
  3909. StreamWriter sw = new StreamWriter(fs);
  3910. DataReceivedEventHandler callback = (sender, e) =>
  3911. {
  3912. if (e != null && e.Data != null && sw != null)
  3913. {
  3914. string outputData = e.Data;
  3915. if (printToScreen)
  3916. {
  3917. Console.WriteLine(outputData);
  3918. }
  3919. try
  3920. {
  3921. sw.WriteLine(outputData);
  3922. sw.Flush();
  3923. }
  3924. catch (IOException)
  3925. { }
  3926. catch (ObjectDisposedException)
  3927. { }
  3928. }
  3929. };
  3930.  
  3931. int retVal = monitor_for_output_redirectionCallback(str_opt, test_str, timeout, callback);
  3932.  
  3933. sw.Close();
  3934.  
  3935. fs = new FileStream(mon_rc_file, FileMode.Create);
  3936. sw = new StreamWriter(fs);
  3937. sw.WriteLine(retVal);
  3938. sw.Flush();
  3939. sw.Close();
  3940.  
  3941. return retVal;
  3942. }
  3943.  
  3944. static int monitor_for_output_redirectionCallback(string str_opt, string test_str, string timeout, DataReceivedEventHandler callback)
  3945. {
  3946. #if DEBUG
  3947. Log.WriteLine("monitor_for_output_redirectionCallback started.");
  3948. #endif
  3949.  
  3950. string ret = monitor.start_redirectionCallback(str_opt, test_str, timeout, callback).ToString();
  3951.  
  3952. int return_val = evaluate_monitor_return_value(ret);
  3953. if (return_val == 0)
  3954. {
  3955. Console.WriteLine("monitor returned PASS");
  3956. }
  3957. else
  3958. {
  3959. return return_val;
  3960. }
  3961.  
  3962. return 0;
  3963. }
  3964.  
  3965. static int evaluate_monitor_return_value(string mon_rval)
  3966. {
  3967. #if DEBUG
  3968. Log.WriteLine("evaluate_monitor_return_value started.");
  3969. #endif
  3970.  
  3971. int return_val = 1;
  3972. string status_str = string.Empty;
  3973.  
  3974. if (mon_rval == MON_RESULT_PASS.value)
  3975. {
  3976. return_val = 0;
  3977. }
  3978.  
  3979. else if (mon_rval == MON_RESULT_TIMEOUT.value)
  3980. {
  3981. status_str = "TIMEOUT";
  3982. }
  3983.  
  3984. else if (mon_rval == MON_RESULT_UNDEF.value)
  3985. {
  3986. status_str = "UNDEF";
  3987. }
  3988.  
  3989. else if (mon_rval == MON_RESULT_MANUAL.value)
  3990. {
  3991. status_str = "MANUAL";
  3992. }
  3993.  
  3994. else if (mon_rval == MON_RESULT_NOLOG.value)
  3995. {
  3996. status_str = "NOLOG";
  3997. }
  3998.  
  3999. else if (mon_rval == MON_RESULT_FAIL.value)
  4000. {
  4001. status_str = "FAIL";
  4002. }
  4003.  
  4004. else if (mon_rval == MON_RESULT_EXPECTED_TERM.value)
  4005. {
  4006. status_str = "EXPECTED_TERM";
  4007. }
  4008.  
  4009. else if (mon_rval == MON_RESULT_EXEC_WIN_APP.value)
  4010. {
  4011. status_str = "EXEC_WIN_APP";
  4012. }
  4013.  
  4014. else if (mon_rval == MON_RESULT_NOPPC.value)
  4015. {
  4016. status_str = "NOPPC";
  4017. }
  4018.  
  4019. else if (mon_rval == MON_RESULT_BOOT1.value)
  4020. {
  4021. status_str = "BOOT1";
  4022. }
  4023.  
  4024. else if (mon_rval == MON_RESULT_RESTART.value)
  4025. {
  4026. status_str = "RESTART";
  4027. }
  4028.  
  4029. else if (mon_rval == MON_RESULT_TEST_AS_RPL.value)
  4030. {
  4031. status_str = "TEST_AS_RPL";
  4032. }
  4033.  
  4034. else if (mon_rval == MON_RESULT_END_OF_RPL_LOADER.value)
  4035. {
  4036. status_str = "END_OF_RPL_LOADER";
  4037. }
  4038.  
  4039. else if (mon_rval == MON_RESULT_BRIDGE_COLLISION.value)
  4040. {
  4041. status_str = "BRIDGE_COLLISION";
  4042. }
  4043.  
  4044. else if (mon_rval == MON_RESULT_TEST_DBG_OUT.value)
  4045. {
  4046. status_str = "TEST_DBG_OUT";
  4047. }
  4048.  
  4049. else if (mon_rval == MON_RESULT_BRIDGE_OFF.value)
  4050. {
  4051. status_str = "BRIDGE_OFF";
  4052. }
  4053.  
  4054. else if (mon_rval == MON_RESULT_ERROR.value)
  4055. {
  4056. status_str = "ERROR";
  4057. }
  4058.  
  4059. else if (mon_rval == MON_RESULT_FATAL_ERROR.value)
  4060. {
  4061. status_str = "FATAL_ERROR";
  4062. }
  4063.  
  4064. else if (mon_rval == MON_RESULT_MISSING.value)
  4065. {
  4066. status_str = "MISSING";
  4067. }
  4068.  
  4069. else if (mon_rval == MON_RESULT_ABORTED.value)
  4070. {
  4071. status_str = "ABORTED";
  4072. }
  4073.  
  4074. else
  4075. {
  4076. status_str = mon_rval.ToString();
  4077. }
  4078.  
  4079. if (return_val != 0)
  4080. {
  4081. Console.WriteLine("monitor returned " + status_str);
  4082. return return_val;
  4083. }
  4084.  
  4085. return 0;
  4086. }
  4087.  
  4088. static bool check_xml_version(string file_dest, string file_default, string file_option)
  4089. {
  4090. #if DEBUG
  4091. Log.WriteLine("check_xml_version started.");
  4092. #endif
  4093.  
  4094. if (!File.Exists(file_dest))
  4095. {
  4096. Console.WriteLine("cafex run: Copying default config file to " + file_dest);
  4097. if (!File.Exists(file_default))
  4098. {
  4099. Console.WriteLine("cafex run failed: Cannot find default config file " + file_default);
  4100. return false;
  4101. }
  4102.  
  4103. create_directory_if_noexist(Path.GetDirectoryName(file_dest));
  4104.  
  4105. File.Copy(file_default, file_dest, true);
  4106. File.SetAttributes(file_dest, FileAttributes.Normal);
  4107. }
  4108. else
  4109. {
  4110. string ver_default = XmlHandler.GetNodeValue(file_default, "version");
  4111. string ver_dest = XmlHandler.GetNodeValue(file_dest, "version");
  4112. #if DEBUG
  4113. Log.WriteLine("file_dest: " + file_dest + ", ver_default: " + ver_default + " - ver_dest: " + ver_dest);
  4114. #endif
  4115. if (ver_dest == null || ver_default == null)
  4116. {
  4117. Console.WriteLine("cafex run failed: Cannot get xml version numbers");
  4118. return false;
  4119. }
  4120.  
  4121. if (ver_dest != ver_default)
  4122. {
  4123. if (file_option == "copy_default_if_mismatch")
  4124. {
  4125. Console.WriteLine("cafex run: Config file version mismatch detected: " + ver_dest + " vs. required " + ver_default);
  4126. Console.WriteLine(" Copying default config file to " + file_dest);
  4127. if (!File.Exists(file_default))
  4128. {
  4129. Console.WriteLine("cafex run failed: Cannot find default config file " + file_default);
  4130. return false;
  4131. }
  4132.  
  4133. create_directory_if_noexist(Path.GetDirectoryName(file_dest));
  4134.  
  4135. File.Copy(file_default, file_dest, true);
  4136. File.SetAttributes(file_dest, FileAttributes.Normal);
  4137. }
  4138. else
  4139. {
  4140. Console.WriteLine("cafex run failed: Please update or delete config file: " + file_dest);
  4141. Console.WriteLine(" Version number mismatch: " + ver_dest + " vs. required " + ver_default);
  4142. return false;
  4143. }
  4144. }
  4145. }
  4146.  
  4147. return true;
  4148. }
  4149.  
  4150. static string args_to_bar_sep_list(string[] input)
  4151. {
  4152. return StringCombiner.MakeDelimitedString(input, '|');
  4153. }
  4154.  
  4155. static bool exit_if_specials(string[] arglist)
  4156. {
  4157. #if DEBUG
  4158. Log.WriteLine("exit_if_specials started.");
  4159. #endif
  4160.  
  4161. foreach (string s in arglist)
  4162. {
  4163. if (s.Contains("?") || s.Contains("+") || s.Contains("<") || s.Contains(">") || s.Contains("|"))
  4164. {
  4165. Console.WriteLine("cafex run failed: Unable to parse special character (?+<>|) in arg list.");
  4166. Console.WriteLine(" arglist: " + s);
  4167. return false;
  4168. }
  4169. }
  4170. return true;
  4171. }
  4172.  
  4173. static bool update_arglist(string file_dest, string cafe_elf, string[] arglist)
  4174. {
  4175. #if DEBUG
  4176. Log.WriteLine("update_arglist started.");
  4177. #endif
  4178.  
  4179. string[] arglist_without_rpx = new string[arglist.Length - 1];
  4180. Array.Copy(arglist, 1, arglist_without_rpx, 0, arglist_without_rpx.Length);
  4181.  
  4182. bool exit_if_specials_ret = exit_if_specials(arglist_without_rpx);
  4183. if (!exit_if_specials_ret)
  4184. {
  4185. return false;
  4186. }
  4187.  
  4188. string args_trailing_with_bars = args_to_bar_sep_list(arglist_without_rpx);
  4189.  
  4190. if (DASH_A_ARGS.value == null)
  4191. {
  4192. ARGS_WITH_BARS.value = args_trailing_with_bars;
  4193. }
  4194. else
  4195. {
  4196. ARGS_WITH_BARS.value = DASH_A_ARGS.value.Replace(',', '|');
  4197. if (args_trailing_with_bars != string.Empty)
  4198. {
  4199. ARGS_WITH_BARS.value = ARGS_WITH_BARS.value + "|" + args_trailing_with_bars;
  4200. }
  4201. }
  4202.  
  4203. string args_with_bars_no_quotes = string.Empty;
  4204. //if (ARGS_WITH_BARS.value != null)
  4205. //{
  4206. // args_with_bars_no_quotes = ARGS_WITH_BARS.value.Replace("\"", "\\\"");
  4207. //}
  4208. //else
  4209. //{
  4210. // args_with_bars_no_quotes = ARGS_WITH_BARS.value;
  4211. //}
  4212.  
  4213. args_with_bars_no_quotes = ARGS_WITH_BARS.value;
  4214.  
  4215.  
  4216. string total_arglist = Path.GetFileName(cafe_elf);
  4217. if (args_with_bars_no_quotes != string.Empty)
  4218. {
  4219. total_arglist += "|" + args_with_bars_no_quotes;
  4220. }
  4221.  
  4222. string total_arglist_with_qs = total_arglist.Replace(' ', '?');
  4223. total_arglist_with_qs = total_arglist_with_qs.Replace('|', ' ');
  4224.  
  4225. XmlHandler.UpdateNodeValue(file_dest, "argstr", total_arglist_with_qs);
  4226.  
  4227. return true;
  4228. }
  4229.  
  4230. static CAFEX_ERROR update_system_xml(string file_dest, string cmd_flags, string system_mode, string sys_xml_mcp_launch_hint)
  4231. {
  4232. #if DEBUG
  4233. Log.WriteLine("update_system_xml started.");
  4234. #endif
  4235.  
  4236. string sys_default_xml = CAFE_ROOT.value + "\\system\\bin\\ghs\\cafe\\boot\\system_default.xml";
  4237.  
  4238. if (!check_xml_version(file_dest, sys_default_xml, "copy_default_if_mismatch"))
  4239. {
  4240. return CAFEX_ERROR.UNEXPECTED;
  4241. }
  4242.  
  4243. string old_system_mode_num = XmlHandler.GetNodeValue(file_dest, "dev_mode");
  4244. string[] system_mode_name_table = new string[4];
  4245. system_mode_name_table[0] = "PROD";
  4246. system_mode_name_table[1] = "DEV";
  4247. system_mode_name_table[2] = "TEST";
  4248. system_mode_name_table[3] = "Unknown";
  4249.  
  4250. if (old_system_mode_num != "0" && old_system_mode_num != "1" && old_system_mode_num != "2")
  4251. {
  4252. old_system_mode_num = "3";
  4253. }
  4254.  
  4255. string new_system_mode_num = "3";
  4256. if (system_mode == "dev")
  4257. {
  4258. new_system_mode_num = "1";
  4259. }
  4260. else if (system_mode == "test")
  4261. {
  4262. new_system_mode_num = "2";
  4263. }
  4264. else
  4265. {
  4266. if (system_mode == "prod")
  4267. {
  4268. Console.WriteLine("cafex run Warning: Changing to PROD mode is not supported.");
  4269. Console.WriteLine(" Please specify '-M dev' or '-M test'.");
  4270. }
  4271. else if (system_mode != null)
  4272. {
  4273. Console.WriteLine("cafex run Warning: Invalid system mode is specified.");
  4274. }
  4275.  
  4276. if (old_system_mode_num != "3")
  4277. {
  4278. new_system_mode_num = old_system_mode_num;
  4279. }
  4280. else
  4281. {
  4282. new_system_mode_num = "1";
  4283. Console.WriteLine("cafex run: System mode is broken. Fixing it to DEV");
  4284. }
  4285. }
  4286.  
  4287. if (CAFE_BOOT_MODE.value == "PCFS")
  4288. {
  4289. if (new_system_mode_num != old_system_mode_num)
  4290. {
  4291. Console.WriteLine("cafex run: Changing system mode " + system_mode_name_table[HexHandler.GetNumberFromString(old_system_mode_num)] + " to " + system_mode_name_table[HexHandler.GetNumberFromString(new_system_mode_num)]);
  4292. XmlHandler.UpdateNodeValue(file_dest, "dev_mode", new_system_mode_num);
  4293. }
  4294. else
  4295. {
  4296. Console.WriteLine("cafex run: Current system mode is " + system_mode_name_table[HexHandler.GetNumberFromString(new_system_mode_num)]);
  4297. }
  4298. }
  4299.  
  4300. if (CAFERUN_OPTION_MLC_EMU_LAUNCH.value == "1")
  4301. {
  4302. string mlc_emu_tid_hi = CAFERUN_OPTION_MLC_EMU_TITLEID.value.Substring(2, 8);
  4303. string mlc_emu_tid_lo = CAFERUN_OPTION_MLC_EMU_TITLEID.value.Substring(10, 8);
  4304.  
  4305. string mlc_emu_title_dir = string.Empty;
  4306. if (mlc_emu_tid_hi == "00050010")
  4307. {
  4308. mlc_emu_title_dir = CAFE_MLC_DIR.value + "\\sys\\title\\" + mlc_emu_tid_hi + "\\" + mlc_emu_tid_lo;
  4309. }
  4310. else
  4311. {
  4312. mlc_emu_title_dir = CAFE_MLC_DIR.value + "\\usr\\title\\" + mlc_emu_tid_hi + "\\" + mlc_emu_tid_lo;
  4313. }
  4314.  
  4315. if (File.Exists(mlc_emu_title_dir + "\\code\\app.xml"))
  4316. {
  4317. string mlc_emu_os_version = XmlHandler.GetNodeValue(mlc_emu_title_dir + "\\code\\app.xml", "os_version");
  4318. CAFERUN_COLDBOOT_OS_VERSION.value = String.Format("{0:x16}", mlc_emu_os_version);
  4319. }
  4320. else
  4321. {
  4322. if (DISC_EMU_TYPE.value != "hdd" && ALLOW_DISCRUN_IN_SOFTLAUNCH.value == "0")
  4323. {
  4324. Console.WriteLine("cafex run failed: Cannot find title 0x" + mlc_emu_tid_hi + mlc_emu_tid_lo + " on MLC emulation");
  4325. Console.WriteLine(" Please cafeinstall before using -t option");
  4326. return CAFEX_ERROR.RUN_TITLE_ID_NOT_FOUND_IN_MLC;
  4327. }
  4328. }
  4329.  
  4330. XmlHandler.UpdateNodeValue(file_dest, "default_title_id", mlc_emu_tid_hi + mlc_emu_tid_lo);
  4331. }
  4332. else
  4333. {
  4334. if (APP_XML_FILE.value != null)
  4335. {
  4336. DISC_EMU_TID.value = XmlHandler.GetNodeValue(APP_XML_FILE.value, "title_id");
  4337. XmlHandler.UpdateNodeValue(file_dest, "default_title_id", DISC_EMU_TID.value);
  4338. }
  4339. else
  4340. {
  4341. if (DISC_EMU_TYPE.value == "hdd" && string.IsNullOrEmpty(CAFE_WUMAD.value))
  4342. {
  4343. Console.WriteLine();
  4344. Console.WriteLine("-------");
  4345. Console.WriteLine("cafex run WARNING: Title ID is not specified.");
  4346. Console.WriteLine(" Use -t option to specify Title ID of application on HDD.");
  4347. Console.WriteLine(" Title ID of previously launched app will be used at this time,");
  4348. Console.WriteLine(" but launch may fail if it was different from that of application on HDD.");
  4349. Console.WriteLine("-------");
  4350. Console.WriteLine();
  4351. }
  4352. }
  4353. }
  4354.  
  4355. if (APP_XML_FILE.value != null)
  4356. {
  4357. string disc_emu_app_type = XmlHandler.GetNodeValue(APP_XML_FILE.value, "app_type");
  4358.  
  4359. XmlHandler.UpdateNodeValue(file_dest, "default_app_type", disc_emu_app_type);
  4360. }
  4361.  
  4362. XmlHandler.UpdateNodeValue(file_dest, "cmdFlags", cmd_flags);
  4363.  
  4364. XmlHandler.UpdateNodeValue(file_dest, "default_os_id", CAFERUN_COLDBOOT_OS_VERSION.value);
  4365.  
  4366. if (sys_xml_mcp_launch_hint != null)
  4367. {
  4368. bool node_exists = XmlHandler.NodeExists(file_dest, "default_device_type");
  4369. if (!node_exists)
  4370. {
  4371. XmlHandler.AddNode(file_dest, "default_device_type", sys_xml_mcp_launch_hint, new string[4] { "type", "string", "length", "16" });
  4372. }
  4373. else
  4374. {
  4375. XmlHandler.UpdateNodeValue(file_dest, "default_device_type", sys_xml_mcp_launch_hint);
  4376. }
  4377. }
  4378. else
  4379. {
  4380. XmlHandler.RemoveNode(file_dest, "default_device_type");
  4381. }
  4382.  
  4383. return CAFEX_ERROR.OK;
  4384. }
  4385.  
  4386. static int set_system_xml_cos_flags(string ppc_os_flags)
  4387. {
  4388. #if DEBUG
  4389. Log.WriteLine("setting cos_flags:" + ppc_os_flags);
  4390. #endif
  4391.  
  4392. int ret = devkitmsg.CosFlags(ppc_os_flags);
  4393. if (ret != 0)
  4394. {
  4395. Console.WriteLine("devkitmsg \"cos_flags " + ppc_os_flags + "\" - failed result = " + ret);
  4396. }
  4397. return ret;
  4398. }
  4399.  
  4400. static bool update_app_xml(string file_dest)
  4401. {
  4402. #if DEBUG
  4403. Log.WriteLine("update_app_xml started.");
  4404. #endif
  4405.  
  4406. string app_default_xml = CAFE_ROOT.value + "\\system\\bin\\ghs\\cafe\\boot\\app_default.xml";
  4407.  
  4408. if (!check_xml_version(file_dest, app_default_xml, "copy_default_if_mismatch"))
  4409. {
  4410. return false;
  4411. }
  4412.  
  4413. XmlHandler.UpdateNodeValue(file_dest, "os_version", CAFERUN_COLDBOOT_OS_VERSION.value);
  4414.  
  4415. return true;
  4416. }
  4417.  
  4418. static bool update_cos_xml(string file_dest, string cmd_flags)
  4419. {
  4420. #if DEBUG
  4421. Log.WriteLine("update_cos_xml started.");
  4422. #endif
  4423.  
  4424. string cos_default_xml = CAFE_ROOT.value + "\\system\\bin\\ghs\\cafe\\boot\\cos_default.xml";
  4425.  
  4426. if (!check_xml_version(file_dest, cos_default_xml, "copy_default_if_mismatch"))
  4427. {
  4428. return false;
  4429. }
  4430.  
  4431. if (XmlHandler.GetNodeValue(file_dest, "cmdFlags") == "0")
  4432. {
  4433. XmlHandler.UpdateNodeValue(file_dest, "cmdFlags", cmd_flags);
  4434. }
  4435.  
  4436. return true;
  4437. }
  4438.  
  4439. static CAFEX_ERROR update_meta_xml(string file_dest)
  4440. {
  4441. #if DEBUG
  4442. Log.WriteLine("update_meta_xml started on file = " + file_dest);
  4443. #endif
  4444.  
  4445. string meta_default_xml = CAFE_ROOT.value + "\\system\\bin\\ghs\\cafe\\boot\\meta_default.xml";
  4446. string own_meta_xml = CAFE_CODE_DIR.value + "\\..\\meta\\meta.xml";
  4447.  
  4448. if (CAFERUN_OPTION_MLC_EMU_LAUNCH.value == "0")
  4449. {
  4450. if (File.Exists(own_meta_xml))
  4451. {
  4452. string abs_own_meta_xml = Path.GetFullPath(own_meta_xml);
  4453. string abs_file_dest = Path.GetFullPath(file_dest);
  4454. if (abs_own_meta_xml != abs_file_dest)
  4455. {
  4456. Console.Write("Copy " + own_meta_xml + " to " + file_dest);
  4457. File.Copy(own_meta_xml, file_dest, true);
  4458. File.SetAttributes(file_dest, FileAttributes.Normal);
  4459. }
  4460. }
  4461. else
  4462. {
  4463. CAFEX_ERROR ret = check_title_id(file_dest, meta_default_xml, APP_XML_FILE.value);
  4464. if (ret != CAFEX_ERROR.OK)
  4465. {
  4466. return ret;
  4467. }
  4468. }
  4469.  
  4470. if (!check_xml_version(file_dest, meta_default_xml, "copy_default_if_mismatch"))
  4471. {
  4472. return CAFEX_ERROR.RUN_UPDATE_META_XML_FAILED;
  4473. }
  4474.  
  4475. XmlHandler.UpdateNodeValue(file_dest, "os_version", CAFERUN_COLDBOOT_OS_VERSION.value);
  4476.  
  4477. Console.WriteLine("Updating title id, group id, title version and app type in " + file_dest + ", based on " + APP_XML_FILE.value + "...");
  4478.  
  4479. // get titleId from disc emulation's app.xml
  4480. DISC_EMU_TID.value = XmlHandler.GetNodeValue(APP_XML_FILE.value, "title_id");
  4481. XmlHandler.UpdateNodeValue(file_dest, "title_id", DISC_EMU_TID.value);
  4482.  
  4483. // get group Id from disc emulation's app.xml
  4484. string disc_emu_gid = XmlHandler.GetNodeValue(APP_XML_FILE.value, "group_id");
  4485. XmlHandler.UpdateNodeValue(file_dest, "group_id", disc_emu_gid);
  4486.  
  4487. // get title version from disc emulation's app.xml
  4488. string disc_emu_titlever_hex = XmlHandler.GetNodeValue(APP_XML_FILE.value, "title_version").Trim();
  4489. if (!disc_emu_titlever_hex.StartsWith("0x"))
  4490. {
  4491. disc_emu_titlever_hex = "0x" + disc_emu_titlever_hex;
  4492. }
  4493.  
  4494. int disc_emu_titlever_decimal = HexHandler.GetNumberFromString(disc_emu_titlever_hex);
  4495. // insert system default_title_id
  4496. XmlHandler.UpdateNodeValue(file_dest, "title_version", disc_emu_titlever_decimal.ToString("X5"));
  4497. }
  4498.  
  4499. return CAFEX_ERROR.OK;
  4500. }
  4501.  
  4502. static void update_boot_logos(string file_dest)
  4503. {
  4504. #if DEBUG
  4505. Log.WriteLine("update_boot_logos started.");
  4506. #endif
  4507.  
  4508. string boot_movie_h264 = CAFE_ROOT.value + "\\system\\bin\\ghs\\cafe\\meta\\bootMovie.h264";
  4509. string boot_logo_tex_tga = CAFE_ROOT.value + "\\system\\bin\\ghs\\cafe\\meta\\bootLogoTex.tga";
  4510.  
  4511. string icon_tex_tga = CAFE_ROOT.value + "\\system\\bin\\ghs\\cafe\\meta\\iconTex.tga";
  4512. string boot_tv_tga = CAFE_ROOT.value + "\\system\\bin\\ghs\\cafe\\meta\\bootTvTex.tga";
  4513. string boot_drc_tga = CAFE_ROOT.value + "\\system\\bin\\ghs\\cafe\\meta\\bootDrcTex.tga";
  4514.  
  4515. string dest_boot_movie_h264 = file_dest + "\\bootMovie.h264";
  4516. string dest_boot_logo_tex_tga = file_dest + "\\bootLogoTex.tga";
  4517.  
  4518. string dest_icon_tex_tga = file_dest + "\\iconTex.tga";
  4519. string dest_boot_tv_tga = file_dest + "\\bootTvTex.tga";
  4520. string dest_boot_drc_tga = file_dest + "\\bootDrcTex.tga";
  4521.  
  4522. // Copy boot_movie logo_text only if ghs\cafe\meta has a newer timestamp.
  4523. // This replaces the "cp -u" from the BASH script.
  4524. bool copyRequired = false;
  4525.  
  4526. if (!File.Exists(dest_boot_movie_h264))
  4527. {
  4528. // Should copy because it does not exist
  4529. copyRequired = true;
  4530. }
  4531. else
  4532. {
  4533. // Check the timestamps
  4534. if (file_timestamp_compare(boot_movie_h264, dest_boot_movie_h264) > 0)
  4535. {
  4536. copyRequired = true;
  4537. }
  4538. }
  4539.  
  4540. if (copyRequired)
  4541. {
  4542. file_copy_retry(boot_movie_h264, dest_boot_movie_h264, true, 1);
  4543. File.SetAttributes(dest_boot_movie_h264, FileAttributes.Normal);
  4544. }
  4545.  
  4546. // Copy logo_text only if ghs\cafe\meta has a newer timestamp
  4547. copyRequired = false;
  4548.  
  4549. if (!File.Exists(dest_boot_logo_tex_tga))
  4550. {
  4551. // Should copy because it does not exist
  4552. copyRequired = true;
  4553. }
  4554. else
  4555. {
  4556. // Check the timestamps
  4557. if (file_timestamp_compare(boot_logo_tex_tga, dest_boot_logo_tex_tga) > 0)
  4558. {
  4559. copyRequired = true;
  4560. }
  4561. }
  4562.  
  4563. if (copyRequired)
  4564. {
  4565. file_copy_retry(boot_logo_tex_tga, dest_boot_logo_tex_tga, true, 1);
  4566. File.SetAttributes(dest_boot_logo_tex_tga, FileAttributes.Normal);
  4567. }
  4568.  
  4569. // Applicaion Boot Textures should be copy if files do not exist
  4570. if (!File.Exists(dest_icon_tex_tga))
  4571. {
  4572. file_copy_retry(icon_tex_tga, dest_icon_tex_tga, true, 1);
  4573. File.SetAttributes(dest_icon_tex_tga, FileAttributes.Normal);
  4574. }
  4575.  
  4576. if (!File.Exists(dest_boot_tv_tga))
  4577. {
  4578. file_copy_retry(boot_tv_tga, dest_boot_tv_tga, true, 1);
  4579. File.SetAttributes(dest_boot_tv_tga, FileAttributes.Normal);
  4580. }
  4581.  
  4582. if (!File.Exists(dest_boot_drc_tga))
  4583. {
  4584. file_copy_retry(boot_drc_tga, dest_boot_drc_tga, true, 1);
  4585. File.SetAttributes(dest_boot_drc_tga, FileAttributes.Normal);
  4586. }
  4587. }
  4588.  
  4589. static void create_directory_if_noexist(string directory)
  4590. {
  4591. #if DEBUG
  4592. Log.WriteLine("create_directory_if_noexist started.");
  4593. #endif
  4594.  
  4595. if (!Directory.Exists(directory))
  4596. {
  4597. Directory.CreateDirectory(directory);
  4598. }
  4599. }
  4600.  
  4601. static int compute_flat_version(string version)
  4602. {
  4603. #if DEBUG
  4604. Log.WriteLine("compute_flat_version started.");
  4605. #endif
  4606.  
  4607. string[] numbers = version.Split('.');
  4608. int ret = 0;
  4609. ret = Convert.ToInt32(numbers[0]) * 1000000 + Convert.ToInt32(numbers[1]) * 10000 + Convert.ToInt32(numbers[2]) * 1000 + Convert.ToInt32(numbers[3]);
  4610. return ret;
  4611. }
  4612.  
  4613. static CAFEX_ERROR check_title_id(string file_dest, string file_default, string file_app)
  4614. {
  4615. #if DEBUG
  4616. Log.WriteLine("check_title_id started. file_dest=" + file_dest + ",file_default=" + file_default + ",file_app=" + file_app);
  4617. #endif
  4618. if (!File.Exists(file_dest))
  4619. {
  4620. Console.WriteLine("cafex run: Copying default config file to " + file_dest);
  4621. if (!File.Exists(file_default))
  4622. {
  4623. Console.WriteLine("cafex run failed: Cannot find default config file " + file_default);
  4624. return CAFEX_ERROR.RUN_CHECKTITLEID_FAILED;
  4625. }
  4626.  
  4627. File.Copy(file_default, file_dest, true);
  4628. File.SetAttributes(file_default, FileAttributes.Normal);
  4629. }
  4630. else
  4631. {
  4632. string titleid_dest = XmlHandler.GetNodeValue(file_dest, "title_id");
  4633. string titleid_app = XmlHandler.GetNodeValue(file_app, "title_id");
  4634. if (titleid_dest == string.Empty || titleid_app == string.Empty)
  4635. {
  4636. Console.WriteLine("cafex run failed: Cannot get title id");
  4637. return CAFEX_ERROR.RUN_CHECKTITLEID_FAILED;
  4638. }
  4639.  
  4640. if (titleid_dest != titleid_app)
  4641. {
  4642. Console.WriteLine("cafex run: Config file title id mismatch detected: " + titleid_dest + " vs. required " + titleid_app);
  4643. Console.WriteLine(" Copying default config file to " + file_dest);
  4644.  
  4645. if (!File.Exists(file_default))
  4646. {
  4647. Console.WriteLine("cafex run failed: Cannot find default config file " + file_default);
  4648. return CAFEX_ERROR.RUN_CHECKTITLEID_FAILED;
  4649. }
  4650.  
  4651. File.Copy(file_default, file_dest, true);
  4652. File.SetAttributes(file_default, FileAttributes.Normal);
  4653. }
  4654. }
  4655.  
  4656. return CAFEX_ERROR.OK;
  4657. }
  4658.  
  4659. /// <summary>
  4660. /// Compares timestamsp between two files.
  4661. /// </summary>
  4662. /// <param name="file1">The first file for comparison.</param>
  4663. /// <param name="file2">The second file for comparison.</param>
  4664. /// <returns>0 if timestamps are the same, negative if first file is older than the second, and positive if first is newer than the second.</returns>
  4665. static int file_timestamp_compare(string file1, string file2)
  4666. {
  4667. if (!File.Exists(file1))
  4668. {
  4669. throw new ArgumentException("file1 = " + file1);
  4670. }
  4671.  
  4672. if (!File.Exists(file2))
  4673. {
  4674. throw new ArgumentException("file2 = " + file2);
  4675. }
  4676.  
  4677. DateTime file1Time = File.GetLastWriteTime(file1);
  4678. DateTime file2Time = File.GetLastWriteTime(file2);
  4679.  
  4680. return DateTime.Compare(file1Time, file2Time);
  4681. }
  4682.  
  4683. /// <summary>
  4684. /// Tries to copy a file from source to destination, and if it fails and attempts is greater than one,
  4685. /// it will wait for 1s and retry.
  4686. /// </summary>
  4687. /// <param name="sourceFileName">Source file.</param>
  4688. /// <param name="destFileName">Destination file.</param>
  4689. /// <param name="overwrite">Overwrite if destination exists.</param>
  4690. /// <param name="attempts">Number of attempts</param>
  4691. static void file_copy_retry(string sourceFileName, string destFileName, bool overwrite, uint attempts)
  4692. {
  4693.  
  4694. try
  4695. {
  4696. File.Copy(sourceFileName, destFileName, overwrite);
  4697. }
  4698. catch (IOException)
  4699. {
  4700. if (attempts > 1)
  4701. {
  4702. --attempts;
  4703. // Wait a second
  4704. Thread.Sleep(Program.RETRY_TIMESPAN);
  4705. // Retry
  4706. file_copy_retry(sourceFileName, destFileName, overwrite, attempts);
  4707. }
  4708. else
  4709. {
  4710. // Re-throw if no attempts
  4711. throw;
  4712. }
  4713. }
  4714. }
  4715.  
  4716. #endregion
  4717.  
  4718. #region HELP
  4719. static void run_usage()
  4720. {
  4721. string caller;
  4722. if (CAFERUN_OPTION_CALLED_FROM_CAFEDISCRUN.value == "yes")
  4723. {
  4724. caller = "discrun";
  4725. }
  4726. else
  4727. {
  4728. caller = "run";
  4729. }
  4730.  
  4731. Console.WriteLine("Usage: cafex " + caller + " [options] <app>.rpx \"[arg1]\" \"[arg2]\" ... \"[argN]\"");
  4732. Console.WriteLine();
  4733. Console.WriteLine("options:");
  4734. Console.WriteLine(" -a a,b... : arguments (comma delimited)");
  4735. Console.WriteLine(" -b : use DEBUG OS version");
  4736. Console.WriteLine(" -u : use FDEBUG OS version");
  4737. Console.WriteLine(" -l <path> : user RPL directory or file to gather for RPX");
  4738. Console.WriteLine();
  4739. Console.WriteLine(" -d multi : launch multi (GHS UI) on elf file");
  4740. Console.WriteLine(" -i : stop debugger in the initialization code");
  4741. Console.WriteLine(" -j : stop debugger in the pre-initialization code");
  4742. Console.WriteLine(" -R : do not stop in debugger until OSDebug or OSDebugStr call");
  4743. Console.WriteLine(" -p <n> : indicate process to debug (defaults to game process)");
  4744. Console.WriteLine(" -g <n> : 32-bit flag to pass along to PPC COS");
  4745. Console.WriteLine(" -w <n> : 32-bit flag to pass along to PPC COS.");
  4746. Console.WriteLine(" -k <file> : path to elf file for debugger");
  4747. Console.WriteLine(" -x : Enable AHCI/SATA disk access. Deprecated - will soon become default.");
  4748. Console.WriteLine(" -r : restart last application");
  4749. Console.WriteLine(" -s : use soft launch to run");
  4750. Console.WriteLine(" -F : force restart (stop and then boot hw)");
  4751. Console.WriteLine(" -t <tid> : title launch from mlc emulation (tid is hex that starts with 0x)");
  4752. Console.WriteLine(" -e <op> : pass op through to FSEmul or MCP (e.g.: -e sata -> FSEmul -sata)");
  4753. Console.WriteLine();
  4754. Console.WriteLine(" -v <n> : set COS verbose level (0=off 7 == MAX)");
  4755. Console.WriteLine(" -q : do not display OS warning or informational messages.");
  4756. Console.WriteLine(" -z : suppress mirroring the data to the session data directory.");
  4757. Console.WriteLine();
  4758. Console.WriteLine(" -h : (help) display options");
  4759. }
  4760.  
  4761. static void update_usage()
  4762. {
  4763. Console.WriteLine("Usage: cafex update [-q] [-h]");
  4764. Console.WriteLine();
  4765. Console.WriteLine("options:");
  4766. Console.WriteLine(" -h : (help) display options");
  4767. Console.WriteLine(" -u <package> : path to update package (from System Config Tool's perspective)");
  4768. Console.WriteLine(" -s : use system updater (update package should contain system updater)");
  4769. Console.WriteLine(" -t <dir> : patch to single title (from System Config Tool's perspective)");
  4770. }
  4771.  
  4772. static void stop_usage()
  4773. {
  4774. Console.WriteLine("Usage: cafex stop [-hostonly] [-makemine] [-h]");
  4775. Console.WriteLine();
  4776. Console.WriteLine("options:");
  4777. Console.WriteLine(" -h : (help) display options.");
  4778. Console.WriteLine(" -hostonly : stops the host applications that control the bridge, keeping the bridge on.");
  4779. Console.WriteLine(" -makemine : forces assigning the bridge to this host.");
  4780. }
  4781.  
  4782. static void setbootmode_usage()
  4783. {
  4784. Console.WriteLine("Usage: cafex setbootmode [-noreflash] [PCFS|NAND]");
  4785. Console.WriteLine();
  4786. Console.WriteLine("options:");
  4787. Console.WriteLine(" -noreflash : when this parameter is used to set the boot");
  4788. Console.WriteLine(" as NAND, it will only reflash the Bridge if it's");
  4789. Console.WriteLine(" in PCFS. If already in NAND, it won't reflash.");
  4790. Console.WriteLine(" PCFS : sets the Bridge to boot using PCFS.");
  4791. Console.WriteLine(" NAND : sets the Bridge to boot from internal NAND.");
  4792. }
  4793. #endregion
  4794. }
  4795. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement