protected override void _backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            if (!string.IsNullOrEmpty(_logoFileName))
            {
                ReportStatus("Checking logo file... ");

                string error;

                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 (_replaceHdmi.Length > 8)
                {
                    ReportStatus("Error! String is too long!\r\n");
                    e.Result = RTD266x.Result.NotOk;
                    return;
                }

                foreach (char chr in _replaceHdmi)
                {
                    if (!_osdCharacters.ContainsKey(chr))
                    {
                        ReportStatus($"Error! Invalid character \"{chr}\"!\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.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 = null;

            foreach (Firmware fw in _firmwares)
            {
                if (fw.CheckHash(firmware))
                {
                    detectedFirmware = fw;
                    break;
                }
            }

            if (detectedFirmware == null)
            {
                ReportStatus("Error! Could not detect firmware.\r\n");
                e.Result = result;
                return;
            }

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

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

                FontCoder logo = new FontCoder(FontCoder.FontWidthKedei, FontCoder.FontHeightKedei);

                if (!logo.LoadImage(_logoFileName))
                {
                    ReportStatus($"Error! Cannot load logo from \"{_logoFileName}\".\r\n");
                    e.Result = result;
                    return;
                }

                byte[] logoBytes = logo.Encode();

                if (logoBytes.Length > detectedFirmware.MaxLogoLength)
                {
                    ReportStatus("Error! Encoded logo is too long and would overwrite other firmware parts.\r\n");
                    e.Result = result;
                    return;
                }

                ReportStatus("ok\r\n");
                ReportStatus("Embedding the new logo... ");

                Array.Copy(logoBytes, 0, firmware, detectedFirmware.LogoOffset, logoBytes.Length);

                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... ");

                firmware[detectedFirmware.PaletteOffset + 42] = _logoBackground.R;
                firmware[detectedFirmware.PaletteOffset + 43] = _logoBackground.G;
                firmware[detectedFirmware.PaletteOffset + 44] = _logoBackground.B;

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

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

                firmware[detectedFirmware.PaletteOffset + 12] = _logoForeground.R;
                firmware[detectedFirmware.PaletteOffset + 13] = _logoForeground.G;
                firmware[detectedFirmware.PaletteOffset + 14] = _logoForeground.B;

                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... ");

                firmware[detectedFirmware.AdjustBackgroundColorOffset + 0x1D] = 0x7D; // MOV R5
                firmware[detectedFirmware.AdjustBackgroundColorOffset + 0x1E] = _displayBackground.R;
                firmware[detectedFirmware.AdjustBackgroundColorOffset + 0x1F] = 0x00; // NOP

                firmware[detectedFirmware.AdjustBackgroundColorOffset + 0x23] = 0x7D; // MOV R5
                firmware[detectedFirmware.AdjustBackgroundColorOffset + 0x24] = _displayBackground.G;
                firmware[detectedFirmware.AdjustBackgroundColorOffset + 0x25] = 0x00; // NOP

                firmware[detectedFirmware.AdjustBackgroundColorOffset + 0x29] = 0x7D; // MOV R5
                firmware[detectedFirmware.AdjustBackgroundColorOffset + 0x2A] = _displayBackground.B;
                firmware[detectedFirmware.AdjustBackgroundColorOffset + 0x2B] = 0x00; // NOP
                firmware[detectedFirmware.AdjustBackgroundColorOffset + 0x2C] = 0x00; // NOP
                firmware[detectedFirmware.AdjustBackgroundColorOffset + 0x2D] = 0x00; // NOP

                firmware[detectedFirmware.AdjustBackgroundColorOffset + 0x3C] = 0x00; // NOP
                firmware[detectedFirmware.AdjustBackgroundColorOffset + 0x3D] = 0x00; // NOP

                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... ");

                firmware[detectedFirmware.ShowNoteOffset] = 0x22; // RET

                ReportStatus("ok\r\n");

                result = WritePatchedSector(firmware, detectedFirmware.ShowNoteOffset);

                if (result != RTD266x.Result.Ok)
                {
                    e.Result = result;
                    return;
                }
            }
            else
            {
                ReportStatus("Enabling \"HDMI\" pop-up... ");

                firmware[detectedFirmware.ShowNoteOffset] = 0xE4; // CLR A

                ReportStatus("ok\r\n");

                result = WritePatchedSector(firmware, detectedFirmware.ShowNoteOffset);

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

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

                int offset = 0;

                foreach (char chr in _replaceHdmi)
                {
                    byte[] bytes = _osdCharacters[chr];

                    foreach (byte b in bytes)
                    {
                        firmware[detectedFirmware.HdmiStringOffset + offset] = b;
                        offset++;
                    }
                }

                firmware[detectedFirmware.HdmiStringOffset + offset] = 0x00;

                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;
        }
        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;
        }
Exemple #3
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);
        }