Skip to content

Commit

Permalink
src/emmc: use explicit GError domains and codes
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
ejoerns committed Jan 15, 2025
1 parent 7264831 commit 3e2f568
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
11 changes: 11 additions & 0 deletions include/emmc.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@
#include <glib.h>
#include <linux/types.h>

#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)

Expand Down
31 changes: 18 additions & 13 deletions src/emmc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};
Expand All @@ -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;
Expand All @@ -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 */
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
}
Expand Down

0 comments on commit 3e2f568

Please sign in to comment.