Guest User

Untitled

a guest
Apr 21st, 2018
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.74 KB | None | 0 0
  1. /*-------------------------------------------------------------
  2.  
  3. title_lister.c -- displays contents of Wii NAND flash FS
  4.  
  5. Copyright (C) 2008 bushing
  6.  
  7. This software is provided 'as-is', without any express or implied
  8. warranty. In no event will the authors be held liable for any
  9. damages arising from the use of this software.
  10.  
  11. Permission is granted to anyone to use this software for any
  12. purpose, including commercial applications, and to alter it and
  13. redistribute it freely, subject to the following restrictions:
  14.  
  15. 1.The origin of this software must not be misrepresented; you
  16. must not claim that you wrote the original software. If you use
  17. this software in a product, an acknowledgment in the product
  18. documentation would be appreciated but is not required.
  19.  
  20. 2.Altered source versions must be plainly marked as such, and
  21. must not be misrepresented as being the original software.
  22.  
  23. 3.This notice may not be removed or altered from any source
  24. distribution.
  25.  
  26. -------------------------------------------------------------*/
  27.  
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <malloc.h>
  32. #include <ogcsys.h>
  33. #include <gccore.h>
  34. #include <ogc/ipc.h>
  35. #include <stdarg.h>
  36. #include <ctype.h>
  37. #include <unistd.h>
  38. #include <assert.h>
  39. #include <network.h>
  40.  
  41. char ascii(char s) {
  42. if(s < 0x20) return '.';
  43. if(s > 0x7E) return '.';
  44. return s;
  45. }
  46.  
  47. static void *xfb = NULL;
  48. static GXRModeObj *rmode = NULL;
  49.  
  50. typedef void (*Loader_Entry)(void);
  51.  
  52. Loader_Entry loader = (Loader_Entry)0x80001800;
  53.  
  54. void checkAndReload(void) {
  55. PAD_ScanPads();
  56. int buttonsDown = PAD_ButtonsHeld(0);
  57. if( (buttonsDown & PAD_TRIGGER_Z) && (buttonsDown & PAD_BUTTON_START)) {
  58. loader();
  59. }
  60. }
  61.  
  62. void waita(void) {
  63. while(1) {
  64. VIDEO_WaitVSync();
  65. PAD_ScanPads();
  66. int buttonsDown = PAD_ButtonsDown(0);
  67. if(buttonsDown & PAD_BUTTON_A)
  68. return;
  69. if( (buttonsDown & PAD_TRIGGER_Z) && (buttonsDown & PAD_BUTTON_START)) {
  70. loader();
  71. }
  72. }
  73. }
  74.  
  75. void printvers(void) {
  76. printf("IOS Version: %08x\n", *((u32*)0xC0003140));
  77. }
  78.  
  79. s32 __STM_Init();
  80.  
  81. void console_setup(void) {
  82. VIDEO_Init();
  83. PAD_Init();
  84.  
  85. switch(VIDEO_GetCurrentTvMode()) {
  86. case VI_NTSC:
  87. rmode = &TVNtsc480IntDf;
  88. break;
  89. case VI_PAL:
  90. rmode = &TVPal528IntDf;
  91. break;
  92. case VI_MPAL:
  93. rmode = &TVMpal480IntDf;
  94. break;
  95. default:
  96. rmode = &TVNtsc480IntDf;
  97. break;
  98. }
  99.  
  100. xfb = MEM_K0_TO_K1(SYS_AllocateFramebuffer(rmode));
  101. console_init(xfb,20,20,rmode->fbWidth,rmode->xfbHeight,rmode->fbWidth*VI_DISPLAY_PIX_SZ);
  102.  
  103. VIDEO_Configure(rmode);
  104. VIDEO_SetNextFramebuffer(xfb);
  105. VIDEO_SetBlack(FALSE);
  106. VIDEO_Flush();
  107. VIDEO_WaitVSync();
  108. if(rmode->viTVMode&VI_NON_INTERLACE) VIDEO_WaitVSync();
  109. }
  110.  
  111. char * display_tmd_info(const tmd *t) {
  112. static char desc[256];
  113. u32 kind = t->title_id >> 32;
  114. u32 title_l = t->title_id & 0xFFFFFFFF;
  115.  
  116. char title_ascii[5];
  117. u32 i;
  118. memcpy(title_ascii, &title_l, 4);
  119. for (i=0; i<4; i++) title_ascii[i]=ascii(title_ascii[i]);
  120. title_ascii[4]='\0';
  121.  
  122. switch (kind) {
  123. case 1: // IOS, MIOS, BC, System Menu
  124. snprintf(desc, sizeof(desc), "Title=1-%x (", title_l);
  125. switch(title_l) {
  126. case 2: strlcat(desc, "System Menu) ", sizeof(desc)); break;
  127. case 0x100: strlcat(desc, "BC) ", sizeof(desc)); break;
  128. case 0x101: strlcat(desc, "MIOS) ", sizeof(desc)); break;
  129. default: sprintf(desc + strlen(desc), "IOS%d) ", title_l); break;
  130. }
  131. break;
  132. case 0x10000: // TMD installed by running a disc
  133. snprintf(desc, sizeof(desc), "Title=10000-%08x (savedata for '%s')",
  134. title_l, title_ascii); break;
  135. case 0x10001: // Normal channels / VC
  136. snprintf(desc, sizeof(desc), "Title=10001-%08x (downloaded channel '%s')",
  137. title_l, title_ascii); break;
  138. case 0x10002: // "System channels" -- News, Weather, etc.
  139. snprintf(desc, sizeof(desc), "Title=10002-%08x (system channel '%s')",
  140. title_l, title_ascii); break;
  141. case 0x10004: // "Hidden channels" -- WiiFit channel
  142. snprintf(desc, sizeof(desc), "Title=10004-%08x (game channel '%s')",
  143. title_l, title_ascii); break;
  144. case 0x10008: // "Hidden channels" -- EULA, rgnsel
  145. snprintf(desc, sizeof(desc), "Title=10008-%08x (hidden? channel '%s')",
  146. title_l, title_ascii); break;
  147. default:
  148. printf("Unknown title type %x %08x\n", kind, title_l);
  149. break;
  150. }
  151.  
  152. if (t->title_version)
  153. snprintf(desc, sizeof(desc), "%s vers: %d.%d (%d)", desc, t->title_version >> 8, t->title_version & 0xFF,
  154. t->title_version);
  155. if (t->sys_version) snprintf(desc, sizeof(desc), "%s FW: IOS%llu ", desc, t->sys_version & 0xff);
  156.  
  157. return desc;
  158. }
  159. /* code to establish a socket; originally from bzs@bu-cs.bu.edu
  160. */
  161.  
  162. int establish(unsigned short portnum) {
  163. int s;
  164. struct sockaddr_in sa;
  165.  
  166. memset(&sa, 0, sizeof(struct sockaddr_in)); /* clear our address */
  167. sa.sin_family= AF_INET; /* this is our host address */
  168. sa.sin_port= htons(portnum); /* this is our port number */
  169. sa.sin_addr.s_addr = net_gethostip();
  170. sa.sin_len = 8;
  171. if ((s= net_socket(AF_INET, SOCK_STREAM, 0)) < 0) /* create socket */
  172. return(-1);
  173. if (net_bind(s,(struct sockaddr *)&sa,8) < 0) {
  174. net_close(s);
  175. return(-1); /* bind address to socket */
  176. }
  177. net_listen(s, 3); /* max # of queued connects */
  178. return(s);
  179. }
  180.  
  181. // wait for a connection to occur on a socket created with establish()
  182.  
  183. int get_connection(int s)
  184. {
  185. int t; /* socket of connection */
  186. struct sockaddr_in sa;
  187. sa.sin_len = 8;
  188. sa.sin_family = AF_INET;
  189. u32 buflen = 8;
  190. t = net_accept(s,(struct sockaddr *)&sa, &buflen);
  191. printf("Incoming connection from %d.%d.%d.%d\n",
  192. (sa.sin_addr.s_addr >> 24) & 0xFF,
  193. (sa.sin_addr.s_addr >> 16) & 0xFF,
  194. (sa.sin_addr.s_addr >> 8) & 0xFF,
  195. (sa.sin_addr.s_addr) & 0xFF);
  196.  
  197. return t;
  198. }
  199.  
  200. int read_data(int s, /* connected socket */
  201. char *buf, /* pointer to the buffer */
  202. int n /* number of characters (bytes) we want */
  203. )
  204. { int bcount; /* counts bytes read */
  205. int br; /* bytes read this pass */
  206.  
  207. bcount= 0;
  208. br= 0;
  209. while (bcount < n) { /* loop until full buffer */
  210. if ((br= net_read(s,buf,n-bcount)) > 0) {
  211. bcount += br; /* increment byte counter */
  212. buf += br; /* move buffer ptr for next read */
  213. }
  214. else if (br < 0) /* signal an error to the caller */
  215. return br;
  216. }
  217. // debug_printf("read_data(%d, %p, %d): \n", s, buf, n);
  218. return bcount;
  219. }
  220.  
  221. int write_data(int s, char *buf, int n)
  222. {
  223. int bcount;
  224. int br;
  225. bcount = 0;
  226. br = 0;
  227. while(bcount < n) {
  228. if ((br = net_write(s,buf,n-bcount)) > 0) {
  229. bcount += br;
  230. buf += br;
  231. }
  232. else if (br < 0)
  233. return br;
  234. }
  235. // debug_printf("write_data(%d, %p, %d): \n", s, buf, n);
  236. // hexdump(buf, bcount);
  237. return (bcount);
  238. }
  239.  
  240. int socket_printf(const s32 socket, const char *format, ...) {
  241. char *buf;
  242. int retval;
  243. va_list ap;
  244. va_start(ap, format);
  245. retval = vasprintf(&buf, format, ap);
  246. va_end(ap);
  247. write_data(socket, buf, retval);
  248. free(buf);
  249. return retval;
  250. }
  251.  
  252. int main(int argc, char **argv) {
  253.  
  254. console_setup();
  255. // printvers();
  256.  
  257. __STM_Init();
  258. SYS_SetResetCallback(loader);
  259. printf("Initializing network, please wait.\n");
  260. net_init();
  261.  
  262. s32 ret;
  263. u32 count;
  264. printf("Opening listening socket on port 8080:\n");
  265. s32 listener = establish(8080);
  266. printf("Listener socket = %d\n", listener);
  267. s32 client_socket = get_connection(listener);
  268.  
  269. if (client_socket >= 0) {
  270. socket_printf(client_socket, "Hello, World!\n");
  271. ret = ES_GetNumTitles(&count);
  272. if (ret) {
  273. socket_printf(client_socket, "ES_GetNumTitles=%d, count=%08x\n", ret, count);
  274. return ret;
  275. }
  276.  
  277. socket_printf(client_socket, "Found %d titles:\n", count);
  278.  
  279. static u64 title_list[256] ATTRIBUTE_ALIGN(32);
  280. ret = ES_GetTitles(title_list, count);
  281. if (ret) {
  282. socket_printf(client_socket, "ES_GetTitles=%d\n", ret);
  283. return ret;
  284. }
  285.  
  286.  
  287. int i;
  288.  
  289. for(i=0; i<0xe0; i++) socket_printf(client_socket, "%02hhx", buffer[i]);
  290. socket_printf(client_socket, "\n");
  291.  
  292. for (i=0; i < count; i++) {
  293. u32 tmd_size;
  294. ret = ES_GetStoredTMDSize(title_list[i], &tmd_size);
  295. static u8 tmd_buf[MAX_SIGNED_TMD_SIZE] ATTRIBUTE_ALIGN(32);
  296. signed_blob *s_tmd = (signed_blob *)tmd_buf;
  297. ret = ES_GetStoredTMD(title_list[i], s_tmd, tmd_size);
  298. socket_printf(client_socket, "%d: %s\n", i+1, display_tmd_info(SIGNATURE_PAYLOAD(s_tmd)));
  299. }
  300. net_close(client_socket);
  301. }
  302. return 0;
  303. }
Add Comment
Please, Sign In to add comment