even even more rework, drop zlib dep

Also fix the mark_boot_successful feature which I broke earlier
main
Caleb Connolly 1 year ago
parent bca9aa5dd7
commit e9e86efb96
No known key found for this signature in database
GPG Key ID: 0583312B195F64B6
  1. 80
      bootctrl_impl.c
  2. 128
      crc32.c
  3. 38
      crc32.h
  4. 34
      gpt-utils.c
  5. 6
      gpt-utils.h
  6. 6
      meson.build

@ -141,61 +141,46 @@ static int get_partition_attribute(struct gpt_disk *disk, const char *partname,
// Set a particular attribute for all the partitions in a // Set a particular attribute for all the partitions in a
// slot // slot
static int update_slot_attribute(struct gpt_disk *disk, const char *slot, static int update_slot_attribute(struct gpt_disk *disk, unsigned slot,
enum part_attr_type ab_attr) enum part_attr_type ab_attr)
{ {
unsigned int i = 0; unsigned int i = 0;
char buf[PATH_MAX]; char buf[GPT_PTN_PATH_MAX];
struct stat st; struct stat st;
uint8_t *pentry = NULL; uint8_t *pentry = NULL;
uint8_t *pentry_bak = NULL; uint8_t *pentry_bak = NULL;
int rc = -1; int rc = -1;
uint8_t *attr = NULL; uint8_t *attr = NULL;
uint8_t *attr_bak = NULL; uint8_t *attr_bak = NULL;
char partName[MAX_GPT_NAME_SIZE + 1] = { 0 }; const char *partName;
int slot_name_valid = 0; char devpath[GPT_PTN_PATH_MAX] = { 0 };
char devpath[PATH_MAX] = { 0 };
if (!slot) {
fprintf(stderr, "%s: Invalid argument\n", __func__);
return -1;
}
for (i = 0; slot_suffix_arr[i] != NULL; i++) {
if (!strncmp(slot, slot_suffix_arr[i], strlen(slot_suffix_arr[i])))
slot_name_valid = 1;
}
if (!slot_name_valid) {
fprintf(stderr, "%s: Invalid slot name\n", __func__);
return -1;
}
for (i = 0; i < ARRAY_SIZE(g_all_ptns); i++) { for (i = 0; i < ARRAY_SIZE(g_all_ptns); i++) {
memset(buf, '\0', sizeof(buf)); memset(buf, '\0', sizeof(buf));
// Check if A/B versions of this ptn exist // Check if A/B versions of this ptn exist
snprintf(buf, sizeof(buf) - 1, "%s/%s%s", BOOT_DEV_DIR, g_all_ptns[i], rc = snprintf(buf, sizeof(buf) - 1, "%s/%.72s", BOOT_DEV_DIR, g_all_ptns[i]);
AB_SLOT_A_SUFFIX);
if (stat(buf, &st) < 0) { if (stat(buf, &st) < 0) {
// partition does not have _a version // partition does not have _a version
continue; continue;
} }
memset(buf, '\0', sizeof(buf)); buf[strlen(buf) - 1] = 'b';
snprintf(buf, sizeof(buf) - 1, "%s/%s%s", BOOT_DEV_DIR, g_all_ptns[i],
AB_SLOT_B_SUFFIX);
if (stat(buf, &st) < 0) { if (stat(buf, &st) < 0) {
// partition does not have _b version // partition does not have _b version
continue; continue;
} }
memset(partName, '\0', sizeof(partName)); if (slot == 0)
snprintf(partName, sizeof(partName) - 1, "%s%s", g_all_ptns[i], slot); partName = g_all_ptns[i];
else
partName = buf + strlen(BOOT_DEV_DIR) + 1;
LOGD("%s: partName = '%s'\n", __func__, partName);
// If the current partition is for a different disk (e.g. /dev/sde when the current disk is /dev/sda) // If the current partition is for a different disk (e.g. /dev/sde when the current disk is /dev/sda)
// Then commit the current disk // Then commit the current disk
if (partition_is_for_disk(disk, partName, devpath, sizeof(devpath)) != 0) { if (!partition_is_for_disk(disk, partName, devpath, sizeof(devpath))) {
if (!gpt_disk_commit(disk)) { if (gpt_disk_commit(disk)) {
fprintf(stderr, "%s: Failed to commit disk\n", __func__); fprintf(stderr, "%s: Failed to commit disk\n", __func__);
return -1; return -1;
} }
@ -216,8 +201,8 @@ static int update_slot_attribute(struct gpt_disk *disk, const char *slot,
} }
attr = pentry + AB_FLAG_OFFSET; attr = pentry + AB_FLAG_OFFSET;
LOGD("%s: got pentry for part '%s': 0x%lx (at flags: 0x%x)\n", __func__, partName, // LOGD("%s: got pentry for part '%s': 0x%lx (at flags: 0x%x)\n", __func__, partName,
*(uint64_t *)pentry, *attr); // *(uint64_t *)pentry, *attr);
attr_bak = pentry_bak + AB_FLAG_OFFSET; attr_bak = pentry_bak + AB_FLAG_OFFSET;
switch (ab_attr) { switch (ab_attr) {
case ATTR_BOOT_SUCCESSFUL: case ATTR_BOOT_SUCCESSFUL:
@ -243,8 +228,7 @@ static int update_slot_attribute(struct gpt_disk *disk, const char *slot,
} }
if (gpt_disk_commit(disk)) { if (gpt_disk_commit(disk)) {
fprintf(stderr, "%s: Failed to write back entry for %s\n", __func__, fprintf(stderr, "%s: Failed to commit disk %s\n", __func__, disk->devpath);
partName);
return -1; return -1;
} }
@ -368,20 +352,20 @@ int mark_boot_successful(unsigned slot)
{ {
struct gpt_disk disk = { 0 }; struct gpt_disk disk = { 0 };
int successful = get_boot_attr(&disk, slot, ATTR_BOOT_SUCCESSFUL); int successful = get_boot_attr(&disk, slot, ATTR_BOOT_SUCCESSFUL);
int bootable = get_boot_attr(&disk, slot, ATTR_UNBOOTABLE); int unbootable = get_boot_attr(&disk, slot, ATTR_UNBOOTABLE);
int ret = 0; int ret = 0;
if (successful < 0 || bootable < 0) { if (successful < 0 || unbootable < 0) {
fprintf(stderr, "SLOT %s: Failed to read attributes\n", slot_suffix_arr[slot]); fprintf(stderr, "SLOT %s: Failed to read attributes\n", slot_suffix_arr[slot]);
ret = -1; ret = -1;
goto out; goto out;
} }
if (!is_slot_bootable(slot)) { if (unbootable) {
printf("SLOT %s: was marked unbootable, fixing this" printf("SLOT %s: was marked unbootable, fixing this"
" (I hope you know what you're doing...)\n", " (I hope you know what you're doing...)\n",
slot_suffix_arr[slot]); slot_suffix_arr[slot]);
update_slot_attribute(&disk, slot_suffix_arr[slot], ATTR_BOOTABLE); update_slot_attribute(&disk, slot, ATTR_BOOTABLE);
} }
if (successful) { if (successful) {
@ -389,7 +373,7 @@ int mark_boot_successful(unsigned slot)
goto out; goto out;
} }
if (update_slot_attribute(&disk, slot_suffix_arr[slot], ATTR_BOOT_SUCCESSFUL)) { if (update_slot_attribute(&disk, slot, ATTR_BOOT_SUCCESSFUL)) {
fprintf(stderr, "SLOT %s: Failed to mark boot successful\n", slot_suffix_arr[slot]); fprintf(stderr, "SLOT %s: Failed to mark boot successful\n", slot_suffix_arr[slot]);
ret = -1; ret = -1;
goto out; goto out;
@ -411,10 +395,10 @@ const char *get_suffix(unsigned slot)
// The argument here is a vector of partition names(including the slot suffix) // The argument here is a vector of partition names(including the slot suffix)
// that lie on a single disk // that lie on a single disk
static int boot_ctl_set_active_slot_for_partitions(struct gpt_disk *disk, const char ptn_list[][MAX_GPT_NAME_SIZE], int len, static int boot_ctl_set_active_slot_for_partitions(struct gpt_disk *disk,
unsigned slot) unsigned slot)
{ {
char buf[PATH_MAX] = { 0 }; char buf[GPT_PTN_PATH_MAX] = { 0 };
const char *slotA; const char *slotA;
char slotB[MAX_GPT_NAME_SIZE] = { 0 }; char slotB[MAX_GPT_NAME_SIZE] = { 0 };
char active_guid[TYPE_GUID_SIZE + 1] = { 0 }; char active_guid[TYPE_GUID_SIZE + 1] = { 0 };
@ -430,7 +414,7 @@ static int boot_ctl_set_active_slot_for_partitions(struct gpt_disk *disk, const
LOGD("Marking slot %s as active:\n", slot_suffix_arr[slot]); LOGD("Marking slot %s as active:\n", slot_suffix_arr[slot]);
for (i = 0, slotA = ptn_list[0]; i < len; slotA = ptn_list[++i]) { for (i = 0, slotA = g_all_ptns[0]; i < ARRAY_SIZE(g_all_ptns); slotA = g_all_ptns[++i]) {
// Chop off the slot suffix from the partition name to // Chop off the slot suffix from the partition name to
// make the string easier to work with. // make the string easier to work with.
LOGD("Part: %s\n", slotA); LOGD("Part: %s\n", slotA);
@ -441,11 +425,11 @@ static int boot_ctl_set_active_slot_for_partitions(struct gpt_disk *disk, const
} }
memset(slotB, 0, sizeof(slotB)); memset(slotB, 0, sizeof(slotB));
strncat(slotB, slotA, n); strncat(slotB, slotA, MAX_GPT_NAME_SIZE - 1);
strncat(slotB + n, AB_SLOT_B_SUFFIX, 3); slotB[strlen(slotB) - 1] = 'b';
rc = snprintf(buf, sizeof(buf) - 1, "%s", BOOT_DEV_DIR); rc = snprintf(buf, sizeof(buf) - 1, "%s", BOOT_DEV_DIR);
snprintf(buf + rc, PATH_MAX - rc, "/%s", slotA); snprintf(buf + rc, GPT_PTN_PATH_MAX - rc, "/%s", slotA);
LOGD("Checking for partition %s\n", buf); LOGD("Checking for partition %s\n", buf);
if (stat(buf, &st)) { if (stat(buf, &st)) {
if (!strcmp(slotA, "boot_a") || !strcmp(slotA, "dtbo_a")) { if (!strcmp(slotA, "boot_a") || !strcmp(slotA, "dtbo_a")) {
@ -456,9 +440,9 @@ static int boot_ctl_set_active_slot_for_partitions(struct gpt_disk *disk, const
continue; continue;
} }
snprintf(buf + rc, PATH_MAX - rc, "/%s", slotB); buf[strlen(buf) - 1] = 'b';
if (stat(buf, &st)) { if (stat(buf, &st)) {
fprintf(stderr, "Partition %s does not exist\n", slotB); fprintf(stderr, "Partition %s does not exist\n", buf);
return -1; return -1;
} }
@ -569,7 +553,7 @@ int set_active_boot_slot(unsigned slot)
return -1; return -1;
} }
rc = boot_ctl_set_active_slot_for_partitions(&disk, g_all_ptns, ARRAY_SIZE(g_all_ptns), slot); rc = boot_ctl_set_active_slot_for_partitions(&disk, slot);
if (rc) { if (rc) {
fprintf(stderr, "%s: Failed to set active slot for partitions \n", __func__); fprintf(stderr, "%s: Failed to set active slot for partitions \n", __func__);
@ -605,7 +589,7 @@ int set_slot_as_unbootable(unsigned slot)
if (boot_control_check_slot_sanity(slot) != 0) if (boot_control_check_slot_sanity(slot) != 0)
return -1; return -1;
ret = update_slot_attribute(&disk, slot_suffix_arr[slot], ATTR_UNBOOTABLE); ret = update_slot_attribute(&disk, slot, ATTR_UNBOOTABLE);
gpt_disk_free(&disk); gpt_disk_free(&disk);
return ret; return ret;

@ -0,0 +1,128 @@
/*
* Dec 5, 2000 Matt Domsch <Matt_Domsch@dell.com>
* - Copied crc32.c from the linux/drivers/net/cipe directory.
* - Now pass seed as an arg
* - changed len to be an unsigned long
* - changed crc32val to be a register
* - License remains unchanged! It's still GPL-compatable!
*/
/*
* Copied from efibootmgr 2023-06-23
*/
/* ============================================================= */
/* COPYRIGHT (C) 1986 Gary S. Brown. You may use this program, or */
/* code or tables extracted from it, as desired without restriction. */
/* */
/* First, the polynomial itself and its table of feedback terms. The */
/* polynomial is */
/* X^32+X^26+X^23+X^22+X^16+X^12+X^11+X^10+X^8+X^7+X^5+X^4+X^2+X^1+X^0 */
/* */
/* Note that we take it "backwards" and put the highest-order term in */
/* the lowest-order bit. The X^32 term is "implied"; the LSB is the */
/* X^31 term, etc. The X^0 term (usually shown as "+1") results in */
/* the MSB being 1. */
/* */
/* Note that the usual hardware shift register implementation, which */
/* is what we're using (we're merely optimizing it by doing eight-bit */
/* chunks at a time) shifts bits into the lowest-order term. In our */
/* implementation, that means shifting towards the right. Why do we */
/* do it this way? Because the calculated CRC must be transmitted in */
/* order from highest-order term to lowest-order term. UARTs transmit */
/* characters in order from LSB to MSB. By storing the CRC this way, */
/* we hand it to the UART in the order low-byte to high-byte; the UART */
/* sends each low-bit to hight-bit; and the result is transmission bit */
/* by bit from highest- to lowest-order term without requiring any bit */
/* shuffling on our part. Reception works similarly. */
/* */
/* The feedback terms table consists of 256, 32-bit entries. Notes: */
/* */
/* The table can be generated at runtime if desired; code to do so */
/* is shown later. It might not be obvious, but the feedback */
/* terms simply represent the results of eight shift/xor opera- */
/* tions for all combinations of data and CRC register values. */
/* */
/* The values must be right-shifted by eight bits by the "updcrc" */
/* logic; the shift must be unsigned (bring in zeroes). On some */
/* hardware you could probably optimize the shift in assembler by */
/* using byte-swap instructions. */
/* polynomial $edb88320 */
/* */
/* -------------------------------------------------------------------- */
#include <stdint.h>
static uint32_t crc32_tab[] = {
0x00000000L, 0x77073096L, 0xee0e612cL, 0x990951baL, 0x076dc419L,
0x706af48fL, 0xe963a535L, 0x9e6495a3L, 0x0edb8832L, 0x79dcb8a4L,
0xe0d5e91eL, 0x97d2d988L, 0x09b64c2bL, 0x7eb17cbdL, 0xe7b82d07L,
0x90bf1d91L, 0x1db71064L, 0x6ab020f2L, 0xf3b97148L, 0x84be41deL,
0x1adad47dL, 0x6ddde4ebL, 0xf4d4b551L, 0x83d385c7L, 0x136c9856L,
0x646ba8c0L, 0xfd62f97aL, 0x8a65c9ecL, 0x14015c4fL, 0x63066cd9L,
0xfa0f3d63L, 0x8d080df5L, 0x3b6e20c8L, 0x4c69105eL, 0xd56041e4L,
0xa2677172L, 0x3c03e4d1L, 0x4b04d447L, 0xd20d85fdL, 0xa50ab56bL,
0x35b5a8faL, 0x42b2986cL, 0xdbbbc9d6L, 0xacbcf940L, 0x32d86ce3L,
0x45df5c75L, 0xdcd60dcfL, 0xabd13d59L, 0x26d930acL, 0x51de003aL,
0xc8d75180L, 0xbfd06116L, 0x21b4f4b5L, 0x56b3c423L, 0xcfba9599L,
0xb8bda50fL, 0x2802b89eL, 0x5f058808L, 0xc60cd9b2L, 0xb10be924L,
0x2f6f7c87L, 0x58684c11L, 0xc1611dabL, 0xb6662d3dL, 0x76dc4190L,
0x01db7106L, 0x98d220bcL, 0xefd5102aL, 0x71b18589L, 0x06b6b51fL,
0x9fbfe4a5L, 0xe8b8d433L, 0x7807c9a2L, 0x0f00f934L, 0x9609a88eL,
0xe10e9818L, 0x7f6a0dbbL, 0x086d3d2dL, 0x91646c97L, 0xe6635c01L,
0x6b6b51f4L, 0x1c6c6162L, 0x856530d8L, 0xf262004eL, 0x6c0695edL,
0x1b01a57bL, 0x8208f4c1L, 0xf50fc457L, 0x65b0d9c6L, 0x12b7e950L,
0x8bbeb8eaL, 0xfcb9887cL, 0x62dd1ddfL, 0x15da2d49L, 0x8cd37cf3L,
0xfbd44c65L, 0x4db26158L, 0x3ab551ceL, 0xa3bc0074L, 0xd4bb30e2L,
0x4adfa541L, 0x3dd895d7L, 0xa4d1c46dL, 0xd3d6f4fbL, 0x4369e96aL,
0x346ed9fcL, 0xad678846L, 0xda60b8d0L, 0x44042d73L, 0x33031de5L,
0xaa0a4c5fL, 0xdd0d7cc9L, 0x5005713cL, 0x270241aaL, 0xbe0b1010L,
0xc90c2086L, 0x5768b525L, 0x206f85b3L, 0xb966d409L, 0xce61e49fL,
0x5edef90eL, 0x29d9c998L, 0xb0d09822L, 0xc7d7a8b4L, 0x59b33d17L,
0x2eb40d81L, 0xb7bd5c3bL, 0xc0ba6cadL, 0xedb88320L, 0x9abfb3b6L,
0x03b6e20cL, 0x74b1d29aL, 0xead54739L, 0x9dd277afL, 0x04db2615L,
0x73dc1683L, 0xe3630b12L, 0x94643b84L, 0x0d6d6a3eL, 0x7a6a5aa8L,
0xe40ecf0bL, 0x9309ff9dL, 0x0a00ae27L, 0x7d079eb1L, 0xf00f9344L,
0x8708a3d2L, 0x1e01f268L, 0x6906c2feL, 0xf762575dL, 0x806567cbL,
0x196c3671L, 0x6e6b06e7L, 0xfed41b76L, 0x89d32be0L, 0x10da7a5aL,
0x67dd4accL, 0xf9b9df6fL, 0x8ebeeff9L, 0x17b7be43L, 0x60b08ed5L,
0xd6d6a3e8L, 0xa1d1937eL, 0x38d8c2c4L, 0x4fdff252L, 0xd1bb67f1L,
0xa6bc5767L, 0x3fb506ddL, 0x48b2364bL, 0xd80d2bdaL, 0xaf0a1b4cL,
0x36034af6L, 0x41047a60L, 0xdf60efc3L, 0xa867df55L, 0x316e8eefL,
0x4669be79L, 0xcb61b38cL, 0xbc66831aL, 0x256fd2a0L, 0x5268e236L,
0xcc0c7795L, 0xbb0b4703L, 0x220216b9L, 0x5505262fL, 0xc5ba3bbeL,
0xb2bd0b28L, 0x2bb45a92L, 0x5cb36a04L, 0xc2d7ffa7L, 0xb5d0cf31L,
0x2cd99e8bL, 0x5bdeae1dL, 0x9b64c2b0L, 0xec63f226L, 0x756aa39cL,
0x026d930aL, 0x9c0906a9L, 0xeb0e363fL, 0x72076785L, 0x05005713L,
0x95bf4a82L, 0xe2b87a14L, 0x7bb12baeL, 0x0cb61b38L, 0x92d28e9bL,
0xe5d5be0dL, 0x7cdcefb7L, 0x0bdbdf21L, 0x86d3d2d4L, 0xf1d4e242L,
0x68ddb3f8L, 0x1fda836eL, 0x81be16cdL, 0xf6b9265bL, 0x6fb077e1L,
0x18b74777L, 0x88085ae6L, 0xff0f6a70L, 0x66063bcaL, 0x11010b5cL,
0x8f659effL, 0xf862ae69L, 0x616bffd3L, 0x166ccf45L, 0xa00ae278L,
0xd70dd2eeL, 0x4e048354L, 0x3903b3c2L, 0xa7672661L, 0xd06016f7L,
0x4969474dL, 0x3e6e77dbL, 0xaed16a4aL, 0xd9d65adcL, 0x40df0b66L,
0x37d83bf0L, 0xa9bcae53L, 0xdebb9ec5L, 0x47b2cf7fL, 0x30b5ffe9L,
0xbdbdf21cL, 0xcabac28aL, 0x53b39330L, 0x24b4a3a6L, 0xbad03605L,
0xcdd70693L, 0x54de5729L, 0x23d967bfL, 0xb3667a2eL, 0xc4614ab8L,
0x5d681b02L, 0x2a6f2b94L, 0xb40bbe37L, 0xc30c8ea1L, 0x5a05df1bL,
0x2d02ef8dL
};
/* Return a 32-bit CRC of the contents of the buffer. */
uint32_t
efi_crc32(const void *buf, unsigned long len)
{
unsigned long i;
register uint32_t crc32val;
const unsigned char *s = buf;
crc32val = ~0U;
for (i = 0; i < len; i ++)
{
crc32val =
crc32_tab[(crc32val ^ s[i]) & 0xff] ^
(crc32val >> 8);
}
return crc32val ^ ~0U;
}

@ -0,0 +1,38 @@
/*
libparted - a library for manipulating disk partitions
Copyright (C) 1998-2000 Free Software Foundation, Inc.
crc32.h
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.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
* Copied from efibootmgr 2023-06-23
*/
#ifndef _CRC32_H
#define _CRC32_H
#include <stdint.h>
/*
* This computes a 32 bit CRC of the data in the buffer, and returns the CRC.
* The polynomial used is 0xedb88320.
*/
extern uint32_t efi_crc32 (const void *buf, unsigned long len);
#endif /* _CRC32_H */

@ -47,10 +47,10 @@
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <unistd.h> #include <unistd.h>
#include <zlib.h>
#include "gpt-utils.h" #include "gpt-utils.h"
#include "utils.h" #include "utils.h"
#include "crc32.h"
/* list the names of the backed-up partitions to be swapped */ /* list the names of the backed-up partitions to be swapped */
/* extension used for the backup partitions - tzbak, abootbak, etc. */ /* extension used for the backup partitions - tzbak, abootbak, etc. */
@ -97,7 +97,7 @@ enum gpt_state { GPT_OK = 0, GPT_BAD_SIGNATURE, GPT_BAD_CRC };
// List of LUN's containing boot critical images. // List of LUN's containing boot critical images.
// Required in the case of UFS devices // Required in the case of UFS devices
struct update_data { struct update_data {
char lun_list[MAX_LUNS][PATH_MAX]; char lun_list[MAX_LUNS][GPT_PTN_PATH_MAX];
uint32_t num_valid_entries; uint32_t num_valid_entries;
}; };
@ -292,7 +292,7 @@ error:
// the path to the LUN from there. // the path to the LUN from there.
static int get_dev_path_from_partition_name(const char *partname, char *buf, size_t buflen) static int get_dev_path_from_partition_name(const char *partname, char *buf, size_t buflen)
{ {
char path[PATH_MAX] = { 0 }; char path[GPT_PTN_PATH_MAX] = { 0 };
int i; int i;
if (!partname || !buf || buflen < ((PATH_TRUNCATE_LOC) + 1)) { if (!partname || !buf || buflen < ((PATH_TRUNCATE_LOC) + 1)) {
@ -386,7 +386,7 @@ error:
static int gpt_get_headers(const char *partname, uint8_t **primary, uint8_t **backup) static int gpt_get_headers(const char *partname, uint8_t **primary, uint8_t **backup)
{ {
uint8_t *hdr = NULL; uint8_t *hdr = NULL;
char devpath[PATH_MAX] = { 0 }; char devpath[GPT_PTN_PATH_MAX] = { 0 };
off_t hdr_offset = 0; off_t hdr_offset = 0;
uint32_t block_size = 0; uint32_t block_size = 0;
int instance; int instance;
@ -594,7 +594,7 @@ int gpt_disk_get_disk_info(const char *dev, struct gpt_disk *disk)
{ {
int fd = -1, rc; int fd = -1, rc;
uint32_t gpt_header_size = 0; uint32_t gpt_header_size = 0;
char devpath[PATH_MAX] = { 0 }; char devpath[GPT_PTN_PATH_MAX] = { 0 };
if (!disk || !dev) { if (!disk || !dev) {
fprintf(stderr, "%s: Invalid arguments\n", __func__); fprintf(stderr, "%s: Invalid arguments\n", __func__);
@ -617,6 +617,8 @@ int gpt_disk_get_disk_info(const char *dev, struct gpt_disk *disk)
gpt_disk_free(disk); gpt_disk_free(disk);
} }
LOGD("%s: Initializing disk handle for %s... -> %s\n", __func__, disk->devpath, devpath);
// devpath popualted by partition_is_for_disk // devpath popualted by partition_is_for_disk
strncpy(disk->devpath, devpath, sizeof(disk->devpath)); strncpy(disk->devpath, devpath, sizeof(disk->devpath));
@ -631,9 +633,9 @@ int gpt_disk_get_disk_info(const char *dev, struct gpt_disk *disk)
gpt_header_size = GET_4_BYTES(disk->hdr + HEADER_SIZE_OFFSET); gpt_header_size = GET_4_BYTES(disk->hdr + HEADER_SIZE_OFFSET);
// FIXME: pointer offsets crc bleh // FIXME: pointer offsets crc bleh
disk->hdr_crc = crc32(0, disk->hdr, gpt_header_size); disk->hdr_crc = efi_crc32(disk->hdr, gpt_header_size);
disk->hdr_bak_crc = crc32(0, disk->hdr_bak, gpt_header_size); disk->hdr_bak_crc = efi_crc32(disk->hdr_bak, gpt_header_size);
fd = open(disk->devpath, O_RDWR); fd = open(disk->devpath, O_RDWR);
if (fd < 0) { if (fd < 0) {
@ -699,14 +701,16 @@ static int gpt_disk_update_crc(struct gpt_disk *disk)
uint32_t old_crc = disk->pentry_arr_crc; uint32_t old_crc = disk->pentry_arr_crc;
#endif #endif
// Recalculate the CRC of the primary partiton array // Recalculate the CRC of the primary partiton array
disk->pentry_arr_crc = crc32(0, disk->pentry_arr, disk->pentry_arr_size); disk->pentry_arr_crc = efi_crc32(disk->pentry_arr, disk->pentry_arr_size);
LOGD("%s() disk %8s GPT hdr len %u crc: %08x -> %08x\n", __func__, disk->devpath, LOGD("%s() disk %8s GPT pentry len %u crc: %08x -> %08x\n", __func__, disk->devpath,
disk->pentry_arr_size, old_crc, disk->pentry_arr_crc); disk->pentry_arr_size, old_crc, disk->pentry_arr_crc);
// DumpHex(disk->pentry_arr, disk->pentry_arr_size); // DumpHex(disk->pentry_arr, disk->pentry_arr_size);
// Recalculate the CRC of the backup partition array // Recalculate the CRC of the backup partition array
disk->pentry_arr_bak_crc = crc32(0, disk->pentry_arr_bak, disk->pentry_arr_size); 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);
// Update the partition CRC value in the primary GPT header // Update the partition CRC value in the primary GPT header
PUT_4_BYTES(disk->hdr + PARTITION_CRC_OFFSET, disk->pentry_arr_crc); PUT_4_BYTES(disk->hdr + PARTITION_CRC_OFFSET, disk->pentry_arr_crc);
@ -720,8 +724,8 @@ static int gpt_disk_update_crc(struct gpt_disk *disk)
// Header CRC is calculated with its own CRC field set to 0 // 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 + HEADER_CRC_OFFSET, 0);
PUT_4_BYTES(disk->hdr_bak + 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_crc = efi_crc32(disk->hdr, gpt_header_size);
disk->hdr_bak_crc = crc32(0, disk->hdr_bak, gpt_header_size); disk->hdr_bak_crc = efi_crc32(disk->hdr_bak, gpt_header_size);
PUT_4_BYTES(disk->hdr + HEADER_CRC_OFFSET, disk->hdr_crc); PUT_4_BYTES(disk->hdr + HEADER_CRC_OFFSET, disk->hdr_crc);
PUT_4_BYTES(disk->hdr_bak + HEADER_CRC_OFFSET, disk->hdr_bak_crc); PUT_4_BYTES(disk->hdr_bak + HEADER_CRC_OFFSET, disk->hdr_bak_crc);
return 0; return 0;
@ -776,9 +780,13 @@ int gpt_disk_commit(struct gpt_disk *disk)
fprintf(stderr, "%s: Failed to write backup GPT partition arr\n", __func__); fprintf(stderr, "%s: Failed to write backup GPT partition arr\n", __func__);
goto error; goto error;
} }
LOGD("%s: Done\n", __func__);
fsync(fd); fsync(fd);
close(fd); close(fd);
return 0; return 0;
error: error:
if (fd >= 0) if (fd >= 0)
close(fd); close(fd);
@ -793,7 +801,7 @@ error:
// program should exit (e.g. by failing) before making any changes. // program should exit (e.g. by failing) before making any changes.
bool gpt_utils_is_partition_backed_by_emmc(const char *part) bool gpt_utils_is_partition_backed_by_emmc(const char *part)
{ {
char devpath[PATH_MAX] = { '\0' }; char devpath[GPT_PTN_PATH_MAX] = { '\0' };
if (get_dev_path_from_partition_name(part, devpath, sizeof(devpath))) if (get_dev_path_from_partition_name(part, devpath, sizeof(devpath)))
return false; return false;

@ -82,8 +82,8 @@ extern "C" {
"hyp_a", "keymaster_a", "msadp_a", "qupfw_a", "storsec_a", "tz_a", \ "hyp_a", "keymaster_a", "msadp_a", "qupfw_a", "storsec_a", "tz_a", \
"vbmeta_a", "vbmeta_system_a" "vbmeta_a", "vbmeta_system_a"
static const char g_all_ptns[][MAX_GPT_NAME_SIZE] = { static const char g_all_ptns[][MAX_GPT_NAME_SIZE + 1] = {
PTN_SWAP_LIST, "boot_a", "system", PTN_SWAP_LIST, "boot_a", "system_a",
"vendor_a", "modem_a", "system_ext_a", "product_a" "vendor_a", "modem_a", "system_ext_a", "product_a"
}; };
@ -92,6 +92,8 @@ static const char g_all_ptns[][MAX_GPT_NAME_SIZE] = {
#define BOOT_DEV_DIR "/dev/disk/by-partlabel" #define BOOT_DEV_DIR "/dev/disk/by-partlabel"
#define GPT_PTN_PATH_MAX sizeof(BOOT_DEV_DIR) + MAX_GPT_NAME_SIZE + 2
#define EMMC_DEVICE "/dev/mmcblk0" #define EMMC_DEVICE "/dev/mmcblk0"
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))

@ -2,10 +2,6 @@ project('qbootctl', 'c', default_options : ['c_std=c11'])
cc = meson.get_compiler('c') cc = meson.get_compiler('c')
deps = [
dependency('zlib'),
]
if not cc.has_header('linux/bsg.h') if not cc.has_header('linux/bsg.h')
error('linux-headers not found') error('linux-headers not found')
endif endif
@ -16,6 +12,7 @@ src = [
'bootctrl_test.c', 'bootctrl_test.c',
'gpt-utils.c', 'gpt-utils.c',
'ufs-bsg.c', 'ufs-bsg.c',
'crc32.c',
] ]
inc = [ inc = [
@ -24,7 +21,6 @@ inc = [
executable('qbootctl', src, executable('qbootctl', src,
include_directories: inc, include_directories: inc,
dependencies: deps,
install: true, install: true,
c_args: [], c_args: [],
) )

Loading…
Cancel
Save