Kernel: Allow to boot from a partition with partition UUID

Instead of specifying the boot argument to be root=/dev/hdXY, now
one can write root=PARTUUID= with the right UUID, and if the partition
is found, the kernel will boot from it.

This feature is mainly used with GUID partitions, and is considered to
be the most reliable way for the kernel to identify partitions.
This commit is contained in:
Liav A 2020-12-31 13:17:03 +02:00 committed by Andreas Kling
parent d22d29a29a
commit 9dc8bea3e7
Notes: sideshowbarker 2024-07-19 00:15:15 +09:00
9 changed files with 162 additions and 62 deletions

View file

@ -24,7 +24,8 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <AK/ByteBuffer.h>
#include <AK/AllOf.h>
#include <AK/Array.h>
#include <Kernel/Storage/Partition/GUIDPartitionTable.h>
#ifndef GPT_DEBUG
@ -121,31 +122,28 @@ bool GUIDPartitionTable::initialize()
}
auto* entries = (const GPTPartitionEntry*)entries_buffer.data();
auto& entry = entries[entry_index % (m_device->block_size() / (size_t)header().partition_entry_size)];
ByteBuffer partition_type = ByteBuffer::copy(entry.partition_guid, 16);
Array<u8, 16> partition_type {};
partition_type.span().overwrite(0, entry.partition_guid, partition_type.size());
if (is_unused_entry(partition_type)) {
raw_byte_index += header().partition_entry_size;
continue;
}
ByteBuffer unique_guid = ByteBuffer::copy(entry.unique_guid, 16);
Array<u8, 16> unique_guid {};
unique_guid.span().overwrite(0, entry.unique_guid, unique_guid.size());
String name = entry.partition_name;
dbg() << "Detected GPT partition (entry " << entry_index << ") , offset " << entry.first_lba << " , limit " << entry.last_lba;
m_partitions.append(DiskPartitionMetadata({ entry.first_lba, entry.last_lba, partition_type }));
m_partitions.append({ entry.first_lba, entry.last_lba, partition_type, unique_guid, entry.attributes, "" });
raw_byte_index += header().partition_entry_size;
}
return true;
}
bool GUIDPartitionTable::is_unused_entry(ByteBuffer partition_type) const
bool GUIDPartitionTable::is_unused_entry(Array<u8, 16> partition_type) const
{
ASSERT(partition_type.size() == 16);
for (size_t byte_index = 0; byte_index < 16; byte_index++) {
if (partition_type[byte_index] != 0)
return false;
}
return true;
return all_of(partition_type.begin(), partition_type.end(), [](const auto octet) { return octet == 0; });
}
}