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