SysConf.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. // Copyright 2009 Dolphin Emulator Project
  2. // Licensed under GPLv2+
  3. // Refer to the license.txt file included.
  4. #include <cinttypes>
  5. #include <cstdio>
  6. #include <cstring>
  7. #include <string>
  8. #include <vector>
  9. #include "Common/CommonPaths.h"
  10. #include "Common/CommonTypes.h"
  11. #include "Common/FileUtil.h"
  12. #include "Common/SysConf.h"
  13. SysConf::SysConf()
  14. : m_IsValid(false)
  15. {
  16. UpdateLocation();
  17. }
  18. SysConf::~SysConf()
  19. {
  20. if (!m_IsValid)
  21. return;
  22. Save();
  23. Clear();
  24. }
  25. void SysConf::Clear()
  26. {
  27. for (auto i = m_Entries.begin(); i < m_Entries.end() - 1; ++i)
  28. delete [] i->data;
  29. m_Entries.clear();
  30. }
  31. bool SysConf::LoadFromFile(const std::string& filename)
  32. {
  33. if (m_IsValid)
  34. Clear();
  35. m_IsValid = false;
  36. // Basic check
  37. if (!File::Exists(filename))
  38. {
  39. File::CreateFullPath(filename);
  40. GenerateSysConf();
  41. return true;
  42. }
  43. u64 size = File::GetSize(filename);
  44. if (size != SYSCONF_SIZE)
  45. {
  46. if (AskYesNoT("Your SYSCONF file is the wrong size.\nIt should be 0x%04x (but is 0x%04" PRIx64 ")\nDo you want to generate a new one?",
  47. SYSCONF_SIZE, size))
  48. {
  49. GenerateSysConf();
  50. return true;
  51. }
  52. else
  53. {
  54. return false;
  55. }
  56. }
  57. File::IOFile f(filename, "rb");
  58. if (f.IsOpen())
  59. {
  60. if (LoadFromFileInternal(f.ReleaseHandle()))
  61. {
  62. m_Filename = filename;
  63. m_IsValid = true;
  64. return true;
  65. }
  66. }
  67. return false;
  68. }
  69. bool SysConf::LoadFromFileInternal(FILE *fh)
  70. {
  71. File::IOFile f(fh);
  72. // Fill in infos
  73. SSysConfHeader s_Header;
  74. f.ReadArray(s_Header.version, 4);
  75. f.ReadArray(&s_Header.numEntries, 1);
  76. s_Header.numEntries = Common::swap16(s_Header.numEntries) + 1;
  77. for (u16 index = 0; index < s_Header.numEntries; index++)
  78. {
  79. SSysConfEntry tmpEntry;
  80. f.ReadArray(&tmpEntry.offset, 1);
  81. tmpEntry.offset = Common::swap16(tmpEntry.offset);
  82. m_Entries.push_back(tmpEntry);
  83. }
  84. // Last offset is an invalid entry. We ignore it throughout this class
  85. for (auto i = m_Entries.begin(); i < m_Entries.end() - 1; ++i)
  86. {
  87. SSysConfEntry& curEntry = *i;
  88. f.Seek(curEntry.offset, SEEK_SET);
  89. u8 description = 0;
  90. f.ReadArray(&description, 1);
  91. // Data type
  92. curEntry.type = (SysconfType)((description & 0xe0) >> 5);
  93. // Length of name in bytes - 1
  94. curEntry.nameLength = (description & 0x1f) + 1;
  95. // Name
  96. f.ReadArray(curEntry.name, curEntry.nameLength);
  97. curEntry.name[curEntry.nameLength] = '\0';
  98. // Get length of data
  99. curEntry.data = nullptr;
  100. curEntry.dataLength = 0;
  101. switch (curEntry.type)
  102. {
  103. case Type_BigArray:
  104. f.ReadArray(&curEntry.dataLength, 1);
  105. curEntry.dataLength = Common::swap16(curEntry.dataLength);
  106. break;
  107. case Type_SmallArray:
  108. {
  109. u8 dlength = 0;
  110. f.ReadBytes(&dlength, 1);
  111. curEntry.dataLength = dlength;
  112. break;
  113. }
  114. case Type_Byte:
  115. case Type_Bool:
  116. curEntry.dataLength = 1;
  117. break;
  118. case Type_Short:
  119. curEntry.dataLength = 2;
  120. break;
  121. case Type_Long:
  122. curEntry.dataLength = 4;
  123. break;
  124. default:
  125. PanicAlertT("Unknown entry type %i in SYSCONF (%s@%x)!",
  126. curEntry.type, curEntry.name, curEntry.offset);
  127. return false;
  128. break;
  129. }
  130. // Fill in the actual data
  131. if (curEntry.dataLength)
  132. {
  133. curEntry.data = new u8[curEntry.dataLength];
  134. f.ReadArray(curEntry.data, curEntry.dataLength);
  135. }
  136. }
  137. return f.IsGood();
  138. }
  139. // Returns the size of the item in file
  140. static unsigned int create_item(SSysConfEntry &item, SysconfType type, const std::string &name,
  141. const int data_length, unsigned int offset)
  142. {
  143. item.offset = offset;
  144. item.type = type;
  145. item.nameLength = (u8)(name.length());
  146. strncpy(item.name, name.c_str(), 32);
  147. item.dataLength = data_length;
  148. item.data = new u8[data_length];
  149. memset(item.data, 0, data_length);
  150. switch (type)
  151. {
  152. case Type_BigArray:
  153. // size of description + name length + size of dataLength + data length + null
  154. return 1 + item.nameLength + 2 + item.dataLength + 1;
  155. case Type_SmallArray:
  156. // size of description + name length + size of dataLength + data length + null
  157. return 1 + item.nameLength + 1 + item.dataLength + 1;
  158. case Type_Byte:
  159. case Type_Bool:
  160. case Type_Short:
  161. case Type_Long:
  162. // size of description + name length + data length
  163. return 1 + item.nameLength + item.dataLength;
  164. default:
  165. return 0;
  166. }
  167. }
  168. void SysConf::GenerateSysConf()
  169. {
  170. SSysConfHeader s_Header;
  171. strncpy(s_Header.version, "SCv0", 4);
  172. s_Header.numEntries = Common::swap16(28 - 1);
  173. SSysConfEntry items[27];
  174. memset(items, 0, sizeof(SSysConfEntry) * 27);
  175. // version length + size of numEntries + 28 * size of offset
  176. unsigned int current_offset = 4 + 2 + 28 * 2;
  177. // BT.DINF
  178. current_offset += create_item(items[0], Type_BigArray, "BT.DINF", 0x460, current_offset);
  179. items[0].data[0] = 4;
  180. for (u8 i = 0; i < 4; ++i)
  181. {
  182. const u8 bt_addr[6] = {i, 0x00, 0x79, 0x19, 0x02, 0x11};
  183. memcpy(&items[0].data[1 + 70 * i], bt_addr, sizeof(bt_addr));
  184. memcpy(&items[0].data[7 + 70 * i], "Nintendo RVL-CNT-01", 19);
  185. }
  186. // BT.SENS
  187. current_offset += create_item(items[1], Type_Long, "BT.SENS", 4, current_offset);
  188. items[1].data[3] = 0x03;
  189. // IPL.NIK
  190. current_offset += create_item(items[2], Type_SmallArray, "IPL.NIK", 0x15, current_offset);
  191. const u8 console_nick[14] = {0, 'd', 0, 'o', 0, 'l', 0, 'p', 0, 'h', 0, 'i', 0, 'n'};
  192. memcpy(items[2].data, console_nick, 14);
  193. // IPL.AR
  194. current_offset += create_item(items[3], Type_Byte, "IPL.AR", 1, current_offset);
  195. items[3].data[0] = 0x01;
  196. // BT.BAR
  197. current_offset += create_item(items[4], Type_Byte, "BT.BAR", 1, current_offset);
  198. items[4].data[0] = 0x01;
  199. // IPL.SSV
  200. current_offset += create_item(items[5], Type_Byte, "IPL.SSV", 1, current_offset);
  201. // IPL.LNG
  202. current_offset += create_item(items[6], Type_Byte, "IPL.LNG", 1, current_offset);
  203. items[6].data[0] = 0x01;
  204. // IPL.SADR
  205. current_offset += create_item(items[7], Type_BigArray, "IPL.SADR", 0x1007, current_offset);
  206. items[7].data[0] = 0x6c; //(Switzerland) TODO should this default be changed?
  207. // IPL.CB
  208. current_offset += create_item(items[8], Type_Long, "IPL.CB", 4, current_offset);
  209. items[8].data[0] = 0x0f; items[8].data[1] = 0x11;
  210. items[8].data[2] = 0x14; items[8].data[3] = 0xa6;
  211. // BT.SPKV
  212. current_offset += create_item(items[9], Type_Byte, "BT.SPKV", 1, current_offset);
  213. items[9].data[0] = 0x58;
  214. // IPL.PC
  215. current_offset += create_item(items[10], Type_SmallArray, "IPL.PC", 0x49, current_offset);
  216. items[10].data[1] = 0x04; items[10].data[2] = 0x14;
  217. // NET.CTPC
  218. current_offset += create_item(items[11], Type_Long, "NET.CTPC", 4, current_offset);
  219. // WWW.RST
  220. current_offset += create_item(items[12], Type_Bool, "WWW.RST", 1, current_offset);
  221. // BT.CDIF
  222. current_offset += create_item(items[13], Type_BigArray, "BT.CDIF", 0x204, current_offset);
  223. // IPL.INC
  224. current_offset += create_item(items[14], Type_Long, "IPL.INC", 4, current_offset);
  225. items[14].data[3] = 0x08;
  226. // IPL.FRC
  227. current_offset += create_item(items[15], Type_Long, "IPL.FRC", 4, current_offset);
  228. items[15].data[3] = 0x28;
  229. // IPL.CD
  230. current_offset += create_item(items[16], Type_Bool, "IPL.CD", 1, current_offset);
  231. items[16].data[0] = 0x01;
  232. // IPL.CD2
  233. current_offset += create_item(items[17], Type_Bool, "IPL.CD2", 1, current_offset);
  234. items[17].data[0] = 0x01;
  235. // IPL.UPT
  236. current_offset += create_item(items[18], Type_Byte, "IPL.UPT", 1, current_offset);
  237. items[18].data[0] = 0x02;
  238. // IPL.PGS
  239. current_offset += create_item(items[19], Type_Byte, "IPL.PGS", 1, current_offset);
  240. // IPL.E60
  241. current_offset += create_item(items[20], Type_Byte, "IPL.E60", 1, current_offset);
  242. items[20].data[0] = 0x01;
  243. // IPL.DH
  244. current_offset += create_item(items[21], Type_Byte, "IPL.DH", 1, current_offset);
  245. // NET.WCFG
  246. current_offset += create_item(items[22], Type_Long, "NET.WCFG", 4, current_offset);
  247. items[22].data[3] = 0x01;
  248. // IPL.IDL
  249. current_offset += create_item(items[23], Type_SmallArray, "IPL.IDL", 1, current_offset);
  250. items[23].data[0] = 0x01;
  251. // IPL.EULA
  252. current_offset += create_item(items[24], Type_Bool, "IPL.EULA", 1, current_offset);
  253. items[24].data[0] = 0x01;
  254. // BT.MOT
  255. current_offset += create_item(items[25], Type_Byte, "BT.MOT", 1, current_offset);
  256. items[25].data[0] = 0x01;
  257. // MPLS.MOVIE
  258. current_offset += create_item(items[26], Type_Bool, "MPLS.MOVIE", 1, current_offset);
  259. items[26].data[0] = 0x01;
  260. for (const SSysConfEntry& item : items)
  261. m_Entries.push_back(item);
  262. File::CreateFullPath(m_FilenameDefault);
  263. File::IOFile g(m_FilenameDefault, "wb");
  264. // Write the header and item offsets
  265. g.WriteBytes(&s_Header.version, sizeof(s_Header.version));
  266. g.WriteBytes(&s_Header.numEntries, sizeof(u16));
  267. for (int i = 0; i != 27; ++i)
  268. {
  269. const u16 tmp_offset = Common::swap16(items[i].offset);
  270. g.WriteBytes(&tmp_offset, 2);
  271. }
  272. const u16 end_data_offset = Common::swap16(current_offset);
  273. g.WriteBytes(&end_data_offset, 2);
  274. // Write the items
  275. const u8 null_byte = 0;
  276. for (int i = 0; i != 27; ++i)
  277. {
  278. u8 description = (items[i].type << 5) | (items[i].nameLength - 1);
  279. g.WriteBytes(&description, sizeof(description));
  280. g.WriteBytes(&items[i].name, items[i].nameLength);
  281. switch (items[i].type)
  282. {
  283. case Type_BigArray:
  284. {
  285. const u16 tmpDataLength = Common::swap16(items[i].dataLength);
  286. g.WriteBytes(&tmpDataLength, 2);
  287. g.WriteBytes(items[i].data, items[i].dataLength);
  288. g.WriteBytes(&null_byte, 1);
  289. }
  290. break;
  291. case Type_SmallArray:
  292. g.WriteBytes(&items[i].dataLength, 1);
  293. g.WriteBytes(items[i].data, items[i].dataLength);
  294. g.WriteBytes(&null_byte, 1);
  295. break;
  296. default:
  297. g.WriteBytes(items[i].data, items[i].dataLength);
  298. break;
  299. }
  300. }
  301. // Pad file to the correct size
  302. const u64 cur_size = g.GetSize();
  303. for (unsigned int i = 0; i != 16380 - cur_size; ++i)
  304. g.WriteBytes(&null_byte, 1);
  305. // Write the footer
  306. g.WriteBytes("SCed", 4);
  307. m_Filename = m_FilenameDefault;
  308. m_IsValid = true;
  309. }
  310. bool SysConf::SaveToFile(const std::string& filename)
  311. {
  312. File::IOFile f(filename, "r+b");
  313. for (auto i = m_Entries.begin(); i < m_Entries.end() - 1; ++i)
  314. {
  315. // Seek to after the name of this entry
  316. f.Seek(i->offset + i->nameLength + 1, SEEK_SET);
  317. // We may have to write array length value...
  318. if (i->type == Type_BigArray)
  319. {
  320. const u16 tmpDataLength = Common::swap16(i->dataLength);
  321. f.WriteArray(&tmpDataLength, 1);
  322. }
  323. else if (i->type == Type_SmallArray)
  324. {
  325. const u8 len = (u8)(i->dataLength);
  326. f.WriteArray(&len, 1);
  327. }
  328. // Now write the actual data
  329. f.WriteBytes(i->data, i->dataLength);
  330. }
  331. return f.IsGood();
  332. }
  333. bool SysConf::Save()
  334. {
  335. if (!m_IsValid)
  336. return false;
  337. return SaveToFile(m_Filename);
  338. }
  339. void SysConf::UpdateLocation()
  340. {
  341. // if the old Wii User dir had a sysconf file save any settings that have been changed to it
  342. if (m_IsValid)
  343. Save();
  344. // Clear the old filename and set the default filename to the new user path
  345. // So that it can be generated if the file does not exist in the new location
  346. m_Filename.clear();
  347. // Note: We don't use the dummy Wii root here (if in use) because this is
  348. // all tied up with the configuration code. In the future this should
  349. // probably just be synced with the other settings.
  350. m_FilenameDefault = File::GetUserPath(D_WIIROOT_IDX) + DIR_SEP WII_SYSCONF_DIR DIR_SEP WII_SYSCONF;
  351. Reload();
  352. }
  353. bool SysConf::Reload()
  354. {
  355. std::string& filename = m_Filename.empty() ? m_FilenameDefault : m_Filename;
  356. LoadFromFile(filename);
  357. return m_IsValid;
  358. }