Example #1
0
        private void enableWriteProtectionMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult _protectConfirmation = MessageBox.Show(
                "You won't be able to write to EEPROM again until the protection is cleared.\n\nAre you sure you want to enable write protection?\n\n",
                "Write protection",
                MessageBoxButtons.YesNo,
                MessageBoxIcon.Exclamation,
                MessageBoxDefaultButton.Button2);

            if (_protectConfirmation == DialogResult.No)
            {
                return;
            }

            for (int i = 0; i <= 3; i++)
            {
                if (Eeprom.SetWriteProtection(MySpdReader, i))
                {
                    Logger($"Write protection is set on block {i}");
                }
                else
                {
                    Logger($"Could not set write protection on block {i}");
                }
            }
        }
Example #2
0
        static void ParseCommand(string[] args)
        {
            string mode = args[0];

            if (mode == "/help")
            {
                ShowHelp();
                return;
            }

            try {
                // Setup
                SerialPortSettings readerSettings = new SerialPortSettings(
                    115200,
                    true,
                    true,
                    8,
                    Handshake.None,
                    "\n",
                    Parity.None,
                    StopBits.One,
                    true,
                    10);

                // Find
                if (mode == "/find")
                {
                    // Find
                    string[] devices = new Device(readerSettings).Find();
                    if (devices.Length > 0)
                    {
                        foreach (string portName in devices)
                        {
                            Console.WriteLine($"Found Device on Serial Port: {portName}\n");
                        }
                    }
                    else
                    {
                        throw new Exception("Nothing found");
                    }
                    return;
                }

                // Other functions that require additional parameters
                if (mode != "/find" && args.Length >= 2)
                {
                    // Init
                    string portName = args[1];

                    if (!portName.StartsWith("COM"))
                    {
                        throw new Exception("Port name should start with \"COM\" followed by a number.");
                    }

                    Device reader = new Device(readerSettings, portName);

                    if (!reader.Connect())
                    {
                        throw new Exception($"Could not connect to the device on port {portName}.");
                    }

                    if (reader.GetFirmwareVersion() < SpdReaderWriterDll.Settings.MINVERSION)
                    {
                        throw new Exception($"The device on port {portName} requires its firmware to be updated.");
                    }

                    //if (!reader.Test()) {
                    //	throw new Exception($"The device on port {portName} does not respond.");
                    //}

                    // Scan
                    if (mode == "/scan")
                    {
                        byte[] addresses = reader.Scan();

                        if (addresses.Length == 0)
                        {
                            throw new Exception("No EEPROM devices found.");
                        }

                        foreach (int location in addresses)
                        {
                            Console.WriteLine($"Found EEPROM at address: {location}");
                        }

                        reader.Disconnect();
                        return;
                    }

                    // Test reversible write protection capabilities
                    if (mode == "/enablewriteprotection" || mode == "/disablewriteprotection")
                    {
                        if (!reader.TestAdvancedFeatures())
                        {
                            throw new Exception("Your device does not support write protection features.");
                        }

                        // Turn on write protection
                        if (mode == "/enablewriteprotection")
                        {
                            int[] block;

                            if (args.Length == 3)   // Block # was specified
                            {
                                try {
                                    block = new[] { (Int32.Parse(args[2])) };
                                }
                                catch {
                                    throw new Exception("Block number should be specified in decimal notation.");
                                }
                            }
                            else   // No block number specified, protect all available
                            {
                                if (Spd.GetRamType(reader) == RamType.DDR4)
                                {
                                    block = new[] { 0, 1, 2, 3 };
                                }
                                else   // DDR3 + DDR2
                                {
                                    block = new[] { 0 };
                                }
                            }

                            reader.ResetAddressPins();

                            for (int i = 0; i < block.Length; i++)
                            {
                                if (Eeprom.SetWriteProtection(reader, i))
                                {
                                    Console.WriteLine($"Block {i} is now read-only");
                                }
                                else
                                {
                                    throw new Exception($"Unable to set write protection for block {i}. Either SA0 is not connected to HV, or the block is already read-only.");
                                }
                            }

                            return;
                        }

                        // Disable write protection
                        if (mode == "/disablewriteprotection")
                        {
                            reader.SetAddressPin(Pin.SA1, PinState.HIGH);

                            if (Eeprom.ClearWriteProtection(reader))
                            {
                                Console.WriteLine("Write protection successfully disabled.");
                            }
                            else
                            {
                                throw new Exception("Unable to clear write protection");
                            }

                            reader.ResetAddressPins();

                            return;
                        }
                    }

                    byte address;

                    try {
                        address = (byte)Int32.Parse(args[2]);
                    }
                    catch {
                        throw new Exception("EEPROM address should be specified in decimal notation.");
                    }

                    reader.I2CAddress = address;
                    reader.SpdSize    = Spd.GetSpdSize(reader);

                    if (!reader.ProbeAddress())
                    {
                        throw new Exception($"EEPROM is not present at address {reader.I2CAddress}.");
                    }

                    string filePath = (args.Length >= 4) ? args[3] : "";
                    bool   silent   = (args.Length >= 5 && args[4] == "/silent") ? true : false;

                    // Read SPD contents
                    if (mode == "/read")
                    {
                        Console.Write($"Reading EEPROM at address {reader.I2CAddress} ({Spd.GetRamType(reader)})");

                        if (filePath != "")
                        {
                            Console.WriteLine($" to {filePath}");
                        }
                        Console.WriteLine("\n");

                        int startTick = Environment.TickCount;

                        byte[] spdDump = Eeprom.ReadByte(reader, 0, (int)reader.SpdSize);

                        for (int i = 0; i < spdDump.Length; i++)
                        {
                            if (!silent)
                            {
                                ConsoleDisplayByte(i, spdDump[i]);
                            }
                        }

                        Console.Write("\n\nRead {0} {1} from EEPROM at address {2} on port {3} in {4} ms",
                                      spdDump.Length,
                                      (spdDump.Length > 1) ? "bytes" : "byte",
                                      reader.I2CAddress,
                                      reader.PortName,
                                      Environment.TickCount - startTick
                                      );

                        if (filePath != "")
                        {
                            try {
                                File.WriteAllBytes(filePath, spdDump);
                            }
                            catch {
                                throw new Exception($"Unable to write to {filePath}");
                            }
                            Console.Write($" to file \"{filePath}\"");
                        }

                        reader.Disconnect();
                        return;
                    }

                    // Write contents to EEPROM
                    if (mode.StartsWith("/write"))
                    {
                        if (filePath.Length < 1)
                        {
                            throw new Exception("File path is mandatory for write mode.");
                        }

                        if (!File.Exists(filePath))
                        {
                            throw new Exception($"File \"{filePath}\" not found.");
                        }

                        byte[] inputFile;
                        try {
                            inputFile = File.ReadAllBytes(filePath);
                        }
                        catch {
                            throw new Exception($"Unable to read {filePath}");
                        }

                        Console.WriteLine(
                            "Writing \"{0}\" ({1} {2}) to EEPROM at address {3}\n",
                            filePath,
                            inputFile.Length,
                            (inputFile.Length > 1) ? "bytes" : "byte",
                            reader.I2CAddress);

                        if (inputFile.Length > (int)reader.SpdSize)
                        {
                            throw new Exception($"File \"{filePath}\" is larger than {reader.SpdSize} bytes.");
                        }

                        int  bytesWritten = 0;
                        int  startTick    = Environment.TickCount;
                        byte b;

                        for (int i = 0; i != inputFile.Length; i++)
                        {
                            b = inputFile[i];
                            bool writeResult = (mode == "/writeforce")
                                ? Eeprom.WriteByte(reader, (ushort)i, inputFile[i])
                                : Eeprom.UpdateByte(reader, (ushort)i, inputFile[i]);

                            if (!writeResult)
                            {
                                throw new Exception($"Could not write byte {i} to EEPROM at address {reader.I2CAddress} on port {reader.PortName}.");
                            }

                            bytesWritten++;

                            if (!silent)
                            {
                                ConsoleDisplayByte(i, b);
                            }
                        }
                        reader.Disconnect();

                        Console.WriteLine(
                            "\n\nWritten {0} {1} to EEPROM at address {2} on port {3} in {4} ms",
                            bytesWritten,
                            (bytesWritten > 1) ? "bytes" : "byte",
                            reader.I2CAddress,
                            reader.PortName,
                            Environment.TickCount - startTick);
                        return;
                    }

                    if (mode == "/enablepermanentwriteprotection")
                    {
                        if (Eeprom.SetPermanentWriteProtection(reader))
                        {
                            Console.WriteLine($"Permanent write protection enabled on {reader.PortName}:{reader.I2CAddress}");
                        }
                        else
                        {
                            throw new Exception($"Unable to set permanent write protection on {reader.PortName}:{reader.I2CAddress}");
                        }
                    }
                }
            }
            catch (Exception e) {
                //Console.ForegroundColor = ConsoleColor.Red;
#if DEBUG
                Console.WriteLine($"{e}\n");
#else
                Console.WriteLine($"{e.Message}\n");
#endif

                //Console.ForegroundColor = ConsoleColor.Gray;
                return;
            }

            Console.WriteLine("Unknown command line parameters.\n");
            ShowHelp();
        }