1 /******************************************************************************
2  *
3  * Module Name: tbutils   - table utilities
4  *
5  *****************************************************************************/
6 
7 /*
8  * Copyright (C) 2000 - 2012, 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 <acpi/acpi.h>
45 #include "accommon.h"
46 #include "actables.h"
47 
48 #define _COMPONENT          ACPI_TABLES
49 ACPI_MODULE_NAME("tbutils")
50 
51 /* Local prototypes */
52 static void acpi_tb_fix_string(char *string, acpi_size length);
53 
54 static void
55 acpi_tb_cleanup_table_header(struct acpi_table_header *out_header,
56 			     struct acpi_table_header *header);
57 
58 static acpi_physical_address
59 acpi_tb_get_root_table_entry(u8 *table_entry, u32 table_entry_size);
60 
61 /*******************************************************************************
62  *
63  * FUNCTION:    acpi_tb_check_xsdt
64  *
65  * PARAMETERS:  address                    - Pointer to the XSDT
66  *
67  * RETURN:      status
68  *		AE_OK - XSDT is okay
69  *		AE_NO_MEMORY - can't map XSDT
70  *		AE_INVALID_TABLE_LENGTH - invalid table length
71  *		AE_NULL_ENTRY - XSDT has NULL entry
72  *
73  * DESCRIPTION: validate XSDT
74 ******************************************************************************/
75 
76 static acpi_status
acpi_tb_check_xsdt(acpi_physical_address address)77 acpi_tb_check_xsdt(acpi_physical_address address)
78 {
79 	struct acpi_table_header *table;
80 	u32 length;
81 	u64 xsdt_entry_address;
82 	u8 *table_entry;
83 	u32 table_count;
84 	int i;
85 
86 	table = acpi_os_map_memory(address, sizeof(struct acpi_table_header));
87 	if (!table)
88 		return AE_NO_MEMORY;
89 
90 	length = table->length;
91 	acpi_os_unmap_memory(table, sizeof(struct acpi_table_header));
92 	if (length < sizeof(struct acpi_table_header))
93 		return AE_INVALID_TABLE_LENGTH;
94 
95 	table = acpi_os_map_memory(address, length);
96 	if (!table)
97 		return AE_NO_MEMORY;
98 
99 	/* Calculate the number of tables described in XSDT */
100 	table_count =
101 		(u32) ((table->length -
102 		sizeof(struct acpi_table_header)) / sizeof(u64));
103 	table_entry =
104 		ACPI_CAST_PTR(u8, table) + sizeof(struct acpi_table_header);
105 	for (i = 0; i < table_count; i++) {
106 		ACPI_MOVE_64_TO_64(&xsdt_entry_address, table_entry);
107 		if (!xsdt_entry_address) {
108 			/* XSDT has NULL entry */
109 			break;
110 		}
111 		table_entry += sizeof(u64);
112 	}
113 	acpi_os_unmap_memory(table, length);
114 
115 	if (i < table_count)
116 		return AE_NULL_ENTRY;
117 	else
118 		return AE_OK;
119 }
120 
121 #if (!ACPI_REDUCED_HARDWARE)
122 /*******************************************************************************
123  *
124  * FUNCTION:    acpi_tb_initialize_facs
125  *
126  * PARAMETERS:  None
127  *
128  * RETURN:      Status
129  *
130  * DESCRIPTION: Create a permanent mapping for the FADT and save it in a global
131  *              for accessing the Global Lock and Firmware Waking Vector
132  *
133  ******************************************************************************/
134 
acpi_tb_initialize_facs(void)135 acpi_status acpi_tb_initialize_facs(void)
136 {
137 	acpi_status status;
138 
139 	/* If Hardware Reduced flag is set, there is no FACS */
140 
141 	if (acpi_gbl_reduced_hardware) {
142 		acpi_gbl_FACS = NULL;
143 		return (AE_OK);
144 	}
145 
146 	status = acpi_get_table_by_index(ACPI_TABLE_INDEX_FACS,
147 					 ACPI_CAST_INDIRECT_PTR(struct
148 								acpi_table_header,
149 								&acpi_gbl_FACS));
150 	return status;
151 }
152 #endif				/* !ACPI_REDUCED_HARDWARE */
153 
154 /*******************************************************************************
155  *
156  * FUNCTION:    acpi_tb_tables_loaded
157  *
158  * PARAMETERS:  None
159  *
160  * RETURN:      TRUE if required ACPI tables are loaded
161  *
162  * DESCRIPTION: Determine if the minimum required ACPI tables are present
163  *              (FADT, FACS, DSDT)
164  *
165  ******************************************************************************/
166 
acpi_tb_tables_loaded(void)167 u8 acpi_tb_tables_loaded(void)
168 {
169 
170 	if (acpi_gbl_root_table_list.current_table_count >= 3) {
171 		return (TRUE);
172 	}
173 
174 	return (FALSE);
175 }
176 
177 /*******************************************************************************
178  *
179  * FUNCTION:    acpi_tb_fix_string
180  *
181  * PARAMETERS:  String              - String to be repaired
182  *              Length              - Maximum length
183  *
184  * RETURN:      None
185  *
186  * DESCRIPTION: Replace every non-printable or non-ascii byte in the string
187  *              with a question mark '?'.
188  *
189  ******************************************************************************/
190 
acpi_tb_fix_string(char * string,acpi_size length)191 static void acpi_tb_fix_string(char *string, acpi_size length)
192 {
193 
194 	while (length && *string) {
195 		if (!ACPI_IS_PRINT(*string)) {
196 			*string = '?';
197 		}
198 		string++;
199 		length--;
200 	}
201 }
202 
203 /*******************************************************************************
204  *
205  * FUNCTION:    acpi_tb_cleanup_table_header
206  *
207  * PARAMETERS:  out_header          - Where the cleaned header is returned
208  *              Header              - Input ACPI table header
209  *
210  * RETURN:      Returns the cleaned header in out_header
211  *
212  * DESCRIPTION: Copy the table header and ensure that all "string" fields in
213  *              the header consist of printable characters.
214  *
215  ******************************************************************************/
216 
217 static void
acpi_tb_cleanup_table_header(struct acpi_table_header * out_header,struct acpi_table_header * header)218 acpi_tb_cleanup_table_header(struct acpi_table_header *out_header,
219 			     struct acpi_table_header *header)
220 {
221 
222 	ACPI_MEMCPY(out_header, header, sizeof(struct acpi_table_header));
223 
224 	acpi_tb_fix_string(out_header->signature, ACPI_NAME_SIZE);
225 	acpi_tb_fix_string(out_header->oem_id, ACPI_OEM_ID_SIZE);
226 	acpi_tb_fix_string(out_header->oem_table_id, ACPI_OEM_TABLE_ID_SIZE);
227 	acpi_tb_fix_string(out_header->asl_compiler_id, ACPI_NAME_SIZE);
228 }
229 
230 /*******************************************************************************
231  *
232  * FUNCTION:    acpi_tb_print_table_header
233  *
234  * PARAMETERS:  Address             - Table physical address
235  *              Header              - Table header
236  *
237  * RETURN:      None
238  *
239  * DESCRIPTION: Print an ACPI table header. Special cases for FACS and RSDP.
240  *
241  ******************************************************************************/
242 
243 void
acpi_tb_print_table_header(acpi_physical_address address,struct acpi_table_header * header)244 acpi_tb_print_table_header(acpi_physical_address address,
245 			   struct acpi_table_header *header)
246 {
247 	struct acpi_table_header local_header;
248 
249 	/*
250 	 * The reason that the Address is cast to a void pointer is so that we
251 	 * can use %p which will work properly on both 32-bit and 64-bit hosts.
252 	 */
253 	if (ACPI_COMPARE_NAME(header->signature, ACPI_SIG_FACS)) {
254 
255 		/* FACS only has signature and length fields */
256 
257 		ACPI_INFO((AE_INFO, "%4.4s %p %05X",
258 			   header->signature, ACPI_CAST_PTR(void, address),
259 			   header->length));
260 	} else if (ACPI_COMPARE_NAME(header->signature, ACPI_SIG_RSDP)) {
261 
262 		/* RSDP has no common fields */
263 
264 		ACPI_MEMCPY(local_header.oem_id,
265 			    ACPI_CAST_PTR(struct acpi_table_rsdp,
266 					  header)->oem_id, ACPI_OEM_ID_SIZE);
267 		acpi_tb_fix_string(local_header.oem_id, ACPI_OEM_ID_SIZE);
268 
269 		ACPI_INFO((AE_INFO, "RSDP %p %05X (v%.2d %6.6s)",
270 			   ACPI_CAST_PTR (void, address),
271 			   (ACPI_CAST_PTR(struct acpi_table_rsdp, header)->
272 			    revision >
273 			    0) ? ACPI_CAST_PTR(struct acpi_table_rsdp,
274 					       header)->length : 20,
275 			   ACPI_CAST_PTR(struct acpi_table_rsdp,
276 					 header)->revision,
277 			   local_header.oem_id));
278 	} else {
279 		/* Standard ACPI table with full common header */
280 
281 		acpi_tb_cleanup_table_header(&local_header, header);
282 
283 		ACPI_INFO((AE_INFO,
284 			   "%4.4s %p %05X (v%.2d %6.6s %8.8s %08X %4.4s %08X)",
285 			   local_header.signature, ACPI_CAST_PTR(void, address),
286 			   local_header.length, local_header.revision,
287 			   local_header.oem_id, local_header.oem_table_id,
288 			   local_header.oem_revision,
289 			   local_header.asl_compiler_id,
290 			   local_header.asl_compiler_revision));
291 
292 	}
293 }
294 
295 /*******************************************************************************
296  *
297  * FUNCTION:    acpi_tb_validate_checksum
298  *
299  * PARAMETERS:  Table               - ACPI table to verify
300  *              Length              - Length of entire table
301  *
302  * RETURN:      Status
303  *
304  * DESCRIPTION: Verifies that the table checksums to zero. Optionally returns
305  *              exception on bad checksum.
306  *
307  ******************************************************************************/
308 
acpi_tb_verify_checksum(struct acpi_table_header * table,u32 length)309 acpi_status acpi_tb_verify_checksum(struct acpi_table_header *table, u32 length)
310 {
311 	u8 checksum;
312 
313 	/* Compute the checksum on the table */
314 
315 	checksum = acpi_tb_checksum(ACPI_CAST_PTR(u8, table), length);
316 
317 	/* Checksum ok? (should be zero) */
318 
319 	if (checksum) {
320 		ACPI_WARNING((AE_INFO,
321 			      "Incorrect checksum in table [%4.4s] - 0x%2.2X, should be 0x%2.2X",
322 			      table->signature, table->checksum,
323 			      (u8) (table->checksum - checksum)));
324 
325 #if (ACPI_CHECKSUM_ABORT)
326 
327 		return (AE_BAD_CHECKSUM);
328 #endif
329 	}
330 
331 	return (AE_OK);
332 }
333 
334 /*******************************************************************************
335  *
336  * FUNCTION:    acpi_tb_checksum
337  *
338  * PARAMETERS:  Buffer          - Pointer to memory region to be checked
339  *              Length          - Length of this memory region
340  *
341  * RETURN:      Checksum (u8)
342  *
343  * DESCRIPTION: Calculates circular checksum of memory region.
344  *
345  ******************************************************************************/
346 
acpi_tb_checksum(u8 * buffer,u32 length)347 u8 acpi_tb_checksum(u8 *buffer, u32 length)
348 {
349 	u8 sum = 0;
350 	u8 *end = buffer + length;
351 
352 	while (buffer < end) {
353 		sum = (u8) (sum + *(buffer++));
354 	}
355 
356 	return sum;
357 }
358 
359 /*******************************************************************************
360  *
361  * FUNCTION:    acpi_tb_check_dsdt_header
362  *
363  * PARAMETERS:  None
364  *
365  * RETURN:      None
366  *
367  * DESCRIPTION: Quick compare to check validity of the DSDT. This will detect
368  *              if the DSDT has been replaced from outside the OS and/or if
369  *              the DSDT header has been corrupted.
370  *
371  ******************************************************************************/
372 
acpi_tb_check_dsdt_header(void)373 void acpi_tb_check_dsdt_header(void)
374 {
375 
376 	/* Compare original length and checksum to current values */
377 
378 	if (acpi_gbl_original_dsdt_header.length != acpi_gbl_DSDT->length ||
379 	    acpi_gbl_original_dsdt_header.checksum != acpi_gbl_DSDT->checksum) {
380 		ACPI_ERROR((AE_INFO,
381 			    "The DSDT has been corrupted or replaced - old, new headers below"));
382 		acpi_tb_print_table_header(0, &acpi_gbl_original_dsdt_header);
383 		acpi_tb_print_table_header(0, acpi_gbl_DSDT);
384 
385 		ACPI_ERROR((AE_INFO,
386 			    "Please send DMI info to linux-acpi@vger.kernel.org\n"
387 			    "If system does not work as expected, please boot with acpi=copy_dsdt"));
388 
389 		/* Disable further error messages */
390 
391 		acpi_gbl_original_dsdt_header.length = acpi_gbl_DSDT->length;
392 		acpi_gbl_original_dsdt_header.checksum =
393 		    acpi_gbl_DSDT->checksum;
394 	}
395 }
396 
397 /*******************************************************************************
398  *
399  * FUNCTION:    acpi_tb_copy_dsdt
400  *
401  * PARAMETERS:  table_desc          - Installed table to copy
402  *
403  * RETURN:      None
404  *
405  * DESCRIPTION: Implements a subsystem option to copy the DSDT to local memory.
406  *              Some very bad BIOSs are known to either corrupt the DSDT or
407  *              install a new, bad DSDT. This copy works around the problem.
408  *
409  ******************************************************************************/
410 
acpi_tb_copy_dsdt(u32 table_index)411 struct acpi_table_header *acpi_tb_copy_dsdt(u32 table_index)
412 {
413 	struct acpi_table_header *new_table;
414 	struct acpi_table_desc *table_desc;
415 
416 	table_desc = &acpi_gbl_root_table_list.tables[table_index];
417 
418 	new_table = ACPI_ALLOCATE(table_desc->length);
419 	if (!new_table) {
420 		ACPI_ERROR((AE_INFO, "Could not copy DSDT of length 0x%X",
421 			    table_desc->length));
422 		return (NULL);
423 	}
424 
425 	ACPI_MEMCPY(new_table, table_desc->pointer, table_desc->length);
426 	acpi_tb_delete_table(table_desc);
427 	table_desc->pointer = new_table;
428 	table_desc->flags = ACPI_TABLE_ORIGIN_ALLOCATED;
429 
430 	ACPI_INFO((AE_INFO,
431 		   "Forced DSDT copy: length 0x%05X copied locally, original unmapped",
432 		   new_table->length));
433 
434 	return (new_table);
435 }
436 
437 /*******************************************************************************
438  *
439  * FUNCTION:    acpi_tb_install_table
440  *
441  * PARAMETERS:  Address                 - Physical address of DSDT or FACS
442  *              Signature               - Table signature, NULL if no need to
443  *                                        match
444  *              table_index             - Index into root table array
445  *
446  * RETURN:      None
447  *
448  * DESCRIPTION: Install an ACPI table into the global data structure. The
449  *              table override mechanism is called to allow the host
450  *              OS to replace any table before it is installed in the root
451  *              table array.
452  *
453  ******************************************************************************/
454 
455 void
acpi_tb_install_table(acpi_physical_address address,char * signature,u32 table_index)456 acpi_tb_install_table(acpi_physical_address address,
457 		      char *signature, u32 table_index)
458 {
459 	struct acpi_table_header *table;
460 	struct acpi_table_header *final_table;
461 	struct acpi_table_desc *table_desc;
462 
463 	if (!address) {
464 		ACPI_ERROR((AE_INFO,
465 			    "Null physical address for ACPI table [%s]",
466 			    signature));
467 		return;
468 	}
469 
470 	/* Map just the table header */
471 
472 	table = acpi_os_map_memory(address, sizeof(struct acpi_table_header));
473 	if (!table) {
474 		ACPI_ERROR((AE_INFO,
475 			    "Could not map memory for table [%s] at %p",
476 			    signature, ACPI_CAST_PTR(void, address)));
477 		return;
478 	}
479 
480 	/* If a particular signature is expected (DSDT/FACS), it must match */
481 
482 	if (signature && !ACPI_COMPARE_NAME(table->signature, signature)) {
483 		ACPI_ERROR((AE_INFO,
484 			    "Invalid signature 0x%X for ACPI table, expected [%s]",
485 			    *ACPI_CAST_PTR(u32, table->signature), signature));
486 		goto unmap_and_exit;
487 	}
488 
489 	/*
490 	 * Initialize the table entry. Set the pointer to NULL, since the
491 	 * table is not fully mapped at this time.
492 	 */
493 	table_desc = &acpi_gbl_root_table_list.tables[table_index];
494 
495 	table_desc->address = address;
496 	table_desc->pointer = NULL;
497 	table_desc->length = table->length;
498 	table_desc->flags = ACPI_TABLE_ORIGIN_MAPPED;
499 	ACPI_MOVE_32_TO_32(table_desc->signature.ascii, table->signature);
500 
501 	/*
502 	 * ACPI Table Override:
503 	 *
504 	 * Before we install the table, let the host OS override it with a new
505 	 * one if desired. Any table within the RSDT/XSDT can be replaced,
506 	 * including the DSDT which is pointed to by the FADT.
507 	 *
508 	 * NOTE: If the table is overridden, then final_table will contain a
509 	 * mapped pointer to the full new table. If the table is not overridden,
510 	 * or if there has been a physical override, then the table will be
511 	 * fully mapped later (in verify table). In any case, we must
512 	 * unmap the header that was mapped above.
513 	 */
514 	final_table = acpi_tb_table_override(table, table_desc);
515 	if (!final_table) {
516 		final_table = table;	/* There was no override */
517 	}
518 
519 	acpi_tb_print_table_header(table_desc->address, final_table);
520 
521 	/* Set the global integer width (based upon revision of the DSDT) */
522 
523 	if (table_index == ACPI_TABLE_INDEX_DSDT) {
524 		acpi_ut_set_integer_width(final_table->revision);
525 	}
526 
527 	/*
528 	 * If we have a physical override during this early loading of the ACPI
529 	 * tables, unmap the table for now. It will be mapped again later when
530 	 * it is actually used. This supports very early loading of ACPI tables,
531 	 * before virtual memory is fully initialized and running within the
532 	 * host OS. Note: A logical override has the ACPI_TABLE_ORIGIN_OVERRIDE
533 	 * flag set and will not be deleted below.
534 	 */
535 	if (final_table != table) {
536 		acpi_tb_delete_table(table_desc);
537 	}
538 
539       unmap_and_exit:
540 
541 	/* Always unmap the table header that we mapped above */
542 
543 	acpi_os_unmap_memory(table, sizeof(struct acpi_table_header));
544 }
545 
546 /*******************************************************************************
547  *
548  * FUNCTION:    acpi_tb_get_root_table_entry
549  *
550  * PARAMETERS:  table_entry         - Pointer to the RSDT/XSDT table entry
551  *              table_entry_size    - sizeof 32 or 64 (RSDT or XSDT)
552  *
553  * RETURN:      Physical address extracted from the root table
554  *
555  * DESCRIPTION: Get one root table entry. Handles 32-bit and 64-bit cases on
556  *              both 32-bit and 64-bit platforms
557  *
558  * NOTE:        acpi_physical_address is 32-bit on 32-bit platforms, 64-bit on
559  *              64-bit platforms.
560  *
561  ******************************************************************************/
562 
563 static acpi_physical_address
acpi_tb_get_root_table_entry(u8 * table_entry,u32 table_entry_size)564 acpi_tb_get_root_table_entry(u8 *table_entry, u32 table_entry_size)
565 {
566 	u64 address64;
567 
568 	/*
569 	 * Get the table physical address (32-bit for RSDT, 64-bit for XSDT):
570 	 * Note: Addresses are 32-bit aligned (not 64) in both RSDT and XSDT
571 	 */
572 	if (table_entry_size == sizeof(u32)) {
573 		/*
574 		 * 32-bit platform, RSDT: Return 32-bit table entry
575 		 * 64-bit platform, RSDT: Expand 32-bit to 64-bit and return
576 		 */
577 		return ((acpi_physical_address)
578 			(*ACPI_CAST_PTR(u32, table_entry)));
579 	} else {
580 		/*
581 		 * 32-bit platform, XSDT: Truncate 64-bit to 32-bit and return
582 		 * 64-bit platform, XSDT: Move (unaligned) 64-bit to local,
583 		 *  return 64-bit
584 		 */
585 		ACPI_MOVE_64_TO_64(&address64, table_entry);
586 
587 #if ACPI_MACHINE_WIDTH == 32
588 		if (address64 > ACPI_UINT32_MAX) {
589 
590 			/* Will truncate 64-bit address to 32 bits, issue warning */
591 
592 			ACPI_WARNING((AE_INFO,
593 				      "64-bit Physical Address in XSDT is too large (0x%8.8X%8.8X),"
594 				      " truncating",
595 				      ACPI_FORMAT_UINT64(address64)));
596 		}
597 #endif
598 		return ((acpi_physical_address) (address64));
599 	}
600 }
601 
602 /*******************************************************************************
603  *
604  * FUNCTION:    acpi_tb_parse_root_table
605  *
606  * PARAMETERS:  Rsdp                    - Pointer to the RSDP
607  *
608  * RETURN:      Status
609  *
610  * DESCRIPTION: This function is called to parse the Root System Description
611  *              Table (RSDT or XSDT)
612  *
613  * NOTE:        Tables are mapped (not copied) for efficiency. The FACS must
614  *              be mapped and cannot be copied because it contains the actual
615  *              memory location of the ACPI Global Lock.
616  *
617  ******************************************************************************/
618 
619 acpi_status __init
acpi_tb_parse_root_table(acpi_physical_address rsdp_address)620 acpi_tb_parse_root_table(acpi_physical_address rsdp_address)
621 {
622 	struct acpi_table_rsdp *rsdp;
623 	u32 table_entry_size;
624 	u32 i;
625 	u32 table_count;
626 	struct acpi_table_header *table;
627 	acpi_physical_address address;
628 	acpi_physical_address uninitialized_var(rsdt_address);
629 	u32 length;
630 	u8 *table_entry;
631 	acpi_status status;
632 
633 	ACPI_FUNCTION_TRACE(tb_parse_root_table);
634 
635 	/*
636 	 * Map the entire RSDP and extract the address of the RSDT or XSDT
637 	 */
638 	rsdp = acpi_os_map_memory(rsdp_address, sizeof(struct acpi_table_rsdp));
639 	if (!rsdp) {
640 		return_ACPI_STATUS(AE_NO_MEMORY);
641 	}
642 
643 	acpi_tb_print_table_header(rsdp_address,
644 				   ACPI_CAST_PTR(struct acpi_table_header,
645 						 rsdp));
646 
647 	/* Differentiate between RSDT and XSDT root tables */
648 
649 	if (rsdp->revision > 1 && rsdp->xsdt_physical_address
650 			&& !acpi_rsdt_forced) {
651 		/*
652 		 * Root table is an XSDT (64-bit physical addresses). We must use the
653 		 * XSDT if the revision is > 1 and the XSDT pointer is present, as per
654 		 * the ACPI specification.
655 		 */
656 		address = (acpi_physical_address) rsdp->xsdt_physical_address;
657 		table_entry_size = sizeof(u64);
658 		rsdt_address = (acpi_physical_address)
659 					rsdp->rsdt_physical_address;
660 	} else {
661 		/* Root table is an RSDT (32-bit physical addresses) */
662 
663 		address = (acpi_physical_address) rsdp->rsdt_physical_address;
664 		table_entry_size = sizeof(u32);
665 	}
666 
667 	/*
668 	 * It is not possible to map more than one entry in some environments,
669 	 * so unmap the RSDP here before mapping other tables
670 	 */
671 	acpi_os_unmap_memory(rsdp, sizeof(struct acpi_table_rsdp));
672 
673 	if (table_entry_size == sizeof(u64)) {
674 		if (acpi_tb_check_xsdt(address) == AE_NULL_ENTRY) {
675 			/* XSDT has NULL entry, RSDT is used */
676 			address = rsdt_address;
677 			table_entry_size = sizeof(u32);
678 			ACPI_WARNING((AE_INFO, "BIOS XSDT has NULL entry, "
679 					"using RSDT"));
680 		}
681 	}
682 	/* Map the RSDT/XSDT table header to get the full table length */
683 
684 	table = acpi_os_map_memory(address, sizeof(struct acpi_table_header));
685 	if (!table) {
686 		return_ACPI_STATUS(AE_NO_MEMORY);
687 	}
688 
689 	acpi_tb_print_table_header(address, table);
690 
691 	/* Get the length of the full table, verify length and map entire table */
692 
693 	length = table->length;
694 	acpi_os_unmap_memory(table, sizeof(struct acpi_table_header));
695 
696 	if (length < sizeof(struct acpi_table_header)) {
697 		ACPI_ERROR((AE_INFO, "Invalid length 0x%X in RSDT/XSDT",
698 			    length));
699 		return_ACPI_STATUS(AE_INVALID_TABLE_LENGTH);
700 	}
701 
702 	table = acpi_os_map_memory(address, length);
703 	if (!table) {
704 		return_ACPI_STATUS(AE_NO_MEMORY);
705 	}
706 
707 	/* Validate the root table checksum */
708 
709 	status = acpi_tb_verify_checksum(table, length);
710 	if (ACPI_FAILURE(status)) {
711 		acpi_os_unmap_memory(table, length);
712 		return_ACPI_STATUS(status);
713 	}
714 
715 	/* Calculate the number of tables described in the root table */
716 
717 	table_count = (u32)((table->length - sizeof(struct acpi_table_header)) /
718 			    table_entry_size);
719 	/*
720 	 * First two entries in the table array are reserved for the DSDT
721 	 * and FACS, which are not actually present in the RSDT/XSDT - they
722 	 * come from the FADT
723 	 */
724 	table_entry =
725 	    ACPI_CAST_PTR(u8, table) + sizeof(struct acpi_table_header);
726 	acpi_gbl_root_table_list.current_table_count = 2;
727 
728 	/*
729 	 * Initialize the root table array from the RSDT/XSDT
730 	 */
731 	for (i = 0; i < table_count; i++) {
732 		if (acpi_gbl_root_table_list.current_table_count >=
733 		    acpi_gbl_root_table_list.max_table_count) {
734 
735 			/* There is no more room in the root table array, attempt resize */
736 
737 			status = acpi_tb_resize_root_table_list();
738 			if (ACPI_FAILURE(status)) {
739 				ACPI_WARNING((AE_INFO,
740 					      "Truncating %u table entries!",
741 					      (unsigned) (table_count -
742 					       (acpi_gbl_root_table_list.
743 							  current_table_count -
744 							  2))));
745 				break;
746 			}
747 		}
748 
749 		/* Get the table physical address (32-bit for RSDT, 64-bit for XSDT) */
750 
751 		acpi_gbl_root_table_list.tables[acpi_gbl_root_table_list.
752 						current_table_count].address =
753 		    acpi_tb_get_root_table_entry(table_entry, table_entry_size);
754 
755 		table_entry += table_entry_size;
756 		acpi_gbl_root_table_list.current_table_count++;
757 	}
758 
759 	/*
760 	 * It is not possible to map more than one entry in some environments,
761 	 * so unmap the root table here before mapping other tables
762 	 */
763 	acpi_os_unmap_memory(table, length);
764 
765 	/*
766 	 * Complete the initialization of the root table array by examining
767 	 * the header of each table
768 	 */
769 	for (i = 2; i < acpi_gbl_root_table_list.current_table_count; i++) {
770 		acpi_tb_install_table(acpi_gbl_root_table_list.tables[i].
771 				      address, NULL, i);
772 
773 		/* Special case for FADT - get the DSDT and FACS */
774 
775 		if (ACPI_COMPARE_NAME
776 		    (&acpi_gbl_root_table_list.tables[i].signature,
777 		     ACPI_SIG_FADT)) {
778 			acpi_tb_parse_fadt(i);
779 		}
780 	}
781 
782 	return_ACPI_STATUS(AE_OK);
783 }
784