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
28Archive::Archive(uint8_t* pPhys, size_t sSize) : m_pBase(pPhys), m_Region("Archive") {
29 EMIT_IF(!HOSTED) {
30 if ((reinterpret_cast<physical_uintptr_t>(pPhys) &
32 panic("Archive: Alignment issues");
33
34 if (PhysicalMemoryManager::instance().allocateRegion(
35 m_Region,
39 reinterpret_cast<physical_uintptr_t>(pPhys)) == false) {
40 ERROR("Archive: allocateRegion failed.");
41 } else {
42 NOTICE("Archive: mapped to " << m_Region.virtualAddress());
43 }
44 }
45}
46
48 m_Region.free();
49}
50
52 size_t i = 0;
53 ArchiveFile* pFile = getFirst();
54 while (pFile != 0) {
55 i++;
56 pFile = getNext(pFile);
57 }
58 return i;
59}
60
61size_t Archive::getFileSize(size_t n) {
62 ArchiveFile* pFile = get(n);
63 NormalStaticString str(pFile->size);
64 return str.intValue(8); // Octal
65}
66
67char* Archive::getFileName(size_t n) {
68 return get(n)->name;
69}
70
71uintptr_t* Archive::getFile(size_t n) {
72 return reinterpret_cast<uintptr_t*>(reinterpret_cast<uintptr_t>(get(n)) + 512);
73}
74
75Archive::ArchiveFile* Archive::getFirst() {
76 EMIT_IF(HOSTED) {
77 return reinterpret_cast<ArchiveFile*>(m_pBase);
78 }
79 else {
80 return reinterpret_cast<ArchiveFile*>(m_Region.virtualAddress());
81 }
82}
83
84Archive::ArchiveFile* Archive::getNext(ArchiveFile* pFile) {
85 NormalStaticString str(pFile->size);
86 size_t size = str.intValue(8); // Octal.
87 size_t nBlocks = (size + 511) / 512;
88 pFile = adjust_pointer(pFile, 512 * (nBlocks + 1));
89 if (pFile->name[0] == '\0')
90 return 0;
91 return pFile;
92}
93
94Archive::ArchiveFile* Archive::get(size_t n) {
95 ArchiveFile* pFile = getFirst();
96 for (size_t i = 0; i < n; i++)
97 pFile = getNext(pFile);
98 return pFile;
99}
Archive(uint8_t *pPhys, size_t sSize)
Definition Archive.cc:28
~Archive()
Definition Archive.cc:47
uintptr_t * getFile(size_t n)
Definition Archive.cc:71
size_t getNumFiles()
Definition Archive.cc:51
char * getFileName(size_t n)
Definition Archive.cc:67
size_t getFileSize(size_t n)
Definition Archive.cc:61
void * virtualAddress() const
static PhysicalMemoryManager & instance()
void EXPORTED_PUBLIC panic(const char *msg) NORETURN
Definition panic.cc:118