1 /** @file
2   Unaligned access functions of BaseLib.
3 
4   Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>
5   SPDX-License-Identifier: BSD-2-Clause-Patent
6 
7 **/
8 
9 
10 #include "BaseLibInternals.h"
11 
12 
13 /**
14   Reads a 16-bit value from memory that may be unaligned.
15 
16   This function returns the 16-bit value pointed to by Buffer. The function
17   guarantees that the read operation does not produce an alignment fault.
18 
19   If the Buffer is NULL, then ASSERT().
20 
21   @param  Buffer  A pointer to a 16-bit value that may be unaligned.
22 
23   @return The 16-bit value read from Buffer.
24 
25 **/
26 UINT16
27 EFIAPI
ReadUnaligned16(IN CONST UINT16 * Buffer)28 ReadUnaligned16 (
29   IN CONST UINT16              *Buffer
30   )
31 {
32   ASSERT (Buffer != NULL);
33 
34   return *Buffer;
35 }
36 
37 /**
38   Writes a 16-bit value to memory that may be unaligned.
39 
40   This function writes the 16-bit value specified by Value to Buffer. Value is
41   returned. The function guarantees that the write operation does not produce
42   an alignment fault.
43 
44   If the Buffer is NULL, then ASSERT().
45 
46   @param  Buffer  A pointer to a 16-bit value that may be unaligned.
47   @param  Value   16-bit value to write to Buffer.
48 
49   @return The 16-bit value to write to Buffer.
50 
51 **/
52 UINT16
53 EFIAPI
WriteUnaligned16(OUT UINT16 * Buffer,IN UINT16 Value)54 WriteUnaligned16 (
55   OUT UINT16                    *Buffer,
56   IN  UINT16                    Value
57   )
58 {
59   ASSERT (Buffer != NULL);
60 
61   return *Buffer = Value;
62 }
63 
64 /**
65   Reads a 24-bit value from memory that may be unaligned.
66 
67   This function returns the 24-bit value pointed to by Buffer. The function
68   guarantees that the read operation does not produce an alignment fault.
69 
70   If the Buffer is NULL, then ASSERT().
71 
72   @param  Buffer  A pointer to a 24-bit value that may be unaligned.
73 
74   @return The 24-bit value read from Buffer.
75 
76 **/
77 UINT32
78 EFIAPI
ReadUnaligned24(IN CONST UINT32 * Buffer)79 ReadUnaligned24 (
80   IN CONST UINT32              *Buffer
81   )
82 {
83   ASSERT (Buffer != NULL);
84 
85   return *Buffer & 0xffffff;
86 }
87 
88 /**
89   Writes a 24-bit value to memory that may be unaligned.
90 
91   This function writes the 24-bit value specified by Value to Buffer. Value is
92   returned. The function guarantees that the write operation does not produce
93   an alignment fault.
94 
95   If the Buffer is NULL, then ASSERT().
96 
97   @param  Buffer  A pointer to a 24-bit value that may be unaligned.
98   @param  Value   24-bit value to write to Buffer.
99 
100   @return The 24-bit value to write to Buffer.
101 
102 **/
103 UINT32
104 EFIAPI
WriteUnaligned24(OUT UINT32 * Buffer,IN UINT32 Value)105 WriteUnaligned24 (
106   OUT UINT32                    *Buffer,
107   IN  UINT32                    Value
108   )
109 {
110   ASSERT (Buffer != NULL);
111 
112   *Buffer = BitFieldWrite32 (*Buffer, 0, 23, Value);
113   return Value;
114 }
115 
116 /**
117   Reads a 32-bit value from memory that may be unaligned.
118 
119   This function returns the 32-bit value pointed to by Buffer. The function
120   guarantees that the read operation does not produce an alignment fault.
121 
122   If the Buffer is NULL, then ASSERT().
123 
124   @param  Buffer  A pointer to a 32-bit value that may be unaligned.
125 
126   @return The 32-bit value read from Buffer.
127 
128 **/
129 UINT32
130 EFIAPI
ReadUnaligned32(IN CONST UINT32 * Buffer)131 ReadUnaligned32 (
132   IN CONST UINT32              *Buffer
133   )
134 {
135   ASSERT (Buffer != NULL);
136 
137   return *Buffer;
138 }
139 
140 /**
141   Writes a 32-bit value to memory that may be unaligned.
142 
143   This function writes the 32-bit value specified by Value to Buffer. Value is
144   returned. The function guarantees that the write operation does not produce
145   an alignment fault.
146 
147   If the Buffer is NULL, then ASSERT().
148 
149   @param  Buffer  A pointer to a 32-bit value that may be unaligned.
150   @param  Value   The 32-bit value to write to Buffer.
151 
152   @return The 32-bit value to write to Buffer.
153 
154 **/
155 UINT32
156 EFIAPI
WriteUnaligned32(OUT UINT32 * Buffer,IN UINT32 Value)157 WriteUnaligned32 (
158   OUT UINT32                    *Buffer,
159   IN  UINT32                    Value
160   )
161 {
162   ASSERT (Buffer != NULL);
163 
164   return *Buffer = Value;
165 }
166 
167 /**
168   Reads a 64-bit value from memory that may be unaligned.
169 
170   This function returns the 64-bit value pointed to by Buffer. The function
171   guarantees that the read operation does not produce an alignment fault.
172 
173   If the Buffer is NULL, then ASSERT().
174 
175   @param  Buffer  A pointer to a 64-bit value that may be unaligned.
176 
177   @return The 64-bit value read from Buffer.
178 
179 **/
180 UINT64
181 EFIAPI
ReadUnaligned64(IN CONST UINT64 * Buffer)182 ReadUnaligned64 (
183   IN CONST UINT64              *Buffer
184   )
185 {
186   ASSERT (Buffer != NULL);
187 
188   return *Buffer;
189 }
190 
191 /**
192   Writes a 64-bit value to memory that may be unaligned.
193 
194   This function writes the 64-bit value specified by Value to Buffer. Value is
195   returned. The function guarantees that the write operation does not produce
196   an alignment fault.
197 
198   If the Buffer is NULL, then ASSERT().
199 
200   @param  Buffer  A pointer to a 64-bit value that may be unaligned.
201   @param  Value   The 64-bit value to write to Buffer.
202 
203   @return The 64-bit value to write to Buffer.
204 
205 **/
206 UINT64
207 EFIAPI
WriteUnaligned64(OUT UINT64 * Buffer,IN UINT64 Value)208 WriteUnaligned64 (
209   OUT UINT64                    *Buffer,
210   IN  UINT64                    Value
211   )
212 {
213   ASSERT (Buffer != NULL);
214 
215   return *Buffer = Value;
216 }
217