Advertisement
Evilnat

stage0

May 29th, 2025
621
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.26 KB | None | 0 0
  1. #include <lv2/lv2.h>
  2. #include <lv2/memory.h>
  3. #include <lv2/io.h>
  4. #include <lv2/libc.h>
  5. #include <lv2/thread.h>
  6. #include <lv2/patch.h>
  7. #include <lv1/lv1.h>
  8.  
  9. #define STAGE2_FLASH            "/dev_flash/sys/stage2"
  10. #define STAGE2_BLIND            "/dev_blind/sys/stage2"
  11. #define STAGE2_USB0             "/dev_usb000/stage2"
  12. #define USB_FLAG                "/dev_flash/sys/usb_flag"
  13.  
  14. #if defined (FIRMWARE_CEX)
  15.     #define STAGE2_FILE         STAGE2_FLASH ".cex"
  16.     #define STAGE2_DEVBLIND     STAGE2_BLIND ".cex"
  17.     #define STAGE2_USB0_FILE    STAGE2_USB0  ".cex"
  18. #elif defined (FIRMWARE_DEX)
  19.     #define STAGE2_FILE         STAGE2_FLASH ".dex"
  20.     #define STAGE2_DEVBLIND     STAGE2_BLIND ".dex"
  21.     #define STAGE2_USB0_FILE    STAGE2_USB0  ".dex"
  22. #endif
  23.  
  24. void main(void)
  25. {
  26.     void *stage2 = NULL;
  27.     char *stage2_file = (char *)STAGE2_FILE;
  28.    
  29.     f_desc_t f;
  30.     int (* func)(void);
  31.  
  32.     CellFsStat stat;
  33.     int fd;
  34.     uint64_t rs;
  35.  
  36.     for (int i = 0; i < 128; i++)
  37.     {
  38.         uint64_t pte0 = *(uint64_t *)(MKA(0xf000000 | (i << 7)));
  39.         uint64_t pte1 = *(uint64_t *)(MKA(0xf000000 | ((i << 7) + 8)));    
  40.         lv1_write_htab_entry(0, i << 3, pte0, (pte1 & 0xff0000) | 0x190);
  41.     }
  42.  
  43.     // Check if usb_flag exists in /dev_flash
  44.     if(cellFsStat(USB_FLAG, &stat) == 0)
  45.     {
  46.         // Mounting /dev_usb000 (Thanks to haxxxen)
  47.         if(cellFsUtilMount_Usb000() == 0)
  48.         {
  49.             // Use external stage2.cex
  50.             stage2_file = (char *)STAGE2_USB0_FILE;
  51.         }
  52.     }
  53.  
  54.     // Load stage2
  55.     if(cellFsStat(stage2_file, &stat) == 0)
  56.     {
  57.         // Avoid loading an empty stage2 or with a size higher than 0x1FE00
  58.         if(stat.st_size != 0 && stat.st_size < 0x1FE00)
  59.         {
  60.             if(cellFsOpen(stage2_file, CELL_FS_O_RDONLY, &fd, 0, NULL, 0) == 0)
  61.             {
  62.                 stage2 = alloc(stat.st_size, 0x27);
  63.                 if(stage2)
  64.                 {      
  65.                     if(cellFsRead(fd, stage2, stat.st_size, &rs) != 0)
  66.                     {
  67.                         dealloc(stage2, 0x27);
  68.                         stage2 = NULL;
  69.                     }                      
  70.                 }  
  71.  
  72.                 cellFsClose(fd);
  73.             }
  74.         }
  75.     }
  76.  
  77.     f.toc = (void *)MKA(TOC);
  78.    
  79.     if(stage2)     
  80.     {
  81.         // stage2 fail save by bguerville / AV
  82.         // Disabling to avoid semibrick on NAND, will be enabled in stage2
  83.         cellFsUtilMount("CELL_FS_IOS:BUILTIN_FLSH1", "CELL_FS_FAT", "/dev_blind", 0, 0, 0, 0, 0);
  84.         cellFsRename(STAGE2_DEVBLIND, STAGE2_DEVBLIND ".bak");
  85.  
  86.         f.addr = stage2;
  87.     }
  88.     else   
  89.         f.addr = (void *)MKA(0x17e0);  
  90.        
  91.     func = (void *)&f; 
  92.     func();
  93. }
  94.  
Tags: Code
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement