private void cbImageAdd_Click(object sender, EventArgs e) { if (lvImages.SelectedItems.Count != 0) { return; } try { OpenFileDialog ofd = new OpenFileDialog(); ofd.FileName = ""; ofd.Multiselect = false; ofd.Filter = "Raw data files (*.bin)|*.bin|All files (*.*)|*.*"; ofd.FilterIndex = 0; ofd.Title = "Import data segment"; ofd.CheckFileExists = true; if (ofd.ShowDialog() != DialogResult.OK) { return; } if (!File.Exists(ofd.FileName)) { MessageBox.Show("File not found!", "An error occured!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } FileStream imgStream = File.OpenRead(ofd.FileName); imgStream.Seek(0, SeekOrigin.End); if (imgStream.Position > (NandImage.NandSize - image.CalculateUsage())) { MessageBox.Show("The supplied data segment is too large!", "An error occured!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); imgStream.Close(); return; } uint imgSize = (uint)imgStream.Position; string imgName = Path.GetFileNameWithoutExtension(ofd.FileName).Trim(); if (imgName.Length > 30) { imgName = imgName.Substring(0, 30); } imgStream.Seek(0, SeekOrigin.Begin); byte[] imgData = new byte[imgSize]; imgStream.Read(imgData, 0, (int)imgSize); imgStream.Close(); formSegment fseg = new formSegment(new ImageHeader() { ImageName = imgName, ImageType = libw5nand.ImageType.Execute }, false); if (fseg.ShowDialog() != DialogResult.OK) { return; } ImageEntry newImg = new ImageEntry(fseg.Header.ImageName, fseg.Header.ImageType, fseg.Header.ExecuteAddress, imgData); if (!image.CheckAddEntry(newImg)) { MessageBox.Show("The configured data segment is invalid!", "An error occured!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } image.Images.Add(newImg); imageModified = true; RefreshImagesList(); } catch (Exception ex) { ExceptionBox(ex); } }
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(); }