The Pedigree Project 0.1
SwapHeader.cc
1/* Copyright (c) 2026, Pedigree Developers. */
2#include "pedigree/kernel/processor/PhysicalMemoryManager.h"
3#include "pedigree/kernel/utilities/utility.h"
4
5#include "SwapStore.h"
6
7SwapStatus SwapStore::validateHeader(const void* page, uint64_t bytes, size_t& pages) {
8 pages = 0;
9 constexpr size_t Page = PagingChannel::PageBytes;
10 if (!page || PhysicalMemoryManager::getPageSize() != Page || bytes % Page || bytes < 2 * Page)
11 return SwapStatus::Invalid;
12 const auto* data = static_cast<const unsigned char*>(page);
13 if (MemoryCompare(data + Page - 10, "SWAPSPACE2", 10))
14 return SwapStatus::Invalid;
15 // Linux version-one fields are little-endian on the supported x64 ABI.
16 auto word = [&](size_t at) {
17 return uint32_t(data[at]) | (uint32_t(data[at + 1]) << 8) | (uint32_t(data[at + 2]) << 16) |
18 (uint32_t(data[at + 3]) << 24);
19 };
20 if (word(1024) != 1 || !word(1028) || uint64_t(word(1028)) >= bytes / Page)
21 return SwapStatus::Invalid;
22 if (word(1032))
23 return SwapStatus::Unsupported;
24 pages = word(1028) < MaximumPages ? word(1028) : MaximumPages;
25 return SwapStatus::Success;
26}