FreeNOS
BootSymbolStorage.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 "BootSymbolStorage.h"
21
23 const char *symbolName)
24 : m_bootImage(bootImage)
25 , m_symbol(loadSymbol(symbolName))
26 , m_segment(loadSegment(m_symbol))
27{
28}
29
31{
33 {
35 }
36 else
37 {
39 }
40}
41
42FileSystem::Result BootSymbolStorage::read(const u64 offset, void *buffer, const Size size) const
43{
44 return m_bootImage.read(offset + m_segment.offset, buffer, size);
45}
46
51
52const BootSymbol BootSymbolStorage::loadSymbol(const char *name) const
53{
54 const String symbolName(name);
55 const BootImage image = m_bootImage.bootImage();
56 BootSymbol symbol;
57
58 // Clear symbol first
59 MemoryBlock::set(&symbol, 0, sizeof(symbol));
60
61 // Search for the given BootSymbol
62 for (uint i = 0; i < image.symbolTableCount; i++)
63 {
64 const FileSystem::Result result = m_bootImage.read(image.symbolTableOffset + (i * sizeof(BootSymbol)),
65 &symbol, sizeof(BootSymbol));
66 if (result != FileSystem::Success)
67 {
68 ERROR("failed to read BootSymbol: result = " << (int) result);
69 return symbol;
70 }
71
72 if (symbolName.equals(symbol.name))
73 {
74 return symbol;
75 }
76 }
77
78 // No match, return empty symbol
79 MemoryBlock::set(&symbol, 0, sizeof(symbol));
80 return symbol;
81}
82
84{
85 const BootImage image = m_bootImage.bootImage();
86 BootSegment segment;
87
88 // Clear segment first
89 MemoryBlock::set(&segment, 0, sizeof(segment));
90
91 if (symbol.segmentsTotalSize > 0)
92 {
93 const FileSystem::Result result = m_bootImage.read(
94 image.segmentsTableOffset + (symbol.segmentsOffset * sizeof(BootSegment)),
95 &segment,
96 sizeof(BootSegment)
97 );
98
99 if (result != FileSystem::Success)
100 {
101 ERROR("failed to read BootSegment: result = " << (int) result);
102 MemoryBlock::set(&segment, 0, sizeof(segment));
103 }
104 }
105
106 return segment;
107}
Uses a BootImage as a storage provider.
const BootImage bootImage() const
Get BootImage header.
virtual FileSystem::Result read(const u64 offset, void *buffer, const Size size) const
Reads data from the boot image.
virtual FileSystem::Result initialize()
Initialize the Storage device.
BootSymbolStorage(const BootImageStorage &bootImage, const char *symbolName)
Constructor function.
virtual u64 capacity() const
Retrieve maximum storage capacity.
const BootSymbol m_symbol
BootSymbol value.
const BootSymbol loadSymbol(const char *name) const
Loads the BootSymbol from the BootImage.
const BootSegment m_segment
BootSegment value.
virtual FileSystem::Result read(const u64 offset, void *buffer, const Size size) const
Reads data from the BootSymbol.
const BootSegment loadSegment(const BootSymbol &symbol) const
Load the BootSegment for the given BootSymbol.
const BootImageStorage & m_bootImage
Read-only reference to the BootImage storage.
static void * set(void *dest, int ch, unsigned count)
Fill memory with a constant byte.
Abstraction of strings.
Definition String.h:42
virtual bool equals(const String &str) const
Alias for compareTo().
Definition String.cpp:262
#define ERROR(msg)
Output an error message.
Definition Log.h:61
unsigned int uint
Unsigned integer number.
Definition Types.h:44
unsigned int Size
Any sane size indicator cannot go negative.
Definition Types.h:128
unsigned long long u64
Unsigned 64-bit number.
Definition Types.h:50
Result
Result code for filesystem Actions.
Definition FileSystem.h:53
BootImage contains executable programs to be loaded at system bootup.
Definition BootImage.h:45
u16 symbolTableCount
Number of entries in the symbols table.
Definition BootImage.h:59
u32 segmentsTableOffset
Offset of the segments table.
Definition BootImage.h:62
u32 symbolTableOffset
Offset of the symbol table.
Definition BootImage.h:56
Memory segment.
Definition BootImage.h:110
u32 offset
Offset in the boot image of the segment contents.
Definition BootImage.h:118
Program embedded in the BootImage.
Definition BootImage.h:85
u32 segmentsTotalSize
Total size of the BootSymbol segments.
Definition BootImage.h:102
u32 segmentsOffset
Offset in the segments table.
Definition BootImage.h:96
char name[BOOTIMAGE_NAMELEN]
Name of the boot symbol.
Definition BootImage.h:87