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 71206C4708D for ; Fri, 6 Jan 2023 14:55:47 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 52A6385613; Fri, 6 Jan 2023 15:53:33 +0100 (CET) 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="ELGMdPE7"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 0B3268563D; Fri, 6 Jan 2023 15:53:19 +0100 (CET) Received: from mail-il1-x134.google.com (mail-il1-x134.google.com [IPv6:2607:f8b0:4864:20::134]) (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 716FB855FD for ; Fri, 6 Jan 2023 15:52:58 +0100 (CET) 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-x134.google.com with SMTP id m15so1078489ilj.8 for ; Fri, 06 Jan 2023 06:52:58 -0800 (PST) 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=iKwcke0CYXnfg0399Ao6BztAcWGDJxzGxqSuo9Rgrmk=; b=ELGMdPE7gKJGNF4t2URz2TwkzDc5q1Bk7jmr9YqtdPgw40zbMFwWcpVYVpEwKyZ23k VuqKcDyxQwqQ39zH/gn1iT/Mr7h52TFUyyv1CwJJaVUrfW+UNfSj/TN/j5bZUdVWZ+NK 6QrPVDSqgEo2Sz6LdrinAOP0NycWfgGLEHYTw= 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=iKwcke0CYXnfg0399Ao6BztAcWGDJxzGxqSuo9Rgrmk=; b=n1TWg72YIeJTzoqo4efvYCKSF49Y89I38p/jrhxSDGunYXpXhzEMZYnt5Nu0L/swCq eebNcH+79NAt5o6H76xusR0156rD30k35RMuB2sv4ac9w099ikpC3aM9emII3Yf4lM35 GZO8KL+wEa0L1GLbhQf4xhaf3DGm02yL+R66eswa30t/akW8VcYMsHqltrMIPjCLY8Fc 7gW1jAA7lAfKFnTDtiTm+o/r3IGLhz8z5E34iItdiQM/55tHOfn0Tgc+Qh3jcEDJBWPT p6LH0bF/3M9OCng1Ir72Z1FQ+KjpbjXk2SbddQdLFal2gC6h86QLEn6Q8bP/XXWh3DP6 VGMQ== X-Gm-Message-State: AFqh2konrUWGUKbCJr/avybU9EB7tyywq8Os9H+9HaeZT8t+mYS9RKAM b2KAFtbiTirBl19lATnOpWo8ZISrOrCyx1lS7TM= X-Google-Smtp-Source: AMrXdXugxGBR9PzV12EuDj6zbbM89cl7RGQh9GmqEvNClsUNLmZuzEQm+ZFrGq+WIckWO0v1KxjoIg== X-Received: by 2002:a92:ab06:0:b0:303:8ffb:9345 with SMTP id v6-20020a92ab06000000b003038ffb9345mr40521479ilh.17.1673016777867; Fri, 06 Jan 2023 06:52:57 -0800 (PST) Received: from sjg1.lan (c-71-56-217-229.hsd1.co.comcast.net. [71.56.217.229]) by smtp.gmail.com with ESMTPSA id w20-20020a05663800d400b00375783003fcsm360273jao.136.2023.01.06.06.52.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 06 Jan 2023 06:52:57 -0800 (PST) From: Simon Glass To: U-Boot Mailing List Cc: Anatolij Gustschin , Tom Rini , Heinrich Schuchardt , Simon Glass , Joe Hershberger , Marek Vasut Subject: [PATCH v3 09/25] image: Add a function to find a script in an image Date: Fri, 6 Jan 2023 08:52:27 -0600 Message-Id: <20230106145243.411626-10-sjg@chromium.org> X-Mailer: git-send-email 2.39.0.314.g84b9a713c41-goog In-Reply-To: <20230106145243.411626-1-sjg@chromium.org> References: <20230106145243.411626-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. For now leave it where it is, but a future patch will move it out of cmd/ Signed-off-by: Simon Glass --- (no changes since v1) 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 6f21dafba8c..1e67335eedc 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.39.0.314.g84b9a713c41-goog