The Pedigree Project  0.1
DiskImage.cc
1 /*
2  * Copyright (c) 2008-2014, Pedigree Developers
3  *
4  * Please see the CONTRIB file in the root of the source tree for a full
5  * list of contributors.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #include "DiskImage.h"
21 #include "pedigree/kernel/BootstrapInfo.h"
22 #include "pedigree/kernel/Log.h"
23 
24 extern BootstrapStruct_t *g_pBootstrapInfo;
25 
26 bool DiskImage::initialise()
27 {
28  if (g_pBootstrapInfo->getModuleCount() < 3)
29  {
30  NOTICE("not enough modules to create a DiskImage");
31  return false;
32  }
33 
34  uintptr_t baseAddress = g_pBootstrapInfo->getModuleArray()[2].base;
35  uintptr_t endAddress = g_pBootstrapInfo->getModuleArray()[2].end;
36 
37  m_pBase = reinterpret_cast<void *>(baseAddress);
38  m_nSize = endAddress - baseAddress;
39  return true;
40 }
41 
42 uintptr_t DiskImage::read(uint64_t location)
43 {
44  if ((location > m_nSize) || !m_pBase)
45  {
46  ERROR("DiskImage::read() - location " << location << " > " << m_nSize);
47  ERROR(" -> or " << m_pBase << " is null");
48  return ~0;
49  }
50 
51  uint64_t offset = location % getBlockSize();
52  location &= ~(getBlockSize() - 1);
53 
54  uintptr_t buffer = m_Cache.lookup(location);
55  if (buffer)
56  {
57  return buffer + offset;
58  }
59 
60  buffer = m_Cache.insert(location, getBlockSize());
61  MemoryCopy(
62  reinterpret_cast<void *>(buffer), adjust_pointer(m_pBase, location),
63  getBlockSize());
64 
65  m_Cache.markNoLongerEditing(location);
66 
67  return buffer + offset;
68 }
69 
70 size_t DiskImage::getSize() const
71 {
72  return m_nSize;
73 }
74 
75 void DiskImage::pin(uint64_t location)
76 {
77  m_Cache.pin(location);
78 }
79 
80 void DiskImage::unpin(uint64_t location)
81 {
82  m_Cache.release(location);
83 }
Bootstrap structure passed to the kernel entry point.
uintptr_t insert(uintptr_t key, bool *alreadyExisted=nullptr)
Definition: Cache.cc:258
void markNoLongerEditing(uintptr_t key, size_t length=0)
Definition: Cache.cc:919
virtual size_t getBlockSize() const
Gets the block size of the disk.
Definition: DiskImage.h:54
virtual uintptr_t read(uint64_t location)
Definition: DiskImage.cc:42
virtual void unpin(uint64_t location)
Definition: DiskImage.cc:80
virtual void pin(uint64_t location)
Pins a cache page.
Definition: DiskImage.cc:75
uintptr_t lookup(uintptr_t key)
Definition: Cache.cc:235
#define NOTICE(text)
Definition: Log.h:74
void release(uintptr_t key)
Definition: Cache.cc:578
bool pin(uintptr_t key)
Definition: Cache.cc:557
#define ERROR(text)
Definition: Log.h:82
virtual size_t getSize() const
Gets the size of the disk.
Definition: DiskImage.cc:70