Refactor IMA buffer related functions to make them reusable for carrying TPM logs across kexec. Signed-off-by: Stefan Berger <stefanb@xxxxxxxxxxxxx> Cc: Rob Herring <robh+dt@xxxxxxxxxx> Cc: Frank Rowand <frowand.list@xxxxxxxxx> Cc: Mimi Zohar <zohar@xxxxxxxxxxxxx> --- drivers/of/kexec.c | 99 +++++++++++++++++++++++++++++----------------- 1 file changed, 62 insertions(+), 37 deletions(-) diff --git a/drivers/of/kexec.c b/drivers/of/kexec.c index b9bd1cff1793..601ea9727b0e 100644 --- a/drivers/of/kexec.c +++ b/drivers/of/kexec.c @@ -115,6 +115,18 @@ static int do_get_kexec_buffer(const void *prop, int len, unsigned long *addr, return 0; } +static int get_kexec_buffer(const char *name, unsigned long *addr, size_t *size) +{ + const void *prop; + int len; + + prop = of_get_property(of_chosen, name, &len); + if (!prop) + return -ENOENT; + + return do_get_kexec_buffer(prop, len, addr, size); +} + /** * ima_get_kexec_buffer - get IMA buffer from the previous kernel * @addr: On successful return, set to point to the buffer contents. @@ -124,19 +136,14 @@ static int do_get_kexec_buffer(const void *prop, int len, unsigned long *addr, */ int ima_get_kexec_buffer(void **addr, size_t *size) { - int ret, len; + int ret; unsigned long tmp_addr; size_t tmp_size; - const void *prop; if (!IS_ENABLED(CONFIG_HAVE_IMA_KEXEC)) return -ENOTSUPP; - prop = of_get_property(of_chosen, "linux,ima-kexec-buffer", &len); - if (!prop) - return -ENOENT; - - ret = do_get_kexec_buffer(prop, len, &tmp_addr, &tmp_size); + ret = get_kexec_buffer("linux,ima-kexec-buffer", &tmp_addr, &tmp_size); if (ret) return ret; @@ -174,6 +181,28 @@ int ima_free_kexec_buffer(void) return memblock_phys_free(addr, size); } +static int remove_buffer(void *fdt, int chosen_node, const char *name) +{ + int ret, len; + unsigned long addr; + size_t size; + const void *prop; + + prop = fdt_getprop(fdt, chosen_node, name, &len); + if (!prop) + return -ENOENT; + + ret = do_get_kexec_buffer(prop, len, &addr, &size); + fdt_delprop(fdt, chosen_node, name); + if (ret) + return ret; + + ret = fdt_find_and_del_mem_rsv(fdt, addr, size); + if (!ret) + pr_debug("Remove old %s buffer reserveration", name); + return ret; +} + /** * remove_ima_buffer - remove the IMA buffer property and reservation from @fdt * @@ -185,29 +214,34 @@ int ima_free_kexec_buffer(void) */ static void remove_ima_buffer(void *fdt, int chosen_node) { - int ret, len; - unsigned long addr; - size_t size; - const void *prop; - if (!IS_ENABLED(CONFIG_HAVE_IMA_KEXEC)) return; - prop = fdt_getprop(fdt, chosen_node, "linux,ima-kexec-buffer", &len); - if (!prop) - return; + remove_buffer(fdt, chosen_node, "linux,ima-kexec-buffer"); +} - ret = do_get_kexec_buffer(prop, len, &addr, &size); - fdt_delprop(fdt, chosen_node, "linux,ima-kexec-buffer"); +#ifdef CONFIG_IMA_KEXEC +static int setup_buffer(void *fdt, int chosen_node, const char *name, + uint64_t addr, uint64_t size) +{ + int ret; + + if (!size) + return 0; + + ret = fdt_appendprop_addrrange(fdt, 0, chosen_node, + name, addr, size); + if (ret < 0) + return -EINVAL; + + ret = fdt_add_mem_rsv(fdt, addr, size); if (ret) - return; + return -EINVAL; + + return 0; - ret = fdt_find_and_del_mem_rsv(fdt, addr, size); - if (!ret) - pr_debug("Removed old IMA buffer reservation.\n"); } -#ifdef CONFIG_IMA_KEXEC /** * setup_ima_buffer - add IMA buffer information to the fdt * @image: kexec image being loaded. @@ -221,23 +255,14 @@ static int setup_ima_buffer(const struct kimage *image, void *fdt, { int ret; - if (!image->ima_buffer_size) - return 0; - - ret = fdt_appendprop_addrrange(fdt, 0, chosen_node, - "linux,ima-kexec-buffer", - image->ima_buffer_addr, - image->ima_buffer_size); - if (ret < 0) - return -EINVAL; - - ret = fdt_add_mem_rsv(fdt, image->ima_buffer_addr, - image->ima_buffer_size); + ret = setup_buffer(fdt, chosen_node, "linux,ima-kexec-buffer", + image->ima_buffer_addr, image->ima_buffer_size); if (ret) - return -EINVAL; + return ret; - pr_debug("IMA buffer at 0x%llx, size = 0x%zx\n", - image->ima_buffer_addr, image->ima_buffer_size); + if (image->ima_buffer_addr) + pr_debug("IMA buffer at 0x%llx, size = 0x%zx\n", + image->ima_buffer_addr, image->ima_buffer_size); return 0; } -- 2.35.1