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

+102
View File
@@ -0,0 +1,102 @@
#pragma once
#include "openpu/cancel.hpp"
#include <cstdint>
#include <filesystem>
#include <functional>
#include <memory>
#include <optional>
#include <stdexcept>
#include <string>
#include <string_view>
#include <vector>
namespace openpu {
/// A removable disk that can receive an update.
struct UsbDrive {
std::string id; ///< backend identifier (UDisks2 block object path, Haiku device path)
std::string device; ///< user-facing device node, e.g. /dev/sdb
std::string vendor;
std::string model;
std::string label; ///< current volume label (first partition), may be empty
std::uint64_t size = 0; ///< bytes
std::vector<std::filesystem::path> mountPoints;
bool isLoop = false; ///< loop device exposed for testing (OPENPU_USB_ALLOW_LOOP=1)
/// "SanDisk Ultra (/dev/sdb, 32.0 GB)"-style description.
std::string description() const;
};
class UsbError : public std::runtime_error {
public:
enum class Kind {
Unavailable, ///< no backend (UDisks2 not running...)
NotAuthorized,///< polkit / permission refused
Busy, ///< cannot unmount
Failed, ///< any other backend failure
};
UsbError(Kind kind, const std::string& what) : std::runtime_error(what), kind_(kind) {}
Kind kind() const noexcept { return kind_; }
private:
Kind kind_;
};
class UsbBackend {
public:
virtual ~UsbBackend() = default;
/// Removable USB disks (whole devices, never system disks).
virtual std::vector<UsbDrive> list() = 0;
/// Erases the drive: new MBR table with a single FAT32 partition named `label`, then mounts it.
/// Returns the mount point of the new file system.
virtual std::filesystem::path formatFat32(const UsbDrive& drive, const std::string& label) = 0;
/// Flushes, unmounts and powers off / ejects the drive.
virtual void eject(const UsbDrive& drive) = 0;
};
/// UDisks2 on Linux, Storage Kit on Haiku. Throws UsbError(Unavailable) if none can be used.
std::unique_ptr<UsbBackend> makeUsbBackend();
/// Volume label rules of the official app: keep [A-Za-z0-9_], max 11 chars, upper case, default "NEW".
std::string sanitizeLabel(std::string_view label);
/// Smallest stick size (2, 4, 8... GB, decimal) able to hold `bytes` (+10 % margin, as the official app).
std::uint64_t minimumStickSize(std::uint64_t bytes);
/// Required free size for `bytes` of payload (+10 %).
std::uint64_t withMargin(std::uint64_t bytes);
// --- writing an update -----------------------------------------------------------------------
enum class UsbStep { Verifying, Formatting, CopyingLicense, Extracting, Ejecting, Done };
struct UsbProgress {
UsbStep step = UsbStep::Verifying;
std::size_t archiveIndex = 0; ///< archive being verified / extracted
std::size_t archiveCount = 0;
std::uint64_t done = 0; ///< bytes of the current archive
std::uint64_t total = 0;
std::string detail; ///< current entry
};
using UsbProgressCallback = std::function<void(const UsbProgress&)>;
struct UsbWritePlan {
std::optional<std::filesystem::path> license; ///< copied to <root>/license/<name>
std::vector<std::filesystem::path> archives; ///< extracted in order (firmware, then map)
/// Bytes written on the stick (license + archive payloads).
std::uint64_t payloadSize() const;
};
/// Copies the license and extracts the archives below `root` (no formatting). Used by
/// writeToUsb and to prepare an already formatted / mounted stick or a test directory.
void writePayload(const std::filesystem::path& root, const UsbWritePlan& plan, const UsbProgressCallback& onProgress = {},
const CancelToken* cancel = nullptr);
/// Full sequence of the official application: verify archives, format FAT32, copy the license,
/// extract the archives, eject. Throws ArchiveError, UsbError, Cancelled.
void writeToUsb(UsbBackend& backend, const UsbDrive& drive, const std::string& label, const UsbWritePlan& plan,
const UsbProgressCallback& onProgress = {}, const CancelToken* cancel = nullptr, bool eject = true);
} // namespace openpu