Files
android_vendor_pawlet/hwupdate/daemon/http.cpp
T
oxmc 4e9f147e55 hwupdate: PawletOS hardware/peripheral firmware update subsystem
Adds pawlethwd, a native C++ system_ext daemon (coredomain) that detects
peripherals, checks the hardware update catalog (ota.php?mode=hardware),
verifies payloads, and flashes firmware via authorized handler plugins.

- daemon/: manifest parse, libcurl HTTP + sha256, sysfs USB detection,
  catalog client, dlopen handler registry (fixed path + ABI + allowlist),
  state persistence, orchestration engine, AIDL service, main.
- aidl/: IPawletHardwareUpdate (getComponents/getPendingUpdates/checkNow/...).
- include/pawlet/hwh_abi.h: stable C plugin ABI.
- handlers/usbdfu/: libpawlethwh_usbdfu.so, DFU 1.1 download over libusb.
- sepolicy/: pawlethwd domain, pawlet_hw_data_file, service type.
- schemas/, etc/supported_hardware.default.json, README design doc.
- hwupdate.mk wires packages + ro.pawlet.hardware.manifest + sepolicy dir;
  inherited from config/common.mk.

Trust: a server-named installer handler is honored only if allowlisted in
the immutable supported_hardware.json, maps to a fixed dm-verity-protected
.so path, is ABI/id-checked, and every payload is sha256-verified before a
handler runs.
2026-07-18 01:11:15 -07:00

97 lines
2.9 KiB
C++

/*
* Copyright (C) 2026 oxmc / PawletOS
* SPDX-License-Identifier: Apache-2.0
*/
#include "http.h"
#include <curl/curl.h>
#include <cstdio>
#include <android-base/logging.h>
namespace pawlet::hwupdate::http {
namespace {
constexpr long kConnectTimeoutSec = 15;
constexpr long kTransferTimeoutSec = 300;
constexpr char kUserAgent[] = "pawlethwd/1.0";
size_t WriteToString(char* ptr, size_t size, size_t nmemb, void* userdata) {
auto* out = static_cast<std::string*>(userdata);
out->append(ptr, size * nmemb);
return size * nmemb;
}
size_t WriteToFile(char* ptr, size_t size, size_t nmemb, void* userdata) {
auto* f = static_cast<FILE*>(userdata);
return fwrite(ptr, size, nmemb, f) * size;
}
void ApplyCommon(CURL* c, const std::string& url) {
curl_easy_setopt(c, CURLOPT_URL, url.c_str());
curl_easy_setopt(c, CURLOPT_USERAGENT, kUserAgent);
curl_easy_setopt(c, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(c, CURLOPT_MAXREDIRS, 5L);
curl_easy_setopt(c, CURLOPT_CONNECTTIMEOUT, kConnectTimeoutSec);
curl_easy_setopt(c, CURLOPT_TIMEOUT, kTransferTimeoutSec);
curl_easy_setopt(c, CURLOPT_FAILONERROR, 0L);
curl_easy_setopt(c, CURLOPT_NOSIGNAL, 1L);
// HTTPS only, verified against the system CA store.
curl_easy_setopt(c, CURLOPT_SSL_VERIFYPEER, 1L);
curl_easy_setopt(c, CURLOPT_SSL_VERIFYHOST, 2L);
curl_easy_setopt(c, CURLOPT_CAPATH, "/system/etc/security/cacerts");
}
} // namespace
bool Get(const std::string& url, std::string* body, long* httpCode) {
CURL* c = curl_easy_init();
if (!c) return false;
body->clear();
ApplyCommon(c, url);
curl_easy_setopt(c, CURLOPT_WRITEFUNCTION, WriteToString);
curl_easy_setopt(c, CURLOPT_WRITEDATA, body);
CURLcode rc = curl_easy_perform(c);
if (rc == CURLE_OK && httpCode) {
curl_easy_getinfo(c, CURLINFO_RESPONSE_CODE, httpCode);
}
curl_easy_cleanup(c);
if (rc != CURLE_OK) {
LOG(WARNING) << "hwupdate: GET failed: " << curl_easy_strerror(rc);
return false;
}
return true;
}
bool Download(const std::string& url, const std::string& destPath) {
FILE* f = fopen(destPath.c_str(), "wb");
if (!f) {
LOG(ERROR) << "hwupdate: cannot open " << destPath << " for write";
return false;
}
CURL* c = curl_easy_init();
if (!c) {
fclose(f);
return false;
}
ApplyCommon(c, url);
curl_easy_setopt(c, CURLOPT_WRITEFUNCTION, WriteToFile);
curl_easy_setopt(c, CURLOPT_WRITEDATA, f);
CURLcode rc = curl_easy_perform(c);
long code = 0;
curl_easy_getinfo(c, CURLINFO_RESPONSE_CODE, &code);
curl_easy_cleanup(c);
fclose(f);
if (rc != CURLE_OK || code != 200) {
LOG(WARNING) << "hwupdate: download failed rc=" << curl_easy_strerror(rc)
<< " http=" << code;
remove(destPath.c_str());
return false;
}
return true;
}
} // namespace pawlet::hwupdate::http