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> --- drivers/of/kexec.c | 98 +++++++++++++++++++++++++++++----------------- 1 file changed, 62 insertions(+), 36 deletions(-) diff --git a/drivers/of/kexec.c b/drivers/of/kexec.c index b9bd1cff1793..eef6f3b9939c 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,25 @@ 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; + + return fdt_find_and_del_mem_rsv(fdt, addr, size); +} + /** * remove_ima_buffer - remove the IMA buffer property and reservation from @fdt * @@ -185,29 +211,38 @@ 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; + int ret; if (!IS_ENABLED(CONFIG_HAVE_IMA_KEXEC)) return; - prop = fdt_getprop(fdt, chosen_node, "linux,ima-kexec-buffer", &len); - if (!prop) - return; - - ret = do_get_kexec_buffer(prop, len, &addr, &size); - fdt_delprop(fdt, chosen_node, "linux,ima-kexec-buffer"); - if (ret) - return; - - ret = fdt_find_and_del_mem_rsv(fdt, addr, size); + ret = remove_buffer(fdt, chosen_node, "linux,ima-kexec-buffer"); if (!ret) pr_debug("Removed old IMA buffer reservation.\n"); } #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 -EINVAL; + + return 0; + +} + /** * setup_ima_buffer - add IMA buffer information to the fdt * @image: kexec image being loaded. @@ -221,23 +256,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