usb: backend UDisks2 et écriture de la mise à jour sur clé

- API UsbBackend (liste, formatage FAT32, éjection) + implémentation
  UDisks2 via GDBus : disques USB amovibles uniquement (jamais HintSystem),
  table MBR + partition 0x0c à 1 Mio formatée vfat, montage, PowerOff ;
  autorisation déléguée à polkit (pas de root)
- writeToUsb : vérification des archives AVANT effacement, formatage,
  licence dans license/, extraction fw puis carte, éjection
- règles de l'app officielle : label assaini (11 car., majuscules, NEW),
  taille de clé minimale 2/4/8… Go avec marge de 10 %
- CLI : usb-list, write-usb (confirmation YES), write-dir
- tests : faux backend (ordre des étapes, archive corrompue => clé
  intacte), labels, tailles
- tools/e2e-usb.sh : image loop via UDisks2 + contrôle sfdisk/fsck/mtools ;
  tools/dev-polkit.sh pour WSL/CI
- validé de bout en bout avec le vrai firmware RCC 8,39 Go : MBR, FAT32,
  label, arborescence, fsck propre, en environ 20 s

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WDASLCiN764v685boRVYar
This commit is contained in:
yann64andClaude Opus 5.5 committed 2026-11-28 19:17:00 +01:00
1 parent 543347a1e6
commit 6a7a2f1ee9
11 files changed
+1047 -8

No files matched your search

+141
View File
@@ -4,8 +4,10 @@
#include "openpu/archive.hpp"
#include "openpu/download.hpp"
#include "openpu/storage.hpp"
#include "openpu/usb.hpp"
#include "openpu/vin.hpp"
#include <algorithm>
#include <csignal>
#include <cstdio>
#include <cstdlib>
@@ -36,6 +38,11 @@ void usage()
" resuming partial files\n"
" verify <file.tar> check the integrity of an update archive\n"
" extract <file.tar> <dir> extract an update archive (skips UpdateInfo.xml)\n"
" usb-list list removable USB drives\n"
" write-usb <device> [--license F] [--label L] [--yes] [--no-eject] <archive.tar>...\n"
" ERASE <device>, format FAT32 and write the update\n"
" write-dir <dir> [--license F] <archive.tar>...\n"
" write the update into an existing directory\n"
" raw-devices <VIN> print the raw devices/{VIN} response\n"
" raw-updates <VIN> print the raw getAvailableUpdate response\n"
"\n"
@@ -221,6 +228,132 @@ int cmdExtract(const fs::path& tar, const fs::path& dest)
return 0;
}
const char* stepName(openpu::UsbStep s)
{
switch (s) {
case openpu::UsbStep::Verifying: return "verify ";
case openpu::UsbStep::Formatting: return "format ";
case openpu::UsbStep::CopyingLicense: return "license";
case openpu::UsbStep::Extracting: return "write ";
case openpu::UsbStep::Ejecting: return "eject ";
case openpu::UsbStep::Done: return "done ";
}
return "";
}
openpu::UsbProgressCallback usbProgress()
{
return [last = openpu::UsbStep::Done](const openpu::UsbProgress& p) mutable {
if (p.step != last && last != openpu::UsbStep::Done)
(void)std::fprintf(stderr, "\n");
last = p.step;
std::string label = stepName(p.step);
if (p.archiveCount > 1 && (p.step == openpu::UsbStep::Verifying || p.step == openpu::UsbStep::Extracting))
label += " " + std::to_string(p.archiveIndex + 1) + "/" + std::to_string(p.archiveCount);
if (p.total > 0)
progressLine(label, p.done, p.total, "");
else
(void)std::fprintf(stderr, "\r%s %s ", label.c_str(), p.detail.c_str());
};
}
/// Parses "[--license F] [--label L] [--yes] [--no-eject] archives..." options.
struct WriteOptions {
openpu::UsbWritePlan plan;
std::string label;
bool yes = false;
bool eject = true;
bool ok = true;
};
WriteOptions parseWriteOptions(const std::vector<std::string>& opts)
{
WriteOptions w;
for (std::size_t i = 0; i < opts.size(); ++i) {
if (opts[i] == "--license" && i + 1 < opts.size())
w.plan.license = fs::path(opts[++i]);
else if (opts[i] == "--label" && i + 1 < opts.size())
w.label = opts[++i];
else if (opts[i] == "--yes")
w.yes = true;
else if (opts[i] == "--no-eject")
w.eject = false;
else if (opts[i].rfind("--", 0) == 0)
w.ok = false;
else
w.plan.archives.emplace_back(opts[i]);
}
if (w.plan.archives.empty() && !w.plan.license)
w.ok = false;
return w;
}
int cmdUsbList()
{
auto backend = openpu::makeUsbBackend();
const auto drives = backend->list();
if (drives.empty())
std::cout << "no removable USB drive found\n";
for (const auto& d : drives) {
std::cout << d.device << " " << d.description() << (d.label.empty() ? "" : " label=" + d.label);
for (const auto& mp : d.mountPoints)
std::cout << " mounted=" << mp.string();
std::cout << '\n';
}
return 0;
}
int cmdWriteUsb(const std::string& target, const std::vector<std::string>& opts)
{
WriteOptions w = parseWriteOptions(opts);
if (!w.ok) {
usage();
return 2;
}
auto backend = openpu::makeUsbBackend();
const auto drives = backend->list();
const auto it = std::find_if(drives.begin(), drives.end(),
[&](const openpu::UsbDrive& d) { return d.device == target || d.id == target; });
if (it == drives.end()) {
std::cerr << "error: " << target << " is not a removable USB drive (see usb-list)\n";
return 1;
}
const std::uint64_t payload = w.plan.payloadSize();
if (it->size < openpu::withMargin(payload)) {
std::cerr << "error: " << it->description() << " is too small: " << humanSize(openpu::withMargin(payload))
<< " needed (a " << humanSize(openpu::minimumStickSize(payload)) << " stick)\n";
return 1;
}
const std::string label = openpu::sanitizeLabel(w.label.empty() ? it->label : w.label);
if (!w.yes) {
std::cout << "ALL DATA ON " << it->description() << " WILL BE ERASED.\n"
<< "It will be formatted FAT32 with label " << label << ". Type YES to continue: " << std::flush;
std::string answer;
std::getline(std::cin, answer);
if (answer != "YES") {
std::cout << "aborted\n";
return 1;
}
}
openpu::writeToUsb(*backend, *it, label, w.plan, usbProgress(), &gCancel, w.eject);
(void)std::fprintf(stderr, "\n");
std::cout << "the USB stick is ready" << (w.eject ? " and can be removed" : "") << '\n';
return 0;
}
int cmdWriteDir(const fs::path& dir, const std::vector<std::string>& opts)
{
WriteOptions w = parseWriteOptions(opts);
if (!w.ok) {
usage();
return 2;
}
openpu::writePayload(dir, w.plan, usbProgress(), &gCancel);
(void)std::fprintf(stderr, "\n");
std::cout << "written to " << dir.string() << '\n';
return 0;
}
int run(std::vector<std::string> args)
{
openpu::CurlOptions curlOptions;
@@ -228,11 +361,17 @@ int run(std::vector<std::string> args)
curlOptions.verbose = true;
args.erase(args.begin());
}
if (args.size() == 1 && args[0] == "usb-list")
return cmdUsbList();
if (args.size() < 2) {
usage();
return 2;
}
const std::string command = args[0];
if (command == "write-usb")
return cmdWriteUsb(args[1], {args.begin() + 2, args.end()});
if (command == "write-dir")
return cmdWriteDir(args[1], {args.begin() + 2, args.end()});
if (command == "verify" && args.size() == 2)
return cmdVerify(args[1]);
if (command == "extract" && args.size() == 3)
@@ -277,6 +416,8 @@ int main(int argc, char** argv)
} catch (const openpu::ArchiveError& e) {
std::cerr << "\narchive error (" << (e.kind() == openpu::ArchiveError::Kind::Corrupt ? "corrupt" : "I/O")
<< "): " << e.what() << '\n';
} catch (const openpu::UsbError& e) {
std::cerr << "\nUSB error: " << e.what() << '\n';
} catch (const openpu::StorageError& e) {
std::cerr << "error: " << e.what() << '\n';
}