From patchwork Thu Dec 17 11:27:38 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Szyprowski X-Patchwork-Id: 344921 Delivered-To: patch@linaro.org Received: by 2002:a02:85a7:0:0:0:0:0 with SMTP id d36csp1076821jai; Thu, 17 Dec 2020 03:28:17 -0800 (PST) X-Google-Smtp-Source: ABdhPJwdP23YE0RvhtED1ncWJTbeeD3qnMCM+7+eGf95aBSj3xVG0gyRworoLmQBqYmSyU1Ri+Ep X-Received: by 2002:a17:906:4544:: with SMTP id s4mr1988355ejq.366.1608204497295; Thu, 17 Dec 2020 03:28:17 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1608204497; cv=none; d=google.com; s=arc-20160816; b=hBz8lVJY8KDcSlKeSY899BP9c7XT/2b693E/KEIWe5WmVd/vIvlQ/Zv/+T0Hc78wOS t1sdd7fop0G615QdzXL3D1vjhJafhE0N4JFYKG46V6/4NRkvMSR/PxBRSQej+E0tB55Q EXjeEgNJIpqszR6FHJQ8rTnaxMEScTFybcgVAzg2MdSYkGZ2lk2fkTJ5p9yQ8K0tQM4+ cnnvCV8Tr1xW6RgU8Ra0w7tM3BnUAk7n+MgGQ5bMat2MOW4Gnf1Zwn61ImSAUn3PbLDB 6wiuaVJzut7SIBMgIEEhPY9FRXiEr9raUOO+LheD70J0WCs/yY31IZ7vSJBrmeM5zj2p xPkQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:references:cms-type:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature:dkim-filter; bh=k9oMBZwID77q6mtg2L5cpHL5kStBnaPFM2CjIM8gmj0=; b=0QNBX8HzmlGwU1ebCLxgisBK7OpS4SU00jJ6KeC5hL6BGqjWbahqbO/nabR+9EqCza jVjGuXfF37GjTeQABVy/6TRhmbXOUHwnOR37gvnF7Hpiomu7YgFdLGi6fik+L6tudMmR bbI28s83dbkzucSYtZdGj1RY/jN0QvQ4KqGyhKtETeanq1kx/CaJC9TVS5oCgeXonlmX QbwLae036UM+05Z80675++6O3Od9hCDwzSELnuAbZbPlRx53ThlM6WkG9otz7q3WwMZH xbNFjVVpKZr1HN+TI70HgMd7+FbMnVCONPE63RcLFFxCPyM4kYMP+y4XAksgyA3h4Mle SbYw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@samsung.com header.s=mail20170921 header.b=ciZ8enUP; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=samsung.com Return-Path: Received: from phobos.denx.de (phobos.denx.de. [2a01:238:438b:c500:173d:9f52:ddab:ee01]) by mx.google.com with ESMTPS id n13si4210063eds.172.2020.12.17.03.28.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Dec 2020 03:28:17 -0800 (PST) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; Authentication-Results: mx.google.com; dkim=pass header.i=@samsung.com header.s=mail20170921 header.b=ciZ8enUP; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=samsung.com Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id C9C3B82B9F; Thu, 17 Dec 2020 12:28:07 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=samsung.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (1024-bit key; unprotected) header.d=samsung.com header.i=@samsung.com header.b="ciZ8enUP"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 7E30482BA0; Thu, 17 Dec 2020 12:28:02 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-7.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,RCVD_IN_DNSWL_HI, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mailout2.w1.samsung.com (mailout2.w1.samsung.com [210.118.77.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 9F28382B96 for ; Thu, 17 Dec 2020 12:27:58 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=samsung.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=m.szyprowski@samsung.com Received: from eucas1p1.samsung.com (unknown [182.198.249.206]) by mailout2.w1.samsung.com (KnoxPortal) with ESMTP id 20201217112747euoutp029be16fff6fd68f1f901d44c3777b7bb3~RfXFkzCFJ2518325183euoutp02d for ; Thu, 17 Dec 2020 11:27:47 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 mailout2.w1.samsung.com 20201217112747euoutp029be16fff6fd68f1f901d44c3777b7bb3~RfXFkzCFJ2518325183euoutp02d DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=samsung.com; s=mail20170921; t=1608204467; bh=k9oMBZwID77q6mtg2L5cpHL5kStBnaPFM2CjIM8gmj0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ciZ8enUPbuuTtcQY8eVnjpSxZmVwXVezJVUNhUHAQMMK9V4biuMNgedXnFoiGAqXM UaMFpu19yGD0NLQuSO2BMF3UF4FL1KcDf/rbEmolrDzwLKi/7UDYlP/pW0IKZlso5Y Oi4L6J4hFJ6l8lITSaadGVF67ym5+K8djPCWX7DI= Received: from eusmges2new.samsung.com (unknown [203.254.199.244]) by eucas1p1.samsung.com (KnoxPortal) with ESMTP id 20201217112747eucas1p19ae7d5665e520857b947581c79cc9299~RfXFNRXit0731407314eucas1p17; Thu, 17 Dec 2020 11:27:47 +0000 (GMT) Received: from eucas1p2.samsung.com ( [182.198.249.207]) by eusmges2new.samsung.com (EUCPMTA) with SMTP id 28.2B.44805.3B04BDF5; Thu, 17 Dec 2020 11:27:47 +0000 (GMT) Received: from eusmtrp1.samsung.com (unknown [182.198.249.138]) by eucas1p2.samsung.com (KnoxPortal) with ESMTPA id 20201217112747eucas1p2d701d716f010a08e19afb6c133432c0e~RfXE4yJlm0440704407eucas1p2m; Thu, 17 Dec 2020 11:27:47 +0000 (GMT) Received: from eusmgms1.samsung.com (unknown [182.198.249.179]) by eusmtrp1.samsung.com (KnoxPortal) with ESMTP id 20201217112747eusmtrp1cc4585efd676a8190da88bf02673fa50~RfXE4Ly_P0498804988eusmtrp1k; Thu, 17 Dec 2020 11:27:47 +0000 (GMT) X-AuditID: cbfec7f4-b4fff7000000af05-a5-5fdb40b3391d Received: from eusmtip2.samsung.com ( [203.254.199.222]) by eusmgms1.samsung.com (EUCPMTA) with SMTP id 5E.73.21957.3B04BDF5; Thu, 17 Dec 2020 11:27:47 +0000 (GMT) Received: from AMDC2765.digital.local (unknown [106.120.51.73]) by eusmtip2.samsung.com (KnoxPortal) with ESMTPA id 20201217112746eusmtip2c555b0aa749741d858981bad6112ae09~RfXEdg4oY1639616396eusmtip2y; Thu, 17 Dec 2020 11:27:46 +0000 (GMT) From: Marek Szyprowski To: u-boot@lists.denx.de Cc: Marek Szyprowski , Lukasz Majewski , Simon Glass , Heinrich Schuchardt , Jaehoon Chung , Bartlomiej Zolnierkiewicz Subject: [PATCH 5/6] disk: dos: add code for creating MBR partition layout Date: Thu, 17 Dec 2020 12:27:38 +0100 Message-Id: <20201217112739.5045-6-m.szyprowski@samsung.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20201217112739.5045-1-m.szyprowski@samsung.com> X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFnrPIsWRmVeSWpSXmKPExsWy7djP87qbHW7HG0zslLHYOGM9q8WNX22s Fs/fXWayWHvkLrvFty3bGC3e7u1ktzg89QOjA7vH7IaLLB7zZp1g8fjwMc7j7J0djB59W1Yx BrBGcdmkpOZklqUW6dslcGUcW3qYsaDBsGLJ9x62BsY+tS5GTg4JAROJq+t+MncxcnEICaxg lDjY/JoRwvnCKHFm1h92COczo8TFc83MMC1fbt+DqlrOKPH50RsmuJaNm7+zglSxCRhKdL3t YgOxRQQkJH71XwXrYBb4B7Tk10ewImEBL4kn/XOBxnJwsAioSjzcmwsS5hWwkWi79hlqm7zE 6g0HwEo4BWwllreXgYyREJjJIfFt/lMmiBoXiX+njrFB2MISr45vYYewZSROT+5hgWhoZpR4 eG4tO4TTwyhxuWkGI0SVtcSdc7/YQDYwC2hKrN+lDxF2lJj2qQEsLCHAJ3HjrSBImBnInLRt OjNEmFeio00IolpNYtbxdXBrD164BHW+h8ST722skPCZwChx4OFFlgmM8rMQli1gZFzFKJ5a WpybnlpslJdarlecmFtcmpeul5yfu4kRmCZO/zv+ZQfj8lcf9Q4xMnEwHmKU4GBWEuFNOHAz Xog3JbGyKrUoP76oNCe1+BCjNAeLkjhv0pY18UIC6YklqdmpqQWpRTBZJg5OqQamxQvvfVP4 xjtl9SlR5+I1wTOVdd51iT0Q8Gi3OC9+fEKM8JZnHmxydoxXg6cEXC5RMwpjrTZ3bN2de8Th 6Q3G2Ts51hquN+tKZlX/MU/sa6Mjc2txpwJXc+t7eZXwGcd3217wffls7RJ/R8P3y47KrY7R rU1WFl6RwrFAryql5H/+SrPC2RtfNGzgfGIVe2z1zglRE478Xfx4hu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Ip jPELHx8+mBP79mmz9f+Xri/NeLVvrHgzW9PtamXMmthLH21vrCyRXqzLdJtx2QWfyd+2G/QU dutmuJ1aMMtDbFfFv5ay4+1KLMUZiYZazEXFiQBTDKWc4QIAAA== X-CMS-MailID: 20201217112747eucas1p2d701d716f010a08e19afb6c133432c0e X-Msg-Generator: CA X-RootMTR: 20201217112747eucas1p2d701d716f010a08e19afb6c133432c0e X-EPHeader: CA CMS-TYPE: 201P X-CMS-RootMailID: 20201217112747eucas1p2d701d716f010a08e19afb6c133432c0e References: <20201217112739.5045-1-m.szyprowski@samsung.com> X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.102.3 at phobos.denx.de X-Virus-Status: Clean Add a code for creating and writing MBR partition layout. The code generates similar layout of EBRs (Exteneded Block Records) and logical volumes as Linux's fdisk utility. Signed-off-by: Marek Szyprowski --- disk/part_dos.c | 167 ++++++++++++++++++++++++++++++++++++++++++++++++ disk/part_dos.h | 2 + include/part.h | 5 ++ 3 files changed, 174 insertions(+) -- 2.17.1 diff --git a/disk/part_dos.c b/disk/part_dos.c index 2c4ad0b6ba..f77f927995 100644 --- a/disk/part_dos.c +++ b/disk/part_dos.c @@ -319,6 +319,173 @@ int is_valid_dos_buf(void *buf) return test_block_type(buf) == DOS_MBR ? 0 : -1; } +#if CONFIG_IS_ENABLED(CMD_MBR) +static void lba_to_chs(lbaint_t lba, unsigned char *rc, unsigned char *rh, + unsigned char *rs) +{ + unsigned int c, h, s; + /* use fixed CHS geometry */ + unsigned int sectpertrack = 63; + unsigned int heads = 255; + + c = (lba + 1) / sectpertrack / heads; + h = (lba + 1) / sectpertrack - c * heads; + s = (lba + 1) - (c * heads + h) * sectpertrack; + + if (c > 1023) { + c = 1023; + h = 254; + s = 63; + } + + *rc = c & 0xff; + *rh = h; + *rs = s + ((c & 0x300) >> 2); +} + +static void mbr_fill_pt_entry(dos_partition_t *pt, lbaint_t start, + lbaint_t relative, lbaint_t size, uchar sys_ind, bool bootable) +{ + pt->boot_ind = bootable ? 0x80 : 0x00; + pt->sys_ind = sys_ind; + lba_to_chs(start, &pt->cyl, &pt->head, &pt->sector); + lba_to_chs(start + size - 1, &pt->end_cyl, &pt->end_head, &pt->end_sector); + put_unaligned_le32(relative, &pt->start4); + put_unaligned_le32(size, &pt->size4); +} + +int write_mbr_partitions(struct blk_desc *dev, + struct disk_partition *p, int count, unsigned int disksig) +{ + ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev->blksz); + lbaint_t ext_part_start = 0, ext_part_size = 0, ext_part_sect = 0; + dos_partition_t *pt; + int i; + + memset(buffer, 0, dev->blksz); + buffer[DOS_PART_MAGIC_OFFSET] = 0x55; + buffer[DOS_PART_MAGIC_OFFSET + 1] = 0xaa; + put_unaligned_le32(disksig, &buffer[DOS_PART_DISKSIG_OFFSET]); + pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET); + + /* create all primary partitions */ + for (i = 0; i < 4 && i < count; i++, pt++) { + mbr_fill_pt_entry(pt, p[i].start, p[i].start, p[i].size, + p[i].sys_ind, p[i].bootable); + if (is_extended(p[i].sys_ind)) { + ext_part_start = p[i].start; + ext_part_size = p[i].size; + ext_part_sect = p[i].start; + } + } + + if (i < count && !ext_part_start) { + printf("%s: extended partition is needed for more than 4 partitions\n", + __func__); + return -1; + } + + /* write MBR */ + if (blk_dwrite(dev, 0, 1, buffer) != 1) { + printf("%s: failed writing 'MBR' (1 blks at 0x0)\n", + __func__); + return -1; + } + + /* create extended volumes */ + for (; i < count; i++) { + lbaint_t next_ebr = 0; + + memset(buffer, 0, dev->blksz); + buffer[DOS_PART_MAGIC_OFFSET] = 0x55; + buffer[DOS_PART_MAGIC_OFFSET + 1] = 0xaa; + pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET); + + mbr_fill_pt_entry(pt, p[i].start, p[i].start - ext_part_sect, + p[i].size, p[i].sys_ind, p[i].bootable); + + if (i + 1 < count) { + pt++; + next_ebr = p[i].start + p[i].size; + mbr_fill_pt_entry(pt, next_ebr, + next_ebr - ext_part_start, + p[i+1].start + p[i+1].size - next_ebr, + DOS_PART_TYPE_EXTENDED, 0); + } + + /* write EBR */ + if (blk_dwrite(dev, ext_part_sect, 1, buffer) != 1) { + printf("%s: failed writing 'EBR' (1 blks at 0x%lx)\n", + __func__, ext_part_sect); + return -1; + } + ext_part_sect = next_ebr; + } + + return 0; +} + +int layout_mbr_partitions(struct disk_partition *p, int count, + lbaint_t total_sectors) +{ + struct disk_partition *ext = NULL; + int i, j; + lbaint_t ext_vol_start; + + /* calculate primary partitions start and size if needed */ + if (!p[0].start) + p[0].start = DOS_PART_DEFAULT_GAP; + for (i = 0; i < 4 && i < count; i++) { + if (!p[i].start) + p[i].start = p[i - 1].start + p[i - 1].size; + if (!p[i].size) { + lbaint_t end = total_sectors; + lbaint_t allocated = 0; + + for (j = i + 1; j < 4 && j < count; j++) { + if (p[j].start) { + end = p[j].start; + break; + } + allocated += p[j].size; + } + p[i].size = end - allocated - p[i].start; + } + if (p[i].sys_ind == 0x05) + ext = &p[i]; + } + + if (i >= 4 && !ext) { + printf("%s: extended partition is needed for more than 4 partitions\n", + __func__); + return -1; + } + + /* calculate extended volumes start and size if needed */ + ext_vol_start = ext->start; + for (i = 4; i < count; i++) { + if (!p[i].start) + p[i].start = ext_vol_start + DOS_PART_DEFAULT_GAP; + if (!p[i].size) { + lbaint_t end = ext->start + ext->size; + lbaint_t allocated = 0; + + for (j = i + 1; j < count; j++) { + if (p[j].start) { + end = p[j].start - DOS_PART_DEFAULT_GAP; + break; + } + allocated += p[j].size + DOS_PART_DEFAULT_GAP; + } + p[i].size = end - allocated - p[i].start; + } + ext_vol_start = p[i].start + p[i].size; + } + + return 0; +} +#endif + int write_mbr_sector(struct blk_desc *dev_desc, void *buf) { if (is_valid_dos_buf(buf)) diff --git a/disk/part_dos.h b/disk/part_dos.h index dd909a9317..5055822422 100644 --- a/disk/part_dos.h +++ b/disk/part_dos.h @@ -19,6 +19,8 @@ #define DOS_PART_TYPE_EXTENDED_LBA 0x0F #define DOS_PART_TYPE_EXTENDED_LINUX 0x85 +#define DOS_PART_DEFAULT_GAP 2048 + typedef struct dos_partition { unsigned char boot_ind; /* 0x80 - active */ unsigned char head; /* starting head */ diff --git a/include/part.h b/include/part.h index 67b8b2a5cc..fac36364bd 100644 --- a/include/part.h +++ b/include/part.h @@ -474,6 +474,11 @@ int is_valid_dos_buf(void *buf); */ int write_mbr_sector(struct blk_desc *dev_desc, void *buf); +int write_mbr_partitions(struct blk_desc *dev, + struct disk_partition *p, int count, unsigned int disksig); +int layout_mbr_partitions(struct disk_partition *p, int count, + lbaint_t total_sectors); + #endif