The bootloader counts a slot's retries down on every boot until the slot is marked successful, and at 0 stops on "image destroyed" without falling back to the other slot (measured). qcom-ab-slot (soc/sm8150/rootfs, POSIX sh) sets the successful bit of boot_<slot> in byte 6 of its GPT attributes, in the primary and the backup table with both CRCs, checking both before and after -- the edit qbootctl makes (docs/reference/ qbootctl.txt). The runit service slot-successful runs it once sshd runs; tools/mkrootimg.sh copies soc/<soc>/rootfs and rootfs-<base> into the image. Measured on hotdogb: after a flash (retry 7, not successful) the first boot marks the slot at 6.4 s; fastboot then reports slot-successful:a:yes and the retry count no longer drops. devices/hotdogb/AGENTS.md: a rarer hang remains, 2 of 10 boots with kernel patch 0003. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
271 lines
8.2 KiB
Plaintext
271 lines
8.2 KiB
Plaintext
qbootctl -- Qualcomm A/B slot control for Linux (linux-msm), excerpts
|
|
Source: https://github.com/linux-msm/qbootctl, commit 39a6e6daaf029fb0a083777679a15ea2c18f72de
|
|
Fetched: 2026-11-07. Licence: GPL-3.0 (the repository's LICENSE); quoted here as documentation, no code of it is in this tree.
|
|
Read for: mobile/soc/sm8150/rootfs/usr/bin/qcom-ab-slot (docs/BOOT.md).
|
|
|
|
Settled by it:
|
|
- gpt-utils.h:65-69: "#define AB_FLAG_OFFSET (ATTRIBUTE_FLAG_OFFSET + 6)",
|
|
"AB_PARTITION_ATTR_SLOT_ACTIVE (0x1 << 2)", "AB_PARTITION_ATTR_BOOT_SUCCESSFUL
|
|
(0x1 << 6)", "AB_PARTITION_ATTR_UNBOOTABLE (0x1 << 7)" -- the byte and bits.
|
|
- update_slot_attribute: the bit is set in the primary and the backup entry;
|
|
gpt_disk_commit recomputes the CRCs and writes both tables.
|
|
- Retry count in bits 3-5 and priority in bits 0-1: not named by qbootctl;
|
|
measured against fastboot getvar on hotdogb, 2026-11-07 (docs/BOOT.md).
|
|
|
|
== gpt-utils.h, lines 41-99 ==
|
|
#define GPT_SIGNATURE "EFI PART"
|
|
#define HEADER_SIZE_OFFSET 12
|
|
#define HEADER_CRC_OFFSET 16
|
|
#define PRIMARY_HEADER_OFFSET 24
|
|
#define BACKUP_HEADER_OFFSET 32
|
|
#define FIRST_USABLE_LBA_OFFSET 40
|
|
#define LAST_USABLE_LBA_OFFSET 48
|
|
#define PENTRIES_OFFSET 72
|
|
#define PARTITION_COUNT_OFFSET 80
|
|
#define PENTRY_SIZE_OFFSET 84
|
|
#define PARTITION_CRC_OFFSET 88
|
|
|
|
#define TYPE_GUID_OFFSET 0
|
|
#define TYPE_GUID_SIZE 16
|
|
#define PTN_ENTRY_SIZE 128
|
|
#define UNIQUE_GUID_OFFSET 16
|
|
#define FIRST_LBA_OFFSET 32
|
|
#define LAST_LBA_OFFSET 40
|
|
#define ATTRIBUTE_FLAG_OFFSET 48
|
|
#define PARTITION_NAME_OFFSET 56
|
|
#define MAX_GPT_NAME_SIZE 72
|
|
|
|
// Bit 48 onwords in the attribute field are the ones where we are allowed to
|
|
// store our AB attributes.
|
|
#define AB_FLAG_OFFSET (ATTRIBUTE_FLAG_OFFSET + 6)
|
|
#define GPT_DISK_INIT_MAGIC 0xABCD
|
|
#define AB_PARTITION_ATTR_SLOT_ACTIVE (0x1 << 2)
|
|
#define AB_PARTITION_ATTR_BOOT_SUCCESSFUL (0x1 << 6)
|
|
#define AB_PARTITION_ATTR_UNBOOTABLE (0x1 << 7)
|
|
#define AB_SLOT_ACTIVE_VAL 0xF
|
|
#define AB_SLOT_INACTIVE_VAL 0x0
|
|
#define AB_SLOT_ACTIVE 1
|
|
#define AB_SLOT_INACTIVE 0
|
|
#define AB_SLOT_A_SUFFIX "_a"
|
|
#define AB_SLOT_B_SUFFIX "_b"
|
|
#define PTN_XBL "xbl"
|
|
// XBL is not included because the slot attributes are meaningless there
|
|
// *which* XBL partition is active is determined via the UFS bBootLunEn field
|
|
// as it needs to be handled by PBL
|
|
#define PTN_SWAP_LIST \
|
|
"abl_a", "aop_a", "apdp_a", "cmnlib_a", "cmnlib64_a", "devcfg_a", "dtbo_a", \
|
|
"hyp_a", "keymaster_a", "msadp_a", "qupfw_a", "storsec_a", "tz_a", \
|
|
"vbmeta_a", "vbmeta_system_a"
|
|
|
|
static const char g_all_ptns[][MAX_GPT_NAME_SIZE + 1] = {
|
|
PTN_SWAP_LIST, "boot_a", "system_a",
|
|
"vendor_a", "modem_a", "system_ext_a", "product_a"
|
|
};
|
|
|
|
// No more than /dev/sdk
|
|
#define MAX_BLOCK_DEVICES 10
|
|
|
|
#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 ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
|
|
|
|
== bootctrl_impl.c, update_slot_attribute (lines 169-262) ==
|
|
// Set a particular attribute for all the partitions in a
|
|
// slot
|
|
static int update_slot_attribute(struct gpt_disk *disk, unsigned slot,
|
|
enum part_attr_type ab_attr)
|
|
{
|
|
unsigned int i = 0;
|
|
char buf[GPT_PTN_PATH_MAX];
|
|
struct stat st;
|
|
uint8_t *pentry = NULL;
|
|
uint8_t *pentry_bak = NULL;
|
|
int rc = -1;
|
|
uint8_t *attr = NULL;
|
|
uint8_t *attr_bak = NULL;
|
|
const char *partName;
|
|
char devpath[GPT_PTN_PATH_MAX] = { 0 };
|
|
|
|
for (i = 0; i < ARRAY_SIZE(g_all_ptns); i++) {
|
|
memset(buf, '\0', sizeof(buf));
|
|
// Check if A/B versions of this ptn exist
|
|
rc = snprintf(buf, sizeof(buf) - 1, "%s/%.72s", BOOT_DEV_DIR, g_all_ptns[i]);
|
|
if (stat(buf, &st) < 0) {
|
|
// partition does not have _a version
|
|
continue;
|
|
}
|
|
|
|
buf[strlen(buf) - 1] = 'b';
|
|
if (stat(buf, &st) < 0) {
|
|
// partition does not have _b version
|
|
continue;
|
|
}
|
|
|
|
if (slot == 0)
|
|
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)
|
|
// Then commit the current disk
|
|
if (!partition_is_for_disk(disk, partName, devpath, sizeof(devpath))) {
|
|
if (gpt_disk_commit(disk)) {
|
|
fprintf(stderr, "%s: Failed to commit disk\n", __func__);
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
rc = gpt_disk_get_disk_info(partName, disk);
|
|
if (rc != 0) {
|
|
fprintf(stderr, "%s: Failed to get disk info for %s\n", __func__, partName);
|
|
return -1;
|
|
}
|
|
|
|
pentry = gpt_disk_get_pentry(disk, partName, PRIMARY_GPT);
|
|
pentry_bak = gpt_disk_get_pentry(disk, partName, SECONDARY_GPT);
|
|
if (!pentry || !pentry_bak) {
|
|
fprintf(stderr, "%s: Failed to get pentry/pentry_bak for %s\n", __func__,
|
|
partName);
|
|
return -1;
|
|
}
|
|
|
|
attr = pentry + AB_FLAG_OFFSET;
|
|
// LOGD("%s: got pentry for part '%s': 0x%lx (at flags: 0x%x)\n", __func__, partName,
|
|
// *(uint64_t *)pentry, *attr);
|
|
attr_bak = pentry_bak + AB_FLAG_OFFSET;
|
|
switch (ab_attr) {
|
|
case ATTR_BOOT_SUCCESSFUL:
|
|
*attr = (*attr) | AB_PARTITION_ATTR_BOOT_SUCCESSFUL;
|
|
*attr_bak = (*attr_bak) | AB_PARTITION_ATTR_BOOT_SUCCESSFUL;
|
|
break;
|
|
case ATTR_UNBOOTABLE:
|
|
*attr = (*attr) | AB_PARTITION_ATTR_UNBOOTABLE;
|
|
*attr_bak = (*attr_bak) | AB_PARTITION_ATTR_UNBOOTABLE;
|
|
break;
|
|
case ATTR_BOOTABLE:
|
|
*attr = (*attr) ^ AB_PARTITION_ATTR_UNBOOTABLE;
|
|
*attr_bak = (*attr_bak) ^ AB_PARTITION_ATTR_UNBOOTABLE;
|
|
break;
|
|
case ATTR_SLOT_ACTIVE:
|
|
*attr = (*attr) | AB_PARTITION_ATTR_SLOT_ACTIVE;
|
|
*attr_bak = (*attr) | AB_PARTITION_ATTR_SLOT_ACTIVE;
|
|
break;
|
|
default:
|
|
fprintf(stderr, "%s: Unrecognized attr\n", __func__);
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
if (gpt_disk_commit(disk)) {
|
|
fprintf(stderr, "%s: Failed to commit disk %s\n", __func__, disk->devpath);
|
|
return -1;
|
|
}
|
|
|
|
return 0;
|
|
|
|
== bootctrl_impl.c, mark_boot_successful (lines 407-441) ==
|
|
int mark_boot_successful(unsigned slot)
|
|
{
|
|
struct gpt_disk disk = { 0 };
|
|
int successful = get_boot_attr(&disk, slot, ATTR_BOOT_SUCCESSFUL);
|
|
int unbootable = get_boot_attr(&disk, slot, ATTR_UNBOOTABLE);
|
|
int ret = 0;
|
|
|
|
if (successful < 0 || unbootable < 0) {
|
|
fprintf(stderr, "SLOT %s: Failed to read attributes\n", slot_suffix_arr[slot]);
|
|
ret = -1;
|
|
goto out;
|
|
}
|
|
|
|
if (unbootable) {
|
|
printf("SLOT %s: was marked unbootable, fixing this"
|
|
" (I hope you know what you're doing...)\n",
|
|
slot_suffix_arr[slot]);
|
|
update_slot_attribute(&disk, slot, ATTR_BOOTABLE);
|
|
}
|
|
|
|
if (successful) {
|
|
fprintf(stderr, "SLOT %s: already marked successful\n", slot_suffix_arr[slot]);
|
|
goto out;
|
|
}
|
|
|
|
if (update_slot_attribute(&disk, slot, ATTR_BOOT_SUCCESSFUL)) {
|
|
fprintf(stderr, "SLOT %s: Failed to mark boot successful\n", slot_suffix_arr[slot]);
|
|
ret = -1;
|
|
goto out;
|
|
}
|
|
|
|
out:
|
|
gpt_disk_free(&disk);
|
|
return ret;
|
|
}
|
|
|
|
== gpt-utils.c, gpt_disk_commit (lines 740-800) ==
|
|
// Write the contents of struct gpt_disk back to the actual disk
|
|
int gpt_disk_commit(struct gpt_disk *disk)
|
|
{
|
|
int fd = -1;
|
|
|
|
if (!disk || (disk->is_initialized != GPT_DISK_INIT_MAGIC)) {
|
|
fprintf(stderr, "%s: Invalid args\n", __func__);
|
|
goto error;
|
|
}
|
|
|
|
if (gpt_disk_update_crc(disk)) {
|
|
fprintf(stderr, "%s: Failed to update CRC values\n", __func__);
|
|
goto error;
|
|
}
|
|
|
|
fd = open(disk->devpath, O_RDWR);
|
|
if (fd < 0) {
|
|
fprintf(stderr, "%s: Failed to open %s: %s\n", __func__, disk->devpath,
|
|
strerror(errno));
|
|
goto error;
|
|
}
|
|
|
|
LOGD("%s: Writing back primary GPT header\n", __func__);
|
|
|
|
// Write the primary header
|
|
if (gpt_set_header(disk->hdr, fd, PRIMARY_GPT) != 0) {
|
|
fprintf(stderr, "%s: Failed to update primary GPT header\n", __func__);
|
|
goto error;
|
|
}
|
|
LOGD("%s: Writing back primary partition array\n", __func__);
|
|
|
|
// Write back the primary partition array
|
|
if (gpt_set_pentry_arr(disk->hdr, fd, disk->pentry_arr)) {
|
|
fprintf(stderr, "%s: Failed to write primary GPT partition arr\n", __func__);
|
|
goto error;
|
|
}
|
|
|
|
// Write the backup header
|
|
if (gpt_set_header(disk->hdr_bak, fd, SECONDARY_GPT) != 0) {
|
|
fprintf(stderr, "%s: Failed to update backup GPT header\n", __func__);
|
|
goto error;
|
|
}
|
|
LOGD("%s: Writing back backup partition array\n", __func__);
|
|
|
|
// Write back the backup partition array
|
|
if (gpt_set_pentry_arr(disk->hdr_bak, fd, disk->pentry_arr_bak)) {
|
|
fprintf(stderr, "%s: Failed to write backup GPT partition arr\n", __func__);
|
|
goto error;
|
|
}
|
|
|
|
LOGD("%s: Done\n", __func__);
|
|
|
|
fsync(fd);
|
|
close(fd);
|
|
return 0;
|
|
|
|
error:
|
|
if (fd >= 0)
|
|
close(fd);
|
|
return -1;
|
|
}
|