Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

9.2 Parsing the Bootconfig File

The Linux kernel initialization process start_kernel(), upon completing the framework-based hardware configurations, invokes the function setup_boot_config() to prepare for the configuration of supplemental kernel parameters. The function setup_boot_config() is defined in the file git/lib/bootconfig.c, and its definition is as follows:

static void __init setup_boot_config(const char *cmdline)

{

static char tmp_cmdline[COMMAND_LINE_SIZE] __initdata;

const char *msg;

int pos;

u32 size, csum;

char *data, *copy, *err;

int ret;

data = get_boot_config_from_initrd(&size, &csum);

strlcpy(tmp_cmdline, boot_command_line, COMMAND_LINE_SIZE);

err = parse_args("bootconfig", tmp_cmdline, NULL, 0, 0, 0, NULL, bootconfig_params);

if (IS_ERR(err) || !bootconfig_found)

return;

if (err)

initargs_found = true;

if (!data) {

pr_err("'bootconfig' found on command line, but no bootconfig found\n");

return;

}

if (size >= XBC_DATA_MAX) {

pr_err("bootconfig size %d greater than max size %d\n", size, XBC_DATA_MAX);

return;

}

if (boot_config_checksum((unsigned char *)data, size) != csum) {

pr_err("bootconfig checksum failed\n");

return;

}

copy = memblock_alloc(size + 1, SMP_CACHE_BYTES);

if (!copy) {

pr_err("Failed to allocate memory for bootconfig\n");

return;

}

memcpy(copy, data, size);

copy[size] = '\0';

ret = xbc_init(copy, &msg, &pos);

if (ret < 0) {

if (pos < 0)

pr_err("Failed to init bootconfig: %s.\n", msg);

else

pr_err("Failed to parse bootconfig: %s at %d.\n", msg, pos);

} else {

pr_info("Load bootconfig: %d bytes %d nodes\n", size, ret);

extra_command_line = xbc_make_cmdline("kernel");

extra_init_args = xbc_make_cmdline("init");

}

return;

}

The variable passed to this function by start_kernel() is command_line. This function first invokes get_boot_config_from_initrd() to obtain the trailing boundary address of the initrd, and reads 12 bytes of data from the position corresponding to the magic string within the kernel image payload. If the retrieved payload matches the string "#BOOTCONFIG\n", it indicates that a bootconfig file is appended after the initrd; otherwise, no bootconfig file is attached. Upon identifying the bootconfig file, the function get_boot_config_from_initrd() reads the checksum block and the file size attribute from the image file to determine the starting address of the bootconfig file. If this established address is smaller than the trailing boundary address of the initrd, it indicates a structural fault within the bootconfig metadata layout. If all verifications pass, get_boot_config_from_initrd() stores the file size and checksum value into the variables size and csum respectively, and returns the base address of the bootconfig file. In the event of a failure, the function returns a null pointer.

After retrieving the base address of the bootconfig file, the contents of boot_command_line are copied into tmp_cmdline, and the function parse_args() is employed to traverse and tokenized the string payload within tmp_cmdline. For ARM-based architectures, boot_command_line preserves the string data supplied by the cmdline field originating from the U-Boot ATAGs/tag infrastructures. boot_command_line is leveraged for parameter orchestration during system boot operations and typically reflects the default environment configuration criteria, which is populated by the function setup_machine_tags() during tag parsing blocks.

parse_args() passes the entry address of the function bootconfig_params() to parse_one(), executing the bootconfig_params routine to determine whether the "bootconfig" token is enclosed within boot_command_line. It should be noted here that because parse_args() specifies a num argument value of 0 when invoking parse_one(), parse_one() bypasses the logic path intended to confirm whether the parameters encapsulated within tmp_cmdline are secure. Instead, it exclusively executes the bootconfig_params routine. Consequently, when parse_one() is triggered through parse_args(), its role is simplified to merely detecting the presence of the "bootconfig" string to determine whether supplemental parameter orchestration needs to be processed.

When the requirement for supplemental parameter configuration is confirmed, and the presence of a valid bootconfig file within the image payload is established, setup_boot_config() utilizes boot_config_checksum() to ensure the data integrity of the bootconfig file payload.

After creating a copy of the bootconfig file, the system parses the bootconfig file through the function xbc_init(), and constructs a tree topology designated as the XBC tree based on the text payload to describe the relationship hierarchy between the diverse variables. The nodes of this tree architecture are composed of keys and values, which are represented by the structure:

struct xbc_node {

u16 next;

u16 child;

u16 parent;

u16 data;

} __attribute__ ((__packed__));

The next field stores the index value of the next node, child stores the index value of its child node, and parent stores the index value of its parent node. The lower 15 bits of the data field store the index value of either the key or the value, while the most significant bit (MSB) is utilized to distinguish between a key and a value. When the MSB evaluates to 1, it indicates that the address corresponding to data stores a value; when the MSB evaluates to 0, it signifies that the address corresponding to data stores a key.

During the process of parsing the bootconfig file, if a syntax error is discovered within the file, the error message is preserved in emsg, and the position where the error occurred is preserved in epos. Upon completing the syntax parsing of the bootconfig file, the function xbc_verify_tree() is invoked to ensure that the constructed tree structure is free of issues, and then a value of 0 is returned.

图片 1{width=“2.038031496062992in” height=“2.388059930008749in”}图片 1{width=“1.7238801399825021in” height=“2.3800634295713037in”}

Figure 9-2 Bootconfig File and Corresponding Tree Structure

For the bootconfig file illustrated on the left side of Figure 9-2, xbc_init() generates the tree structure illustrated on the right side of the figure.

When parsing a bootconfig file, the xbc_init() procedure first searches for characters possessing special significance, such as {, }, =, +=, ;,
=, \n, and #, to determine the syntax format of the variables. If the symbol =, +=, or := is located, it indicates that the variable syntax follows a value assignment format—meaning the left side of the symbol represents the variable name and the right side represents the variable value—which requires invoking the function xbc_parse_kv() to parse the key-value pair. If the symbol located is #, it indicates that this portion represents a comment, under which circumstance all characters starting from # to the end of the line are ignored. If the symbol located is ; or \n, it must be succeeded by a key, which triggers a call to xbc_parse_key() to parse the key. If the character located is { or }, it indicates that entries sharing identical key segments are enclosed between { and }, which requires invoking xbc_open_brace() and xbc_close_brace() to parse the contents enclosed within.

Since a key can contain subkeys delimited by ., the functions xbc_parse_kv(), xbc_parse_key(), and xbc_open_brace() all invoke the internal function __xbc_parse_keys(), which sequentially searches for the . symbol to determine each individual subkey. Upon locating each subkey (including the root key itself) or confirming a value, __xbc_parse_keys() invokes the function __xbc_add_key(). If the current variable tree structure does not yet contain this key or value, it appends this key or value as a child node of its upper-level key. If they already reside within the XBC tree, they will not be added to the XBC tree again; instead, the routine merely records the upper-level node of that key or value.

After establishing the hierarchical parameter tree structure, setup_boot_config() traverses the XBC tree by invoking the function xbc_make_cmdline() to check whether the bootconfig file contains the "kernel" and "init" keys. If the "kernel" key is present, it assigns the string "kernel:" along with its corresponding variable values to the global variable extra_command_line. If the kernel key possesses subkeys, it likewise appends "kernel.subkey1...subkeyn:" and its corresponding variable values to extra_command_line. Similarly, it assigns "init:" and its values to extra_init_args, making them available for use by the kernel and the init() process.

Copyright © 2026 Guangzhou eSmart Information Technology Co., Ltd. All Rights Reserved.
All content in this book is copyrighted by the author. For personal learning and exchange only. Commercial use without authorization is strictly prohibited. Email:

✉ 3348194517@qq.com | hai616878@gmail.com