Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp695152pxu; Thu, 7 Jan 2021 16:09:39 -0800 (PST) X-Google-Smtp-Source: ABdhPJznSEKCIiCp3cIne/TL8cNbjEn1B3VMAUQHhvNWQnsgJKXk2HAr15kRnMRPpdDJk8Mmt6sm X-Received: by 2002:a05:6402:2207:: with SMTP id cq7mr3443566edb.310.1610064579063; Thu, 07 Jan 2021 16:09:39 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1610064579; cv=none; d=google.com; s=arc-20160816; b=0XWsWZIK8J+qE6IZQmHarrV3VbSxNdbisYBjioTDYYuuYC7SiYx+PvLHI26PsQd4Xu 3/iG0sC52n2oZBzn27WUtUhBgHr0zc1BWUyD9v7lLHWGg4AF17ixHAPO4C/c13wDpoaA YXgyyK7D9VwzZRXO2VcUJVbUUaUG7r/MYXFK9Qy81nquebQ4bQc0nQGmzx/cWb4R/d4I p2s9zVB69Uik+tiORTBSPf/dr9OMP271gAOx1v8qRmxMP1EIeKZQc/x59YA0/WcIpREd ZOQCpc7SoxH0gViCsnbHi9Ov+1pLRb2wWpOyr/g3n5sGNax0o1AsSl29oWAyPAcmKnzj nMCA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=/+e7mJ1mrcrNXilPv4TPaogW9Nw3A2mzcQj+9eVwG/Q=; b=bgDFffhoRC3PUqOT0IT3EWlsx9qYSS6OnLtEMY3cjTaV2/8j0NkDzxLOVJjXg119xr 2yc0IGIm+9MI+lggAh4+NHqXjpfy0NADZ1mYzJXBZ3jeZ4E/eTnzfB+UzkDnC5X5fl1e Pqu3eaEAD9m8OE2VziBfPX5WR4UmlU46rVxO681ExIdUE4eHNGKpC13xGtVs1YHHFE8x WbjzZJy5vpV5AGd9XgAgS+7SAV8FtsO2/RC0yiSX5iwZDiFozDo/9UHebMp7u855vu+l ggVv28iOsnee2ByehdbyQ4E9d39+t9ZFn/PzIVSFFBCJJBWBOBaIC7Ae2Rl+pbaM8jdI yokg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="bbYGAE/b"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id gw26si2839455ejb.537.2021.01.07.16.09.15; Thu, 07 Jan 2021 16:09:39 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="bbYGAE/b"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729411AbhAHAIK (ORCPT + 99 others); Thu, 7 Jan 2021 19:08:10 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34206 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729305AbhAHAIK (ORCPT ); Thu, 7 Jan 2021 19:08:10 -0500 Received: from mail-oi1-x235.google.com (mail-oi1-x235.google.com [IPv6:2607:f8b0:4864:20::235]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 035D5C0612F4 for ; Thu, 7 Jan 2021 16:07:29 -0800 (PST) Received: by mail-oi1-x235.google.com with SMTP id f132so9404201oib.12 for ; Thu, 07 Jan 2021 16:07:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=/+e7mJ1mrcrNXilPv4TPaogW9Nw3A2mzcQj+9eVwG/Q=; b=bbYGAE/b+oEutRi1w4PJfRLGhGMm1Iii6kPJliriSbZCFYn0zdpqoObnarD1SdQdKg MKvZlSc6cKj24gykJuPXUvd3vMZ5omYddXS0qpymc1nohnVKm0k53HNJzA5vPXMKDRqA p8S9m1OPgXGbD1vahZEY1tmZfWBfuoyObRkknk4CygwqDmBIVP51l5GwyM2eTmCJjfbv rUH4hY9m+tWe9H+bLg5og3CGGepX6kxKvjHGGzcGjZxb65XtOz7tmqBdc6LxWtpjkieW NVk5LmfAmTSjAHXwEk6Lmv5HBIvRP+Emf97ELyI3ToqUxW0zTY8i0aWxq1WxDVqtGEaZ XJag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=/+e7mJ1mrcrNXilPv4TPaogW9Nw3A2mzcQj+9eVwG/Q=; b=o8F+7FgijmTLBfQd/eOIUC7UAoFL3KF2ig4ZajE+DB4qzEt4CC+JeJJ+gEMBJc7Qeh i4z/jNJGEO3V3XC7/FFvY4y/dkK1peRe0nkGAO9wRUfmhcJ9MMDQPqInPmXzvxfNfd7L ClKskITXoM58AiEz92z9c5brq7jjPVDK96usVHxv8a+qqpmBwQM3T6jHyjhwZiG/LPNH XUeWjFLGRH5IE85SVaf7hDkcPJ8RBDmP7vdHN/XphrVdfjqNyEAHAQcu84L7V8bXnWC0 fek3Trks5GmWoz5Sf2R30vJ+adVyy3BO8oyXNHT4hm3tjOJ7wFV2Ngvm9u0t5fGBUUwC +ekw== X-Gm-Message-State: AOAM532Vod52uCWeEjmYacQNx2Tg6BLrDklm1jwm+bSz+Og8qJY0TFbb Go/RTHdjw2PGeYoYCgLtC3jh0A== X-Received: by 2002:aca:4dc3:: with SMTP id a186mr681591oib.107.1610064449329; Thu, 07 Jan 2021 16:07:29 -0800 (PST) Received: from builder.lan (104-57-184-186.lightspeed.austtx.sbcglobal.net. [104.57.184.186]) by smtp.gmail.com with ESMTPSA id u3sm1424640otk.31.2021.01.07.16.07.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Jan 2021 16:07:28 -0800 (PST) Date: Thu, 7 Jan 2021 18:07:27 -0600 From: Bjorn Andersson To: Siddharth Gupta Cc: agross@kernel.org, ohad@wizery.com, linux-arm-msm@vger.kernel.org, linux-remoteproc@vger.kernel.org, linux-kernel@vger.kernel.org, psodagud@codeaurora.org, rishabhb@codeaurora.org Subject: Re: [PATCH 2/3] soc: qcom: mdt_loader: Handle split bins correctly Message-ID: References: <1609968211-7579-1-git-send-email-sidgup@codeaurora.org> <1609968211-7579-3-git-send-email-sidgup@codeaurora.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1609968211-7579-3-git-send-email-sidgup@codeaurora.org> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed 06 Jan 15:23 CST 2021, Siddharth Gupta wrote: > It may be that the offset of the first program header lies inside the mdt's > filesize, in this case the loader would incorrectly assume that the bins > were not split. The loading would then continue on to fail for split bins. > This change updates the logic used by the mdt loader to understand whether > the firmware images are split or not. It figures this out by checking if > each program header's segment lies within the file or not. > > Signed-off-by: Siddharth Gupta > --- > drivers/soc/qcom/mdt_loader.c | 60 +++++++++++++++++++++++++++---------------- > 1 file changed, 38 insertions(+), 22 deletions(-) > > diff --git a/drivers/soc/qcom/mdt_loader.c b/drivers/soc/qcom/mdt_loader.c > index 813216d..c9bbd8c 100644 > --- a/drivers/soc/qcom/mdt_loader.c > +++ b/drivers/soc/qcom/mdt_loader.c > @@ -31,6 +31,26 @@ static bool mdt_phdr_valid(const struct elf32_phdr *phdr) > return true; > } > > +static bool qcom_mdt_bins_are_split(const struct firmware *fw) > +{ > + const struct elf32_phdr *phdrs; > + const struct elf32_hdr *ehdr; > + uint64_t seg_start, seg_end; > + int i; > + > + ehdr = (struct elf32_hdr *)fw->data; > + phdrs = (struct elf32_phdr *)(ehdr + 1); > + > + for (i = 0; i < ehdr->e_phnum; i++) { > + seg_start = phdrs[i].p_offset; > + seg_end = phdrs[i].p_offset + phdrs[i].p_filesz; > + if (seg_start > fw->size || seg_end > fw->size) > + return true; > + } > + > + return false; > +} > + > /** > * qcom_mdt_get_size() - acquire size of the memory region needed to load mdt > * @fw: firmware object for the mdt file > @@ -118,7 +138,7 @@ void *qcom_mdt_read_metadata(const struct firmware *fw, size_t *data_len) > return ERR_PTR(-ENOMEM); > > /* Is the header and hash already packed */ > - if (ehdr_size + hash_size == fw->size) > + if (qcom_mdt_bins_are_split(fw)) > hash_offset = phdrs[0].p_filesz; > else > hash_offset = phdrs[hash_index].p_offset; > @@ -150,6 +170,7 @@ static int __qcom_mdt_load(struct device *dev, const struct firmware *fw, > void *metadata; > char *fw_name; > bool relocate = false; > + bool is_split; > void *ptr; > int ret = 0; > int i; > @@ -157,6 +178,7 @@ static int __qcom_mdt_load(struct device *dev, const struct firmware *fw, > if (!fw || !mem_region || !mem_phys || !mem_size) > return -EINVAL; > > + is_split = qcom_mdt_bins_are_split(fw); > ehdr = (struct elf32_hdr *)fw->data; > phdrs = (struct elf32_phdr *)(ehdr + 1); > > @@ -238,28 +260,22 @@ static int __qcom_mdt_load(struct device *dev, const struct firmware *fw, > > ptr = mem_region + offset; > > - if (phdr->p_filesz && phdr->p_offset < fw->size) { > - /* Firmware is large enough to be non-split */ > - if (phdr->p_offset + phdr->p_filesz > fw->size) { > - dev_err(dev, > - "failed to load segment %d from truncated file %s\n", > - i, firmware); > - ret = -EINVAL; > - break; > + if (phdr->p_filesz) { > + if (!is_split) { In an effort to reduce the diff size and avoid adding another level of indentation, how about making the conditionals: if (is_split && phdr->p_filesz) { memcpy(); } else if (phdr->p_filesz) { ... } Apart from that I think this patch looks good! Regards, Bjorn > + /* Firmware is large enough to be non-split */ > + memcpy(ptr, fw->data + phdr->p_offset, phdr->p_filesz); > + } else { > + /* Firmware not large enough, load split-out segments */ > + snprintf(fw_name + fw_name_len - 3, 4, "b%02d", i); > + ret = request_firmware_into_buf(&seg_fw, fw_name, dev, > + ptr, phdr->p_filesz); > + if (ret) { > + dev_err(dev, "failed to load %s\n", fw_name); > + break; > + } > + > + release_firmware(seg_fw); > } > - > - memcpy(ptr, fw->data + phdr->p_offset, phdr->p_filesz); > - } else if (phdr->p_filesz) { > - /* Firmware not large enough, load split-out segments */ > - sprintf(fw_name + fw_name_len - 3, "b%02d", i); > - ret = request_firmware_into_buf(&seg_fw, fw_name, dev, > - ptr, phdr->p_filesz); > - if (ret) { > - dev_err(dev, "failed to load %s\n", fw_name); > - break; > - } > - > - release_firmware(seg_fw); > } > > if (phdr->p_memsz > phdr->p_filesz) > -- > Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, > a Linux Foundation Collaborative Project >