MappedROM.h 988 B

123456789101112131415161718192021222324252627282930313233343536
  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. for (auto* candidate = base(); candidate < end(); candidate += chunk_size) {
  22. if (!__builtin_memcmp(prefix.characters_without_null_termination(), candidate, prefix.length()))
  23. return paddr_of(candidate);
  24. }
  25. return {};
  26. }
  27. PhysicalAddress paddr_of(const u8* ptr) const { return paddr.offset(ptr - this->base()); }
  28. };
  29. }