Browse Source

[libc] Rewrite byte-swapping code

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 9 years ago
parent
commit
01d16d821f

+ 0
- 8
src/arch/i386/include/bits/endian.h View File

@@ -1,8 +0,0 @@
1
-#ifndef ETHERBOOT_BITS_ENDIAN_H
2
-#define ETHERBOOT_BITS_ENDIAN_H
3
-
4
-FILE_LICENCE ( GPL2_OR_LATER );
5
-
6
-#define __BYTE_ORDER __LITTLE_ENDIAN
7
-
8
-#endif /* ETHERBOOT_BITS_ENDIAN_H */

+ 8
- 0
src/arch/x86/include/bits/endian.h View File

@@ -0,0 +1,8 @@
1
+#ifndef _BITS_ENDIAN_H
2
+#define _BITS_ENDIAN_H
3
+
4
+FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
5
+
6
+#define __BYTE_ORDER __LITTLE_ENDIAN
7
+
8
+#endif /* _BITS_ENDIAN_H */

+ 0
- 6
src/arch/x86_64/include/bits/endian.h View File

@@ -1,6 +0,0 @@
1
-#ifndef ETHERBOOT_BITS_ENDIAN_H
2
-#define ETHERBOOT_BITS_ENDIAN_H
3
-
4
-#define __BYTE_ORDER __LITTLE_ENDIAN
5
-
6
-#endif /* ETHERBOOT_BITS_ENDIAN_H */

+ 3
- 3
src/drivers/net/eepro.c View File

@@ -601,9 +601,9 @@ static int eepro_probe ( struct nic *nic, struct isa_device *isa ) {
601 601
 		l_eepro = 0;
602 602
 		name = "Intel 82595-based LAN card";
603 603
 	}
604
-	station_addr.saddr[0] = swap16(station_addr.saddr[0]);
605
-	station_addr.saddr[1] = swap16(station_addr.saddr[1]);
606
-	station_addr.saddr[2] = swap16(station_addr.saddr[2]);
604
+	station_addr.saddr[0] = bswap_16(station_addr.saddr[0]);
605
+	station_addr.saddr[1] = bswap_16(station_addr.saddr[1]);
606
+	station_addr.saddr[2] = bswap_16(station_addr.saddr[2]);
607 607
 	for (i = 0; i < ETH_ALEN; i++) {
608 608
 		nic->node_addr[i] = station_addr.caddr[i];
609 609
 	}

+ 0
- 35
src/include/big_bswap.h View File

@@ -1,35 +0,0 @@
1
-#ifndef ETHERBOOT_BIG_BSWAP_H
2
-#define ETHERBOOT_BIG_BSWAP_H
3
-
4
-#define htonll(x)	(x)
5
-#define ntohll(x)	(x)
6
-#define ntohl(x) 	(x)
7
-#define htonl(x) 	(x)
8
-#define ntohs(x) 	(x)
9
-#define htons(x) 	(x)
10
-#define cpu_to_le64(x)	__bswap_64(x)
11
-#define cpu_to_le32(x)	__bswap_32(x)
12
-#define cpu_to_le16(x)	__bswap_16(x)
13
-#define cpu_to_be64(x)	(x)
14
-#define cpu_to_be32(x)	(x)
15
-#define cpu_to_be16(x)	(x)
16
-#define le64_to_cpu(x)	__bswap_64(x)
17
-#define le32_to_cpu(x)	__bswap_32(x)
18
-#define le16_to_cpu(x)	__bswap_16(x)
19
-#define be64_to_cpu(x)	(x)
20
-#define be32_to_cpu(x)	(x)
21
-#define be16_to_cpu(x)	(x)
22
-#define cpu_to_le64s(x) __bswap_64s(x)
23
-#define cpu_to_le32s(x) __bswap_32s(x)
24
-#define cpu_to_le16s(x) __bswap_16s(x)
25
-#define cpu_to_be64s(x) do {} while (0)
26
-#define cpu_to_be32s(x) do {} while (0)
27
-#define cpu_to_be16s(x) do {} while (0)
28
-#define le64_to_cpus(x) __bswap_64s(x)
29
-#define le32_to_cpus(x) __bswap_32s(x)
30
-#define le16_to_cpus(x) __bswap_16s(x)
31
-#define be64_to_cpus(x) do {} while (0)
32
-#define be32_to_cpus(x) do {} while (0)
33
-#define be16_to_cpus(x) do {} while (0)
34
-
35
-#endif /* ETHERBOOT_BIG_BSWAP_H */

+ 132
- 53
src/include/byteswap.h View File

@@ -1,59 +1,138 @@
1
-#ifndef ETHERBOOT_BYTESWAP_H
2
-#define ETHERBOOT_BYTESWAP_H
3
-
4
-FILE_LICENCE ( GPL2_OR_LATER );
5
-
6
-#include "endian.h"
7
-#include "bits/byteswap.h"
8
-
9
-#define __bswap_constant_16(x) \
10
-	((uint16_t)((((uint16_t)(x) & 0x00ff) << 8) | \
11
-		    (((uint16_t)(x) & 0xff00) >> 8)))
12
-
13
-#define __bswap_constant_32(x) \
14
-	((uint32_t)((((uint32_t)(x) & 0x000000ffU) << 24) | \
15
-		    (((uint32_t)(x) & 0x0000ff00U) <<  8) | \
16
-		    (((uint32_t)(x) & 0x00ff0000U) >>  8) | \
17
-		    (((uint32_t)(x) & 0xff000000U) >> 24)))
18
-
19
-#define __bswap_constant_64(x) \
20
-	((uint64_t)((((uint64_t)(x) & 0x00000000000000ffULL) << 56) | \
21
-		    (((uint64_t)(x) & 0x000000000000ff00ULL) << 40) | \
22
-		    (((uint64_t)(x) & 0x0000000000ff0000ULL) << 24) | \
23
-		    (((uint64_t)(x) & 0x00000000ff000000ULL) <<  8) | \
24
-		    (((uint64_t)(x) & 0x000000ff00000000ULL) >>  8) | \
25
-		    (((uint64_t)(x) & 0x0000ff0000000000ULL) >> 24) | \
26
-		    (((uint64_t)(x) & 0x00ff000000000000ULL) >> 40) | \
27
-		    (((uint64_t)(x) & 0xff00000000000000ULL) >> 56)))
28
-
29
-#define __bswap_16(x) \
30
-	((uint16_t)(__builtin_constant_p(x) ? \
31
-	__bswap_constant_16(x) : \
32
-	__bswap_variable_16(x)))
33
-
34
-#define __bswap_32(x) \
35
-	((uint32_t)(__builtin_constant_p(x) ? \
36
-	__bswap_constant_32(x) : \
37
-	__bswap_variable_32(x)))
38
-
39
-#define __bswap_64(x) \
40
-	((uint64_t)(__builtin_constant_p(x) ? \
41
-	__bswap_constant_64(x) : \
42
-	__bswap_variable_64(x)))
1
+#ifndef BYTESWAP_H
2
+#define BYTESWAP_H
3
+
4
+FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
5
+
6
+#include <stdint.h>
7
+#include <endian.h>
8
+#include <bits/byteswap.h>
9
+
10
+/**
11
+ * Byte-swap a 16-bit constant
12
+ *
13
+ * @v value		Constant value
14
+ * @ret swapped		Byte-swapped value
15
+ */
16
+#define __bswap_constant_16( value )					\
17
+	( ( ( (value) & 0x00ff ) << 8 ) |				\
18
+	  ( ( (value) & 0xff00 ) >> 8 ) )
19
+
20
+/**
21
+ * Byte-swap a 32-bit constant
22
+ *
23
+ * @v value		Constant value
24
+ * @ret swapped		Byte-swapped value
25
+ */
26
+#define __bswap_constant_32( value ) \
27
+	( ( ( (value) & 0x000000ffUL ) << 24 ) |			\
28
+	  ( ( (value) & 0x0000ff00UL ) <<  8 ) |			\
29
+	  ( ( (value) & 0x00ff0000UL ) >>  8 ) |			\
30
+	  ( ( (value) & 0xff000000UL ) >> 24 ) )
31
+
32
+/**
33
+ * Byte-swap a 64-bit constant
34
+ *
35
+ * @v value		Constant value
36
+ * @ret swapped		Byte-swapped value
37
+ */
38
+#define __bswap_constant_64( value )					\
39
+	( ( ( (value) & 0x00000000000000ffULL ) << 56 ) |		\
40
+	  ( ( (value) & 0x000000000000ff00ULL ) << 40 ) |		\
41
+	  ( ( (value) & 0x0000000000ff0000ULL ) << 24 ) |		\
42
+	  ( ( (value) & 0x00000000ff000000ULL ) <<  8 ) |		\
43
+	  ( ( (value) & 0x000000ff00000000ULL ) >>  8 ) |		\
44
+	  ( ( (value) & 0x0000ff0000000000ULL ) >> 24 ) |		\
45
+	  ( ( (value) & 0x00ff000000000000ULL ) >> 40 ) |		\
46
+	  ( ( (value) & 0xff00000000000000ULL ) >> 56 ) )
47
+
48
+/**
49
+ * Byte-swap a 16-bit value
50
+ *
51
+ * @v value		Value
52
+ * @ret swapped		Byte-swapped value
53
+ */
54
+#define __bswap_16( value )						\
55
+	( __builtin_constant_p (value) ?				\
56
+	  ( ( uint16_t ) __bswap_constant_16 ( ( uint16_t ) (value) ) ) \
57
+	  : __bswap_variable_16 (value) )
58
+#define bswap_16( value ) __bswap_16 (value)
59
+
60
+/**
61
+ * Byte-swap a 32-bit value
62
+ *
63
+ * @v value		Value
64
+ * @ret swapped		Byte-swapped value
65
+ */
66
+#define __bswap_32( value )						\
67
+	( __builtin_constant_p (value) ?				\
68
+	  ( ( uint32_t ) __bswap_constant_32 ( ( uint32_t ) (value) ) ) \
69
+	  : __bswap_variable_32 (value) )
70
+#define bswap_32( value ) __bswap_32 (value)
71
+
72
+/**
73
+ * Byte-swap a 64-bit value
74
+ *
75
+ * @v value		Value
76
+ * @ret swapped		Byte-swapped value
77
+ */
78
+#define __bswap_64( value )						\
79
+	( __builtin_constant_p (value) ?				\
80
+	  ( ( uint64_t ) __bswap_constant_64 ( ( uint64_t ) (value) ) ) \
81
+          : __bswap_variable_64 (value) )
82
+#define bswap_64( value ) __bswap_64 (value)
43 83
 
44 84
 #if __BYTE_ORDER == __LITTLE_ENDIAN
45
-#include "little_bswap.h"
85
+#define __cpu_to_leNN( bits, value ) (value)
86
+#define __cpu_to_beNN( bits, value ) __bswap_ ## bits (value)
87
+#define __leNN_to_cpu( bits, value ) (value)
88
+#define __beNN_to_cpu( bits, value ) __bswap_ ## bits (value)
89
+#define __cpu_to_leNNs( bits, ptr ) do { } while ( 0 )
90
+#define __cpu_to_beNNs( bits, ptr ) __bswap_ ## bits ## s (ptr)
91
+#define __leNN_to_cpus( bits, ptr ) do { } while ( 0 )
92
+#define __beNN_to_cpus( bits, ptr ) __bswap_ ## bits ## s (ptr)
46 93
 #endif
94
+
47 95
 #if __BYTE_ORDER == __BIG_ENDIAN
48
-#include "big_bswap.h"
96
+#define __cpu_to_leNN( bits, value ) __bswap_ ## bits (value)
97
+#define __cpu_to_beNN( bits, value ) (value)
98
+#define __leNN_to_cpu( bits, value ) __bswap_ ## bits (value)
99
+#define __beNN_to_cpu( bits, value ) (value)
100
+#define __cpu_to_leNNs( bits, ptr ) __bswap_ ## bits ## s (ptr)
101
+#define __cpu_to_beNNs( bits, ptr ) do { } while ( 0 )
102
+#define __leNN_to_cpus( bits, ptr ) __bswap_ ## bits ## s (ptr)
103
+#define __beNN_to_cpus( bits, ptr ) do { } while ( 0 )
49 104
 #endif
50 105
 
51
-/* Make routines available to all */
52
-#define swap64(x)	__bswap_64(x)
53
-#define swap32(x)	__bswap_32(x)
54
-#define swap16(x)	__bswap_16(x)
55
-#define bswap_64(x)	__bswap_64(x)
56
-#define bswap_32(x)	__bswap_32(x)
57
-#define bswap_16(x)	__bswap_16(x)
58
-	
59
-#endif /* ETHERBOOT_BYTESWAP_H */
106
+#define cpu_to_le16( value ) __cpu_to_leNN ( 16, value )
107
+#define cpu_to_le32( value ) __cpu_to_leNN ( 32, value )
108
+#define cpu_to_le64( value ) __cpu_to_leNN ( 64, value )
109
+#define cpu_to_be16( value ) __cpu_to_beNN ( 16, value )
110
+#define cpu_to_be32( value ) __cpu_to_beNN ( 32, value )
111
+#define cpu_to_be64( value ) __cpu_to_beNN ( 64, value )
112
+#define le16_to_cpu( value ) __leNN_to_cpu ( 16, value )
113
+#define le32_to_cpu( value ) __leNN_to_cpu ( 32, value )
114
+#define le64_to_cpu( value ) __leNN_to_cpu ( 64, value )
115
+#define be16_to_cpu( value ) __beNN_to_cpu ( 16, value )
116
+#define be32_to_cpu( value ) __beNN_to_cpu ( 32, value )
117
+#define be64_to_cpu( value ) __beNN_to_cpu ( 64, value )
118
+#define cpu_to_le16s( ptr ) __cpu_to_leNNs ( 16, ptr )
119
+#define cpu_to_le32s( ptr ) __cpu_to_leNNs ( 32, ptr )
120
+#define cpu_to_le64s( ptr ) __cpu_to_leNNs ( 64, ptr )
121
+#define cpu_to_be16s( ptr ) __cpu_to_beNNs ( 16, ptr )
122
+#define cpu_to_be32s( ptr ) __cpu_to_beNNs ( 32, ptr )
123
+#define cpu_to_be64s( ptr ) __cpu_to_beNNs ( 64, ptr )
124
+#define le16_to_cpus( ptr ) __leNN_to_cpus ( 16, ptr )
125
+#define le32_to_cpus( ptr ) __leNN_to_cpus ( 32, ptr )
126
+#define le64_to_cpus( ptr ) __leNN_to_cpus ( 64, ptr )
127
+#define be16_to_cpus( ptr ) __beNN_to_cpus ( 16, ptr )
128
+#define be32_to_cpus( ptr ) __beNN_to_cpus ( 32, ptr )
129
+#define be64_to_cpus( ptr ) __beNN_to_cpus ( 64, ptr )
130
+
131
+#define htonll( value ) cpu_to_be64 (value)
132
+#define ntohll( value ) be64_to_cpu (value)
133
+#define htonl( value ) cpu_to_be32 (value)
134
+#define ntohl( value ) be32_to_cpu (value)
135
+#define htons( value ) cpu_to_be16 (value)
136
+#define ntohs( value ) be16_to_cpu (value)
137
+
138
+#endif /* BYTESWAP_H */

+ 16
- 15
src/include/endian.h View File

@@ -1,21 +1,22 @@
1
-#ifndef ETHERBOOT_ENDIAN_H
2
-#define ETHERBOOT_ENDIAN_H
1
+#ifndef _ENDIAN_H
2
+#define _ENDIAN_H
3 3
 
4
-FILE_LICENCE ( GPL2_OR_LATER );
4
+FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
5 5
 
6
-/* Definitions for byte order, according to significance of bytes,
7
-   from low addresses to high addresses.  The value is what you get by
8
-   putting '4' in the most significant byte, '3' in the second most
9
-   significant byte, '2' in the second least significant byte, and '1'
10
-   in the least significant byte, and then writing down one digit for
11
-   each byte, starting with the byte at the lowest address at the left,
12
-   and proceeding to the byte with the highest address at the right.  */
6
+/** Constant representing little-endian byte order
7
+ *
8
+ * Little-endian systems should define BYTE_ORDER as LITTLE_ENDIAN.
9
+ * This constant is intended to be used only at compile time.
10
+ */
11
+#define __LITTLE_ENDIAN 0x44332211UL
13 12
 
14
-#define __LITTLE_ENDIAN 1234
15
-#define __BIG_ENDIAN    4321
16
-#define __PDP_ENDIAN    3412
13
+/** Constant representing big-endian byte order
14
+ *
15
+ * Big-endian systems should define BYTE_ORDER as BIG_ENDIAN.
16
+ * This constant is intended to be used only at compile time.
17
+ */
18
+#define __BIG_ENDIAN 0x11223344UL
17 19
 
18 20
 #include "bits/endian.h"
19 21
 
20
-
21
-#endif /* ETHERBOOT_ENDIAN_H */
22
+#endif /* _ENDIAN_H */

+ 0
- 37
src/include/little_bswap.h View File

@@ -1,37 +0,0 @@
1
-#ifndef ETHERBOOT_LITTLE_BSWAP_H
2
-#define ETHERBOOT_LITTLE_BSWAP_H
3
-
4
-FILE_LICENCE ( GPL2_OR_LATER );
5
-
6
-#define htonll(x)	__bswap_64(x)
7
-#define ntohll(x)	__bswap_64(x)
8
-#define ntohl(x)	__bswap_32(x)
9
-#define htonl(x) 	__bswap_32(x)
10
-#define ntohs(x) 	__bswap_16(x)
11
-#define htons(x) 	__bswap_16(x)
12
-#define cpu_to_le64(x)	(x)
13
-#define cpu_to_le32(x)	(x)
14
-#define cpu_to_le16(x)	(x)
15
-#define cpu_to_be64(x)	__bswap_64(x)
16
-#define cpu_to_be32(x)	__bswap_32(x)
17
-#define cpu_to_be16(x)	__bswap_16(x)
18
-#define le64_to_cpu(x)	(x)
19
-#define le32_to_cpu(x)	(x)
20
-#define le16_to_cpu(x)	(x)
21
-#define be64_to_cpu(x)	__bswap_64(x)
22
-#define be32_to_cpu(x)	__bswap_32(x)
23
-#define be16_to_cpu(x)	__bswap_16(x)
24
-#define cpu_to_le64s(x) do {} while (0)
25
-#define cpu_to_le32s(x) do {} while (0)
26
-#define cpu_to_le16s(x) do {} while (0)
27
-#define cpu_to_be64s(x) __bswap_64s(x)
28
-#define cpu_to_be32s(x) __bswap_32s(x)
29
-#define cpu_to_be16s(x) __bswap_16s(x)
30
-#define le64_to_cpus(x) do {} while (0)
31
-#define le32_to_cpus(x) do {} while (0)
32
-#define le16_to_cpus(x) do {} while (0)
33
-#define be64_to_cpus(x) __bswap_64s(x)
34
-#define be32_to_cpus(x) __bswap_32s(x)
35
-#define be16_to_cpus(x) __bswap_16s(x)
36
-
37
-#endif /* ETHERBOOT_LITTLE_BSWAP_H */

+ 1
- 1
src/net/80211/wpa_tkip.c View File

@@ -136,7 +136,7 @@ static const u16 Sbox[256] = {
136 136
  */
137 137
 static inline u16 S ( u16 v )
138 138
 {
139
-	return Sbox[v & 0xFF] ^ swap16 ( Sbox[v >> 8] );
139
+	return Sbox[v & 0xFF] ^ bswap_16 ( Sbox[v >> 8] );
140 140
 }
141 141
 
142 142
 /**

Loading…
Cancel
Save