Skip to content

Commit 9bfdba9

Browse files
committed
Merge tag 'bootconfig-v6.17' of git://git.kernel.org/pub/scm/linux/kernel/git/trace/linux-trace
Pull bootconfig updates from Masami Hiramatsu: - tools/bootconfig: - Fix unaligned access when building footer to avoid SIGBUS - Cleanup bootconfig footer size calculations - test scripts: - Fix to add shebang for a test script - Improve script portability using portable commands - Improve script portability using printf instead of echo - Enclose regex with quotes for syntax highlighter * tag 'bootconfig-v6.17' of git://git.kernel.org/pub/scm/linux/kernel/git/trace/linux-trace: bootconfig: Fix unaligned access when building footer tools/bootconfig: scripts/ftrace.sh was missing the shebang line, so added it tools/bootconfig: Cleanup bootconfig footer size calculations tools/bootconfig: Replace some echo with printf for more portability tools/bootconfig: Improve portability tools: bootconfig: Regex enclosed with quotes to make syntax highlight proper
2 parents e8d780d + 6ed5e20 commit 9bfdba9

File tree

3 files changed

+44
-37
lines changed

3 files changed

+44
-37
lines changed

tools/bootconfig/main.c

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,16 @@
1111
#include <string.h>
1212
#include <errno.h>
1313
#include <endian.h>
14+
#include <assert.h>
1415

1516
#include <linux/bootconfig.h>
1617

1718
#define pr_err(fmt, ...) fprintf(stderr, fmt, ##__VA_ARGS__)
1819

20+
/* Bootconfig footer is [size][csum][BOOTCONFIG_MAGIC]. */
21+
#define BOOTCONFIG_FOOTER_SIZE \
22+
(sizeof(uint32_t) * 2 + BOOTCONFIG_MAGIC_LEN)
23+
1924
static int xbc_show_value(struct xbc_node *node, bool semicolon)
2025
{
2126
const char *val, *eol;
@@ -185,7 +190,7 @@ static int load_xbc_from_initrd(int fd, char **buf)
185190
if (ret < 0)
186191
return -errno;
187192

188-
if (stat.st_size < 8 + BOOTCONFIG_MAGIC_LEN)
193+
if (stat.st_size < BOOTCONFIG_FOOTER_SIZE)
189194
return 0;
190195

191196
if (lseek(fd, -BOOTCONFIG_MAGIC_LEN, SEEK_END) < 0)
@@ -198,7 +203,7 @@ static int load_xbc_from_initrd(int fd, char **buf)
198203
if (memcmp(magic, BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_LEN) != 0)
199204
return 0;
200205

201-
if (lseek(fd, -(8 + BOOTCONFIG_MAGIC_LEN), SEEK_END) < 0)
206+
if (lseek(fd, -BOOTCONFIG_FOOTER_SIZE, SEEK_END) < 0)
202207
return pr_errno("Failed to lseek for size", -errno);
203208

204209
if (read(fd, &size, sizeof(uint32_t)) < 0)
@@ -210,12 +215,12 @@ static int load_xbc_from_initrd(int fd, char **buf)
210215
csum = le32toh(csum);
211216

212217
/* Wrong size error */
213-
if (stat.st_size < size + 8 + BOOTCONFIG_MAGIC_LEN) {
218+
if (stat.st_size < size + BOOTCONFIG_FOOTER_SIZE) {
214219
pr_err("bootconfig size is too big\n");
215220
return -E2BIG;
216221
}
217222

218-
if (lseek(fd, stat.st_size - (size + 8 + BOOTCONFIG_MAGIC_LEN),
223+
if (lseek(fd, stat.st_size - (size + BOOTCONFIG_FOOTER_SIZE),
219224
SEEK_SET) < 0)
220225
return pr_errno("Failed to lseek", -errno);
221226

@@ -346,7 +351,7 @@ static int delete_xbc(const char *path)
346351
ret = fstat(fd, &stat);
347352
if (!ret)
348353
ret = ftruncate(fd, stat.st_size
349-
- size - 8 - BOOTCONFIG_MAGIC_LEN);
354+
- size - BOOTCONFIG_FOOTER_SIZE);
350355
if (ret)
351356
ret = -errno;
352357
} /* Ignore if there is no boot config in initrd */
@@ -359,7 +364,12 @@ static int delete_xbc(const char *path)
359364

360365
static int apply_xbc(const char *path, const char *xbc_path)
361366
{
362-
char *buf, *data, *p;
367+
struct {
368+
uint32_t size;
369+
uint32_t csum;
370+
char magic[BOOTCONFIG_MAGIC_LEN];
371+
} footer;
372+
char *buf, *data;
363373
size_t total_size;
364374
struct stat stat;
365375
const char *msg;
@@ -376,8 +386,7 @@ static int apply_xbc(const char *path, const char *xbc_path)
376386
csum = xbc_calc_checksum(buf, size);
377387

378388
/* Backup the bootconfig data */
379-
data = calloc(size + BOOTCONFIG_ALIGN +
380-
sizeof(uint32_t) + sizeof(uint32_t) + BOOTCONFIG_MAGIC_LEN, 1);
389+
data = calloc(size + BOOTCONFIG_ALIGN + BOOTCONFIG_FOOTER_SIZE, 1);
381390
if (!data)
382391
return -ENOMEM;
383392
memcpy(data, buf, size);
@@ -425,22 +434,18 @@ static int apply_xbc(const char *path, const char *xbc_path)
425434
}
426435

427436
/* To align up the total size to BOOTCONFIG_ALIGN, get padding size */
428-
total_size = stat.st_size + size + sizeof(uint32_t) * 2 + BOOTCONFIG_MAGIC_LEN;
437+
total_size = stat.st_size + size + BOOTCONFIG_FOOTER_SIZE;
429438
pad = ((total_size + BOOTCONFIG_ALIGN - 1) & (~BOOTCONFIG_ALIGN_MASK)) - total_size;
430439
size += pad;
431440

432441
/* Add a footer */
433-
p = data + size;
434-
*(uint32_t *)p = htole32(size);
435-
p += sizeof(uint32_t);
436-
437-
*(uint32_t *)p = htole32(csum);
438-
p += sizeof(uint32_t);
439-
440-
memcpy(p, BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_LEN);
441-
p += BOOTCONFIG_MAGIC_LEN;
442+
footer.size = htole32(size);
443+
footer.csum = htole32(csum);
444+
memcpy(footer.magic, BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_LEN);
445+
static_assert(sizeof(footer) == BOOTCONFIG_FOOTER_SIZE);
446+
memcpy(data + size, &footer, BOOTCONFIG_FOOTER_SIZE);
442447

443-
total_size = p - data;
448+
total_size = size + BOOTCONFIG_FOOTER_SIZE;
444449

445450
ret = write(fd, data, total_size);
446451
if (ret < total_size) {

tools/bootconfig/scripts/ftrace.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#!/bin/sh
12
# SPDX-License-Identifier: GPL-2.0-only
23

34
clear_trace() { # reset trace output

tools/bootconfig/test-bootconfig.sh

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,16 @@ NO=1
2727

2828
xpass() { # pass test command
2929
echo "test case $NO ($*)... "
30-
if ! ($@ && echo "\t\t[OK]"); then
31-
echo "\t\t[NG]"; NG=$((NG + 1))
30+
if ! ($@ && printf "\t\t[OK]\n"); then
31+
printf "\t\t[NG]\n"; NG=$((NG + 1))
3232
fi
3333
NO=$((NO + 1))
3434
}
3535

3636
xfail() { # fail test command
3737
echo "test case $NO ($*)... "
38-
if ! (! $@ && echo "\t\t[OK]"); then
39-
echo "\t\t[NG]"; NG=$((NG + 1))
38+
if ! (! $@ && printf "\t\t[OK]\n"); then
39+
printf "\t\t[NG]\n"; NG=$((NG + 1))
4040
fi
4141
NO=$((NO + 1))
4242
}
@@ -48,13 +48,13 @@ echo "Delete command should success without bootconfig"
4848
xpass $BOOTCONF -d $INITRD
4949

5050
dd if=/dev/zero of=$INITRD bs=4096 count=1
51-
echo "key = value;" > $TEMPCONF
52-
bconf_size=$(stat -c %s $TEMPCONF)
53-
initrd_size=$(stat -c %s $INITRD)
51+
printf "key = value;" > $TEMPCONF
52+
bconf_size=$(wc -c < $TEMPCONF)
53+
initrd_size=$(wc -c < $INITRD)
5454

5555
echo "Apply command test"
5656
xpass $BOOTCONF -a $TEMPCONF $INITRD
57-
new_size=$(stat -c %s $INITRD)
57+
new_size=$(wc -c < $INITRD)
5858

5959
echo "Show command test"
6060
xpass $BOOTCONF $INITRD
@@ -69,13 +69,13 @@ echo "Apply command repeat test"
6969
xpass $BOOTCONF -a $TEMPCONF $INITRD
7070

7171
echo "File size check"
72-
xpass test $new_size -eq $(stat -c %s $INITRD)
72+
xpass test $new_size -eq $(wc -c < $INITRD)
7373

7474
echo "Delete command check"
7575
xpass $BOOTCONF -d $INITRD
7676

7777
echo "File size check"
78-
new_size=$(stat -c %s $INITRD)
78+
new_size=$(wc -c < $INITRD)
7979
xpass test $new_size -eq $initrd_size
8080

8181
echo "No error messge while applying"
@@ -97,19 +97,20 @@ BEGIN {
9797
' > $TEMPCONF
9898
xpass $BOOTCONF -a $TEMPCONF $INITRD
9999

100-
echo "badnode" >> $TEMPCONF
100+
printf "badnode\n" >> $TEMPCONF
101101
xfail $BOOTCONF -a $TEMPCONF $INITRD
102102

103103
echo "Max filesize check"
104104

105105
# Max size is 32767 (including terminal byte)
106-
echo -n "data = \"" > $TEMPCONF
106+
printf "data = \"" > $TEMPCONF
107107
dd if=/dev/urandom bs=768 count=32 | base64 -w0 >> $TEMPCONF
108-
echo "\"" >> $TEMPCONF
108+
printf "\"\n" >> $TEMPCONF
109109
xfail $BOOTCONF -a $TEMPCONF $INITRD
110110

111-
truncate -s 32764 $TEMPCONF
112-
echo "\"" >> $TEMPCONF # add 2 bytes + terminal ('\"\n\0')
111+
dd if=$TEMPCONF of=$OUTFILE bs=1 count=32764
112+
cp $OUTFILE $TEMPCONF
113+
printf "\"\n" >> $TEMPCONF # add 2 bytes + terminal ('\"\n\0')
113114
xpass $BOOTCONF -a $TEMPCONF $INITRD
114115

115116
echo "Adding same-key values"
@@ -139,7 +140,7 @@ xfail grep -q "baz" $OUTFILE
139140
xpass grep -q "qux" $OUTFILE
140141

141142
echo "Double/single quotes test"
142-
echo "key = '\"string\"';" > $TEMPCONF
143+
printf "key = '\"string\"';" > $TEMPCONF
143144
$BOOTCONF -a $TEMPCONF $INITRD
144145
$BOOTCONF $INITRD > $TEMPCONF
145146
cat $TEMPCONF
@@ -167,8 +168,8 @@ echo > $INITRD
167168

168169
xpass $BOOTCONF -a $TEMPCONF $INITRD
169170
$BOOTCONF $INITRD > $OUTFILE
170-
xfail grep -q val[[:space:]] $OUTFILE
171-
xpass grep -q val2[[:space:]] $OUTFILE
171+
xfail grep -q 'val[[:space:]]' $OUTFILE
172+
xpass grep -q 'val2[[:space:]]' $OUTFILE
172173

173174
echo "=== expected failure cases ==="
174175
for i in samples/bad-* ; do

0 commit comments

Comments
 (0)