Skip to content

Commit 2ee01bf

Browse files
committed
storage: Try opening the slot-suffixed partition
Some devices ship 2 copies of remote partitions (see e.g. #22), which the current code can't cope with. Add parsing of a slot suffix (via '-S', single character) and if passed, retry opening partition (via a partlabel) with it. Signed-off-by: Konrad Dybcio <[email protected]>
1 parent 586372e commit 2ee01bf

File tree

3 files changed

+58
-25
lines changed

3 files changed

+58
-25
lines changed

rmtfs.c

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
static struct rmtfs_mem *rmem;
2929
static sig_atomic_t sig_int_count;
30+
static char slot_suffix[SLOT_SUFFIX_LEN];
3031

3132
static bool dbgprintf_enabled;
3233
static void dbgprintf(const char *fmt, ...)
@@ -69,7 +70,7 @@ static void rmtfs_open(int sock, const struct qrtr_packet *pkt)
6970
goto respond;
7071
}
7172

72-
rmtfd = storage_open(pkt->node, req.path);
73+
rmtfd = storage_open(pkt->node, req.path, slot_suffix);
7374
if (!rmtfd) {
7475
qmi_result_error(&resp.result, QMI_RMTFS_ERR_INTERNAL);
7576
goto respond;
@@ -504,7 +505,7 @@ int main(int argc, char **argv)
504505
int option;
505506
const char *storage_root = NULL;
506507

507-
while ((option = getopt(argc, argv, "o:Prsv")) != -1) {
508+
while ((option = getopt(argc, argv, "oS:Prsv")) != -1) {
508509
switch (option) {
509510
/*
510511
* -o sets the directory where EFS images are stored,
@@ -535,6 +536,20 @@ int main(int argc, char **argv)
535536

536537
break;
537538

539+
/* Partlabel slot suffix on A/B devices */
540+
case 'S':
541+
if (strnlen(optarg, 1 + 1) != 1) {
542+
fprintf(stderr, "Couldn't parse slot name (too long?)\n");
543+
return -1;
544+
}
545+
546+
ret = snprintf(slot_suffix, SLOT_SUFFIX_LEN, "_%s", optarg);
547+
if (ret != SLOT_SUFFIX_LEN - 1)
548+
return -1;
549+
550+
dbgprintf("Using slot %s\n", slot_suffix);
551+
break;
552+
538553
/* -v is for verbose */
539554
case 'v':
540555
dbgprintf_enabled = 1;

rmtfs.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ ssize_t rmtfs_mem_write(struct rmtfs_mem *rmem, unsigned long phys_address, cons
2626
struct rmtfd;
2727

2828
int storage_init(const char *storage_root, bool read_only, bool use_partitions);
29-
struct rmtfd *storage_open(unsigned node, const char *path);
29+
#define SLOT_SUFFIX_LEN (2 + 1) /* "_a" or "_b", null-terminated */
30+
struct rmtfd *storage_open(unsigned node, const char *path, const char *slot_suffix);
3031
struct rmtfd *storage_get(unsigned node, int caller_id);
3132
void storage_close(struct rmtfd *rmtfd);
3233
int storage_get_caller_id(const struct rmtfd *rmtfd);

storage.c

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,43 @@ int storage_init(const char *storage_root, bool read_only, bool use_partitions)
7474
return 0;
7575
}
7676

77-
struct rmtfd *storage_open(unsigned node, const char *path)
77+
static int fd_open(struct rmtfd *rmtfd, const char *fspath, const struct partition *part)
78+
{
79+
int saved_errno;
80+
int ret;
81+
int fd;
82+
83+
if (!storage_read_only) {
84+
fd = open(fspath, O_RDWR);
85+
if (fd < 0) {
86+
saved_errno = errno;
87+
fprintf(stderr, "[storage] failed to open '%s' (requested '%s'): %s\n",
88+
fspath, part->path, strerror(saved_errno));
89+
return saved_errno;
90+
}
91+
rmtfd->fd = fd;
92+
rmtfd->shadow_len = 0;
93+
} else {
94+
ret = storage_populate_shadow_buf(rmtfd, fspath);
95+
if (ret < 0) {
96+
saved_errno = errno;
97+
fprintf(stderr, "[storage] failed to open '%s' (requested '%s'): %s\n",
98+
fspath, part->path, strerror(saved_errno));
99+
return saved_errno;
100+
}
101+
}
102+
103+
return 0;
104+
}
105+
106+
struct rmtfd *storage_open(unsigned node, const char *path, const char *slot_suffix)
78107
{
79108
char *fspath;
80109
const struct partition *part;
81110
struct rmtfd *rmtfd = NULL;
82111
const char *file;
83112
size_t pathlen;
84-
int saved_errno;
85113
int ret;
86-
int fd;
87114
int i;
88115

89116
for (part = partition_table; part->path; part++) {
@@ -119,29 +146,19 @@ struct rmtfd *storage_open(unsigned node, const char *path)
119146
else
120147
file = part->actual;
121148

122-
pathlen = strlen(storage_dir) + strlen(file) + 2;
149+
pathlen = strlen(storage_dir) + strlen(file) + 2 + strnlen(slot_suffix, SLOT_SUFFIX_LEN);
123150
fspath = alloca(pathlen);
124151
snprintf(fspath, pathlen, "%s/%s", storage_dir, file);
125-
if (!storage_read_only) {
126-
fd = open(fspath, O_RDWR);
127-
if (fd < 0) {
128-
saved_errno = errno;
129-
fprintf(stderr, "[storage] failed to open '%s' (requested '%s'): %s\n",
130-
fspath, part->path, strerror(saved_errno));
131-
errno = saved_errno;
152+
ret = fd_open(rmtfd, fspath, part);
153+
if (ret) {
154+
/* Try again with the slot suffix before giving up */
155+
if (!slot_suffix)
132156
return NULL;
133-
}
134-
rmtfd->fd = fd;
135-
rmtfd->shadow_len = 0;
136-
} else {
137-
ret = storage_populate_shadow_buf(rmtfd, fspath);
138-
if (ret < 0) {
139-
saved_errno = errno;
140-
fprintf(stderr, "[storage] failed to open '%s' (requested '%s'): %s\n",
141-
fspath, part->path, strerror(saved_errno));
142-
errno = saved_errno;
157+
158+
snprintf(fspath, pathlen, "%s/%s%s", storage_dir, file, slot_suffix);
159+
ret = fd_open(rmtfd, fspath, part);
160+
if (ret)
143161
return NULL;
144-
}
145162
}
146163

147164
rmtfd->node = node;

0 commit comments

Comments
 (0)