Esempio n. 1
0
        private bool SaveTo(Stream stream)
        {
            if (!NandImage.FormatImage(stream))
            {
                MessageBox.Show("Couldn't format the image!",
                                "An error occured!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return(false);
            }

            if (!image.WriteImage(stream))
            {
                MessageBox.Show("Couldn't save the image!",
                                "An error occured!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return(false);
            }

            return(true);
        }
Esempio n. 2
0
        private void cbNew_Click(object sender, EventArgs e)
        {
            if (imageModified)
            {
                DialogResult result;
                if ((result = MessageBox.Show("Your last changes are not saved!\n" +
                                              "Do you want to save them now or cancel opening?",
                                              "Unsaved changes", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question))
                    == System.Windows.Forms.DialogResult.Cancel)
                {
                    return;
                }
                else if (result == System.Windows.Forms.DialogResult.Yes)
                {
                    cbSave.PerformClick();
                }
            }

            if (MessageBox.Show("Creating a completely new image is discouraged.\n" +
                                "Only proceed if you know what you are doing!\n" +
                                "Wrongfully created images will result in a bricked device!!!\n\n" +
                                "This process will require you to provide a working NANDLoader (aka. ROM).\n" +
                                "It is easier and recommended to start off an existing image.\n\n" +
                                "Do you really want to continue?", "Warning", MessageBoxButtons.YesNo,
                                MessageBoxIcon.Question) != DialogResult.Yes)
            {
                return;
            }

            OpenFileDialog ofd = new OpenFileDialog();

            ofd.FileName        = "NANDLoader_192MHz.bin";
            ofd.Multiselect     = false;
            ofd.Filter          = "NANDLoader ROM images (*.bin)|*.bin|All files (*.*)|*.*";
            ofd.FilterIndex     = 0;
            ofd.Title           = "Select NANDLoader ROM";
            ofd.CheckFileExists = true;

            if (ofd.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            try
            {
                if (!File.Exists(ofd.FileName))
                {
                    MessageBox.Show("File not found!", "An error occured!",
                                    MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    image  = null;
                    stream = null;
                    SetNoFileLoaded();
                    return;
                }

                FileStream romStream = File.OpenRead(ofd.FileName);
                romStream.Seek(0, SeekOrigin.End);

                if (romStream.Position > NandImage.BytesPerBlock / 2)
                {
                    MessageBox.Show("The supplied NANDLoader ROM is too large!",
                                    "An error occured!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    romStream.Close();
                    image  = null;
                    stream = null;
                    SetNoFileLoaded();
                    return;
                }

                uint   romSize = (uint)romStream.Position;
                string romName = "NANDLoader";
                if (Path.GetFileNameWithoutExtension(ofd.FileName).ToLower().Contains("nandloader"))
                {
                    romName = Path.GetFileNameWithoutExtension(ofd.FileName).Trim();
                }
                if (romName.Length > 30)
                {
                    romName = romName.Substring(0, 30);
                }
                romStream.Seek(0, SeekOrigin.Begin);
                image = new NandImage();
                ImageEntry nandLoader = new ImageEntry(romName, ImageType.System, 0,
                                                       new byte[romSize]);
                romStream.Read(nandLoader.Data, 0, (int)romSize);
                romStream.Close();

                if (!image.CheckAddEntry(nandLoader))
                {
                    MessageBox.Show("Error generating the image!",
                                    "An error occured!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    image  = null;
                    stream = null;
                    SetNoFileLoaded();
                    return;
                }

                // Add the image
                image.Images.Add(nandLoader);

                // Initalize the boot header
                image.Header = new BootHeader();
                image.Header.BootCodeMarker = 0x57425AA5;
                image.Header.ExecuteAddress = 0x900000;
                image.Header.ImageSize      = romSize + 0x20;
                image.Header.SkewMarker     = 0xA55A4257;
                image.Header.DQSODS         = 0x00001010;
                image.Header.CLKQSDS        = 0x00888800;
                image.Header.DramMarker     = 0;
                image.Header.DramSize       = 0;
            }
            catch (Exception ex)
            {
                ExceptionBox(ex);
                image  = null;
                stream = null;
                SetNoFileLoaded();
                return;
            }

            SaveFileDialog sfd = new SaveFileDialog();

            sfd.FileName        = "";
            sfd.Filter          = "NAND image files (*.img)|*.img|All files (*.*)|*.*";
            sfd.FilterIndex     = 0;
            sfd.Title           = "Save NAND image as";
            sfd.OverwritePrompt = true;

            if (sfd.ShowDialog() != DialogResult.OK)
            {
                image  = null;
                stream = null;
                SetNoFileLoaded();
                return;
            }

            try
            {
                fileName = sfd.FileName;
                stream   = File.Open(fileName, FileMode.Create);

                if (!NandImage.FormatImage(stream))
                {
                    MessageBox.Show("Couldn't format the new image!",
                                    "An error occured!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    stream.Close();
                    image  = null;
                    stream = null;
                    SetNoFileLoaded();
                    return;
                }

                if (!image.WriteImage(stream))
                {
                    MessageBox.Show("Couldn't save the new image!",
                                    "An error occured!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    stream.Close();
                    image  = null;
                    stream = null;
                    SetNoFileLoaded();
                    return;
                }
            }
            catch (Exception ex)
            {
                ExceptionBox(ex);
                image  = null;
                stream = null;
                SetNoFileLoaded();
                return;
            }

            imageModified = false;
            SetFileLoaded();
        }