Пример #1
0
        public static string GenerateKeyStr(string Aid)
        {
            try
            {
                Int64 longlong = Convert.ToInt64(Aid, 16);

                byte[] AidBytes = BitConverter.GetBytes(longlong);
                Array.Reverse(AidBytes);

                byte[] DerivedKey = CmaKeys.GenerateKey(AidBytes);


                return(BitConverter.ToString(DerivedKey).Replace("-", ""));
            }
            catch (Exception)
            {
                return("INVALID_AID");
            }
        }
        private void Ccs_FormClosing(object sender, FormClosingEventArgs e)
        {
            CHOVYCmaSelector ccs    = (CHOVYCmaSelector)sender;
            string           CmaDir = ccs.GetCmaDir();
            string           CmaAid = ccs.GetCmaAid();
            string           Backup = ccs.GetSelectedBackup();

            WriteSetting("CmaDir", CmaDir);

            if (Backup == "")
            {
                return;
            }

            string BackupPath = Path.Combine(CmaDir, "PGAME", CmaAid, Backup, "game", "game.psvimg");

            if (!File.Exists(BackupPath))
            {
                MessageBox.Show("Could not find \n" + BackupPath + "\n Perhaps backup failed?", "License Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                ccs.Hide();
                this.Show();
                this.Focus();
                return;
            }

            byte[] AID = BitConverter.GetBytes(Convert.ToInt64(CmaAid, 16));
            Array.Reverse(AID);
            byte[] Key = CmaKeys.GenerateKey(AID);

            PSVIMGStream GamePsvimg = new PSVIMGStream(File.OpenRead(BackupPath), Key);

            BackupPath = Path.Combine(CmaDir, "PGAME", CmaAid, Backup, "license", "license.psvimg");
            if (!File.Exists(BackupPath))
            {
                MessageBox.Show("Could not find \n" + BackupPath + "\n Perhaps backup failed?", "License Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                ccs.Hide();
                this.Show();
                this.Focus();
                return;
            }

            PSVIMGStream LicensePsvimg = new PSVIMGStream(File.OpenRead(BackupPath), Key);

            PSVIMGFileStream EbootPbp = new PSVIMGFileStream(GamePsvimg, "/EBOOT.PBP");

            byte[] VersionKey = Pbp.GetVersionKey(EbootPbp);
            string VerKey     = BitConverter.ToString(VersionKey).Replace("-", "");

            WriteSetting("VersionKey", VerKey);

            string           ContentID  = Pbp.GetContentId(EbootPbp);
            PSVIMGFileStream LicenseRif = new PSVIMGFileStream(LicensePsvimg, "/" + ContentID + ".rif");

            byte[] LicenseRifBytes = new byte[LicenseRif.Length];
            LicenseRif.Read(LicenseRifBytes, 0x00, LicenseRifBytes.Length);

            LicenseRif.Close();
            LicensePsvimg.Close();
            EbootPbp.Close();
            GamePsvimg.Close();

            byte[] zRifBytes = ZlibStream.CompressBuffer(LicenseRifBytes);
            string Rif       = Convert.ToBase64String(zRifBytes);

            WriteSetting("RifPath", Rif);

            Versionkey.Text = VerKey;
            RifPath.Text    = Rif;

            ccs.Hide();
            this.Show();
            this.Focus();

            MessageBox.Show("KEYS HAVE BEEN EXTRACTED FROM CMA, YOU MAY NOW LIBERATE YOURSELF", "SUCCESS", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        private void FREEDOM_Click(object sender, EventArgs e)
        {
            Action enable = () => {
                this.FREEDOM.Enabled      = true;
                this.Ps1Menu.Enabled      = true;
                this.FindFromCMA.Enabled  = true;
                this.RifPath.ReadOnly     = false;
                this.Versionkey.ReadOnly  = false;
                this.ISOPath.ReadOnly     = false;
                this.CompressPBP.Enabled  = true;
                this.BrowseButton.Enabled = true;
            };

            Action disable = () => {
                this.FREEDOM.Enabled      = false;
                this.Ps1Menu.Enabled      = false;
                this.FindFromCMA.Enabled  = false;
                this.RifPath.ReadOnly     = true;
                this.Versionkey.ReadOnly  = true;
                this.ISOPath.ReadOnly     = true;
                this.CompressPBP.Enabled  = false;
                this.BrowseButton.Enabled = false;
            };

            disable();

            bool isZrif = false;

            if (RifPath.Text == "" || !File.Exists(RifPath.Text))
            {
                // Check if valid 'zRIF'
                try
                {
                    ZlibStream.UncompressBuffer(Convert.FromBase64String(RifPath.Text));
                    isZrif = true;
                }
                catch (Exception)
                {
                    MessageBox.Show("INVALID RIF!\nPlease try \"Find from CMA\"", "RIF ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    enable();
                    return;
                }
            }
            if (Versionkey.Text.Length != 32)
            {
                MessageBox.Show("INVALID VERSION KEY!\nPlease try \"Find from CMA\"", "VERKEY ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                enable();
                return;
            }
            if (ISOPath.Text == "" || !File.Exists(ISOPath.Text))
            {
                MessageBox.Show("INVALID ISO PATH!", "ISO ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                enable();
                return;
            }

            this.FREEDOM.Enabled = false;
            string CmaDir = ReadSetting("CmaDir");

            if (CmaDir == "")
            {
                FolderBrowserDialog fbd = new FolderBrowserDialog();
                fbd.Description = "Select CMA Backups Directory";
                fbd.ShowDialog();
                CmaDir = fbd.SelectedPath;
            }
            string TitleID = GetTitleID(ISOPath.Text);

            string TmpDir         = Path.Combine(Application.StartupPath, "_tmp");
            string GameWorkDir    = Path.Combine(TmpDir, TitleID);
            string EbootFile      = Path.Combine(GameWorkDir, "EBOOT.PBP");
            string EbootSignature = Path.Combine(GameWorkDir, "__sce_ebootpbp");

            Directory.CreateDirectory(TmpDir);
            Directory.CreateDirectory(GameWorkDir);


            //Read RIF data
            byte[] ContentId = new byte[0x24];
            byte[] RifAid    = new byte[0x08];
            Stream rif       = null;

            if (!isZrif)
            {
                rif = File.OpenRead(RifPath.Text);
            }
            else
            {
                rif = new MemoryStream(ZlibStream.UncompressBuffer(Convert.FromBase64String(RifPath.Text)));
            }

            rif.Seek(0x10, SeekOrigin.Begin);
            rif.Read(ContentId, 0x00, 0x24);
            rif.Seek(0x08, SeekOrigin.Begin);
            rif.Read(RifAid, 0x00, 0x08);


            string ContentID = Encoding.UTF8.GetString(ContentId);
            string AidStr    = BitConverter.ToString(RifAid).Replace("-", "").ToLower();

            if (IsDexAidSet())
            {
                AidStr = "0000000000000000";
            }
            string BackupWorkDir = Path.Combine(CmaDir, "PGAME", AidStr, TitleID);

            TotalProgress.Maximum = 100;
            Status.Text           = "Overthrowing The PSPEMU Monarchy 0%";



            Thread BuildPbpThread = new Thread(() =>
            {
                // Try New System
                try
                {
                    FileStream EbootStream = File.OpenWrite(EbootFile);
                    FileStream IsoStream   = File.OpenRead(ISOPath.Text);
                    Bitmap BootupImage;
                    if (isMini(ISOPath.Text))
                    {
                        BootupImage = Resources.MINIS;
                    }
                    else
                    {
                        BootupImage = Resources.ChovyLogo;
                    }

                    byte[] Sfo      = ReadFileFromISO(ISOPath.Text, @"PSP_GAME\PARAM.SFO");
                    byte[] Icon0Png = ReadFileFromISO(ISOPath.Text, @"PSP_GAME\ICON0.PNG");
                    byte[] Icon1    = ReadFileFromISO(ISOPath.Text, @"PSP_GAME\ICON1.PMF");
                    byte[] Pic0     = ReadFileFromISO(ISOPath.Text, @"PSP_GAME\PIC0.PNG");
                    byte[] Pic1     = ReadFileFromISO(ISOPath.Text, @"PSP_GAME\PIC1.PNG");
                    byte[] Snd0     = ReadFileFromISO(ISOPath.Text, @"PSP_GAME\SND0.AT3");

                    Pbp.BuildPbp(EbootStream, IsoStream, CompressPBP.Checked, FromHex(Versionkey.Text), BootupImage, ContentID, Sfo, Icon0Png, Icon1, Pic0, Pic1, Snd0);
                    IsoStream.Close();
                    EbootStream.Close();
                }
                catch (Exception exp)
                {
                    MessageBox.Show(exp.Message, "Error.", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            });

            BuildPbpThread.Start();
            while (BuildPbpThread.IsAlive)
            {
                if (Pbp.DoEvents)
                {
                    TotalProgress.Maximum = Pbp.NumberOfSectors;
                    TotalProgress.Value   = Pbp.SectorsDone;
                    if (Pbp.SectorsDone != 0 && Pbp.NumberOfSectors != 0)
                    {
                        decimal progress = Math.Floor(((decimal)Pbp.SectorsDone / (decimal)Pbp.NumberOfSectors) * 100);
                        Status.Text = "Overthrowing The PSPEMU Monarchy " + progress.ToString() + "%";
                    }
                    else
                    {
                        Status.Text = "Overthrowing The PSPEMU Monarchy 0%";
                    }
                    Application.DoEvents();
                }
            }


            TotalProgress.Value = 0;

            Status.Text = "Signing the Declaration of Independance 0%";
            UInt64 IntAid         = BitConverter.ToUInt64(RifAid, 0x00);
            Thread ChovyGenThread = new Thread(() =>
            {
                int ChovyGenRes = Pbp.gen__sce_ebootpbp(EbootFile, IntAid, EbootSignature);
            });

            ChovyGenThread.Start();
            while (ChovyGenThread.IsAlive)
            {
                Application.DoEvents();
            }

            /*
             *  BUILD PSVIMG FILE
             */

            // Pacakge GAME
            byte[] CmaKey;
            if (IsDexAidSet())
            {
                CmaKey = CmaKeys.GenerateKey(new byte[0x8] {
                    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
                });
            }
            else
            {
                CmaKey = CmaKeys.GenerateKey(RifAid);
            }



            string[] entrys     = Directory.GetFileSystemEntries(GameWorkDir, "*", SearchOption.AllDirectories);
            long     noEntrys   = entrys.LongLength;
            string   parentPath = "ux0:pspemu/temp/game/PSP/GAME/" + TitleID;
            int      noBlocks   = 0;

            foreach (string fileName in Directory.GetFiles(GameWorkDir, "*", SearchOption.AllDirectories))
            {
                noBlocks += Convert.ToInt32(new FileInfo(fileName).Length / PSVIMGConstants.PSVIMG_BLOCK_SIZE);
            }
            TotalProgress.Maximum = noBlocks;


            string BackupDir = Path.Combine(BackupWorkDir, "game");

            Directory.CreateDirectory(BackupDir);
            string     psvimgFilepath = Path.Combine(BackupDir, "game.psvimg");
            FileStream gamePsvimg     = File.OpenWrite(psvimgFilepath);

            gamePsvimg.SetLength(0);
            PSVIMGBuilder builder = new PSVIMGBuilder(gamePsvimg, CmaKey);

            foreach (string entry in entrys)
            {
                string relativePath = entry.Remove(0, GameWorkDir.Length);
                relativePath = relativePath.Replace('\\', '/');

                bool isDir = File.GetAttributes(entry).HasFlag(FileAttributes.Directory);

                if (isDir)
                {
                    builder.AddDir(entry, parentPath, relativePath);
                }
                else
                {
                    builder.AddFileAsync(entry, parentPath, relativePath);
                    while (!builder.HasFinished)
                    {
                        try
                        {
                            int tBlocks = builder.BlocksWritten;
                            TotalProgress.Value = tBlocks;
                            decimal progress = Math.Floor(((decimal)tBlocks / (decimal)noBlocks) * 100);
                            Status.Text = "Signing the Declaration of Independance " + progress.ToString() + "%";
                        }
                        catch (Exception) { }

                        Application.DoEvents();
                    }
                }
            }

            long ContentSize = builder.Finish();

            gamePsvimg = File.OpenRead(psvimgFilepath);
            FileStream gamePsvmd = File.OpenWrite(Path.Combine(BackupDir, "game.psvmd"));

            PSVMDBuilder.CreatePsvmd(gamePsvmd, gamePsvimg, ContentSize, "game", CmaKey);
            gamePsvmd.Close();
            gamePsvimg.Close();

            // Package LICENSE

            BackupDir      = Path.Combine(BackupWorkDir, "license");
            psvimgFilepath = Path.Combine(BackupDir, "license.psvimg");

            Directory.CreateDirectory(BackupDir);
            FileStream licensePsvimg = File.OpenWrite(psvimgFilepath);

            licensePsvimg.SetLength(0);

            builder = new PSVIMGBuilder(licensePsvimg, CmaKey);
            if (!isZrif)
            {
                builder.AddFile(RifPath.Text, "ux0:pspemu/temp/game/PSP/LICENSE", "/" + ContentID + ".rif");
            }
            else
            {
                rif.Seek(0x00, SeekOrigin.Begin);
                builder.AddFileFromStream(rif, "ux0:pspemu/temp/game/PSP/LICENSE", "/" + ContentID + ".rif");
            }

            rif.Close();
            ContentSize = builder.Finish();

            licensePsvimg = File.OpenRead(psvimgFilepath);
            FileStream licensePsvmd = File.OpenWrite(Path.Combine(BackupDir, "license.psvmd"));

            PSVMDBuilder.CreatePsvmd(licensePsvmd, licensePsvimg, ContentSize, "license", CmaKey);
            licensePsvmd.Close();
            licensePsvimg.Close();

            // Write PARAM.SFO & ICON0.PNG
            string SceSysWorkDir = Path.Combine(BackupWorkDir, "sce_sys");

            Directory.CreateDirectory(SceSysWorkDir);

            byte[] ParamSfo = ReadFileFromISO(ISOPath.Text, @"PSP_GAME\PARAM.SFO");
            byte[] Icon0    = ReadFileFromISO(ISOPath.Text, @"PSP_GAME\ICON0.PNG");
            File.WriteAllBytes(Path.Combine(SceSysWorkDir, "param.sfo"), ParamSfo);
            File.WriteAllBytes(Path.Combine(SceSysWorkDir, "icon0.png"), Icon0);

            Status.Text         = "YOU HAVE MADE A SOCIAL CONTRACT WITH FREEDOM!";
            TotalProgress.Value = 0;

            try
            {
                Directory.Delete(TmpDir, true);
            }
            catch (Exception) { };

            if (!MutedAudio)
            {
                Stream      str = Resources.Murica;
                SoundPlayer snd = new SoundPlayer(str);
                snd.Play();
            }

            enable();
            MessageBox.Show("YOU HAVE MADE A SOCIAL CONTRACT WITH FREEDOM!", "FREEDOM!", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }