コード例 #1
0
        protected override void _backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            string error;

            if (!string.IsNullOrEmpty(_logoFileName))
            {
                ReportStatus("Checking logo file... ");

                if (!FontCoder.CheckFile(_logoFileName, FontCoder.FontWidthKedei, FontCoder.FontHeightKedei, out error))
                {
                    ReportStatus($"Error! {error}\r\n");
                    e.Result = RTD266x.Result.NotOk;
                    return;
                }

                ReportStatus("ok\r\n");
            }

            if (!string.IsNullOrEmpty(_replaceHdmi))
            {
                ReportStatus("Checking \"HDMI\" replacement... ");

                if (!FirmwareModifier.CheckHdmiReplacement(_replaceHdmi, out error))
                {
                    ReportStatus($"{error}\r\n");
                    e.Result = RTD266x.Result.NotOk;
                    return;
                }

                ReportStatus("ok\r\n");
            }

            ReportStatus("Identifying device... ");

            RTD266x.Result     result;
            RTD266x.StatusInfo status;

            result = _rtd.ReadStatus(out status);

            if (result != RTD266x.Result.Ok || (status.ManufacturerId != 0xC8 && status.ManufacturerId != 0x1C && status.ManufacturerId != 0xC2 && status.ManufacturerId != 0x85) || status.DeviceId != 0x12)
            {
                ReportStatus("Error! Cannot identify chip.\r\n");
                e.Result = result;
                return;
            }

            ReportStatus("ok\r\n");
            ReportStatus("Reading firmware...\r\n");

            byte[] firmware;

            result = Read(0, 512 * 1024, out firmware, true);

            if (result != RTD266x.Result.Ok)
            {
                ReportStatus(RTD266x.ResultToString(result) + "\r\n");
                e.Result = result;
                return;
            }

            string backupFirmwareFileName = "firmware-" + DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + ".bin";

            ReportStatus($"Creating firmware backup file \"{backupFirmwareFileName}\"... ");

            try
            {
                File.WriteAllBytes(backupFirmwareFileName, firmware);
            }
            catch (Exception ex)
            {
                ReportStatus($"Error! Could not save file \"{backupFirmwareFileName}\". {ex.Message}\r\n");
                e.Result = result;
                return;
            }

            ReportStatus("ok\r\n");
            ReportStatus("Checking firmware... ");

            Firmware detectedFirmware = FirmwareModifier.DetectFirmware(firmware);

            if (detectedFirmware == null)
            {
                ReportStatus("Error! Unknown firmware. You can send your firmware and the name of the display (printed on the cable on the back side) to the author ([email protected]), maybe it can be added to the known firmwares.\r\n");
                e.Result = result;
                return;
            }

            ReportStatus("ok\r\n");
            ReportStatus($"Detected firmware is {detectedFirmware.Name}\r\n");

            if (!string.IsNullOrEmpty(_logoFileName))
            {
                ReportStatus("Converting and embedding the new logo... ");

                if (!FirmwareModifier.ChangeLogo(_logoFileName, firmware, detectedFirmware, out error))
                {
                    ReportStatus($"{error}\r\n");
                    e.Result = result;
                    return;
                }

                ReportStatus("ok\r\n");

                result = WritePatchedSector(firmware, detectedFirmware.LogoOffset);

                if (result != RTD266x.Result.Ok)
                {
                    e.Result = result;
                    return;
                }
            }

            if (_logoBackground != Color.Empty)
            {
                ReportStatus("Patching logo background color... ");

                FirmwareModifier.ChangeLogoBackgroundColor(_logoBackground, firmware, detectedFirmware);

                ReportStatus("ok\r\n");
            }

            if (_logoForeground != Color.Empty)
            {
                ReportStatus("Patching logo foreground color... ");

                FirmwareModifier.ChangeLogoForegroundColor(_logoForeground, firmware, detectedFirmware);

                ReportStatus("ok\r\n");
            }

            if ((_logoBackground != Color.Empty) || (_logoForeground != Color.Empty))
            {
                result = WritePatchedSector(firmware, detectedFirmware.PaletteOffset);

                if (result != RTD266x.Result.Ok)
                {
                    e.Result = result;
                    return;
                }
            }

            if (_displayBackground != Color.Empty)
            {
                ReportStatus("Patching display background color... ");

                FirmwareModifier.ChangeBackgroundColor(_displayBackground, firmware, detectedFirmware);

                ReportStatus("ok\r\n");

                result = WritePatchedSector(firmware, detectedFirmware.AdjustBackgroundColorOffset);

                if (result != RTD266x.Result.Ok)
                {
                    e.Result = result;
                    return;
                }
            }

            if (_removeHdmi)
            {
                ReportStatus("Removing \"HDMI\" pop-up... ");

                FirmwareModifier.ToggleHdmiPopup(false, firmware, detectedFirmware);

                ReportStatus("ok\r\n");
            }
            else
            {
                ReportStatus("Enabling \"HDMI\" pop-up... ");

                FirmwareModifier.ToggleHdmiPopup(true, firmware, detectedFirmware);

                ReportStatus("ok\r\n");
            }

            result = WritePatchedSector(firmware, detectedFirmware.ShowNoteOffset);

            if (result != RTD266x.Result.Ok)
            {
                e.Result = result;
                return;
            }

            if (_removeNoSignal)
            {
                ReportStatus("Removing \"No Signal\" pop-up...");

                FirmwareModifier.ToggleNoSignalPopup(false, firmware, detectedFirmware);

                ReportStatus("ok\r\n");
            }
            else
            {
                ReportStatus("Enabling \"No Signal\" pop-up...");

                FirmwareModifier.ToggleNoSignalPopup(true, firmware, detectedFirmware);

                ReportStatus("ok\r\n");
            }

            result = WritePatchedSector(firmware, detectedFirmware.NoSignalOffset);

            if (result != RTD266x.Result.Ok)
            {
                e.Result = result;
                return;
            }

            if (!string.IsNullOrEmpty(_replaceHdmi))
            {
                ReportStatus($"Replacing \"HDMI\" pop-up with \"{_replaceHdmi}\"... ");

                FirmwareModifier.ReplaceHdmiPopup(_replaceHdmi, firmware, detectedFirmware);

                ReportStatus("ok\r\n");

                result = WritePatchedSector(firmware, detectedFirmware.HdmiStringOffset);

                if (result != RTD266x.Result.Ok)
                {
                    e.Result = result;
                    return;
                }
            }

            ReportStatus("Finished! Now reboot the display and enjoy your new firmware :)\r\n");

            e.Result = RTD266x.Result.Ok;
        }
コード例 #2
0
        private void modificationSettings_ModifyClickedEvent()
        {
            string error;
            string inputFileName = txtInputFileName.Text;

            if (string.IsNullOrEmpty(inputFileName))
            {
                ShowErrorMessage("No input file specified!");
                return;
            }

            if (!File.Exists(inputFileName))
            {
                ShowErrorMessage($"The file {inputFileName} does not exist!");
                return;
            }

            if (!string.IsNullOrEmpty(modificationSettings.LogoFileName))
            {
                if (!FontCoder.CheckFile(modificationSettings.LogoFileName, FontCoder.FontWidthKedei, FontCoder.FontHeightKedei, out error))
                {
                    ShowErrorMessage(error);
                    return;
                }
            }

            if (!string.IsNullOrEmpty(modificationSettings.HdmiReplacementText))
            {
                if (!FirmwareModifier.CheckHdmiReplacement(modificationSettings.HdmiReplacementText, out error))
                {
                    ShowErrorMessage($"Invalid HDMI replacement text: {error}");
                    return;
                }
            }

            byte[] firmware;

            try
            {
                firmware = File.ReadAllBytes(inputFileName);
            }
            catch (Exception ex)
            {
                ShowErrorMessage($"Cannot load firmware from file {inputFileName}! {ex.Message}");
                return;
            }

            Firmware detectedFirmware = FirmwareModifier.DetectFirmware(firmware);

            if (detectedFirmware == null)
            {
                ShowErrorMessage("Error! Unknown firmware. You can send your firmware and the name of the display (printed on the cable on the back side) to the author ([email protected]), maybe it can be added to the known firmwares.");
                return;
            }

            if (!string.IsNullOrEmpty(modificationSettings.LogoFileName))
            {
                if (!FirmwareModifier.ChangeLogo(modificationSettings.LogoFileName, firmware, detectedFirmware, out error))
                {
                    ShowErrorMessage(error);
                    return;
                }
            }

            if (modificationSettings.LogoBackgroundColor != Color.Empty)
            {
                FirmwareModifier.ChangeLogoBackgroundColor(modificationSettings.LogoBackgroundColor, firmware, detectedFirmware);
            }

            if (modificationSettings.LogoForegroundColor != Color.Empty)
            {
                FirmwareModifier.ChangeLogoForegroundColor(modificationSettings.LogoForegroundColor, firmware, detectedFirmware);
            }

            if (modificationSettings.BackgroundColor != Color.Empty)
            {
                FirmwareModifier.ChangeBackgroundColor(modificationSettings.BackgroundColor, firmware, detectedFirmware);
            }

            FirmwareModifier.ToggleHdmiPopup(!modificationSettings.RemoveHdmiPopup, firmware, detectedFirmware);

            if (!string.IsNullOrEmpty(modificationSettings.HdmiReplacementText))
            {
                FirmwareModifier.ReplaceHdmiPopup(modificationSettings.HdmiReplacementText, firmware, detectedFirmware);
            }

            FirmwareModifier.ToggleNoSignalPopup(!modificationSettings.RemoveNoSignalPopup, firmware, detectedFirmware);

            string outputFileName = Path.GetFileNameWithoutExtension(inputFileName) + "_modified.bin";

            SaveFileDialog saveFileDialog = new SaveFileDialog();

            saveFileDialog.Filter   = "Bin files (*.bin)|*.bin";
            saveFileDialog.FileName = outputFileName;

            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    File.WriteAllBytes(saveFileDialog.FileName, firmware);
                }
                catch (Exception ex)
                {
                    ShowErrorMessage($"Could not write file {saveFileDialog.FileName}! {ex.Message}");
                    return;
                }
            }

            MessageBox.Show($"The modified firmware was successfully saved to {saveFileDialog.FileName}.\r\n\r\nYou can now flash it to your RTD266x chip.", "Modify firmware", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }