Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 5 additions & 10 deletions 15 rpiidp/src/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -632,17 +632,12 @@ bool IDPdeviceWriter::WritePhysicalPartitions(std::string& reason)
return false;
}

if (device_->image_.device_storage.ptable_type == IDPptable_type::DOS) {
ret = parted.createPartitionTable("DOS", std::nullopt);
}
else {
std::optional<std::string> id = std::nullopt;
if (device_->image_.device_storage.ptable_id &&
!device_->image_.device_storage.ptable_id->empty()) {
id = device_->image_.device_storage.ptable_id;
}
ret = parted.createPartitionTable("GPT", id);
std::optional<std::string> id = std::nullopt;
if (device_->image_.device_storage.ptable_id &&
!device_->image_.device_storage.ptable_id->empty()) {
id = device_->image_.device_storage.ptable_id;
}
ret = parted.createPartitionTable(type, id);

if (!ret) {
reason = "parted: failed to create " + type + " partition table on " + dev;
Expand Down
24 changes: 16 additions & 8 deletions 24 rpiidp/src/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -459,29 +459,37 @@ bool IDPparser::parseIGv2(const Json::Value& json, const IDPversion& version, st
return false;
}

// Set partition table attrs
size_t align = fromGIsz(str);
if (align % (1024*1024) != 0) {
ERR("Partition alignment must be a multiple of 1MB.");
return false;
}
image_.device_storage.ptable_align = align;


const Json::Value& ptable = json["layout"]["partitiontable"]["label"];

if (ptable.asString() == "dos")
image_.device_storage.ptable_type = IDPptable_type::DOS;
if (ptable.asString() == "gpt")
else if (ptable.asString() == "gpt")
image_.device_storage.ptable_type = IDPptable_type::GPT;
else {
ERR("partitiontable: invalid 'label'");
return false;
}

if (image_.device_storage.ptable_type == IDPptable_type::GPT) {
const Json::Value& id = json["layout"]["partitiontable"]["id"];
if (id.isString() && !id.asString().empty())
image_.device_storage.ptable_id = id.asString();
else
image_.device_storage.ptable_id.reset();
const Json::Value& id = json["layout"]["partitiontable"]["id"];

if (!id.isNull() && !id.isString()) {
ERR("partitiontable: 'id' must be a string");
return false;
}

if (id.isString() && !id.asString().empty())
image_.device_storage.ptable_id = id.asString();
else
image_.device_storage.ptable_id.reset();

if (!checkPMAPCompat(json, str, error)) {
ERR("PMAP compat check failed: " << error);
return false;
Expand Down
41 changes: 36 additions & 5 deletions 41 rpiparted/src/rpiparted.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <iostream>
#include <algorithm>
#include <cerrno>
#include <cstdlib>
#include <cstring>
#include <unistd.h>

Expand All @@ -13,6 +14,23 @@
#define ASSERT(cond, msg) do { if (!(cond)) { ERR(msg); std::abort(); } } while(0)


namespace {
// Check a disklabel id string for being nothing but zeros. libfdisk
// does not reject an all-zero id.
bool is_zero_id(const std::string& id) {
std::string digits = id;
if (digits.size() > 1 && digits[0] == '0' && (digits[1] == 'x' || digits[1] == 'X')) {
digits.erase(0, 2);
}
digits.erase(std::remove(digits.begin(), digits.end(), '-'), digits.end());

return !digits.empty() && std::all_of(digits.begin(), digits.end(), [](unsigned char c) {
return c == '0';
});
}
}


FdiskContextDeleter::FdiskContextDeleter(bool* assigned)
: assigned_(assigned) {}

Expand Down Expand Up @@ -106,14 +124,27 @@ bool RPIparted::createPartitionTable(const std::string& type, const std::optiona
const char* label_type = (type_lower == "mbr") ? "dos" : type_lower.c_str();

if (fdisk_create_disklabel(context_.get(), label_type) != 0) {
ERR("Failed to create label " << type);
ERR("Failed to create disk label for type " << type);
return false;
}

if (id && !id->empty() && \
fdisk_set_disklabel_id_from_string(context_.get(), id->c_str())) {
ERR("Invalid label ID " << id->c_str());
return false;
// Set disk label from user, else keep the one just generated
if (id && !id->empty()) {
if (fdisk_set_disklabel_id_from_string(context_.get(), id->c_str())) {
ERR("Invalid disk label (rejected) " << id->c_str() << " for type " << type);
return false;
}

// Reject an all-zero ID
char* applied_id = nullptr;
if (fdisk_get_disklabel_id(context_.get(), &applied_id) == 0 && applied_id) {
bool zero = is_zero_id(applied_id);
free(applied_id);
if (zero) {
ERR("Invalid disk label (rejected all-zero) " << id->c_str() << " for type " << type);
return false;
}
}
}

is_gpt_ = (type_lower == "gpt");
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.