2021-10-10 06:16:20 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2013, The Linux Foundation. All rights reserved.
|
2022-06-04 22:51:22 +08:00
|
|
|
* Copyright (C) 2021-2022 Caleb Connolly <caleb@connolly.tech>
|
2021-10-10 06:16:20 +08:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are
|
|
|
|
* met:
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above
|
|
|
|
* copyright notice, this list of conditions and the following
|
|
|
|
* disclaimer in the documentation and/or other materials provided
|
|
|
|
* with the distribution.
|
|
|
|
* * Neither the name of The Linux Foundation nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
|
|
|
|
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
|
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
|
|
|
|
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
|
|
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
|
|
|
|
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
|
|
|
|
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define _LARGEFILE64_SOURCE /* enable lseek64() */
|
|
|
|
|
2023-06-24 06:21:24 +08:00
|
|
|
#include <assert.h>
|
2023-06-24 03:16:24 +08:00
|
|
|
#include <asm/byteorder.h>
|
2023-06-24 06:21:24 +08:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdlib.h>
|
2021-10-10 06:16:20 +08:00
|
|
|
#include <dirent.h>
|
2023-06-24 03:16:24 +08:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2021-10-10 06:16:20 +08:00
|
|
|
#include <inttypes.h>
|
2023-06-24 03:16:24 +08:00
|
|
|
#include <limits.h>
|
|
|
|
#include <linux/fs.h>
|
2021-10-10 06:16:20 +08:00
|
|
|
#include <linux/kernel.h>
|
2023-06-24 03:16:24 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
2021-10-10 06:16:20 +08:00
|
|
|
|
|
|
|
#include "gpt-utils.h"
|
2023-06-24 03:16:24 +08:00
|
|
|
#include "utils.h"
|
2023-06-24 07:59:09 +08:00
|
|
|
#include "crc32.h"
|
2021-10-10 06:16:20 +08:00
|
|
|
|
|
|
|
/* list the names of the backed-up partitions to be swapped */
|
|
|
|
/* extension used for the backup partitions - tzbak, abootbak, etc. */
|
2022-06-04 22:51:22 +08:00
|
|
|
#define BAK_PTN_NAME_EXT "bak"
|
|
|
|
#define XBL_PRIMARY "/dev/disk/by-partlabel/xbl_a" // FIXME
|
|
|
|
#define XBL_BACKUP "/dev/disk/by-partlabel/xblbak"
|
|
|
|
#define XBL_AB_PRIMARY "/dev/disk/by-partlabel/xbl_a"
|
|
|
|
#define XBL_AB_SECONDARY "/dev/disk/by-partlabel/xbl_b"
|
2021-10-10 06:16:20 +08:00
|
|
|
/* GPT defines */
|
2022-06-04 22:51:22 +08:00
|
|
|
#define MAX_LUNS 26
|
2023-06-24 03:16:24 +08:00
|
|
|
// Size of the buffer that needs to be passed to the UFS ioctl
|
2022-06-04 22:51:22 +08:00
|
|
|
#define UFS_ATTR_DATA_SIZE 32
|
2023-06-24 03:16:24 +08:00
|
|
|
// This will allow us to get the root lun path from the path to the partition.
|
|
|
|
// i.e: from /dev/disk/sdaXXX get /dev/disk/sda. The assumption here is that
|
|
|
|
// the boot critical luns lie between sda to sdz which is acceptable because
|
|
|
|
// only user added external disks,etc would lie beyond that limit which do not
|
|
|
|
// contain partitions that interest us here.
|
2021-10-10 09:30:13 +08:00
|
|
|
#define PATH_TRUNCATE_LOC (sizeof("/dev/sda") - 1)
|
2021-10-10 06:16:20 +08:00
|
|
|
|
2023-06-24 03:16:24 +08:00
|
|
|
// From /dev/disk/sda get just sda
|
2021-10-10 09:30:13 +08:00
|
|
|
#define LUN_NAME_START_LOC (sizeof("/dev/") - 1)
|
2022-06-04 22:51:22 +08:00
|
|
|
#define BOOT_LUN_A_ID 1
|
|
|
|
#define BOOT_LUN_B_ID 2
|
2021-10-10 06:16:20 +08:00
|
|
|
|
2023-06-24 03:16:24 +08:00
|
|
|
#define GET_4_BYTES(ptr) \
|
|
|
|
((uint32_t) * ((uint8_t *)(ptr)) | ((uint32_t) * ((uint8_t *)(ptr) + 1) << 8) | \
|
|
|
|
((uint32_t) * ((uint8_t *)(ptr) + 2) << 16) | ((uint32_t) * ((uint8_t *)(ptr) + 3) << 24))
|
|
|
|
|
|
|
|
#define GET_8_BYTES(ptr) \
|
|
|
|
((uint64_t) * ((uint8_t *)(ptr)) | ((uint64_t) * ((uint8_t *)(ptr) + 1) << 8) | \
|
|
|
|
((uint64_t) * ((uint8_t *)(ptr) + 2) << 16) | \
|
|
|
|
((uint64_t) * ((uint8_t *)(ptr) + 3) << 24) | \
|
|
|
|
((uint64_t) * ((uint8_t *)(ptr) + 4) << 32) | \
|
|
|
|
((uint64_t) * ((uint8_t *)(ptr) + 5) << 40) | \
|
|
|
|
((uint64_t) * ((uint8_t *)(ptr) + 6) << 48) | ((uint64_t) * ((uint8_t *)(ptr) + 7) << 56))
|
|
|
|
|
|
|
|
#define PUT_4_BYTES(ptr, y) \
|
|
|
|
*((uint8_t *)(ptr)) = (y)&0xff; \
|
|
|
|
*((uint8_t *)(ptr) + 1) = ((y) >> 8) & 0xff; \
|
|
|
|
*((uint8_t *)(ptr) + 2) = ((y) >> 16) & 0xff; \
|
2022-06-04 22:51:22 +08:00
|
|
|
*((uint8_t *)(ptr) + 3) = ((y) >> 24) & 0xff;
|
2021-10-10 06:16:20 +08:00
|
|
|
|
2022-06-04 22:51:22 +08:00
|
|
|
enum gpt_state { GPT_OK = 0, GPT_BAD_SIGNATURE, GPT_BAD_CRC };
|
2023-06-24 03:16:24 +08:00
|
|
|
// List of LUN's containing boot critical images.
|
|
|
|
// Required in the case of UFS devices
|
2021-10-10 06:16:20 +08:00
|
|
|
struct update_data {
|
2023-06-24 07:59:09 +08:00
|
|
|
char lun_list[MAX_LUNS][GPT_PTN_PATH_MAX];
|
2022-06-04 22:51:22 +08:00
|
|
|
uint32_t num_valid_entries;
|
2021-10-10 06:16:20 +08:00
|
|
|
};
|
|
|
|
|
2022-06-04 22:51:22 +08:00
|
|
|
void DumpHex(const void *data, size_t size)
|
|
|
|
{
|
2021-10-10 09:30:13 +08:00
|
|
|
char ascii[17];
|
|
|
|
size_t i, j;
|
|
|
|
ascii[16] = '\0';
|
|
|
|
for (i = 0; i < size; ++i) {
|
2022-06-04 22:51:22 +08:00
|
|
|
printf("%02X ", ((unsigned char *)data)[i]);
|
2023-06-24 03:16:24 +08:00
|
|
|
if (((unsigned char *)data)[i] >= ' ' && ((unsigned char *)data)[i] <= '~') {
|
2022-06-04 22:51:22 +08:00
|
|
|
ascii[i % 16] = ((unsigned char *)data)[i];
|
2021-10-10 09:30:13 +08:00
|
|
|
} else {
|
|
|
|
ascii[i % 16] = '.';
|
|
|
|
}
|
2022-06-04 22:51:22 +08:00
|
|
|
if ((i + 1) % 8 == 0 || i + 1 == size) {
|
2021-10-10 09:30:13 +08:00
|
|
|
printf(" ");
|
2022-06-04 22:51:22 +08:00
|
|
|
if ((i + 1) % 16 == 0) {
|
2021-10-10 09:30:13 +08:00
|
|
|
printf("| %s \n", ascii);
|
2022-06-04 22:51:22 +08:00
|
|
|
} else if (i + 1 == size) {
|
|
|
|
ascii[(i + 1) % 16] = '\0';
|
|
|
|
if ((i + 1) % 16 <= 8) {
|
2021-10-10 09:30:13 +08:00
|
|
|
printf(" ");
|
|
|
|
}
|
2022-06-04 22:51:22 +08:00
|
|
|
for (j = (i + 1) % 16; j < 16; ++j) {
|
2021-10-10 09:30:13 +08:00
|
|
|
printf(" ");
|
|
|
|
}
|
|
|
|
printf("| %s \n", ascii);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-10 06:16:20 +08:00
|
|
|
/**
|
|
|
|
* ==========================================================================
|
|
|
|
*
|
|
|
|
* \brief Read/Write len bytes from/to block dev
|
|
|
|
*
|
|
|
|
* \param [in] fd block dev file descriptor (returned from open)
|
|
|
|
* \param [in] rw RW flag: 0 - read, != 0; - write
|
|
|
|
* \param [in] offset block dev offset [bytes] - RW start position
|
|
|
|
* \param [in] buf Pointer to the buffer containing the data
|
|
|
|
* \param [in] len RW size in bytes. Buf must be at least that big
|
|
|
|
*
|
|
|
|
* \return 0 on success
|
|
|
|
*
|
|
|
|
* ==========================================================================
|
|
|
|
*/
|
|
|
|
static int blk_rw(int fd, int rw, uint64_t offset, uint8_t *buf, unsigned len)
|
|
|
|
{
|
2022-06-04 22:51:22 +08:00
|
|
|
int r;
|
2021-10-10 06:16:20 +08:00
|
|
|
|
2022-06-04 22:51:22 +08:00
|
|
|
if (lseek64(fd, offset, SEEK_SET) < 0) {
|
2023-06-24 03:16:24 +08:00
|
|
|
fprintf(stderr, "block dev lseek64 %" PRIu64 " failed: %s\n", offset,
|
|
|
|
strerror(errno));
|
2022-06-04 22:51:22 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2021-10-10 06:16:20 +08:00
|
|
|
|
2022-06-04 22:51:22 +08:00
|
|
|
if (rw)
|
|
|
|
r = write(fd, buf, len);
|
|
|
|
else
|
|
|
|
r = read(fd, buf, len);
|
|
|
|
|
|
|
|
if (r < 0) {
|
2023-06-24 03:16:24 +08:00
|
|
|
fprintf(stderr, "block dev %s failed: %s\n", rw ? "write" : "read\n",
|
|
|
|
strerror(errno));
|
2022-06-04 22:51:22 +08:00
|
|
|
} else {
|
|
|
|
if (rw) {
|
|
|
|
r = fsync(fd);
|
|
|
|
if (r < 0)
|
2023-06-24 03:16:24 +08:00
|
|
|
fprintf(stderr, "fsync failed: %s\n", strerror(errno));
|
2022-06-04 22:51:22 +08:00
|
|
|
} else {
|
|
|
|
r = 0;
|
|
|
|
}
|
|
|
|
}
|
2021-10-10 06:16:20 +08:00
|
|
|
|
2022-06-04 22:51:22 +08:00
|
|
|
return r;
|
2021-10-10 06:16:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ==========================================================================
|
|
|
|
*
|
|
|
|
* \brief Search within GPT for partition entry with the given name
|
|
|
|
* or it's backup twin (name-bak).
|
|
|
|
*
|
|
|
|
* \param [in] ptn_name Partition name to seek
|
|
|
|
* \param [in] pentries_start Partition entries array start pointer
|
|
|
|
* \param [in] pentries_end Partition entries array end pointer
|
|
|
|
* \param [in] pentry_size Single partition entry size [bytes]
|
|
|
|
*
|
2023-06-24 03:16:24 +08:00
|
|
|
* \return First partition entry pointer that matches the name or null
|
2021-10-10 06:16:20 +08:00
|
|
|
*
|
|
|
|
* ==========================================================================
|
|
|
|
*/
|
2023-06-24 03:16:24 +08:00
|
|
|
static uint8_t *gpt_pentry_seek(const char *ptn_name, const uint8_t *pentries_start,
|
|
|
|
const uint8_t *pentries_end, uint32_t pentry_size)
|
2021-10-10 06:16:20 +08:00
|
|
|
{
|
2022-06-04 22:51:22 +08:00
|
|
|
char *pentry_name;
|
|
|
|
unsigned len = strlen(ptn_name);
|
|
|
|
|
|
|
|
for (pentry_name = (char *)(pentries_start + PARTITION_NAME_OFFSET);
|
|
|
|
pentry_name < (char *)pentries_end; pentry_name += pentry_size) {
|
|
|
|
char name8[MAX_GPT_NAME_SIZE / 2];
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
/* Partition names in GPT are UTF-16 - ignoring UTF-16 2nd byte */
|
|
|
|
for (i = 0; i < sizeof(name8); i++)
|
|
|
|
name8[i] = pentry_name[i * 2];
|
|
|
|
if (!strncmp(ptn_name, name8, len))
|
2023-06-24 03:16:24 +08:00
|
|
|
if (name8[len] == 0 || !strcmp(&name8[len], BAK_PTN_NAME_EXT))
|
|
|
|
return (uint8_t *)(pentry_name - PARTITION_NAME_OFFSET);
|
2022-06-04 22:51:22 +08:00
|
|
|
}
|
2021-10-10 06:16:20 +08:00
|
|
|
|
2023-06-24 06:21:24 +08:00
|
|
|
return NULL;
|
2021-10-10 06:16:20 +08:00
|
|
|
}
|
|
|
|
|
2022-06-04 11:42:40 +08:00
|
|
|
// Defined in ufs-bsg.cpp
|
|
|
|
int32_t set_boot_lun(uint8_t lun_id);
|
2021-10-10 06:16:20 +08:00
|
|
|
|
2023-09-28 01:56:43 +08:00
|
|
|
// Switch between using either the primary or the backup
|
2023-06-24 03:16:24 +08:00
|
|
|
// boot LUN for boot. This is required since UFS boot partitions
|
|
|
|
// cannot have a backup GPT which is what we use for failsafe
|
|
|
|
// updates of the other 'critical' partitions. This function will
|
|
|
|
// not be invoked for emmc targets and on UFS targets is only required
|
|
|
|
// to be invoked for XBL.
|
2021-10-10 06:16:20 +08:00
|
|
|
//
|
2023-06-24 03:16:24 +08:00
|
|
|
// The algorithm to do this is as follows:
|
2021-10-10 06:16:20 +08:00
|
|
|
//- Find the real block device(eg: /dev/disk/sdb) that corresponds
|
|
|
|
// to the /dev/disk/bootdevice/by-name/xbl(bak) symlink
|
|
|
|
//
|
|
|
|
//- Once we have the block device 'node' name(sdb in the above example)
|
|
|
|
// use this node to to locate the scsi generic device that represents
|
|
|
|
// it by checking the file /sys/block/sdb/device/scsi_generic/sgY
|
|
|
|
//
|
|
|
|
//- Once we locate sgY we call the query ioctl on /dev/sgy to switch
|
2023-06-24 03:16:24 +08:00
|
|
|
// the boot lun to either LUNA or LUNB
|
2021-10-10 06:16:20 +08:00
|
|
|
int gpt_utils_set_xbl_boot_partition(enum boot_chain chain)
|
|
|
|
{
|
2022-06-04 22:51:22 +08:00
|
|
|
struct stat st;
|
|
|
|
uint8_t boot_lun_id = 0;
|
2023-06-24 06:21:24 +08:00
|
|
|
const char *boot_dev = NULL;
|
2023-09-28 01:56:43 +08:00
|
|
|
int ret = -1;
|
2022-06-04 22:51:22 +08:00
|
|
|
|
|
|
|
(void)st;
|
|
|
|
(void)boot_dev;
|
|
|
|
|
|
|
|
if (chain == BACKUP_BOOT) {
|
|
|
|
boot_lun_id = BOOT_LUN_B_ID;
|
|
|
|
if (!stat(XBL_BACKUP, &st))
|
|
|
|
boot_dev = XBL_BACKUP;
|
|
|
|
else if (!stat(XBL_AB_SECONDARY, &st))
|
|
|
|
boot_dev = XBL_AB_SECONDARY;
|
|
|
|
else {
|
2023-06-24 03:16:24 +08:00
|
|
|
fprintf(stderr, "%s: Failed to locate secondary xbl\n", __func__);
|
2023-06-23 20:30:08 +08:00
|
|
|
goto error;
|
2022-06-04 22:51:22 +08:00
|
|
|
}
|
|
|
|
} else if (chain == NORMAL_BOOT) {
|
|
|
|
boot_lun_id = BOOT_LUN_A_ID;
|
|
|
|
if (!stat(XBL_PRIMARY, &st))
|
|
|
|
boot_dev = XBL_PRIMARY;
|
|
|
|
else if (!stat(XBL_AB_PRIMARY, &st))
|
|
|
|
boot_dev = XBL_AB_PRIMARY;
|
|
|
|
else {
|
2023-06-24 03:16:24 +08:00
|
|
|
fprintf(stderr, "%s: Failed to locate primary xbl\n", __func__);
|
2023-06-23 20:30:08 +08:00
|
|
|
goto error;
|
2022-06-04 22:51:22 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "%s: Invalid boot chain id\n", __func__);
|
2023-06-23 20:30:08 +08:00
|
|
|
goto error;
|
2022-06-04 22:51:22 +08:00
|
|
|
}
|
2023-06-24 03:16:24 +08:00
|
|
|
// We need either both xbl and xblbak or both xbl_a and xbl_b to exist at
|
|
|
|
// the same time. If not the current configuration is invalid.
|
2022-06-04 22:51:22 +08:00
|
|
|
if ((stat(XBL_PRIMARY, &st) || stat(XBL_BACKUP, &st)) &&
|
|
|
|
(stat(XBL_AB_PRIMARY, &st) || stat(XBL_AB_SECONDARY, &st))) {
|
2023-06-24 03:16:24 +08:00
|
|
|
fprintf(stderr, "%s:primary/secondary XBL prt not found(%s)\n", __func__,
|
|
|
|
strerror(errno));
|
2023-06-23 20:30:08 +08:00
|
|
|
goto error;
|
2022-06-04 22:51:22 +08:00
|
|
|
}
|
|
|
|
LOGD("%s: setting %s lun as boot lun\n", __func__, boot_dev);
|
2022-06-04 11:42:40 +08:00
|
|
|
|
2023-06-23 20:30:08 +08:00
|
|
|
if (set_boot_lun(boot_lun_id)) {
|
2023-09-28 01:56:43 +08:00
|
|
|
ret = -ENODEV;
|
2023-06-23 20:30:08 +08:00
|
|
|
goto error;
|
|
|
|
}
|
2022-06-04 22:51:22 +08:00
|
|
|
return 0;
|
2023-06-23 20:30:08 +08:00
|
|
|
error:
|
2023-09-28 01:56:43 +08:00
|
|
|
return ret;
|
2021-10-10 06:16:20 +08:00
|
|
|
}
|
|
|
|
|
2023-06-24 03:16:24 +08:00
|
|
|
// Given a parttion name(eg: rpm) get the path to the block device that
|
|
|
|
// represents the GPT disk the partition resides on. In the case of emmc it
|
|
|
|
// would be the default emmc dev(/dev/mmcblk0). In the case of UFS we look
|
|
|
|
// through the /dev/disk/bootdevice/by-name/ tree for partname, and resolve
|
|
|
|
// the path to the LUN from there.
|
|
|
|
static int get_dev_path_from_partition_name(const char *partname, char *buf, size_t buflen)
|
2021-10-10 06:16:20 +08:00
|
|
|
{
|
2023-06-24 07:59:09 +08:00
|
|
|
char path[GPT_PTN_PATH_MAX] = { 0 };
|
2022-08-30 05:50:57 +08:00
|
|
|
int i;
|
2022-06-04 11:42:40 +08:00
|
|
|
|
2022-06-04 22:51:22 +08:00
|
|
|
if (!partname || !buf || buflen < ((PATH_TRUNCATE_LOC) + 1)) {
|
|
|
|
fprintf(stderr, "%s: Invalid argument\n", __func__);
|
|
|
|
return -1;
|
|
|
|
}
|
2023-06-24 03:16:24 +08:00
|
|
|
|
|
|
|
// Need to find the lun that holds partition partname
|
2022-06-04 22:51:22 +08:00
|
|
|
snprintf(path, sizeof(path), "%s/%s", BOOT_DEV_DIR, partname);
|
2023-06-24 03:16:24 +08:00
|
|
|
|
|
|
|
|
2022-06-04 22:51:22 +08:00
|
|
|
buf = realpath(path, buf);
|
|
|
|
if (!buf) {
|
|
|
|
return -1;
|
|
|
|
} else {
|
2022-08-30 05:50:57 +08:00
|
|
|
for (i = strlen(buf); i > 0; i--)
|
|
|
|
if (!isdigit(buf[i - 1]))
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (i >= 2 && buf[i - 1] == 'p' && isdigit(buf[i - 2]))
|
|
|
|
i--;
|
|
|
|
|
|
|
|
buf[i] = 0;
|
2022-06-04 22:51:22 +08:00
|
|
|
}
|
2023-06-24 03:16:24 +08:00
|
|
|
|
2022-06-04 22:51:22 +08:00
|
|
|
return 0;
|
2021-10-10 06:16:20 +08:00
|
|
|
}
|
|
|
|
|
2023-06-24 03:16:24 +08:00
|
|
|
// Get the block size of the disk represented by decsriptor fd
|
2021-10-10 06:16:20 +08:00
|
|
|
static uint32_t gpt_get_block_size(int fd)
|
|
|
|
{
|
2022-06-04 22:51:22 +08:00
|
|
|
uint32_t block_size = 0;
|
2023-06-24 03:16:24 +08:00
|
|
|
|
2022-06-04 22:51:22 +08:00
|
|
|
if (fd < 0) {
|
|
|
|
fprintf(stderr, "%s: invalid descriptor\n", __func__);
|
2023-06-23 20:30:08 +08:00
|
|
|
goto error;
|
2022-06-04 22:51:22 +08:00
|
|
|
}
|
2023-06-24 03:16:24 +08:00
|
|
|
|
2022-06-04 22:51:22 +08:00
|
|
|
if (ioctl(fd, BLKSSZGET, &block_size) != 0) {
|
2023-06-24 03:16:24 +08:00
|
|
|
fprintf(stderr, "%s: Failed to get GPT dev block size : %s\n", __func__,
|
|
|
|
strerror(errno));
|
2023-06-23 20:30:08 +08:00
|
|
|
goto error;
|
2022-06-04 22:51:22 +08:00
|
|
|
}
|
2023-06-24 03:16:24 +08:00
|
|
|
|
2022-06-04 22:51:22 +08:00
|
|
|
return block_size;
|
2023-06-23 20:30:08 +08:00
|
|
|
error:
|
|
|
|
return 0;
|
2021-10-10 06:16:20 +08:00
|
|
|
}
|
|
|
|
|
2023-06-24 03:16:24 +08:00
|
|
|
// Write the GPT header present in the passed in buffer back to the
|
|
|
|
// disk represented by fd
|
|
|
|
static int gpt_set_header(uint8_t *gpt_header, int fd, enum gpt_instance instance)
|
2021-10-10 06:16:20 +08:00
|
|
|
{
|
2022-06-04 22:51:22 +08:00
|
|
|
uint32_t block_size = 0;
|
|
|
|
off_t gpt_header_offset = 0;
|
2023-06-24 03:16:24 +08:00
|
|
|
|
2022-06-04 22:51:22 +08:00
|
|
|
if (!gpt_header || fd < 0) {
|
|
|
|
fprintf(stderr, "%s: Invalid arguments\n", __func__);
|
2023-06-23 20:30:08 +08:00
|
|
|
goto error;
|
2022-06-04 22:51:22 +08:00
|
|
|
}
|
2023-06-24 03:16:24 +08:00
|
|
|
|
2022-06-04 22:51:22 +08:00
|
|
|
block_size = gpt_get_block_size(fd);
|
|
|
|
LOGD("%s: Block size is : %d\n", __func__, block_size);
|
|
|
|
if (block_size == 0) {
|
|
|
|
fprintf(stderr, "%s: Failed to get block size\n", __func__);
|
2023-06-23 20:30:08 +08:00
|
|
|
goto error;
|
2022-06-04 22:51:22 +08:00
|
|
|
}
|
2023-06-24 03:16:24 +08:00
|
|
|
|
2022-06-04 22:51:22 +08:00
|
|
|
if (instance == PRIMARY_GPT)
|
|
|
|
gpt_header_offset = block_size;
|
|
|
|
else
|
|
|
|
gpt_header_offset = lseek64(fd, 0, SEEK_END) - block_size;
|
|
|
|
if (gpt_header_offset <= 0) {
|
2023-06-24 03:16:24 +08:00
|
|
|
fprintf(stderr, "%s: Failed to get gpt header offset\n", __func__);
|
2023-06-23 20:30:08 +08:00
|
|
|
goto error;
|
2022-06-04 22:51:22 +08:00
|
|
|
}
|
2023-06-24 03:16:24 +08:00
|
|
|
|
|
|
|
LOGD("%s: Writing back header to offset %ld\n", __func__, gpt_header_offset);
|
2022-06-04 22:51:22 +08:00
|
|
|
if (blk_rw(fd, 1, gpt_header_offset, gpt_header, block_size)) {
|
2023-06-24 03:16:24 +08:00
|
|
|
fprintf(stderr, "%s: Failed to write back GPT header\n", __func__);
|
2023-06-23 20:30:08 +08:00
|
|
|
goto error;
|
2022-06-04 22:51:22 +08:00
|
|
|
}
|
2023-06-24 03:16:24 +08:00
|
|
|
|
2022-06-04 22:51:22 +08:00
|
|
|
return 0;
|
2023-06-24 03:16:24 +08:00
|
|
|
|
2023-06-23 20:30:08 +08:00
|
|
|
error:
|
|
|
|
return -1;
|
2021-10-10 06:16:20 +08:00
|
|
|
}
|
|
|
|
|
2023-06-24 03:16:24 +08:00
|
|
|
// Read out the GPT headers for the disk that contains the partition partname
|
|
|
|
static int gpt_get_headers(const char *partname, uint8_t **primary, uint8_t **backup)
|
2021-10-10 06:16:20 +08:00
|
|
|
{
|
2023-06-24 06:21:24 +08:00
|
|
|
uint8_t *hdr = NULL;
|
2023-06-24 07:59:09 +08:00
|
|
|
char devpath[GPT_PTN_PATH_MAX] = { 0 };
|
2023-05-08 23:42:21 +08:00
|
|
|
off_t hdr_offset = 0;
|
2022-06-04 22:51:22 +08:00
|
|
|
uint32_t block_size = 0;
|
2023-06-24 03:16:24 +08:00
|
|
|
int instance;
|
2023-06-23 20:30:08 +08:00
|
|
|
int fd = -1;
|
2023-06-24 03:16:24 +08:00
|
|
|
|
2022-06-04 22:51:22 +08:00
|
|
|
if (!partname) {
|
|
|
|
fprintf(stderr, "%s: Invalid partition name\n", __func__);
|
2023-06-23 20:30:08 +08:00
|
|
|
goto error;
|
2022-06-04 22:51:22 +08:00
|
|
|
}
|
2023-06-24 03:16:24 +08:00
|
|
|
|
|
|
|
if (get_dev_path_from_partition_name(partname, devpath, sizeof(devpath)) != 0) {
|
|
|
|
fprintf(stderr, "%s: Failed to resolve path for %s\n", __func__, partname);
|
2023-06-23 20:30:08 +08:00
|
|
|
goto error;
|
2022-06-04 22:51:22 +08:00
|
|
|
}
|
2023-06-24 03:16:24 +08:00
|
|
|
|
2023-06-23 20:30:08 +08:00
|
|
|
fd = open(devpath, O_RDWR);
|
|
|
|
if (fd < 0) {
|
2023-06-24 03:16:24 +08:00
|
|
|
fprintf(stderr, "%s: Failed to open %s : %s\n", __func__, devpath, strerror(errno));
|
|
|
|
return -1;
|
2022-06-04 22:51:22 +08:00
|
|
|
}
|
2023-06-24 03:16:24 +08:00
|
|
|
|
2023-06-23 20:30:08 +08:00
|
|
|
block_size = gpt_get_block_size(fd);
|
2022-06-04 22:51:22 +08:00
|
|
|
if (block_size == 0) {
|
2023-06-24 03:16:24 +08:00
|
|
|
fprintf(stderr, "%s: Failed to get gpt block size for %s\n", __func__, partname);
|
2023-06-23 20:30:08 +08:00
|
|
|
goto error;
|
2022-06-04 22:51:22 +08:00
|
|
|
}
|
2021-10-10 06:16:20 +08:00
|
|
|
|
2023-06-24 03:16:24 +08:00
|
|
|
for (instance = PRIMARY_GPT; instance <= SECONDARY_GPT; instance++) {
|
|
|
|
hdr = (uint8_t *)calloc(block_size, 1);
|
|
|
|
if (!hdr) {
|
|
|
|
fprintf(stderr, "%s: Failed to allocate memory for gpt header\n", __func__);
|
|
|
|
}
|
|
|
|
if (instance == PRIMARY_GPT)
|
|
|
|
hdr_offset = block_size;
|
|
|
|
else {
|
|
|
|
hdr_offset = lseek64(fd, 0, SEEK_END) - block_size;
|
|
|
|
}
|
|
|
|
if (hdr_offset < 0) {
|
|
|
|
fprintf(stderr, "%s: Failed to get gpt header offset\n", __func__);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
if (blk_rw(fd, 0, hdr_offset, hdr, block_size)) {
|
|
|
|
fprintf(stderr, "%s: Failed to read GPT header from device\n", __func__);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
if (instance == PRIMARY_GPT)
|
|
|
|
*primary = hdr;
|
|
|
|
else
|
|
|
|
*backup = hdr;
|
2023-06-23 20:30:08 +08:00
|
|
|
}
|
2023-06-24 03:16:24 +08:00
|
|
|
|
2023-06-23 20:30:08 +08:00
|
|
|
close(fd);
|
2023-06-24 03:16:24 +08:00
|
|
|
return 0;
|
|
|
|
|
2023-06-23 20:30:08 +08:00
|
|
|
error:
|
2023-06-24 03:16:24 +08:00
|
|
|
close(fd);
|
2023-06-23 20:30:08 +08:00
|
|
|
if (hdr)
|
|
|
|
free(hdr);
|
2023-06-24 03:16:24 +08:00
|
|
|
return -1;
|
2021-10-10 06:16:20 +08:00
|
|
|
}
|
|
|
|
|
2023-06-24 03:16:24 +08:00
|
|
|
// Returns the partition entry array based on the
|
|
|
|
// passed in buffer which contains the gpt header.
|
|
|
|
// The fd here is the descriptor for the 'disk' which
|
|
|
|
// holds the partition
|
2023-06-23 20:30:08 +08:00
|
|
|
static uint8_t *gpt_get_pentry_arr(uint8_t *hdr, int fd)
|
2021-10-10 06:16:20 +08:00
|
|
|
{
|
2022-06-04 22:51:22 +08:00
|
|
|
uint64_t pentries_start = 0;
|
|
|
|
uint32_t pentry_size = 0;
|
|
|
|
uint32_t block_size = 0;
|
|
|
|
uint32_t pentries_arr_size = 0;
|
2023-06-24 06:21:24 +08:00
|
|
|
uint8_t *pentry_arr = NULL;
|
2022-06-04 22:51:22 +08:00
|
|
|
int rc = 0;
|
2023-06-23 20:30:08 +08:00
|
|
|
if (!hdr) {
|
|
|
|
fprintf(stderr, "%s: Invalid header\n", __func__);
|
|
|
|
goto error;
|
|
|
|
}
|
2022-06-04 22:51:22 +08:00
|
|
|
if (fd < 0) {
|
|
|
|
fprintf(stderr, "%s: Invalid fd\n", __func__);
|
2023-06-23 20:30:08 +08:00
|
|
|
goto error;
|
2022-06-04 22:51:22 +08:00
|
|
|
}
|
|
|
|
block_size = gpt_get_block_size(fd);
|
|
|
|
if (!block_size) {
|
2023-06-24 03:16:24 +08:00
|
|
|
fprintf(stderr, "%s: Failed to get gpt block size for\n", __func__);
|
2023-06-23 20:30:08 +08:00
|
|
|
goto error;
|
2022-06-04 22:51:22 +08:00
|
|
|
}
|
|
|
|
pentries_start = GET_8_BYTES(hdr + PENTRIES_OFFSET) * block_size;
|
|
|
|
pentry_size = GET_4_BYTES(hdr + PENTRY_SIZE_OFFSET);
|
2023-06-24 03:16:24 +08:00
|
|
|
pentries_arr_size = GET_4_BYTES(hdr + PARTITION_COUNT_OFFSET) * pentry_size;
|
2023-06-23 20:30:08 +08:00
|
|
|
pentry_arr = (uint8_t *)calloc(1, pentries_arr_size);
|
|
|
|
if (!pentry_arr) {
|
2023-06-24 03:16:24 +08:00
|
|
|
fprintf(stderr, "%s: Failed to allocate memory for partition array\n", __func__);
|
2023-06-23 20:30:08 +08:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
rc = blk_rw(fd, 0, pentries_start, pentry_arr, pentries_arr_size);
|
2022-06-04 22:51:22 +08:00
|
|
|
if (rc) {
|
2023-06-24 03:16:24 +08:00
|
|
|
fprintf(stderr, "%s: Failed to read partition entry array\n", __func__);
|
2023-06-23 20:30:08 +08:00
|
|
|
goto error;
|
2022-06-04 22:51:22 +08:00
|
|
|
}
|
2023-06-23 20:30:08 +08:00
|
|
|
return pentry_arr;
|
|
|
|
error:
|
|
|
|
if (pentry_arr)
|
|
|
|
free(pentry_arr);
|
2023-06-24 06:21:24 +08:00
|
|
|
return NULL;
|
2021-10-10 06:16:20 +08:00
|
|
|
}
|
|
|
|
|
2022-06-04 22:51:22 +08:00
|
|
|
static int gpt_set_pentry_arr(uint8_t *hdr, int fd, uint8_t *arr)
|
2021-10-10 06:16:20 +08:00
|
|
|
{
|
2022-06-04 22:51:22 +08:00
|
|
|
uint32_t block_size = 0;
|
|
|
|
uint64_t pentries_start = 0;
|
|
|
|
uint32_t pentry_size = 0;
|
|
|
|
uint32_t pentries_arr_size = 0;
|
|
|
|
int rc = 0;
|
|
|
|
if (!hdr || fd < 0 || !arr) {
|
|
|
|
fprintf(stderr, "%s: Invalid argument\n", __func__);
|
2023-06-23 20:30:08 +08:00
|
|
|
goto error;
|
2022-06-04 22:51:22 +08:00
|
|
|
}
|
|
|
|
block_size = gpt_get_block_size(fd);
|
|
|
|
if (!block_size) {
|
2023-06-24 03:16:24 +08:00
|
|
|
fprintf(stderr, "%s: Failed to get gpt block size for\n", __func__);
|
2023-06-23 20:30:08 +08:00
|
|
|
goto error;
|
2022-06-04 22:51:22 +08:00
|
|
|
}
|
|
|
|
LOGD("%s : Block size is %d\n", __func__, block_size);
|
|
|
|
pentries_start = GET_8_BYTES(hdr + PENTRIES_OFFSET) * block_size;
|
|
|
|
pentry_size = GET_4_BYTES(hdr + PENTRY_SIZE_OFFSET);
|
2023-06-24 03:16:24 +08:00
|
|
|
pentries_arr_size = GET_4_BYTES(hdr + PARTITION_COUNT_OFFSET) * pentry_size;
|
|
|
|
LOGD("%s: Writing partition entry array of size %d to offset %" PRIu64 "\n", __func__,
|
|
|
|
pentries_arr_size, pentries_start);
|
2022-06-04 22:51:22 +08:00
|
|
|
LOGD("pentries_start: %lu\n", pentries_start);
|
|
|
|
rc = blk_rw(fd, 1, pentries_start, arr, pentries_arr_size);
|
|
|
|
if (rc) {
|
2023-06-24 03:16:24 +08:00
|
|
|
fprintf(stderr, "%s: Failed to read partition entry array\n", __func__);
|
2023-06-23 20:30:08 +08:00
|
|
|
goto error;
|
2022-06-04 22:51:22 +08:00
|
|
|
}
|
2023-05-09 06:49:26 +08:00
|
|
|
return 0;
|
2023-06-23 20:30:08 +08:00
|
|
|
error:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2023-06-24 03:16:24 +08:00
|
|
|
/*
|
|
|
|
* Free previously allocated/initialized handle
|
|
|
|
* This function is always safe and must be called
|
|
|
|
* before discarding the handle.
|
|
|
|
* it is called automatically by gpt_disk_get_disk_info()
|
|
|
|
*/
|
2023-06-23 20:30:08 +08:00
|
|
|
void gpt_disk_free(struct gpt_disk *disk)
|
|
|
|
{
|
|
|
|
if (!disk)
|
|
|
|
return;
|
2023-06-24 03:16:24 +08:00
|
|
|
|
|
|
|
if (disk->hdr) {
|
2023-06-23 20:30:08 +08:00
|
|
|
free(disk->hdr);
|
2023-06-24 06:21:24 +08:00
|
|
|
disk->hdr = NULL;
|
2023-06-24 03:16:24 +08:00
|
|
|
}
|
|
|
|
if (disk->hdr_bak) {
|
2023-06-23 20:30:08 +08:00
|
|
|
free(disk->hdr_bak);
|
2023-06-24 06:21:24 +08:00
|
|
|
disk->hdr_bak = NULL;
|
2023-06-24 03:16:24 +08:00
|
|
|
}
|
|
|
|
if (disk->pentry_arr) {
|
2023-06-23 20:30:08 +08:00
|
|
|
free(disk->pentry_arr);
|
2023-06-24 06:21:24 +08:00
|
|
|
disk->pentry_arr = NULL;
|
2023-06-24 03:16:24 +08:00
|
|
|
}
|
|
|
|
if (disk->pentry_arr_bak) {
|
2023-06-23 20:30:08 +08:00
|
|
|
free(disk->pentry_arr_bak);
|
2023-06-24 06:21:24 +08:00
|
|
|
disk->pentry_arr_bak = NULL;
|
2023-06-24 03:16:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
disk->is_initialized = 0;
|
|
|
|
|
2023-06-23 20:30:08 +08:00
|
|
|
return;
|
2021-10-10 06:16:20 +08:00
|
|
|
}
|
|
|
|
|
2023-06-24 03:16:24 +08:00
|
|
|
bool gpt_disk_is_valid(struct gpt_disk *disk)
|
|
|
|
{
|
|
|
|
return disk->is_initialized == GPT_DISK_INIT_MAGIC;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check if a partition by-path is for the disk we have info for
|
|
|
|
* and populate the blockdev path.
|
|
|
|
* e.g. for /dev/disk/by-partlabel/system_a blockdev would be /dev/sda
|
|
|
|
*/
|
2023-06-24 06:21:24 +08:00
|
|
|
int partition_is_for_disk(const struct gpt_disk *disk, const char *part, char *blockdev, int blockdev_len)
|
2023-06-24 03:16:24 +08:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = get_dev_path_from_partition_name(part, blockdev, blockdev_len);
|
|
|
|
if (ret) {
|
|
|
|
fprintf(stderr, "%s: Failed to resolve path for %s\n", __func__, part);
|
2023-06-24 06:21:24 +08:00
|
|
|
return -1;
|
2023-06-24 03:16:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcmp(blockdev, disk->devpath)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* fills up the passed in gpt_disk struct with information about the
|
|
|
|
* 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 *disk)
|
2021-10-10 06:16:20 +08:00
|
|
|
{
|
2023-06-24 06:21:24 +08:00
|
|
|
int fd = -1, rc;
|
2022-06-04 22:51:22 +08:00
|
|
|
uint32_t gpt_header_size = 0;
|
2023-06-24 07:59:09 +08:00
|
|
|
char devpath[GPT_PTN_PATH_MAX] = { 0 };
|
2021-10-10 06:16:20 +08:00
|
|
|
|
2023-06-24 03:16:24 +08:00
|
|
|
if (!disk || !dev) {
|
2022-06-04 22:51:22 +08:00
|
|
|
fprintf(stderr, "%s: Invalid arguments\n", __func__);
|
2023-06-23 20:30:08 +08:00
|
|
|
goto error;
|
2022-06-04 22:51:22 +08:00
|
|
|
}
|
2023-06-24 03:16:24 +08:00
|
|
|
|
2023-06-24 06:21:24 +08:00
|
|
|
rc = partition_is_for_disk(disk, dev, devpath, sizeof(devpath));
|
|
|
|
|
|
|
|
if (rc > 0)
|
2023-06-24 03:16:24 +08:00
|
|
|
return 0;
|
2023-06-24 06:21:24 +08:00
|
|
|
|
|
|
|
if (rc < 0) {
|
|
|
|
fprintf(stderr, "%s: Failed to resolve path for %s\n", __func__, dev);
|
|
|
|
return -1;
|
2023-06-24 03:16:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (disk->is_initialized == GPT_DISK_INIT_MAGIC) {
|
|
|
|
// We already have a valid disk handle. Free it.
|
|
|
|
LOGD("%s: Freeing disk handle for %s... -> %s\n", __func__, disk->devpath, devpath);
|
|
|
|
gpt_disk_free(disk);
|
|
|
|
}
|
|
|
|
|
2023-06-24 07:59:09 +08:00
|
|
|
LOGD("%s: Initializing disk handle for %s... -> %s\n", __func__, disk->devpath, devpath);
|
|
|
|
|
2023-06-24 03:16:24 +08:00
|
|
|
// devpath popualted by partition_is_for_disk
|
|
|
|
strncpy(disk->devpath, devpath, sizeof(disk->devpath));
|
|
|
|
|
|
|
|
if (gpt_get_headers(dev, &disk->hdr, &disk->hdr_bak)) {
|
|
|
|
fprintf(stderr, "%s: Failed to get GPT headers\n", __func__);
|
2023-06-23 20:30:08 +08:00
|
|
|
goto error;
|
2022-06-04 22:51:22 +08:00
|
|
|
}
|
2023-06-24 03:16:24 +08:00
|
|
|
|
2023-06-24 06:21:24 +08:00
|
|
|
assert(disk->hdr != NULL);
|
|
|
|
assert(disk->hdr_bak != NULL);
|
2023-06-24 03:16:24 +08:00
|
|
|
|
2023-06-23 20:30:08 +08:00
|
|
|
gpt_header_size = GET_4_BYTES(disk->hdr + HEADER_SIZE_OFFSET);
|
2023-06-24 03:16:24 +08:00
|
|
|
|
2022-06-04 22:51:22 +08:00
|
|
|
// FIXME: pointer offsets crc bleh
|
2023-06-24 07:59:09 +08:00
|
|
|
disk->hdr_crc = efi_crc32(disk->hdr, gpt_header_size);
|
2023-06-24 00:22:39 +08:00
|
|
|
|
2023-06-24 07:59:09 +08:00
|
|
|
disk->hdr_bak_crc = efi_crc32(disk->hdr_bak, gpt_header_size);
|
2022-06-04 22:51:22 +08:00
|
|
|
|
2023-06-23 20:30:08 +08:00
|
|
|
fd = open(disk->devpath, O_RDWR);
|
|
|
|
if (fd < 0) {
|
2023-06-24 03:16:24 +08:00
|
|
|
fprintf(stderr, "%s: Failed to open %s: %s\n", __func__, disk->devpath,
|
|
|
|
strerror(errno));
|
2023-06-23 20:30:08 +08:00
|
|
|
goto error;
|
2022-06-04 22:51:22 +08:00
|
|
|
}
|
2023-06-24 03:16:24 +08:00
|
|
|
|
2023-06-24 06:21:24 +08:00
|
|
|
assert(disk->pentry_arr == NULL);
|
2023-06-23 20:30:08 +08:00
|
|
|
disk->pentry_arr = gpt_get_pentry_arr(disk->hdr, fd);
|
|
|
|
if (!disk->pentry_arr) {
|
2023-06-24 03:16:24 +08:00
|
|
|
fprintf(stderr, "%s: Failed to obtain partition entry array\n", __func__);
|
2023-06-23 20:30:08 +08:00
|
|
|
goto error;
|
2022-06-04 22:51:22 +08:00
|
|
|
}
|
2023-06-24 03:16:24 +08:00
|
|
|
|
2023-06-24 06:21:24 +08:00
|
|
|
assert(disk->pentry_arr_bak == NULL);
|
2023-06-23 20:30:08 +08:00
|
|
|
disk->pentry_arr_bak = gpt_get_pentry_arr(disk->hdr_bak, fd);
|
|
|
|
if (!disk->pentry_arr_bak) {
|
2023-06-24 03:16:24 +08:00
|
|
|
fprintf(stderr, "%s: Failed to obtain backup partition entry array\n", __func__);
|
2023-06-23 20:30:08 +08:00
|
|
|
goto error;
|
2022-06-04 22:51:22 +08:00
|
|
|
}
|
2023-06-24 03:16:24 +08:00
|
|
|
|
2023-06-23 20:30:08 +08:00
|
|
|
disk->pentry_size = GET_4_BYTES(disk->hdr + PENTRY_SIZE_OFFSET);
|
2023-06-24 03:16:24 +08:00
|
|
|
disk->pentry_arr_size = GET_4_BYTES(disk->hdr + PARTITION_COUNT_OFFSET) * disk->pentry_size;
|
2023-06-23 20:30:08 +08:00
|
|
|
disk->pentry_arr_crc = GET_4_BYTES(disk->hdr + PARTITION_CRC_OFFSET);
|
2023-06-24 03:16:24 +08:00
|
|
|
disk->pentry_arr_bak_crc = GET_4_BYTES(disk->hdr_bak + PARTITION_CRC_OFFSET);
|
2023-06-23 20:30:08 +08:00
|
|
|
disk->block_size = gpt_get_block_size(fd);
|
|
|
|
close(fd);
|
2022-06-04 22:51:22 +08:00
|
|
|
disk->is_initialized = GPT_DISK_INIT_MAGIC;
|
|
|
|
return 0;
|
2023-06-23 20:30:08 +08:00
|
|
|
error:
|
|
|
|
if (fd >= 0)
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
2021-10-10 06:16:20 +08:00
|
|
|
}
|
|
|
|
|
2023-06-24 03:16:24 +08:00
|
|
|
// Get pointer to partition entry from a allocated gpt_disk structure
|
|
|
|
uint8_t *gpt_disk_get_pentry(struct gpt_disk *disk, const char *partname, enum gpt_instance instance)
|
2021-10-10 06:16:20 +08:00
|
|
|
{
|
2023-06-24 06:21:24 +08:00
|
|
|
uint8_t *ptn_arr = NULL;
|
2022-06-04 22:51:22 +08:00
|
|
|
if (!disk || !partname || disk->is_initialized != GPT_DISK_INIT_MAGIC) {
|
2023-06-24 03:16:24 +08:00
|
|
|
fprintf(stderr, "%s: disk handle not initialised\n", __func__);
|
2023-06-24 06:21:24 +08:00
|
|
|
return NULL;
|
2022-06-04 22:51:22 +08:00
|
|
|
}
|
2023-06-24 03:16:24 +08:00
|
|
|
ptn_arr = (instance == PRIMARY_GPT) ? disk->pentry_arr : disk->pentry_arr_bak;
|
|
|
|
return (gpt_pentry_seek(partname, ptn_arr, ptn_arr + disk->pentry_arr_size,
|
2022-06-04 22:51:22 +08:00
|
|
|
disk->pentry_size));
|
2021-10-10 06:16:20 +08:00
|
|
|
}
|
|
|
|
|
2023-06-24 03:16:24 +08:00
|
|
|
// Update CRC values for the various components of the gpt_disk
|
|
|
|
// structure. This function should be called after any of the fields
|
|
|
|
// have been updated before the structure contents are written back to
|
|
|
|
// disk.
|
|
|
|
static int gpt_disk_update_crc(struct gpt_disk *disk)
|
2021-10-10 06:16:20 +08:00
|
|
|
{
|
2022-06-04 22:51:22 +08:00
|
|
|
uint32_t gpt_header_size = 0;
|
|
|
|
if (!disk || (disk->is_initialized != GPT_DISK_INIT_MAGIC)) {
|
2023-06-24 03:16:24 +08:00
|
|
|
fprintf(stderr, "%s: disk not initialised!\n", __func__);
|
|
|
|
return -1;
|
2022-06-04 22:51:22 +08:00
|
|
|
}
|
2023-06-24 03:16:24 +08:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
uint32_t old_crc = disk->pentry_arr_crc;
|
|
|
|
#endif
|
|
|
|
// Recalculate the CRC of the primary partiton array
|
2023-06-24 07:59:09 +08:00
|
|
|
disk->pentry_arr_crc = efi_crc32(disk->pentry_arr, disk->pentry_arr_size);
|
|
|
|
LOGD("%s() disk %8s GPT pentry len %u crc: %08x -> %08x\n", __func__, disk->devpath,
|
2023-06-24 03:16:24 +08:00
|
|
|
disk->pentry_arr_size, old_crc, disk->pentry_arr_crc);
|
|
|
|
|
|
|
|
// DumpHex(disk->pentry_arr, disk->pentry_arr_size);
|
|
|
|
|
|
|
|
// Recalculate the CRC of the backup partition array
|
2023-06-24 07:59:09 +08:00
|
|
|
disk->pentry_arr_bak_crc = efi_crc32(disk->pentry_arr_bak, disk->pentry_arr_size);
|
|
|
|
LOGD("%s() disk %8s GPT pentry_bak len %u crc: %08x -> %08x\n", __func__, disk->devpath,
|
|
|
|
disk->pentry_arr_size, old_crc, disk->pentry_arr_crc);
|
2023-06-24 03:16:24 +08:00
|
|
|
|
|
|
|
// Update the partition CRC value in the primary GPT header
|
2023-06-23 20:30:08 +08:00
|
|
|
PUT_4_BYTES(disk->hdr + PARTITION_CRC_OFFSET, disk->pentry_arr_crc);
|
2023-06-24 03:16:24 +08:00
|
|
|
|
|
|
|
// Update the partition CRC value in the backup GPT header
|
|
|
|
PUT_4_BYTES(disk->hdr_bak + PARTITION_CRC_OFFSET, disk->pentry_arr_bak_crc);
|
|
|
|
|
|
|
|
// Update the CRC value of the primary header
|
2023-06-23 20:30:08 +08:00
|
|
|
gpt_header_size = GET_4_BYTES(disk->hdr + HEADER_SIZE_OFFSET);
|
2023-06-24 03:16:24 +08:00
|
|
|
|
|
|
|
// Header CRC is calculated with its own CRC field set to 0
|
2023-06-23 20:30:08 +08:00
|
|
|
PUT_4_BYTES(disk->hdr + HEADER_CRC_OFFSET, 0);
|
|
|
|
PUT_4_BYTES(disk->hdr_bak + HEADER_CRC_OFFSET, 0);
|
2023-06-24 07:59:09 +08:00
|
|
|
disk->hdr_crc = efi_crc32(disk->hdr, gpt_header_size);
|
|
|
|
disk->hdr_bak_crc = efi_crc32(disk->hdr_bak, gpt_header_size);
|
2023-06-23 20:30:08 +08:00
|
|
|
PUT_4_BYTES(disk->hdr + HEADER_CRC_OFFSET, disk->hdr_crc);
|
|
|
|
PUT_4_BYTES(disk->hdr_bak + HEADER_CRC_OFFSET, disk->hdr_bak_crc);
|
2022-06-04 22:51:22 +08:00
|
|
|
return 0;
|
2021-10-10 06:16:20 +08:00
|
|
|
}
|
|
|
|
|
2023-06-24 03:16:24 +08:00
|
|
|
// Write the contents of struct gpt_disk back to the actual disk
|
2021-10-10 06:16:20 +08:00
|
|
|
int gpt_disk_commit(struct gpt_disk *disk)
|
|
|
|
{
|
2023-06-23 20:30:08 +08:00
|
|
|
int fd = -1;
|
2023-06-24 03:16:24 +08:00
|
|
|
|
2022-06-04 22:51:22 +08:00
|
|
|
if (!disk || (disk->is_initialized != GPT_DISK_INIT_MAGIC)) {
|
|
|
|
fprintf(stderr, "%s: Invalid args\n", __func__);
|
2023-06-23 20:30:08 +08:00
|
|
|
goto error;
|
2022-06-04 22:51:22 +08:00
|
|
|
}
|
2023-06-24 03:16:24 +08:00
|
|
|
|
|
|
|
if (gpt_disk_update_crc(disk)) {
|
|
|
|
fprintf(stderr, "%s: Failed to update CRC values\n", __func__);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2023-06-23 20:30:08 +08:00
|
|
|
fd = open(disk->devpath, O_RDWR);
|
|
|
|
if (fd < 0) {
|
2023-06-24 03:16:24 +08:00
|
|
|
fprintf(stderr, "%s: Failed to open %s: %s\n", __func__, disk->devpath,
|
|
|
|
strerror(errno));
|
2023-06-23 20:30:08 +08:00
|
|
|
goto error;
|
2022-06-04 22:51:22 +08:00
|
|
|
}
|
2023-06-24 03:16:24 +08:00
|
|
|
|
2022-06-04 22:51:22 +08:00
|
|
|
LOGD("%s: Writing back primary GPT header\n", __func__);
|
2023-06-24 03:16:24 +08:00
|
|
|
|
|
|
|
// Write the primary header
|
2023-06-23 20:30:08 +08:00
|
|
|
if (gpt_set_header(disk->hdr, fd, PRIMARY_GPT) != 0) {
|
2023-06-24 03:16:24 +08:00
|
|
|
fprintf(stderr, "%s: Failed to update primary GPT header\n", __func__);
|
2023-06-23 20:30:08 +08:00
|
|
|
goto error;
|
2022-06-04 22:51:22 +08:00
|
|
|
}
|
|
|
|
LOGD("%s: Writing back primary partition array\n", __func__);
|
2023-06-24 03:16:24 +08:00
|
|
|
|
|
|
|
// Write back the primary partition array
|
2023-06-23 20:30:08 +08:00
|
|
|
if (gpt_set_pentry_arr(disk->hdr, fd, disk->pentry_arr)) {
|
2023-06-24 03:16:24 +08:00
|
|
|
fprintf(stderr, "%s: Failed to write primary GPT partition arr\n", __func__);
|
2023-06-23 20:30:08 +08:00
|
|
|
goto error;
|
2022-06-04 22:51:22 +08:00
|
|
|
}
|
2023-06-24 00:23:56 +08:00
|
|
|
|
|
|
|
// Write the backup header
|
|
|
|
if (gpt_set_header(disk->hdr_bak, fd, SECONDARY_GPT) != 0) {
|
2023-06-24 03:16:24 +08:00
|
|
|
fprintf(stderr, "%s: Failed to update backup GPT header\n", __func__);
|
2023-06-24 00:23:56 +08:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
LOGD("%s: Writing back backup partition array\n", __func__);
|
2023-06-24 03:16:24 +08:00
|
|
|
|
2023-06-24 00:23:56 +08:00
|
|
|
// Write back the backup partition array
|
|
|
|
if (gpt_set_pentry_arr(disk->hdr_bak, fd, disk->pentry_arr_bak)) {
|
2023-06-24 03:16:24 +08:00
|
|
|
fprintf(stderr, "%s: Failed to write backup GPT partition arr\n", __func__);
|
2023-06-24 00:23:56 +08:00
|
|
|
goto error;
|
|
|
|
}
|
2023-06-24 07:59:09 +08:00
|
|
|
|
|
|
|
LOGD("%s: Done\n", __func__);
|
|
|
|
|
2023-06-23 20:30:08 +08:00
|
|
|
fsync(fd);
|
|
|
|
close(fd);
|
2022-06-04 22:51:22 +08:00
|
|
|
return 0;
|
2023-06-24 07:59:09 +08:00
|
|
|
|
2023-06-23 20:30:08 +08:00
|
|
|
error:
|
|
|
|
if (fd >= 0)
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
2021-10-10 06:16:20 +08:00
|
|
|
}
|
2022-08-30 08:54:35 +08:00
|
|
|
|
2023-06-24 03:16:24 +08:00
|
|
|
// Determine whether to handle the given partition as eMMC or UFS, using the
|
|
|
|
// name of the backing device.
|
2022-08-30 08:54:35 +08:00
|
|
|
//
|
2023-06-24 03:16:24 +08:00
|
|
|
// Note: In undefined cases (i.e. /dev/mmcblk1 and unresolvable), this function
|
|
|
|
// will tend to prefer UFS behavior. If it incorrectly reports this, then the
|
|
|
|
// program should exit (e.g. by failing) before making any changes.
|
|
|
|
bool gpt_utils_is_partition_backed_by_emmc(const char *part)
|
|
|
|
{
|
2023-06-24 07:59:09 +08:00
|
|
|
char devpath[GPT_PTN_PATH_MAX] = { '\0' };
|
2022-08-30 08:54:35 +08:00
|
|
|
|
|
|
|
if (get_dev_path_from_partition_name(part, devpath, sizeof(devpath)))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return !strcmp(devpath, EMMC_DEVICE);
|
|
|
|
}
|