+static int
+rpc_luci2_upgrade_test(struct ubus_context *ctx, struct ubus_object *obj,
+ struct ubus_request_data *req, const char *method,
+ struct blob_attr *msg)
+{
+ const char *cmd[4] = { "sysupgrade", "--test", "/tmp/firmware.bin", NULL };
+ return ops->exec(cmd, NULL, NULL, NULL, NULL, NULL, ctx, req);
+}
+
+static int
+rpc_luci2_upgrade_start(struct ubus_context *ctx, struct ubus_object *obj,
+ struct ubus_request_data *req, const char *method,
+ struct blob_attr *msg)
+{
+ return 0;
+}
+
+static int
+rpc_luci2_upgrade_clean(struct ubus_context *ctx, struct ubus_object *obj,
+ struct ubus_request_data *req, const char *method,
+ struct blob_attr *msg)
+{
+ if (unlink("/tmp/firmware.bin"))
+ return rpc_errno_status();
+
+ return 0;
+}
+
+static int
+rpc_luci2_backup_restore(struct ubus_context *ctx, struct ubus_object *obj,
+ struct ubus_request_data *req, const char *method,
+ struct blob_attr *msg)
+{
+ const char *cmd[4] = { "sysupgrade", "--restore-backup",
+ "/tmp/backup.tar.gz", NULL };
+
+ return ops->exec(cmd, NULL, NULL, NULL, NULL, NULL, ctx, req);
+}
+
+static int
+rpc_luci2_backup_clean(struct ubus_context *ctx, struct ubus_object *obj,
+ struct ubus_request_data *req, const char *method,
+ struct blob_attr *msg)
+{
+ if (unlink("/tmp/backup.tar.gz"))
+ return rpc_errno_status();
+
+ return 0;
+}
+
+static int
+rpc_luci2_backup_config_get(struct ubus_context *ctx, struct ubus_object *obj,
+ struct ubus_request_data *req, const char *method,
+ struct blob_attr *msg)
+{
+ FILE *f;
+ char conf[2048] = { 0 };
+
+ if (!(f = fopen("/etc/sysupgrade.conf", "r")))
+ return rpc_errno_status();
+
+ fread(conf, sizeof(conf) - 1, 1, f);
+ fclose(f);
+
+ blob_buf_init(&buf, 0);
+ blobmsg_add_string(&buf, "config", conf);
+
+ ubus_send_reply(ctx, req, buf.head);
+ return 0;
+}
+
+static int
+rpc_luci2_backup_config_set(struct ubus_context *ctx, struct ubus_object *obj,
+ struct ubus_request_data *req, const char *method,
+ struct blob_attr *msg)
+{
+ FILE *f;
+ struct blob_attr *tb[__RPC_D_MAX];
+
+ blobmsg_parse(rpc_data_policy, __RPC_D_MAX, tb,
+ blob_data(msg), blob_len(msg));
+
+ if (!tb[RPC_D_DATA])
+ return UBUS_STATUS_INVALID_ARGUMENT;
+
+ if (blobmsg_data_len(tb[RPC_D_DATA]) >= 2048)
+ return UBUS_STATUS_NOT_SUPPORTED;
+
+ if (!(f = fopen("/etc/sysupgrade.conf", "w")))
+ return rpc_errno_status();
+
+ fwrite(blobmsg_data(tb[RPC_D_DATA]),
+ blobmsg_data_len(tb[RPC_D_DATA]) - 1, 1, f);
+
+ fclose(f);
+ return 0;
+}
+
+struct backup_state {
+ bool open;
+ void *array;
+};
+
+static int
+backup_parse_list(struct blob_buf *blob, char *buf, int len, void *priv)
+{
+ struct backup_state *s = priv;
+ char *nl = strchr(buf, '\n');
+
+ if (!nl)
+ return 0;
+
+ if (!s->open)
+ {
+ s->open = true;
+ s->array = blobmsg_open_array(blob, "files");
+ }
+
+ *nl = 0;
+ blobmsg_add_string(blob, NULL, buf);
+
+ return (nl - buf + 1);
+}
+
+static int
+backup_finish_list(struct blob_buf *blob, int status, void *priv)
+{
+ struct backup_state *s = priv;
+
+ if (!s->open)
+ return UBUS_STATUS_NO_DATA;
+
+ blobmsg_close_array(blob, s->array);
+
+ return UBUS_STATUS_OK;
+}
+
+static int
+rpc_luci2_backup_list(struct ubus_context *ctx, struct ubus_object *obj,
+ struct ubus_request_data *req, const char *method,
+ struct blob_attr *msg)
+{
+ struct backup_state *state = NULL;
+ const char *cmd[3] = { "sysupgrade", "--list-backup", NULL };
+
+ state = malloc(sizeof(*state));
+
+ if (!state)
+ return UBUS_STATUS_UNKNOWN_ERROR;
+
+ memset(state, 0, sizeof(*state));
+
+ return ops->exec(cmd, NULL, backup_parse_list, NULL, backup_finish_list,
+ state, ctx, req);
+}
+
+static int
+rpc_luci2_reset_test(struct ubus_context *ctx, struct ubus_object *obj,
+ struct ubus_request_data *req, const char *method,
+ struct blob_attr *msg)
+{
+ FILE *mtd;
+ struct stat s;
+ char line[64] = { 0 };
+ bool supported = false;
+
+ if (!stat("/sbin/mtd", &s) && (s.st_mode & S_IXUSR))
+ {
+ if ((mtd = fopen("/proc/mtd", "r")) != NULL)
+ {
+ while (fgets(line, sizeof(line) - 1, mtd))
+ {
+ if (strstr(line, "\"rootfs_data\""))
+ {
+ supported = true;
+ break;
+ }
+ }
+
+ fclose(mtd);
+ }
+ }
+
+ blob_buf_init(&buf, 0);
+ blobmsg_add_u8(&buf, "supported", supported);
+
+ ubus_send_reply(ctx, req, buf.head);
+
+ return 0;
+}
+
+static int
+rpc_luci2_reset_start(struct ubus_context *ctx, struct ubus_object *obj,
+ struct ubus_request_data *req, const char *method,
+ struct blob_attr *msg)
+{
+ switch (fork())
+ {
+ case -1:
+ return rpc_errno_status();
+
+ case 0:
+ uloop_done();
+
+ chdir("/");
+
+ close(0);
+ close(1);
+ close(2);
+
+ sleep(1);
+
+ execl("/sbin/mtd", "/sbin/mtd", "-r", "erase", "rootfs_data", NULL);
+
+ return rpc_errno_status();
+
+ default:
+ return 0;
+ }
+}
+
+static int
+rpc_luci2_reboot(struct ubus_context *ctx, struct ubus_object *obj,
+ struct ubus_request_data *req, const char *method,
+ struct blob_attr *msg)
+{
+ switch (fork())
+ {
+ case -1:
+ return rpc_errno_status();
+
+ case 0:
+ chdir("/");
+
+ close(0);
+ close(1);
+ close(2);
+
+ sleep(1);
+
+ execl("/sbin/reboot", "/sbin/reboot", NULL);
+
+ return rpc_errno_status();
+
+ default:
+ return 0;
+ }
+}
+