mirror of
https://git.savannah.gnu.org/git/grub.git
synced 2026-10-21 06:49:35 +00:00
The GRUB is failing to build with GCC-12 in many places like this:
In function 'init_cbfsdisk',
inlined from 'grub_mod_init' at ../../grub-core/fs/cbfs.c:391:3:
../../grub-core/fs/cbfs.c:345:7: error: array subscript 0 is outside array bounds of 'grub_uint32_t[0]' {aka 'unsigned int[]'} [-Werror=array-bounds]
345 | ptr = *(grub_uint32_t *) 0xfffffffc;
| ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This is caused by GCC regression in 11/12 [1]. In a nut shell, the
warning is about detected invalid accesses at non-zero offsets to NULL
pointers. Since hardwired constant address is treated as NULL plus an
offset in the same underlying code, the warning is therefore triggered.
Instead of inserting #pragma all over the places where literal pointers
are accessed to avoid diagnosing array-bounds, we can try to borrow the
idea from Linux kernel that the absolute_pointer() macro [2][3] is used
to disconnect a pointer using literal address from it's original object,
hence GCC won't be able to make assumptions on the boundary while doing
pointer arithmetic. With that we can greatly reduce the code we have to
cover up by making initial literal pointer assignment to use the new
wrapper but not having to track everywhere literal pointers are
accessed. This also makes code looks cleaner.
Please note the grub_absolute_pointer() macro requires to be invoked in
a function as long as it is compound expression. Some global variables
with literal pointers has been changed to local ones in order to use
grub_absolute_pointer() to initialize it. The shuffling is basically done
in a selective and careful way that the variable's scope doesn't matter
being local or global, for example, the global variable must not get
modified at run time throughout. For the record, here's the list of
global variables got shuffled in this patch:
grub-core/commands/i386/pc/drivemap.c:int13slot
grub-core/term/i386/pc/console.c:bios_data_area
grub-core/term/ns8250.c:serial_hw_io_addr
[1] https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99578
[2] https://elixir.bootlin.com/linux/v5.16.14/source/include/linux/compiler.h#L180
[3] https://elixir.bootlin.com/linux/v5.16.14/source/include/linux/compiler-gcc.h#L31
Signed-off-by: Michael Chang <mchang@suse.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
84 lines
3.2 KiB
C
84 lines
3.2 KiB
C
/* acpi.c - get acpi tables. */
|
|
/*
|
|
* GRUB -- GRand Unified Bootloader
|
|
* Copyright (C) 2009 Free Software Foundation, Inc.
|
|
*
|
|
* GRUB is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* GRUB is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include <grub/acpi.h>
|
|
#include <grub/misc.h>
|
|
|
|
struct grub_acpi_rsdp_v10 *
|
|
grub_machine_acpi_get_rsdpv1 (void)
|
|
{
|
|
int ebda_len;
|
|
grub_uint8_t *ebda, *ptr;
|
|
|
|
grub_dprintf ("acpi", "Looking for RSDP. Scanning EBDA\n");
|
|
ebda = (grub_uint8_t *) ((* ((grub_uint16_t *) grub_absolute_pointer (0x40e))) << 4);
|
|
ebda_len = * (grub_uint16_t *) ebda;
|
|
if (! ebda_len) /* FIXME do we really need this check? */
|
|
goto scan_bios;
|
|
for (ptr = ebda; ptr < ebda + 0x400; ptr += 16)
|
|
if (grub_memcmp (ptr, GRUB_RSDP_SIGNATURE, GRUB_RSDP_SIGNATURE_SIZE) == 0
|
|
&& grub_byte_checksum (ptr, sizeof (struct grub_acpi_rsdp_v10)) == 0
|
|
&& ((struct grub_acpi_rsdp_v10 *) ptr)->revision == 0)
|
|
return (struct grub_acpi_rsdp_v10 *) ptr;
|
|
|
|
scan_bios:
|
|
grub_dprintf ("acpi", "Looking for RSDP. Scanning BIOS\n");
|
|
for (ptr = (grub_uint8_t *) 0xe0000; ptr < (grub_uint8_t *) 0x100000;
|
|
ptr += 16)
|
|
if (grub_memcmp (ptr, GRUB_RSDP_SIGNATURE, GRUB_RSDP_SIGNATURE_SIZE) == 0
|
|
&& grub_byte_checksum (ptr, sizeof (struct grub_acpi_rsdp_v10)) == 0
|
|
&& ((struct grub_acpi_rsdp_v10 *) ptr)->revision == 0)
|
|
return (struct grub_acpi_rsdp_v10 *) ptr;
|
|
return 0;
|
|
}
|
|
|
|
struct grub_acpi_rsdp_v20 *
|
|
grub_machine_acpi_get_rsdpv2 (void)
|
|
{
|
|
int ebda_len;
|
|
grub_uint8_t *ebda, *ptr;
|
|
|
|
grub_dprintf ("acpi", "Looking for RSDP. Scanning EBDA\n");
|
|
ebda = (grub_uint8_t *) ((* ((grub_uint16_t *) grub_absolute_pointer (0x40e))) << 4);
|
|
ebda_len = * (grub_uint16_t *) ebda;
|
|
if (! ebda_len) /* FIXME do we really need this check? */
|
|
goto scan_bios;
|
|
for (ptr = ebda; ptr < ebda + 0x400; ptr += 16)
|
|
if (grub_memcmp (ptr, GRUB_RSDP_SIGNATURE, GRUB_RSDP_SIGNATURE_SIZE) == 0
|
|
&& grub_byte_checksum (ptr, sizeof (struct grub_acpi_rsdp_v10)) == 0
|
|
&& ((struct grub_acpi_rsdp_v10 *) ptr)->revision != 0
|
|
&& ((struct grub_acpi_rsdp_v20 *) ptr)->length < 1024
|
|
&& grub_byte_checksum (ptr, ((struct grub_acpi_rsdp_v20 *) ptr)->length)
|
|
== 0)
|
|
return (struct grub_acpi_rsdp_v20 *) ptr;
|
|
|
|
scan_bios:
|
|
grub_dprintf ("acpi", "Looking for RSDP. Scanning BIOS\n");
|
|
for (ptr = (grub_uint8_t *) 0xe0000; ptr < (grub_uint8_t *) 0x100000;
|
|
ptr += 16)
|
|
if (grub_memcmp (ptr, GRUB_RSDP_SIGNATURE, GRUB_RSDP_SIGNATURE_SIZE) == 0
|
|
&& grub_byte_checksum (ptr, sizeof (struct grub_acpi_rsdp_v10)) == 0
|
|
&& ((struct grub_acpi_rsdp_v10 *) ptr)->revision != 0
|
|
&& ((struct grub_acpi_rsdp_v20 *) ptr)->length < 1024
|
|
&& grub_byte_checksum (ptr, ((struct grub_acpi_rsdp_v20 *) ptr)->length)
|
|
== 0)
|
|
return (struct grub_acpi_rsdp_v20 *) ptr;
|
|
return 0;
|
|
}
|