Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1p8EqS-0002Zz-6B for mharc-grub-devel@gnu.org; Thu, 22 Dec 2022 01:14:20 -0500 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 1p8EqP-0002ZF-P2 for grub-devel@gnu.org; Thu, 22 Dec 2022 01:14:18 -0500 Received: from gate.crashing.org ([63.228.1.57]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1p8EqN-0002hv-NG for grub-devel@gnu.org; Thu, 22 Dec 2022 01:14:17 -0500 Received: from u0f3226c756e75a.ant.amazon.com (localhost.localdomain [127.0.0.1]) by gate.crashing.org (8.14.1/8.14.1) with ESMTP id 2BM6CxBm025856; Thu, 22 Dec 2022 00:13:13 -0600 From: Benjamin Herrenschmidt To: grub-devel@gnu.org Cc: Daniel Kiper Subject: [PATCH v2 8/8] serial: Add ability to specify MMIO ports via 'serial' Date: Thu, 22 Dec 2022 17:12:57 +1100 Message-Id: <20221222061257.1598483-9-benh@kernel.crashing.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221222061257.1598483-1-benh@kernel.crashing.org> References: <20221222061257.1598483-1-benh@kernel.crashing.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 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: Thu, 22 Dec 2022 06:14:18 -0000 From: Benjamin Herrenschmidt This adds the ability to explicitely add an MMIO based serial port via the 'serial' command. The syntax is: serial --port=mmio,{.b,.w,.l,.q} Signed-off-by: Benjamin Herrenschmidt --- docs/grub.texi | 26 ++++++++++++++++++-- grub-core/term/serial.c | 53 +++++++++++++++++++++++++++++++++++++---- 2 files changed, 72 insertions(+), 7 deletions(-) diff --git a/docs/grub.texi b/docs/grub.texi index fd22a69fa..502ca2ef7 100644 --- a/docs/grub.texi +++ b/docs/grub.texi @@ -4168,8 +4168,24 @@ Commands usable anywhere in the menu and in the command-line. @deffn Command serial [@option{--unit=unit}] [@option{--port=port}] [@option{--speed=speed}] [@option{--word=word}] [@option{--parity=parity}] [@option{--stop=stop}] Initialize a serial device. @var{unit} is a number in the range 0-3 specifying which serial port to use; default is 0, which corresponds to -the port often called COM1. @var{port} is the I/O port where the UART -is to be found; if specified it takes precedence over @var{unit}. +the port often called COM1. + +@var{port} is the I/O port where the UART is to be found or, if prefixed +with @samp{mmio,}, the MMIO address of the UART. If specified it takes +precedence over @var{unit}. + +Additionally, an MMIO address can be suffixed with: +@itemize @bullet +@item +@samp{.b} for bytes access (default) +@item +@samp{.w} for 16-bit word access +@item +@samp{.l} for 32-bit long word access or +@item +@samp{.q} for 64-bit long long word access +@end itemize + @var{speed} is the transmission speed; default is 9600. @var{word} and @var{stop} are the number of data bits and stop bits. Data bits must be in the range 5-8 and stop bits must be 1 or 2. Default is 8 data @@ -4185,6 +4201,12 @@ The serial port is not used as a communication channel unless the @command{terminal_input} or @command{terminal_output} command is used (@pxref{terminal_input}, @pxref{terminal_output}). +Examples: +@example +serial --port=3f8 --speed=9600 +serial --port=mmio,fefb0000.l --speed=115200 +@end example + See also @ref{Serial terminal}. @end deffn diff --git a/grub-core/term/serial.c b/grub-core/term/serial.c index ed7b398b7..d374495cb 100644 --- a/grub-core/term/serial.c +++ b/grub-core/term/serial.c @@ -152,8 +152,8 @@ grub_serial_find (const char *name) break; #if (defined(__mips__) || defined (__i386__) || defined (__x86_64__)) && !defined(GRUB_MACHINE_EMU) && !defined(GRUB_MACHINE_ARC) - if (!port && grub_memcmp (name, "port", sizeof ("port") - 1) == 0 - && grub_isxdigit (name [sizeof ("port") - 1])) + if (!port && grub_strncmp (name, "port", grub_strlen ("port")) == 0 + && grub_isxdigit (name [grub_strlen ("port")])) { name = grub_serial_ns8250_add_port (grub_strtoul (&name[sizeof ("port") - 1], 0, 16), NULL); @@ -164,6 +164,49 @@ grub_serial_find (const char *name) if (grub_strcmp (port->name, name) == 0) break; } + if (!port && grub_strncmp (name, "mmio,", grub_strlen ("mmio,")) == 0 + && grub_isxdigit (name [grub_strlen ("mmio,")])) + { + const char *p1, *p = &name[grub_strlen ("mmio,")]; + grub_addr_t addr = grub_strtoul (p, &p1, 16); + unsigned int acc_size = 1; + unsigned int nlen = p1 - p; + + /* Check address validity and general syntax */ + if (addr == 0 || (p1[0] != '\0' && p1[0] != '.')) + return NULL; + if (p1[0] == '.') + switch(p1[1]) + { + case 'w': + acc_size = 2; + break; + case 'l': + acc_size = 3; + break; + case 'q': + acc_size = 4; + break; + case 'b': + acc_size = 1; + /* fallthrough */ + default: + /* + * Should we abort for an unknown size ? Let's just default + * to 1 byte, it would increase the chance that the user who + * did a typo can actually see the console. + */ + acc_size = 1; + } + name = grub_serial_ns8250_add_mmio (addr, acc_size, NULL); + if (!name) + return NULL; + + FOR_SERIAL_PORTS (port) + if (grub_strncmp (port->name, name, nlen) == 0) { + break; + } + } if (!port && grub_strcmp (name, "auto") == 0) { /* Look for an SPCR if any. If not, default to com0 */ @@ -178,9 +221,9 @@ grub_serial_find (const char *name) #endif #ifdef GRUB_MACHINE_IEEE1275 - if (!port && grub_memcmp (name, "ieee1275/", sizeof ("ieee1275/") - 1) == 0) + if (!port && grub_strncmp (name, "ieee1275/", grub_strlen ("ieee1275/")) == 0) { - name = grub_ofserial_add_port (&name[sizeof ("ieee1275/") - 1]); + name = grub_ofserial_add_port (&name[grub_strlen ("ieee1275/")]); if (!name) return NULL; @@ -212,7 +255,7 @@ grub_cmd_serial (grub_extcmd_context_t ctxt, int argc, char **args) if (state[OPTION_PORT].set) { - if (grub_memcmp (state[OPTION_PORT].arg, "mmio", 4) == 0) + if (grub_strncmp (state[OPTION_PORT].arg, "mmio,", grub_strlen ("mmio,")) == 0) grub_snprintf(pname, sizeof (pname), "%s", state[1].arg); else grub_snprintf (pname, sizeof (pname), "port%lx", -- 2.34.1