611 lines
16 KiB
C
611 lines
16 KiB
C
// SPDX-License-Identifier: GPL-2.0-only
|
|
/*
|
|
* qseecom-supplicant: what Qualcomm QSEE trusted apps call back into, for
|
|
* Linux's QSEECOM TEE driver, as qseecomd does on Android.
|
|
*
|
|
* It serves their listeners: the file system (10), the GP file system where
|
|
* they keep sealed objects (28672), and RPMB (8192), which Gatekeeper in
|
|
* keymaster64 keeps its state in. Their Android paths are served from below
|
|
* a root directory. It also loads the apps named on its command line, from a
|
|
* firmware directory, and keeps them loaded while it runs.
|
|
*
|
|
* qseecom-supplicant <root> <firmware dir> [app...]
|
|
*
|
|
* The listeners and the RPMB bridge come from gfharness in
|
|
* wrobelda/goodix-fp-spi-linux.
|
|
*/
|
|
#define _GNU_SOURCE
|
|
#include <dirent.h>
|
|
#include <errno.h>
|
|
#include <fcntl.h>
|
|
#include <pthread.h>
|
|
#include <stdint.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/ioctl.h>
|
|
#include <sys/mman.h>
|
|
#include <sys/stat.h>
|
|
#include <unistd.h>
|
|
|
|
#include <linux/bsg.h>
|
|
#include <linux/tee.h>
|
|
#include <scsi/sg.h>
|
|
|
|
#define TEE_IMPL_ID_QSEECOM 5
|
|
|
|
#define LOG(...) fprintf(stderr, __VA_ARGS__)
|
|
|
|
/* Where the apps' Android paths are served from */
|
|
static const char *fs_root;
|
|
|
|
/* ---- TEE ---- */
|
|
|
|
struct shm {
|
|
int id;
|
|
uint8_t *va;
|
|
size_t size;
|
|
};
|
|
|
|
static int shm_alloc(int fd, size_t size, struct shm *s)
|
|
{
|
|
struct tee_ioctl_shm_alloc_data d = { .size = size };
|
|
int sfd = ioctl(fd, TEE_IOC_SHM_ALLOC, &d);
|
|
|
|
if (sfd < 0)
|
|
return -1;
|
|
s->va = mmap(NULL, d.size, PROT_READ | PROT_WRITE, MAP_SHARED, sfd, 0);
|
|
close(sfd);
|
|
if (s->va == MAP_FAILED)
|
|
return -1;
|
|
s->id = d.id;
|
|
s->size = d.size;
|
|
memset(s->va, 0, s->size);
|
|
return 0;
|
|
}
|
|
|
|
/* The QSEECOM client (teeN) or supplicant (teeprivN) device, by impl id. */
|
|
static int tee_open(const char *base)
|
|
{
|
|
struct tee_ioctl_version_data v;
|
|
char path[32];
|
|
|
|
for (int i = 0; i < 8; i++) {
|
|
int fd;
|
|
|
|
snprintf(path, sizeof(path), "/dev/%s%d", base, i);
|
|
fd = open(path, O_RDWR | O_CLOEXEC);
|
|
if (fd < 0)
|
|
continue;
|
|
if (!ioctl(fd, TEE_IOC_VERSION, &v) &&
|
|
v.impl_id == TEE_IMPL_ID_QSEECOM)
|
|
return fd;
|
|
close(fd);
|
|
}
|
|
LOG("no QSEECOM %s device\n", base);
|
|
return -1;
|
|
}
|
|
|
|
/*
|
|
* Open a session on a trusted app by name. On the supplicant device this
|
|
* loads the app, which stays loaded while the session is open.
|
|
*/
|
|
static int tee_session(int fd, const char *app)
|
|
{
|
|
struct {
|
|
struct tee_ioctl_open_session_arg arg;
|
|
struct tee_ioctl_param params[1];
|
|
} s = {};
|
|
struct tee_ioctl_buf_data bd = { (uintptr_t)&s, sizeof(s) };
|
|
struct shm name;
|
|
|
|
if (shm_alloc(fd, 64, &name))
|
|
return -1;
|
|
strcpy((char *)name.va, app);
|
|
s.arg.num_params = 1;
|
|
s.params[0].attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT;
|
|
s.params[0].b = strlen(app) + 1;
|
|
s.params[0].c = name.id;
|
|
if (ioctl(fd, TEE_IOC_OPEN_SESSION, &bd) || s.arg.ret) {
|
|
LOG("open session on %s: %s (ret 0x%x)\n", app,
|
|
strerror(errno), s.arg.ret);
|
|
return -1;
|
|
}
|
|
return s.arg.session;
|
|
}
|
|
|
|
/* ---- Listeners the TA calls back into ---- */
|
|
|
|
static int fs_last_errno;
|
|
|
|
static void fs_path(char *out, size_t n, const char *req)
|
|
{
|
|
snprintf(out, n, "%s%s%s", fs_root, req[0] == '/' ? "" : "/", req);
|
|
}
|
|
|
|
static void mkparents(const char *path)
|
|
{
|
|
char tmp[512];
|
|
|
|
snprintf(tmp, sizeof(tmp), "%s", path);
|
|
for (char *p = tmp + 1; *p; p++) {
|
|
if (*p != '/')
|
|
continue;
|
|
*p = '\0';
|
|
mkdir(tmp, 0700);
|
|
*p = '/';
|
|
}
|
|
}
|
|
|
|
/*
|
|
* Listener 10, the file system. A request carries an op at +0 and either a
|
|
* path at +4 (flags or mode at +260) or an fd at +4 (count or offset at +8,
|
|
* whence at +12, write count at +20008). The reply is {op, result} at the
|
|
* head of the buffer; a read puts its data at +4 and its count at +20004.
|
|
* Failures return -1, and the errno is fetched with its own op.
|
|
*/
|
|
static void fs_reply(uint8_t *b, uint32_t op, int32_t ret)
|
|
{
|
|
if (ret < 0)
|
|
fs_last_errno = errno;
|
|
memcpy(b, &op, 4);
|
|
memcpy(b + 4, &ret, 4);
|
|
}
|
|
|
|
static int fs_serve(uint8_t *b)
|
|
{
|
|
uint32_t op = *(uint32_t *)b, n;
|
|
int32_t fd = *(int32_t *)(b + 4);
|
|
char path[512] = "", path2[512];
|
|
ssize_t r;
|
|
|
|
/* Only these carry a path; for the others +4 on is an fd and data */
|
|
if (op == 0x202 || op == 0x206 || op == 0x20c || op == 0x210 ||
|
|
op == 0x217) {
|
|
b[4 + 255] = '\0';
|
|
fs_path(path, sizeof(path), (const char *)b + 4);
|
|
}
|
|
|
|
switch (op) {
|
|
case 0x202: /* open */
|
|
if (*(uint32_t *)(b + 260) & O_CREAT)
|
|
mkparents(path);
|
|
fs_reply(b, op, open(path, *(uint32_t *)(b + 260) | O_CLOEXEC,
|
|
0600));
|
|
break;
|
|
case 0x206: /* creat */
|
|
mkparents(path);
|
|
fs_reply(b, op, open(path, O_CREAT | O_WRONLY | O_TRUNC |
|
|
O_CLOEXEC, 0600));
|
|
break;
|
|
case 0x207: /* read */
|
|
n = *(uint32_t *)(b + 8);
|
|
r = read(fd, b + 4, n > 20000 ? 20000 : n);
|
|
if (r < 0)
|
|
fs_last_errno = errno;
|
|
memcpy(b, &op, 4);
|
|
*(int32_t *)(b + 20004) = r;
|
|
break;
|
|
case 0x208: /* write */
|
|
n = *(uint32_t *)(b + 20008);
|
|
fs_reply(b, op, write(fd, b + 8, n > 20000 ? 20000 : n));
|
|
break;
|
|
case 0x209: /* close */
|
|
fs_reply(b, op, close(fd));
|
|
break;
|
|
case 0x20a: /* lseek */
|
|
fs_reply(b, op, lseek(fd, *(int32_t *)(b + 8),
|
|
*(uint32_t *)(b + 12)));
|
|
break;
|
|
case 0x216: /* fsync */
|
|
fs_reply(b, op, fsync(fd));
|
|
break;
|
|
case 0x20c: /* unlink */
|
|
fs_reply(b, op, unlink(path));
|
|
break;
|
|
case 0x210: /* mkdir */
|
|
mkparents(path);
|
|
r = mkdir(path, 0700);
|
|
fs_reply(b, op, r && errno == EEXIST ? 0 : r);
|
|
break;
|
|
case 0x217: /* rename */
|
|
b[260 + 255] = '\0';
|
|
fs_path(path2, sizeof(path2), (const char *)b + 260);
|
|
fs_reply(b, op, rename(path, path2));
|
|
break;
|
|
case 0x21c: /* the last errno */
|
|
fs_reply(b, op, fs_last_errno);
|
|
break;
|
|
case 0x21d: /* shutdown */
|
|
fs_reply(b, op, 0);
|
|
break;
|
|
default:
|
|
LOG("fs: unhandled op 0x%x\n", op);
|
|
errno = ENOSYS;
|
|
fs_reply(b, op, -1);
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
/*
|
|
* Listener 28672, the GP file system, where the TA keeps its sealed objects.
|
|
* op % 4 picks read, write, delete or rename; op 12 asks the version. Request:
|
|
* op +0, name +4, offset (or the new name) +0x104, length +0x108, keep a
|
|
* backup +0x10c, data +0x110. Reply: op +0, errno or 0 +4, count +8, data +12.
|
|
*/
|
|
#define GP_READ_MAX 512000
|
|
|
|
static void copy_file(const char *from, const char *to)
|
|
{
|
|
char buf[8192];
|
|
int in = open(from, O_RDONLY | O_CLOEXEC), out;
|
|
ssize_t n;
|
|
|
|
if (in < 0)
|
|
return;
|
|
out = open(to, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0600);
|
|
while (out >= 0 && (n = read(in, buf, sizeof(buf))) > 0)
|
|
if (write(out, buf, n) != n)
|
|
break;
|
|
close(in);
|
|
if (out >= 0)
|
|
close(out);
|
|
}
|
|
|
|
static int gpfs_serve(uint8_t *b)
|
|
{
|
|
uint32_t op = *(uint32_t *)b, len = *(uint32_t *)(b + 0x108);
|
|
int32_t off = *(int32_t *)(b + 0x104), ret = 0;
|
|
char path[512], path2[512];
|
|
ssize_t n = 0;
|
|
int fd;
|
|
|
|
b[4 + 255] = '\0';
|
|
fs_path(path, sizeof(path), (const char *)b + 4);
|
|
if (len > GP_READ_MAX)
|
|
len = GP_READ_MAX;
|
|
|
|
if (op == 12) {
|
|
ret = 2;
|
|
} else if (op > 12) {
|
|
ret = -1;
|
|
} else if (op % 4 == 0) {
|
|
fd = open(path, O_RDONLY | O_CLOEXEC);
|
|
n = fd < 0 ? -1 : pread(fd, b + 12, len, off);
|
|
if (fd >= 0)
|
|
close(fd);
|
|
} else if (op % 4 == 1) {
|
|
char bak[600];
|
|
|
|
mkparents(path);
|
|
if (*(uint32_t *)(b + 0x10c)) {
|
|
snprintf(bak, sizeof(bak), "%s.bak", path);
|
|
copy_file(path, bak);
|
|
}
|
|
fd = open(path, O_WRONLY | O_CREAT | O_CLOEXEC, 0600);
|
|
n = fd < 0 ? -1 : pwrite(fd, b + 0x110, len, off);
|
|
if (n > 0 && off == 0 && ftruncate(fd, n))
|
|
n = -1;
|
|
if (fd >= 0)
|
|
close(fd);
|
|
} else if (op % 4 == 2) {
|
|
if (unlink(path) && (errno != EISDIR || rmdir(path)))
|
|
ret = errno;
|
|
} else {
|
|
b[0x104 + 255] = '\0';
|
|
fs_path(path2, sizeof(path2), (const char *)b + 0x104);
|
|
mkparents(path2);
|
|
if (rename(path, path2))
|
|
ret = errno;
|
|
}
|
|
if (n < 0) {
|
|
ret = errno;
|
|
n = 0;
|
|
}
|
|
memcpy(b, &op, 4);
|
|
memcpy(b + 4, &ret, 4);
|
|
*(uint32_t *)(b + 8) = n;
|
|
return 0;
|
|
}
|
|
|
|
/*
|
|
* Listener 8192, RPMB. Gatekeeper (in keymaster64) keeps its password handles
|
|
* and its rollback counter in the UFS Replay Protected Memory Block. The
|
|
* secure world MACs every frame; this only moves 512-byte frames between it
|
|
* and the RPMB well-known LUN with SCSI SECURITY PROTOCOL IN/OUT, as
|
|
* Qualcomm's librpmbservice does. Each command is answered in place, with
|
|
* the status in its response struct.
|
|
*/
|
|
#define RPMB_BLK 512
|
|
|
|
struct __attribute__((packed)) rpmb_init_res {
|
|
uint32_t cmd_id, version;
|
|
int32_t status;
|
|
uint32_t num_sectors, rel_wr_count, dev_type, reserved[4];
|
|
};
|
|
struct __attribute__((packed)) rpmb_rw_req {
|
|
uint32_t cmd_id, num_sectors, req_buff_len, req_buff_offset, version,
|
|
rel_wr_count;
|
|
};
|
|
struct __attribute__((packed)) rpmb_rw_res {
|
|
uint32_t cmd_id;
|
|
int32_t status;
|
|
uint32_t res_buff_len, res_buff_offset, version;
|
|
};
|
|
|
|
static int rpmb_fd = -1;
|
|
|
|
static int rpmb_open(void)
|
|
{
|
|
struct dirent *e;
|
|
char path[300];
|
|
DIR *d;
|
|
|
|
if (rpmb_fd >= 0)
|
|
return 0;
|
|
d = opendir("/dev/bsg");
|
|
while (d && (e = readdir(d))) {
|
|
size_t l = strlen(e->d_name);
|
|
|
|
if (l > 6 && !strcmp(e->d_name + l - 6, ":49476")) {
|
|
snprintf(path, sizeof(path), "/dev/bsg/%s", e->d_name);
|
|
rpmb_fd = open(path, O_RDWR | O_SYNC | O_CLOEXEC);
|
|
break;
|
|
}
|
|
}
|
|
if (d)
|
|
closedir(d);
|
|
if (rpmb_fd < 0)
|
|
LOG("rpmb: no usable UFS RPMB LUN\n");
|
|
return rpmb_fd < 0 ? -1 : 0;
|
|
}
|
|
|
|
/* One SCSI command on the RPMB LUN, retried once on a UNIT ATTENTION. */
|
|
static int rpmb_scsi(uint8_t *cdb, int cdb_len, void *buf, uint32_t len,
|
|
int out)
|
|
{
|
|
uint8_t sense[96];
|
|
struct sg_io_v4 io = {
|
|
.guard = 'Q',
|
|
.protocol = BSG_PROTOCOL_SCSI,
|
|
.subprotocol = BSG_SUB_PROTOCOL_SCSI_CMD,
|
|
.request_len = cdb_len,
|
|
.request = (uintptr_t)cdb,
|
|
.response = (uintptr_t)sense,
|
|
.max_response_len = sizeof(sense),
|
|
};
|
|
|
|
if (out) {
|
|
io.dout_xfer_len = len;
|
|
io.dout_xferp = (uintptr_t)buf;
|
|
} else {
|
|
io.din_xfer_len = len;
|
|
io.din_xferp = (uintptr_t)buf;
|
|
}
|
|
for (int i = 0; i < 2; i++) {
|
|
memset(sense, 0, sizeof(sense));
|
|
if (ioctl(rpmb_fd, SG_IO, &io))
|
|
break;
|
|
if (!io.info)
|
|
return 0;
|
|
if ((sense[2] & 0xf) != 6)
|
|
break;
|
|
}
|
|
LOG("rpmb: SCSI 0x%02x failed (info 0x%x, sense key %u)\n", cdb[0],
|
|
io.info, sense[2] & 0xf);
|
|
return -1;
|
|
}
|
|
|
|
static int rpmb_sec(int in, void *buf, uint32_t len)
|
|
{
|
|
uint8_t cdb[12] = {
|
|
in ? 0xa2 : 0xb5, 0xec, 0x00, 0x01, 0, 0,
|
|
len >> 24, len >> 16, len >> 8, len, 0, 0,
|
|
};
|
|
|
|
return rpmb_scsi(cdb, sizeof(cdb), buf, len, !in);
|
|
}
|
|
|
|
static int rpmb_serve(uint8_t *b)
|
|
{
|
|
static uint8_t frames[32 * RPMB_BLK];
|
|
static const uint8_t result_req[RPMB_BLK] = { [511] = 0x05 };
|
|
struct rpmb_rw_req rq;
|
|
struct rpmb_rw_res res = { .res_buff_offset = sizeof(res) };
|
|
uint8_t *resp = b + sizeof(res);
|
|
uint32_t cmd = *(uint32_t *)b, per, total;
|
|
int st = 0;
|
|
|
|
switch (cmd) {
|
|
case 0x101: { /* init */
|
|
struct rpmb_init_res r = {
|
|
.cmd_id = cmd,
|
|
.version = *(uint32_t *)(b + 4) >= 2 ? 2 : 1,
|
|
.num_sectors = 8192,
|
|
.rel_wr_count = 1,
|
|
.dev_type = 9, /* UFS */
|
|
};
|
|
uint8_t sense_cdb[6] = { 0x03, 0, 0, 0, 18, 0 }, sense[18];
|
|
|
|
r.status = rpmb_open();
|
|
if (!r.status) /* clears a pending UNIT ATTENTION */
|
|
rpmb_scsi(sense_cdb, sizeof(sense_cdb), sense,
|
|
sizeof(sense), 0);
|
|
memcpy(b, &r, r.version == 2 ? sizeof(r) : 20);
|
|
return 0;
|
|
}
|
|
case 0x102: /* read: one request frame out, the result in */
|
|
memcpy(&rq, b, sizeof(rq));
|
|
total = rq.num_sectors * RPMB_BLK;
|
|
st = rpmb_open() || total > 32 * RPMB_BLK ||
|
|
rpmb_sec(0, b + rq.req_buff_offset, RPMB_BLK) ||
|
|
rpmb_sec(1, resp, total) ? -1 : 0;
|
|
res.res_buff_len = st ? 0 : total;
|
|
break;
|
|
case 0x103: /* write: frames out, then read the result register */
|
|
memcpy(&rq, b, sizeof(rq));
|
|
per = rq.rel_wr_count ? rq.rel_wr_count : 1;
|
|
total = rq.num_sectors * RPMB_BLK;
|
|
if (!total || total > sizeof(frames) || rpmb_open()) {
|
|
st = -1;
|
|
break;
|
|
}
|
|
memcpy(frames, b + rq.req_buff_offset, total);
|
|
for (uint32_t i = 0; !st && i < rq.num_sectors / per; i++)
|
|
st = rpmb_sec(0, frames + i * per * RPMB_BLK,
|
|
per * RPMB_BLK) ||
|
|
rpmb_sec(0, (void *)result_req, RPMB_BLK) ||
|
|
rpmb_sec(1, resp, RPMB_BLK) ? -1 : 0;
|
|
res.res_buff_len = st ? 0 : RPMB_BLK;
|
|
break;
|
|
case 0x104: /* partitions: none provisioned */
|
|
*(int32_t *)(b + 4) = -1;
|
|
*(uint32_t *)(b + 8) = 0;
|
|
*(uint32_t *)(b + 12) = 16;
|
|
return 0;
|
|
default:
|
|
LOG("rpmb: unknown command 0x%x\n", cmd);
|
|
return -1;
|
|
}
|
|
res.cmd_id = rq.cmd_id;
|
|
res.version = rq.version;
|
|
res.status = st;
|
|
memcpy(b, &res, sizeof(res));
|
|
return 0;
|
|
}
|
|
|
|
static struct listener {
|
|
uint32_t id, size;
|
|
int (*serve)(uint8_t *buf);
|
|
struct shm buf;
|
|
} listeners[] = {
|
|
{ .id = 10, .size = 0x5000, .serve = fs_serve },
|
|
{ .id = 28672, .size = 0x7e000, .serve = gpfs_serve },
|
|
{ .id = 8192, .size = 0x6000, .serve = rpmb_serve }, /* page aligned */
|
|
};
|
|
|
|
static int supp_fd = -1;
|
|
|
|
/* Register the listeners; then serve_listeners() answers their requests. */
|
|
static int register_listeners(void)
|
|
{
|
|
supp_fd = tee_open("teepriv");
|
|
if (supp_fd < 0)
|
|
return -1;
|
|
for (size_t i = 0; i < sizeof(listeners) / sizeof(*listeners); i++) {
|
|
struct listener *l = &listeners[i];
|
|
struct {
|
|
struct tee_ioctl_open_session_arg arg;
|
|
struct tee_ioctl_param params[2];
|
|
} s = {};
|
|
struct tee_ioctl_buf_data bd = { (uintptr_t)&s, sizeof(s) };
|
|
|
|
if (shm_alloc(supp_fd, l->size, &l->buf))
|
|
return -1;
|
|
s.arg.num_params = 2;
|
|
s.params[0].attr = TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT;
|
|
s.params[0].a = l->id;
|
|
s.params[1].attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT;
|
|
s.params[1].b = l->size;
|
|
s.params[1].c = l->buf.id;
|
|
if (ioctl(supp_fd, TEE_IOC_OPEN_SESSION, &bd)) {
|
|
LOG("register listener %u: %s\n", l->id,
|
|
strerror(errno));
|
|
return -1;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
static void *serve_listeners(void *unused)
|
|
{
|
|
(void)unused;
|
|
for (;;) {
|
|
struct {
|
|
struct tee_iocl_supp_recv_arg arg;
|
|
struct tee_ioctl_param params[2];
|
|
} recv = { .arg.num_params = 2 };
|
|
struct {
|
|
struct tee_iocl_supp_send_arg arg;
|
|
struct tee_ioctl_param params[1];
|
|
} send = { .arg.num_params = 1 };
|
|
struct tee_ioctl_buf_data bd = { (uintptr_t)&recv, sizeof(recv) };
|
|
int ok = 0;
|
|
|
|
if (ioctl(supp_fd, TEE_IOC_SUPPL_RECV, &bd)) {
|
|
LOG("SUPPL_RECV: %s\n", strerror(errno));
|
|
exit(1);
|
|
}
|
|
for (size_t i = 0; i < sizeof(listeners) / sizeof(*listeners); i++)
|
|
if (listeners[i].id == recv.arg.func)
|
|
ok = !listeners[i].serve(listeners[i].buf.va);
|
|
if (!ok)
|
|
LOG("request for listener %u refused\n", recv.arg.func);
|
|
|
|
/* Echo the request id, or the answer is taken for a stale one */
|
|
send.arg.ret = !ok;
|
|
send.params[0].attr = TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT;
|
|
send.params[0].a = recv.params[0].a;
|
|
bd = (struct tee_ioctl_buf_data){ (uintptr_t)&send, sizeof(send) };
|
|
if (ioctl(supp_fd, TEE_IOC_SUPPL_SEND, &bd)) {
|
|
LOG("SUPPL_SEND: %s\n", strerror(errno));
|
|
exit(1);
|
|
}
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
/*
|
|
* The app loader fetches the .mdt and its segments with request_firmware(),
|
|
* whose search path NixOS sets at boot, so point it at the apps meanwhile.
|
|
* Their sessions stay open, keeping them loaded, while this runs.
|
|
*/
|
|
static int load_apps(const char *dir, char **apps, int n)
|
|
{
|
|
const char *param = "/sys/module/firmware_class/parameters/path";
|
|
char old[256] = "";
|
|
int fd = open(param, O_RDWR | O_CLOEXEC), load_fd = tee_open("teepriv");
|
|
int ok = 1;
|
|
ssize_t len;
|
|
|
|
if (fd < 0 || load_fd < 0)
|
|
return -1;
|
|
len = read(fd, old, sizeof(old) - 1);
|
|
old[len > 0 ? len : 0] = '\0';
|
|
old[strcspn(old, "\n")] = '\0';
|
|
if (pwrite(fd, dir, strlen(dir), 0) < 0)
|
|
LOG("set firmware path: %s\n", strerror(errno));
|
|
for (int i = 0; i < n; i++)
|
|
if (tee_session(load_fd, apps[i]) < 0)
|
|
ok = 0;
|
|
/* An empty write is refused, but a lone newline clears it */
|
|
if (pwrite(fd, *old ? old : "\n", *old ? strlen(old) : 1, 0) < 0)
|
|
LOG("restore firmware path: %s\n", strerror(errno));
|
|
close(fd);
|
|
return ok ? 0 : -1;
|
|
}
|
|
|
|
int main(int argc, char **argv)
|
|
{
|
|
setvbuf(stderr, NULL, _IOLBF, 0);
|
|
if (argc < 3) {
|
|
LOG("usage: %s <root> <firmware dir> [app...]\n", argv[0]);
|
|
return 2;
|
|
}
|
|
fs_root = argv[1];
|
|
mkdir(fs_root, 0700);
|
|
|
|
/* The listeners first: loading an app can already call into them */
|
|
if (register_listeners())
|
|
return 1;
|
|
{
|
|
pthread_t thread;
|
|
|
|
if (pthread_create(&thread, NULL, serve_listeners, NULL) ||
|
|
load_apps(argv[2], argv + 3, argc - 3))
|
|
return 1;
|
|
LOG("ready\n");
|
|
pthread_join(thread, NULL);
|
|
}
|
|
return 1;
|
|
}
|