FreeNOS
BootImageStorage.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2009 Niek Linnenbank
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include <FreeNOS/User.h>
19 #include <MemoryBlock.h>
20 #include "BootImageStorage.h"
21 
23  : m_image(image != ZERO ? image : load())
24 {
25 }
26 
28 {
30  read(0, &header, sizeof(header));
31  return header;
32 }
33 
35 {
36  if (m_image == ZERO)
37  {
38  return FileSystem::IOError;
39  }
40 
41  if (m_image->magic[0] == BOOTIMAGE_MAGIC0 &&
44  {
45  return FileSystem::Success;
46  }
47  else
48  {
49  ERROR("invalid BootImage: signature = " <<
50  m_image->magic[0] << ", " << m_image->magic[1] <<
51  " revision = " << m_image->layoutRevision);
53  }
54 }
55 
56 FileSystem::Result BootImageStorage::read(const u64 offset, void *buffer, const Size size) const
57 {
58  const u8 *data = ((const u8 *)(m_image)) + offset;
59  MemoryBlock::copy(buffer, data, size);
60  return FileSystem::Success;
61 }
62 
64 {
65  return m_image->bootImageSize;
66 }
67 
69 {
70  const SystemInformation info;
71  Memory::Range range;
72 
73  // Request boot image memory
74  range.size = info.bootImageSize;
75  range.access = Memory::User |
77  range.virt = ZERO;
78  range.phys = info.bootImageAddress;
79 
80  // Map BootImage into our address space
81  const API::Result r = VMCtl(SELF, MapContiguous, &range);
82  if (r != API::Success)
83  {
84  ERROR("failed to map BootImage using VMCtl: result = " << (int) r);
85  return ZERO;
86  }
87 
88  return (const BootImage *) range.virt;
89 }
MemoryBlock::copy
static Size copy(void *dest, const void *src, Size count)
Copy memory from one place to another.
Definition: MemoryBlock.cpp:36
Memory::Range
Memory range.
Definition: Memory.h:55
API::Result
Result
Enumeration of generic kernel API result codes.
Definition: API.h:68
Memory::User
@ User
Definition: Memory.h:44
BootImageStorage::read
virtual FileSystem::Result read(const u64 offset, void *buffer, const Size size) const
Reads data from the boot image.
Definition: BootImageStorage.cpp:56
BootImageStorage::capacity
virtual u64 capacity() const
Retrieve maximum storage capacity.
Definition: BootImageStorage.cpp:63
FileSystem::InvalidArgument
@ InvalidArgument
Definition: FileSystem.h:55
FileSystem::IOError
@ IOError
Definition: FileSystem.h:58
MemoryBlock.h
BootImageStorage::bootImage
const BootImage bootImage() const
Get BootImage header.
Definition: BootImageStorage.cpp:27
VMCtl
API::Result VMCtl(const ProcessID procID, const MemoryOperation op, Memory::Range *range=ZERO)
Prototype for user applications.
Definition: VMCtl.h:61
BOOTIMAGE_MAGIC0
#define BOOTIMAGE_MAGIC0
First magic byte.
Definition: BootImage.h:30
FileSystem::Success
@ Success
Definition: FileSystem.h:54
Memory::Readable
@ Readable
Definition: Memory.h:41
BootImage::bootImageSize
u32 bootImageSize
Total size of the boot image in bytes.
Definition: BootImage.h:53
SELF
#define SELF
Definition: ProcessID.h:35
u64
unsigned long long u64
Unsigned 64-bit number.
Definition: Types.h:50
Memory::Range::phys
Address phys
Physical address.
Definition: Memory.h:58
header
SystemDescriptorHeader header
Definition: IntelACPI.h:63
Size
unsigned int Size
Any sane size indicator cannot go negative.
Definition: Types.h:128
SystemInformation::bootImageSize
Size bootImageSize
BootImage size.
Definition: SystemInfo.h:111
BootImageStorage::initialize
virtual FileSystem::Result initialize()
Initialize the Storage device.
Definition: BootImageStorage.cpp:34
BootImageStorage::m_image
const BootImage * m_image
Pointer to the BootImage.
Definition: BootImageStorage.h:93
ERROR
#define ERROR(msg)
Output an error message.
Definition: Log.h:61
SystemInformation
System information structure.
Definition: SystemInfo.h:79
FileSystem::Result
Result
Result code for filesystem Actions.
Definition: FileSystem.h:52
u8
unsigned char u8
Unsigned 8-bit number.
Definition: Types.h:59
BootImageStorage::BootImageStorage
BootImageStorage(const BootImage *image=ZERO)
Constructor function.
Definition: BootImageStorage.cpp:22
BootImage::layoutRevision
u8 layoutRevision
Version of the boot image layout.
Definition: BootImage.h:50
API::Success
@ Success
Definition: API.h:70
BOOTIMAGE_REVISION
#define BOOTIMAGE_REVISION
Version of the boot image layout.
Definition: BootImage.h:36
Memory::Range::virt
Address virt
Virtual address.
Definition: Memory.h:57
BootImageStorage.h
BootImage
BootImage contains executable programs to be loaded at system bootup.
Definition: BootImage.h:44
Memory::Range::size
Size size
Size in number of bytes.
Definition: Memory.h:59
Memory::Range::access
Access access
Page access flags.
Definition: Memory.h:60
BootImage::magic
u32 magic[2]
Magic numbers to detect a valid boot image.
Definition: BootImage.h:47
ZERO
#define ZERO
Zero value.
Definition: Macros.h:43
MapContiguous
@ MapContiguous
Definition: VMCtl.h:37
BOOTIMAGE_MAGIC1
#define BOOTIMAGE_MAGIC1
Second magic byte.
Definition: BootImage.h:33
SystemInformation::bootImageAddress
Address bootImageAddress
BootImage physical address.
Definition: SystemInfo.h:108
BootImageStorage::load
const BootImage * load() const
Loads the BootImage into virtual memory.
Definition: BootImageStorage.cpp:68