2014-04-14 16:05:20 +02:00
|
|
|
/*
|
|
|
|
* \brief ROM filesystem
|
|
|
|
* \author Norman Feske
|
|
|
|
* \date 2014-04-14
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2017-02-20 13:23:52 +01:00
|
|
|
* Copyright (C) 2014-2017 Genode Labs GmbH
|
2014-04-14 16:05:20 +02:00
|
|
|
*
|
|
|
|
* This file is part of the Genode OS framework, which is distributed
|
2017-02-20 13:23:52 +01:00
|
|
|
* under the terms of the GNU Affero General Public License version 3.
|
2014-04-14 16:05:20 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _INCLUDE__VFS__ROM_FILE_SYSTEM_H_
|
|
|
|
#define _INCLUDE__VFS__ROM_FILE_SYSTEM_H_
|
|
|
|
|
2016-05-25 15:47:22 +02:00
|
|
|
#include <base/attached_rom_dataspace.h>
|
2014-04-14 16:05:20 +02:00
|
|
|
#include <vfs/file_system.h>
|
|
|
|
|
|
|
|
namespace Vfs { class Rom_file_system; }
|
|
|
|
|
|
|
|
|
|
|
|
class Vfs::Rom_file_system : public Single_file_system
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
|
2018-07-04 14:59:02 +02:00
|
|
|
enum Rom_type { ROM_TEXT, ROM_BINARY };
|
|
|
|
|
2019-01-21 10:48:39 +01:00
|
|
|
typedef String<64> Label;
|
|
|
|
|
|
|
|
Label const _label;
|
|
|
|
|
|
|
|
bool const _binary;
|
2014-04-14 16:05:20 +02:00
|
|
|
|
|
|
|
Genode::Attached_rom_dataspace _rom;
|
|
|
|
|
2017-08-15 20:51:53 +02:00
|
|
|
class Rom_vfs_handle : public Single_vfs_handle
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
|
|
|
|
Genode::Attached_rom_dataspace &_rom;
|
|
|
|
|
2018-07-04 14:59:02 +02:00
|
|
|
file_size const _content_size;
|
|
|
|
|
|
|
|
file_size _init_content_size(Rom_type type)
|
|
|
|
{
|
|
|
|
if (type == ROM_TEXT) {
|
|
|
|
for (file_size pos = 0; pos < _rom.size(); pos++)
|
|
|
|
if (_rom.local_addr<char>()[pos] == 0x00)
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
return _rom.size();
|
|
|
|
}
|
|
|
|
|
2017-08-15 20:51:53 +02:00
|
|
|
public:
|
|
|
|
|
|
|
|
Rom_vfs_handle(Directory_service &ds,
|
|
|
|
File_io_service &fs,
|
|
|
|
Genode::Allocator &alloc,
|
2018-07-04 14:59:02 +02:00
|
|
|
Genode::Attached_rom_dataspace &rom,
|
|
|
|
Rom_type type)
|
2020-01-14 23:05:30 +01:00
|
|
|
:
|
|
|
|
Single_vfs_handle(ds, fs, alloc, 0),
|
|
|
|
_rom(rom), _content_size(_init_content_size(type))
|
|
|
|
{ }
|
2017-08-15 20:51:53 +02:00
|
|
|
|
|
|
|
Read_result read(char *dst, file_size count,
|
|
|
|
file_size &out_count) override
|
|
|
|
{
|
|
|
|
/* file read limit is the size of the dataspace */
|
2018-07-04 14:59:02 +02:00
|
|
|
file_size const max_size = _content_size;
|
2017-08-15 20:51:53 +02:00
|
|
|
|
|
|
|
/* current read offset */
|
|
|
|
file_size const read_offset = seek();
|
|
|
|
|
|
|
|
/* maximum read offset, clamped to dataspace size */
|
|
|
|
file_size const end_offset = min(count + read_offset, max_size);
|
|
|
|
|
|
|
|
/* check if end of file is reached */
|
|
|
|
if (read_offset >= end_offset) {
|
|
|
|
out_count = 0;
|
|
|
|
return READ_OK;
|
|
|
|
}
|
|
|
|
|
2020-01-14 23:05:30 +01:00
|
|
|
/* source address within the dataspace */
|
|
|
|
char const *src = _rom.local_addr<char>() + read_offset;
|
|
|
|
|
2017-08-15 20:51:53 +02:00
|
|
|
/* copy-out bytes from ROM dataspace */
|
|
|
|
file_size const num_bytes = end_offset - read_offset;
|
|
|
|
|
|
|
|
memcpy(dst, src, num_bytes);
|
|
|
|
|
|
|
|
out_count = num_bytes;
|
|
|
|
return READ_OK;
|
|
|
|
}
|
|
|
|
|
Follow practices suggested by "Effective C++"
The patch adjust the code of the base, base-<kernel>, and os repository.
To adapt existing components to fix violations of the best practices
suggested by "Effective C++" as reported by the -Weffc++ compiler
argument. The changes follow the patterns outlined below:
* A class with virtual functions can no longer publicly inherit base
classed without a vtable. The inherited object may either be moved
to a member variable, or inherited privately. The latter would be
used for classes that inherit 'List::Element' or 'Avl_node'. In order
to enable the 'List' and 'Avl_tree' to access the meta data, the
'List' must become a friend.
* Instead of adding a virtual destructor to abstract base classes,
we inherit the new 'Interface' class, which contains a virtual
destructor. This way, single-line abstract base classes can stay
as compact as they are now. The 'Interface' utility resides in
base/include/util/interface.h.
* With the new warnings enabled, all member variables must be explicitly
initialized. Basic types may be initialized with '='. All other types
are initialized with braces '{ ... }' or as class initializers. If
basic types and non-basic types appear in a row, it is nice to only
use the brace syntax (also for basic types) and align the braces.
* If a class contains pointers as members, it must now also provide a
copy constructor and assignment operator. In the most cases, one
would make them private, effectively disallowing the objects to be
copied. Unfortunately, this warning cannot be fixed be inheriting
our existing 'Noncopyable' class (the compiler fails to detect that
the inheriting class cannot be copied and still gives the error).
For now, we have to manually add declarations for both the copy
constructor and assignment operator as private class members. Those
declarations should be prepended with a comment like this:
/*
* Noncopyable
*/
Thread(Thread const &);
Thread &operator = (Thread const &);
In the future, we should revisit these places and try to replace
the pointers with references. In the presence of at least one
reference member, the compiler would no longer implicitly generate
a copy constructor. So we could remove the manual declaration.
Issue #465
2017-12-21 15:42:15 +01:00
|
|
|
Write_result write(char const *, file_size,
|
2017-08-15 20:51:53 +02:00
|
|
|
file_size &out_count) override
|
|
|
|
{
|
|
|
|
out_count = 0;
|
|
|
|
return WRITE_ERR_INVALID;
|
|
|
|
}
|
|
|
|
|
2019-02-14 22:39:08 +01:00
|
|
|
bool read_ready() override { return true; }
|
2017-08-15 20:51:53 +02:00
|
|
|
};
|
|
|
|
|
2014-04-14 16:05:20 +02:00
|
|
|
public:
|
|
|
|
|
2018-04-03 15:59:35 +02:00
|
|
|
Rom_file_system(Vfs::Env &env,
|
|
|
|
Genode::Xml_node config)
|
2014-04-14 16:05:20 +02:00
|
|
|
:
|
2019-09-25 17:13:29 +02:00
|
|
|
Single_file_system(Node_type::CONTINUOUS_FILE, name(),
|
|
|
|
Node_rwx::ro(), config),
|
2019-01-21 10:48:39 +01:00
|
|
|
|
|
|
|
/* use 'label' attribute if present, fall back to 'name' if not */
|
|
|
|
_label(config.attribute_value("label",
|
|
|
|
config.attribute_value("name", Label()))),
|
|
|
|
|
|
|
|
_binary(config.attribute_value("binary", true)),
|
|
|
|
|
|
|
|
_rom(env.env(), _label.string())
|
2014-04-14 16:05:20 +02:00
|
|
|
{ }
|
|
|
|
|
2017-02-07 19:03:23 +01:00
|
|
|
static char const *name() { return "rom"; }
|
|
|
|
char const *type() override { return "rom"; }
|
2014-04-14 16:05:20 +02:00
|
|
|
|
2015-06-01 19:45:01 +02:00
|
|
|
/*********************************
|
|
|
|
** Directory-service interface **
|
|
|
|
********************************/
|
|
|
|
|
2016-03-30 15:24:19 +02:00
|
|
|
Open_result open(char const *path, unsigned,
|
|
|
|
Vfs_handle **out_handle,
|
|
|
|
Allocator &alloc) override
|
2015-06-01 19:45:01 +02:00
|
|
|
{
|
2017-08-15 20:51:53 +02:00
|
|
|
if (!_single_file(path))
|
|
|
|
return OPEN_ERR_UNACCESSIBLE;
|
2015-06-01 19:45:01 +02:00
|
|
|
|
|
|
|
_rom.update();
|
|
|
|
|
2018-01-12 20:47:48 +01:00
|
|
|
try {
|
|
|
|
*out_handle = new (alloc)
|
2019-01-21 10:48:39 +01:00
|
|
|
Rom_vfs_handle(*this, *this, alloc, _rom, _binary ? ROM_BINARY : ROM_TEXT);
|
2018-01-12 20:47:48 +01:00
|
|
|
return OPEN_OK;
|
|
|
|
}
|
|
|
|
catch (Genode::Out_of_ram) { return OPEN_ERR_OUT_OF_RAM; }
|
|
|
|
catch (Genode::Out_of_caps) { return OPEN_ERR_OUT_OF_CAPS; }
|
2015-06-01 19:45:01 +02:00
|
|
|
}
|
|
|
|
|
Follow practices suggested by "Effective C++"
The patch adjust the code of the base, base-<kernel>, and os repository.
To adapt existing components to fix violations of the best practices
suggested by "Effective C++" as reported by the -Weffc++ compiler
argument. The changes follow the patterns outlined below:
* A class with virtual functions can no longer publicly inherit base
classed without a vtable. The inherited object may either be moved
to a member variable, or inherited privately. The latter would be
used for classes that inherit 'List::Element' or 'Avl_node'. In order
to enable the 'List' and 'Avl_tree' to access the meta data, the
'List' must become a friend.
* Instead of adding a virtual destructor to abstract base classes,
we inherit the new 'Interface' class, which contains a virtual
destructor. This way, single-line abstract base classes can stay
as compact as they are now. The 'Interface' utility resides in
base/include/util/interface.h.
* With the new warnings enabled, all member variables must be explicitly
initialized. Basic types may be initialized with '='. All other types
are initialized with braces '{ ... }' or as class initializers. If
basic types and non-basic types appear in a row, it is nice to only
use the brace syntax (also for basic types) and align the braces.
* If a class contains pointers as members, it must now also provide a
copy constructor and assignment operator. In the most cases, one
would make them private, effectively disallowing the objects to be
copied. Unfortunately, this warning cannot be fixed be inheriting
our existing 'Noncopyable' class (the compiler fails to detect that
the inheriting class cannot be copied and still gives the error).
For now, we have to manually add declarations for both the copy
constructor and assignment operator as private class members. Those
declarations should be prepended with a comment like this:
/*
* Noncopyable
*/
Thread(Thread const &);
Thread &operator = (Thread const &);
In the future, we should revisit these places and try to replace
the pointers with references. In the presence of at least one
reference member, the compiler would no longer implicitly generate
a copy constructor. So we could remove the manual declaration.
Issue #465
2017-12-21 15:42:15 +01:00
|
|
|
Dataspace_capability dataspace(char const *) override
|
2017-08-15 20:51:53 +02:00
|
|
|
{
|
|
|
|
return _rom.cap();
|
|
|
|
}
|
2015-06-01 19:45:01 +02:00
|
|
|
|
2014-04-14 16:05:20 +02:00
|
|
|
/********************************
|
|
|
|
** File I/O service interface **
|
|
|
|
********************************/
|
|
|
|
|
|
|
|
Stat_result stat(char const *path, Stat &out) override
|
|
|
|
{
|
2018-04-19 11:54:00 +02:00
|
|
|
Stat_result const result = Single_file_system::stat(path, out);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the stat call refers to our node ('Single_file_system::stat'
|
|
|
|
* found a file), obtain the size of the most current ROM module
|
|
|
|
* version.
|
|
|
|
*/
|
2019-09-25 17:13:29 +02:00
|
|
|
if (out.type == Node_type::CONTINUOUS_FILE) {
|
2018-04-19 11:54:00 +02:00
|
|
|
_rom.update();
|
|
|
|
out.size = _rom.valid() ? _rom.size() : 0;
|
2019-09-25 17:13:29 +02:00
|
|
|
out.rwx = { .readable = true,
|
|
|
|
.writeable = false,
|
|
|
|
.executable = true };
|
2018-04-19 11:54:00 +02:00
|
|
|
}
|
2015-06-01 19:45:01 +02:00
|
|
|
|
2014-04-14 16:05:20 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* _INCLUDE__VFS__ROM_FILE_SYSTEM_H_ */
|