From ec00e71b643e6bb6abcb1f977cb51df31bd8c058 Mon Sep 17 00:00:00 2001 From: Adeeb Shihadeh Date: Mon, 5 Jul 2021 23:48:10 -0700 Subject: [PATCH] add recover mode to factory resetter (#21492) * recover mode * clean up * less margin * use inter * little nicer Co-authored-by: Comma Device --- selfdrive/ui/qt/setup/reset.cc | 103 +++++++++++++++++++-------------- selfdrive/ui/qt/setup/reset.h | 20 +++++++ 2 files changed, 78 insertions(+), 45 deletions(-) create mode 100644 selfdrive/ui/qt/setup/reset.h diff --git a/selfdrive/ui/qt/setup/reset.cc b/selfdrive/ui/qt/setup/reset.cc index 74d94c2d6a..dce454e9b8 100644 --- a/selfdrive/ui/qt/setup/reset.cc +++ b/selfdrive/ui/qt/setup/reset.cc @@ -2,82 +2,89 @@ #include #include #include +#include #include -#include #include "selfdrive/ui/qt/qt_window.h" +#include "selfdrive/ui/qt/setup/reset.h" -#define USERDATA "/dev/disk/by-partlabel/userdata" #define NVME "/dev/nvme0n1" +#define USERDATA "/dev/disk/by-partlabel/userdata" -bool do_reset() { +void Reset::doReset() { std::vector cmds = { - "sudo umount " NVME, - "yes | sudo mkfs.ext4 " NVME, - "sudo umount " USERDATA, + "sudo umount " NVME " || true", + "yes | sudo mkfs.ext4 " NVME " || true", + "sudo umount " USERDATA " || true", "yes | sudo mkfs.ext4 " USERDATA, "sudo reboot", }; for (auto &cmd : cmds) { int ret = std::system(cmd); - if (ret != 0) return false; + if (ret != 0) { + body->setText("Reset failed. Reboot to try again."); + rebootBtn->show(); + return; + } } - return true; } -int main(int argc, char *argv[]) { - QApplication a(argc, argv); - QWidget window; - setMainWindow(&window); +void Reset::confirm() { + const QString confirm_txt = "Are you sure you want to reset your device?"; + if (body->text() != confirm_txt) { + body->setText(confirm_txt); + } else { + body->setText("Resetting device..."); + rejectBtn->hide(); + rebootBtn->hide(); + confirmBtn->hide(); +#ifdef __aarch64__ + QTimer::singleShot(100, this, &Reset::doReset); +#endif + } +} - QVBoxLayout *main_layout = new QVBoxLayout(&window); - main_layout->setContentsMargins(125, 125, 125, 125); +Reset::Reset(bool recover, QWidget *parent) : QWidget(parent) { + QVBoxLayout *main_layout = new QVBoxLayout(this); + main_layout->setContentsMargins(100, 100, 100, 100); QLabel *title = new QLabel("System Reset"); - title->setStyleSheet(R"( - font-weight: 500; - font-size: 100px; - )"); + title->setStyleSheet(R"(font-weight: 500; font-size: 100px;)"); main_layout->addWidget(title, 0, Qt::AlignTop); - QLabel *body = new QLabel("System reset triggered. Press confirm to erase all content and settings. Press cancel to resume boot."); + body = new QLabel("System reset triggered. Press confirm to erase all content and settings. Press cancel to resume boot."); body->setWordWrap(true); body->setAlignment(Qt::AlignCenter); - body->setStyleSheet("font-size: 65px;"); + body->setStyleSheet("font-size: 80px;"); main_layout->addWidget(body, 1, Qt::AlignCenter); - QHBoxLayout *btn_layout = new QHBoxLayout(); + QHBoxLayout *blayout = new QHBoxLayout(); + main_layout->addLayout(blayout); - QPushButton *cancel_btn = new QPushButton("Cancel"); - btn_layout->addWidget(cancel_btn, 0, Qt::AlignLeft); - QObject::connect(cancel_btn, &QPushButton::released, &a, &QApplication::quit); + rejectBtn = new QPushButton("Cancel"); + blayout->addWidget(rejectBtn, 0, Qt::AlignLeft); + QObject::connect(rejectBtn, &QPushButton::released, QCoreApplication::instance(), &QCoreApplication::quit); - QPushButton *confirm_btn = new QPushButton("Confirm"); - btn_layout->addWidget(confirm_btn, 0, Qt::AlignRight); - QObject::connect(confirm_btn, &QPushButton::released, [=]() { - const QString confirm_txt = "Are you sure you want to reset your device?"; - if (body->text() != confirm_txt) { - body->setText(confirm_txt); - } else { - body->setText("Resetting device..."); - cancel_btn->hide(); - confirm_btn->hide(); - QCoreApplication::processEvents(QEventLoop::AllEvents, 1000); -#ifdef __aarch64__ - bool ret = do_reset(); - if (!ret) { - body->setText("Reset failed."); - cancel_btn->show(); - } -#endif - } + rebootBtn = new QPushButton("Reboot"); + blayout->addWidget(rebootBtn, 0, Qt::AlignLeft); + QObject::connect(rebootBtn, &QPushButton::released, [=]{ + std::system("sudo reboot"); }); - main_layout->addLayout(btn_layout); + confirmBtn = new QPushButton("Confirm"); + blayout->addWidget(confirmBtn, 0, Qt::AlignRight); + QObject::connect(confirmBtn, &QPushButton::released, this, &Reset::confirm); - window.setStyleSheet(R"( + rejectBtn->setVisible(!recover); + rebootBtn->setVisible(recover); + if (recover) { + body->setText("Unable to mount data partition. Press confirm to reset your device."); + } + + setStyleSheet(R"( * { + font-family: Inter; color: white; background-color: black; } @@ -90,6 +97,12 @@ int main(int argc, char *argv[]) { font-size: 50px; } )"); +} +int main(int argc, char *argv[]) { + bool recover = argc > 1 && strcmp(argv[1], "--recover") == 0; + QApplication a(argc, argv); + Reset reset(recover); + setMainWindow(&reset); return a.exec(); } diff --git a/selfdrive/ui/qt/setup/reset.h b/selfdrive/ui/qt/setup/reset.h new file mode 100644 index 0000000000..3a4994077c --- /dev/null +++ b/selfdrive/ui/qt/setup/reset.h @@ -0,0 +1,20 @@ +#include +#include +#include + +class Reset : public QWidget { + Q_OBJECT + +public: + explicit Reset(bool recover = false, QWidget *parent = 0); + +private: + QLabel *body; + QPushButton *rejectBtn; + QPushButton *rebootBtn; + QPushButton *confirmBtn; + void doReset(); + +private slots: + void confirm(); +};