Skip to content

Commit 26dda57

Browse files
committed
tools/bootconfig: Cleanup bootconfig footer size calculations
There are many same pattern of 8 + BOOTCONFIG_MAGIC_LEN for calculating the size of bootconfig footer. Use BOOTCONFIG_FOOTER_SIZE macro to clean up those magic numbers. Link: https://lore.kernel.org/all/175211425693.2591046.16029516706923643510.stgit@mhiramat.tok.corp.google.com/ Signed-off-by: Masami Hiramatsu (Google) <[email protected]>
1 parent a141656 commit 26dda57

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

tools/bootconfig/main.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616

1717
#define pr_err(fmt, ...) fprintf(stderr, fmt, ##__VA_ARGS__)
1818

19+
/* Bootconfig footer is [size][csum][BOOTCONFIG_MAGIC]. */
20+
#define BOOTCONFIG_FOOTER_SIZE \
21+
(sizeof(uint32_t) * 2 + BOOTCONFIG_MAGIC_LEN)
22+
1923
static int xbc_show_value(struct xbc_node *node, bool semicolon)
2024
{
2125
const char *val, *eol;
@@ -185,7 +189,7 @@ static int load_xbc_from_initrd(int fd, char **buf)
185189
if (ret < 0)
186190
return -errno;
187191

188-
if (stat.st_size < 8 + BOOTCONFIG_MAGIC_LEN)
192+
if (stat.st_size < BOOTCONFIG_FOOTER_SIZE)
189193
return 0;
190194

191195
if (lseek(fd, -BOOTCONFIG_MAGIC_LEN, SEEK_END) < 0)
@@ -198,7 +202,7 @@ static int load_xbc_from_initrd(int fd, char **buf)
198202
if (memcmp(magic, BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_LEN) != 0)
199203
return 0;
200204

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

204208
if (read(fd, &size, sizeof(uint32_t)) < 0)
@@ -210,12 +214,12 @@ static int load_xbc_from_initrd(int fd, char **buf)
210214
csum = le32toh(csum);
211215

212216
/* Wrong size error */
213-
if (stat.st_size < size + 8 + BOOTCONFIG_MAGIC_LEN) {
217+
if (stat.st_size < size + BOOTCONFIG_FOOTER_SIZE) {
214218
pr_err("bootconfig size is too big\n");
215219
return -E2BIG;
216220
}
217221

218-
if (lseek(fd, stat.st_size - (size + 8 + BOOTCONFIG_MAGIC_LEN),
222+
if (lseek(fd, stat.st_size - (size + BOOTCONFIG_FOOTER_SIZE),
219223
SEEK_SET) < 0)
220224
return pr_errno("Failed to lseek", -errno);
221225

@@ -346,7 +350,7 @@ static int delete_xbc(const char *path)
346350
ret = fstat(fd, &stat);
347351
if (!ret)
348352
ret = ftruncate(fd, stat.st_size
349-
- size - 8 - BOOTCONFIG_MAGIC_LEN);
353+
- size - BOOTCONFIG_FOOTER_SIZE);
350354
if (ret)
351355
ret = -errno;
352356
} /* Ignore if there is no boot config in initrd */
@@ -376,8 +380,7 @@ static int apply_xbc(const char *path, const char *xbc_path)
376380
csum = xbc_calc_checksum(buf, size);
377381

378382
/* Backup the bootconfig data */
379-
data = calloc(size + BOOTCONFIG_ALIGN +
380-
sizeof(uint32_t) + sizeof(uint32_t) + BOOTCONFIG_MAGIC_LEN, 1);
383+
data = calloc(size + BOOTCONFIG_ALIGN + BOOTCONFIG_FOOTER_SIZE, 1);
381384
if (!data)
382385
return -ENOMEM;
383386
memcpy(data, buf, size);
@@ -425,7 +428,7 @@ static int apply_xbc(const char *path, const char *xbc_path)
425428
}
426429

427430
/* 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;
431+
total_size = stat.st_size + size + BOOTCONFIG_FOOTER_SIZE;
429432
pad = ((total_size + BOOTCONFIG_ALIGN - 1) & (~BOOTCONFIG_ALIGN_MASK)) - total_size;
430433
size += pad;
431434

0 commit comments

Comments
 (0)