Skip to content

Commit 85c46b7

Browse files
mhiramatrostedt
authored andcommitted
bootconfig: Add bootconfig magic word for indicating bootconfig explicitly
Add bootconfig magic word to the end of bootconfig on initrd image for indicating explicitly the bootconfig is there. Also tools/bootconfig treats wrong size or wrong checksum or parse error as an error, because if there is a bootconfig magic word, there must be a bootconfig. The bootconfig magic word is "#BOOTCONFIG\n", 12 bytes word. Thus the block image of the initrd file with bootconfig is as follows. [Initrd][bootconfig][size][csum][#BOOTCONFIG\n] Link: http://lkml.kernel.org/r/158220112263.26565.3944814205960612841.stgit@devnote2 Suggested-by: Steven Rostedt <[email protected]> Signed-off-by: Masami Hiramatsu <[email protected]> Signed-off-by: Steven Rostedt (VMware) <[email protected]>
1 parent d8a953d commit 85c46b7

File tree

6 files changed

+49
-17
lines changed

6 files changed

+49
-17
lines changed

Documentation/admin-guide/bootconfig.rst

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,13 @@ Boot Kernel With a Boot Config
102102
==============================
103103

104104
Since the boot configuration file is loaded with initrd, it will be added
105-
to the end of the initrd (initramfs) image file. The Linux kernel decodes
106-
the last part of the initrd image in memory to get the boot configuration
107-
data.
105+
to the end of the initrd (initramfs) image file with size, checksum and
106+
12-byte magic word as below.
107+
108+
[initrd][bootconfig][size(u32)][checksum(u32)][#BOOTCONFIG\n]
109+
110+
The Linux kernel decodes the last part of the initrd image in memory to
111+
get the boot configuration data.
108112
Because of this "piggyback" method, there is no need to change or
109113
update the boot loader and the kernel image itself.
110114

include/linux/bootconfig.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
#include <linux/kernel.h>
1111
#include <linux/types.h>
1212

13+
#define BOOTCONFIG_MAGIC "#BOOTCONFIG\n"
14+
#define BOOTCONFIG_MAGIC_LEN 12
15+
1316
/* XBC tree node */
1417
struct xbc_node {
1518
u16 next;

init/Kconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1222,7 +1222,7 @@ config BOOT_CONFIG
12221222
Extra boot config allows system admin to pass a config file as
12231223
complemental extension of kernel cmdline when booting.
12241224
The boot config file must be attached at the end of initramfs
1225-
with checksum and size.
1225+
with checksum, size and magic word.
12261226
See <file:Documentation/admin-guide/bootconfig.rst> for details.
12271227

12281228
If unsure, say Y.

init/main.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,11 @@ static void __init setup_boot_config(const char *cmdline)
374374
if (!initrd_end)
375375
goto not_found;
376376

377-
hdr = (u32 *)(initrd_end - 8);
377+
data = (char *)initrd_end - BOOTCONFIG_MAGIC_LEN;
378+
if (memcmp(data, BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_LEN))
379+
goto not_found;
380+
381+
hdr = (u32 *)(data - 8);
378382
size = hdr[0];
379383
csum = hdr[1];
380384

tools/bootconfig/main.c

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -131,15 +131,26 @@ int load_xbc_from_initrd(int fd, char **buf)
131131
struct stat stat;
132132
int ret;
133133
u32 size = 0, csum = 0, rcsum;
134+
char magic[BOOTCONFIG_MAGIC_LEN];
134135

135136
ret = fstat(fd, &stat);
136137
if (ret < 0)
137138
return -errno;
138139

139-
if (stat.st_size < 8)
140+
if (stat.st_size < 8 + BOOTCONFIG_MAGIC_LEN)
140141
return 0;
141142

142-
if (lseek(fd, -8, SEEK_END) < 0) {
143+
if (lseek(fd, -BOOTCONFIG_MAGIC_LEN, SEEK_END) < 0) {
144+
pr_err("Failed to lseek: %d\n", -errno);
145+
return -errno;
146+
}
147+
if (read(fd, magic, BOOTCONFIG_MAGIC_LEN) < 0)
148+
return -errno;
149+
/* Check the bootconfig magic bytes */
150+
if (memcmp(magic, BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_LEN) != 0)
151+
return 0;
152+
153+
if (lseek(fd, -(8 + BOOTCONFIG_MAGIC_LEN), SEEK_END) < 0) {
143154
pr_err("Failed to lseek: %d\n", -errno);
144155
return -errno;
145156
}
@@ -150,11 +161,14 @@ int load_xbc_from_initrd(int fd, char **buf)
150161
if (read(fd, &csum, sizeof(u32)) < 0)
151162
return -errno;
152163

153-
/* Wrong size, maybe no boot config here */
154-
if (stat.st_size < size + 8)
155-
return 0;
164+
/* Wrong size error */
165+
if (stat.st_size < size + 8 + BOOTCONFIG_MAGIC_LEN) {
166+
pr_err("bootconfig size is too big\n");
167+
return -E2BIG;
168+
}
156169

157-
if (lseek(fd, stat.st_size - 8 - size, SEEK_SET) < 0) {
170+
if (lseek(fd, stat.st_size - (size + 8 + BOOTCONFIG_MAGIC_LEN),
171+
SEEK_SET) < 0) {
158172
pr_err("Failed to lseek: %d\n", -errno);
159173
return -errno;
160174
}
@@ -163,17 +177,17 @@ int load_xbc_from_initrd(int fd, char **buf)
163177
if (ret < 0)
164178
return ret;
165179

166-
/* Wrong Checksum, maybe no boot config here */
180+
/* Wrong Checksum */
167181
rcsum = checksum((unsigned char *)*buf, size);
168182
if (csum != rcsum) {
169183
pr_err("checksum error: %d != %d\n", csum, rcsum);
170-
return 0;
184+
return -EINVAL;
171185
}
172186

173187
ret = xbc_init(*buf);
174-
/* Wrong data, maybe no boot config here */
188+
/* Wrong data */
175189
if (ret < 0)
176-
return 0;
190+
return ret;
177191

178192
return size;
179193
}
@@ -226,7 +240,8 @@ int delete_xbc(const char *path)
226240
} else if (size > 0) {
227241
ret = fstat(fd, &stat);
228242
if (!ret)
229-
ret = ftruncate(fd, stat.st_size - size - 8);
243+
ret = ftruncate(fd, stat.st_size
244+
- size - 8 - BOOTCONFIG_MAGIC_LEN);
230245
if (ret)
231246
ret = -errno;
232247
} /* Ignore if there is no boot config in initrd */
@@ -295,6 +310,12 @@ int apply_xbc(const char *path, const char *xbc_path)
295310
pr_err("Failed to apply a boot config: %d\n", ret);
296311
return ret;
297312
}
313+
/* Write a magic word of the bootconfig */
314+
ret = write(fd, BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_LEN);
315+
if (ret < 0) {
316+
pr_err("Failed to apply a boot config magic: %d\n", ret);
317+
return ret;
318+
}
298319
close(fd);
299320
free(data);
300321

tools/bootconfig/test-bootconfig.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ xpass $BOOTCONF -a $TEMPCONF $INITRD
4949
new_size=$(stat -c %s $INITRD)
5050

5151
echo "File size check"
52-
xpass test $new_size -eq $(expr $bconf_size + $initrd_size + 9)
52+
xpass test $new_size -eq $(expr $bconf_size + $initrd_size + 9 + 12)
5353

5454
echo "Apply command repeat test"
5555
xpass $BOOTCONF -a $TEMPCONF $INITRD

0 commit comments

Comments
 (0)