[rdkb][common][app][Refactor regs and mtk-factory-rw tools]

[Description]
Refactor regs and mtk-factory-rw tools

[Release-log]

Change-Id: Ifcaece41f2fe204fbc6804e659ea78160a86f041
diff --git a/recipes-devtools/mtk-factory-rw/files/fix-rdkb-get-board-name-issue.patch b/recipes-devtools/mtk-factory-rw/files/fix-rdkb-get-board-name-issue.patch
new file mode 100644
index 0000000..010ecdc
--- /dev/null
+++ b/recipes-devtools/mtk-factory-rw/files/fix-rdkb-get-board-name-issue.patch
@@ -0,0 +1,20 @@
+--- ../mtk_factory_rw.sh.bak	2022-11-03 17:09:43.001641800 +0800
++++ ../mtk_factory_rw.sh	2022-11-03 17:26:01.636647400 +0800
+@@ -27,7 +27,7 @@
+ lan_mac_offset=0x2A
+ wan_mac_offset=0x24
+ 
+-case `cat /tmp/sysinfo/board_name` in
++case `cat /proc/device-tree/model` in
+ 	*7621*ax*)
+ 		# 256k - 12 byte
+ 		lan_mac_offset=0x3FFF4
+@@ -65,7 +65,7 @@
+ 	local length=$1
+ 	local offset=$2
+ 
+-	hexdump -v -n ${length} -s ${offset} -e ''`expr ${length} - 1`'/1 "%02x-" "%02x "' ${factory_mtd}
++	hexdump -v -n ${length} -s ${offset} -e ''`expr ${length} - 1`'/1 "%02x:" "%02x "' ${factory_mtd}
+ }
+ 
+ overwrite_data=
diff --git a/recipes-devtools/mtk-factory-rw/files/mtk_factory_rw.sh b/recipes-devtools/mtk-factory-rw/files/mtk_factory_rw.sh
index 4b4a03f..3124088 100644
--- a/recipes-devtools/mtk-factory-rw/files/mtk_factory_rw.sh
+++ b/recipes-devtools/mtk-factory-rw/files/mtk_factory_rw.sh
@@ -27,7 +27,7 @@
 lan_mac_offset=0x2A
 wan_mac_offset=0x24
 
-case `cat /proc/device-tree/model` in
+case `cat /tmp/sysinfo/board_name` in
 	*7621*ax*)
 		# 256k - 12 byte
 		lan_mac_offset=0x3FFF4
@@ -46,6 +46,12 @@
 		lan_mac_offset=0x1F800
 		wan_mac_offset=0x1F806
 		;;
+	*7988*)
+		#1024k - 18 byte
+		lan2_mac_offset=0xFFFEE
+		lan_mac_offset=0xFFFF4
+		wan_mac_offset=0xFFFFA
+		;;
 	*)
 		lan_mac_offset=0x2A
 		wan_mac_offset=0x24
@@ -59,7 +65,7 @@
 	local length=$1
 	local offset=$2
 
-	hexdump -v -n ${length} -s ${offset} -e ''`expr ${length} - 1`'/1 "%02x:" "%02x "' ${factory_mtd}
+	hexdump -v -n ${length} -s ${offset} -e ''`expr ${length} - 1`'/1 "%02x-" "%02x "' ${factory_mtd}
 }
 
 overwrite_data=
@@ -99,6 +105,9 @@
 	if [ "$1" == "lan" ]; then
 		#read lan mac
 		Get_offset_data 6 ${lan_mac_offset}
+	elif [ "$1" == "lan2" ]; then
+		#read lan2 mac
+		Get_offset_data 6 ${lan2_mac_offset}
 	elif [ "$1" == "wan" ]; then
 		#read wan mac
 		Get_offset_data 6 ${wan_mac_offset}
@@ -121,6 +130,10 @@
 		#write lan mac
 		Set_offset_data 6 ${lan_mac_offset} $@
 
+	elif [ "$1" == "lan2" ]; then
+		#write lan2 mac
+		Set_offset_data 6 ${lan2_mac_offset} $@
+
 	elif [ "$1" == "wan" ]; then
 		#write wan mac
 		Set_offset_data 6 ${wan_mac_offset} $@
@@ -135,7 +148,7 @@
 # 2. Set/Get the offset data: mtk_factory -r/-w length offset /data
 # 3. Overwrite from offset1 to offset2 by length byte : mtk_factory -o length from to
 if [ "$1" == "-r" ]; then
-	if [ "$2" == "lan" -o "$2" == "wan" ]; then
+	if [ "$2" == "lan" -o "$2" == "lan2" -o "$2" == "wan" ]; then
 		GetMac $2
 	elif [ "$2" -eq "$2" ]; then
 		Get_offset_data $2 $3
@@ -145,7 +158,7 @@
 		exit 1
 	fi
 elif [ "$1" == "-w" ]; then
-	if [ "$2" == "lan" -o "$2" == "wan" ]; then
+	if [ "$2" == "lan"  -o "$2" == "lan2" -o "$2" == "wan" ]; then
 		SetMac $2 $@
 	else
 		Set_offset_data $2 $3 $@
diff --git a/recipes-devtools/mtk-factory-rw/mtk-factory-rw.bb b/recipes-devtools/mtk-factory-rw/mtk-factory-rw.bb
index 0d86d6c..66eacd7 100644
--- a/recipes-devtools/mtk-factory-rw/mtk-factory-rw.bb
+++ b/recipes-devtools/mtk-factory-rw/mtk-factory-rw.bb
@@ -11,6 +11,7 @@
     file://mtk_factory_rw.sh \
     file://init-MacAddr.sh \
     file://init-MacAddr.service \
+    file://fix-rdkb-get-board-name-issue.patch \
     "
 
 SYSTEMD_PACKAGES = "${PN}"
diff --git a/recipes-devtools/regs/files/src/Makefile b/recipes-devtools/regs/files/src/Makefile
new file mode 100644
index 0000000..bc3a12f
--- /dev/null
+++ b/recipes-devtools/regs/files/src/Makefile
@@ -0,0 +1,13 @@
+EXEC = regs
+
+all: $(EXEC)
+
+$(EXEC): $(EXEC).c
+	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $@.c $(LDLIBS)
+
+romfs:
+	$(ROMFSINST) /bin/$(EXEC)
+
+clean:
+	-rm -f $(EXEC) *.elf *.gdb *.o
+
diff --git a/recipes-devtools/regs/files/regs.c b/recipes-devtools/regs/files/src/regs.c
similarity index 76%
rename from recipes-devtools/regs/files/regs.c
rename to recipes-devtools/regs/files/src/regs.c
index 93a3532..09e088c 100644
--- a/recipes-devtools/regs/files/regs.c
+++ b/recipes-devtools/regs/files/src/regs.c
@@ -15,7 +15,7 @@
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
- * 
+ *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
@@ -44,8 +44,8 @@
 #define MAP_SIZE 4096UL
 #define MAP_MASK (MAP_SIZE - 1)
 
-void dump_page(uint32_t *vaddr, uint32_t *vbase, uint32_t *pbase){
-	
+void dump_page(uint32_t *vaddr, uint32_t *vbase, uint32_t *pbase)
+{
 	int i =0;
 	uint32_t *end = vaddr + (MAP_SIZE >> 6);
 	uint32_t *start = vaddr;
@@ -63,15 +63,27 @@
     int value;
     int i;
 
+	if ((start_bit < 0) || (start_bit > 31) ||
+	    (data_len < 1) || (data_len > 32) ||
+	    (start_bit + data_len > 32)) {
+		fprintf(stderr, "Startbit range[0~31], and DataLen range[1~32], and Startbit + DataLen <= 32\n");
+		return;
+	}
+
-    for (i = 0; i < data_len; i++) {
-        mask |= 1 << (start_bit + i);
-    }
+	for (i = 0; i < data_len; i++) {
+		if (start_bit + i > 31)
+			break;
+
+		mask |= 1 << (start_bit + i);
+	}
+
+	value = *((volatile uint32_t *) virt_addr);
+	value &= ~mask;
+	value |= (data << start_bit) & mask;;
 
-    value = *((volatile uint32_t *) virt_addr);
-    value &= ~mask;
-    value |= (data << start_bit) & mask;;
+	*((uint32_t *) virt_addr) = value;
 
-    *((uint32_t *) virt_addr) = value;
+	printf("Modify 0x%X[%d:%d]; ", data, start_bit + data_len - 1, start_bit);
 }
 
 void usage(void)
@@ -80,8 +92,8 @@
 			"\tType    : access operation type : [m]odify, [w]wite, [d]ump\n"
 			"\tOffset  : offset into memory region to act upon\n"
 			"\tData    : data to be written\n"
-			"\tStartbit: Startbit of Addr that want to be modified\n"
-			"\tDataLen : Data length of Data\n\n"
+			"\tStartbit: Startbit of Addr that want to be modified. Range[0~31]\n"
+			"\tDataLen : Data length of Data. Range[1~32], and Startbit + DataLen <= 32\n\n"
 			"Example:\tRead/Write/Modify register \n"
 			"\tRead    : regs d 0x1b100000           //dump 0x1b100000~0x1b1000f0 \n"
 			"\tWrite   : regs w 0x1b100000 0x1234    //write 0x1b100000=0x1234\n"
@@ -100,30 +112,31 @@
 	off_t offset = 0;
 	int access_type = 0;
 
-	if(argc < 2) {
+	if(argc < 3) {
 		usage();
 		exit(1);
 	}
 
 	access_type = tolower(argv[1][0]);
-	if((access_type == 'd' && argc < 3) || (access_type == 'w' && argc < 4) || (access_type == 'm' && argc < 6)) {
+	if ((access_type == 'w' && argc < 4) || (access_type == 'm' && argc < 6)) {
 		usage();
 		exit(1);
 	}
 
 	filename = "/dev/mem";
-	if((fd = open(filename, O_RDWR | O_SYNC)) == -1) 
+	if((fd = open(filename, O_RDWR | O_SYNC)) == -1)
 		PRINT_ERROR;
 
 	/* Map one page */
 	offset = strtoul(argv[2], NULL, 16);
 	map_base = mmap(0, 2*MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, offset & ~MAP_MASK);
-	if(map_base == (void *) -1) 
+	if(map_base == (void *) -1)
 		PRINT_ERROR;
 
 	virt_addr = map_base + (offset & MAP_MASK);
 	read_result = *((volatile uint32_t *) virt_addr);
-	printf("Value at 0x%X (%p): 0x%X\n", offset, virt_addr, read_result);
+	printf("Value at 0x%llX (%p): 0x%X\n",
+	       (unsigned long long)offset, virt_addr, read_result);
 
 	switch(access_type) {
 		case 'm':
@@ -135,19 +148,21 @@
 		case 'w':
 			writeval = strtoul(argv[3], 0, 16);
 			*((uint32_t *) virt_addr) = writeval;
+			printf("Written 0x%X; ", writeval);
 			break;
 		case 'd':
 			dump_page(virt_addr, map_base, (uint32_t *)(offset & ~MAP_MASK));
-			return;
+			goto out;
 		default:
 			fprintf(stderr, "Illegal data type '%c'.\n", access_type);
-			exit(2);
+			goto out;
 	}
 
 	read_result = *((volatile uint32_t *) virt_addr);
-	printf("Written 0x%X; Readback 0x%X\n", writeval, read_result);
+	printf("Readback 0x%X\n", read_result);
 
-	if(munmap(map_base, MAP_SIZE) == -1) 
+out:
+	if(munmap(map_base, MAP_SIZE) == -1)
 		PRINT_ERROR;
 
 	close(fd);
diff --git a/recipes-devtools/regs/regs_1.0.bb b/recipes-devtools/regs/regs_1.0.bb
index 80666fa..7761918 100644
--- a/recipes-devtools/regs/regs_1.0.bb
+++ b/recipes-devtools/regs/regs_1.0.bb
@@ -3,18 +3,18 @@
 LICENSE = "GPLv2"
 LIC_FILES_CHKSUM = "file://COPYING;md5=c188eeeb69c0a05d0545816f1458a0c9"
 
-S = "${WORKDIR}"
-
 SRC_URI = " \
-    file://COPYING \
-    file://regs.c \
+    file://COPYING;subdir=git/src \
+    file://src;subdir=git \
     "
 
+S = "${WORKDIR}/git/src"
+
 do_compile() {
     ${CC} ${CFLAGS} ${LDFLAGS} regs.c -o regs
 }
 
 do_install() {
     install -d ${D}${base_bindir}
-    install -m 0755 ${WORKDIR}/regs ${D}${base_bindir}
+    install -m 0755 ${S}/regs ${D}${base_bindir}
 }