Embedded Linux kernel debugging output dynamic configuration method and system
By storing kernel console configuration parameters in a non-volatile storage area and having them read and written to the memory area by the U-Boot loader, the problem of difficult debugging output port switching in the early stages of embedded Linux system kernels is solved, enabling flexible debugging output and efficient fault location.
Patent Information
- Application Number
- CN202611354109.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-09-02
- Publication Date
- 2026-11-17
AI Technical Summary
Existing embedded Linux systems have difficulty switching debug output ports flexibly during the early kernel startup phase, especially in scenarios where the device has been deployed, the casing is inconvenient to disassemble, or the serial port pins are not easy to access. Traditional UART serial port and USB debug port methods are cumbersome to operate and cannot output critical logs in the early kernel phase.
Kernel console configuration parameters are stored in a non-volatile storage area. The U-Boot bootloader reads and writes them to a specified memory area before the kernel starts. During the boot parameter parsing phase, the kernel updates bootargs to redirect debug output to the UART serial port.
It enables flexible switching of debug output ports in the early stages of kernel development without recompiling the kernel or modifying the device tree image, improving fault location efficiency and on-site debugging flexibility. It also overcomes the problem of USB Gadget serial port not being able to output logs and is suitable for various baud rate configurations and hardware environments.
Smart Images

Figure CN123114564A_ABST
Abstract
Description
Technical Field
[0001] The embodiments of this application relate to the field of embedded system debugging technology; in particular, a method and system for dynamically configuring embedded Linux kernel debugging output. Background Technology
[0002] In the development, production testing, fault reproduction, and field maintenance of embedded Linux systems, the output of kernel boot information plays a crucial role in locating problems such as system boot anomalies, driver loading failures, memory configuration errors, device tree configuration errors, and file system mount failures. Some faults occur in the early kernel initialization phase, such as driver conflicts, memory overflows, file system errors, or abnormal boot parameter configurations. If effective logs are not output during this phase, it is difficult to reconstruct the state at the time of the fault even after the system enters user mode or starts debug services.
[0003] One common debugging output method for existing embedded Linux systems is the traditional UART serial port debugging method. This method is usually implemented by specifying the `console` parameter in the `bootargs` boot parameters of the device tree, for example, outputting the kernel boot log to the specified UART serial port using `console=ttyS1,115200`. This method has the advantages of stability and reliability, but the console output port and baud rate are usually pre-written into the device tree, kernel boot parameters, or system image. When switching the debug serial port or changing the baud rate during on-site debugging, it is usually necessary to modify the device tree, modify the `bootargs` configuration, recompile the kernel, or re-flash the image, which is cumbersome and has low debugging efficiency. For devices that need to be located on-site, especially in scenarios where the device has been deployed, the casing is inconvenient to disassemble, or the serial port pins are not easily accessible, the above method is difficult to meet the need for quickly switching debug ports.
[0004] Existing embedded Linux systems may also employ a USB debug port. This method typically involves configuring the inittab startup file or other user-space startup scripts to start the USB Gadget serial port service after system boot and redirect debug output to the USB virtual serial port. However, USB devices are not fully enumerated and initialized during the early stages of kernel startup. USB-related driver loading depends on a complete kernel boot, and the USB Gadget serial port service relies on file system mounting and user-space service execution. Therefore, when the kernel fails to boot completely due to driver conflicts, memory overflows, file system errors, or other reasons, the inittab file has not yet been executed, the USB debug port cannot be established, critical early boot logs cannot be output, and developers cannot pinpoint the specific location and cause of the kernel boot exception.
[0005] Furthermore, some consumer electronics devices or enclosed embedded devices are difficult to connect directly to traditional UART serial ports due to their hardware structure, or can only be connected to some serial ports temporarily during the production and maintenance phases. In these hardware environments, if the console output port must be fixed in the image beforehand, on-site debugging personnel cannot flexibly switch the debugging output port according to the actual wiring conditions, thus limiting the debugging methods.
[0006] For console configurations that can be modified on-site, if the configuration is directly stored in the file system, the kernel cannot rely on the file system to read the configuration in the early stages. If the kernel directly reads the configuration area in Flash, the Flash driver and related storage subsystems have not yet been initialized during the boot parameter parsing phase, and the kernel may not have the conditions to directly access the Flash configuration area in the early stages. Therefore, on-site modifiable configuration parameters and console configurations that can be read and applied by the kernel in the early stages cannot work together during the boot phase. A debug output dynamic configuration scheme is needed that can read the on-site configuration during the boot loader phase and make the configuration effective during the kernel boot parameter parsing phase. Summary of the Invention
[0007] This application provides a method and system for dynamically configuring embedded Linux kernel debug output, which enables the kernel to obtain field-modifiable console configuration parameters in the early stages of kernel startup, and establishes an early-usable UART debug output channel accordingly.
[0008] Firstly, the embedded Linux kernel debugging output dynamic configuration method provided in this application includes:
[0009] Store kernel console configuration parameters in a non-volatile storage area;
[0010] The U-Boot bootloader reads the kernel console configuration parameters before starting the kernel and writes the kernel console configuration parameters into a specified memory region accessible early in the kernel startup process.
[0011] During the boot parameter parsing phase, the kernel reads the kernel console configuration parameters from the specified memory region. When the kernel console configuration parameters are available, the kernel updates the console configuration in the bootargs boot parameters according to the kernel console configuration parameters, so that the kernel debug output is redirected to the corresponding UART serial port.
[0012] Using the above method, the field-modifiable kernel console configuration parameters are no longer fixed in the kernel image, device tree, or fixed bootargs, but are stored in a non-volatile storage area. The U-Boot bootloader reads these configuration parameters before kernel startup and writes them to a designated memory area accessible in the early stages of kernel startup. This allows the kernel to read these configuration parameters and update the console configuration in the bootargs boot parameters during the boot parameter parsing phase without waiting for Flash driver loading, file system mounting, or user-mode debugging service execution. Therefore, debug output can be redirected to a field-specified UART serial port in the early stages of kernel startup, improving the efficiency and debugging flexibility of locating startup anomalies in embedded Linux systems.
[0013] In some implementations, the non-volatile storage area is a dedicated storage area within the Flash memory, independent of the file system settings, so that the U-Boot bootloader can read the kernel console configuration parameters even when the file system is not mounted. Therefore, the console configuration parameters can still be read by the bootloader even if the root file system is not mounted, the file system is corrupted, or user-mode services have not yet started.
[0014] In some implementations, the kernel console configuration parameters include a target UART serial port identifier and a baud rate parameter. The target UART serial port identifier is used to determine the corresponding Linux console device name. Therefore, the console output configuration can be flexibly determined based on the actual serial port connected in the field and the baud rate supported by the debugging device.
[0015] In some implementations, the designated memory region is a high-end memory region within the system memory, and this designated memory region avoids the kernel loading region, device tree loading region, and initialization memory allocation region during kernel startup. This reduces the risk that console configuration parameters written by the U-Boot bootloader may be overwritten or reused during kernel decompression, device tree relocation, or early memory initialization.
[0016] In some implementations, the U-Boot bootloader will also be used to instruct the writing of the address information of the specified memory region into the device tree or boot parameters. This address information includes the starting address and data length of the specified memory region. Thus, the kernel can locate the specified memory region during the boot parameter parsing phase and read console configuration parameters from that region.
[0017] In some implementations, the kernel obtains the address information when parsing the chosen field of the device tree, and reads the kernel console configuration parameters from the specified memory region based on the address information. Thus, the console configuration parameters can enter the kernel boot parameter processing flow during the device tree chosen field parsing phase.
[0018] In some implementations, the kernel console configuration parameters may include: the data format of the kernel console configuration parameters is correct, and the target UART serial port identifier and baud rate parameter in the kernel console configuration parameters are within a preset valid range. This prevents abnormal data, out-of-bounds serial port identifiers, or illegal baud rate parameters from being written to the console configuration.
[0019] In some implementations, when the kernel console configuration parameters are unavailable, the kernel retains the original console configuration in the bootargs startup parameters. Therefore, even if configuration parameters are corrupted, not written, or do not meet format requirements, the system can still use the default console configuration, reducing the risk of completely losing debug output.
[0020] In some implementations, updating the console configuration in the bootargs startup parameters according to the kernel console configuration parameters includes: generating a console parameter based on the target UART serial port identifier and the baud rate parameter; if an existing console parameter exists in the bootargs startup parameters, using the generated console parameter to overwrite or replace the existing console parameter; if an existing console parameter does not exist in the bootargs startup parameters, adding the generated console parameter to the bootargs startup parameters. Preferably, the generated console parameter is appended to the beginning of the bootargs startup parameters to give the dynamically generated console configuration a clear priority.
[0021] Secondly, the embedded Linux kernel debug output dynamic configuration system provided in this application includes: a configuration storage module for storing kernel console configuration parameters in a non-volatile storage area; a boot loading processing module for reading the kernel console configuration parameters before booting the kernel via the U-Boot bootloader and writing the kernel console configuration parameters into a specified memory area accessible in the early stages of kernel boot; a kernel early parsing module for reading the kernel console configuration parameters from the specified memory area during the kernel boot parameter parsing stage; a console configuration update module for updating the console configuration in the bootargs boot parameters according to the kernel console configuration parameters when the kernel console configuration parameters are available; and a console redirection module for redirecting the kernel debug output to the corresponding UART serial port according to the updated bootargs boot parameters.
[0022] Through the above system, the configuration storage module, boot loading processing module, kernel early parsing module, console configuration update module, and console redirection module work sequentially according to the boot phase of the embedded Linux system, so that the console configuration parameters can enter the kernel boot parameter parsing phase from the non-volatile storage area via the U-Boot bootloader and the specified memory area, and finally be applied to the UART console output.
[0023] This application has at least the following beneficial effects: First, it allows switching of debug output ports without recompiling the kernel or modifying the device tree image, reducing the operational costs of on-site debugging and fault reproduction; Second, by pre-reading configuration parameters and writing them to a specified memory area during the U-Boot stage, the kernel can obtain console configurations at the initial startup stage, enabling debug information output from the early kernel loading stage to the complete system startup stage; Third, it does not rely on USB device enumeration and user-mode service startup, overcoming the problem that the USB Gadget serial port cannot output early kernel startup logs; Fourth, it supports modifying debug port configurations through the U-Boot command-line interface or maintenance tools in on-site device deployment or remote maintenance scenarios, without disassembling the device or re-flashing the system; Fifth, the configuration parameter storage and retrieval mechanism does not depend on specific kernel versions or specific UART hardware implementations, exhibiting good applicability and portability; Sixth, it supports multiple baud rate configurations and improves system operational stability through a parameter availability verification mechanism. Attached Figure Description
[0024] Figure 1 This is a flowchart illustrating the embedded Linux kernel debugging output dynamic configuration method in this embodiment of the application.
[0025] Figure 2 This is a schematic diagram illustrating the data flow of console configuration parameters during the startup phase in this embodiment of the application.
[0026] Figure 3 This is a schematic diagram illustrating the specified memory region and address information transmission method in the embodiments of this application.
[0027] Figure 4 This is a schematic diagram illustrating the process of console configuration parameter availability verification and rollback handling in an embodiment of this application.
[0028] Figure 5 This is a schematic diagram of the console configuration update process in the bootargs startup parameters in this application embodiment.
[0029] Figure 6 This is a schematic diagram of the structure of the embedded Linux kernel debugging output dynamic configuration system in the embodiments of this application.
[0030] In the diagram: 10. Configuration storage module; 20. Startup loading processing module; 30. Kernel early parsing module; 40. Console configuration update module; 50. Console redirection module. Detailed Implementation
[0031] The technical solutions in the embodiments of this application will be described below with reference to the accompanying drawings. The following embodiments are used to illustrate the technical solutions of this application and do not limit each technical feature to be implemented in only a single manner. Where there is no conflict, technical features in different embodiments can be combined with each other.
[0032] It should be understood that when an embedded Linux system boots, the U-Boot bootloader typically runs before the Linux kernel. The U-Boot bootloader performs basic hardware initialization, kernel image loading, device tree loading, boot parameter preparation, and jumps to the kernel entry address. After the Linux kernel boots, it sequentially executes decompression, early memory management, device tree resolution, boot parameter resolution, console initialization, driver initialization, file system mounting, and user-space service startup. Console output configurations must take effect before the kernel outputs its main boot logs; otherwise, early kernel logs in the event of a failure will not be output through the target port.
[0033] In traditional methods, console configuration is typically fixed in the bootargs startup parameters or device tree, such as `console=ttyS1,115200`. This method requires the debug port to be predetermined during image generation or device tree configuration. If the actual wiring conditions change or a UART serial port becomes unusable, the device tree or startup parameters need to be modified and the image re-flashed. On the other hand, the USB Gadget serial port relies on USB enumeration, USB driver loading, and user-mode service execution. When the kernel remains in an early stage due to driver conflicts, memory overflows, or file system errors, the inittab or other user-mode startup scripts have not yet been executed, and the USB virtual serial port cannot be established. In the embodiments of this application, the field-modifiable console configuration parameters are stored in a non-volatile storage area. The U-Boot bootloader reads and writes these parameters to a designated memory area accessible to the kernel before kernel startup. The kernel then reads these parameters during the startup parameter parsing stage and updates the console configuration in the bootargs startup parameters.
[0034] like Figure 1 As shown, the first embodiment of this application provides a method for dynamically configuring embedded Linux kernel debugging output, the method comprising:
[0035] S1. Store kernel console configuration parameters in a non-volatile memory area;
[0036] The S2.U-Boot bootloader reads the kernel console configuration parameters before starting the kernel and writes the kernel console configuration parameters into a specified memory area accessible early in the kernel boot process.
[0037] S3. During the boot parameter parsing phase, the kernel reads the kernel console configuration parameters from the specified memory region, and when the kernel console configuration parameters are available, updates the console configuration in the bootargs boot parameters according to the kernel console configuration parameters, so that the kernel debug output is redirected to the corresponding UART serial port.
[0038] The embedded Linux device includes a processor, system memory, non-volatile memory, at least one UART serial port, a U-Boot bootloader, and a Linux kernel. The non-volatile memory can be a reserved area in SPI Flash, NAND Flash, NOR Flash, eMMC, or other storage media capable of retaining data after power loss. The non-volatile memory area is used to store field-modifiable kernel console configuration parameters. These parameters indicate which UART serial port the kernel boot log and debug information are output to, and the communication rate used by that UART serial port.
[0039] In some embodiments, the non-volatile storage area is a dedicated storage area within the Flash memory. This dedicated storage area is independent of the root file system settings and can be a fixed offset address range in the Flash memory or a reserved area independent of the file system partition. This setting allows console configuration parameters to be saved independently of the file system, enabling the U-Boot bootloader to directly read this dedicated storage area at a preset offset address even when the file system is not mounted. Even if the root file system is corrupted, cannot be mounted, or has not yet entered user mode, the console configuration parameters in this dedicated storage area can still be read during the bootloader phase.
[0040] Preferably, the kernel console configuration parameters include the target UART serial port identifier and the baud rate parameter. The target UART serial port identifier is used to determine the corresponding Linux console device name.
[0041] For example, the target UART serial port identifier can be used to determine the corresponding Linux console device name according to a preset mapping relationship. In one configuration rule, target UART serial port identifier 1 indicates output to ttyS1, and target UART serial port identifier 2 indicates output to ttyS2. The baud rate parameter is used to determine the serial communication rate. The kernel can generate corresponding console parameters based on the target UART serial port identifier and the baud rate parameter, for example, generating a console configuration string in the form of console=ttyS1,115200.
[0042] After the device is powered on, the U-Boot bootloader runs before the Linux kernel. The U-Boot bootloader reads the kernel console configuration parameters from the non-volatile storage area before booting the kernel. "Before booting the kernel" means before the U-Boot bootloader jumps to the Linux kernel entry address. Preferably, the U-Boot bootloader first reads the kernel console configuration parameters from the dedicated Flash storage area before loading the kernel image and device tree. This allows for the synchronous preparation of the console configuration parameter dumping and address transfer during subsequent kernel and device tree loading, reducing the stage difference between console configuration reading and kernel startup.
[0043] After reading the kernel console configuration parameters, the U-Boot bootloader writes these parameters to a designated memory region accessible during the early stages of kernel startup. This designated memory region serves as the data carrier for transmitting console configuration parameters between the U-Boot bootloader and the Linux kernel. Because the Linux kernel can access a designated address in system memory during the boot parameter parsing phase, it can obtain the console configuration parameters pre-written by the U-Boot bootloader without waiting for the Flash driver to load or the file system to be mounted.
[0044] After the Linux kernel boots, it reads kernel console configuration parameters from a specified memory region during the boot parameter parsing phase. The boot parameter parsing phase refers to the stage where the kernel parses bootargs boot parameters, the device tree choose field, or other early boot parameters. This phase precedes file system mounting and user-space debugging service execution; therefore, by reading console configuration parameters from a specified memory region, it is possible to bypass the limitations of the Flash driver not yet being loaded, the USB Gadget serial port not yet being established, and inittab not yet being executed.
[0045] When kernel console configuration parameters become available, the Linux kernel updates the console configuration in the bootargs boot parameters based on these parameters. The updated bootargs boot parameters participate in subsequent console initialization, redirecting kernel debug output to the corresponding UART serial port. This redirection is used not only to output some early boot logs but also to output all subsequent kernel boot logs and debug information, ensuring that debug information can be output through the target UART serial port from the early stages of kernel loading until the system is fully booted.
[0046] By having the U-Boot bootloader read the console configuration parameters in the non-volatile storage area before starting the kernel and write them into a specified memory area accessible early in the kernel startup process, the kernel can read the console configuration parameters that can be modified in the field without relying on Flash driver loading, file system mounting, and user-mode debugging service execution. Based on this, it updates the console configuration in the bootargs boot parameters, thereby establishing an early-available and field-switchable UART debug output path.
[0047] In some embodiments, field maintenance personnel can modify kernel console configuration parameters in the dedicated Flash storage area via the U-Boot command-line interface. For example, the maintenance personnel enter commands in the U-Boot command line to set the target UART serial port and baud rate, and the U-Boot bootloader writes the corresponding configuration to the dedicated Flash storage area. When the device restarts, the U-Boot bootloader reads the new configuration parameters and writes them to the specified memory area, and the Linux kernel updates the console configuration in the bootargs boot parameters accordingly. Thus, console output can be flexibly switched based on the available debug ports on-site without recompiling the kernel, modifying the device tree image, re-flashing the system image, or disassembling the device.
[0048] The second embodiment, based on the first embodiment, further addresses the issues that console configuration parameters in a specified memory region may be overwritten during kernel startup, and that the kernel cannot accurately locate the specified memory region. The second embodiment further includes:
[0049] The specified memory region is a high-end memory region in the system memory, and the specified memory region avoids the kernel loading region, device tree loading region and initialization memory allocation region during kernel startup.
[0050] The U-Boot bootloader will also be used to instruct the address information of the specified memory region to be written into the device tree or boot parameters. The address information includes the starting address and data length of the specified memory region.
[0051] The kernel obtains the address information when parsing the choose field of the device tree, and reads the kernel console configuration parameters from the specified memory region based on the address information.
[0052] In the second embodiment, the U-Boot bootloader determines a specified memory region based on the system memory layout before writing the kernel console configuration parameters. The specified memory region can be a high-end memory region in the system memory, such as a reserved region within the high address range of memory mapping. The purpose of selecting a high-end memory region is to avoid the kernel image loading region, device tree loading region, initrd loading region, U-Boot's own running region, and early kernel initialization memory allocation region, thereby ensuring that the written console configuration parameters are not overwritten or reused during the Linux kernel boot process.
[0053] The specific address, capacity, and reservation method of a designated memory region can be determined based on the system memory layout, kernel image loading location, device tree loading location, initial memory allocation range, and console configuration parameter length of different hardware platforms. When determining a designated memory region, it should be ensured that this region avoids conflict with other loaded and allocated regions used during kernel startup, and sufficient storage space should be reserved for reading and parsing console configuration parameters.
[0054] Early kernel accessibility of a specified memory region includes at least two aspects: first, the region must be located within the system memory range accessible via physical addresses or early mappings during the Linux kernel boot process; second, the region must not be overwritten by other data during kernel decompression, device tree relocation, initrd loading, and early memory allocation. To achieve this, the U-Boot bootloader can avoid known load addresses when selecting this region, or it can add a reserved-memory node or other reserved description to the device tree, enabling the Linux kernel's early memory management logic to recognize that this region does not participate in normal memory allocation.
[0055] After writing the kernel console configuration parameters to a specified memory region, the U-Boot bootloader also transmits address information indicating that memory region to the Linux kernel. The address information includes the starting address and data length of the specified memory region. The starting address allows the kernel to locate the storage location of the console configuration parameters, and the data length allows the kernel to determine read boundaries and avoid reading memory data exceeding the valid parameter range. For fixed-length parameter structures, the data length can be the length of the structure; for string-type parameters, the data length can be the maximum string length or the actual length written.
[0056] In one specific implementation, the U-Boot bootloader passes address information by modifying the device tree. Specifically, the U-Boot bootloader writes the starting address and data length of a specified memory region into the `chosen` field of the device tree or a custom attribute under the `chosen` field. When the Linux kernel parses the `chosen` field of the device tree during the initial boot process, it reads this custom attribute to obtain the address information of the specified memory region and then reads the kernel console configuration parameters from that region based on that address information. Since the `chosen` field itself is used to carry early kernel boot information such as boot parameters, writing the address information into the `chosen` field allows the console configuration parameters to enter the processing flow before the kernel parses `bootargs` and initializes the console.
[0057] In another specific implementation, the U-Boot bootloader provides address information by passing specific boot parameters. Specifically, the U-Boot bootloader can write parameter entries representing the console configuration address and length into the bootargs or other boot parameter areas. The Linux kernel reads these parameter entries during the boot parameter parsing phase and then accesses the specified memory region based on the starting address and data length. This method is suitable for scenarios where modifying the device tree structure is inconvenient, compatibility with multiple device tree versions is required, or the device tree file needs to remain unchanged.
[0058] The second embodiment utilizes high-end memory regions, memory protection, and address information transmission mechanisms to ensure that console configuration parameters written by the U-Boot bootloader are stably saved and accurately located early in the kernel startup process. Compared to simply specifying a fixed memory address, this embodiment further reduces the risk of configuration read failures due to parameter overwriting, ambiguous addresses, or incorrect read lengths.
[0059] The third embodiment, based on the first or second embodiment, further addresses the issue that abnormal console configuration parameters may lead to incorrect console configurations taking effect. The third embodiment further includes:
[0060] The kernel console configuration parameters may include: the data format of the kernel console configuration parameters is correct, and the target UART serial port identifier and baud rate parameter in the kernel console configuration parameters are within a preset valid range.
[0061] When the kernel console configuration parameters are unavailable, the kernel retains the original console configuration in the bootargs boot parameters.
[0062] In the third embodiment, after the Linux kernel reads the kernel console configuration parameters from the specified memory region, it does not directly use the read content to update the bootargs startup parameters. Instead, it first determines whether the kernel console configuration parameters are available. This availability check is used to prevent abnormal data from directly affecting the kernel console configuration when data in the Flash dedicated storage area is corrupted, write parameters are incorrect, address information is abnormal, or memory data is corrupted.
[0063] The availability check for kernel console configuration parameters can include data format checking, target UART serial port identification checking, and baud rate parameter checking. Data format checking determines whether the read data conforms to the preset parameter structure. Console configuration parameters can use a fixed field structure, including a target UART serial port identification field and a baud rate field; or they can use a string format or key-value pair format that can express the target UART serial port identification and baud rate parameters. If a field is missing, the field length is abnormal, the delimiter is incorrect, the end-of-string character is incorrect, or the string cannot be parsed, the data format is determined to be incorrect.
[0064] The target UART serial port identifier determination is used to determine whether the target UART serial port identifier is within a preset valid range. The preset valid range can be determined based on the actual UART resources provided by the device. If the read target UART serial port identifier does not belong to the range of console candidate UARTs supported by the device, then the target UART serial port identifier is determined to be unavailable. Furthermore, the device can maintain a list of console candidate UARTs, allowing kernel debug output to be configured only to UART serial ports that are brought out, enabled, and suitable for field debugging, avoiding configuring the console to unbrought-out serial ports or serial ports occupied by other functions.
[0065] The baud rate parameter check determines whether the baud rate parameter is within a preset valid range. This preset valid range can be determined based on the set or range of baud rates supported by both the device's UART controller and the external debugging device. If the read baud rate parameter is non-numerical or does not belong to the preset baud rate set or range, it is considered unusable. This check prevents invalid baud rates from causing the external debugging device to be unable to receive valid logs.
[0066] When the kernel console configuration parameter has the correct data format, and the target UART serial port identifier and baud rate parameters are both within the preset valid range, the Linux kernel determines that the kernel console configuration parameter is available and updates the console configuration in the bootargs boot parameters accordingly. When the kernel console configuration parameter is unavailable, or a valid console configuration parameter is not read from the specified memory area, the Linux kernel keeps the original console configuration in the bootargs boot parameters unchanged.
[0067] When the kernel console configuration parameters are read in the correct data format, and the target UART serial port identifier and baud rate parameters both meet the preset valid range, the kernel determines that the parameter is usable and updates the console configuration in the bootargs boot parameters accordingly. If the read kernel console configuration parameters have missing fields, abnormal formats, the target UART serial port identifier exceeds the device's supported range, or the baud rate parameter does not meet the preset valid range, the kernel determines that the parameter is unusable and maintains the original console configuration in the bootargs boot parameters.
[0068] In some embodiments, kernel console configuration parameters may further include a configuration version number, parameter length, a checksum field, an enable flag, or a default console identifier. The configuration version number distinguishes different formats of console configuration parameters; the parameter length specifies the valid data range; the checksum field determines whether the console configuration parameters have been corrupted during Flash storage or memory transfer; the enable flag determines whether dynamic console configuration is enabled; and the default console identifier provides a backup output port in case of dynamic configuration failure. These fields can be used as extended fields beyond the target UART serial port identifier and baud rate parameters to further enhance configuration reliability.
[0069] The third embodiment improves the reliability and stability of the early debug output path of the kernel by using availability verification and original configuration retention mechanisms to ensure that incorrect, corrupted, or non-compliant configurations that do not conform to the actual serial port resources of the device will not overwrite the original console configuration.
[0070] The fourth embodiment, based on the first embodiment, further addresses the console configuration conflict issue when the bootargs boot parameters already contain a console parameter. The fourth embodiment further includes updating the console configuration in the bootargs boot parameters according to the kernel console configuration parameters, including:
[0071] Generate console parameters based on the target UART serial port identifier and the baud rate parameter;
[0072] If the original console parameter exists in the bootargs startup parameters, the generated console parameter will be used to overwrite or replace the original console parameter.
[0073] If the original console parameter is not present in the bootargs startup parameters, the generated console parameter will be added to the bootargs startup parameters.
[0074] In the fourth embodiment, after confirming that the kernel console configuration parameters are available, the Linux kernel generates corresponding console parameters based on the target UART serial port identifier and the baud rate parameter. The target UART serial port identifier is used to determine the Linux console device name, the baud rate parameter is used to determine the serial communication rate, and the generated console parameters are used to indicate the target UART serial port and its communication rate used for kernel debugging output.
[0075] After generating the `console` parameter, the Linux kernel checks if the original `console` parameter already exists in the `bootargs` startup parameters. If the `console` parameter does not exist in the `bootargs` startup parameters, the generated `console` parameter is added to the `bootargs` startup parameters. Preferably, the generated `console` parameter is dynamically appended to the beginning of the `bootargs` startup parameters so that the dynamically generated console configuration can participate in the console configuration process during startup parameter parsing.
[0076] If the original console parameter already exists in the bootargs boot parameters, the Linux kernel will use the generated console parameter to overwrite or replace the original console parameter, or supplement it before the original console parameter according to preset rules. By overwriting, replacing, or prioritizing the supplementation of the original console parameter, the console output port and communication rate configured on-site can be applied to the kernel debug output, avoiding the original console configuration from continuing to take effect and causing inconsistencies between the debug output port and the on-site configuration.
[0077] In some embodiments, when multiple console parameters exist in the bootargs startup parameters, they can be processed according to preset rules. For example, the first console parameter can be replaced, the last console parameter can be replaced, or all original console parameters can be deleted and the generated console parameter can be appended to the beginning of the bootargs startup parameters. Preferably, the generated console parameter is added after deleting all original console parameters to avoid the kernel debug output port being uncertain due to the coexistence of multiple console parameters.
[0078] In some embodiments, the generated console parameters, in addition to the target UART serial port identifier and baud rate parameters, may also include additional serial communication parameters such as data bits, parity bits, and stop bits, depending on the format supported by different kernels or serial port drivers. For scenarios where these extended parameters are not configured, the default serial communication format can be used. This approach is compatible with the differences in console parameter parsing across different UART hardware implementations and kernel versions.
[0079] The fourth embodiment clarifies the rules for generating, adding, overwriting, replacing, and preferably concatenating console parameters to the beginning of bootargs startup parameters, so that the console parameters configured on-site can take effect in bootargs in a deterministic manner, thereby reducing the conflict between the original console parameters and dynamically generated console parameters.
[0080] The fifth embodiment provides an embedded Linux kernel debugging output dynamic configuration system, which includes:
[0081] The configuration storage module is used to store kernel console configuration parameters in a non-volatile storage area;
[0082] The boot loading processing module is used to read the kernel console configuration parameters before booting the kernel via the U-Boot bootloader, and write the kernel console configuration parameters into a specified memory area accessible in the early stages of kernel boot.
[0083] The kernel early parsing module is used to read the kernel console configuration parameters from the specified memory region during the kernel boot parameter parsing phase.
[0084] The console configuration update module is used to update the console configuration in the bootargs boot parameters according to the kernel console configuration parameters when the kernel console configuration parameters are available.
[0085] The console redirection module is used to redirect kernel debug output to the corresponding UART serial port based on the updated bootargs boot parameters.
[0086] In the fifth embodiment, the configuration storage module 10 is used to store kernel console configuration parameters in a non-volatile storage area. The configuration storage module 10 can also be called a Flash storage module, which can be implemented by a dedicated storage area in the Flash memory. This dedicated storage area is independent of the file system settings and is used to store the target UART serial port identifier, baud rate parameters, and optional configuration version number, parameter length, or checksum field. The data in the configuration storage module 10 is retained even after the device is powered off and can be read by the U-Boot bootloader in the early stages of system startup.
[0087] The boot loading processing module 20 is used to read kernel console configuration parameters before booting the kernel via the U-Boot boot loader, and write these parameters into a designated memory area accessible in the early stages of kernel boot. The boot loading processing module 20 can also be called the U-Boot boot module. During the U-Boot phase, it reads console configuration parameters from the Flash storage module, writes the read data to a designated high memory address in system memory, and transmits the address information corresponding to that high memory address to the Linux kernel. Preferably, the boot loading processing module 20 first reads the console configuration parameters from the dedicated Flash storage area before loading the kernel image and device tree.
[0088] The early kernel parsing module 30 is used to read kernel console configuration parameters from a specified memory region during the kernel's boot parameter parsing phase. The early kernel parsing module 30 can also be called the kernel parsing module. This module can obtain the starting address and data length of a specified memory region when the Linux kernel parses the `chosen` field of the device tree, and read the console configuration parameters based on this address information. This module can also perform availability verification on the read parameters, including data format verification, target UART serial port identifier range verification, and baud rate parameter range verification.
[0089] The console configuration update module 40 is used to update the console configuration in the bootargs startup parameters when the kernel console configuration parameters are available. The console configuration update module 40 can also be called the bootargs concatenation module. This module generates console parameters based on the target UART serial port identifier and baud rate parameters. If a console parameter does not exist in the bootargs startup parameters, the generated console parameter is added to the bootargs startup parameters; if an existing console parameter exists in the bootargs startup parameters, the generated console parameter is used to overwrite or replace the original console parameter. Preferably, the generated console parameter is concatenated to the beginning of the bootargs startup parameters.
[0090] The console redirection module 50 redirects kernel debug output to the corresponding UART serial port based on the updated bootargs startup parameters. The console redirection module 50 utilizes the existing console initialization mechanism in the Linux kernel to select the corresponding UART device as the kernel console output device based on the console parameter in the bootargs startup parameters. After the console redirection module 50 completes its configuration, kernel boot logs and subsequent debug information are output via the target UART serial port.
[0091] In a specific operational process, maintenance personnel at the device deployment site write the target UART serial port identifier and baud rate parameters to a dedicated Flash storage area via the U-Boot command-line interface. After the device powers on, the U-Boot bootloader reads these console configuration parameters before loading the kernel and device tree, writes them to a specified high memory region in system memory, and writes the starting address and data length of this specified high memory region into the device tree's `chosen` field. After the Linux kernel boots, it parses the `chosen` field and reads the target UART serial port identifier and baud rate parameters from the specified high memory region based on the address information. After determining that the parameters are available, the Linux kernel generates the corresponding console parameters and appends them to the beginning of the `bootargs` boot parameters or replaces the original console parameters. Subsequently, the Linux kernel redirects debug output to the corresponding UART serial port based on the updated `bootargs` boot parameters. If a driver initialization exception occurs before the file system is mounted, the relevant kernel boot logs can still be output through this UART serial port.
[0092] In another specific operation, the console configuration parameters in the Flash dedicated storage area were mistakenly written, causing the baud rate field to no longer conform to the default format. The U-Boot bootloader could still write the read data to the specified memory area and transmit the address information of that memory area. After the Linux kernel boots, it reads this data and performs an availability verification, determining that the baud rate field is unusable. Therefore, it retains the original console configuration in the bootargs boot parameters. Thus, the incorrect configuration does not overwrite the default console configuration, and the system can still output logs through the original console path.
[0093] In another specific operational process, the device's USB Gadget serial port typically needs to be established after the file system is mounted and inittab is executed. If the device remains stuck before mounting the root file system due to a file system error, the USB virtual serial port cannot be established. With the solution proposed in this application, the console configuration parameters are already read and stored in the designated memory area during the U-Boot stage. The Linux kernel can then read these parameters and complete the UART console configuration during the boot parameter parsing stage. Therefore, even if the USB debug port is unavailable, early kernel logs can still be output through the target UART serial port.
[0094] In some embodiments, the configuration parameter storage and retrieval mechanism can be independent of specific Linux kernel versions and UART hardware implementations. For different kernel versions, as long as the specified memory region can be read and the console configuration in the bootargs boot parameters can be updated during the boot parameter parsing phase, the configuration transmission mechanism of this application can be used. For different UART hardware, as long as the UART can be recognized by the Linux kernel as a candidate console device, the corresponding console parameters can be generated through the target UART serial port identifier and baud rate parameters. Therefore, this application is applicable to different embedded platforms, different kernel versions, and different UART hardware conditions.
[0095] Through the above embodiments, this application enables field-modifiable kernel console configuration parameters to enter the Linux kernel boot parameter parsing stage from non-volatile storage area via the U-Boot bootloader and a designated memory area, and updates the console configuration in the bootargs boot parameters when the parameters are available. This scheme does not rely on Flash driver loading, file system mounting, or user-mode debugging service execution. It can establish a usable UART debug output path early in the kernel boot process and output subsequent kernel boot logs and debugging information via the target UART serial port, thereby improving the efficiency of locating boot anomalies in embedded Linux systems, the flexibility of on-site debugging, and the convenience of remote maintenance.
[0096] The above description is merely a specific embodiment of this application and is not intended to limit this application. For those skilled in the art, without departing from the technical concept of this application, adjustments or replacements can be made to the non-volatile storage medium type, the location of the dedicated Flash storage area, the method for selecting a specified memory area, the method for writing address information, the format of the device tree choose field attribute, the format of the console configuration parameters, the availability judgment rules, the bootargs update rules, and the UART serial port device name mapping method. All such adjustments or replacements should be understood as equivalent embodiments of the technical solution of this application.
Claims
1. A method for dynamically configuring embedded Linux kernel debugging output, characterized in that, include: Store kernel console configuration parameters in a non-volatile storage area; The U-Boot bootloader reads the kernel console configuration parameters before starting the kernel and writes the kernel console configuration parameters into a specified memory region accessible early in the kernel startup process. During the boot parameter parsing phase, the kernel reads the kernel console configuration parameters from the specified memory region. When the kernel console configuration parameters are available, the kernel updates the console configuration in the bootargs boot parameters according to the kernel console configuration parameters, so that the kernel debug output is redirected to the corresponding UART serial port.
2. The embedded Linux kernel debugging output dynamic configuration method as described in claim 1, characterized in that: The non-volatile storage area is a dedicated storage area in the Flash memory. This dedicated storage area is independent of the file system settings, so that the U-Boot bootloader can read the kernel console configuration parameters when the file system is not mounted.
3. The embedded Linux kernel debugging output dynamic configuration method as described in claim 1, characterized in that: The kernel console configuration parameters include the target UART serial port identifier and the baud rate parameter. The target UART serial port identifier is used to determine the corresponding Linux console device name.
4. The embedded Linux kernel debugging output dynamic configuration method as described in claim 1, characterized in that: The specified memory region is a high-end memory region in the system memory, and the specified memory region avoids the kernel loading region, device tree loading region and initialization memory allocation region during kernel startup.
5. The embedded Linux kernel debugging output dynamic configuration method as described in claim 1, characterized in that: The U-Boot bootloader will also be used to instruct the address information of the specified memory region to be written into the device tree or boot parameters. The address information includes the starting address and data length of the specified memory region.
6. The embedded Linux kernel debugging output dynamic configuration method as described in claim 5, characterized in that: The kernel obtains the address information when parsing the choose field of the device tree, and reads the kernel console configuration parameters from the specified memory region based on the address information.
7. The embedded Linux kernel debugging output dynamic configuration method as described in claim 1, characterized in that: The kernel console configuration parameters may include: the data format of the kernel console configuration parameters is correct, and the target UART serial port identifier and baud rate parameter in the kernel console configuration parameters are within a preset valid range.
8. The embedded Linux kernel debugging output dynamic configuration method as described in claim 7, characterized in that: When the kernel console configuration parameters are unavailable, the kernel retains the original console configuration in the bootargs boot parameters.
9. The embedded Linux kernel debugging output dynamic configuration method as described in claim 1, characterized in that: Updating the console configuration in the bootargs boot parameters according to the kernel console configuration parameters includes: Generate console parameters based on the target UART serial port identifier and the baud rate parameter; If the original console parameter exists in the bootargs startup parameters, the generated console parameter will be used to overwrite or replace the original console parameter. If the original console parameter is not present in the bootargs startup parameters, the generated console parameter will be added to the bootargs startup parameters.
10. An embedded Linux kernel debugging and dynamic configuration system, characterized in that, include: The configuration storage module is used to store kernel console configuration parameters in a non-volatile storage area; The boot loading processing module is used to read the kernel console configuration parameters before booting the kernel via the U-Boot bootloader, and write the kernel console configuration parameters into a specified memory area accessible in the early stages of kernel boot. The kernel early parsing module is used to read the kernel console configuration parameters from the specified memory region during the kernel boot parameter parsing phase. The console configuration update module is used to update the console configuration in the bootargs boot parameters according to the kernel console configuration parameters when the kernel console configuration parameters are available. The console redirection module is used to redirect kernel debug output to the corresponding UART serial port based on the updated bootargs boot parameters.
