Merge tag 'android-14.0.0_r17' into staging/lineage-21.0_merge-android-14.0.0_r17

Android 14.0.0 Release 17 (UQ1A.231205.015)

# -----BEGIN PGP SIGNATURE-----
#
# iF0EABECAB0WIQRDQNE1cO+UXoOBCWTorT+BmrEOeAUCZXDPJgAKCRDorT+BmrEO
# eNrvAJ0VnrC+yN6JEYjAea2XRoE1E/JMfQCdEc7gGBotfm61VCfWyRy+IpPhIi0=
# =3+5v
# -----END PGP SIGNATURE-----
# gpg: Signature made Wed Dec  6 21:44:38 2023 EET
# gpg:                using DSA key 4340D13570EF945E83810964E8AD3F819AB10E78
# gpg: Good signature from "The Android Open Source Project <initial-contribution@android.com>" [marginal]
# gpg: initial-contribution@android.com: Verified 2181 signatures in the past
#      2 years.  Encrypted 4 messages in the past 23 months.
# gpg: WARNING: This key is not certified with sufficiently trusted signatures!
# gpg:          It is not certain that the signature belongs to the owner.
# Primary key fingerprint: 4340 D135 70EF 945E 8381  0964 E8AD 3F81 9AB1 0E78

# By Elliott Hughes
# Via Automerger Merge Worker (2) and others
* tag 'android-14.0.0_r17':
  Build sgdisk with _FILE_OFFSET_BITS=64.

Change-Id: I75e562f769c874973df7e282714ee036f684a710
diff --git a/Android.bp b/Android.bp
index c1a916a..99a446a 100644
--- a/Android.bp
+++ b/Android.bp
@@ -58,6 +58,7 @@
 
     shared_libs: ["libext2_uuid"],
     static_libs: ["libgptf"],
+    recovery_available: true,
 }
 
 lib_common_srcs = [
@@ -81,6 +82,7 @@
     name: "libgptf",
     defaults: ["gptfdisk_default_flags"],
     host_supported: true,
+    recovery_available: true,
 
     export_include_dirs: ["."],
     srcs: lib_common_srcs + ["diskio-unix.cc"],
@@ -99,3 +101,20 @@
 
     shared_libs: ["libext2_uuid"],
 }
+
+cc_library {
+    name: "libsgdisk",
+    recovery_available: true,
+
+    defaults: ["gptfdisk_default_flags"],
+
+    srcs: [
+        "sgdisk.cc",
+    ],
+    cflags: [
+        "-Dmain=sgdisk_main",
+    ],
+
+    shared_libs: ["libext2_uuid"],
+    static_libs: ["libgptf"],
+}
diff --git a/basicmbr.h b/basicmbr.h
index 696f826..8db61cd 100644
--- a/basicmbr.h
+++ b/basicmbr.h
@@ -24,6 +24,7 @@
 
 // A 512-byte data structure into which the MBR can be loaded in one
 // go. Also used when loading logical partitions.
+#pragma pack(push)
 #pragma pack(1)
 struct TempMBR {
    uint8_t code[440];
@@ -149,4 +150,6 @@
    void ShowCommands(void);
 }; // class BasicMBRData
 
+#pragma pack(pop)
+
 #endif
diff --git a/bsd.h b/bsd.h
index e042b7b..b9d5374 100644
--- a/bsd.h
+++ b/bsd.h
@@ -54,6 +54,7 @@
 // Full data in tweaked BSD format
 // For some reason this has to be packed or MS Visual C++'s debugger complains
 // about memory errors whenever a BSDData variable is destroyed.
+#pragma pack(push)
 #pragma pack (8)
 class BSDData {
    protected:
@@ -88,4 +89,6 @@
 }; // struct MBRData
 #pragma pack ()
 
+#pragma pack(pop)
+
 #endif
diff --git a/gpt.h b/gpt.h
index 6e1e0bb..a1ff47f 100644
--- a/gpt.h
+++ b/gpt.h
@@ -37,6 +37,7 @@
 enum WhichToUse {use_gpt, use_mbr, use_bsd, use_new, use_abort};
 
 // Header (first 512 bytes) of GPT table
+#pragma pack(push)
 #pragma pack(1)
 struct GPTHeader {
    uint64_t signature;
@@ -212,4 +213,6 @@
 // Function prototypes....
 int SizesOK(void);
 
+#pragma pack(pop)
+
 #endif
diff --git a/mbrpart.h b/mbrpart.h
index 6c7b21a..b889f3d 100644
--- a/mbrpart.h
+++ b/mbrpart.h
@@ -39,6 +39,7 @@
 // On read of logical entries, it's relative to the EBR record for that
 // partition. When writing EBR records, it's relative to the extended
 // partition's start.
+#pragma pack(push)
 #pragma pack(1)
 struct MBRRecord {
    uint8_t status;
@@ -108,4 +109,6 @@
     void ShowData(int isGpt);
 }; // MBRPart
 
+#pragma pack(pop)
+
 #endif // MBRPART_H
diff --git a/sgdisk.cc b/sgdisk.cc
index 1892e2d..d3524c5 100644
--- a/sgdisk.cc
+++ b/sgdisk.cc
@@ -16,29 +16,26 @@
 #include <iostream>
 #include <sstream>
 #include <errno.h>
-#include "gptcl.h"
 #include <fcntl.h>
 #include <unistd.h>
 
+#include "sgdisk.h"
+#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 android_dump(char* device) {
+int sgdisk_read(const char* device, sgdisk_partition_table& ptbl,
+                vector<sgdisk_partition>& partitions) {
     BasicMBRData mbrData;
     GPTData gptData;
     GPTPart partData;
     int numParts = 0;
-    stringstream res;
 
     /* Silence noisy underlying library */
-    int stdout = dup(STDOUT_FILENO);
+    int stdout_fd = dup(STDOUT_FILENO);
+    int stderr_fd = dup(STDERR_FILENO);
     int silence = open("/dev/null", 0);
     dup2(silence, STDOUT_FILENO);
     dup2(silence, STDERR_FILENO);
@@ -50,14 +47,20 @@
 
     switch (mbrData.GetValidity()) {
     case mbr:
-        res << "DISK mbr" << endl;
-        for (int i = 0; i < MAX_MBR_PARTS; i++) {
+        ptbl.type = MBR;
+        ptbl.guid.clear();
+        for (size_t i = 0; i < MAX_MBR_PARTS; i++) {
             if (mbrData.GetLength(i) > 0) {
-                res << "PART " << (i + 1) << " " << hex
-                        << (int) mbrData.GetType(i) << dec << endl;
+                char typebuf[2+8+1];
+                sprintf(typebuf, "%x", (unsigned int)mbrData.GetType(i));
+                sgdisk_partition part;
+                part.num = i + 1;
+                part.type = typebuf;
+                partitions.push_back(part);
             }
         }
         break;
+    case hybrid:
     case gpt:
         gptData.JustLooking();
         if (!gptData.LoadPartitions((string) device)) {
@@ -65,14 +68,17 @@
             return 9;
         }
 
-        res << "DISK gpt " << gptData.GetDiskGUID() << endl;
-        numParts = gptData.GetNumParts();
-        for (int i = 0; i < numParts; i++) {
+        ptbl.type = GPT;
+        ptbl.guid = gptData.GetDiskGUID().AsString();
+        for (size_t i = 0; i < gptData.GetNumParts(); i++) {
             partData = gptData[i];
             if (partData.GetFirstLBA() > 0) {
-                res << "PART " << (i + 1) << " " << partData.GetType() << " "
-                        << partData.GetUniqueGUID() << " "
-                        << partData.GetDescription() << endl;
+                sgdisk_partition part;
+                part.num = i + 1;
+                part.type = partData.GetType().AsString();
+                part.guid = partData.GetUniqueGUID().AsString();
+                part.name = partData.GetDescription();
+                partitions.push_back(part);
             }
         }
         break;
@@ -81,13 +87,51 @@
         return 10;
     }
 
-    /* Write our actual output */
-    string resString = res.str();
-    write(stdout, resString.c_str(), resString.length());
+    fflush(stdout);
+    fflush(stderr);
+    dup2(stdout_fd, STDOUT_FILENO);
+    dup2(stderr_fd, STDERR_FILENO);
+    close(silence);
+
     return 0;
 }
 
-int main(int argc, char *argv[]) {
+/*
+ * Dump partition details in a machine readable format:
+ *
+ * DISK [mbr|gpt] [guid]
+ * PART [n] [type] [guid]
+ */
+static int android_dump(const char* device) {
+    sgdisk_partition_table ptbl;
+    vector<sgdisk_partition> partitions;
+    int rc = sgdisk_read(device, ptbl, partitions);
+    if (rc == 0) {
+        stringstream res;
+        switch (ptbl.type) {
+        case MBR:
+            res << "DISK mbr" << endl;
+            for (auto& part : partitions) {
+                res << "PART " << part.num << " " << part.type << endl;
+            }
+            break;
+        case GPT:
+            res << "DISK gpt " << ptbl.guid << endl;
+            for (auto& part : partitions) {
+                res << "PART " << part.num << " " << part.type << " "
+                    << part.guid << " " << part.name << endl;
+            }
+            break;
+        default:
+            return 10;
+        }
+        string partStr = res.str();
+        write(STDOUT_FILENO, partStr.c_str(), partStr.length());
+    }
+    return rc;
+}
+
+extern "C" int main(int argc, char *argv[]) {
     for (int i = 0; i < argc; i++) {
         if (!strcmp("--android-dump", argv[i])) {
             return android_dump(argv[i + 1]);
diff --git a/sgdisk.h b/sgdisk.h
new file mode 100644
index 0000000..d98ee2d
--- /dev/null
+++ b/sgdisk.h
@@ -0,0 +1,43 @@
+/*
+ * Copyright (C) 2008 The Lineage Android Project
+ *
+ * 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.
+ */
+
+#ifndef __SGDISK
+#define __SGDISK
+
+#include <string>
+#include <vector>
+
+enum ptbl_type {
+    MBR,
+    GPT
+};
+
+struct sgdisk_partition_table {
+    ptbl_type   type;
+    std::string guid;
+};
+
+struct sgdisk_partition {
+    int         num;
+    std::string type;
+    std::string guid;
+    std::string name;
+};
+
+int sgdisk_read(const char* device, sgdisk_partition_table& ptbl,
+                std::vector<sgdisk_partition>& partitions);
+
+#endif