[PATCH] xbedump improvements

Richard Osborne <[email protected]> Thu, 29 Sep 2005 00:32:48 +0100
Newsgroups gmane.linux.ports.xbox.devel
Message-ID <[email protected]>
The attached patch

-Adds some basic error handling so xbedump won't seg fault if the
specified xbe doesn't exist or if out.xbe cannot be written to.
-Fixes a couple of typos
-Cleans up the usage infomation to make it easier to read

Richard
xbedump.diff (text/x-patch, 4.4 KB)
Index: xbedump/main.c
===================================================================
RCS file: /cvsroot/xbox-linux/xbedump/main.c,v
retrieving revision 1.13
diff -u -r1.13 main.c
--- xbedump/main.c	23 Jun 2003 02:22:24 -0000	1.13
+++ xbedump/main.c	28 Sep 2005 22:44:54 -0000
@@ -26,39 +26,27 @@
 
 void usage(){
 	
- printf("\n"	
-	 
-	"  Usage:    xbe [xbefile] [options]\n\n"
-	
+ printf(
+	"Usage: xbe xbefile [options]\n"
+	"\n"
+	"Options:\n"
 	"   -da          Dumps the compleate XBE Header Structure\n"
 	"   -dh          Dumps the Header info\n"
 	"   -dc          Dumps the Certificate\n"
 	"   -ds          Dumps the Sections\n"
-	"   -dl          Dumps the Libary Sections\n\n"
-	
+	"   -dl          Dumps the Library Sections\n"
 	"   -vh          Verifies the .xbe Header \n"
-	"   -wb          Writes back the update to file out.xbe \n\n"
-	
+	"   -wb          Writes back the update to file out.xbe \n"
 	"   -sm          Uses Microsoft Signature (default mode)\n"
-	"                (Note: Signing not possible, as we do not have the private key)\n"
-	"   -shabibi     Uses the Habibi Signature Keys\n\n"
-	"   -st          Uses the Test Keys i have created .. leaves the XOR unchanged\n\n"
-		
-	"   -d1          Debugoutput for option -vh\n\n"
-	
-	"  ---- Special Options -----\n\n"
-	"   -habibi      Special Option, Signes the xbe with Habibi Key and Sets all media flags\n\n"
-	"   -sign        Special Option, Signes the xbe with the key who is stored in the xboxlib.c\n"
-	"                patches the XOR Keys\n"
-	
+	"   -shabibi     Uses the Habibi Signature Keys\n"
+	"   -st          Uses the Test Keys i have created .. leaves the XOR unchanged\n"
+	"   -d1          Debugoutput for option -vh\n"
+	"   -habibi      Signes the xbe with Habibi Key and Sets all media flags\n"
+	"   -sign        Signes the xbe with the key who is stored in the xboxlib.c\n"
 	"   -xbgs        Dumps xbgs output\n"
 	"   ?            Display Help\n\n"
 	
-	"  Note:         This code will work on little-endian 32-bit machines only! \n\n"
-	
 	"  (C)2002,2003 by XBL Team (hamtitampti) \n");
-	
-	
 }
 
 int main (int argc, const char * argv[])
@@ -146,9 +134,9 @@
 				dumpxbe((void *)xbefile,dumpflag);
 				}
 		if (dumpflag & 0x0fff0000) {
-				load_xbefile(xbefile,filesize,&filename[0]);						
+				load_xbefile(xbefile,filesize,&filename[0]);
 				validatexbe((void *)xbefile,filesize,dumpflag);
-				}
+		}
 						
 		// Verify the signed file
 		
@@ -172,7 +160,6 @@
 
 	}
 
-	printf("\n");
 	return 0;
 }
 
Index: xbedump/xbevalidate.c
===================================================================
RCS file: /cvsroot/xbox-linux/xbedump/xbevalidate.c,v
retrieving revision 1.12
diff -u -r1.12 xbevalidate.c
--- xbedump/xbevalidate.c	23 Jun 2003 02:22:24 -0000	1.12
+++ xbedump/xbevalidate.c	28 Sep 2005 22:44:54 -0000
@@ -70,9 +70,12 @@
 	eax += 0x10000;
 	
 	// Validates the Certificate Entry Address 
-	printf("Certificate Adress:    ");
+	printf("Certificate Address:   ");
 	if (eax == (int)header->Certificate) { printf("pass\n"); } else { fail=1; printf("fail\n"); }
 
+	// Only continue if xbe is valid
+	if (fail == 1) { fprintf(stderr,"Invalid xbe\n"); exit(1); }
+	
 	printf("Certificate Size  :    ");
 	cert = (XBE_CERTIFICATE *)(((char *)xbe) + (int)header->Certificate - (int)header->BaseAddress);
 	if (cert->Size>=0x1d0) { printf("pass\n"); } else { fail=1; printf("fail\n"); }
@@ -176,12 +179,16 @@
 	if (fail==0) printf("\nXBE file integrity:    OK\n"); else  printf("\nXBE file integrity:    FALSE !!!!!!! FALSE !!!!!\n");
 	
 	if (option_flag & 0x00020000){
-	 f = fopen("out.xbe", "wb");
-	 fwrite(xbe, 1, filesize, f);
-         
-         fclose(f);	
-		
-	}
+		f = fopen("out.xbe", "wb");
+		if (f==NULL) {
+			fprintf(stderr,"\nError writing out.xbe - %s\n",strerror(errno));
+			exit(1);
+		}
+	else {
+		fwrite(xbe, 1, filesize, f);
+		fclose(f);	
+	}	
+}
 
 	
 	
Index: xbedump/xboxlib.c
===================================================================
RCS file: /cvsroot/xbox-linux/xbedump/xboxlib.c,v
retrieving revision 1.17
diff -u -r1.17 xboxlib.c
--- xbedump/xboxlib.c	3 Jul 2003 19:03:22 -0000	1.17
+++ xbedump/xboxlib.c	28 Sep 2005 22:44:55 -0000
@@ -587,8 +587,8 @@
          fread(file, 1, filesize, f);
          fclose(f);	
    } else {
-        printf("File Not found %s, use parameter filename \n",filename);
-   	      
+        fprintf(stderr,"Error opening %s - %s\n",filename,strerror(errno));
+	exit(1);
     }
 	return 0;
 }