Files
android_vendor_pawlet/hwinfo/pawlet-hwinfo.cpp
oxmc 918038437b hwinfo: native helper for bootloader and VideoCore versions; memtier: drop awk
pawlet-hwinfo is now a C++ binary. It reads the bootloader git hash and
build-timestamp from the chosen/bootloader device-tree node and queries the
VideoCore firmware revision, hash and variant over the /dev/vcio mailbox.
Both are formatted date-first like vcgencmd/rpi-eeprom-update.

pawlet-memtier reads MemTotal with shell builtins instead of awk, which is
labelled vendor_file and not executable from the memtier domain.
2026-10-13 21:03:23 -07:00

180 lines
6.4 KiB
C++

/*
* Copyright (C) 2026 oxmc / PawletOS
*
* SPDX-License-Identifier: Apache-2.0
*
* pawlet-hwinfo: reads the Raspberry Pi bootloader (EEPROM) and VideoCore
* firmware versions at boot and exposes them as ro.vendor.pawlet.hw.*
* properties, read by PawletRpiDeviceSettings
* (pawlet_rpi_common/apps/PawletRpiDeviceSettings). Each property is only set
* when its source answers, and the app hides rows whose property is absent.
*
* Both are formatted like `vcgencmd bootloader_version` / `vcgencmd version`
* lead with: the build date, then the short git hash, e.g.
* "2024-11-12 15:15:44 (4b01994)".
*
* Bootloader: the EEPROM bootloader publishes itself in the device tree under
* chosen/bootloader (version = git hash string, build-timestamp = big-endian
* u32 unix time), the same node rpi-eeprom-update falls back to. Read via the
* real sysfs path, not the /proc/device-tree symlink: sepolicy labels the
* sysfs files. bootloader_version and eeprom_version get the same value on
* purpose -- on Pi 4/5 the bootloader lives in the SPI EEPROM, one flash, one
* version.
*
* VideoCore: not in the device tree. Queried from the firmware over the
* /dev/vcio mailbox (what vcgencmd uses), with the property tags the kernel's
* raspberrypi-firmware driver also uses: GET_FIRMWARE_REVISION (build time),
* GET_FIRMWARE_VARIANT and GET_FIRMWARE_HASH (newer firmware only).
*/
#include <fcntl.h>
#include <sys/ioctl.h>
#include <time.h>
#include <unistd.h>
#include <cstdint>
#include <cstdio>
#include <string>
#include <android-base/file.h>
#include <android-base/logging.h>
#include <android-base/properties.h>
#include <android-base/stringprintf.h>
#include <android-base/strings.h>
#include <android-base/unique_fd.h>
using android::base::ReadFileToString;
using android::base::SetProperty;
using android::base::StringPrintf;
using android::base::unique_fd;
namespace {
constexpr const char* kBootloaderNode = "/sys/firmware/devicetree/base/chosen/bootloader";
constexpr const char* kVcioDevice = "/dev/vcio";
// /dev/vcio mailbox property interface (raspberrypi/linux vcio driver).
#define IOCTL_MBOX_PROPERTY _IOWR(100, 0, char*)
constexpr uint32_t kMboxRequest = 0x00000000;
constexpr uint32_t kMboxSuccess = 0x80000000;
constexpr uint32_t kTagFirmwareRevision = 0x00000001;
constexpr uint32_t kTagFirmwareVariant = 0x00000002;
constexpr uint32_t kTagFirmwareHash = 0x00000003;
// Same order as the kernel's raspberrypi-firmware variant_strs[].
constexpr const char* kVariants[] = {"unknown", "start", "start_x", "start_db", "start_cd"};
std::string FormatTime(uint32_t timestamp) {
time_t t = timestamp;
struct tm tm;
if (gmtime_r(&t, &tm) == nullptr) return "";
char buf[32];
strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", &tm);
return buf;
}
// "<date> (<short hash>)", or whichever half is known.
std::string FormatVersion(const std::string& date, const std::string& hash) {
std::string short_hash = hash.substr(0, 7);
if (date.empty()) return short_hash;
if (short_hash.empty()) return date;
return date + " (" + short_hash + ")";
}
void SetHwProp(const std::string& name, const std::string& value) {
if (value.empty()) return;
std::string prop = "ro.vendor.pawlet.hw." + name;
if (!SetProperty(prop, value)) {
LOG(ERROR) << "failed to set " << prop;
} else {
LOG(INFO) << prop << "=" << value;
}
}
void ReadBootloader() {
std::string version;
std::string node = kBootloaderNode;
if (ReadFileToString(node + "/version", &version)) {
// Device-tree strings are NUL-terminated.
version = android::base::Trim(version.c_str());
}
std::string date;
std::string raw;
if (ReadFileToString(node + "/build-timestamp", &raw) && raw.size() >= 4) {
uint32_t ts = (uint8_t(raw[0]) << 24) | (uint8_t(raw[1]) << 16) |
(uint8_t(raw[2]) << 8) | uint8_t(raw[3]);
if (ts != 0) date = FormatTime(ts);
}
std::string value = FormatVersion(date, version);
SetHwProp("bootloader_version", value);
SetHwProp("eeprom_version", value);
}
// One mailbox property call carrying a single tag. Returns false if the
// firmware rejected the request or doesn't know the tag.
bool MboxQuery(int fd, uint32_t tag, uint32_t* out, size_t out_words) {
// header(2) + tag header(3) + value buffer + end tag(1)
uint32_t msg[2 + 3 + 8 + 1] = {};
if (out_words > 8) return false;
size_t words = 2 + 3 + out_words + 1;
msg[0] = words * sizeof(uint32_t);
msg[1] = kMboxRequest;
msg[2] = tag;
msg[3] = out_words * sizeof(uint32_t); // value buffer size
msg[4] = 0; // request
msg[5 + out_words] = 0; // end tag
if (ioctl(fd, IOCTL_MBOX_PROPERTY, msg) < 0) {
PLOG(WARNING) << "mailbox tag 0x" << std::hex << tag;
return false;
}
// msg[4] bit 31 set = response, low bits = response length.
if (msg[1] != kMboxSuccess || !(msg[4] & 0x80000000)) return false;
for (size_t i = 0; i < out_words; i++) out[i] = msg[5 + i];
return true;
}
void ReadVideoCore() {
unique_fd fd(open(kVcioDevice, O_RDWR | O_CLOEXEC));
if (fd < 0) {
PLOG(WARNING) << "open " << kVcioDevice;
return;
}
std::string date;
uint32_t revision = 0;
if (MboxQuery(fd, kTagFirmwareRevision, &revision, 1) && revision != 0) {
date = FormatTime(revision);
}
std::string hash;
uint32_t hash_words[5] = {};
if (MboxQuery(fd, kTagFirmwareHash, hash_words, 5)) {
// The kernel prints these 20 bytes in memory order (%*phN).
const uint8_t* bytes = reinterpret_cast<const uint8_t*>(hash_words);
for (size_t i = 0; i < sizeof(hash_words); i++) hash += StringPrintf("%02x", bytes[i]);
if (hash.find_first_not_of('0') == std::string::npos) hash.clear();
}
std::string value = FormatVersion(date, hash);
uint32_t variant = 0;
if (!value.empty() && MboxQuery(fd, kTagFirmwareVariant, &variant, 1) && variant > 0 &&
variant < sizeof(kVariants) / sizeof(kVariants[0])) {
// Only the non-default variants are worth showing.
if (variant != 1) value += StringPrintf(", %s", kVariants[variant]);
}
SetHwProp("vc_firmware_version", value);
}
} // namespace
int main(int /* argc */, char** argv) {
android::base::InitLogging(argv, android::base::KernelLogger);
ReadBootloader();
ReadVideoCore();
return 0;
}