MappedROM.h 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/OwnPtr.h>
  8. #include <Kernel/Memory/Region.h>
  9. #include <Kernel/PhysicalAddress.h>
  10. namespace Kernel::Memory {
  11. class MappedROM {
  12. public:
  13. const u8* base() const { return region->vaddr().offset(offset).as_ptr(); }
  14. const u8* end() const { return base() + size; }
  15. OwnPtr<Region> region;
  16. size_t size { 0 };
  17. size_t offset { 0 };
  18. PhysicalAddress paddr;
  19. Optional<PhysicalAddress> find_chunk_starting_with(StringView prefix, size_t chunk_size) const
  20. {
  21. auto prefix_length = prefix.length();
  22. if (size < prefix_length)
  23. return {};
  24. for (auto* candidate = base(); candidate <= end() - prefix_length; candidate += chunk_size) {
  25. if (!__builtin_memcmp(prefix.characters_without_null_termination(), candidate, prefix.length()))
  26. return paddr_of(candidate);
  27. }
  28. return {};
  29. }
  30. PhysicalAddress paddr_of(const u8* ptr) const { return paddr.offset(ptr - this->base()); }
  31. };
  32. }