Advertisement
Guest User

Untitled

a guest
Sep 25th, 2017
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.44 KB | None | 0 0
  1. //NANDReader
  2.      
  3. #include <fcntl.h>
  4. #include <unistd.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8.      
  9. #include <debug.h>
  10. #include <xenos/xenos.h>
  11. #include <console/console.h>
  12. #include <time/time.h>
  13. #include <usb/usbmain.h>
  14. #include <ppc/register.h>
  15. #include <xenon_nand/xenon_sfcx.h>
  16. #include <xenon_nand/xenon_config.h>
  17. #include <xenon_soc/xenon_secotp.h>
  18. #include <xenon_smc/xenon_smc.h>
  19.  
  20.     //Xenos, console, USB inits
  21. void mainInit(){
  22.     xenos_init(VIDEO_MODE_AUTO);
  23.     console_init();
  24.  
  25.     usb_init();
  26.     usb_do_poll();
  27. }
  28.  
  29.     //Read the entire NAND to file on USB
  30. void writeNANDToFile() {
  31.     if(sfcx_init()<10)
  32.     return;
  33.  
  34.     FILE *f = fopen("uda:/NAND.bin", "wb");
  35.  
  36.     if(f){
  37.         unsigned char* buff = (unsigned char*) malloc(sfc.block_sz_phys);
  38.  
  39.             unsigned int buffer_len = sfc.block_sz_phys;
  40.  
  41.         int i;      
  42.  
  43.             for (i = 0; i < sfc.size_bytes; i += sfc.block_sz) {
  44.                     sfcx_read_block(buff, i, 1);
  45.            
  46.            
  47.                     if(buffer_len!=fwrite(buff, 1, buffer_len, f))
  48.                     {
  49.                         printf("Error dumping nand !!! please check disk space !!\n");
  50.                     }
  51.                 printf("Reading...\r");
  52.             }
  53.  
  54.             fclose(f);
  55.             free(buff);
  56.         }
  57. }
  58.  
  59.     //Read UPDNAND.bin from USB, to memory buffer, to NAND
  60. void writeFileToFlash(){
  61.     int j;
  62.  
  63.     FILE *UPDNAND;
  64.     unsigned char *buffer;
  65.     unsigned long fileLen;
  66.  
  67.     //Open file
  68.     UPDNAND = fopen("uda:/UPDNAND.bin", "rb");
  69.    
  70.     //Get file length
  71.     fseek(UPDNAND, 0, SEEK_END);
  72.     fileLen=ftell(UPDNAND);
  73.     fseek(UPDNAND, 0, SEEK_SET);
  74.  
  75.     //Allocate memory to *buffer
  76.     buffer=(unsigned char *)malloc(fileLen+1);
  77.     if (!buffer)
  78.     {
  79.         fprintf(stderr, "Memory error!");
  80.         fclose(UPDNAND);
  81.         return -1;
  82.     }
  83.  
  84.     //Read file contents into buffer
  85.     fread(buffer, fileLen, 1, UPDNAND);
  86.     fclose(UPDNAND);
  87.  
  88.     //Do what ever with buffer
  89.     for (i = 0; i < sfc.size_bytes; i += sfc.block_sz) {
  90.         sfcx_read_block(buffer, i, 1);
  91.         printf("Writing...\r");
  92.     }
  93.  
  94.     printf("\nDone!\n");
  95.  
  96.     free(buffer);
  97. }
  98.      
  99. int main(){
  100.     mainInit();
  101.            
  102.     printf("SFCX Init.\n");
  103.     sfcx_init();
  104.     if (sfc.initialized != SFCX_INITIALIZED)
  105.         {
  106.         printf(" ! sfcx initialization failure\n");
  107.         printf(" ! nand related features will not be available\n");
  108.         delay(5);
  109.         return -1;
  110.         }
  111.  
  112.     printf("Xenon_config_init.\n");
  113.     xenon_config_init();
  114.            
  115.     writeNANDToFile();
  116.      
  117.     return 0;
  118.      
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement