2018-03-12 17:56:33 -07:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2016-2018, Linaro Ltd.
|
|
|
|
|
* All rights reserved.
|
|
|
|
|
*
|
2024-03-16 17:53:43 +02:00
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
2018-03-12 17:56:33 -07:00
|
|
|
*/
|
2018-02-26 21:04:46 -08:00
|
|
|
#include <err.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <fcntl.h>
|
2019-10-23 21:42:46 -07:00
|
|
|
#include <signal.h>
|
2018-02-26 21:04:46 -08:00
|
|
|
#include <stdint.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <unistd.h>
|
2023-11-08 17:15:27 +03:00
|
|
|
#include <syslog.h>
|
2018-02-26 21:04:46 -08:00
|
|
|
|
2020-04-23 12:46:37 +05:30
|
|
|
#include "cdba-server.h"
|
2018-02-26 21:04:46 -08:00
|
|
|
#include "circ_buf.h"
|
|
|
|
|
#include "device.h"
|
2018-03-12 17:12:02 -07:00
|
|
|
#include "device_parser.h"
|
2018-02-26 21:04:46 -08:00
|
|
|
#include "fastboot.h"
|
|
|
|
|
#include "list.h"
|
2024-03-12 20:17:22 +02:00
|
|
|
#include "watch.h"
|
2018-02-26 21:04:46 -08:00
|
|
|
|
2023-07-08 16:33:45 +03:00
|
|
|
static const char *username;
|
2019-10-23 21:42:46 -07:00
|
|
|
|
2018-02-26 21:04:46 -08:00
|
|
|
struct device *selected_device;
|
|
|
|
|
|
|
|
|
|
static void fastboot_opened(struct fastboot *fb, void *data)
|
|
|
|
|
{
|
|
|
|
|
const uint8_t one = 1;
|
|
|
|
|
|
2020-12-18 14:43:31 +03:00
|
|
|
warnx("fastboot connection opened");
|
|
|
|
|
|
2023-09-23 21:31:14 +03:00
|
|
|
cdba_send_buf(MSG_FASTBOOT_PRESENT, 1, &one);
|
2018-02-26 21:04:46 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void fastboot_info(struct fastboot *fb, const void *buf, size_t len)
|
|
|
|
|
{
|
2023-05-07 11:55:30 +02:00
|
|
|
fprintf(stderr, "%s\n", (const char *)buf);
|
2018-02-26 21:04:46 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void fastboot_disconnect(void *data)
|
|
|
|
|
{
|
|
|
|
|
const uint8_t zero = 0;
|
|
|
|
|
|
2023-09-23 21:31:14 +03:00
|
|
|
cdba_send_buf(MSG_FASTBOOT_PRESENT, 1, &zero);
|
2018-02-26 21:04:46 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static struct fastboot_ops fastboot_ops = {
|
|
|
|
|
.opened = fastboot_opened,
|
|
|
|
|
.disconnect = fastboot_disconnect,
|
|
|
|
|
.info = fastboot_info,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static void msg_select_board(const void *param)
|
|
|
|
|
{
|
2024-03-12 20:30:25 +02:00
|
|
|
selected_device = device_open(param, username);
|
2019-10-23 21:44:32 -07:00
|
|
|
if (!selected_device) {
|
2018-02-26 21:04:46 -08:00
|
|
|
fprintf(stderr, "failed to open %s\n", (const char *)param);
|
2024-03-12 20:17:22 +02:00
|
|
|
watch_quit();
|
2019-10-23 21:44:32 -07:00
|
|
|
}
|
2018-02-26 21:04:46 -08:00
|
|
|
|
2024-03-12 20:30:25 +02:00
|
|
|
device_fastboot_open(selected_device, &fastboot_ops);
|
|
|
|
|
|
2023-09-23 21:31:14 +03:00
|
|
|
cdba_send(MSG_SELECT_BOARD);
|
2018-02-26 21:04:46 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void *fastboot_payload;
|
|
|
|
|
static size_t fastboot_size;
|
|
|
|
|
|
|
|
|
|
static void msg_fastboot_download(const void *data, size_t len)
|
|
|
|
|
{
|
|
|
|
|
size_t new_size = fastboot_size + len;
|
2023-05-07 11:56:06 +02:00
|
|
|
char *newp;
|
2018-02-26 21:04:46 -08:00
|
|
|
|
|
|
|
|
newp = realloc(fastboot_payload, new_size);
|
|
|
|
|
if (!newp)
|
|
|
|
|
err(1, "failed too expant fastboot scratch area");
|
|
|
|
|
|
|
|
|
|
memcpy(newp + fastboot_size, data, len);
|
|
|
|
|
|
|
|
|
|
fastboot_payload = newp;
|
|
|
|
|
fastboot_size = new_size;
|
|
|
|
|
|
|
|
|
|
if (!len) {
|
|
|
|
|
device_boot(selected_device, fastboot_payload, fastboot_size);
|
|
|
|
|
|
2023-09-23 21:31:14 +03:00
|
|
|
cdba_send(MSG_FASTBOOT_DOWNLOAD);
|
2018-02-26 21:04:46 -08:00
|
|
|
free(fastboot_payload);
|
|
|
|
|
fastboot_payload = NULL;
|
|
|
|
|
fastboot_size = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-01 15:25:02 +00:00
|
|
|
static void msg_fastboot_continue(void)
|
|
|
|
|
{
|
|
|
|
|
device_fastboot_continue(selected_device);
|
|
|
|
|
cdba_send(MSG_FASTBOOT_CONTINUE);
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-23 21:31:14 +03:00
|
|
|
void cdba_send_buf(int type, size_t len, const void *buf)
|
2018-02-28 17:06:31 -08:00
|
|
|
{
|
2023-09-23 21:31:14 +03:00
|
|
|
struct msg msg = {
|
|
|
|
|
.type = type,
|
|
|
|
|
.len = len
|
|
|
|
|
};
|
2018-02-28 17:06:31 -08:00
|
|
|
|
|
|
|
|
write(STDOUT_FILENO, &msg, sizeof(msg));
|
2023-09-23 21:31:14 +03:00
|
|
|
if (len)
|
|
|
|
|
write(STDOUT_FILENO, buf, len);
|
2018-02-28 17:06:31 -08:00
|
|
|
}
|
|
|
|
|
|
2018-02-26 21:04:46 -08:00
|
|
|
static int handle_stdin(int fd, void *buf)
|
|
|
|
|
{
|
2023-10-02 17:41:18 +03:00
|
|
|
static struct circ_buf recv_buf = { };
|
2018-02-26 21:04:46 -08:00
|
|
|
struct msg *msg;
|
|
|
|
|
struct msg hdr;
|
|
|
|
|
size_t n;
|
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
|
|
ret = circ_fill(STDIN_FILENO, &recv_buf);
|
|
|
|
|
if (ret < 0 && errno != EAGAIN) {
|
|
|
|
|
fprintf(stderr, "read %d\n", ret);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
|
n = circ_peak(&recv_buf, &hdr, sizeof(hdr));
|
|
|
|
|
if (n != sizeof(hdr))
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
if (CIRC_AVAIL(&recv_buf) < sizeof(*msg) + hdr.len)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
msg = malloc(sizeof(*msg) + hdr.len);
|
|
|
|
|
circ_read(&recv_buf, msg, sizeof(*msg) + hdr.len);
|
|
|
|
|
|
|
|
|
|
switch (msg->type) {
|
|
|
|
|
case MSG_CONSOLE:
|
|
|
|
|
device_write(selected_device, msg->data, msg->len);
|
|
|
|
|
break;
|
|
|
|
|
case MSG_FASTBOOT_PRESENT:
|
|
|
|
|
break;
|
|
|
|
|
case MSG_SELECT_BOARD:
|
|
|
|
|
msg_select_board(msg->data);
|
|
|
|
|
break;
|
|
|
|
|
case MSG_HARDRESET:
|
|
|
|
|
// fprintf(stderr, "hard reset\n");
|
|
|
|
|
break;
|
|
|
|
|
case MSG_POWER_ON:
|
2021-01-22 18:34:58 -06:00
|
|
|
device_power(selected_device, true);
|
2018-02-28 17:06:31 -08:00
|
|
|
|
2023-09-23 21:31:14 +03:00
|
|
|
cdba_send(MSG_POWER_ON);
|
2018-02-26 21:04:46 -08:00
|
|
|
break;
|
|
|
|
|
case MSG_POWER_OFF:
|
2021-01-22 18:34:58 -06:00
|
|
|
device_power(selected_device, false);
|
2018-02-28 17:06:31 -08:00
|
|
|
|
2023-09-23 21:31:14 +03:00
|
|
|
cdba_send(MSG_POWER_OFF);
|
2018-02-26 21:04:46 -08:00
|
|
|
break;
|
|
|
|
|
case MSG_FASTBOOT_DOWNLOAD:
|
|
|
|
|
msg_fastboot_download(msg->data, msg->len);
|
|
|
|
|
break;
|
|
|
|
|
case MSG_FASTBOOT_BOOT:
|
|
|
|
|
// fprintf(stderr, "fastboot boot\n");
|
|
|
|
|
break;
|
|
|
|
|
case MSG_STATUS_UPDATE:
|
2023-11-05 14:16:43 -06:00
|
|
|
device_status_enable(selected_device);
|
2018-02-26 21:04:46 -08:00
|
|
|
break;
|
|
|
|
|
case MSG_VBUS_ON:
|
2021-01-22 18:34:58 -06:00
|
|
|
device_usb(selected_device, true);
|
2018-02-26 21:04:46 -08:00
|
|
|
break;
|
|
|
|
|
case MSG_VBUS_OFF:
|
2021-01-22 18:34:58 -06:00
|
|
|
device_usb(selected_device, false);
|
2018-02-26 21:04:46 -08:00
|
|
|
break;
|
2019-08-26 10:10:49 -07:00
|
|
|
case MSG_SEND_BREAK:
|
|
|
|
|
device_send_break(selected_device);
|
|
|
|
|
break;
|
2020-06-28 23:45:59 -07:00
|
|
|
case MSG_LIST_DEVICES:
|
2023-07-08 16:33:45 +03:00
|
|
|
device_list_devices(username);
|
2020-06-28 23:45:59 -07:00
|
|
|
break;
|
2020-06-29 01:01:01 -07:00
|
|
|
case MSG_BOARD_INFO:
|
2023-07-08 16:33:45 +03:00
|
|
|
device_info(username, msg->data, msg->len);
|
2020-06-29 01:01:01 -07:00
|
|
|
break;
|
2024-01-01 15:25:02 +00:00
|
|
|
case MSG_FASTBOOT_CONTINUE:
|
|
|
|
|
msg_fastboot_continue();
|
|
|
|
|
break;
|
2018-02-26 21:04:46 -08:00
|
|
|
default:
|
|
|
|
|
fprintf(stderr, "unk %d len %d\n", msg->type, msg->len);
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
free(msg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-23 21:42:46 -07:00
|
|
|
static void sigpipe_handler(int signo)
|
|
|
|
|
{
|
2024-03-12 20:17:22 +02:00
|
|
|
watch_quit();
|
2018-12-10 07:49:41 -08:00
|
|
|
}
|
|
|
|
|
|
2018-02-26 21:04:46 -08:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
|
{
|
|
|
|
|
int flags;
|
|
|
|
|
int ret;
|
|
|
|
|
|
2019-10-23 21:42:46 -07:00
|
|
|
signal(SIGPIPE, sigpipe_handler);
|
|
|
|
|
|
2023-07-08 17:32:31 +03:00
|
|
|
fprintf(stderr, "Starting cdba server\n");
|
|
|
|
|
|
2023-07-08 16:33:45 +03:00
|
|
|
username = getenv("CDBA_USER");
|
2023-11-08 17:16:49 +03:00
|
|
|
if (!username)
|
|
|
|
|
username = getenv("USER");
|
|
|
|
|
if (!username)
|
|
|
|
|
username = "nobody";
|
2023-07-08 16:33:45 +03:00
|
|
|
|
2023-11-08 17:15:27 +03:00
|
|
|
openlog("cdba-server", 0, LOG_DAEMON);
|
|
|
|
|
|
2020-06-29 00:02:16 -07:00
|
|
|
ret = device_parser(".cdba");
|
|
|
|
|
if (ret) {
|
|
|
|
|
ret = device_parser("/etc/cdba");
|
|
|
|
|
if (ret) {
|
|
|
|
|
fprintf(stderr, "device parser: unable to open config file\n");
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-03-12 17:12:02 -07:00
|
|
|
|
2018-02-26 21:04:46 -08:00
|
|
|
watch_add_readfd(STDIN_FILENO, handle_stdin, NULL);
|
|
|
|
|
|
|
|
|
|
flags = fcntl(STDIN_FILENO, F_GETFL, 0);
|
|
|
|
|
fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK);
|
|
|
|
|
|
2024-03-12 20:17:22 +02:00
|
|
|
watch_run();
|
2018-02-26 21:04:46 -08:00
|
|
|
|
2023-09-23 18:28:16 +03:00
|
|
|
/* if we got here, stdin/out/err might be not accessible anymore */
|
|
|
|
|
ret = open("/dev/null", O_RDWR);
|
|
|
|
|
if (ret >= 0) {
|
|
|
|
|
close(STDIN_FILENO);
|
|
|
|
|
dup2(ret, STDIN_FILENO);
|
|
|
|
|
close(STDOUT_FILENO);
|
|
|
|
|
dup2(ret, STDOUT_FILENO);
|
|
|
|
|
close(STDERR_FILENO);
|
|
|
|
|
dup2(ret, STDERR_FILENO);
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-25 18:49:21 -06:00
|
|
|
if (selected_device)
|
|
|
|
|
device_close(selected_device);
|
2018-02-26 21:04:46 -08:00
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|