Skip to content

Commit

Permalink
bootloader: change parsing logic for Android bootloader
Browse files Browse the repository at this point in the history
Android bootloader does not require "swapped directory pattern" and it
does not differ between userspace updates and updates to kernel,
cmdline, dtb and initramfs.
  • Loading branch information
ericcurtin committed Jul 19, 2023
1 parent b258375 commit 999f177
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/libostree/ostree-bootloader-aboot.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,41 @@ _ostree_bootloader_aboot_post_bls_sync (OstreeBootloader *bootloader, int bootve
return TRUE;
}

// Parse the kernel argument ostree=
static gboolean
_ostree_bootloader_aboot_parse_bootlink (const char *bootlink, int *out_entry_bootversion,
char **out_osname, char **out_bootcsum,
int *out_treebootserial, GError **error)
{
static gsize regex_initialized;
static GRegex *regex;
if (g_once_init_enter (&regex_initialized))
{
regex = g_regex_new ("^/ostree/boot.([01])/([^/]+)/([^/]+)/([0-9]+)$", 0, 0, NULL);
g_assert (regex);
g_once_init_leave (&regex_initialized, 1);
}

g_autoptr (GMatchInfo) match = NULL;
if (!g_regex_match (regex, bootlink, 0, &match))
return glnx_throw (error,
"Invalid ostree= argument '%s', expected "
"ostree=/ostree/boot.BOOTVERSION/OSNAME/BOOTCSUM/TREESERIAL",
bootlink);

g_autofree char *bootversion_str = g_match_info_fetch (match, 1);
g_autofree char *treebootserial_str = g_match_info_fetch (match, 4);
if (out_entry_bootversion)
*out_entry_bootversion = (int)g_ascii_strtoll (bootversion_str, NULL, 10);
if (out_osname)
*out_osname = g_match_info_fetch (match, 2);
if (out_bootcsum)
*out_bootcsum = g_match_info_fetch (match, 3);
if (out_treebootserial)
*out_treebootserial = (int)g_ascii_strtoll (treebootserial_str, NULL, 10);
return TRUE;
}

static void
_ostree_bootloader_aboot_finalize (GObject *object)
{
Expand All @@ -205,6 +240,7 @@ _ostree_bootloader_aboot_bootloader_iface_init (OstreeBootloaderInterface *iface
iface->get_name = _ostree_bootloader_aboot_get_name;
iface->write_config = _ostree_bootloader_aboot_write_config;
iface->post_bls_sync = _ostree_bootloader_aboot_post_bls_sync;
iface->parse_bootlink = _ostree_bootloader_aboot_parse_bootlink;
}

void
Expand Down
41 changes: 41 additions & 0 deletions src/libostree/ostree-bootloader.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,47 @@ _ostree_bootloader_post_bls_sync (OstreeBootloader *self, int bootversion,
return TRUE;
}

gboolean
_ostree_bootloader_parse_bootlink (const char *bootlink, int *out_entry_bootversion,
char **out_osname, char **out_bootcsum, int *out_treebootserial,
GError **error)
{
g_assert (OSTREE_IS_BOOTLOADER (self));

if (OSTREE_BOOTLOADER_GET_IFACE (self)->parse_bootlink)
return OSTREE_BOOTLOADER_GET_IFACE (self)->parse_bootlink (self, bootversion, cancellable,
error);

static gsize regex_initialized;
static GRegex *regex;
if (g_once_init_enter (&regex_initialized))
{
regex = g_regex_new ("^/ostree/boot.([01])/([^/]+)/([^/]+)/([0-9]+)$", 0, 0, NULL);
g_assert (regex);
g_once_init_leave (&regex_initialized, 1);
}

g_autoptr (GMatchInfo) match = NULL;
if (!g_regex_match (regex, bootlink, 0, &match))
return glnx_throw (error,
"Invalid ostree= argument '%s', expected "
"ostree=/ostree/boot.BOOTVERSION/OSNAME/BOOTCSUM/TREESERIAL",
bootlink);

g_autofree char *bootversion_str = g_match_info_fetch (match, 1);
g_autofree char *treebootserial_str = g_match_info_fetch (match, 4);
if (out_entry_bootversion)
*out_entry_bootversion = (int)g_ascii_strtoll (bootversion_str, NULL, 10);
if (out_osname)
*out_osname = g_match_info_fetch (match, 2);
if (out_bootcsum)
*out_bootcsum = g_match_info_fetch (match, 3);
if (out_treebootserial)
*out_treebootserial = (int)g_ascii_strtoll (treebootserial_str, NULL, 10);

return TRUE;
}

gboolean
_ostree_bootloader_is_atomic (OstreeBootloader *self)
{
Expand Down
2 changes: 2 additions & 0 deletions src/libostree/ostree-bootloader.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ struct _OstreeBootloaderInterface
GCancellable *cancellable, GError **error);
gboolean (*post_bls_sync) (OstreeBootloader *self, int bootversion, GCancellable *cancellable,
GError **error);
gboolean (*parse_bootlink) (const char *bootlink, int *out_entry_bootversion, char **out_osname,
char **out_bootcsum, int *out_treebootserial, GError **error);
gboolean (*is_atomic) (OstreeBootloader *self);
};
G_DEFINE_AUTOPTR_CLEANUP_FUNC (OstreeBootloader, g_object_unref)
Expand Down

0 comments on commit 999f177

Please sign in to comment.