From 91a40a62889b8434faee012dedaa5b00d03e7d7c Mon Sep 17 00:00:00 2001 From: LuK1337 Date: Mon, 6 Apr 2020 12:45:17 +0200 Subject: [PATCH] sdm845-common: Sync gpt-utils with LA.UM.8.3.r1-07600-sdm845.0 Change-Id: Ie64fd5b0b270ec674e8d4d10c0ea6518abfa27b5 --- gpt-utils/gpt-utils.cpp | 73 ++++++++++++++++++++++++++++------------- gpt-utils/gpt-utils.h | 4 +-- 2 files changed, 52 insertions(+), 25 deletions(-) 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 #include #include -#include #include #include +#ifndef _GENERIC_KERNEL_HEADERS #include #include +#endif #include #include #include @@ -49,12 +50,18 @@ #include #include #include +#ifndef __STDC_FORMAT_MACROS +#define __STDC_FORMAT_MACROS +#endif +#include + + #define LOG_TAG "gpt-utils" #include #include #include "gpt-utils.h" -#include #include +#include /****************************************************************************** @@ -146,7 +153,7 @@ static int blk_rw(int fd, int rw, int64_t offset, uint8_t *buf, unsigned len) 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 @@ static uint8_t *gpt_pentry_seek(const char *ptn_name, 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 @@ static int gpt2_set_boot_chain(int fd, enum boot_chain boot) 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 @@ static int gpt2_set_boot_chain(int fd, enum boot_chain boot) 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 @@ static int gpt2_set_boot_chain(int fd, enum boot_chain boot) 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 @@ static int gpt_get_state(int fd, enum gpt_instance gpt, enum gpt_state *state) 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 @@ static int gpt_get_state(int fd, enum gpt_instance gpt, enum gpt_state *state) 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 @@ static int gpt_set_state(int fd, enum gpt_instance gpt, enum gpt_state state) 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 @@ static int gpt_set_state(int fd, enum gpt_instance gpt, enum gpt_state state) /* 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 @@ error: 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 @@ error: if (data) free(data); return -1; +#else + return 0; +#endif } //Swtich betwieen using either the primary or the backup @@ -1367,10 +1389,13 @@ void gpt_disk_free(struct gpt_disk *disk) //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 @@ int gpt_disk_get_disk_info(const char *dev, struct gpt_disk *dsk) 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 @@ error: 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 @@ int gpt_disk_update_crc(struct gpt_disk *disk) //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; diff --git a/gpt-utils/gpt-utils.h b/gpt-utils/gpt-utils.h index a32ae88..58b40be 100644 --- a/gpt-utils/gpt-utils.h +++ b/gpt-utils/gpt-utils.h @@ -80,8 +80,8 @@ extern "C" { #define AB_SLOT_B_SUFFIX "_b" #define PTN_XBL "xbl" #define PTN_XBL_CFG "xbl_config" -#define PTN_SWAP_LIST PTN_XBL, PTN_XBL_CFG, "sbl1", "rpm", "tz", "aboot", "abl", "hyp", "lksecapp", "keymaster", "cmnlib", "cmnlib32", "cmnlib64", "pmic", "apdp", "devcfg", "hosd", "keystore", "msadp", "mdtp", "mdtpsecapp", "dsp", "aop", "qupfw", "vbmeta", "dtbo", "ImageFv" -#define AB_PTN_LIST PTN_SWAP_LIST, "boot", "system", "vendor", "modem", "bluetooth" +#define PTN_SWAP_LIST PTN_XBL, PTN_XBL_CFG, "sbl1", "rpm", "tz", "aboot", "abl", "hyp", "lksecapp", "keymaster", "cmnlib", "cmnlib32", "cmnlib64", "pmic", "apdp", "devcfg", "hosd", "keystore", "msadp", "mdtp", "mdtpsecapp", "dsp", "aop", "qupfw", "vbmeta", "dtbo", "imagefv", "ImageFv", "multiimgoem", "multiimgqti", "uefisecapp" +#define AB_PTN_LIST PTN_SWAP_LIST, "boot", "system", "vendor", "odm", "modem", "bluetooth" #define BOOT_DEV_DIR "/dev/block/bootdevice/by-name" /******************************************************************************