Coverage Report

Created: 2026-11-11 06:07

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/rauc/src/emmc.c
Line
Count
Source
1
#include <config.h>
2
3
#include <errno.h>
4
#include <fcntl.h>
5
#include <glib/gstdio.h>
6
#include <linux/major.h>
7
#include <linux/types.h> /* kernel < 3.4 forgot that in mmc/ioctl.h */
8
#include <linux/mmc/ioctl.h>
9
#include <string.h>
10
#include <sys/ioctl.h>
11
#include <unistd.h>
12
13
#include "emmc.h"
14
#include "update_handler.h"
15
#include "utils.h"
16
17
GQuark r_emmc_error_quark(void)
18
0
{
19
0
  return g_quark_from_static_string("r_emmc_error_quark");
20
0
}
21
22
static int r_emmc_read_extcsd(int fd, guint8 extcsd[512])
23
0
{
24
0
  struct mmc_ioc_cmd cmd = {
25
0
    .write_flag = 0,
26
0
    .opcode = MMC_SEND_EXT_CSD,
27
0
    .arg = 0,
28
0
    .flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC,
29
0
    .blksz = 512,
30
0
    .blocks = 1,
31
0
  };
32
0
  mmc_ioc_cmd_set_data(cmd, extcsd);
33
34
0
  return ioctl(fd, MMC_IOC_CMD, &cmd);
35
0
}
36
37
gboolean r_emmc_read_bootpart(const gchar *device, gint *bootpart_active, GError **error)
38
0
{
39
0
  guint8 extcsd[512] = {};
40
0
  g_auto(filedesc) fd = -1;
41
  /* count from 1 */
42
0
  gint active_partition = -1;
43
44
0
  fd = g_open(device, O_RDONLY);
45
0
  if (fd == -1) {
46
0
    int err = errno;
47
0
    g_set_error(error, G_FILE_ERROR, g_file_error_from_errno(err),
48
0
        "opening eMMC device failed: %s", g_strerror(err));
49
0
    return FALSE;
50
0
  }
51
52
0
  if (r_emmc_read_extcsd(fd, extcsd)) {
53
0
    g_set_error(error, R_EMMC_ERROR, R_EMMC_ERROR_IOCTL,
54
0
        "Could not read from extcsd register %d in %s",
55
0
        EXT_CSD_PART_CONFIG, device);
56
0
    return FALSE;
57
0
  }
58
59
  /* retrieve active partition from BOOT_PART register */
60
0
  active_partition = (extcsd[EXT_CSD_PART_CONFIG] & 0x38) >> 3;
61
62
0
  switch (active_partition) {
63
0
    case 0x0: /* not boot enabled */
64
0
      *bootpart_active = -1;
65
0
      return TRUE;
66
0
    case 0x1: /* boot0 / boot 1 */
67
    /* fallthgrough */
68
0
    case 0x2:
69
      /* return the partitions counted from 0 */
70
0
      *bootpart_active = active_partition - 1;
71
0
      return TRUE;
72
0
    case 0x7: /* user data area */
73
0
      g_set_error(error,
74
0
          R_EMMC_ERROR,
75
0
          R_EMMC_ERROR_BOOTPART_UDA,
76
0
          "Active eMMC partition is UDA when boot0/boot1 was expected.");
77
0
      return FALSE;
78
0
    default:
79
0
      g_set_error(error,
80
0
          R_EMMC_ERROR,
81
0
          R_EMMC_ERROR_BOOTPART_INVALID,
82
0
          "Invalid (Reserved) eMMC boot part number.");
83
0
      return FALSE;
84
0
  }
85
86
0
  g_return_val_if_reached(FALSE);
87
0
}
88
89
static gint r_emmc_write_extcsd(int fd, guint8 index, guint8 value)
90
0
{
91
0
  struct mmc_ioc_cmd cmd = {
92
0
    .write_flag = 1,
93
0
    .opcode = MMC_SWITCH,
94
0
    .arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) | (index << 16) |
95
0
           (value << 8) | EXT_CSD_CMD_SET_NORMAL,
96
0
    .flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC,
97
0
  };
98
99
0
  return ioctl(fd, MMC_IOC_CMD, &cmd);
100
0
}
101
102
gboolean r_emmc_write_bootpart(const gchar *device, gint bootpart_active, GError **error)
103
0
{
104
0
  g_auto(filedesc) fd = -1;
105
0
  guint8 extcsd[512] = {};
106
0
  guint8 value = 0;
107
108
0
  g_return_val_if_fail(bootpart_active == 0 || bootpart_active == 1, FALSE);
109
110
0
  fd = g_open(device, O_RDWR | O_EXCL);
111
0
  if (fd == -1) {
112
0
    int err = errno;
113
0
    g_set_error(error, G_FILE_ERROR, g_file_error_from_errno(err),
114
0
        "opening eMMC device failed: %s", g_strerror(err));
115
0
    return FALSE;
116
0
  }
117
118
0
  if (r_emmc_read_extcsd(fd, extcsd)) {
119
0
    g_set_error(error, R_EMMC_ERROR, R_EMMC_ERROR_IOCTL,
120
0
        "Could not read from extcsd register %d in %s",
121
0
        EXT_CSD_PART_CONFIG, device);
122
0
    return FALSE;
123
0
  }
124
125
  /* Keep BOOT_ACK value as it is. Resetting this bit might prevent
126
   * proper boot process on some platforms since in some SoCs, ROM boot
127
   * loaders need this flag to be set.
128
   * The PARTITION_ACCESS value is handled inside the kernel so we do not
129
   * need to take care of it here. */
130
0
  value = extcsd[EXT_CSD_PART_CONFIG] & (1 << 6);
131
132
  /* write [5:3] : BOOT_PARTITION_ENABLE of PARTITION_CONFIG */
133
0
  if (bootpart_active == 0)
134
0
    value |= 0x08;
135
0
  else if (bootpart_active == 1)
136
0
    value |= 0x10;
137
138
0
  if (r_emmc_write_extcsd(fd, EXT_CSD_PART_CONFIG, value)) {
139
0
    g_set_error(error, R_EMMC_ERROR, R_EMMC_ERROR_IOCTL,
140
0
        "Could not write 0x%02x to extcsd register %d in %s",
141
0
        value, EXT_CSD_PART_CONFIG, device);
142
0
    return FALSE;
143
0
  }
144
145
0
  return TRUE;
146
0
}
147
148
static gboolean r_emmc_force_part_write(const gchar *device, gchar value, GError **error)
149
0
{
150
0
  gboolean ret = FALSE;
151
0
  g_autofree gchar *device_basename = g_path_get_basename(device);
152
0
  g_autofree gchar *sysfs_path = NULL;
153
0
  FILE *f = NULL;
154
155
0
  g_return_val_if_fail(value == '0' || value == '1', FALSE);
156
157
0
  sysfs_path = g_strdup_printf("/sys/block/%s/force_ro", device_basename);
158
0
  f = g_fopen(sysfs_path, "w");
159
0
  if (!f) {
160
0
    int err = errno;
161
0
    g_set_error(error, G_FILE_ERROR, g_file_error_from_errno(err),
162
0
        "Could not open device attribute %s: %s",
163
0
        sysfs_path, g_strerror(err));
164
0
    goto out;
165
0
  }
166
167
0
  if (fwrite(&value, 1, 1, f) != 1) {
168
0
    g_set_error(error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
169
0
        "Could not write to %s", sysfs_path);
170
0
    goto out;
171
0
  }
172
173
0
  ret = TRUE;
174
175
0
out:
176
  /* the buffered write to sysfs only happens when closing */
177
0
  if (f && fclose(f) != 0 && ret) {
178
0
    int err = errno;
179
0
    g_set_error(error, G_FILE_ERROR, g_file_error_from_errno(err),
180
0
        "Could not write to %s: %s",
181
0
        sysfs_path, g_strerror(err));
182
0
    ret = FALSE;
183
0
  }
184
185
0
  return ret;
186
0
}
187
188
gboolean r_emmc_force_part_ro(const gchar *device, GError **error)
189
0
{
190
0
  gboolean ret = FALSE;
191
0
  GError *ierror = NULL;
192
193
0
  ret = r_emmc_force_part_write(device, '1', &ierror);
194
0
  if (!ret)
195
0
    g_propagate_prefixed_error(error, ierror,
196
0
        "failed forcing ro: ");
197
198
0
  return ret;
199
0
}
200
201
gboolean r_emmc_force_part_rw(const gchar *device, GError **error)
202
0
{
203
0
  gboolean ret = FALSE;
204
0
  GError *ierror = NULL;
205
206
0
  ret = r_emmc_force_part_write(device, '0', &ierror);
207
0
  if (!ret)
208
0
    g_propagate_prefixed_error(error, ierror,
209
0
        "failed forcing rw: ");
210
211
0
  return ret;
212
0
}