From 3e2f56893ab4569d107753c56236504cadf8a6ac Mon Sep 17 00:00:00 2001 From: Enrico Joerns Date: Wed, 15 Jan 2025 08:43:53 +0100 Subject: [PATCH] src/emmc: use explicit GError domains and codes So far, emmc leveraged the error domain from update_handler.c, which is imprecise and violates the scope here. Fix this by replacing actual file errors by G_FILE_ERROR and introduce an explicit R_EMMC_ERROR domain with meaningful codes. Signed-off-by: Enrico Joerns --- include/emmc.h | 11 +++++++++++ src/emmc.c | 31 ++++++++++++++++++------------- 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/include/emmc.h b/include/emmc.h index 6aa8735f1..5d80481a7 100644 --- a/include/emmc.h +++ b/include/emmc.h @@ -3,6 +3,17 @@ #include #include +#define R_EMMC_ERROR r_emmc_error_quark() +GQuark r_emmc_error_quark(void); + +typedef enum { + R_EMMC_ERROR_FAILED, + R_EMMC_ERROR_IOCTL, + R_EMMC_ERROR_BOOTPART_NOT_ENABLED, + R_EMMC_ERROR_BOOTPART_UDA, + R_EMMC_ERROR_BOOTPART_INVALID, +} REmmcError; + #define EMMC_BOOT_PARTITIONS 2 #define INACTIVE_BOOT_PARTITION(part_active) ((part_active + 1) % EMMC_BOOT_PARTITIONS) diff --git a/src/emmc.c b/src/emmc.c index 88291a336..3d367a232 100644 --- a/src/emmc.c +++ b/src/emmc.c @@ -14,6 +14,11 @@ #include "update_handler.h" #include "utils.h" +GQuark r_emmc_error_quark(void) +{ + return g_quark_from_static_string("r_emmc_error_quark"); +} + static int r_emmc_read_extcsd(int fd, guint8 extcsd[512]) { struct mmc_ioc_cmd cmd = {}; @@ -39,13 +44,13 @@ gboolean r_emmc_read_bootpart(const gchar *device, gint *bootpart_active, GError fd = g_open(device, O_RDONLY); if (fd == -1) { int err = errno; - g_set_error(error, R_UPDATE_ERROR, R_UPDATE_ERROR_FAILED, + g_set_error(error, G_FILE_ERROR, g_file_error_from_errno(err), "opening eMMC device failed: %s", g_strerror(err)); return FALSE; } if (r_emmc_read_extcsd(fd, extcsd)) { - g_set_error(error, R_UPDATE_ERROR, R_UPDATE_ERROR_FAILED, + g_set_error(error, R_EMMC_ERROR, R_EMMC_ERROR_IOCTL, "Could not read from extcsd register %d in %s", EXT_CSD_PART_CONFIG, device); return FALSE; @@ -57,8 +62,8 @@ gboolean r_emmc_read_bootpart(const gchar *device, gint *bootpart_active, GError switch (active_partition) { case 0x0: g_set_error(error, - R_UPDATE_ERROR, - R_UPDATE_ERROR_FAILED, + R_EMMC_ERROR, + R_EMMC_ERROR_BOOTPART_NOT_ENABLED, "eMMC device is not enabled for booting."); return FALSE; case 0x1: /* boot0 / boot 1 */ @@ -69,14 +74,14 @@ gboolean r_emmc_read_bootpart(const gchar *device, gint *bootpart_active, GError return TRUE; case 0x7: /* user data area */ g_set_error(error, - R_UPDATE_ERROR, - R_UPDATE_ERROR_FAILED, + R_EMMC_ERROR, + R_EMMC_ERROR_BOOTPART_UDA, "Active eMMC partition is UDA when boot0/boot1 was expected."); return FALSE; default: g_set_error(error, - R_UPDATE_ERROR, - R_UPDATE_ERROR_FAILED, + R_EMMC_ERROR, + R_EMMC_ERROR_BOOTPART_INVALID, "Invalid (Reserved) eMMC boot part number."); return FALSE; } @@ -110,13 +115,13 @@ gboolean r_emmc_write_bootpart(const gchar *device, gint bootpart_active, GError fd = g_open(device, O_RDWR | O_EXCL); if (fd == -1) { int err = errno; - g_set_error(error, R_UPDATE_ERROR, R_UPDATE_ERROR_FAILED, + g_set_error(error, G_FILE_ERROR, g_file_error_from_errno(err), "opening eMMC device failed: %s", g_strerror(err)); return FALSE; } if (r_emmc_read_extcsd(fd, extcsd)) { - g_set_error(error, R_UPDATE_ERROR, R_UPDATE_ERROR_FAILED, + g_set_error(error, R_EMMC_ERROR, R_EMMC_ERROR_IOCTL, "Could not read from extcsd register %d in %s", EXT_CSD_PART_CONFIG, device); return FALSE; @@ -136,7 +141,7 @@ gboolean r_emmc_write_bootpart(const gchar *device, gint bootpart_active, GError value |= 0x10; if (r_emmc_write_extcsd(fd, EXT_CSD_PART_CONFIG, value)) { - g_set_error(error, R_UPDATE_ERROR, R_UPDATE_ERROR_FAILED, + g_set_error(error, R_EMMC_ERROR, R_EMMC_ERROR_IOCTL, "Could not write 0x%02x to extcsd register %d in %s", value, EXT_CSD_PART_CONFIG, device); return FALSE; @@ -158,14 +163,14 @@ static gboolean r_emmc_force_part_write(const gchar *device, gchar value, GError f = g_fopen(sysfs_path, "w"); if (!f) { int err = errno; - g_set_error(error, R_UPDATE_ERROR, R_UPDATE_ERROR_FAILED, + g_set_error(error, G_FILE_ERROR, g_file_error_from_errno(err), "Could not open device attribute %s: %s", sysfs_path, g_strerror(err)); goto out; } if (fwrite(&value, 1, 1, f) != 1) { - g_set_error(error, R_UPDATE_ERROR, R_UPDATE_ERROR_FAILED, + g_set_error(error, G_FILE_ERROR, G_FILE_ERROR_FAILED, "Could not write to %s", sysfs_path); goto out; }