Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1pwDcN-0004n4-W0 for mharc-grub-devel@gnu.org; Mon, 08 May 2023 23:02:24 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pwDcJ-0004mk-Nk for grub-devel@gnu.org; Mon, 08 May 2023 23:02:20 -0400 Received: from gate.crashing.org ([63.228.1.57]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pwDcH-0001pv-No for grub-devel@gnu.org; Mon, 08 May 2023 23:02:19 -0400 Received: from [IPv6:::1] (localhost.localdomain [127.0.0.1]) by gate.crashing.org (8.14.1/8.14.1) with ESMTP id 34931BlA030095 for ; Mon, 8 May 2023 22:01:12 -0500 Message-ID: Subject: [PATCH] ns8250: Fix incorrect usage of access_size From: Benjamin Herrenschmidt To: "grub-devel@gnu.org" Date: Tue, 09 May 2023 13:01:11 +1000 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.44.4-0ubuntu1 MIME-Version: 1.0 Received-SPF: pass client-ip=63.228.1.57; envelope-from=benh@kernel.crashing.org; helo=gate.crashing.org X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 09 May 2023 03:02:21 -0000 access_size is part of a union, so doesn't technically exist for a PIO port (ie, not MMIO), but we set it anyways. This doesn't cause a bug today because the other leg of the union doesn't have anything overlapping with it now, but it's bad, I will punish myself for writing it that way :-) In the meantime, fix this and actually name the struct inside the union for clarity of intent and to avoid such issue in the future. Signed-off-by: Benjamin Herrenschmidt --- grub-core/term/ns8250.c | 42 ++++++++++++++++++++--------------------- include/grub/serial.h | 8 ++++---- 2 files changed, 24 insertions(+), 26 deletions(-) diff --git a/grub-core/term/ns8250.c b/grub-core/term/ns8250.c index fea7a45d2..23e8e0904 100644 --- a/grub-core/term/ns8250.c +++ b/grub-core/term/ns8250.c @@ -41,22 +41,22 @@ static grub_uint8_t ns8250_reg_read (struct grub_serial_port *port, grub_addr_t reg) { asm volatile("" : : : "memory"); - if (port->mmio =3D=3D true) + if (port->use_mmio =3D=3D true) { /* * Note: we assume MMIO UARTs are little endian. This is not true of= all * embedded platforms but will do for now. */ - switch(port->access_size) + switch(port->mmio.access_size) { default: /* ACPI tables occasionally uses "0" (legacy) as equivalent to "= 1" (byte). */ case 1: - return *((volatile grub_uint8_t *) (port->mmio_base + reg)); + return *((volatile grub_uint8_t *) (port->mmio.base + reg)); case 2: - return grub_le_to_cpu16 (*((volatile grub_uint16_t *) (port->mmi= o_base + (reg << 1)))); + return grub_le_to_cpu16 (*((volatile grub_uint16_t *) (port->mmi= o.base + (reg << 1)))); case 3: - return grub_le_to_cpu32 (*((volatile grub_uint32_t *) (port->mmi= o_base + (reg << 2)))); + return grub_le_to_cpu32 (*((volatile grub_uint32_t *) (port->mmi= o.base + (reg << 2)))); case 4: /* * This will only work properly on 64-bit systems since 64-bit @@ -64,7 +64,7 @@ ns8250_reg_read (struct grub_serial_port *port, grub_addr= _t reg) * case of a UART with a 64-bit register spacing on 32-bit * also probably doesn't exist. */ - return grub_le_to_cpu64 (*((volatile grub_uint64_t *) (port->mmi= o_base + (reg << 3)))); + return grub_le_to_cpu64 (*((volatile grub_uint64_t *) (port->mmi= o.base + (reg << 3)))); } } return grub_inb (port->port + reg); @@ -74,24 +74,24 @@ static void ns8250_reg_write (struct grub_serial_port *port, grub_uint8_t value, grub_= addr_t reg) { asm volatile("" : : : "memory"); - if (port->mmio =3D=3D true) + if (port->use_mmio =3D=3D true) { - switch(port->access_size) + switch(port->mmio.access_size) { default: /* ACPI tables occasionally uses "0" (legacy) as equivalent to "= 1" (byte). */ case 1: - *((volatile grub_uint8_t *) (port->mmio_base + reg)) =3D value; + *((volatile grub_uint8_t *) (port->mmio.base + reg)) =3D value; break; case 2: - *((volatile grub_uint16_t *) (port->mmio_base + (reg << 1))) =3D= grub_cpu_to_le16 (value); + *((volatile grub_uint16_t *) (port->mmio.base + (reg << 1))) =3D= grub_cpu_to_le16 (value); break; case 3: - *((volatile grub_uint32_t *) (port->mmio_base + (reg << 2))) =3D= grub_cpu_to_le32 (value); + *((volatile grub_uint32_t *) (port->mmio.base + (reg << 2))) =3D= grub_cpu_to_le32 (value); break; case 4: /* See commment in ns8250_reg_read(). */ - *((volatile grub_uint64_t *) (port->mmio_base + (reg << 3))) =3D= grub_cpu_to_le64 (value); + *((volatile grub_uint64_t *) (port->mmio.base + (reg << 3))) =3D= grub_cpu_to_le64 (value); break; } } @@ -323,13 +323,12 @@ grub_ns8250_init (void) com_ports[i].name =3D com_names[i]; com_ports[i].driver =3D &grub_ns8250_driver; com_ports[i].port =3D serial_hw_io_addr[i]; - com_ports[i].mmio =3D false; + com_ports[i].use_mmio =3D false; err =3D grub_serial_config_defaults (&com_ports[i]); if (err) grub_print_error (); =20 grub_serial_register (&com_ports[i]); - com_ports[i].access_size =3D 1; } } =20 @@ -365,7 +364,7 @@ grub_serial_ns8250_add_port (grub_port_t port, struct g= rub_serial_config *config } =20 FOR_SERIAL_PORTS (p) - if (p->mmio =3D=3D false && p->port =3D=3D port) + if (p->use_mmio =3D=3D false && p->port =3D=3D port) { if (config !=3D NULL) grub_serial_port_configure (p, config); @@ -390,9 +389,8 @@ grub_serial_ns8250_add_port (grub_port_t port, struct g= rub_serial_config *config return NULL; } p->driver =3D &grub_ns8250_driver; - p->mmio =3D false; + p->use_mmio =3D false; p->port =3D port; - p->access_size =3D 1; if (config !=3D NULL) grub_serial_port_configure (p, config); else @@ -410,7 +408,7 @@ grub_serial_ns8250_add_mmio (grub_addr_t addr, unsigned= int acc_size, unsigned i; =20 for (i =3D 0; i < GRUB_SERIAL_PORT_NUM; i++) - if (com_ports[i].mmio =3D=3D true && com_ports[i].mmio_base =3D=3D add= r) + if (com_ports[i].use_mmio =3D=3D true && com_ports[i].mmio.base =3D=3D= addr) { if (config !=3D NULL) grub_serial_port_configure (&com_ports[i], config); @@ -418,7 +416,7 @@ grub_serial_ns8250_add_mmio (grub_addr_t addr, unsigned= int acc_size, } =20 FOR_SERIAL_PORTS (p) - if (p->mmio =3D=3D true && p->mmio_base =3D=3D addr) + if (p->use_mmio =3D=3D true && p->mmio.base =3D=3D addr) { if (config !=3D NULL) grub_serial_port_configure (p, config); @@ -435,9 +433,9 @@ grub_serial_ns8250_add_mmio (grub_addr_t addr, unsigned= int acc_size, return NULL; } p->driver =3D &grub_ns8250_driver; - p->mmio =3D true; - p->mmio_base =3D addr; - p->access_size =3D acc_size; + p->use_mmio =3D true; + p->mmio.base =3D addr; + p->mmio.access_size =3D acc_size; if (config !=3D NULL) grub_serial_port_configure (p, config); else diff --git a/include/grub/serial.h b/include/grub/serial.h index a955c076d..c236f31b4 100644 --- a/include/grub/serial.h +++ b/include/grub/serial.h @@ -88,7 +88,7 @@ struct grub_serial_port { struct { - bool mmio; + bool use_mmio; union { #if defined(__mips__) || defined (__i386__) || defined (__x86_64__) @@ -96,10 +96,10 @@ struct grub_serial_port #endif struct { - grub_addr_t mmio_base; - /* Access size uses ACPI definition. */ + grub_addr_t base; + /* Access size uses ACPI definition */ grub_uint8_t access_size; - }; + } mmio; }; }; struct