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: 1031912 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.infradead.org (client-ip=2607:7c80:54:e::133; helo=bombadil.infradead.org; envelope-from=linux-mtd-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=linaro.org Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="lV4s8Red"; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b="UzHReQfF"; dkim-atps=neutral Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 43pB3H2g64z9s6w for ; Tue, 29 Jan 2019 00:55:23 +1100 (AEDT) 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 1go7Nd-0000lX-6b; Mon, 28 Jan 2019 13:55:17 +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-Google-Smtp-Source: ALg8bN5FRhmU7JK6+8dmpIoeaRFNdS8Z4T3P9LAfJ/Pe4t0EoH1qdtlpb2fSV2fnRveeH4D0tfSJUw== 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+incoming=patchwork.ozlabs.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 Acked-by: Liviu Dudau --- drivers/mtd/parsers/afs.c | 67 +++++++++++++++++++-------------------- 1 file changed, 32 insertions(+), 35 deletions(-) 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: 1031913 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.infradead.org (client-ip=2607:7c80:54:e::133; helo=bombadil.infradead.org; envelope-from=linux-mtd-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=linaro.org Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="pIHOBKt3"; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b="FcJNsc+1"; dkim-atps=neutral Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 43pB3Y0pDWz9s6w for ; Tue, 29 Jan 2019 00:55:37 +1100 (AEDT) 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-Ml; 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-Google-Smtp-Source: ALg8bN5soItK2sixqFIOrt/+OiXysrlki/WPEboWEAYdmp56jPacjhd5pXeHbl0n7b8gLBDsr1yrwQ== 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+incoming=patchwork.ozlabs.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(-) 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: 1031914 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.infradead.org (client-ip=2607:7c80:54:e::133; helo=bombadil.infradead.org; envelope-from=linux-mtd-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=linaro.org Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="ME2VQ8RS"; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b="jofZ87qy"; dkim-atps=neutral Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 43pB3p1y19z9s3l for ; Tue, 29 Jan 2019 00:55:50 +1100 (AEDT) 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-Po; 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-Google-Smtp-Source: AHgI3Ia2z/1DyMSUiI86tmxlqH+51skPqyaIohc8dKHKksLtuyYPuAx9gdmssoJn6hvWWIZnlU69eA== 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+incoming=patchwork.ozlabs.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(-) 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: 1031915 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.infradead.org (client-ip=2607:7c80:54:e::133; helo=bombadil.infradead.org; envelope-from=linux-mtd-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=linaro.org Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="mJCsrMOo"; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b="gRDhXiqF"; dkim-atps=neutral Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 43pB436WMgz9s6w for ; Tue, 29 Jan 2019 00:56:03 +1100 (AEDT) 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 1go7OG-0001VT-ND; Mon, 28 Jan 2019 13:55:56 +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-Google-Smtp-Source: ALg8bN7A7m/SJQE8/rq4NAcWoxFVLmRsdLNGuBM+DDB/7RY9s3y45D20p+274acEuMUvztBLgx3PMA== 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+incoming=patchwork.ozlabs.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(-) 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: 1031916 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.infradead.org (client-ip=2607:7c80:54:e::133; helo=bombadil.infradead.org; envelope-from=linux-mtd-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=linaro.org Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="mDqDX3Md"; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b="NjnkcihM"; dkim-atps=neutral Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 43pB4L07S3z9s3l for ; Tue, 29 Jan 2019 00:56:18 +1100 (AEDT) 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-Nq; 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-Google-Smtp-Source: ALg8bN7Wz+K1e0nc1BrFw1niFC/zgEvUENT9GPGTUmFpCchKEAw+lyAryE73vQP43bdM/loDteAMHA== 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+incoming=patchwork.ozlabs.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(-) 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: 1031917 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.infradead.org (client-ip=2607:7c80:54:e::133; helo=bombadil.infradead.org; envelope-from=linux-mtd-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=linaro.org Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="QMOimYSj"; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b="XcY7gFth"; dkim-atps=neutral Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 43pB4d6Trwz9s3l for ; Tue, 29 Jan 2019 00:56:33 +1100 (AEDT) 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-2h; 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-Google-Smtp-Source: ALg8bN7f4JLahviE3LfmUqX02owUDpKm8fXVlvQLL/ysyoXNOLKefp9UorFS/vGXuKS6/ES9OVK25w== 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+incoming=patchwork.ozlabs.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;