Fix overlay creation logic to use reg value for node names

This commit fixes device tree overlay creation logic and templates
to use reg values for node names.

Change-Id: I0945ad204c74a52a96ac283884a699e125b703fe
Signed-off-by: Jae Hyun Yoo <jae.hyun.yoo@intel.com>
diff --git a/overlay_templates/IntelFruDevice.template b/overlay_templates/IntelFruDevice.template
index a55309e..33cba50 100644
--- a/overlay_templates/IntelFruDevice.template
+++ b/overlay_templates/IntelFruDevice.template
@@ -8,9 +8,9 @@
             #address-cells = <1>;
             #size-cells = <0>;
             status = "okay";
-            eeprom@$Name {
+            $Name: eeprom@$address {
                 compatible = "atmel,24c02";
-                reg = <$address>;
+                reg = <0x$address>;
                 oemname1 = "$Name";
                 pagesize = <64>;
             };
diff --git a/overlay_templates/PCA9543Mux.template b/overlay_templates/PCA9543Mux.template
index 49257c4..d85a6c3 100644
--- a/overlay_templates/PCA9543Mux.template
+++ b/overlay_templates/PCA9543Mux.template
@@ -6,9 +6,9 @@
         target = <&i2c$bus>;
         __overlay__{
             status = "okay";
-            smbus_mux@$Name {
+            $Name: smbus_mux@$address {
                 compatible = "nxp,pca9543";
-                reg = <$address>;
+                reg = <0x$address>;
                 #address-cells = <1>;
                 #size-cells = <0>;
                 oemname1 = "$Name";
diff --git a/overlay_templates/PCA9545Mux.template b/overlay_templates/PCA9545Mux.template
index 9da50a5..a106277 100644
--- a/overlay_templates/PCA9545Mux.template
+++ b/overlay_templates/PCA9545Mux.template
@@ -6,9 +6,9 @@
         target = <&i2c$bus>;
         __overlay__{
             status = "okay";
-            smbus_mux@$Name {
+            $Name: smbus_mux@$address {
                 compatible = "nxp,pca9545";
-                reg = <$address>;
+                reg = <0x$address>;
                 #address-cells = <1>;
                 #size-cells = <0>;
                 oemname1 = "$Name";
diff --git a/overlay_templates/SkylakeCPU.template b/overlay_templates/SkylakeCPU.template
index e4b8325..34b10dd 100644
--- a/overlay_templates/SkylakeCPU.template
+++ b/overlay_templates/SkylakeCPU.template
@@ -8,10 +8,10 @@
             #address-cells = <1>;
             #size-cells = <0>;
 
-            peci-client@Skylake_CPU_$cpu_id {
+            Skylake_CPU_$cpu_id: peci-client@$address {
                 compatible = "intel,peci-client";
                 oemname1 = "$Name";
-                reg = <$address>;
+                reg = <0x$address>;
                 status = "okay";
             };
         };
diff --git a/overlay_templates/TMP421.template b/overlay_templates/TMP421.template
index 70b2ab4..745d824 100644
--- a/overlay_templates/TMP421.template
+++ b/overlay_templates/TMP421.template
@@ -8,9 +8,9 @@
             #address-cells = <1>;
             #size-cells = <0>;
             status = "okay";
-            tmp421@$Name$Name1 {
+            $Name$Name1: tmp421@$address {
                 compatible = "ti,tmp421";
-                reg = <$address>;
+                reg = <0x$address>;
                 oemname1 = "$Name";
                 oemname2 = "$Name1";
                 scale = "-3";
diff --git a/overlay_templates/TMP75.template b/overlay_templates/TMP75.template
index 69d70ca..ddb5607 100644
--- a/overlay_templates/TMP75.template
+++ b/overlay_templates/TMP75.template
@@ -8,9 +8,9 @@
             #address-cells = <1>;
             #size-cells = <0>;
             status = "okay";
-            tmp75@$Name {
+            $Name: tmp75@$address {
                 compatible = "ti,tmp75";
-                reg = <$address>;
+                reg = <0x$address>;
                 oemname1 = "$Name";
                 scale = "-3";
             };
diff --git a/overlay_templates/pmbus.template b/overlay_templates/pmbus.template
index b48780b..7aca62a 100644
--- a/overlay_templates/pmbus.template
+++ b/overlay_templates/pmbus.template
@@ -8,9 +8,9 @@
             #address-cells = <1>;
             #size-cells = <0>;
             status = "okay";
-            pmbus@$Name {
+            $Name: pmbus@$address {
                 compatible = "pmbus";
-                reg = <$address>;
+                reg = <0x$address>;
                 oemname1 = "$Name";
             };
         };
diff --git a/src/Overlay.cpp b/src/Overlay.cpp
index f80994f..697456d 100644
--- a/src/Overlay.cpp
+++ b/src/Overlay.cpp
@@ -30,6 +30,7 @@
 constexpr const char *OUTPUT_DIR = "/tmp/overlays";
 constexpr const char *TEMPLATE_DIR = "/usr/share/overlay_templates";
 constexpr const char *TEMPLATE_CHAR = "$";
+constexpr const char *HEX_FORMAT_STR = "0x";
 constexpr const char *PLATFORM = "aspeed,ast2500";
 constexpr const char *I2C_DEVS_DIR = "/sys/class/i2c-dev";
 
@@ -173,6 +174,21 @@
                 keyPair.value().get<std::string>(), ILLEGAL_NAME_REGEX, "_");
             name = subsituteString;
         }
+        else if (keyPair.key() == "address")
+        {
+            if (keyPair.value().type() == nlohmann::json::value_t::string)
+            {
+                subsituteString = keyPair.value().get<std::string>();
+                subsituteString.erase(
+                    0, subsituteString.find_first_not_of(HEX_FORMAT_STR));
+            }
+            else
+            {
+                std::ostringstream hex;
+                hex << std::hex << keyPair.value().get<unsigned int>();
+                subsituteString = hex.str();
+            }
+        }
         else
         {
             subsituteString = jsonToString(keyPair.value());
@@ -289,4 +305,4 @@
     }
 
     return true;
-}
\ No newline at end of file
+}