From 8d8d8ddf2c09e26d3e2721094a182c172bd1b28f Mon Sep 17 00:00:00 2001 From: Aaron Bamberger Date: Tue, 11 Aug 2020 14:16:19 -0500 Subject: [PATCH 1/6] Fix missing 64-bit offset types in diskio-unix Most of the types used in the diskio routines are explicitly 64-bit, but there were two usages of off_t, which is a 32-bit value when compiled in a 32-bit userspace. This causes gpt-fdisk to be unable to correctly write partitions on a drive larger than 4GiB when compiled in a 32-bit userspace. This change updates the two usages of off_t to off64_t, which is explicitly 64-bit and allows the program to work as intended. --- diskio-unix.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/diskio-unix.cc b/diskio-unix.cc index c6b30a7..101e1c9 100644 --- a/diskio-unix.cc +++ b/diskio-unix.cc @@ -309,7 +309,7 @@ int DiskIO::DiskSync(void) { // Note that seeking beyond the end of the file is NOT detected as a failure! int DiskIO::Seek(uint64_t sector) { int retval = 1; - off_t seekTo, sought; + off64_t seekTo, sought; // If disk isn't open, try to open it.... if (!isOpen) { @@ -426,7 +426,7 @@ int DiskIO::Write(void* buffer, int numBytes) { // return correct values for disk image files. uint64_t DiskIO::DiskSize(int *err) { uint64_t sectors = 0; // size in sectors - off_t bytes = 0; // size in bytes + off64_t bytes = 0; // size in bytes struct stat64 st; int platformFound = 0; #ifdef __sun__ -- Gitee From ee6e551bf9372a9f98b9b58baa30e60a7260537b Mon Sep 17 00:00:00 2001 From: Rod Smith Date: Wed, 13 Jan 2021 10:29:24 -0500 Subject: [PATCH 2/6] Fix bug that could cause crash if a badly-formatted MBR disk was read. --- basicmbr.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/basicmbr.cc b/basicmbr.cc index 8ac9789..89b0bdc 100644 --- a/basicmbr.cc +++ b/basicmbr.cc @@ -292,7 +292,8 @@ int BasicMBRData::ReadLogicalParts(uint64_t extendedStart, int partNum) { if (EbrLocations[i] == offset) { // already read this one; infinite logical partition loop! cerr << "Logical partition infinite loop detected! This is being corrected.\n"; allOK = -1; - partNum -= 1; + if (partNum > 0) //don't go negative + partNum -= 1; } // if } // for EbrLocations[partNum] = offset; -- Gitee From b14bf7b5f3a7ae71ebe5e27c37be3eb7b5027a26 Mon Sep 17 00:00:00 2001 From: Rod Smith Date: Mon, 14 Sep 2020 10:08:18 -0400 Subject: [PATCH 3/6] Fix segfault on some weird data structures --- NEWS | 6 ++++++ gpt.cc | 13 ++++++++++++- support.h | 2 +- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/NEWS b/NEWS index 54c865e..bac3da3 100644 --- a/NEWS +++ b/NEWS @@ -1,3 +1,9 @@ +1.0.6 (?/??/2020): +------------------ + +- Fixed bug that could cause segfault if GPT header claimed partition + entries are oversized. + 1.0.5 (2/17/2020): ------------------ diff --git a/gpt.cc b/gpt.cc index 1546313..724eec3 100644 --- a/gpt.cc +++ b/gpt.cc @@ -1043,6 +1043,14 @@ int GPTData::LoadHeader(struct GPTHeader *header, DiskIO & disk, uint64_t sector } // if *crcOk = CheckHeaderCRC(&tempHeader); + if (tempHeader.sizeOfPartitionEntries != sizeof(GPTPart)) { + cerr << "Warning: Partition table header claims that the size of partition table\n"; + cerr << "entries is " << tempHeader.sizeOfPartitionEntries << " bytes, but this program "; + cerr << " supports only " << sizeof(GPTPart) << "-byte entries.\n"; + cerr << "Adjusting accordingly, but partition table may be garbage.\n"; + tempHeader.sizeOfPartitionEntries = sizeof(GPTPart); + } + if (allOK && (numParts != tempHeader.numParts) && *crcOk) { allOK = SetGPTSize(tempHeader.numParts, 0); } @@ -1060,7 +1068,10 @@ int GPTData::LoadPartitionTable(const struct GPTHeader & header, DiskIO & disk, uint32_t sizeOfParts, newCRC; int retval; - if (disk.OpenForRead()) { + if (header.sizeOfPartitionEntries != sizeof(GPTPart)) { + cerr << "Error! GPT header contains invalid partition entry size!\n"; + retval = 0; + } else if (disk.OpenForRead()) { if (sector == 0) { retval = disk.Seek(header.partitionEntriesLBA); } else { diff --git a/support.h b/support.h index 9a79b95..978bfe1 100644 --- a/support.h +++ b/support.h @@ -8,7 +8,7 @@ #ifndef __GPTSUPPORT #define __GPTSUPPORT -#define GPTFDISK_VERSION "1.0.5" +#define GPTFDISK_VERSION "1.0.5.1" #if defined (__FreeBSD__) || defined (__FreeBSD_kernel__) || defined (__APPLE__) // Darwin (Mac OS) & FreeBSD: disk IOCTLs are different, and there is no lseek64 -- Gitee From 397bb5357d1713bebde40fba6bac27cc62763f77 Mon Sep 17 00:00:00 2001 From: Erik Larsson Date: Sun, 7 Mar 2021 14:46:59 +0000 Subject: [PATCH 4/6] gptpart.cc: Fix double byteswap for big-endian architectures. The data in 'name' was already byteswapped by ReversePartBytes, so byteswapping it again in GetDescription returned each UTF-16BE unit back to UTF-16LE and caused seemingly garbage strings to be printed. --- gptpart.cc | 1 - 1 file changed, 1 deletion(-) diff --git a/gptpart.cc b/gptpart.cc index b7f9d7f..7c875ba 100644 --- a/gptpart.cc +++ b/gptpart.cc @@ -94,7 +94,6 @@ string GPTPart::GetDescription(void) { size_t pos = 0 ; while ( ( pos < NAME_SIZE ) && ( name[ pos ] != 0 ) ) { uint16_t cp = name[ pos ++ ] ; - if ( ! IsLittleEndian() ) ReverseBytes( & cp , 2 ) ; // first to utf32 uint32_t uni ; if ( cp < 0xd800 || cp > 0xdfff ) { -- Gitee From 066a209dbd777d29501d01090c43e128d3308a84 Mon Sep 17 00:00:00 2001 From: xlfeng Date: Fri, 17 Dec 2021 15:31:56 +0800 Subject: [PATCH 5/6] Dump machine-readable MBR or GPT details. from Jeff Sharkey d4e73c9d. Signed-off-by: xlfeng --- sgdisk.cc | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/sgdisk.cc b/sgdisk.cc index d3fe1a8..c97d763 100644 --- a/sgdisk.cc +++ b/sgdisk.cc @@ -9,13 +9,85 @@ /* This program is copyright (c) 2009-2011 by Roderick W. Smith. It is distributed under the terms of the GNU GPL version 2, as detailed in the COPYING file. */ +#include +#include +#include +#include + #include "gptcl.h" using namespace std; #define MAX_OPTIONS 50 +/* + * Dump partition details in a machine readable format: + * + * DISK[mbr|gpt][guid] + * PART[n][type][guid] + */ +static int ohos_dump(char* device) { + BasicMBRData mbrData; + GPTData gptData; + GPTPart partData; + int numParts = 0; + stringstream res; + + /* Silence noisy underlying library */ + int stdout = dup(STDOUT_FILENO); + int silence = open("/dev/null", 0); + dup2(silence, STDOUT_FILENO); + dup2(silence, STDERR_FILENO); + + if (!mbrData.ReadMBRData((string)device)) { + cerr << "Failed to read MBR" << endl; + return 8; + } + + switch(mbrData.GetValidity()) { + case mbr: + res << "DISK mbr" << endl; + for (int i = 0; i < MAX_MBR_PARTS; i++) { + if(mbrData.GetLength(i) > 0) { + res << "PART" << (i + 1) << " " << hex << (int)mbrData.GetType(i) << dec << endl; + } + } + break; + case gpt: + gptData.JustLooking(); + if(!gptData.LoadPartitions((string)device)) { + cerr << "Failed to read GPT" << endl; + return 9; + } + + res << "DISK gpt " << gptData.GetDiskGUID() << endl; + numParts = gptData.GetNumParts(); + for (int i = 0; i < numParts; i++) { + partData = gptData[i]; + if (partData.GetFirstLBA() > 0) { + res << "PART " << (i + 1) << " " << partData.GetType() << " " << partData.GetUniqueGUID() << " " + << partData.GetDescription() << endl; + } + } + break; + default: + cerr << "Unknown partition table" << endl; + return 10; + } + + /* Write our actual output */ + string resString = res.str(); + write(stdout, resString.c_str(), resString.length()); + return 0; +} + int main(int argc, char *argv[]) { + for (int i = 0; i < argc; i++) { + if (!strcmp("--ohos-dump", argv[i])) { + return ohos_dump(argv[i + 1]); + } + } GPTDataCL theGPT; return theGPT.DoOptions(argc, argv); } // main + -- Gitee From dae229dbac60899d0b5152921d98ded97533dcb1 Mon Sep 17 00:00:00 2001 From: xlfeng Date: Mon, 20 Dec 2021 20:12:42 +0800 Subject: [PATCH 6/6] Modify Huawei license to GPL-2.0 license.Modify the value of subsystem_name.Delete annotation line. Signed-off-by: xlfeng --- BUILD.gn | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/BUILD.gn b/BUILD.gn index 103f79a..ecaf40d 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -1,15 +1,9 @@ -#Copyright (c) 2021 Huawei Device Co., Ltd. -#Licensed under the Apache License, Version 2.0 (the "License"); -#you may not use this file except in compliance with the License. -#You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -#Unless required by applicable law or agreed to in writing, software -#distributed under the License is distributed on an "AS IS" BASIS, -#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -#See the License for the specific language governing permissions and -#limitations under the License. +# Copyright (c) 2021 Huawei Device Co., Ltd. +# +# 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 +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. import("//build/ohos.gni") import("//build/ohos/ndk/ndk.gni") @@ -44,7 +38,6 @@ ohos_executable("sgdisk") { "guid.cc", "mbr.cc", "mbrpart.cc", - # "ohos_popt.cc", "parttypes.cc", "sgdisk.cc", "support.cc", @@ -54,6 +47,6 @@ ohos_executable("sgdisk") { "//third_party/e2fsprogs/lib/uuid:libext2_uuid", "//third_party/popt:popt_static", ] - subsystem_name = "storage" + subsystem_name = "storage_manager" part_name = "storage_standard" } -- Gitee