Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id BD323C4332F for ; Mon, 17 Oct 2022 20:33:17 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id A441884FA6; Mon, 17 Oct 2022 22:31:09 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="Wgn4WnAF"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 76F8A84F4A; Mon, 17 Oct 2022 22:30:42 +0200 (CEST) Received: from mail-il1-x135.google.com (mail-il1-x135.google.com [IPv6:2607:f8b0:4864:20::135]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 7F95F84F64 for ; Mon, 17 Oct 2022 22:30:23 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=sjg@chromium.org Received: by mail-il1-x135.google.com with SMTP id d14so6451188ilf.2 for ; Mon, 17 Oct 2022 13:30:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=4UNvETpNdIiVevn01637noUAWjqHsTSgPPsar+jwAwg=; b=Wgn4WnAFnYdfoZ/DFAnbFTBqYE5hgPUymXMj1xyhgn2GQkHdasuQTDao/BfRzSDYCU zJJU8ILFi9r7sV9YAi1A5hDwiYNdW5iBGdcPFbQHZUMOdR/spVNXUQeCjet13OE1M7vu YdukRnhcJQydfdHtv9IHfmFJhvhHGjBNUYEnY= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=4UNvETpNdIiVevn01637noUAWjqHsTSgPPsar+jwAwg=; b=TfbNYNYqS2AFitI4QHJtJLbImkbTQWwctG03aTVdSLLz9C4Tw0hVjMNsUwZQKvkw15 479cNiVXWU80OaTkqzLV8xS8qoaOhBvWoUBHvl7Tkng14oR/vQR/0kiSyzjI7sXKgYmb rBZdrIgVswhVGMHAQkm4/ptILetk5XbtrAtPNAijltjZ9+1YRIPaDGmatZ4ZgetVBuwD YZ1Lf9OYz9VPhdxQfhEs/cBZRejOYHXBmIgkYtq/PktkIliw4Cf+eztKNjxaWPa/Ykvt JUyyT+quPV18WUWNpYczpbOmoXqnDqa4iizQAqsG/kwwYL0kv1pBahqQ0vBqGR1KwxaC cjtg== X-Gm-Message-State: ACrzQf0j2Iy5eReujF9xWkko8MK5Q6BVNCFweVpZRUJB0swBB5YeH3RC uwKCTyrTcnhag06+EYJh4OzTBwQs8Qa65w== X-Google-Smtp-Source: AMsMyM5B6Y4iNV0PGhsZ8cDMFKRdQAgS5uLwpdFE7o7YY/7YU/GPQwOJYGZcUWnzlOkn7gIfNpD7kg== X-Received: by 2002:a92:d84d:0:b0:2fa:d879:1440 with SMTP id h13-20020a92d84d000000b002fad8791440mr5859950ilq.273.1666038622652; Mon, 17 Oct 2022 13:30:22 -0700 (PDT) Received: from kea.bld.corp.google.com ([2620:15c:183:200:91e:7804:2764:d161]) by smtp.gmail.com with ESMTPSA id j9-20020a926e09000000b002e939413e83sm258071ilc.48.2022.10.17.13.30.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Oct 2022 13:30:22 -0700 (PDT) From: Simon Glass To: u-boot@lists.denx.de Cc: Anatolij Gustschin , Tom Rini , Simon Glass , Andre Przywara , Heinrich Schuchardt , Jan Kiszka , Philippe Reynes Subject: [PATCH 09/24] image: Add a function to find a script in an image Date: Mon, 17 Oct 2022 14:29:45 -0600 Message-Id: <20221017203000.2207887-10-sjg@chromium.org> X-Mailer: git-send-email 2.38.0.413.g74048e4d9e-goog In-Reply-To: <20221017203000.2207887-1-sjg@chromium.org> References: <20221017203000.2207887-1-sjg@chromium.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.6 at phobos.denx.de X-Virus-Status: Clean Split this functionality out of the 'source' command so it can be used from another place. Signed-off-by: Simon Glass --- cmd/source.c | 173 ++++++++++++++++++++++++++---------------------- include/image.h | 13 ++++ 2 files changed, 107 insertions(+), 79 deletions(-) diff --git a/cmd/source.c b/cmd/source.c index 698d9f86d96..dfa995f1df6 100644 --- a/cmd/source.c +++ b/cmd/source.c @@ -24,7 +24,6 @@ #include #include -#if defined(CONFIG_FIT) /** * get_default_image() - Return default property from /images * @@ -40,123 +39,139 @@ static const char *get_default_image(const void *fit) return fdt_getprop(fit, images_noffset, FIT_DEFAULT_PROP, NULL); } -#endif -int image_source_script(ulong addr, const char *fit_uname) +int image_locate_script(void *buf, int size, const char *fit_uname, + char **datap, uint *lenp) { ulong len; -#if defined(CONFIG_LEGACY_IMAGE_FORMAT) const struct legacy_img_hdr *hdr; -#endif u32 *data; int verify; - void *buf; -#if defined(CONFIG_FIT) const void* fit_hdr; int noffset; const void *fit_data; size_t fit_len; -#endif verify = env_get_yesno("verify"); - buf = map_sysmem(addr, 0); switch (genimg_get_format(buf)) { -#if defined(CONFIG_LEGACY_IMAGE_FORMAT) case IMAGE_FORMAT_LEGACY: - hdr = buf; + if (IS_ENABLED(CONFIG_LEGACY_IMAGE_FORMAT)) { + hdr = buf; - if (!image_check_magic (hdr)) { - puts ("Bad magic number\n"); - return 1; - } + if (!image_check_magic(hdr)) { + puts("Bad magic number\n"); + return 1; + } - if (!image_check_hcrc (hdr)) { - puts ("Bad header crc\n"); - return 1; - } + if (!image_check_hcrc(hdr)) { + puts("Bad header crc\n"); + return 1; + } + + if (verify) { + if (!image_check_dcrc(hdr)) { + puts("Bad data crc\n"); + return 1; + } + } - if (verify) { - if (!image_check_dcrc (hdr)) { - puts ("Bad data crc\n"); + if (!image_check_type(hdr, IH_TYPE_SCRIPT)) { + puts("Bad image type\n"); return 1; } - } - if (!image_check_type (hdr, IH_TYPE_SCRIPT)) { - puts ("Bad image type\n"); - return 1; - } + /* get length of script */ + data = (u32 *)image_get_data(hdr); - /* get length of script */ - data = (u32 *)image_get_data (hdr); + len = uimage_to_cpu(*data); + if (!len) { + puts("Empty Script\n"); + return 1; + } - if ((len = uimage_to_cpu (*data)) == 0) { - puts ("Empty Script\n"); - return 1; + /* + * scripts are just multi-image files with one + * component, so seek past the zero-terminated sequence + * of image lengths to get to the actual image data + */ + while (*data++); } - - /* - * scripts are just multi-image files with one component, seek - * past the zero-terminated sequence of image lengths to get - * to the actual image data - */ - while (*data++); break; -#endif -#if defined(CONFIG_FIT) case IMAGE_FORMAT_FIT: - fit_hdr = buf; - if (fit_check_format(fit_hdr, IMAGE_SIZE_INVAL)) { - puts ("Bad FIT image format\n"); - return 1; - } + if (IS_ENABLED(CONFIG_FIT)) { + fit_hdr = buf; + if (fit_check_format(fit_hdr, IMAGE_SIZE_INVAL)) { + puts("Bad FIT image format\n"); + return 1; + } - if (!fit_uname) - fit_uname = get_default_image(fit_hdr); + if (!fit_uname) + fit_uname = get_default_image(fit_hdr); - if (!fit_uname) { - puts("No FIT subimage unit name\n"); - return 1; - } + if (!fit_uname) { + puts("No FIT subimage unit name\n"); + return 1; + } - /* get script component image node offset */ - noffset = fit_image_get_node (fit_hdr, fit_uname); - if (noffset < 0) { - printf ("Can't find '%s' FIT subimage\n", fit_uname); - return 1; - } + /* get script component image node offset */ + noffset = fit_image_get_node(fit_hdr, fit_uname); + if (noffset < 0) { + printf("Can't find '%s' FIT subimage\n", + fit_uname); + return 1; + } - if (!fit_image_check_type (fit_hdr, noffset, IH_TYPE_SCRIPT)) { - puts ("Not a image image\n"); - return 1; - } + if (!fit_image_check_type(fit_hdr, noffset, + IH_TYPE_SCRIPT)) { + puts("Not a image image\n"); + return 1; + } + + /* verify integrity */ + if (verify) { + if (!fit_image_verify(fit_hdr, noffset)) { + puts("Bad Data Hash\n"); + return 1; + } + } - /* verify integrity */ - if (verify) { - if (!fit_image_verify(fit_hdr, noffset)) { - puts ("Bad Data Hash\n"); + /* get script subimage data address and length */ + if (fit_image_get_data(fit_hdr, noffset, &fit_data, &fit_len)) { + puts("Could not find script subimage data\n"); return 1; } - } - /* get script subimage data address and length */ - if (fit_image_get_data (fit_hdr, noffset, &fit_data, &fit_len)) { - puts ("Could not find script subimage data\n"); - return 1; + data = (u32 *)fit_data; + len = (ulong)fit_len; } - - data = (u32 *)fit_data; - len = (ulong)fit_len; break; -#endif default: - puts ("Wrong image format for \"source\" command\n"); - return 1; + puts("Wrong image format for \"source\" command\n"); + return -EPERM; } - debug("** Script length: %ld\n", len); - return run_command_list((char *)data, len, 0); + *datap = (char *)data; + *lenp = len; + + return 0; +} + +int image_source_script(ulong addr, const char *fit_uname) +{ + char *data; + void *buf; + uint len; + int ret; + + buf = map_sysmem(addr, 0); + ret = image_locate_script(buf, 0, fit_uname, &data, &len); + unmap_sysmem(buf); + if (ret) + return CMD_RET_FAILURE; + + debug("** Script length: %d\n", len); + return run_command_list(data, len, 0); } /**************************************************/ diff --git a/include/image.h b/include/image.h index feb8e90c2e8..1cbc51ca228 100644 --- a/include/image.h +++ b/include/image.h @@ -720,6 +720,19 @@ int fit_image_load(struct bootm_headers *images, ulong addr, */ int image_source_script(ulong addr, const char *fit_uname); +/** + * image_locate_script() - Locate the raw script in an image + * + * @buf: Address of image + * @size: Size of image in bytes + * @fit_uname: Node name of FIT image to read + * @datap: Returns pointer to raw script on success + * @lenp: Returns size of raw script on success + * @return 0 if OK, non-zero on error + */ +int image_locate_script(void *buf, int size, const char *fit_uname, + char **datap, uint *lenp); + /** * fit_get_node_from_config() - Look up an image a FIT by type * -- 2.38.0.413.g74048e4d9e-goog