From patchwork Mon Jan 28 13:54:44 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Walleij X-Patchwork-Id: 156740 Delivered-To: patch@linaro.org Received: by 2002:a02:48:0:0:0:0:0 with SMTP id 69csp3453461jaa; Mon, 28 Jan 2019 05:55:24 -0800 (PST) X-Google-Smtp-Source: ALg8bN4DuQLIAX9pP6WDwrKKJxBB+HsLZCYzQapjI0YqwW8WoBdAOk//+hJF4nNrSyUKOJH1PgFG X-Received: by 2002:a62:345:: with SMTP id 66mr21872428pfd.189.1548683724425; Mon, 28 Jan 2019 05:55:24 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1548683724; cv=none; d=google.com; s=arc-20160816; b=RZ518SSRAyzwCBHXDH3Y12aFvOZmjrwPUimFVT+Wxy0E+0drKtJAf8tf/zRwvcCKAq nx3z71adIAUaw6GJDUcsBwiSWazXBmL738w8cjYyuq7GQsK0KMtfUwAzQdZdT4gwGuGI MCwDL1/encsVIDouvhUnrNKvHaT7rBOU5QGkFeZgjNviBSmrKXrwfQt/bsUin71Xh4x4 fIKOP/RdSdKhrlFa+BbLL5+Ru6BAMNrHRB0SUInJwyC6sX2EtOpHtvbp0wfk6xLcP2fb RKDt/qeDxvH+IJ1Eh3eez+IjHLoWfeyMCnERScwiDGfPs7zkv2mG9GlpC3kNlUk0ieuX 1Ikw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:sender:content-transfer-encoding:cc:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:mime-version:references:in-reply-to:message-id:date :subject:to:from:dkim-signature:dkim-signature; bh=0f6MnjWOdxMV+/5/XQ7AnOefWp68VqcDmV8j2u0XaT4=; b=MI1EVxi0JIFc7ahox01TuKqlxvYfWXVZfg+3fD6uoRCIJ/GCowtUXMOLpMdgBwqIS9 j58UH/uFB8Y1b095rkKU3NXUS2+diePE9E95cLAwfvpfjeV0oZKWpLIJglH9brrWGPlA pldANo7/lUtwupFFpI6IkwqU1weAP80ocx3WIUTKtYnjFcmXNWgObnypmmG4LCFOvuDM +A4SAS6kbYIVDF82v3uddnh4XYlt9S/xvuiep7Syhk8HdyKyLcNs02kekK5r/dlpEcJR Ru4qhEIoB98/rX7y0Bc1cKBl3aB2sVmRafAzE4Aw6nqofHH1GsOljkHAZgnMDg6IfO+0 QPRg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@lists.infradead.org header.s=bombadil.20170209 header.b=lV4s8Red; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=UzHReQfF; spf=pass (google.com: best guess record for domain of linux-mtd-bounces+patch=linaro.org@lists.infradead.org designates 2607:7c80:54:e::133 as permitted sender) smtp.mailfrom="linux-mtd-bounces+patch=linaro.org@lists.infradead.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from bombadil.infradead.org (bombadil.infradead.org. [2607:7c80:54:e::133]) by mx.google.com with ESMTPS id u7si34370829pfu.270.2019.01.28.05.55.24 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 28 Jan 2019 05:55:24 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-mtd-bounces+patch=linaro.org@lists.infradead.org designates 2607:7c80:54:e::133 as permitted sender) client-ip=2607:7c80:54:e::133; Authentication-Results: mx.google.com; dkim=pass header.i=@lists.infradead.org header.s=bombadil.20170209 header.b=lV4s8Red; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=UzHReQfF; spf=pass (google.com: best guess record for domain of linux-mtd-bounces+patch=linaro.org@lists.infradead.org designates 2607:7c80:54:e::133 as permitted sender) smtp.mailfrom="linux-mtd-bounces+patch=linaro.org@lists.infradead.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=0f6MnjWOdxMV+/5/XQ7AnOefWp68VqcDmV8j2u0XaT4=; b=lV4s8RedtGiN+v YBl6i56KhOyRYijU8zZNMLb3b7lKap1FD2pEkfYd9F/Q/JIKAg8rnWndAJfm4FSQavmh3JbdOva25 a9sUSxCAaqC6fSengltUW2MAv9d2FAQdhEjtWwyZE1w/bHsA4LZ2IsdTXly8YBtGTvDFvYEMwDxss hAYPpn8F4T6iI8xckV28Nt2ARCs9Nrmx+JPT+m14q+YGh8B3hOa9Pp9lQ5v8eO0JLThwM3pmJjTOz hEXzbFm/pNfD86bfbNeFH0fIWeUnzgdAJcT+EGEqqT7zs0BXBr0aiXMLsk4G4sqtcZ2g0iCcDpqwB zMX6N3dFtUezyTwIJxPQ==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1go7Nc-0000lX-Vk; Mon, 28 Jan 2019 13:55:16 +0000 Received: from mail-lj1-x243.google.com ([2a00:1450:4864:20::243]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1go7NS-00089F-BK for linux-mtd@lists.infradead.org; Mon, 28 Jan 2019 13:55:08 +0000 Received: by mail-lj1-x243.google.com with SMTP id g11-v6so14303799ljk.3 for ; Mon, 28 Jan 2019 05:55:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Ctr64GnyMPW2Z4vCyliLOxUJF1YjKnteZrfOLWzfbsw=; b=UzHReQfFZkG3wpvcisKA9TM1+5+xPHfRmqvagRHVr5xKeX9PicLEYasdHadFG2bKKO ihw+KeE7SnBx4oa+sTxLR4/c2IaTina9Xvi2Pupp0fkL0mAezjHYunfUh+zQqLJVVnY+ JmQMrIZdUzrGd0aFCREu/6/sET01nFIOkqs+I= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Ctr64GnyMPW2Z4vCyliLOxUJF1YjKnteZrfOLWzfbsw=; b=mE3QXPwBYKLe3kBBdweL1kXq4EGwJB1Jo43o0aAMMSGYCtZQpYWqEgK7pyAR6ciMOI tDz74tTI67/aYwKazicuF+ceTkHXewQdgsfhVW/eCcPc7gwKul1Un721bHFGF7J8F/IN AV59mnfcR8tPBaZn/Ruh+HHl5U8FE8Yzsiu+Fw5dG23nNtxZHXLikANHFGI/vzLx4SCy WNu8TTQRH62k78d3J9LxDc2m1dqcjt43fE/ugMm0xl30CFlYrBIiXMVnzU8fqhgljbqN FbsD1hYBpy83mc8HFRyL8i+VIi8+banOvxjwNB3agUw+5oSzl+xJA4wah3MwJDYtifmW +VHA== X-Gm-Message-State: AJcUukfAGyNESU1quAbJ9XX7sqwsmsj+7B03JBKzSeXIYoYo42wMFYMg IwgKgqOakMm4u3ZySoIT830K+g== X-Received: by 2002:a2e:6595:: with SMTP id e21-v6mr17134967ljf.123.1548683704125; Mon, 28 Jan 2019 05:55:04 -0800 (PST) Received: from genomnajs.ideon.se ([85.235.10.227]) by smtp.gmail.com with ESMTPSA id a2-v6sm3050503lji.13.2019.01.28.05.55.02 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 28 Jan 2019 05:55:03 -0800 (PST) From: Linus Walleij To: David Woodhouse , Brian Norris , Boris Brezillon , Marek Vasut , Richard Weinberger , Cyrille Pitchen , linux-mtd@lists.infradead.org Subject: [PATCH 1/6] mtd: afs: simplify partition parsing Date: Mon, 28 Jan 2019 14:54:44 +0100 Message-Id: <20190128135449.15555-2-linus.walleij@linaro.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190128135449.15555-1-linus.walleij@linaro.org> References: <20190128135449.15555-1-linus.walleij@linaro.org> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20190128_055506_473445_A895853F X-CRM114-Status: GOOD ( 23.38 ) X-Spam-Score: -0.2 (/) X-Spam-Report: SpamAssassin version 3.4.2 on bombadil.infradead.org summary: Content analysis details: (-0.2 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at http://www.dnswl.org/, no trust [2a00:1450:4864:20:0:0:0:243 listed in] [list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain X-BeenThere: linux-mtd@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: Linux MTD discussion mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Linus Walleij , Liviu Dudau , Ryan Harkin Sender: "linux-mtd" Errors-To: linux-mtd-bounces+patch=linaro.org@lists.infradead.org This simplifies the AFS partition parsing to make the code more straight-forward and readable. Before this patch the code tried to calculate the memory required to hold the partition info by adding up the sizes of the strings of the names and adding that to a single memory allocation, indexing the name pointers in front of the struct mtd_partition allocations so all allocated data was in one chunk. This is overzealous. Instead use kstrdup and bail out, kfree():ing the memory used for MTD partitions and names alike on the errorpath. In the process rename the index variable from idx to i. Cc: Ryan Harkin Cc: Liviu Dudau Signed-off-by: Linus Walleij --- drivers/mtd/parsers/afs.c | 67 +++++++++++++++++++-------------------- 1 file changed, 32 insertions(+), 35 deletions(-) -- 2.20.1 ______________________________________________________ Linux MTD discussion mailing list http://lists.infradead.org/mailman/listinfo/linux-mtd/ Acked-by: Liviu Dudau diff --git a/drivers/mtd/parsers/afs.c b/drivers/mtd/parsers/afs.c index 3679e1d22595..c489938cd665 100644 --- a/drivers/mtd/parsers/afs.c +++ b/drivers/mtd/parsers/afs.c @@ -166,9 +166,9 @@ static int parse_afs_partitions(struct mtd_info *mtd, struct mtd_part_parser_data *data) { struct mtd_partition *parts; - u_int mask, off, idx, sz; + u_int mask, off, sz; int ret = 0; - char *str; + int i; /* * This is the address mask; we use this to mask off out of @@ -181,78 +181,75 @@ static int parse_afs_partitions(struct mtd_info *mtd, * partition information. We include in this the size of * the strings. */ - for (idx = off = sz = 0; off < mtd->size; off += mtd->erasesize) { - struct image_info_v1 iis; + for (i = off = sz = 0; off < mtd->size; off += mtd->erasesize) { u_int iis_ptr, img_ptr; ret = afs_read_footer_v1(mtd, &img_ptr, &iis_ptr, off, mask); if (ret < 0) - break; + return ret; if (ret) { - ret = afs_read_iis_v1(mtd, &iis, iis_ptr); - if (ret < 0) - break; - if (ret == 0) - continue; - sz += sizeof(struct mtd_partition); - sz += strlen(iis.name) + 1; - idx += 1; + i += 1; } } - if (!sz) - return ret; + if (!i) + return 0; parts = kzalloc(sz, GFP_KERNEL); if (!parts) return -ENOMEM; - str = (char *)(parts + idx); - /* * Identify the partitions */ - for (idx = off = 0; off < mtd->size; off += mtd->erasesize) { + for (i = off = 0; off < mtd->size; off += mtd->erasesize) { struct image_info_v1 iis; u_int iis_ptr, img_ptr; /* Read the footer. */ ret = afs_read_footer_v1(mtd, &img_ptr, &iis_ptr, off, mask); if (ret < 0) - break; + goto out_free_parts; if (ret == 0) continue; /* Read the image info block */ ret = afs_read_iis_v1(mtd, &iis, iis_ptr); if (ret < 0) - break; + goto out_free_parts; if (ret == 0) continue; - strcpy(str, iis.name); + parts[i].name = kstrdup(iis.name, GFP_KERNEL); + if (!parts[i].name) { + ret = -ENOMEM; + goto out_free_parts; + } - parts[idx].name = str; - parts[idx].size = (iis.length + mtd->erasesize - 1) & ~(mtd->erasesize - 1); - parts[idx].offset = img_ptr; - parts[idx].mask_flags = 0; + parts[i].size = (iis.length + mtd->erasesize - 1) & ~(mtd->erasesize - 1); + parts[i].offset = img_ptr; + parts[i].mask_flags = 0; printk(" mtd%d: at 0x%08x, %5lluKiB, %8u, %s\n", - idx, img_ptr, parts[idx].size / 1024, - iis.imageNumber, str); - - idx += 1; - str = str + strlen(iis.name) + 1; - } + i, img_ptr, parts[i].size / 1024, + iis.imageNumber, parts[i].name); - if (!idx) { - kfree(parts); - parts = NULL; + i += 1; } *pparts = parts; - return idx ? idx : ret; + return i; + +out_free_parts: + while (i >= 0) { + if (parts[i].name) + kfree(parts[i].name); + i--; + } + kfree(parts); + *pparts = NULL; + return ret; } static const struct of_device_id mtd_parser_afs_of_match_table[] = { From patchwork Mon Jan 28 13:54:45 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Walleij X-Patchwork-Id: 156741 Delivered-To: patch@linaro.org Received: by 2002:a02:48:0:0:0:0:0 with SMTP id 69csp3453704jaa; Mon, 28 Jan 2019 05:55:41 -0800 (PST) X-Google-Smtp-Source: AHgI3IaA9FC0XRs5SpBVBt4ERGHogioMuZgRr/IT3muieqROIBZtNR84PP0GDF3BKlqVyG3Oi3aX X-Received: by 2002:a62:4618:: with SMTP id t24mr4700576pfa.139.1548683741679; Mon, 28 Jan 2019 05:55:41 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1548683741; cv=none; d=google.com; s=arc-20160816; b=JiDjXjeNRbZNvnH3tDiuogaWtZkIoFL5Cy7FIDx3wTwDPprjMPp/dj5y7IKvqF0S40 Z8TJ/593MJ2iaawuyRsnaL6qhh7t0kQxUC/x1/FsD0BA0hmHCP9/s5rq6BTAlQcrd8YP 1GY/ftRvB2hcBBiwwKaO4q9GLxest8t6sFjhH/xBbMWkaSwIkRgz5+G6rma0W4CoaY5x ReJYpH3jh84FZbKGmbpgP8uav7Q4WrAlp/oAJrNXgbNXKLiEaUUXRJhvwvHhaxbTwf4d QoUmoaJtL6c4gc2Cl/3DajvsR9meTSwVDGekWpSwIORr9cJhcNPBxAXyeGVyfLlL7S/B BFbg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:sender:content-transfer-encoding:cc:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:mime-version:references:in-reply-to:message-id:date :subject:to:from:dkim-signature:dkim-signature; bh=6pNKEvb+zIK1zzkJPdwE73I1GcIhybBR4Tz+aLFuKK4=; b=f1RaxI93KhS2gp7BLymBZBG6yPdOwn1iXvRP50BjKs8lc6y+54dvmDYi6wC5ZS3VK/ OUsag7G4on/Mz3/YcFYr6ZV+Xt7PnG+hMSXpez5MY6pdbEin4nPhXZOR7NcMcTaicsJn rcYKV/EpcY7cRsids6Ycu5H4kgvhwm/UreJ7U2Is+yG8LM1mWEpGNumBIsLLVbCoUN62 qF27+q1agA7/Ey46KwTz339abfzcOFAUnKhyoAusftJlOZQsDiQS3RWrWSVYVrIPO19e FvrCdmY+aLgpYO6iQVE/ZAghWcLx3Lcl9NO369jS+dC1ZOn/FoxCLnqFBzkcZw5VXcIr kwkA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@lists.infradead.org header.s=bombadil.20170209 header.b=pIHOBKt3; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=FcJNsc+1; spf=pass (google.com: best guess record for domain of linux-mtd-bounces+patch=linaro.org@lists.infradead.org designates 2607:7c80:54:e::133 as permitted sender) smtp.mailfrom="linux-mtd-bounces+patch=linaro.org@lists.infradead.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from bombadil.infradead.org (bombadil.infradead.org. [2607:7c80:54:e::133]) by mx.google.com with ESMTPS id s62si3189928pgc.113.2019.01.28.05.55.41 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 28 Jan 2019 05:55:41 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-mtd-bounces+patch=linaro.org@lists.infradead.org designates 2607:7c80:54:e::133 as permitted sender) client-ip=2607:7c80:54:e::133; Authentication-Results: mx.google.com; dkim=pass header.i=@lists.infradead.org header.s=bombadil.20170209 header.b=pIHOBKt3; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=FcJNsc+1; spf=pass (google.com: best guess record for domain of linux-mtd-bounces+patch=linaro.org@lists.infradead.org designates 2607:7c80:54:e::133 as permitted sender) smtp.mailfrom="linux-mtd-bounces+patch=linaro.org@lists.infradead.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=6pNKEvb+zIK1zzkJPdwE73I1GcIhybBR4Tz+aLFuKK4=; b=pIHOBKt334edFo w+5EivmzVe14ajI6O+7MkWGivX2cZtFoGecUaLFnGG2Vp2O3X+63YDktjetzVUsB2GyXKnMLbA07D +Mn53HdrrOEoLDLxnqnc1NrKE7O/GHcyH7dDq+ajXr33Yqu5cykag07KTFwKauu4GzhHHm8G1LwoV kkI1YcmVM6MVCt/Yb3dc8hLaTp0wzekobam0uRDhO/FFMU1xW+SC9DYzL8+oTCfi0pTTNjNE6P/mg /pNgRD2in2I6oEVixS9x79FQfToLQ+cbx8dyrF6/nuviC9MK6O3+TGR0vVVj4k/266eHbGbp4+GjZ dCUagkf+w7bGKcJlkqXg==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1go7Nq-00013l-31; Mon, 28 Jan 2019 13:55:30 +0000 Received: from mail-lj1-x244.google.com ([2a00:1450:4864:20::244]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1go7NV-0008UU-Q8 for linux-mtd@lists.infradead.org; Mon, 28 Jan 2019 13:55:15 +0000 Received: by mail-lj1-x244.google.com with SMTP id k15-v6so14264501ljc.8 for ; Mon, 28 Jan 2019 05:55:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=rK8cIESwiOWv9aGmz6Jj/RiNPyMVDDtZglSyhCaz9xI=; b=FcJNsc+16HPm8u7fBLdGIfnyu4j5eGMs7ipFBAsPOk9wkYodNbRAdv7tF1dhgesTGh SlMQmeEehJoF7Dh1BVZsPUwAGtzftskeSq0pfzraY+MFh16qiYXd8M32wKoSP01zfH+T mj5R25Wu4QXBE2L1RtktZI31vwwwAv2dVgMRU= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=rK8cIESwiOWv9aGmz6Jj/RiNPyMVDDtZglSyhCaz9xI=; b=mWiq+mldrznS0bN2/7B3idNfd5ex3+szBVvzK/281lry3FcYPN2o0g9yqfmwyHs+b4 do7wO5Gw2f+9qhquxgidjDM9q+zWSrle5+wz72lcWX1wssHU/etYV2xrFuRStbHRplyb o8Rfuom8s3YfJYE2CiDN1hvcp2y+5IENqhTB1jvfzeCMsfkCUEztXw8nZCIPt6syrXm+ Nq+GyKPmpUboXOIR9HjM7G9LT7RYkLieRXQwr74n32iiijBrXTjyQgyKwGqPbzxKHETF K8tCdn5836o6BPKnX7jVa77pfh0Y08jZHUb+9VuRk3dI1lgVuECWcFBooA3bVL5Cu1Op 4ohA== X-Gm-Message-State: AJcUuke3PTY+ZtKejp9/LLycGdkqEmhIGhsAwe0LgrdMaqrhtWyXbMqe BKfagVi3dcPFYWaKNaAT4cQVXg== X-Received: by 2002:a2e:9ad0:: with SMTP id p16-v6mr17828699ljj.102.1548683707792; Mon, 28 Jan 2019 05:55:07 -0800 (PST) Received: from genomnajs.ideon.se ([85.235.10.227]) by smtp.gmail.com with ESMTPSA id a2-v6sm3050503lji.13.2019.01.28.05.55.06 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 28 Jan 2019 05:55:06 -0800 (PST) From: Linus Walleij To: David Woodhouse , Brian Norris , Boris Brezillon , Marek Vasut , Richard Weinberger , Cyrille Pitchen , linux-mtd@lists.infradead.org Subject: [PATCH 2/6] mtd: afs: simplify partition detection Date: Mon, 28 Jan 2019 14:54:45 +0100 Message-Id: <20190128135449.15555-3-linus.walleij@linaro.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190128135449.15555-1-linus.walleij@linaro.org> References: <20190128135449.15555-1-linus.walleij@linaro.org> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20190128_055510_272338_5570E7DC X-CRM114-Status: GOOD ( 19.96 ) X-Spam-Score: -0.2 (/) X-Spam-Report: SpamAssassin version 3.4.2 on bombadil.infradead.org summary: Content analysis details: (-0.2 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at http://www.dnswl.org/, no trust [2a00:1450:4864:20:0:0:0:244 listed in] [list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain X-BeenThere: linux-mtd@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: Linux MTD discussion mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Linus Walleij , Liviu Dudau , Ryan Harkin Sender: "linux-mtd" Errors-To: linux-mtd-bounces+patch=linaro.org@lists.infradead.org Instead of reading out the AFS footers twice, create a separate function to just check if there is a footer or not. Rids a few local variables and prepare us to join the actual parser into one function. Cc: Ryan Harkin Cc: Liviu Dudau Signed-off-by: Linus Walleij --- drivers/mtd/parsers/afs.c | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) -- 2.20.1 ______________________________________________________ Linux MTD discussion mailing list http://lists.infradead.org/mailman/listinfo/linux-mtd/ diff --git a/drivers/mtd/parsers/afs.c b/drivers/mtd/parsers/afs.c index c489938cd665..ccc198818057 100644 --- a/drivers/mtd/parsers/afs.c +++ b/drivers/mtd/parsers/afs.c @@ -68,6 +68,26 @@ static u32 word_sum(void *words, int num) return sum; } +static bool afs_is_v1(struct mtd_info *mtd, u_int off) +{ + /* The magic is 12 bytes from the end of the erase block */ + u_int ptr = off + mtd->erasesize - 12; + u32 magic; + size_t sz; + int ret; + + ret = mtd_read(mtd, ptr, 4, &sz, (u_char *)&magic); + if (ret < 0) { + printk(KERN_ERR "AFS: mtd read failed at 0x%x: %d\n", + ptr, ret); + return false; + } + if (ret >= 0 && sz != 4) + return false; + + return (magic == AFSV1_FOOTER_MAGIC); +} + static int afs_read_footer_v1(struct mtd_info *mtd, u_int *img_start, u_int *iis_start, u_int off, u_int mask) @@ -176,18 +196,9 @@ static int parse_afs_partitions(struct mtd_info *mtd, */ mask = mtd->size - 1; - /* - * First, calculate the size of the array we need for the - * partition information. We include in this the size of - * the strings. - */ + /* Count the partitions by looping over all erase blocks */ for (i = off = sz = 0; off < mtd->size; off += mtd->erasesize) { - u_int iis_ptr, img_ptr; - - ret = afs_read_footer_v1(mtd, &img_ptr, &iis_ptr, off, mask); - if (ret < 0) - return ret; - if (ret) { + if (afs_is_v1(mtd, off)) { sz += sizeof(struct mtd_partition); i += 1; } From patchwork Mon Jan 28 13:54:46 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Walleij X-Patchwork-Id: 156742 Delivered-To: patch@linaro.org Received: by 2002:a02:48:0:0:0:0:0 with SMTP id 69csp3453891jaa; Mon, 28 Jan 2019 05:55:53 -0800 (PST) X-Google-Smtp-Source: ALg8bN6FAAuwY6zWsl9376wa+DXRdr6ATfihBJoLIbWhGAcz+IgYAYuJ76vkg7Mc+dJHOwMA3575 X-Received: by 2002:a62:1b83:: with SMTP id b125mr22805857pfb.42.1548683753698; Mon, 28 Jan 2019 05:55:53 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1548683753; cv=none; d=google.com; s=arc-20160816; b=tkk9LmpVwpNfQcVpfB4knBQJ5C8/jLK7l2RL5xIa9TzdYrfQxip0kgi3XiRCwVyKBx kzYUX77YC8sa1OHtEMvMfgA8BV/IS4prIoptXdex6FB1Qpk0HHegTTIq72opwELDtaoF ZDA53V+grVrG003rXqyVeZpzTkxWzQzTuP9K1dTDE5R/6uLOfRXhEfjFdBGPYJa9x15r nJY0BBAo9NkIHBPRMUFpUTF0xKtp214HC/M7xrqkd6ALM3XnYjHtSa8gZ3moyRzEBm6C V2O6kuZXXR6A9Q6j18TOP9TUda8BNRJ9OBUEHG8ZO/1/iYDv0S1aMGmh6ffzEHPtqXzz QnHg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:sender:content-transfer-encoding:cc:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:mime-version:references:in-reply-to:message-id:date :subject:to:from:dkim-signature:dkim-signature; bh=MKXedS5IYS1lONMMRTD+CVJWxMtCZHZzjMP4zuhr5gQ=; b=CJi7iLlQMvvszmnItZ/DYBBGQhPPgBlE2yiR7UaFg1izZzMo65g7N7lH0OryRjfHRs MiySrrE3+koiGOXp6J0q/HugqswYSi1YpLM1Q182YTf3z1eHz3mW3vzNVzgVxroJg14S 3PxB9ZNAw3XSRrmr4qiVCwb+SYFiIhBKMIjXlLzfLz2iKo1yPqcYTUGGzXvae5MvlsD4 1z8lJ0V8cjnZXi3iJ1JGZJlDZBDlbrwftCoLRf7WqhZYy3aJ88V2JC0Yd7G+QPcB+00X t77xVXkb8ofkAW29F66F2nHbTADgf5ncA2X6pClWeF2zAMph8NtS+hrAIGYCZ5RRJupD qHUA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@lists.infradead.org header.s=bombadil.20170209 header.b=ME2VQ8RS; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=jofZ87qy; spf=pass (google.com: best guess record for domain of linux-mtd-bounces+patch=linaro.org@lists.infradead.org designates 2607:7c80:54:e::133 as permitted sender) smtp.mailfrom="linux-mtd-bounces+patch=linaro.org@lists.infradead.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from bombadil.infradead.org (bombadil.infradead.org. [2607:7c80:54:e::133]) by mx.google.com with ESMTPS id bi2si32815979plb.200.2019.01.28.05.55.53 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 28 Jan 2019 05:55:53 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-mtd-bounces+patch=linaro.org@lists.infradead.org designates 2607:7c80:54:e::133 as permitted sender) client-ip=2607:7c80:54:e::133; Authentication-Results: mx.google.com; dkim=pass header.i=@lists.infradead.org header.s=bombadil.20170209 header.b=ME2VQ8RS; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=jofZ87qy; spf=pass (google.com: best guess record for domain of linux-mtd-bounces+patch=linaro.org@lists.infradead.org designates 2607:7c80:54:e::133 as permitted sender) smtp.mailfrom="linux-mtd-bounces+patch=linaro.org@lists.infradead.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=MKXedS5IYS1lONMMRTD+CVJWxMtCZHZzjMP4zuhr5gQ=; b=ME2VQ8RSM3pJIf 6SKG7mgxZOXZTv1IpUOSMyViKsZGVbRaQ079V7mGcQ+hfgZjp1IO4a+sey3bDEPLHL+ot6NTczDrH MBpOGsNESqgrMq5hD/6Gm9AhJETJ5KO+41e1GAcDkXq4v2jgvANTdGK+GAnyGiBhJO+PcJNkut5TK B4a6Pn2b6S6bIr646FgCvz6rWmhALuYMiwFcFOiJXjH90EpyHNBaz2izqZlksKV26IQbPBvsjhTth 3y4Jkrd09tVhiKrPVobNj92lFn0afxAdxTUpZhEokeISuV5irXmE6fJ3/LJjxuBmQb2dKxKeCnfoU 4kVSaSNVXacW0sFg5Hxg==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1go7O3-0001I3-Lf; Mon, 28 Jan 2019 13:55:43 +0000 Received: from mail-lj1-x241.google.com ([2a00:1450:4864:20::241]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1go7Na-0000TA-Dm for linux-mtd@lists.infradead.org; Mon, 28 Jan 2019 13:55:21 +0000 Received: by mail-lj1-x241.google.com with SMTP id n18-v6so14274056lji.7 for ; Mon, 28 Jan 2019 05:55:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=OWmZxsQVuMIf2AkP/1PkW+ARHBWjvKhuF5HT3GrzPJc=; b=jofZ87qy0FvOkvCkbIV9gynM1Px1dX3tehlorXzgYUwOsCBJ9waU3OKpZVAlNvIrj+ l9c0Hy9jbYNPbIH3vQ7UgaQZ3uy/HaQM7VRA4P/l+9bV8wG9OAsNy/WUJNWFdzdpy5/i cLJZSM2tR235N9vb+pzOhnEYzWLGNjnAOfSxw= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=OWmZxsQVuMIf2AkP/1PkW+ARHBWjvKhuF5HT3GrzPJc=; b=j/uHDYlgbTbhJbCDdaOyJp5jZhzxWhdaHVXkI2Al3JN6zykjccnjO2XVyPwecX08RK wralcoeeTO5nRZmvMoJWmcOZ1X0YXLIs7jd8HRekO3gBsbGtFWOMxgR1NDD4jGD98n9j 9TaKmqyTBuWNWX+171kdvqu2bzLwYyG7wmmgSuztvwbrmqLD3hSwD0iGxs3eHLut+zBW 7Od/ciJg4jWKA0QdVgn8tREt1S31iBSaomm4/XndXaEUQb4fex27NbOItEAxEt/NZbmQ GlaxaogxixQewfrMWoFCo9UuTB7SRAmVbW80riq+Gs8pL5v2VOcotjJ1PsNNBoSb1qW6 DsrA== X-Gm-Message-State: AHQUAubPI0rEwrrfaXqp749kGRDRTbcOqk4RCvD2ePnjj9d1AbxGVezp sHVe/S6q93cFZ48T83ByS58FyA== X-Received: by 2002:a2e:8786:: with SMTP id n6-v6mr6574348lji.100.1548683712405; Mon, 28 Jan 2019 05:55:12 -0800 (PST) Received: from genomnajs.ideon.se ([85.235.10.227]) by smtp.gmail.com with ESMTPSA id a2-v6sm3050503lji.13.2019.01.28.05.55.10 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 28 Jan 2019 05:55:11 -0800 (PST) From: Linus Walleij To: David Woodhouse , Brian Norris , Boris Brezillon , Marek Vasut , Richard Weinberger , Cyrille Pitchen , linux-mtd@lists.infradead.org Subject: [PATCH 3/6] mtd: factor out v1 partition parsing Date: Mon, 28 Jan 2019 14:54:46 +0100 Message-Id: <20190128135449.15555-4-linus.walleij@linaro.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190128135449.15555-1-linus.walleij@linaro.org> References: <20190128135449.15555-1-linus.walleij@linaro.org> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20190128_055515_006450_6D9B9DB5 X-CRM114-Status: GOOD ( 22.22 ) X-Spam-Score: -0.2 (/) X-Spam-Report: SpamAssassin version 3.4.2 on bombadil.infradead.org summary: Content analysis details: (-0.2 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at http://www.dnswl.org/, no trust [2a00:1450:4864:20:0:0:0:241 listed in] [list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain X-BeenThere: linux-mtd@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: Linux MTD discussion mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Linus Walleij , Liviu Dudau , Ryan Harkin Sender: "linux-mtd" Errors-To: linux-mtd-bounces+patch=linaro.org@lists.infradead.org This breaks out the parsing of v1 partitions so we can later add a v2 partition parser. Cc: Ryan Harkin Cc: Liviu Dudau Signed-off-by: Linus Walleij --- drivers/mtd/parsers/afs.c | 88 ++++++++++++++++++++++----------------- 1 file changed, 50 insertions(+), 38 deletions(-) -- 2.20.1 ______________________________________________________ Linux MTD discussion mailing list http://lists.infradead.org/mailman/listinfo/linux-mtd/ diff --git a/drivers/mtd/parsers/afs.c b/drivers/mtd/parsers/afs.c index ccc198818057..32ded91ae66c 100644 --- a/drivers/mtd/parsers/afs.c +++ b/drivers/mtd/parsers/afs.c @@ -181,14 +181,18 @@ afs_read_iis_v1(struct mtd_info *mtd, struct image_info_v1 *iis, u_int ptr) return ret; } -static int parse_afs_partitions(struct mtd_info *mtd, - const struct mtd_partition **pparts, - struct mtd_part_parser_data *data) +static int afs_parse_v1_partition(struct mtd_info *mtd, + u_int off, struct mtd_partition *part) { - struct mtd_partition *parts; - u_int mask, off, sz; - int ret = 0; - int i; + struct image_info_v1 iis; + u_int mask; + /* + * Static checks cannot see that we bail out if we have an error + * reading the footer. + */ + u_int uninitialized_var(iis_ptr); + u_int uninitialized_var(img_ptr); + int ret; /* * This is the address mask; we use this to mask off out of @@ -196,6 +200,39 @@ static int parse_afs_partitions(struct mtd_info *mtd, */ mask = mtd->size - 1; + ret = afs_read_footer_v1(mtd, &img_ptr, &iis_ptr, off, mask); + if (ret < 0) + return ret; + + /* Read the image info block */ + ret = afs_read_iis_v1(mtd, &iis, iis_ptr); + if (ret < 0) + return ret; + + part->name = kstrdup(iis.name, GFP_KERNEL); + if (!part->name) + return -ENOMEM; + + part->size = (iis.length + mtd->erasesize - 1) & ~(mtd->erasesize - 1); + part->offset = img_ptr; + part->mask_flags = 0; + + printk(" mtd: at 0x%08x, %5lluKiB, %8u, %s\n", + img_ptr, part->size / 1024, + iis.imageNumber, part->name); + + return 0; +} + +static int parse_afs_partitions(struct mtd_info *mtd, + const struct mtd_partition **pparts, + struct mtd_part_parser_data *data) +{ + struct mtd_partition *parts; + u_int off, sz; + int ret = 0; + int i; + /* Count the partitions by looping over all erase blocks */ for (i = off = sz = 0; off < mtd->size; off += mtd->erasesize) { if (afs_is_v1(mtd, off)) { @@ -215,38 +252,13 @@ static int parse_afs_partitions(struct mtd_info *mtd, * Identify the partitions */ for (i = off = 0; off < mtd->size; off += mtd->erasesize) { - struct image_info_v1 iis; - u_int iis_ptr, img_ptr; - - /* Read the footer. */ - ret = afs_read_footer_v1(mtd, &img_ptr, &iis_ptr, off, mask); - if (ret < 0) - goto out_free_parts; - if (ret == 0) - continue; - - /* Read the image info block */ - ret = afs_read_iis_v1(mtd, &iis, iis_ptr); - if (ret < 0) - goto out_free_parts; - if (ret == 0) - continue; - - parts[i].name = kstrdup(iis.name, GFP_KERNEL); - if (!parts[i].name) { - ret = -ENOMEM; - goto out_free_parts; - } - parts[i].size = (iis.length + mtd->erasesize - 1) & ~(mtd->erasesize - 1); - parts[i].offset = img_ptr; - parts[i].mask_flags = 0; - - printk(" mtd%d: at 0x%08x, %5lluKiB, %8u, %s\n", - i, img_ptr, parts[i].size / 1024, - iis.imageNumber, parts[i].name); - - i += 1; + if (afs_is_v1(mtd, off)) { + ret = afs_parse_v1_partition(mtd, off, &parts[i]); + if (ret) + goto out_free_parts; + i++; + } } *pparts = parts; From patchwork Mon Jan 28 13:54:47 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Walleij X-Patchwork-Id: 156743 Delivered-To: patch@linaro.org Received: by 2002:a02:48:0:0:0:0:0 with SMTP id 69csp3454094jaa; Mon, 28 Jan 2019 05:56:06 -0800 (PST) X-Google-Smtp-Source: ALg8bN6U50DdihM+SwClosmdWVLYfAaP+f7F6iE8pRUXmL+HNU+ET8OL5L4kw6Zpb9tB7518Y7ku X-Received: by 2002:a17:902:365:: with SMTP id 92mr20928796pld.327.1548683766932; Mon, 28 Jan 2019 05:56:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1548683766; cv=none; d=google.com; s=arc-20160816; b=IbM3kyiNLF1wDWCskeEWzNLRHvlB+2Ni2zi1FiAsFxSjeTMzeVpj2B6Zn7lNnazKPH NDEZnLdBmUYleiA5m0qQLI0abwSOL3NshsalGllZInZuJT404R8bW5DScRoS+1jiAmEt tXl4p1ocDOayOTRu+rs9oaN/SkgEw+NrcPF5H0tSI+aBm2c2xrxxQi9tTqDwNglOAPQc iloFnUm6Z7a2zZ5xpeTyX40UE2Y3pcd2IuL90HPGAFw3TryMvLKw8ehsakH32JzREz09 +3PvJRJjaVAjdbfy8a/Fy6rqsgQ3unSlApTg1Na4TJvPdUgkDP11EaIKSZE37E0rSpyR uy+Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:sender:content-transfer-encoding:cc:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:mime-version:references:in-reply-to:message-id:date :subject:to:from:dkim-signature:dkim-signature; bh=YzmfFk1bmlBiS6oYB0B9CrGOhkQIUgQ9tHT3+p3yfL8=; b=kzHQ/UFv1Iz1kZTgT6WYdbptT9EpJzwcOpywmVQ9kr/zyOqtHaAkfM7AVz94elTXhF tDuaU/v9lJQRB0ML3Qhta0W2Yq7X4mBQEYEva9BesUmE0ZPiRI/DQqfGzi6Cuw7IXL4T lSsinPzUh6g1KgVfGre1MQHBuLJgwRzXnMVGIobJqBBJcbRudTbn7J/nRiqRq9Ge9ZaW RszSLYI64+sHzuW6Y1z1f69I8thBuiqlce7b542+9IGLCcwo8B8nqJe59GbKLScUAAGc e9pPjaXdDyO2Sw+VIRJTBXYx2OtSDaZFXpR+HMZJur2052sHcfp0zpms70Ldw9mcZUI2 qttQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@lists.infradead.org header.s=bombadil.20170209 header.b=mJCsrMOo; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=gRDhXiqF; spf=pass (google.com: best guess record for domain of linux-mtd-bounces+patch=linaro.org@lists.infradead.org designates 2607:7c80:54:e::133 as permitted sender) smtp.mailfrom="linux-mtd-bounces+patch=linaro.org@lists.infradead.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from bombadil.infradead.org (bombadil.infradead.org. [2607:7c80:54:e::133]) by mx.google.com with ESMTPS id m1si3402222pgm.194.2019.01.28.05.56.06 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 28 Jan 2019 05:56:06 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-mtd-bounces+patch=linaro.org@lists.infradead.org designates 2607:7c80:54:e::133 as permitted sender) client-ip=2607:7c80:54:e::133; Authentication-Results: mx.google.com; dkim=pass header.i=@lists.infradead.org header.s=bombadil.20170209 header.b=mJCsrMOo; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=gRDhXiqF; spf=pass (google.com: best guess record for domain of linux-mtd-bounces+patch=linaro.org@lists.infradead.org designates 2607:7c80:54:e::133 as permitted sender) smtp.mailfrom="linux-mtd-bounces+patch=linaro.org@lists.infradead.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=YzmfFk1bmlBiS6oYB0B9CrGOhkQIUgQ9tHT3+p3yfL8=; b=mJCsrMOoyeyOl4 4qZzrqq9qQNHleEJ9elI/95gFc7EaAKCYctHOduUTowYGl/+j0WDkICx8nUm56X7MJIRAxly5p95v sJ7ta33TKvLLaI9lyClqfF3r3sn0AhqlsyF6ZVd1E9z/DXBVPlt3iseCraNyItC8CQ/viqzFa77bo DyKZLgQ1DE5h8DiGivG8HNU+yg70alP0TzbKtofiIKqiZ6tKuRKcWDj1HFLoqp0bQsUPlBXnTpsEq wO9sAQDX36qfDspWJZNANPG0Fw9fwf/ngYHM20jCyXuUzRLqp1DP0/WJVMW1zVcAWDGLQqVyvWHRS nFnahYRr1nknQhiM7CTQ==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1go7OH-0001VT-En; Mon, 28 Jan 2019 13:55:57 +0000 Received: from mail-lj1-x243.google.com ([2a00:1450:4864:20::243]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1go7Nd-0000ju-OW for linux-mtd@lists.infradead.org; Mon, 28 Jan 2019 13:55:24 +0000 Received: by mail-lj1-x243.google.com with SMTP id k15-v6so14264902ljc.8 for ; Mon, 28 Jan 2019 05:55:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=YQcDIXczqauucFef6KTlzM2bz+VwyoVSWjrYrYT/y2E=; b=gRDhXiqFt/xw3LXKZ9NKnOagKdxiq/rAHfsqRqqwzGaczdYq3u/vWcdpq45aR1a0fv ixY8D1u6bTR33IWT2lSXClOLQyPVmd7H8W7h02hLUSLsCMXmvCNbudVy4MwO94l64Rkm cFjgtLaOPML7kUNwCz4ppnFaHHkougpDPoluc= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=YQcDIXczqauucFef6KTlzM2bz+VwyoVSWjrYrYT/y2E=; b=sY/H/ux8f1KDMCb4WQ2cuoqBGwt3jSm7g90GtyAswHD2l7375YSrvOEU0PB2GlOARH iVuk6c1F7FLhcnEI+7I6cnS5kxXeMKRkoSu7knwLVzq8Uv24syF2C6gPSU5ZEUOXA3Fj YGgT7LFQdBm8wOU3N5v9g0BOoSjTOTT6pDgNAlQHBTe7oUW8liPxvrPX6UOtCEVhnslt pGeHdlmP20nvu67CrKCL3uwIlawVqDHp5Wyu6iCdMDhFYy8uXN73bHMoqb+c5P5mx7ph FDKW+D9TJo+aDiM1TT91gp6JwF+LTztggD8H8QUjWfz1YoOVGdqs74lS+NNuobiMWsV8 gwSQ== X-Gm-Message-State: AHQUAubSCnLecog8zx4icoCz0VrNnhQTxffAiKrdTs2o4EeXMdqs0qzQ HGGYmK3x+r5ULwjiR9v0LhldxA== X-Received: by 2002:a2e:6a13:: with SMTP id f19-v6mr11226359ljc.41.1548683715843; Mon, 28 Jan 2019 05:55:15 -0800 (PST) Received: from genomnajs.ideon.se ([85.235.10.227]) by smtp.gmail.com with ESMTPSA id a2-v6sm3050503lji.13.2019.01.28.05.55.14 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 28 Jan 2019 05:55:14 -0800 (PST) From: Linus Walleij To: David Woodhouse , Brian Norris , Boris Brezillon , Marek Vasut , Richard Weinberger , Cyrille Pitchen , linux-mtd@lists.infradead.org Subject: [PATCH 4/6] mtd: afs: factor footer parsing into the v1 part parsing Date: Mon, 28 Jan 2019 14:54:47 +0100 Message-Id: <20190128135449.15555-5-linus.walleij@linaro.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190128135449.15555-1-linus.walleij@linaro.org> References: <20190128135449.15555-1-linus.walleij@linaro.org> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20190128_055518_177078_BFBB1FE4 X-CRM114-Status: GOOD ( 21.84 ) X-Spam-Score: -0.2 (/) X-Spam-Report: SpamAssassin version 3.4.2 on bombadil.infradead.org summary: Content analysis details: (-0.2 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at http://www.dnswl.org/, no trust [2a00:1450:4864:20:0:0:0:243 listed in] [list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain X-BeenThere: linux-mtd@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: Linux MTD discussion mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Linus Walleij , Liviu Dudau , Ryan Harkin Sender: "linux-mtd" Errors-To: linux-mtd-bounces+patch=linaro.org@lists.infradead.org This simplifies the code by factoring in the image footer parsing into the single function parsing the AFSv1 partitions. Cc: Ryan Harkin Cc: Liviu Dudau Signed-off-by: Linus Walleij --- drivers/mtd/parsers/afs.c | 98 ++++++++++++++++----------------------- 1 file changed, 39 insertions(+), 59 deletions(-) -- 2.20.1 ______________________________________________________ Linux MTD discussion mailing list http://lists.infradead.org/mailman/listinfo/linux-mtd/ diff --git a/drivers/mtd/parsers/afs.c b/drivers/mtd/parsers/afs.c index 32ded91ae66c..8ff82a548252 100644 --- a/drivers/mtd/parsers/afs.c +++ b/drivers/mtd/parsers/afs.c @@ -88,63 +88,6 @@ static bool afs_is_v1(struct mtd_info *mtd, u_int off) return (magic == AFSV1_FOOTER_MAGIC); } -static int -afs_read_footer_v1(struct mtd_info *mtd, u_int *img_start, u_int *iis_start, - u_int off, u_int mask) -{ - struct footer_v1 fs; - u_int ptr = off + mtd->erasesize - sizeof(fs); - size_t sz; - int ret; - - ret = mtd_read(mtd, ptr, sizeof(fs), &sz, (u_char *)&fs); - if (ret >= 0 && sz != sizeof(fs)) - ret = -EINVAL; - - if (ret < 0) { - printk(KERN_ERR "AFS: mtd read failed at 0x%x: %d\n", - ptr, ret); - return ret; - } - - /* - * Does it contain the magic number? - */ - if (fs.signature != AFSV1_FOOTER_MAGIC) - return 0; - - /* - * Check the checksum. - */ - if (word_sum(&fs, sizeof(fs) / sizeof(u32)) != 0xffffffff) - return 0; - - /* - * Don't touch the SIB. - */ - if (fs.type == 2) - return 0; - - *iis_start = fs.image_info_base & mask; - *img_start = fs.image_start & mask; - - /* - * Check the image info base. This can not - * be located after the footer structure. - */ - if (*iis_start >= ptr) - return 0; - - /* - * Check the start of this image. The image - * data can not be located after this block. - */ - if (*img_start > off) - return 0; - - return 1; -} - static int afs_read_iis_v1(struct mtd_info *mtd, struct image_info_v1 *iis, u_int ptr) { @@ -184,6 +127,7 @@ afs_read_iis_v1(struct mtd_info *mtd, struct image_info_v1 *iis, u_int ptr) static int afs_parse_v1_partition(struct mtd_info *mtd, u_int off, struct mtd_partition *part) { + struct footer_v1 fs; struct image_info_v1 iis; u_int mask; /* @@ -192,6 +136,8 @@ static int afs_parse_v1_partition(struct mtd_info *mtd, */ u_int uninitialized_var(iis_ptr); u_int uninitialized_var(img_ptr); + u_int ptr; + size_t sz; int ret; /* @@ -200,9 +146,43 @@ static int afs_parse_v1_partition(struct mtd_info *mtd, */ mask = mtd->size - 1; - ret = afs_read_footer_v1(mtd, &img_ptr, &iis_ptr, off, mask); - if (ret < 0) + ptr = off + mtd->erasesize - sizeof(fs); + ret = mtd_read(mtd, ptr, sizeof(fs), &sz, (u_char *)&fs); + if (ret >= 0 && sz != sizeof(fs)) + ret = -EINVAL; + if (ret < 0) { + printk(KERN_ERR "AFS: mtd read failed at 0x%x: %d\n", + ptr, ret); return ret; + } + /* + * Check the checksum. + */ + if (word_sum(&fs, sizeof(fs) / sizeof(u32)) != 0xffffffff) + return -EINVAL; + + /* + * Hide the SIB (System Information Block) + */ + if (fs.type == 2) + return 0; + + iis_ptr = fs.image_info_base & mask; + img_ptr = fs.image_start & mask; + + /* + * Check the image info base. This can not + * be located after the footer structure. + */ + if (iis_ptr >= ptr) + return 0; + + /* + * Check the start of this image. The image + * data can not be located after this block. + */ + if (img_ptr > off) + return 0; /* Read the image info block */ ret = afs_read_iis_v1(mtd, &iis, iis_ptr); From patchwork Mon Jan 28 13:54:48 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Walleij X-Patchwork-Id: 156744 Delivered-To: patch@linaro.org Received: by 2002:a02:48:0:0:0:0:0 with SMTP id 69csp3454284jaa; Mon, 28 Jan 2019 05:56:19 -0800 (PST) X-Google-Smtp-Source: ALg8bN52pW65qMCWi2nadkXjF3IH0xJkvlr8Vbcmk3IGsHuz2atT5gg9PJlOQKB1+eLK63UpT3+X X-Received: by 2002:a62:6f88:: with SMTP id k130mr21880680pfc.234.1548683779812; Mon, 28 Jan 2019 05:56:19 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1548683779; cv=none; d=google.com; s=arc-20160816; b=KXubC1a42NQJ83336WUi2Db/Ur+PFn0Y52WB+DXdiAhQkDbSnoM7sNGshNXP0FjaH1 LGPtN9DutxCz+/VR9+V6XG8WW1FZSbVHjbJj5CgOPNFcDVGUbjSYYEcfqBvFRWmUih7+ SZ4pHoxOZKQuNbRaoK/PZ+4tR9og+nJ8psel4TDyA9tJ4lSm6GS+cl2+Wc+nO3UNKo6F LCIfK/Dg4WlsEipq/hrNvoQuHr+A7EQoTiSqNf+zXLEgGuekcetwb6emE2TVzvY9/Fw2 0g3WhWG22RAzfvlCTlnTwA04c9KNqHZy+4yvS0JFlSgWii07LuC9An5nFObzKMAMtpR3 sCgA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:sender:content-transfer-encoding:cc:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:mime-version:references:in-reply-to:message-id:date :subject:to:from:dkim-signature:dkim-signature; bh=93fWBYsnQGgybrfRnUMaHX7XQkIq7KE1E3vcEzzSrd0=; b=P1Rumv7E1cGwgaCaBAloUo9DKmj7mlS/P8kWthFTfgvad4mFczWtpnLJBe6D8hWwuv JJd5Ga4zFrV4xvW09Iiebh1rbxAv3uF+QwE+DSPjOaPjXte8/LWb326cEA8geLYzo4p2 yJtNFw/DHUdPuqDT1e0++H6K9lllYDCms2H/L36iL81+lJjJB6xmSjhSSWK3i9zbDmXT +9+y6WSvSq3gMRqaHveMGyY6WvoTXeADCF5b8Td1LAeBoNlbz76FrDogS9mY5lGD0RdJ NHxp2+TGxI5mFNnQd3Wpp7IE0okiS3bHOjRgZwhl2XaGEe1Pvay0JDGc7d8TbsT/wGri Lu4g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@lists.infradead.org header.s=bombadil.20170209 header.b=mDqDX3Md; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=NjnkcihM; spf=pass (google.com: best guess record for domain of linux-mtd-bounces+patch=linaro.org@lists.infradead.org designates 2607:7c80:54:e::133 as permitted sender) smtp.mailfrom="linux-mtd-bounces+patch=linaro.org@lists.infradead.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from bombadil.infradead.org (bombadil.infradead.org. [2607:7c80:54:e::133]) by mx.google.com with ESMTPS id g26si31955841pfe.127.2019.01.28.05.56.19 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 28 Jan 2019 05:56:19 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-mtd-bounces+patch=linaro.org@lists.infradead.org designates 2607:7c80:54:e::133 as permitted sender) client-ip=2607:7c80:54:e::133; Authentication-Results: mx.google.com; dkim=pass header.i=@lists.infradead.org header.s=bombadil.20170209 header.b=mDqDX3Md; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=NjnkcihM; spf=pass (google.com: best guess record for domain of linux-mtd-bounces+patch=linaro.org@lists.infradead.org designates 2607:7c80:54:e::133 as permitted sender) smtp.mailfrom="linux-mtd-bounces+patch=linaro.org@lists.infradead.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=93fWBYsnQGgybrfRnUMaHX7XQkIq7KE1E3vcEzzSrd0=; b=mDqDX3MdFHmi0t qsybhq41M0OBVcxWoWWtpdOPKY/tXCJkMDxYBh1jXZA7fXCzXsWv1CoxTBD84t+MAhzuRywoFf9fI gQXYaGHzNpXPVlrUtloCUnYYeIDO5AGN9qdEJFE3vq0G46t5F4RL7k347Ot1SynQXw5kcNnMG6xgP k5Jjo7/rd1PADGHPTqcIQOh7isbPcC3TdFgCfknsvqU/dLB4SHU5gim0btPNO0BiVoW4OZ1EJG4aa umJlSgp5p2qpfxht0z2Ao0cGf5rt4aT585/lDdGfblkCE9lGSR5JollD/HQZEDpC8qA2E4ijADoUf V0ElLk7qAfgPT6LBcnZw==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1go7OW-0001lN-Ex; Mon, 28 Jan 2019 13:56:12 +0000 Received: from mail-lj1-x244.google.com ([2a00:1450:4864:20::244]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1go7Nh-0000sK-AW for linux-mtd@lists.infradead.org; Mon, 28 Jan 2019 13:55:28 +0000 Received: by mail-lj1-x244.google.com with SMTP id s5-v6so14245666ljd.12 for ; Mon, 28 Jan 2019 05:55:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=m9CK2FIaqX5ahLo+vJZ7bZ0fFWNyscRpfdYobu3qH8I=; b=NjnkcihM6WybI/+gOLacqjiy7inKNIfsfldgKk4+pRQVloWJXFVm3eMuS4Vx611Nfp hHVfJSwR3u6d+PQonWLaTo9rW4a5obBL/6coOHz88+xqWIcGSCT7ev4w6eR54tSMfHRi Rl1ZcI7sfWAuR8/q9VhUjmSZBa1aQOECtnl1M= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=m9CK2FIaqX5ahLo+vJZ7bZ0fFWNyscRpfdYobu3qH8I=; b=Ed3fxsk8BPzronzEI7+qO+Xzsv/T6UnCiPHiQhsditvc0cVN2QEq0mycAyjoUMEx5g g3p54VeqYM0pY2JJ+Xq/enrPUHJZQGK3vaDpPl43v6cSGyoOgNqM4jI8EG7Ptz/W6vst rFg4B85TfdSKNgL9UmSKBIYXGN2fTXBhEcfE3F75yZlyHZPG+sVvziMIAYwFojS8IbDw 9p53CyQGAtoE2xZgHA+vafjdFHU3jlGxYmmKBs+2jSRgP2xqztOdmWsRfriH2wTFYlwz 1a9q7mWnxrorgNRY2vGIiqk7K+5OQ9ceLNwebYWdWtXfFlI6meZIcNCLr7ZNw8JaSt4A XUHg== X-Gm-Message-State: AJcUukdI4JsPYYgojSSOM6VndiGHcnYvGEWR1CXPAg6YpJFpChQ57BQd zYPyuUT0KD9gsqXyztLQs942vg== X-Received: by 2002:a2e:91d1:: with SMTP id u17-v6mr17513569ljg.160.1548683719439; Mon, 28 Jan 2019 05:55:19 -0800 (PST) Received: from genomnajs.ideon.se ([85.235.10.227]) by smtp.gmail.com with ESMTPSA id a2-v6sm3050503lji.13.2019.01.28.05.55.18 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 28 Jan 2019 05:55:18 -0800 (PST) From: Linus Walleij To: David Woodhouse , Brian Norris , Boris Brezillon , Marek Vasut , Richard Weinberger , Cyrille Pitchen , linux-mtd@lists.infradead.org Subject: [PATCH 5/6] mtd: afs: factor the IIS read into partition parser Date: Mon, 28 Jan 2019 14:54:48 +0100 Message-Id: <20190128135449.15555-6-linus.walleij@linaro.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190128135449.15555-1-linus.walleij@linaro.org> References: <20190128135449.15555-1-linus.walleij@linaro.org> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20190128_055522_299287_98FE6154 X-CRM114-Status: GOOD ( 20.08 ) X-Spam-Score: -0.2 (/) X-Spam-Report: SpamAssassin version 3.4.2 on bombadil.infradead.org summary: Content analysis details: (-0.2 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at http://www.dnswl.org/, no trust [2a00:1450:4864:20:0:0:0:244 listed in] [list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain X-BeenThere: linux-mtd@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: Linux MTD discussion mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Linus Walleij , Liviu Dudau , Ryan Harkin Sender: "linux-mtd" Errors-To: linux-mtd-bounces+patch=linaro.org@lists.infradead.org Factor the IIS (Image Information Structure) reading into the partition parser, giving us a single, clean partition parser function. Cc: Ryan Harkin Cc: Liviu Dudau Signed-off-by: Linus Walleij --- drivers/mtd/parsers/afs.c | 59 +++++++++++++-------------------------- 1 file changed, 20 insertions(+), 39 deletions(-) -- 2.20.1 ______________________________________________________ Linux MTD discussion mailing list http://lists.infradead.org/mailman/listinfo/linux-mtd/ diff --git a/drivers/mtd/parsers/afs.c b/drivers/mtd/parsers/afs.c index 8ff82a548252..72c688b8a383 100644 --- a/drivers/mtd/parsers/afs.c +++ b/drivers/mtd/parsers/afs.c @@ -88,42 +88,6 @@ static bool afs_is_v1(struct mtd_info *mtd, u_int off) return (magic == AFSV1_FOOTER_MAGIC); } -static int -afs_read_iis_v1(struct mtd_info *mtd, struct image_info_v1 *iis, u_int ptr) -{ - size_t sz; - int ret, i; - - memset(iis, 0, sizeof(*iis)); - ret = mtd_read(mtd, ptr, sizeof(*iis), &sz, (u_char *)iis); - if (ret < 0) - goto failed; - - if (sz != sizeof(*iis)) { - ret = -EINVAL; - goto failed; - } - - ret = 0; - - /* - * Validate the name - it must be NUL terminated. - */ - for (i = 0; i < sizeof(iis->name); i++) - if (iis->name[i] == '\0') - break; - - if (i < sizeof(iis->name)) - ret = 1; - - return ret; - - failed: - printk(KERN_ERR "AFS: mtd read failed at 0x%x: %d\n", - ptr, ret); - return ret; -} - static int afs_parse_v1_partition(struct mtd_info *mtd, u_int off, struct mtd_partition *part) { @@ -139,6 +103,7 @@ static int afs_parse_v1_partition(struct mtd_info *mtd, u_int ptr; size_t sz; int ret; + int i; /* * This is the address mask; we use this to mask off out of @@ -185,9 +150,25 @@ static int afs_parse_v1_partition(struct mtd_info *mtd, return 0; /* Read the image info block */ - ret = afs_read_iis_v1(mtd, &iis, iis_ptr); - if (ret < 0) - return ret; + memset(&iis, 0, sizeof(iis)); + ret = mtd_read(mtd, iis_ptr, sizeof(iis), &sz, (u_char *)&iis); + if (ret < 0) { + printk(KERN_ERR "AFS: mtd read failed at 0x%x: %d\n", + iis_ptr, ret); + return -EINVAL; + } + + if (sz != sizeof(iis)) + return -EINVAL; + + /* + * Validate the name - it must be NUL terminated. + */ + for (i = 0; i < sizeof(iis.name); i++) + if (iis.name[i] == '\0') + break; + if (i > sizeof(iis.name)) + return -EINVAL; part->name = kstrdup(iis.name, GFP_KERNEL); if (!part->name) From patchwork Mon Jan 28 13:54:49 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Linus Walleij X-Patchwork-Id: 156745 Delivered-To: patch@linaro.org Received: by 2002:a02:48:0:0:0:0:0 with SMTP id 69csp3454528jaa; Mon, 28 Jan 2019 05:56:36 -0800 (PST) X-Google-Smtp-Source: ALg8bN5TdvVsKkAhCayxcJ0OmQXHZklIG0vPIokIBUUgX8ZuzEWQFep/ACCBdHPwZ6FcSuo9oHmr X-Received: by 2002:a63:2b82:: with SMTP id r124mr19417683pgr.300.1548683796071; Mon, 28 Jan 2019 05:56:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1548683796; cv=none; d=google.com; s=arc-20160816; b=bcKpzKkkP3zyq80x0LJH4wNAz1MP6+cqBG5qVtcTq1GMC4bOnMEoptlAdUVuUBkuWZ xc46odHzLLAdnfPXDlu0JJF7/0l3GHknPps/Ehzk65j6I/a/s/AZUefJFjj9xOvLgQO7 GNQ2/11jnS2SflwhxO+eQPLwO+DpVpv2d3UL2UkbP+tWp7wyb7f1VTQ61PSuxAzwnT+k uPT80GlArdMFMALO9rHLm2DxpNZm7s54mTfSy2oSoCcJWLK0algUWwlkZLSs30d2zMvV CXtVeTW0qryfH/r2y3dFG4v81rGBMXAo4fGRlRsjJC0hkOZFxoyFIWIvemagYyG2hYV7 mp1w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:sender:content-transfer-encoding:cc:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:mime-version:references:in-reply-to:message-id:date :subject:to:from:dkim-signature:dkim-signature; bh=8o/lMkse9chJ66KmRIsYjVYt+cE6iHQOccqyk8Na9kM=; b=uAQg63y+ofEpHCNfB4z+bjuUHK3HKvZ24nvwrsR7kQtJgCSzBKgsf60HIo8eZ5FgBN 3Cq6y97/t2LTn7YLaCid0raRwCcFk/CLQxGsdsCudOj9QmNGxXStyWFe1bVOUiIGSOmU oUHmMvPd5RtXlzra7cFhQy7+G+2kVoypsosLI3Kk7MT4iuWQ64capdfUuSpGHx3oOgHj jO0v3RM8hVqSMjVOz9GYIsglq9CkvnNrlIJZeJpRPEDNXEKvvtaHrgraM95HGW8XMENX bbRh1ddLtcsip1x/jz5IKE47+TeX0+qrD1KATRy+Rn3NkK6N9hZv5kvKszi42VBvzZsl 5QLA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@lists.infradead.org header.s=bombadil.20170209 header.b=QMOimYSj; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=XcY7gFth; spf=pass (google.com: best guess record for domain of linux-mtd-bounces+patch=linaro.org@lists.infradead.org designates 2607:7c80:54:e::133 as permitted sender) smtp.mailfrom="linux-mtd-bounces+patch=linaro.org@lists.infradead.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from bombadil.infradead.org (bombadil.infradead.org. [2607:7c80:54:e::133]) by mx.google.com with ESMTPS id 44si21971248plb.57.2019.01.28.05.56.35 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 28 Jan 2019 05:56:36 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-mtd-bounces+patch=linaro.org@lists.infradead.org designates 2607:7c80:54:e::133 as permitted sender) client-ip=2607:7c80:54:e::133; Authentication-Results: mx.google.com; dkim=pass header.i=@lists.infradead.org header.s=bombadil.20170209 header.b=QMOimYSj; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=XcY7gFth; spf=pass (google.com: best guess record for domain of linux-mtd-bounces+patch=linaro.org@lists.infradead.org designates 2607:7c80:54:e::133 as permitted sender) smtp.mailfrom="linux-mtd-bounces+patch=linaro.org@lists.infradead.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=8o/lMkse9chJ66KmRIsYjVYt+cE6iHQOccqyk8Na9kM=; b=QMOimYSjrdFDPL pwSO15NjvQ/4cmtUt+w1TPUynEIrYKV+p9qaS59WXPyZuH0eA7rxbzYBqp9CQmmoEwI+ugcrAxXr8 oTU13w2HbFUL7eQuTBwNd3Zk+O7xvbl+oHRni3QKlcXc9I1OUELheHN0xiBk4sJ5yeAnvg0wuXskk k0bez5uePILI+UIVuTEfn21iINaCgImbIkM9+9hCjbE6XKaJfDuCHtPijWJlHwjbaGCX/tKEbU+0X Q1A85OOietK6y9cmsfBs0ZHIUnA9KZyrC8z8an5H6pFL8aqTQa3H58zzjzxzvJjH7H+XvfKnvbB1f BA93ikGbKZMDRww8+oQA==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1go7Ok-0001yt-CV; Mon, 28 Jan 2019 13:56:26 +0000 Received: from mail-lj1-x241.google.com ([2a00:1450:4864:20::241]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1go7Nj-0000va-Q3 for linux-mtd@lists.infradead.org; Mon, 28 Jan 2019 13:55:38 +0000 Received: by mail-lj1-x241.google.com with SMTP id x85-v6so14317286ljb.2 for ; Mon, 28 Jan 2019 05:55:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=KE63/MbKtvFo8wQD1gE+MH3I+mv+c13P+wzjXzf4p/w=; b=XcY7gFth5pPONdU+25ULef2+IWY9okodoJS22ATKtnGrxVKV05Xdw20FewDzks5Nqw b5I4iZT8VmhITMdJ8CSH5UJN67S3I731asaG/GkBlI68JxyaYBobwcgVu9MuV2TZtUDW utp781IeD+j356ppAw5083o3vmTsC+4xxxHp4= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=KE63/MbKtvFo8wQD1gE+MH3I+mv+c13P+wzjXzf4p/w=; b=nbveKYl7oDU36urxaXw6yNwLT1efZUTtK7bBGwkjpYTKlr/Vb5pKW1AHhdP1WRMZMI mgAY/aplLKyFjt/qEnBoar01tAMEET4cmW/1oP6mJRyXj1+vE9lBt4ogs3AqQbuo8JjX D+pVJ3y/2PbKLZotwDArPGsb0kAykWFXd/qKSzrneDPePyx3fwKR3VOVV+xHT6wEMZVc TBtgeMRCpVXmi9SJ0vN8Eq+jKsDVxVRyaScqOcjW1tBeAMBA/KB+Z2xEvJz8RriPVJc+ 0wHFOnbig/5GqlA6bNBJU8k8nn8+DLbqPqF3HCRTqjvYVJTflohwOJkvmDkg/Bv0fgve P8uw== X-Gm-Message-State: AJcUukf1TCPP7qr5Rt+SrOyU7jfiLt+SxVrUFBsUdWp7qOwwZFJa9lgg 49QBxyKZR3WTof+Gw0xRuMW6rw== X-Received: by 2002:a2e:9b52:: with SMTP id o18-v6mr16924195ljj.108.1548683721891; Mon, 28 Jan 2019 05:55:21 -0800 (PST) Received: from genomnajs.ideon.se ([85.235.10.227]) by smtp.gmail.com with ESMTPSA id a2-v6sm3050503lji.13.2019.01.28.05.55.20 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 28 Jan 2019 05:55:20 -0800 (PST) From: Linus Walleij To: David Woodhouse , Brian Norris , Boris Brezillon , Marek Vasut , Richard Weinberger , Cyrille Pitchen , linux-mtd@lists.infradead.org Subject: [PATCH 6/6] mtd: afs: add v2 partition parsing Date: Mon, 28 Jan 2019 14:54:49 +0100 Message-Id: <20190128135449.15555-7-linus.walleij@linaro.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190128135449.15555-1-linus.walleij@linaro.org> References: <20190128135449.15555-1-linus.walleij@linaro.org> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20190128_055524_430011_2A84A52C X-CRM114-Status: GOOD ( 24.95 ) X-Spam-Score: -0.2 (/) X-Spam-Report: SpamAssassin version 3.4.2 on bombadil.infradead.org summary: Content analysis details: (-0.2 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at http://www.dnswl.org/, no trust [2a00:1450:4864:20:0:0:0:241 listed in] [list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain X-BeenThere: linux-mtd@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: Linux MTD discussion mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Linus Walleij , Liviu Dudau , Ryan Harkin Sender: "linux-mtd" Errors-To: linux-mtd-bounces+patch=linaro.org@lists.infradead.org The AFS v2 partition type appear in later ARM reference designs such as RealView, Versatile Express and the 64bit Juno Development Platform. The image informations is padded with a 32bit word (4 bytes) on the 32bit platforms and a 64bit word (8 bytes) on the 64bit platforms. The boot monitor source code gives at hand that this is because the first entry in the struct mapped over the image information is a "next" pointer for a linked list, filled in by firmware after reading in the info block, and always zero in the flash. We adjust padding by checking what padding gives the right checksum. This was tested on: - Integrator/AP (v1 partitions) - RealView PB11MPCore (v2 32bit partitions) - Juno Development System (v2 64bit partitions) All systems display the images in flash very nicely as separate partitions, e.g on Juno: 4 afs partitions found on MTD device 8000000.flash Creating 4 MTD partitions on "8000000.flash": 0x000000040000-0x0000000c0000 : "fip" 0x000000ec0000-0x0000018c0000 : "Image" 0x000000f00000-0x000000f40000 : "juno" 0x000003ec0000-0x000003f00000 : "bl1" Cc: Ryan Harkin Cc: Liviu Dudau Signed-off-by: Linus Walleij --- drivers/mtd/parsers/afs.c | 158 +++++++++++++++++++++++++++++++++++++- 1 file changed, 157 insertions(+), 1 deletion(-) diff --git a/drivers/mtd/parsers/afs.c b/drivers/mtd/parsers/afs.c index 72c688b8a383..0c730024f806 100644 --- a/drivers/mtd/parsers/afs.c +++ b/drivers/mtd/parsers/afs.c @@ -3,6 +3,7 @@ drivers/mtd/afs.c: ARM Flash Layout/Partitioning Copyright © 2000 ARM Limited + Copyright (C) 2019 Linus Walleij This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -35,6 +36,8 @@ #include #define AFSV1_FOOTER_MAGIC 0xA0FFFF9F +#define AFSV2_FOOTER_MAGIC1 0x464C5348 /* "FLSH" */ +#define AFSV2_FOOTER_MAGIC2 0x464F4F54 /* "FOOT" */ struct footer_v1 { u32 image_info_base; /* Address of first word of ImageFooter */ @@ -68,6 +71,22 @@ static u32 word_sum(void *words, int num) return sum; } +static u32 word_sum_v2(u32 *p, u32 num) +{ + u32 sum = 0; + int i; + + for (i = 0; i < num; i++) { + u32 val; + + val = p[i]; + if (val > ~sum) + sum++; + sum += val; + } + return ~sum; +} + static bool afs_is_v1(struct mtd_info *mtd, u_int off) { /* The magic is 12 bytes from the end of the erase block */ @@ -88,6 +107,27 @@ static bool afs_is_v1(struct mtd_info *mtd, u_int off) return (magic == AFSV1_FOOTER_MAGIC); } +static bool afs_is_v2(struct mtd_info *mtd, u_int off) +{ + /* The magic is the 8 last bytes of the erase block */ + u_int ptr = off + mtd->erasesize - 8; + u32 foot[2]; + size_t sz; + int ret; + + ret = mtd_read(mtd, ptr, 8, &sz, (u_char *)foot); + if (ret < 0) { + printk(KERN_ERR "AFS: mtd read failed at 0x%x: %d\n", + ptr, ret); + return false; + } + if (ret >= 0 && sz != 8) + return false; + + return (foot[0] == AFSV2_FOOTER_MAGIC1 && + foot[1] == AFSV2_FOOTER_MAGIC2); +} + static int afs_parse_v1_partition(struct mtd_info *mtd, u_int off, struct mtd_partition *part) { @@ -185,6 +225,113 @@ static int afs_parse_v1_partition(struct mtd_info *mtd, return 0; } +static int afs_parse_v2_partition(struct mtd_info *mtd, + u_int off, struct mtd_partition *part) +{ + u_int ptr; + u32 footer[12]; + u32 imginfo[36]; + char *name; + u32 version; + u32 entrypoint; + u32 attributes; + u32 region_count; + u32 block_start; + u32 block_end; + u32 crc; + size_t sz; + int ret; + int i; + int pad = 0; + + pr_debug("Parsing v2 partition @%08x-%08x\n", + off, off + mtd->erasesize); + + /* First read the footer */ + ptr = off + mtd->erasesize - sizeof(footer); + ret = mtd_read(mtd, ptr, sizeof(footer), &sz, (u_char *)footer); + if ((ret < 0) || (ret >= 0 && sz != sizeof(footer))) { + pr_err("AFS: mtd read failed at 0x%x: %d\n", + ptr, ret); + return -EIO; + } + name = (char *) &footer[0]; + version = footer[9]; + ptr = off + mtd->erasesize - sizeof(footer) - footer[8]; + + pr_debug("found image \"%s\", version %08x, info @%08x\n", + name, version, ptr); + + /* Then read the image information */ + ret = mtd_read(mtd, ptr, sizeof(imginfo), &sz, (u_char *)imginfo); + if ((ret < 0) || (ret >= 0 && sz != sizeof(imginfo))) { + pr_err("AFS: mtd read failed at 0x%x: %d\n", + ptr, ret); + return -EIO; + } + + /* 32bit platforms have 4 bytes padding */ + crc = word_sum_v2(&imginfo[1], 34); + if (!crc) { + pr_debug("Padding 1 word (4 bytes)\n"); + pad = 1; + } else { + /* 64bit platforms have 8 bytes padding */ + crc = word_sum_v2(&imginfo[2], 34); + if (!crc) { + pr_debug("Padding 2 words (8 bytes)\n"); + pad = 2; + } + } + if (crc) { + pr_err("AFS: bad checksum on v2 image info: %08x\n", crc); + return -EINVAL; + } + entrypoint = imginfo[pad]; + attributes = imginfo[pad+1]; + region_count = imginfo[pad+2]; + block_start = imginfo[20]; + block_end = imginfo[21]; + + pr_debug("image entry=%08x, attr=%08x, regions=%08x, " + "bs=%08x, be=%08x\n", + entrypoint, attributes, region_count, + block_start, block_end); + + for (i = 0; i < region_count; i++) { + u32 region_load_addr = imginfo[pad + 3 + i*4]; + u32 region_size = imginfo[pad + 4 + i*4]; + u32 region_offset = imginfo[pad + 5 + i*4]; + u32 region_start; + u32 region_end; + + pr_debug(" region %d: address: %08x, size: %08x, " + "offset: %08x\n", + i, + region_load_addr, + region_size, + region_offset); + + region_start = off + region_offset; + region_end = region_start + region_size; + /* Align partition to end of erase block */ + region_end += (mtd->erasesize - 1); + region_end &= ~(mtd->erasesize -1); + pr_debug(" partition start = %08x, partition end = %08x\n", + region_start, region_end); + + /* Create one partition per region */ + part->name = kstrdup(name, GFP_KERNEL); + if (!part->name) + return -ENOMEM; + part->offset = region_start; + part->size = region_end - region_start; + part->mask_flags = 0; + } + + return 0; +} + static int parse_afs_partitions(struct mtd_info *mtd, const struct mtd_partition **pparts, struct mtd_part_parser_data *data) @@ -200,6 +347,10 @@ static int parse_afs_partitions(struct mtd_info *mtd, sz += sizeof(struct mtd_partition); i += 1; } + if (afs_is_v2(mtd, off)) { + sz += sizeof(struct mtd_partition); + i += 1; + } } if (!i) @@ -213,13 +364,18 @@ static int parse_afs_partitions(struct mtd_info *mtd, * Identify the partitions */ for (i = off = 0; off < mtd->size; off += mtd->erasesize) { - if (afs_is_v1(mtd, off)) { ret = afs_parse_v1_partition(mtd, off, &parts[i]); if (ret) goto out_free_parts; i++; } + if (afs_is_v2(mtd, off)) { + ret = afs_parse_v2_partition(mtd, off, &parts[i]); + if (ret) + goto out_free_parts; + i++; + } } *pparts = parts;