Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow HEX Serial and SLID values. #3

Merged
merged 1 commit into from
Apr 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@
"args": [
"--host",
"192.168.200.2",
"--dry-run"
"--dry-run",
"--vendor",
"HWTC",
"--serial",
"41-4C-43-4C-90-12-34-5a",
"--slid",
"12345"
],
"cwd": "${workspaceFolder}/src/UFiber.Configurator",
"console": "internalConsole",
Expand Down
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,18 @@ Usage:
UFiber.Configurator [options]
Options:
--host <host> IP or hostname of the target UFiber device.
--host <host> IP or hostname of the target UFiber device. [default: 192.168.1.1]
--user <user> SSH user name. [default: ubnt]
--pw <pw> SSH password. [default: ubnt]
--port <port> SSH port of the target UFiber device. [default: 22]
--dry-run Don't apply the patched file to the target UFiber device. (i.e. dry-run)
--slid <slid> The SLID (or PLOAM Password).
--vendor <vendor> 4-digit Vendor Id (e.g. HWTC, MTSC, etc.). Combined with --serial, a GPON Serial Number is
built.
--serial <serial> 8-digit serial number (e.g. 01234567). Combined with --vendor, a GPON Serial Number is
built.
--serial <serial> 8-digit (e.g. 01234567) serial number or 16-digit (e.g. 41-4C-43-4C-xx-xx-xx-xx) HEX serial
number. Combined with --vendor, a GPON Serial Number is built. Note: If a 16-digit HEX
value is provided, the first 4 bytes (8 digits) will replace whatever value was passed to
Vendor Id with '--vendor'.
--mac <mac> The desired MAC address to clone.
--version Show version information
-?, -h, --help Show help and usage information
Expand Down
33 changes: 28 additions & 5 deletions src/UFiber.Configurator/NVRAM.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using static System.BitConverter;

namespace UFiber.Configurator
Expand All @@ -11,6 +12,8 @@ public class NVRAM
private const int NvRamCrcLength = 4;
private const uint NvRamOffset = 0x580;
private const uint NvRamLength = 0x400;
private static readonly Regex _snMatcher = new Regex(@"^(([a-fA-F0-9]){2}-){7}([a-fA-F0-9]){2}$");
private static readonly Regex _pwMatcher = new Regex(@"^(([a-fA-F0-9]){2}-){9}([a-fA-F0-9]){2}$");
public uint Checksum { get; private set; }
public byte[] AfeId { get; }
public byte[] VoiceBoardId { get; }
Expand Down Expand Up @@ -118,7 +121,16 @@ public void SetGponId(ReadOnlySpan<byte> id)

public void SetGponSerialNumber(string serialNumber)
{
SetGponSerialNumber(Encoding.UTF8.GetBytes(serialNumber));
if (_snMatcher.IsMatch(serialNumber))
{
this.SetGponId(AsBytes(serialNumber.Replace("-", "")[0..8]));
SetGponSerialNumber(Encoding.UTF8.GetBytes(serialNumber.Replace("-", "")[8..]));
}
else
{
if (serialNumber.Contains("-")) throw new InvalidOperationException($"Invalid serial number: {serialNumber}.");
SetGponSerialNumber(Encoding.UTF8.GetBytes(serialNumber));
}
}

public void SetGponSerialNumber(ReadOnlySpan<byte> serialNumber)
Expand Down Expand Up @@ -149,7 +161,18 @@ public void SetGponPassword(string password)
throw new ArgumentOutOfRangeException(nameof(password));
}

var bits = Encoding.UTF8.GetBytes(password);
byte[] bits = default!;

if (_pwMatcher.IsMatch(password))
{
password = password.Replace("-", "").PadLeft(MaxPasswordLength, '0');
bits = AsBytes(password);
}
else
{
password = password.PadLeft(MaxPasswordLength, '0');
bits = Encoding.UTF8.GetBytes(password);
}

SetBytes(bits.AsSpan(), PasswordOffset, password.Length);

Expand Down Expand Up @@ -245,14 +268,14 @@ private static byte[] AsBytes(string data)

while (dataIndex < data.Length)
{
var highNibble = ToHex(data[dataIndex++]);
var lowNibble = ToHex(data[dataIndex++]);
var highNibble = FromHex(data[dataIndex++]);
var lowNibble = FromHex(data[dataIndex++]);
bits[bitsIndex++] = (byte)(highNibble << 4 | lowNibble);
}

return bits;

static int ToHex(char c) =>
static int FromHex(char c) =>
c switch
{
>= 'a' and <= 'f' => 10 + (c - 'a'),
Expand Down
2 changes: 1 addition & 1 deletion src/UFiber.Configurator/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"4-digit Vendor Id (e.g. HWTC, MTSC, etc.). Combined with --serial, a GPON Serial Number is built.", ArgumentArity.ZeroOrOne),
new Option<string>(
"--serial",
"8-digit serial number (e.g. 01234567). Combined with --vendor, a GPON Serial Number is built.", ArgumentArity.ZeroOrOne),
"8-digit (e.g. 01234567) serial number or 16-digit (e.g. 41-4C-43-4C-xx-xx-xx-xx) HEX serial number. Combined with --vendor, a GPON Serial Number is built. Note: If a 16-digit HEX value is provided, the first 4 bytes (8 digits) will replace whatever value was passed to Vendor Id with '--vendor'.", ArgumentArity.ZeroOrOne),
new Option<string>(
"--mac",
"The desired MAC address to clone.", ArgumentArity.ZeroOrOne),
Expand Down