mirror of
git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
synced 2025-09-04 20:19:47 +08:00
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) <mhiramat@kernel.org>
This commit is contained in:
parent
a141656ac8
commit
26dda57695
@ -16,6 +16,10 @@
|
|||||||
|
|
||||||
#define pr_err(fmt, ...) fprintf(stderr, fmt, ##__VA_ARGS__)
|
#define pr_err(fmt, ...) fprintf(stderr, fmt, ##__VA_ARGS__)
|
||||||
|
|
||||||
|
/* Bootconfig footer is [size][csum][BOOTCONFIG_MAGIC]. */
|
||||||
|
#define BOOTCONFIG_FOOTER_SIZE \
|
||||||
|
(sizeof(uint32_t) * 2 + BOOTCONFIG_MAGIC_LEN)
|
||||||
|
|
||||||
static int xbc_show_value(struct xbc_node *node, bool semicolon)
|
static int xbc_show_value(struct xbc_node *node, bool semicolon)
|
||||||
{
|
{
|
||||||
const char *val, *eol;
|
const char *val, *eol;
|
||||||
@ -185,7 +189,7 @@ static int load_xbc_from_initrd(int fd, char **buf)
|
|||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
return -errno;
|
return -errno;
|
||||||
|
|
||||||
if (stat.st_size < 8 + BOOTCONFIG_MAGIC_LEN)
|
if (stat.st_size < BOOTCONFIG_FOOTER_SIZE)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
if (lseek(fd, -BOOTCONFIG_MAGIC_LEN, SEEK_END) < 0)
|
if (lseek(fd, -BOOTCONFIG_MAGIC_LEN, SEEK_END) < 0)
|
||||||
@ -198,7 +202,7 @@ static int load_xbc_from_initrd(int fd, char **buf)
|
|||||||
if (memcmp(magic, BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_LEN) != 0)
|
if (memcmp(magic, BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_LEN) != 0)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
if (lseek(fd, -(8 + BOOTCONFIG_MAGIC_LEN), SEEK_END) < 0)
|
if (lseek(fd, -BOOTCONFIG_FOOTER_SIZE, SEEK_END) < 0)
|
||||||
return pr_errno("Failed to lseek for size", -errno);
|
return pr_errno("Failed to lseek for size", -errno);
|
||||||
|
|
||||||
if (read(fd, &size, sizeof(uint32_t)) < 0)
|
if (read(fd, &size, sizeof(uint32_t)) < 0)
|
||||||
@ -210,12 +214,12 @@ static int load_xbc_from_initrd(int fd, char **buf)
|
|||||||
csum = le32toh(csum);
|
csum = le32toh(csum);
|
||||||
|
|
||||||
/* Wrong size error */
|
/* Wrong size error */
|
||||||
if (stat.st_size < size + 8 + BOOTCONFIG_MAGIC_LEN) {
|
if (stat.st_size < size + BOOTCONFIG_FOOTER_SIZE) {
|
||||||
pr_err("bootconfig size is too big\n");
|
pr_err("bootconfig size is too big\n");
|
||||||
return -E2BIG;
|
return -E2BIG;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (lseek(fd, stat.st_size - (size + 8 + BOOTCONFIG_MAGIC_LEN),
|
if (lseek(fd, stat.st_size - (size + BOOTCONFIG_FOOTER_SIZE),
|
||||||
SEEK_SET) < 0)
|
SEEK_SET) < 0)
|
||||||
return pr_errno("Failed to lseek", -errno);
|
return pr_errno("Failed to lseek", -errno);
|
||||||
|
|
||||||
@ -346,7 +350,7 @@ static int delete_xbc(const char *path)
|
|||||||
ret = fstat(fd, &stat);
|
ret = fstat(fd, &stat);
|
||||||
if (!ret)
|
if (!ret)
|
||||||
ret = ftruncate(fd, stat.st_size
|
ret = ftruncate(fd, stat.st_size
|
||||||
- size - 8 - BOOTCONFIG_MAGIC_LEN);
|
- size - BOOTCONFIG_FOOTER_SIZE);
|
||||||
if (ret)
|
if (ret)
|
||||||
ret = -errno;
|
ret = -errno;
|
||||||
} /* Ignore if there is no boot config in initrd */
|
} /* Ignore if there is no boot config in initrd */
|
||||||
@ -376,8 +380,7 @@ static int apply_xbc(const char *path, const char *xbc_path)
|
|||||||
csum = xbc_calc_checksum(buf, size);
|
csum = xbc_calc_checksum(buf, size);
|
||||||
|
|
||||||
/* Backup the bootconfig data */
|
/* Backup the bootconfig data */
|
||||||
data = calloc(size + BOOTCONFIG_ALIGN +
|
data = calloc(size + BOOTCONFIG_ALIGN + BOOTCONFIG_FOOTER_SIZE, 1);
|
||||||
sizeof(uint32_t) + sizeof(uint32_t) + BOOTCONFIG_MAGIC_LEN, 1);
|
|
||||||
if (!data)
|
if (!data)
|
||||||
return -ENOMEM;
|
return -ENOMEM;
|
||||||
memcpy(data, buf, size);
|
memcpy(data, buf, size);
|
||||||
@ -425,7 +428,7 @@ static int apply_xbc(const char *path, const char *xbc_path)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* To align up the total size to BOOTCONFIG_ALIGN, get padding size */
|
/* To align up the total size to BOOTCONFIG_ALIGN, get padding size */
|
||||||
total_size = stat.st_size + size + sizeof(uint32_t) * 2 + BOOTCONFIG_MAGIC_LEN;
|
total_size = stat.st_size + size + BOOTCONFIG_FOOTER_SIZE;
|
||||||
pad = ((total_size + BOOTCONFIG_ALIGN - 1) & (~BOOTCONFIG_ALIGN_MASK)) - total_size;
|
pad = ((total_size + BOOTCONFIG_ALIGN - 1) & (~BOOTCONFIG_ALIGN_MASK)) - total_size;
|
||||||
size += pad;
|
size += pad;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user