zenfone6: Sync gpt-utils with LA.UM.8.3.r1-07600-sdm845.0

Change-Id: Ie64fd5b0b270ec674e8d4d10c0ea6518abfa27b5
diff --git a/gpt-utils/gpt-utils.cpp b/gpt-utils/gpt-utils.cpp
index b368fa4..a799eb4 100644
--- a/gpt-utils/gpt-utils.cpp
+++ b/gpt-utils/gpt-utils.cpp
@@ -36,11 +36,12 @@
 #include <fcntl.h>
 #include <string.h>
 #include <errno.h>
-#include <inttypes.h>
 #include <sys/stat.h>
 #include <sys/ioctl.h>
+#ifndef _GENERIC_KERNEL_HEADERS
 #include <scsi/ufs/ioctl.h>
 #include <scsi/ufs/ufs.h>
+#endif
 #include <unistd.h>
 #include <linux/fs.h>
 #include <limits.h>
@@ -49,12 +50,18 @@
 #include <map>
 #include <vector>
 #include <string>
+#ifndef __STDC_FORMAT_MACROS
+#define __STDC_FORMAT_MACROS
+#endif
+#include <inttypes.h>
+
+
 #define LOG_TAG "gpt-utils"
 #include <log/log.h>
 #include <cutils/properties.h>
 #include "gpt-utils.h"
-#include <endian.h>
 #include <zlib.h>
+#include <endian.h>
 
 
 /******************************************************************************
@@ -146,7 +153,7 @@
     int r;
 
     if (lseek64(fd, offset, SEEK_SET) < 0) {
-        fprintf(stderr, "block dev lseek64 %" PRId64 " failed: %s\n", offset,
+        fprintf(stderr, "block dev lseek64 %" PRIi64 " failed: %s\n", offset,
                 strerror(errno));
         return -1;
     }
@@ -187,20 +194,24 @@
                                 const uint8_t *pentries_end,
                                 uint32_t pentry_size)
 {
-    char *pentry_name;
-    unsigned len = strlen(ptn_name);
+    char     *pentry_name;
+    unsigned  len = strlen(ptn_name);
+    unsigned  i;
+    char      name8[MAX_GPT_NAME_SIZE] = {0}; // initialize with null
 
     for (pentry_name = (char *) (pentries_start + PARTITION_NAME_OFFSET);
-         pentry_name < (char *) pentries_end; pentry_name += pentry_size) {
-        char name8[MAX_GPT_NAME_SIZE] = {0}; // initialize with null
-        unsigned i;
+         pentry_name < (char *) pentries_end;
+         pentry_name += pentry_size) {
 
         /* Partition names in GPT are UTF-16 - ignoring UTF-16 2nd byte */
         for (i = 0; i < sizeof(name8) / 2; i++)
             name8[i] = pentry_name[i * 2];
-        if (!strncmp(ptn_name, name8, len))
+        name8[i] = '\0';
+
+        if (!strncmp(ptn_name, name8, len)) {
             if (name8[len] == 0 || !strcmp(&name8[len], BAK_PTN_NAME_EXT))
                 return (uint8_t *) (pentry_name - PARTITION_NAME_OFFSET);
+        }
     }
 
     return NULL;
@@ -289,9 +300,12 @@
     uint8_t *gpt_header = NULL;
     uint8_t  *pentries = NULL;
     uint32_t crc;
+    uint32_t crc_zero;
     uint32_t blk_size = 0;
     int r;
 
+
+    crc_zero = crc32(0L, Z_NULL, 0);
     if (ioctl(fd, BLKSSZGET, &blk_size) != 0) {
             fprintf(stderr, "Failed to get GPT device block size: %s\n",
                             strerror(errno));
@@ -337,7 +351,7 @@
     if (r)
         goto EXIT;
 
-    crc = crc32(0, pentries, pentries_array_size);
+    crc = crc32(crc_zero, pentries, pentries_array_size);
     if (GET_4_BYTES(gpt_header + PARTITION_CRC_OFFSET) != crc) {
         fprintf(stderr, "Primary GPT partition entries array CRC invalid\n");
         r = -1;
@@ -360,12 +374,12 @@
             goto EXIT;
     }
 
-    crc = crc32(0, pentries, pentries_array_size);
+    crc = crc32(crc_zero, pentries, pentries_array_size);
     PUT_4_BYTES(gpt_header + PARTITION_CRC_OFFSET, crc);
 
     /* header CRC is calculated with this field cleared */
     PUT_4_BYTES(gpt_header + HEADER_CRC_OFFSET, 0);
-    crc = crc32(0, gpt_header, gpt_header_size);
+    crc = crc32(crc_zero, gpt_header, gpt_header_size);
     PUT_4_BYTES(gpt_header + HEADER_CRC_OFFSET, crc);
 
     /* Write the modified GPT header back to block dev */
@@ -402,10 +416,12 @@
     uint32_t gpt_header_size;
     uint8_t  *gpt_header = NULL;
     uint32_t crc;
+    uint32_t crc_zero;
     uint32_t blk_size = 0;
 
     *state = GPT_OK;
 
+    crc_zero = crc32(0L, Z_NULL, 0);
     if (ioctl(fd, BLKSSZGET, &blk_size) != 0) {
             fprintf(stderr, "Failed to get GPT device block size: %s\n",
                             strerror(errno));
@@ -437,7 +453,7 @@
     crc = GET_4_BYTES(gpt_header + HEADER_CRC_OFFSET);
     /* header CRC is calculated with this field cleared */
     PUT_4_BYTES(gpt_header + HEADER_CRC_OFFSET, 0);
-    if (crc32(0, gpt_header, gpt_header_size) != crc)
+    if (crc32(crc_zero, gpt_header, gpt_header_size) != crc)
         *state = GPT_BAD_CRC;
     free(gpt_header);
     return 0;
@@ -468,8 +484,10 @@
     uint32_t gpt_header_size;
     uint8_t  *gpt_header = NULL;
     uint32_t crc;
+    uint32_t crc_zero;
     uint32_t blk_size = 0;
 
+    crc_zero = crc32(0L, Z_NULL, 0);
     if (ioctl(fd, BLKSSZGET, &blk_size) != 0) {
             fprintf(stderr, "Failed to get GPT device block size: %s\n",
                             strerror(errno));
@@ -506,7 +524,7 @@
 
     /* header CRC is calculated with this field cleared */
     PUT_4_BYTES(gpt_header + HEADER_CRC_OFFSET, 0);
-    crc = crc32(0, gpt_header, gpt_header_size);
+    crc = crc32(crc_zero, gpt_header, gpt_header_size);
     PUT_4_BYTES(gpt_header + HEADER_CRC_OFFSET, crc);
 
     if (blk_rw(fd, 1, gpt_header_offset, gpt_header, blk_size)) {
@@ -597,6 +615,7 @@
 
 int set_boot_lun(char *sg_dev, uint8_t boot_lun_id)
 {
+#ifndef _GENERIC_KERNEL_HEADERS
         int fd = -1;
         int rc;
         struct ufs_ioctl_query_data *data = NULL;
@@ -637,6 +656,9 @@
         if (data)
                 free(data);
         return -1;
+#else
+	return 0;
+#endif
 }
 
 //Swtich betwieen using either the primary or the backup
@@ -1367,10 +1389,13 @@
 //disk represented by path dev. Returns 0 on success and -1 on error.
 int gpt_disk_get_disk_info(const char *dev, struct gpt_disk *dsk)
 {
-        struct gpt_disk *disk = NULL;
-        int fd = -1;
-        uint32_t gpt_header_size = 0;
 
+	struct gpt_disk *disk = NULL;
+	int fd = -1;
+	uint32_t gpt_header_size = 0;
+	uint32_t crc_zero;
+
+	crc_zero = crc32(0L, Z_NULL, 0);
         if (!dsk || !dev) {
                 ALOGE("%s: Invalid arguments", __func__);
                 goto error;
@@ -1382,13 +1407,13 @@
                 goto error;
         }
         gpt_header_size = GET_4_BYTES(disk->hdr + HEADER_SIZE_OFFSET);
-        disk->hdr_crc = crc32(0, disk->hdr, gpt_header_size);
+        disk->hdr_crc = crc32(crc_zero, disk->hdr, gpt_header_size);
         disk->hdr_bak = gpt_get_header(dev, SECONDARY_GPT);
         if (!disk->hdr_bak) {
                 ALOGE("%s: Failed to get backup header", __func__);
                 goto error;
         }
-        disk->hdr_bak_crc = crc32(0, disk->hdr_bak, gpt_header_size);
+        disk->hdr_bak_crc = crc32(crc_zero, disk->hdr_bak, gpt_header_size);
 
         //Descriptor for the block device. We will use this for further
         //modifications to the partition table
@@ -1463,16 +1488,18 @@
 int gpt_disk_update_crc(struct gpt_disk *disk)
 {
         uint32_t gpt_header_size = 0;
+        uint32_t crc_zero;
+        crc_zero = crc32(0L, Z_NULL, 0);
         if (!disk || (disk->is_initialized != GPT_DISK_INIT_MAGIC)) {
                 ALOGE("%s: invalid argument", __func__);
                 goto error;
         }
         //Recalculate the CRC of the primary partiton array
-        disk->pentry_arr_crc = crc32(0,
+        disk->pentry_arr_crc = crc32(crc_zero,
                         disk->pentry_arr,
                         disk->pentry_arr_size);
         //Recalculate the CRC of the backup partition array
-        disk->pentry_arr_bak_crc = crc32(0,
+        disk->pentry_arr_bak_crc = crc32(crc_zero,
                         disk->pentry_arr_bak,
                         disk->pentry_arr_size);
         //Update the partition CRC value in the primary GPT header
@@ -1485,8 +1512,8 @@
         //Header CRC is calculated with its own CRC field set to 0
         PUT_4_BYTES(disk->hdr + HEADER_CRC_OFFSET, 0);
         PUT_4_BYTES(disk->hdr_bak + HEADER_CRC_OFFSET, 0);
-        disk->hdr_crc = crc32(0, disk->hdr, gpt_header_size);
-        disk->hdr_bak_crc = crc32(0, disk->hdr_bak, gpt_header_size);
+        disk->hdr_crc = crc32(crc_zero, disk->hdr, gpt_header_size);
+        disk->hdr_bak_crc = crc32(crc_zero, disk->hdr_bak, gpt_header_size);
         PUT_4_BYTES(disk->hdr + HEADER_CRC_OFFSET, disk->hdr_crc);
         PUT_4_BYTES(disk->hdr_bak + HEADER_CRC_OFFSET, disk->hdr_bak_crc);
         return 0;