When auto-freeing a disk, be sure to commit pending changes first.
fixes setActiveBootslot() since the GPT refactor
Signed-off-by: Caleb Connolly <caleb.connolly@linaro.org>
Detect the case where there are no A/B partitions and print an error
about it rather than erroneously continuing execution.
Signed-off-by: Caleb Connolly <caleb.connolly@linaro.org>
While postmarketOS makes use of a kernel cmdline argument, this isn't
the case for all distros. Furthermore, this argument isn't present in
those added automatically by the Android bootloader.
In order to still be able to use `qbootctl` on other distros, this
change ensures we use the currently active slot as the current slot when
the cmdline argument is missing. This ensure `qbootctl -m` picks up the
correct slot more often than not, instead of always defaulting to `_a`.
Otherwise reading information like `qbootctl -a` triggers removal and
readdition of all partitions like:
```
KERNEL[73264.679695] remove /devices/platform/soc@0/1d84000.ufshc/host0/target0:0:0/0:0:0:4/block/sde/sde11 (block)
KERNEL[73264.748249] add /devices/platform/soc@0/1d84000.ufshc/host0/target0:0:0/0:0:0:4/block/sde/sde11 (block)
KERNEL[73264.863286] remove /devices/platform/soc@0/1d84000.ufshc/host0/target0:0:0/0:0:0:4/block/sde/sde11 (block)
KERNEL[73264.917664] add /devices/platform/soc@0/1d84000.ufshc/host0/target0:0:0/0:0:0:4/block/sde/sde11 (block)
UDEV [73264.980859] remove /devices/platform/soc@0/1d84000.ufshc/host0/target0:0:0/0:0:0:4/block/sde/sde11 (block)
UDEV [73265.150742] add /devices/platform/soc@0/1d84000.ufshc/host0/target0:0:0/0:0:0:4/block/sde/sde11 (block)
UDEV [73265.276134] remove /devices/platform/soc@0/1d84000.ufshc/host0/target0:0:0/0:0:0:4/block/sde/sde11 (block)
UDEV [73265.492442] add /devices/platform/soc@0/1d84000.ufshc/host0/target0:0:0/0:0:0:4/block/sde/sde11 (block)
…[the same for all other partitions on sde]…
```
leading to wasted CPU cycles (and battery) but also making programms
that run right after that command and accessing one of the partitions
fail sporadically. This can be bad if those commands are e.g. doing a
```
cat /dev/disk/by-partlabel/boot_a
```
to backup the current boot partition.
For some usecases it is desired to still update the GPT headers even if
the UFS BSG device is missing. Add a new flag which implements this
behaviour.
It isn't made the default because on some platforms this could result in
an unbootable device, there it's preferable to defer to the user by just
bailing out if the UFS BSG device is unavailable.
Signed-off-by: Caleb Connolly <caleb@connolly.tech>
The only C++ was for handling discovering and iterating over the
partitions PER block device, this was implemented in a really
overcomplicated way.
Simplify things, port everything over to c11 and drop the libstdc++
requirement entirely.
Allocate gpt_disk on stack, allocate when needed, rather than multiple
times for every partition.
Huge code cleanup, rerun clang-format, etc
Many changes here inspired by Eric's earlier work.
Now that we actually have the real backup header, we can safely write it
out without having the wrong LBA pointers and other such issues that
would come from writing the primary header to the backup header (ask me
how I know)
This commit introduced a lot of changes and seems to have caused a few
issues, including breaking crc32 generation. Revert it for now with the
intention to reimplement some of the improvements.
This reverts commit 9d7600df51.
If we are using C++, we might as well use scope-based memory management
for these things, cleaner and less error prone. Also removed
unnecessary gotos.
The lines:
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;
}
are error checked. But previously hdr_offset could never be less than
zero because it was unsigned.