0f1ef0491e73e29d334aadde52fab103f679048d
[yaffs2.git] / yaffs_endian.h
1 /*
2  * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
3  *
4  * Copyright (C) 2002-2018 Aleph One Ltd.
5  *
6  * Created by Charles Manning <charles@aleph1.co.uk>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License version 2.1 as
10  * published by the Free Software Foundation.
11  *
12  * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
13  */
14
15 #ifndef __YAFFS_ENDIAN_H__
16 #define __YAFFS_ENDIAN_H__
17 #include "yaffs_guts.h"
18 #include "yaffs_packedtags2.h"
19
20 static inline u32 swap_u32(u32 val)
21 {
22         return ((val >>24) & 0x000000ff) |
23                ((val >> 8) & 0x0000ff00) |
24                ((val << 8) & 0x00ff0000) |
25                ((val <<24) & 0xff000000);
26 }
27
28 static inline u64 swap_u64(u64 val)
29 {
30         return ((val >> 56) & 0x00000000000000ff) |
31                ((val >> 40) & 0x000000000000ff00) |
32                ((val >> 24) & 0x0000000000ff0000) |
33                ((val >> 8)  & 0x00000000ff000000) |
34                ((val << 8)  & 0x000000ff00000000) |
35                ((val << 24) & 0x0000ff0000000000) |
36                ((val << 40) & 0x00ff000000000000) |
37                ((val << 56) & 0xff00000000000000);
38 }
39
40 //YTIME_T can be a 32 or 64 bit number.
41 #if YAFFS_USE_32_BIT_TIME_T
42         #define swap_ytime_t( val ) swap_u32(val)
43 #else
44         #define swap_ytime_t( val ) swap_u64(val)
45 #endif
46
47 //swap a signed 32 bit integer.
48 #define swap_s32(val) \
49         (s32)(swap_u32((u32)(val)))
50
51 static inline loff_t swap_loff_t(loff_t lval)
52 {
53         u32 vall = swap_u32(FSIZE_LOW(lval));
54         u32 valh;
55
56         if (sizeof(loff_t) == sizeof(u32))
57                 return (loff_t) vall;
58
59         valh = swap_u32(FSIZE_HIGH(lval));
60
61         return FSIZE_COMBINE(vall, valh); /*NB: h and l are swapped. */
62 }
63
64
65
66 struct yaffs_dev;
67 void yaffs_do_endian_s32(struct yaffs_dev *dev, s32 *val);
68 void yaffs_do_endian_u32(struct yaffs_dev *dev, u32 *val);
69 void yaffs_do_endian_oh(struct yaffs_dev *dev, struct yaffs_obj_hdr *oh);
70 void yaffs_do_endian_packed_tags2(struct yaffs_dev *dev,
71                                 struct yaffs_packed_tags2_tags_only *ptt);
72 void yaffs_endian_config(struct yaffs_dev *dev);
73
74 #endif