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 {
32  if (m_symbol.segmentsTotalSize != 0 && m_segment.offset != 0)
33  {
34  return FileSystem::Success;
35  }
36  else
37  {
38  return FileSystem::IOError;
39  }
40 }
41 
42 FileSystem::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 
48 {
50 }
51 
52 const 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 }
BootSymbolStorage::m_segment
const BootSegment m_segment
BootSegment value.
Definition: BootSymbolStorage.h:104
MemoryBlock::set
static void * set(void *dest, int ch, unsigned count)
Fill memory with a constant byte.
Definition: MemoryBlock.cpp:25
String
Abstraction of strings.
Definition: String.h:41
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
Uses a BootImage as a storage provider.
Definition: BootImageStorage.h:38
BootSymbol::segmentsOffset
u32 segmentsOffset
Offset in the segments table.
Definition: BootImage.h:96
FileSystem::IOError
@ IOError
Definition: FileSystem.h:58
BootSymbolStorage.h
MemoryBlock.h
BootImageStorage::bootImage
const BootImage bootImage() const
Get BootImage header.
Definition: BootImageStorage.cpp:27
BootSymbol::name
char name[BOOTIMAGE_NAMELEN]
Name of the boot symbol.
Definition: BootImage.h:87
FileSystem::Success
@ Success
Definition: FileSystem.h:54
BootSymbolStorage::m_bootImage
const BootImageStorage & m_bootImage
Read-only reference to the BootImage storage.
Definition: BootSymbolStorage.h:98
uint
unsigned int uint
Unsigned integer number.
Definition: Types.h:44
u64
unsigned long long u64
Unsigned 64-bit number.
Definition: Types.h:50
BootSymbolStorage::BootSymbolStorage
BootSymbolStorage(const BootImageStorage &bootImage, const char *symbolName)
Constructor function.
Definition: BootSymbolStorage.cpp:22
BootImage::segmentsTableOffset
u32 segmentsTableOffset
Offset of the segments table.
Definition: BootImage.h:62
BootSymbol::segmentsTotalSize
u32 segmentsTotalSize
Total size of the BootSymbol segments.
Definition: BootImage.h:102
BootImage::symbolTableCount
u16 symbolTableCount
Number of entries in the symbols table.
Definition: BootImage.h:59
BootSymbolStorage::capacity
virtual u64 capacity() const
Retrieve maximum storage capacity.
Definition: BootSymbolStorage.cpp:47
Size
unsigned int Size
Any sane size indicator cannot go negative.
Definition: Types.h:128
BootSymbolStorage::initialize
virtual FileSystem::Result initialize()
Initialize the Storage device.
Definition: BootSymbolStorage.cpp:30
BootSymbol
Program embedded in the BootImage.
Definition: BootImage.h:84
ERROR
#define ERROR(msg)
Output an error message.
Definition: Log.h:61
FileSystem::Result
Result
Result code for filesystem Actions.
Definition: FileSystem.h:52
BootImage::symbolTableOffset
u32 symbolTableOffset
Offset of the symbol table.
Definition: BootImage.h:56
BootImage
BootImage contains executable programs to be loaded at system bootup.
Definition: BootImage.h:44
BootSymbolStorage::m_symbol
const BootSymbol m_symbol
BootSymbol value.
Definition: BootSymbolStorage.h:101
BootSymbolStorage::loadSymbol
const BootSymbol loadSymbol(const char *name) const
Loads the BootSymbol from the BootImage.
Definition: BootSymbolStorage.cpp:52
BootSegment::offset
u32 offset
Offset in the boot image of the segment contents.
Definition: BootImage.h:118
BootSegment
Memory segment.
Definition: BootImage.h:109
String::equals
virtual bool equals(const String &str) const
Alias for compareTo().
Definition: String.cpp:262
BootSymbolStorage::read
virtual FileSystem::Result read(const u64 offset, void *buffer, const Size size) const
Reads data from the BootSymbol.
Definition: BootSymbolStorage.cpp:42
BootSymbolStorage::loadSegment
const BootSegment loadSegment(const BootSymbol &symbol) const
Load the BootSegment for the given BootSymbol.
Definition: BootSymbolStorage.cpp:83