Added endianness support for YTIME_T
authorTimothy Manning <codedraftsman@gmail.com>
Thu, 3 Jun 2021 22:21:27 +0000 (10:21 +1200)
committerTimothy Manning <codedraftsman@gmail.com>
Fri, 4 Jun 2021 00:59:52 +0000 (12:59 +1200)
Added a inline function that handles swapping the Endianness for YTIME_T.

Signed-off-by: Timothy Manning <codedraughtsman@users.noreply.github.com>
direct/test-framework/basic-tests/dtest.c
yaffs_endian.c
yaffs_endian.h

index bafd6f82ef132d4dbd0c348f1e83b2fb22631b59..60d17498c7481193f6f9a2eb9eb687dcee4bc008 100644 (file)
 #include <time.h>
 #include <ctype.h>
 
+
 #include "yaffsfs.h"
 
 #include "yaffs_guts.h" /* Only for dumping device innards */
+#include "yaffs_endian.h" /*For testing the swap_u64 macro */
 
 extern int yaffs_trace_mask;
 
@@ -2711,6 +2713,50 @@ void basic_utime_test(const char *mountpt)
 
 }
 
+void print_binary(u64 val){
+       int count = 0;
+       for (int i= 63; i>=0; i --) {
+               if (count == 0){
+                       printf(" ");
+               }
+               if ((((u64)1) << i) & val) {
+                       printf("1");
+               } else {
+                       printf("0");
+               }
+               count = (count +1) % 8;
+       }
+}
+
+void testing_swap_u64() {
+       int numberOfFailedTests = 0;
+       for (int i =0; i < 8; i ++) {
+               u64 startingNumber = (0xffLLu << (i*8));
+               u64 expected = (0xffLLu << (64 - (i*8) -8));
+               u64 converted = swap_u64(startingNumber);
+               if (converted != expected) {
+                       numberOfFailedTests ++;
+                       printf("numbers do not match.\n");
+                       printf("0xff\t\t\t");
+                       print_binary(0xff);
+                       printf("\nStarting Number \t");
+            print_binary(startingNumber);
+                       printf("\nExpecting \t\t");
+                       print_binary(expected);
+                       printf("\nConverted \t\t");
+                       print_binary(converted);
+
+                       printf("\n");
+               }
+       }
+       if (numberOfFailedTests){
+               printf("testing_swap failed %d tests\n", numberOfFailedTests);
+       } else {
+               printf("testing_swap_u64 passed all tests\n");
+       }
+}
+
+
 void size_utime_test(const char *mountpt)
 {
        char name[100];
@@ -3642,6 +3688,7 @@ int main(int argc, char *argv[])
         //readdir_test("/nand");
 
         basic_utime_test("/nand");
+        testing_swap_u64();
         size_utime_test("/nand");
         //case_insensitive_test("/nand");
 
index 6103f4ebb0c20ab062a982b19078ae3273f914a3..571a35f7642739041b888f94bf2102451831c8d8 100644 (file)
@@ -42,9 +42,9 @@ void yaffs_do_endian_oh(struct yaffs_dev *dev, struct yaffs_obj_hdr *oh)
 
        oh->yst_uid = swap_u32(oh->yst_uid);
        oh->yst_gid = swap_u32(oh->yst_gid);
-       oh->yst_atime = swap_u32(oh->yst_atime);
-       oh->yst_mtime = swap_u32(oh->yst_mtime);
-       oh->yst_ctime = swap_u32(oh->yst_ctime);
+       oh->yst_atime = swap_ytime_t(oh->yst_atime);
+       oh->yst_mtime = swap_ytime_t(oh->yst_mtime);
+       oh->yst_ctime = swap_ytime_t(oh->yst_ctime);
 
        oh->file_size_low = swap_u32(oh->file_size_low);
 
index 8c271891a0197cb2d8e1d039c289e27208cd09b8..0f1ef0491e73e29d334aadde52fab103f679048d 100644 (file)
@@ -25,6 +25,26 @@ static inline u32 swap_u32(u32 val)
               ((val <<24) & 0xff000000);
 }
 
+static inline u64 swap_u64(u64 val)
+{
+       return ((val >> 56) & 0x00000000000000ff) |
+              ((val >> 40) & 0x000000000000ff00) |
+              ((val >> 24) & 0x0000000000ff0000) |
+              ((val >> 8)  & 0x00000000ff000000) |
+              ((val << 8)  & 0x000000ff00000000) |
+              ((val << 24) & 0x0000ff0000000000) |
+              ((val << 40) & 0x00ff000000000000) |
+              ((val << 56) & 0xff00000000000000);
+}
+
+//YTIME_T can be a 32 or 64 bit number.
+#if YAFFS_USE_32_BIT_TIME_T
+       #define swap_ytime_t( val ) swap_u32(val)
+#else
+       #define swap_ytime_t( val ) swap_u64(val)
+#endif
+
+//swap a signed 32 bit integer.
 #define swap_s32(val) \
        (s32)(swap_u32((u32)(val)))