⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 patchmii_core.c

📁 xyzzy is a program for the Nintendo Wii which will read a console s OTP key storage and dump out the
💻 C
📖 第 1 页 / 共 2 页
字号:
/*  patchmii_core -- low-level functions to handle the downloading, patching    and installation of updates on the Wii    Copyright (C) 2008 bushing / hackmii.com    This program is free software; you can redistribute it and/or modify    it under the terms of the GNU General Public License as published by    the Free Software Foundation, version 2.    This program is distributed in the hope that it will be useful,    but WITHOUT ANY WARRANTY; without even the implied warranty of    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    GNU General Public License for more details.    You should have received a copy of the GNU General Public License    along with this program; if not, write to the Free Software    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA*/#include <stdio.h>#include <stdlib.h>#include <string.h>#include <malloc.h>#include <ogcsys.h>#include <gccore.h>#include <stdarg.h>#include <ctype.h>#include <unistd.h>#include <network.h>#include <sys/errno.h>#include "patchmii_core.h"#include "sha1.h"#include "debug.h"#include "http.h"#include "haxx_certs.h"#define VERSION "0.1"#define ALIGN(a,b) ((((a)+(b)-1)/(b))*(b))int http_status = 0;int tmd_dirty = 0, tik_dirty = 0, temp_ios_slot = 0;// yeah, yeah, I know.signed_blob *s_tmd = NULL, *s_tik = NULL, *s_certs = NULL;static u8 tmdbuf[MAX_SIGNED_TMD_SIZE] ATTRIBUTE_ALIGN(0x20);static u8 tikbuf[STD_SIGNED_TIK_SIZE] ATTRIBUTE_ALIGN(0x20);void debug_printf(const char *fmt, ...) {  char buf[1024];  int len;  va_list ap;  usb_flush(1);  va_start(ap, fmt);  len = vsnprintf(buf, sizeof(buf), fmt, ap);  va_end(ap);  if (len <= 0 || len > sizeof(buf)) printf("Error: len = %d\n", len);  else usb_sendbuffer(1, buf, len);  puts(buf);  fflush(stdout);}void gecko_printf(const char *fmt, ...) {  char buf[1024];  int len;  va_list ap;  usb_flush(1);  va_start(ap, fmt);  len = vsnprintf(buf, sizeof(buf), fmt, ap);  va_end(ap);  if (len <= 0 || len > sizeof(buf)) printf("Error: len = %d\n", len);  else usb_sendbuffer(1, buf, len);}char ascii(char s) {  if(s < 0x20) return '.';  if(s > 0x7E) return '.';  return s;}void hexdump(FILE *fp, void *d, int len) {  u8 *data;  int i, off;  data = (u8*)d;  for (off=0; off<len; off += 16) {    fprintf(fp, "%08x  ",off);    for(i=0; i<16; i++)      if((i+off)>=len) fprintf(fp, "   ");      else fprintf(fp, "%02x ",data[off+i]);    fprintf(fp, " ");    for(i=0; i<16; i++)      if((i+off)>=len) fprintf(fp," ");      else fprintf(fp,"%c",ascii(data[off+i]));    fprintf(fp,"\n");  }}char *spinner_chars="/-\\|";int spin = 0;void spinner(void) {  printf("\b%c", spinner_chars[spin++]);  if(!spinner_chars[spin]) spin=0;}char *things[] = {"people", "hopes", "fail", "bricks", "firmware", "bugs", "hacks"};u32 progress_count = 0;void progress(int delta) {	if (!(progress_count%10)) spinner();	progress_count += delta;//	gecko_printf("progress=%u\n", progress_count);	if ((progress_count % 800) == 0 && (progress_count/800)< (sizeof(things)/4)) {		printf("\b %s......", things[(progress_count/800)]);	}}static void *xfb = NULL;static GXRModeObj *rmode = NULL;void printvers(void) {  debug_printf("IOS Version: %08x\n", *((u32*)0xC0003140));}void console_setup(void) {  VIDEO_Init();  PAD_Init();    rmode = VIDEO_GetPreferredMode(NULL);  xfb = MEM_K0_TO_K1(SYS_AllocateFramebuffer(rmode));  VIDEO_ClearFrameBuffer(rmode,xfb,COLOR_BLACK);  VIDEO_Configure(rmode);  VIDEO_SetNextFramebuffer(xfb);  VIDEO_SetBlack(FALSE);  VIDEO_Flush();  VIDEO_WaitVSync();  if(rmode->viTVMode&VI_NON_INTERLACE) VIDEO_WaitVSync();  CON_InitEx(rmode,20,30,rmode->fbWidth - 40,rmode->xfbHeight - 60);}int get_nus_object(u32 titleid1, u32 titleid2, char *content, u8 **outbuf, u32 *outlen) {  static char buf[128];  int retval;  u32 http_status;  progress(100);  snprintf(buf, 128, "http://nus.cdn.shop.wii.com/ccs/download/%08x%08x/%s",	   titleid1, titleid2, content);  retval = http_request(buf, 1 << 31);  if (!retval) {    debug_printf("Error making http request\n");    return 1;  }  retval = http_get_result(&http_status, outbuf, outlen);   if (((int)*outbuf & 0xF0000000) == 0xF0000000) {    return (int) *outbuf;  }  return 0;}void decrypt_buffer(u16 index, u8 *source, u8 *dest, u32 len) {  static u8 iv[16];  if (!source) {	debug_printf("decrypt_buffer: invalid source paramater\n");	exit(1);  }  if (!dest) {	debug_printf("decrypt_buffer: invalid dest paramater\n");	exit(1);  }  memset(iv, 0, 16);  memcpy(iv, &index, 2);  aes_decrypt(iv, source, dest, len);}static u8 encrypt_iv[16];void set_encrypt_iv(u16 index) {  memset(encrypt_iv, 0, 16);  memcpy(encrypt_iv, &index, 2);}  void encrypt_buffer(u8 *source, u8 *dest, u32 len) {  aes_encrypt(encrypt_iv, source, dest, len);}int create_temp_dir(void) {  int retval;  retval = ISFS_CreateDir ("/tmp/patchmii", 0, 3, 1, 1);  if (retval) debug_printf("ISFS_CreateDir(/tmp/patchmii) returned %d\n", retval);  return retval;}u32 save_nus_object (u16 index, u8 *buf, u32 size) {  char filename[256];  static u8 bounce_buf[1024] ATTRIBUTE_ALIGN(0x20);  u32 i;  int retval, fd;  snprintf(filename, sizeof(filename), "/tmp/patchmii/%08x", index);    retval = ISFS_CreateFile (filename, 0, 3, 1, 1);  if (retval != ISFS_OK) {    debug_printf("ISFS_CreateFile(%s) returned %d\n", filename, retval);    return retval;  }    fd = ISFS_Open (filename, ISFS_ACCESS_WRITE);  if (fd < 0) {    debug_printf("ISFS_OpenFile(%s) returned %d\n", filename, fd);    return retval;  }  for (i=0; i<size;) {    u32 numbytes = ((size-i) < 1024)?size-i:1024;	progress(1);    memcpy(bounce_buf, buf+i, numbytes);    retval = ISFS_Write(fd, bounce_buf, numbytes);    if (retval < 0) {      debug_printf("ISFS_Write(%d, %p, %d) returned %d at offset %d\n", 		   fd, bounce_buf, numbytes, retval, i);      ISFS_Close(fd);      return retval;    }    i += retval;  }  ISFS_Close(fd);  return size;}s32 install_nus_object (tmd *p_tmd, u16 index) {  char filename[256];  static u8 bounce_buf1[1024] ATTRIBUTE_ALIGN(0x20);  static u8 bounce_buf2[1024] ATTRIBUTE_ALIGN(0x20);  u32 i;  const tmd_content *p_cr = TMD_CONTENTS(p_tmd);//  debug_printf("install_nus_object(%p, %lu)\n", p_tmd, index);    int retval, fd, cfd, ret;  snprintf(filename, sizeof(filename), "/tmp/patchmii/%08x", p_cr[index].cid);  progress(20);  fd = ISFS_Open (filename, ISFS_ACCESS_READ);    if (fd < 0) {    debug_printf("ISFS_OpenFile(%s) returned %d\n", filename, fd);    return fd;  }  set_encrypt_iv(index);//  debug_printf("ES_AddContentStart(%016llx, %x)\n", p_tmd->title_id, index);  cfd = ES_AddContentStart(p_tmd->title_id, p_cr[index].cid);  if(cfd < 0) {    debug_printf(":\nES_AddContentStart(%016llx, %x) failed: %d\n",p_tmd->title_id, index, cfd);    ES_AddTitleCancel();    return -1;  } // debug_printf("\b (cfd %d): ",cfd);  for (i=0; i<p_cr[index].size;) {    u32 numbytes = ((p_cr[index].size-i) < 1024)?p_cr[index].size-i:1024;	progress(1);    numbytes = ALIGN(numbytes, 32);    retval = ISFS_Read(fd, bounce_buf1, numbytes);    if (retval < 0) {      debug_printf("ISFS_Read(%d, %p, %d) returned %d at offset %d\n", 		   fd, bounce_buf1, numbytes, retval, i);      ES_AddContentFinish(cfd);      ES_AddTitleCancel();      ISFS_Close(fd);      return retval;    }        encrypt_buffer(bounce_buf1, bounce_buf2, sizeof(bounce_buf1));    ret = ES_AddContentData(cfd, bounce_buf2, retval);    if (ret < 0) {      debug_printf("ES_AddContentData(%d, %p, %d) returned %d\n", cfd, bounce_buf2, retval, ret);      ES_AddContentFinish(cfd);      ES_AddTitleCancel();      ISFS_Close(fd);      return ret;    }    i += retval;  } // debug_printf("\b  done! (0x%x bytes)\n",i);  ret = ES_AddContentFinish(cfd);  if(ret < 0) {    printf("ES_AddContentFinish failed: %d\n",ret);    ES_AddTitleCancel();    ISFS_Close(fd);    return -1;  }progress(2);   ISFS_Close(fd);    return 0;}int get_title_key(signed_blob *s_tik, u8 *key) {  static u8 iv[16] ATTRIBUTE_ALIGN(0x20);  static u8 keyin[16] ATTRIBUTE_ALIGN(0x20);  static u8 keyout[16] ATTRIBUTE_ALIGN(0x20);  int retval;  const tik *p_tik;  p_tik = (tik*)SIGNATURE_PAYLOAD(s_tik);  u8 *enc_key = (u8 *)&p_tik->cipher_title_key;  memcpy(keyin, enc_key, sizeof keyin);  memset(keyout, 0, sizeof keyout);  memset(iv, 0, sizeof iv);  memcpy(iv, &p_tik->titleid, sizeof p_tik->titleid);    retval = ES_Decrypt(ES_KEY_COMMON, iv, keyin, sizeof keyin, keyout);  if (retval) debug_printf("ES_Decrypt returned %d\n", retval);  memcpy(key, keyout, sizeof keyout);  return retval;}int change_ticket_title_id(signed_blob *s_tik, u32 titleid1, u32 titleid2) {	static u8 iv[16] ATTRIBUTE_ALIGN(0x20);	static u8 keyin[16] ATTRIBUTE_ALIGN(0x20);	static u8 keyout[16] ATTRIBUTE_ALIGN(0x20);	int retval;	tik *p_tik;	p_tik = (tik*)SIGNATURE_PAYLOAD(s_tik);	u8 *enc_key = (u8 *)&p_tik->cipher_title_key;	memcpy(keyin, enc_key, sizeof keyin);	memset(keyout, 0, sizeof keyout);	memset(iv, 0, sizeof iv);	memcpy(iv, &p_tik->titleid, sizeof p_tik->titleid);	retval = ES_Decrypt(ES_KEY_COMMON, iv, keyin, sizeof keyin, keyout);	p_tik->titleid = (u64)titleid1 << 32 | (u64)titleid2;	memset(iv, 0, sizeof iv);	memcpy(iv, &p_tik->titleid, sizeof p_tik->titleid);		retval = ES_Encrypt(ES_KEY_COMMON, iv, keyout, sizeof keyout, keyin);    if (retval) debug_printf("ES_Decrypt returned %d\n", retval);	memcpy(enc_key, keyin, sizeof keyin);	tik_dirty = 1;    return retval;}s32 get_title_version(u32 titleid1, u32 titleid2) {	u32 tmdsize=0;	s32 retval;	static char tmd_buf[1024] ATTRIBUTE_ALIGN(32);	signed_blob *stmd = (signed_blob *)&tmd_buf[0];	int version;	u64 titleid =  (u64)titleid1 << 32 | (u64)titleid2;	retval = ES_GetStoredTMDSize(titleid, &tmdsize);	if (retval < 0) {		if (retval != -106) debug_printf("ES_GetStoredTMDSize(%llx) = %x, retval=%d\n", titleid, tmdsize, retval);		return retval;	}  	retval = ES_GetStoredTMD(titleid, stmd, tmdsize);   	if (retval < 0) {		debug_printf("ES_GetStoredTMD returned %d\n", retval);		return retval;	}	tmd *mytmd = (tmd*)SIGNATURE_PAYLOAD(stmd);	version = mytmd->title_version;	return version;}void change_tmd_version(signed_blob *s_tmd, u32 version) {	tmd *p_tmd;	p_tmd = (tmd*)SIGNATURE_PAYLOAD(s_tmd);	p_tmd->title_version = version;	tmd_dirty = 1;}void change_tmd_title_id(signed_blob *s_tmd, u32 titleid1, u32 titleid2) {	tmd *p_tmd;	u64 title_id = titleid1;	title_id <<= 32;	title_id |= titleid2;	p_tmd = (tmd*)SIGNATURE_PAYLOAD(s_tmd);	p_tmd->title_id = title_id;	tmd_dirty = 1;}void display_tag(u8 *buf) {  debug_printf("Firmware version: %s      Builder: %s",	       buf, buf+0x30);}void display_ios_tags(u8 *buf, u32 size) {  u32 i;  char *ios_version_tag = "$IOSVersion:";  if (size == 64) {    display_tag(buf);    return;  }  for (i=0; i<(size-64); i++) {    if (!strncmp((char *)buf+i, ios_version_tag, 10)) {      char version_buf[128], *date;      while (buf[i+strlen(ios_version_tag)] == ' ') i++; // skip spaces      strlcpy(version_buf, (char *)buf + i + strlen(ios_version_tag), sizeof version_buf);      date = version_buf;      strsep(&date, "$");      date = version_buf;      strsep(&date, ":");      debug_printf("%s (%s)\n", version_buf, date);      i += 64;    }  }}/* This patch allows ES_DeleteTitle and ES_DeleteTicket to delete IOS versions; careful! */int patch_iosdelete(u8 *buf, u32 size){

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -