31 Commits

Author SHA1 Message Date
Caleb Connolly
39a6e6daaf commit disk before free
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>
0.2.2
2025-03-24 23:31:41 +01:00
Caleb Connolly
e1d7c370bc properly error if run on a non-A/B device
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>
2025-03-18 14:10:52 +00:00
Arnaud Ferraris
413ee35eda bootctrl: fall back to active slot if current slot not found in cmdline
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`.
0.2.1
2025-03-18 13:40:57 +00:00
Guido Günther
d30d9dfc03 gpt-utils: Open block device read only when obtaining info
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.
2025-02-11 23:41:55 +00:00
Caleb Connolly
b8d2248914 add a flag to continue when ufs bsg device is missing
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>
0.2.0
2023-09-27 18:56:43 +01:00
Caleb Connolly
3390f642e3 meson.build: use gnu11 std instead of c11
This fixes a few compilation issues on different platforms (yay C
standards)

Signed-off-by: Caleb Connolly <caleb@connolly.tech>
2023-09-27 18:43:46 +01:00
Caleb Connolly
74fcde7a8e header fixes 2023-06-26 14:44:24 +01:00
Caleb Connolly
ea34c3f0b9 drop test stub, fix licenses 2023-06-24 01:38:14 +01:00
Caleb Connolly
e9e86efb96 even even more rework, drop zlib dep
Also fix the mark_boot_successful feature which I broke earlier
2023-06-24 01:04:15 +01:00
Caleb Connolly
bca9aa5dd7 more rework, cleanup, port to pure C
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.
2023-06-24 00:06:02 +01:00
Caleb Connolly
04f4ac81ea rework, fixup, cleanup
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.
2023-06-23 21:53:30 +01:00
Caleb Connolly
7f852512b6 write out the backup header too
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)
2023-06-23 17:23:56 +01:00
Caleb Connolly
7a5d5ee40e make disk->hdr_bak actually be the GPT backup header
I do not know who thought this should be a "backup" copy of the primary
GPT header...
2023-06-23 17:22:39 +01:00
Caleb Connolly
75e30f8d09 Revert "Use scope-based memory management for malloc, free, open, close, etc."
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.
2023-06-23 13:30:08 +01:00
Eric Curtin
20572c1417 Fix segfault originating in get_suffix function
Some callers try to print the return value from here, printf %s NULL,
ends up being a segfault.
2023-05-29 01:27:55 +00:00
Eric Curtin
9d7600df51 Use scope-based memory management for malloc, free, open, close, etc.
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.
2023-05-29 01:26:29 +00:00
Eric Curtin
843aa92266 Change hdr_offset to signed value, so lseek64 return can be checked
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.
2023-05-15 20:28:54 +01:00
Caleb Connolly
e705c38010 add missing linux/limits.h
Needed for PATH_MAX
2023-05-15 20:26:28 +01:00
Caleb Connolly
1752506e75 gitignore: compile_commands.json 2023-05-15 20:25:59 +01:00
alefnode
df63d7c21c Fix to << uint32_t has not been declared >> 2023-05-04 09:15:29 +02:00
Caleb Connolly
77b48f092a Merge branch 'emmc' into 'main'
Support eMMC-backed A/B

See merge request sdm845-mainline/qbootctl!1
0.1.2
2022-09-21 16:25:40 +00:00
Richard Acayan
17ee1b8e65 detect and handle emmc-backed xbl partitions when switching slots 2022-08-31 17:49:18 -04:00
Richard Acayan
c1f24adfb6 properly remove the partition number and separator 2022-08-31 17:10:31 -04:00
Caleb Connolly
50ef0328af minor cleanups, improve error handling
gracefully handle UFS_BSG not being enabled in the kernel, rather than
putting the device into a semi-bricked state (oops).
0.1.1
2022-06-10 19:18:13 +01:00
Caleb Connolly
487a96f663 gpt-utils: drop unused function
it also called strlcpy which only exists on musl and bionic, dropping it
lets this build on glibc too.
2022-06-05 22:30:34 +01:00