From patchwork Wed Apr 19 04:11:41 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cyril Bur X-Patchwork-Id: 752080 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3w77t932g2z9s2x for ; Wed, 19 Apr 2017 14:14:09 +1000 (AEST) Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) by lists.ozlabs.org (Postfix) with ESMTP id 3w77t92CZ0zDqJk for ; Wed, 19 Apr 2017 14:14:09 +1000 (AEST) X-Original-To: skiboot@lists.ozlabs.org Delivered-To: skiboot@lists.ozlabs.org Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com [148.163.156.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 3w77sW0knTzDq9P for ; Wed, 19 Apr 2017 14:13:34 +1000 (AEST) Received: from pps.filterd (m0098396.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.20/8.16.0.20) with SMTP id v3J48ekR005604 for ; Wed, 19 Apr 2017 00:13:20 -0400 Received: from e23smtp02.au.ibm.com (e23smtp02.au.ibm.com [202.81.31.144]) by mx0a-001b2d01.pphosted.com with ESMTP id 29wqq1b03m-1 (version=TLSv1.2 cipher=AES256-SHA bits=256 verify=NOT) for ; Wed, 19 Apr 2017 00:13:20 -0400 Received: from localhost by e23smtp02.au.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Wed, 19 Apr 2017 14:13:17 +1000 Received: from d23relay06.au.ibm.com (202.81.31.225) by e23smtp02.au.ibm.com (202.81.31.208) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Wed, 19 Apr 2017 14:13:16 +1000 Received: from d23av04.au.ibm.com (d23av04.au.ibm.com [9.190.235.139]) by d23relay06.au.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id v3J4D8fB50659440 for ; Wed, 19 Apr 2017 14:13:16 +1000 Received: from d23av04.au.ibm.com (localhost [127.0.0.1]) by d23av04.au.ibm.com (8.14.4/8.14.4/NCO v10.0 AVout) with ESMTP id v3J4Ch2p025890 for ; Wed, 19 Apr 2017 14:12:43 +1000 Received: from ozlabs.au.ibm.com (ozlabs.au.ibm.com [9.192.253.14]) by d23av04.au.ibm.com (8.14.4/8.14.4/NCO v10.0 AVin) with ESMTP id v3J4Chbl025419; Wed, 19 Apr 2017 14:12:43 +1000 Received: from camb691.ozlabs.ibm.com (haven.au.ibm.com [9.192.254.114]) (using TLSv1.2 with cipher DHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by ozlabs.au.ibm.com (Postfix) with ESMTPSA id CE4C0A0265; Wed, 19 Apr 2017 14:12:19 +1000 (AEST) From: Cyril Bur To: skiboot@lists.ozlabs.org Date: Wed, 19 Apr 2017 14:11:41 +1000 X-Mailer: git-send-email 2.12.2 In-Reply-To: <20170419041143.12042-1-cyril.bur@au1.ibm.com> References: <20170419041143.12042-1-cyril.bur@au1.ibm.com> X-TM-AS-MML: disable x-cbid: 17041904-0004-0000-0000-000001F6378D X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 17041904-0005-0000-0000-000009EA552B Message-Id: <20170419041143.12042-4-cyril.bur@au1.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:, , definitions=2017-04-19_02:, , signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 spamscore=0 suspectscore=1 malwarescore=0 phishscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1703280000 definitions=main-1704190039 Subject: [Skiboot] [PATCH v2 3/5] libflash: Add tests for blocklevel_smart_erase() X-BeenThere: skiboot@lists.ozlabs.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Mailing list for skiboot development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: alistair@popple.id.au, sam@mendozajonas.com MIME-Version: 1.0 Errors-To: skiboot-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org Sender: "Skiboot" Signed-off-by: Cyril Bur --- libflash/test/test-blocklevel.c | 245 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 243 insertions(+), 2 deletions(-) diff --git a/libflash/test/test-blocklevel.c b/libflash/test/test-blocklevel.c index 2f0f77ba..85098f12 100644 --- a/libflash/test/test-blocklevel.c +++ b/libflash/test/test-blocklevel.c @@ -1,4 +1,4 @@ -/* Copyright 2013-2014 IBM Corp. +/* Copyright 2013-2017 IBM Corp. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -27,9 +27,93 @@ #define ERR(fmt...) fprintf(stderr, fmt) -int main(void) +/* Setting this to true prints quite a lot of debug */ +bool libflash_debug = 0; + +static int bl_test_bad_read(struct blocklevel_device *bl __unused, uint64_t pos __unused, + void *buf __unused, uint64_t len __unused) +{ + return FLASH_ERR_PARM_ERROR; +} + +static int bl_test_read(struct blocklevel_device *bl, uint64_t pos, void *buf, uint64_t len) +{ + if (pos + len > 0x1000) + return FLASH_ERR_PARM_ERROR; + + memcpy(buf, bl->priv + pos, len); + + return 0; +} + +static int bl_test_bad_write(struct blocklevel_device *bl __unused, uint64_t pos __unused, + const void *buf __unused, uint64_t len __unused) +{ + return FLASH_ERR_PARM_ERROR; +} + +static int bl_test_write(struct blocklevel_device *bl, uint64_t pos, const void *buf, uint64_t len) +{ + if (pos + len > 0x1000) + return FLASH_ERR_PARM_ERROR; + + memcpy(bl->priv + pos, buf, len); + + return 0; +} + +static int bl_test_erase(struct blocklevel_device *bl, uint64_t pos, uint64_t len) +{ + if (pos + len > 0x1000) + return FLASH_ERR_PARM_ERROR; + + memset(bl->priv + pos, 0xff, len); + + return 0; +} + + +static void dump_buf(uint8_t *buf, int start, int end, int miss) +{ + int i; + + printf("pos: value\n"); + for (i = start; i < end; i++) + printf("%04x: %c%s\n", i, buf[i] == 0xff ? '-' : buf[i], i == miss ? " <- First missmatch" : ""); +} + +/* + * Returns zero if the buffer is ok. Otherwise returns the position of + * the mismatch. If the mismatch is at zero -1 is returned + */ +static int check_buf(uint8_t *buf, int zero_start, int zero_end) { int i; + + for (i = 0; i < 0x1000; i++) { + if (i >= zero_start && i < zero_end && buf[i] != 0xff) + return i == 0 ? -1 : i; + if ((i < zero_start || i >= zero_end) && buf[i] != (i % 26) + 'a') + return i == 0 ? -1 : i; + } + + return 0; +} + +static void reset_buf(uint8_t *buf) +{ + int i; + + for (i = 0; i < 0x1000; i++) { + /* This gives repeating a - z which will be nice to visualise */ + buf[i] = (i % 26) + 'a'; + } +} + +int main(void) +{ + int i, miss; + char *buf; struct blocklevel_device bl_mem = { 0 }; struct blocklevel_device *bl = &bl_mem; @@ -208,6 +292,163 @@ int main(void) } } + /* + * Test blocklevel_smart_erase() + * Probably safe to zero the blocklevel we've got + */ + buf = malloc(0x1000); + if (!buf) { + ERR("Malloc failed\n"); + return 1; + } + memset(bl, 0, sizeof(*bl)); + bl_mem.read = &bl_test_read; + bl_mem.write = &bl_test_write; + bl_mem.erase = &bl_test_erase; + bl_mem.erase_mask = 0xff; + bl_mem.priv = buf; + reset_buf(buf); + + + /* + * Test 1: One full and exact erase block, this shouldn't call + * read or write, ensure this fails if it does. + */ + bl_mem.write = &bl_test_bad_write; + bl_mem.read = &bl_test_bad_read; + if (blocklevel_smart_erase(bl, 0x100, 0x100)) { + ERR("Failed to blocklevel_smart_erase(0x100, 0x100)\n"); + return 1; + } + miss = check_buf(buf, 0x100, 0x200); + if (miss) { + ERR("Buffer mismatch after blocklevel_smart_erase(0x100, 0x100) at 0x%0x\n", + miss == -1 ? 0 : miss); + dump_buf(buf, 0xfc, 0x105, miss == -1 ? 0 : miss); + dump_buf(buf, 0x1fc, 0x205, miss == -1 ? 0 : miss); + return 1; + } + bl_mem.read = &bl_test_read; + bl_mem.write = &bl_test_write; + + reset_buf(buf); + /* Test 2: Only touch one erase block */ + if (blocklevel_smart_erase(bl, 0x20, 0x40)) { + ERR("Failed to blocklevel_smart_erase(0x20, 0x40)\n"); + return 1; + } + miss = check_buf(buf, 0x20, 0x60); + if (miss) { + ERR("Buffer mismatch after blocklevel_smart_erase(0x20, 0x40) at 0x%x\n", + miss == -1 ? 0 : miss); + dump_buf(buf, 0x1c, 0x65, miss == -1 ? 0 : miss); + return 1; + } + + reset_buf(buf); + /* Test 3: Start aligned but finish somewhere in it */ + if (blocklevel_smart_erase(bl, 0x100, 0x50)) { + ERR("Failed to blocklevel_smart_erase(0x100, 0x50)\n"); + return 1; + } + miss = check_buf(buf, 0x100, 0x150); + if (miss) { + ERR("Buffer mismatch after blocklevel_smart_erase(0x100, 0x50) at 0x%0x\n", + miss == -1 ? 0 : miss); + dump_buf(buf, 0xfc, 0x105, miss == -1 ? 0 : miss); + dump_buf(buf, 0x14c, 0x155, miss == -1 ? 0 : miss); + return 1; + } + + reset_buf(buf); + /* Test 4: Start somewhere in it, finish aligned */ + if (blocklevel_smart_erase(bl, 0x50, 0xb0)) { + ERR("Failed to blocklevel_smart_erase(0x50, 0xb0)\n"); + return 1; + } + miss = check_buf(buf, 0x50, 0x100); + if (miss) { + ERR("Buffer mismatch after blocklevel_smart_erase(0x50, 0xb0) at 0x%x\n", + miss == -1 ? 0 : miss); + dump_buf(buf, 0x4c, 0x55, miss == -1 ? 0 : miss); + dump_buf(buf, 0x100, 0x105, miss == -1 ? 0 : miss); + return 1; + } + + reset_buf(buf); + /* Test 5: Cover two erase blocks exactly */ + if (blocklevel_smart_erase(bl, 0x100, 0x200)) { + ERR("Failed to blocklevel_smart_erase(0x100, 0x200)\n"); + return 1; + } + miss = check_buf(buf, 0x100, 0x300); + if (miss) { + ERR("Buffer mismatch after blocklevel_smart_erase(0x100, 0x200) at 0x%x\n", + miss == -1 ? 0 : miss); + dump_buf(buf, 0xfc, 0x105, miss == -1 ? 0 : miss); + dump_buf(buf, 0x2fc, 0x305, miss == -1 ? 0 : miss); + return 1; + } + + reset_buf(buf); + /* Test 6: Erase 1.5 blocks (start aligned) */ + if (blocklevel_smart_erase(bl, 0x100, 0x180)) { + ERR("Failed to blocklevel_smart_erase(0x100, 0x180)\n"); + return 1; + } + miss = check_buf(buf, 0x100, 0x280); + if (miss) { + ERR("Buffer mismatch after blocklevel_smart_erase(0x100, 0x180) at 0x%x\n", + miss == -1 ? 0 : miss); + dump_buf(buf, 0xfc, 0x105, miss == -1 ? 0 : miss); + dump_buf(buf, 0x27c, 0x285, miss == -1 ? 0 : miss); + return 1; + } + + reset_buf(buf); + /* Test 7: Erase 1.5 blocks (end aligned) */ + if (blocklevel_smart_erase(bl, 0x80, 0x180)) { + ERR("Failed to blocklevel_smart_erase(0x80, 0x180)\n"); + return 1; + } + miss = check_buf(buf, 0x80, 0x200); + if (miss) { + ERR("Buffer mismatch after blocklevel_smart_erase(0x80, 0x180) at 0x%x\n", + miss == -1 ? 0 : miss); + dump_buf(buf, 0x7c, 0x85, miss == -1 ? 0 : miss); + dump_buf(buf, 0x1fc, 0x205, miss == -1 ? 0 : miss); + return 1; + } + + reset_buf(buf); + /* Test 8: Erase a big section, not aligned */ + if (blocklevel_smart_erase(bl, 0x120, 0x544)) { + ERR("Failed to blocklevel_smart_erase(0x120, 0x544)\n"); + return 1; + } + miss = check_buf(buf, 0x120, 0x664); + if (miss) { + ERR("Buffer mismatch after blocklevel_smart_erase(0x120, 0x544) at 0x%x\n", + miss == -1 ? 0 : miss); + dump_buf(buf, 0x11c, 0x125, miss == -1 ? 0 : miss); + dump_buf(buf, 0x65f, 0x669, miss == -1 ? 0 : miss); + return 1; + } + + reset_buf(buf); + /* Test 9: Erase a biger section, not aligned */ + if (blocklevel_smart_erase(bl, 0x20, 0xa00)) { + ERR("Failed to blocklevel_smart_erase(0x20, 0xa00)\n"); + return 1; + } + miss = check_buf(buf, 0x20, 0xa20); + if (miss) { + ERR("Buffer mismatch after blocklevel_smart_erase(0x20, 0xa00) at 0x%x\n", + miss == -1 ? 0 : miss); + dump_buf(buf, 0x1c, 0x25, miss == -1 ? 0 : miss); + dump_buf(buf, 0xa1c, 0xa25, miss == -1 ? 0 : miss); + return 1; + } return 0; }