The Pedigree Project  0.1
Archive.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 "pedigree/kernel/Archive.h"
21 #include "pedigree/kernel/Log.h"
22 #include "pedigree/kernel/panic.h"
23 #include "pedigree/kernel/processor/PhysicalMemoryManager.h"
24 #include "pedigree/kernel/processor/VirtualAddressSpace.h"
25 #include "pedigree/kernel/utilities/StaticString.h"
26 #include "pedigree/kernel/utilities/utility.h"
27 
28 Archive::Archive(uint8_t *pPhys, size_t sSize)
29  :
30 #ifdef HOSTED
31  m_pBase(pPhys)
32 #else
33  m_Region("Archive")
34 #endif
35 {
36 #ifndef HOSTED
37  if ((reinterpret_cast<physical_uintptr_t>(pPhys) &
39  panic("Archive: Alignment issues");
40 
41  if (PhysicalMemoryManager::instance().allocateRegion(
42  m_Region,
43  (sSize + PhysicalMemoryManager::getPageSize() - 1) /
46  reinterpret_cast<physical_uintptr_t>(pPhys)) == false)
47  {
48  ERROR("Archive: allocateRegion failed.");
49  }
50 #endif
51 }
52 
54 {
55 #ifndef HOSTED
56  m_Region.free();
57 #endif
58 }
59 
61 {
62  size_t i = 0;
63  ArchiveFile *pFile = getFirst();
64  while (pFile != 0)
65  {
66  i++;
67  pFile = getNext(pFile);
68  }
69  return i;
70 }
71 
72 size_t Archive::getFileSize(size_t n)
73 {
74  ArchiveFile *pFile = get(n);
75  NormalStaticString str(pFile->size);
76  return str.intValue(8); // Octal
77 }
78 
79 char *Archive::getFileName(size_t n)
80 {
81  return get(n)->name;
82 }
83 
84 uintptr_t *Archive::getFile(size_t n)
85 {
86  return reinterpret_cast<uintptr_t *>(
87  reinterpret_cast<uintptr_t>(get(n)) + 512);
88 }
89 
90 Archive::ArchiveFile *Archive::getFirst()
91 {
92 #ifdef HOSTED
93  return reinterpret_cast<ArchiveFile *>(m_pBase);
94 #else
95  return reinterpret_cast<ArchiveFile *>(m_Region.virtualAddress());
96 #endif
97 }
98 
99 Archive::ArchiveFile *Archive::getNext(ArchiveFile *pFile)
100 {
101  NormalStaticString str(pFile->size);
102  size_t size = str.intValue(8); // Octal.
103  size_t nBlocks = (size + 511) / 512;
104  pFile = adjust_pointer(pFile, 512 * (nBlocks + 1));
105  if (pFile->name[0] == '\0')
106  return 0;
107  return pFile;
108 }
109 
110 Archive::ArchiveFile *Archive::get(size_t n)
111 {
112  ArchiveFile *pFile = getFirst();
113  for (size_t i = 0; i < n; i++)
114  pFile = getNext(pFile);
115  return pFile;
116 }
uintptr_t * getFile(size_t n)
Definition: Archive.cc:84
static PhysicalMemoryManager & instance()
~Archive()
Definition: Archive.cc:53
char * getFileName(size_t n)
Definition: Archive.cc:79
size_t getNumFiles()
Definition: Archive.cc:60
Archive(uint8_t *pPhys, size_t sSize)
Definition: Archive.cc:28
void * virtualAddress() const
Definition: MemoryRegion.cc:39
#define ERROR(text)
Definition: Log.h:82
void EXPORTED_PUBLIC panic(const char *msg) NORETURN
Definition: panic.cc:121
size_t getFileSize(size_t n)
Definition: Archive.cc:72