Skip to content

Commit

Permalink
Merge pull request #3 from Unifi-Tools/allow-hex-serial
Browse files Browse the repository at this point in the history
Allow HEX Serial and SLID values.
  • Loading branch information
galvesribeiro authored Apr 26, 2021
2 parents 823ccd0 + 25d6255 commit 92d9754
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 10 deletions.
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

0 comments on commit 92d9754

Please sign in to comment.