From 31a773e3cef4d618ddb9ea70309a3f8338cd6e1e Mon Sep 17 00:00:00 2001
From: David Sidrane <david_s5@nscdg.com>
Date: Sat, 21 Nov 2015 06:17:27 -1000
Subject: [PATCH] Use _ prefix o uavcan files in ROMFS , simplify upgrade

---
 Tools/uavcan_copy.sh                  |  7 +++---
 src/modules/uavcan/uavcan_servers.cpp | 33 +++++++++++++--------------
 src/modules/uavcan/uavcan_servers.hpp |  2 +-
 3 files changed, 21 insertions(+), 21 deletions(-)

diff --git a/Tools/uavcan_copy.sh b/Tools/uavcan_copy.sh
index 34969cf8fe..9d21e0b562 100755
--- a/Tools/uavcan_copy.sh
+++ b/Tools/uavcan_copy.sh
@@ -1,20 +1,21 @@
 #!/bin/bash
 UAVCANFW=../ROMFS/px4fmu_common/uavcan/fw
+ROMFS_REFIX=_
 echo Removing : $UAVCANFW
 rm -fr $UAVCANFW
 for f in $(find firmware -type f -name "*.*.bin")
 do
   UAVCAN_NAME=$(echo $f | cut -d"/" -f2 | cut -d. -f1-4 | cut -d- -f1-2)
   UAVCAN_HW=$(echo $f cut -d/ -f2 | cut -d. -f1-4 | cut -d- -f3)
-  DST=$(echo $f | cut -d. -f3-7 | cut -d- -f1,2,3).$(echo $f | cut -d. -f6,7)
+  DST=${ROMFS_REFIX}$(echo $f | cut -d. -f3-7 | cut -d- -f1,2,3).$(echo $f | cut -d. -f6,7)
   # deal with legacy non conforming naming
   if [[ ${DST:(-7)} == bin.bin ]]
   then
     echo " WARNING: Improper name format!!!!!!!!! $f see should be <uavcan_name>-<HW_MAJOR>.<HW_MINOR)-<HW_MAJOR>.<HW_MINOR).<git hash[8]>.bin"
-    DST=${DST%???}
+    DST=${DST%????}
   fi
+  echo Processing file: $f Length:${#DST}
   if [ ${#DST} -le 28 ]
-    echo Processing file: $f
   then
     if [ -d "${UAVCANFW}/${UAVCAN_NAME}/${UAVCAN_HW}" ]
     then
diff --git a/src/modules/uavcan/uavcan_servers.cpp b/src/modules/uavcan/uavcan_servers.cpp
index 318940eb64..2da32688b0 100644
--- a/src/modules/uavcan/uavcan_servers.cpp
+++ b/src/modules/uavcan/uavcan_servers.cpp
@@ -886,14 +886,15 @@ void UavcanServers::unpackFwFromROMFS(const char* sd_path, const char* romfs_pat
 
 		/fs/microsd/fw
 		-	/c
-		-	-	/1a2b3c4d.bin			cache file (copy of /fs/microsd/fw/org.pixhawk.nodename-v1/1.1/1a2b3c4d.bin)
-		-	/org.pixhawk.nodename-v1	device directory for org.pixhawk.nodename-v1
-		-	-	/1.0					version directory for hardware 1.0
-		-	-	/1.1					version directory for hardware 1.1
-		-	-	-	/1a2b3c4d.bin		firmware file for org.pixhawk.nodename-v1 nodes, hardware version 1.1
-		-	/com.example.othernode-v3	device directory for com.example.othernode-v3
-		-	-	/1.0					version directory for hardawre 1.0
-		-	-	-	/deadbeef.bin		firmware file for com.example.othernode-v3, hardware version 1.0
+			-	/nodename-v1-1.0.25d0137d.bin		cache file (copy of /fs/microsd/fw/org.pixhawk.nodename-v1/1.1/nodename-v1-1.0.25d0137d.bin)
+			-	/othernode-v3-1.6.25d0137d.bin		cache file (copy of /fs/microsd/fw/com.example.othernode-v3/1.6/othernode-v3-1.6.25d0137d.bin)
+		-	/org.pixhawk.nodename-v1				device directory for org.pixhawk.nodename-v1
+		-	-	/1.0								version directory for hardware 1.0
+		-	-	/1.1								version directory for hardware 1.1
+		-	-	-	/nodename-v1-1.0.25d0137d.bin	firmware file for org.pixhawk.nodename-v1 nodes, hardware version 1.1
+		-	/com.example.othernode-v3				device directory for com.example.othernode-v3
+		-	-	/1.0								version directory for hardawre 1.0
+		-	-	-	/othernode-v3-1.6.25d0137d.bin	firmware file for com.example.othernode-v3, hardware version 1.6
 
 	The ROMFS directory structure is the same, but located at /etc/uavcan/fw
 
@@ -1037,7 +1038,7 @@ void UavcanServers::unpackFwFromROMFS(const char* sd_path, const char* romfs_pat
 						continue;
 					}
 
-					if (!memcmp(&fw_dirent->d_name[sizeof(UAVCAN_ROMFS_FW_PREFIX) - 1], src_fw_dirent->d_name, fw_len)) {
+					if (!memcmp(&fw_dirent->d_name, src_fw_dirent->d_name, fw_len)) {
 						/*
 						 * Exact match between SD card filename and ROMFS filename; must be the same version
 						 * so don't bother deleting and rewriting it.
@@ -1048,14 +1049,13 @@ void UavcanServers::unpackFwFromROMFS(const char* sd_path, const char* romfs_pat
 						size_t dstpath_fw_len = dstpath_ver_len + sizeof(UAVCAN_ROMFS_FW_PREFIX) + fw_len;
 						if (dstpath_fw_len > maxlen) {
 							// sizeof(prefix) includes trailing NUL, cancelling out the +1 for the path separator
-							warnx("unlink: path '%s/%s%s' too long", dstpath, UAVCAN_ROMFS_FW_PREFIX, fw_dirent->d_name);
+							warnx("unlink: path '%s/%s' too long", dstpath, fw_dirent->d_name);
 						} else {
-							// File name starts with "romfs_", delete it.
+							// File name starts with "_", delete it.
 							dstpath[dstpath_ver_len] = '/';
 							memcpy(&dstpath[dstpath_ver_len + 1], fw_dirent->d_name, fw_len + 1);
 							unlink(dstpath);
-
-							warnx("unlink: removed '%s/%s%s'", dstpath, UAVCAN_ROMFS_FW_PREFIX, fw_dirent->d_name);
+							warnx("unlink: removed '%s/%s'", dstpath, fw_dirent->d_name);
 						}
 					} else {
 						// User file, don't copy firmware
@@ -1068,20 +1068,19 @@ void UavcanServers::unpackFwFromROMFS(const char* sd_path, const char* romfs_pat
 			// If we need to, copy the file from ROMFS to the SD card
 			if (copy_fw) {
 				size_t srcpath_fw_len = srcpath_ver_len + 1 + fw_len;
-				size_t dstpath_fw_len = dstpath_ver_len + sizeof(UAVCAN_ROMFS_FW_PREFIX) + fw_len;
+				size_t dstpath_fw_len = dstpath_ver_len + fw_len;
 
 				if (srcpath_fw_len > maxlen) {
 					warnx("copy: srcpath '%s/%s' too long", srcpath, src_fw_dirent->d_name);
 				} else if (dstpath_fw_len > maxlen) {
-					warnx("copy: dstpath '%s/%s%s' too long", dstpath, UAVCAN_ROMFS_FW_PREFIX, src_fw_dirent->d_name);
+					warnx("copy: dstpath '%s/%s' too long", dstpath, src_fw_dirent->d_name);
 				} else {
 					// All OK, make the paths and copy the file
 					srcpath[srcpath_ver_len] = '/';
 					memcpy(&srcpath[srcpath_ver_len + 1], src_fw_dirent->d_name, fw_len + 1);
 
 					dstpath[dstpath_ver_len] = '/';
-					memcpy(&dstpath[dstpath_ver_len + 1], UAVCAN_ROMFS_FW_PREFIX, sizeof(UAVCAN_ROMFS_FW_PREFIX));
-					memcpy(&dstpath[dstpath_ver_len + sizeof(UAVCAN_ROMFS_FW_PREFIX)], src_fw_dirent->d_name, fw_len + 1);
+					memcpy(&dstpath[dstpath_ver_len], src_fw_dirent->d_name, fw_len + 1);
 
 					rv = copyFw(dstpath, srcpath);
 					if (rv != 0) {
diff --git a/src/modules/uavcan/uavcan_servers.hpp b/src/modules/uavcan/uavcan_servers.hpp
index 3cf4cbb4da..30fb3f5255 100644
--- a/src/modules/uavcan/uavcan_servers.hpp
+++ b/src/modules/uavcan/uavcan_servers.hpp
@@ -70,7 +70,7 @@
 #define UAVCAN_NODE_DB_PATH     "/fs/microsd/uavcan.db"
 #define UAVCAN_FIRMWARE_PATH    "/fs/microsd/fw"
 #define UAVCAN_ROMFS_FW_PATH "/etc/uavcan/fw"
-#define UAVCAN_ROMFS_FW_PREFIX "romfs_"
+#define UAVCAN_ROMFS_FW_PREFIX "_"
 #define UAVCAN_MAX_PATH_LENGTH (128 + 40)
 #define UAVCAN_LOG_FILE         UAVCAN_NODE_DB_PATH"/trace.log"
 
-- 
GitLab