dtfield.c revision 1.1.1.16 1 /******************************************************************************
2 *
3 * Module Name: dtfield.c - Code generation for individual source fields
4 *
5 *****************************************************************************/
6
7 /*
8 * Copyright (C) 2000 - 2019, Intel Corp.
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
29 *
30 * NO WARRANTY
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
42 */
43
44 #include "aslcompiler.h"
45
46 #define _COMPONENT DT_COMPILER
47 ACPI_MODULE_NAME ("dtfield")
48
49
50 /* Local prototypes */
51
52 static void
53 DtCompileString (
54 UINT8 *Buffer,
55 DT_FIELD *Field,
56 UINT32 ByteLength);
57
58 static void
59 DtCompileUnicode (
60 UINT8 *Buffer,
61 DT_FIELD *Field,
62 UINT32 ByteLength);
63
64 static ACPI_STATUS
65 DtCompileUuid (
66 UINT8 *Buffer,
67 DT_FIELD *Field,
68 UINT32 ByteLength);
69
70 static char *
71 DtNormalizeBuffer (
72 char *Buffer,
73 UINT32 *Count);
74
75
76 /******************************************************************************
77 *
78 * FUNCTION: DtCompileOneField
79 *
80 * PARAMETERS: Buffer - Output buffer
81 * Field - Field to be compiled
82 * ByteLength - Byte length of the field
83 * Type - Field type
84 *
85 * RETURN: None
86 *
87 * DESCRIPTION: Compile a field value to binary
88 *
89 *****************************************************************************/
90
91 void
92 DtCompileOneField (
93 UINT8 *Buffer,
94 DT_FIELD *Field,
95 UINT32 ByteLength,
96 UINT8 Type,
97 UINT8 Flags)
98 {
99 ACPI_STATUS Status;
100
101
102 switch (Type)
103 {
104 case DT_FIELD_TYPE_INTEGER:
105
106 DtCompileInteger (Buffer, Field, ByteLength, Flags);
107 break;
108
109 case DT_FIELD_TYPE_STRING:
110
111 DtCompileString (Buffer, Field, ByteLength);
112 break;
113
114 case DT_FIELD_TYPE_UUID:
115
116 Status = DtCompileUuid (Buffer, Field, ByteLength);
117 if (ACPI_SUCCESS (Status))
118 {
119 break;
120 }
121
122 /* Fall through. */
123
124 case DT_FIELD_TYPE_BUFFER:
125
126 DtCompileBuffer (Buffer, Field->Value, Field, ByteLength);
127 break;
128
129 case DT_FIELD_TYPE_UNICODE:
130
131 DtCompileUnicode (Buffer, Field, ByteLength);
132 break;
133
134 case DT_FIELD_TYPE_DEVICE_PATH:
135
136 break;
137
138 default:
139
140 DtFatal (ASL_MSG_COMPILER_INTERNAL, Field, "Invalid field type");
141 break;
142 }
143 }
144
145
146 /******************************************************************************
147 *
148 * FUNCTION: DtCompileString
149 *
150 * PARAMETERS: Buffer - Output buffer
151 * Field - String to be copied to buffer
152 * ByteLength - Maximum length of string
153 *
154 * RETURN: None
155 *
156 * DESCRIPTION: Copy string to the buffer
157 *
158 *****************************************************************************/
159
160 static void
161 DtCompileString (
162 UINT8 *Buffer,
163 DT_FIELD *Field,
164 UINT32 ByteLength)
165 {
166 UINT32 Length;
167
168
169 Length = strlen (Field->Value);
170
171 /* Check if the string is too long for the field */
172
173 if (Length > ByteLength)
174 {
175 sprintf (AslGbl_MsgBuffer,
176 "Maximum %u characters, found %u characters [%s]",
177 ByteLength, Length, Field->Value);
178 DtError (ASL_ERROR, ASL_MSG_STRING_LENGTH, Field, AslGbl_MsgBuffer);
179 Length = ByteLength;
180 }
181
182 memcpy (Buffer, Field->Value, Length);
183 }
184
185
186 /******************************************************************************
187 *
188 * FUNCTION: DtCompileUnicode
189 *
190 * PARAMETERS: Buffer - Output buffer
191 * Field - String to be copied to buffer
192 * ByteLength - Maximum length of string
193 *
194 * RETURN: None
195 *
196 * DESCRIPTION: Convert ASCII string to Unicode string
197 *
198 * Note: The Unicode string is 16 bits per character, no leading signature,
199 * with a 16-bit terminating NULL.
200 *
201 *****************************************************************************/
202
203 static void
204 DtCompileUnicode (
205 UINT8 *Buffer,
206 DT_FIELD *Field,
207 UINT32 ByteLength)
208 {
209 UINT32 Count;
210 UINT32 i;
211 char *AsciiString;
212 UINT16 *UnicodeString;
213
214
215 AsciiString = Field->Value;
216 UnicodeString = (UINT16 *) Buffer;
217 Count = strlen (AsciiString) + 1;
218
219 /* Convert to Unicode string (including null terminator) */
220
221 for (i = 0; i < Count; i++)
222 {
223 UnicodeString[i] = (UINT16) AsciiString[i];
224 }
225 }
226
227
228 /*******************************************************************************
229 *
230 * FUNCTION: DtCompileUuid
231 *
232 * PARAMETERS: Buffer - Output buffer
233 * Field - String to be copied to buffer
234 * ByteLength - Maximum length of string
235 *
236 * RETURN: None
237 *
238 * DESCRIPTION: Convert UUID string to 16-byte buffer
239 *
240 ******************************************************************************/
241
242 static ACPI_STATUS
243 DtCompileUuid (
244 UINT8 *Buffer,
245 DT_FIELD *Field,
246 UINT32 ByteLength)
247 {
248 char *InString;
249 ACPI_STATUS Status;
250
251
252 InString = Field->Value;
253
254 Status = AuValidateUuid (InString);
255 if (ACPI_FAILURE (Status))
256 {
257 sprintf (AslGbl_MsgBuffer, "%s", Field->Value);
258 DtNameError (ASL_ERROR, ASL_MSG_INVALID_UUID, Field, AslGbl_MsgBuffer);
259 }
260 else
261 {
262 AcpiUtConvertStringToUuid (InString, Buffer);
263 }
264
265 return (Status);
266 }
267
268
269 /******************************************************************************
270 *
271 * FUNCTION: DtCompileInteger
272 *
273 * PARAMETERS: Buffer - Output buffer
274 * Field - Field obj with Integer to be compiled
275 * ByteLength - Byte length of the integer
276 * Flags - Additional compile info
277 *
278 * RETURN: None
279 *
280 * DESCRIPTION: Compile an integer. Supports integer expressions with C-style
281 * operators.
282 *
283 *****************************************************************************/
284
285 void
286 DtCompileInteger (
287 UINT8 *Buffer,
288 DT_FIELD *Field,
289 UINT32 ByteLength,
290 UINT8 Flags)
291 {
292 UINT64 Value;
293 UINT64 MaxValue;
294 ACPI_STATUS Status;
295
296
297 /* Output buffer byte length must be in range 1-8 */
298
299 if ((ByteLength > 8) || (ByteLength == 0))
300 {
301 DtFatal (ASL_MSG_COMPILER_INTERNAL, Field,
302 "Invalid internal Byte length");
303 return;
304 }
305
306 /* Resolve integer expression to a single integer value */
307
308 Status = DtResolveIntegerExpression (Field, &Value);
309 if (ACPI_FAILURE (Status))
310 {
311 return;
312 }
313
314 /*
315 * Ensure that reserved fields are set properly. Note: uses
316 * the DT_NON_ZERO flag to indicate that the reserved value
317 * must be exactly one. Otherwise, the value must be zero.
318 * This is sufficient for now.
319 */
320
321 /* TBD: Should use a flag rather than compare "Reserved" */
322
323 if (!strcmp (Field->Name, "Reserved"))
324 {
325 if (Flags & DT_NON_ZERO)
326 {
327 if (Value != 1)
328 {
329 DtError (ASL_WARNING, ASL_MSG_RESERVED_VALUE, Field,
330 "Must be one, setting to one");
331 Value = 1;
332 }
333 }
334 else if (Value != 0)
335 {
336 DtError (ASL_WARNING, ASL_MSG_RESERVED_VALUE, Field,
337 "Must be zero, setting to zero");
338 Value = 0;
339 }
340 }
341
342 /* Check if the value must be non-zero */
343
344 else if ((Flags & DT_NON_ZERO) && (Value == 0))
345 {
346 DtError (ASL_ERROR, ASL_MSG_ZERO_VALUE, Field, NULL);
347 }
348
349 /*
350 * Generate the maximum value for the data type (ByteLength)
351 * Note: construct chosen for maximum portability
352 */
353 MaxValue = ((UINT64) (-1)) >> (64 - (ByteLength * 8));
354
355 /* Validate that the input value is within range of the target */
356
357 if (Value > MaxValue)
358 {
359 sprintf (AslGbl_MsgBuffer, "%8.8X%8.8X - max %u bytes",
360 ACPI_FORMAT_UINT64 (Value), ByteLength);
361 DtError (ASL_ERROR, ASL_MSG_INTEGER_SIZE, Field, AslGbl_MsgBuffer);
362 }
363
364 memcpy (Buffer, &Value, ByteLength);
365 return;
366 }
367
368
369 /******************************************************************************
370 *
371 * FUNCTION: DtNormalizeBuffer
372 *
373 * PARAMETERS: Buffer - Input buffer
374 * Count - Output the count of hex numbers in
375 * the Buffer
376 *
377 * RETURN: The normalized buffer, must be freed by caller
378 *
379 * DESCRIPTION: [1A,2B,3C,4D] or 1A, 2B, 3C, 4D will be normalized
380 * to 1A 2B 3C 4D
381 *
382 *****************************************************************************/
383
384 static char *
385 DtNormalizeBuffer (
386 char *Buffer,
387 UINT32 *Count)
388 {
389 char *NewBuffer;
390 char *TmpBuffer;
391 UINT32 BufferCount = 0;
392 BOOLEAN Separator = TRUE;
393 char c;
394
395
396 NewBuffer = UtLocalCalloc (strlen (Buffer) + 1);
397 TmpBuffer = NewBuffer;
398
399 while ((c = *Buffer++))
400 {
401 switch (c)
402 {
403 /* Valid separators */
404
405 case '[':
406 case ']':
407 case ' ':
408 case ',':
409
410 Separator = TRUE;
411 break;
412
413 default:
414
415 if (Separator)
416 {
417 /* Insert blank as the standard separator */
418
419 if (NewBuffer[0])
420 {
421 *TmpBuffer++ = ' ';
422 BufferCount++;
423 }
424
425 Separator = FALSE;
426 }
427
428 *TmpBuffer++ = c;
429 break;
430 }
431 }
432
433 *Count = BufferCount + 1;
434 return (NewBuffer);
435 }
436
437
438 /******************************************************************************
439 *
440 * FUNCTION: DtCompileBuffer
441 *
442 * PARAMETERS: Buffer - Output buffer
443 * StringValue - Integer list to be compiled
444 * Field - Current field object
445 * ByteLength - Byte length of the integer list
446 *
447 * RETURN: Count of remaining data in the input list
448 *
449 * DESCRIPTION: Compile and pack an integer list, for example
450 * "AA 1F 20 3B" ==> Buffer[] = {0xAA,0x1F,0x20,0x3B}
451 *
452 *****************************************************************************/
453
454 UINT32
455 DtCompileBuffer (
456 UINT8 *Buffer,
457 char *StringValue,
458 DT_FIELD *Field,
459 UINT32 ByteLength)
460 {
461 char *Substring;
462 ACPI_STATUS Status;
463 UINT32 Count;
464 UINT32 i;
465
466
467 /* Allow several different types of value separators */
468
469 StringValue = DtNormalizeBuffer (StringValue, &Count);
470 Substring = StringValue;
471
472 /* Each element of StringValue is now three chars (2 hex + 1 space) */
473
474 for (i = 0; i < Count; i++, Substring += 3)
475 {
476 /* Check for byte value too long */
477
478 if (*(&Substring[2]) &&
479 (*(&Substring[2]) != ' '))
480 {
481 DtError (ASL_ERROR, ASL_MSG_BUFFER_ELEMENT, Field, Substring);
482 goto Exit;
483 }
484
485 /* Convert two ASCII characters to one hex byte */
486
487 Status = AcpiUtAsciiToHexByte (Substring, &Buffer[i]);
488 if (ACPI_FAILURE (Status))
489 {
490 DtError (ASL_ERROR, ASL_MSG_BUFFER_ELEMENT, Field, Substring);
491 goto Exit;
492 }
493 }
494
495 Exit:
496 ACPI_FREE (StringValue);
497 return (ByteLength - Count);
498 }
499
500
501 /******************************************************************************
502 *
503 * FUNCTION: DtCompileFlag
504 *
505 * PARAMETERS: Buffer - Output buffer
506 * Field - Field to be compiled
507 * Info - Flag info
508 *
509 * RETURN: None
510 *
511 * DESCRIPTION: Compile a flag field. Handles flags up to 64 bits.
512 *
513 *****************************************************************************/
514
515 void
516 DtCompileFlag (
517 UINT8 *Buffer,
518 DT_FIELD *Field,
519 ACPI_DMTABLE_INFO *Info)
520 {
521 UINT64 Value = 0;
522 UINT32 BitLength = 1;
523 UINT8 BitPosition = 0;
524
525
526 Value = AcpiUtImplicitStrtoul64 (Field->Value);
527
528 switch (Info->Opcode)
529 {
530 case ACPI_DMT_FLAG0:
531 case ACPI_DMT_FLAG1:
532 case ACPI_DMT_FLAG2:
533 case ACPI_DMT_FLAG3:
534 case ACPI_DMT_FLAG4:
535 case ACPI_DMT_FLAG5:
536 case ACPI_DMT_FLAG6:
537 case ACPI_DMT_FLAG7:
538
539 BitPosition = Info->Opcode;
540 BitLength = 1;
541 break;
542
543 case ACPI_DMT_FLAGS0:
544
545 BitPosition = 0;
546 BitLength = 2;
547 break;
548
549
550 case ACPI_DMT_FLAGS1:
551
552 BitPosition = 1;
553 BitLength = 2;
554 break;
555
556
557 case ACPI_DMT_FLAGS2:
558
559 BitPosition = 2;
560 BitLength = 2;
561 break;
562
563 case ACPI_DMT_FLAGS4:
564
565 BitPosition = 4;
566 BitLength = 2;
567 break;
568
569 case ACPI_DMT_FLAGS4_0:
570
571 BitPosition = 0;
572 BitLength = 4;
573 break;
574
575 case ACPI_DMT_FLAGS4_4:
576
577 BitPosition = 4;
578 BitLength = 4;
579 break;
580
581 case ACPI_DMT_FLAGS4_8:
582
583 BitPosition = 8;
584 BitLength = 4;
585 break;
586
587 case ACPI_DMT_FLAGS4_12:
588
589 BitPosition = 12;
590 BitLength = 4;
591 break;
592
593 case ACPI_DMT_FLAGS16_16:
594
595 BitPosition = 16;
596 BitLength = 16;
597 break;
598
599 default:
600
601 DtFatal (ASL_MSG_COMPILER_INTERNAL, Field, "Invalid flag opcode");
602 break;
603 }
604
605 /* Check range of the input flag value */
606
607 if (Value >= ((UINT64) 1 << BitLength))
608 {
609 sprintf (AslGbl_MsgBuffer, "Maximum %u bit", BitLength);
610 DtError (ASL_ERROR, ASL_MSG_FLAG_VALUE, Field, AslGbl_MsgBuffer);
611 Value = 0;
612 }
613
614 *Buffer |= (UINT8) (Value << BitPosition);
615 }
616