2011-12-22 16:19:25 +01:00
|
|
|
/*
|
|
|
|
* \brief Provide a rom-file as block device (aka loop devices)
|
|
|
|
* \author Stefan Kalkowski
|
|
|
|
* \date 2010-07-07
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2017-01-16 15:33:56 +01:00
|
|
|
* Copyright (C) 2010-2017 Genode Labs GmbH
|
2011-12-22 16:19:25 +01: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.
|
2011-12-22 16:19:25 +01:00
|
|
|
*/
|
|
|
|
|
2016-08-12 15:10:50 +02:00
|
|
|
#include <base/attached_rom_dataspace.h>
|
|
|
|
#include <base/component.h>
|
|
|
|
#include <base/log.h>
|
2013-11-22 23:15:47 +01:00
|
|
|
#include <block/component.h>
|
2016-08-12 15:10:50 +02:00
|
|
|
#include <rom_session/connection.h>
|
2011-12-22 16:19:25 +01:00
|
|
|
|
|
|
|
using namespace Genode;
|
|
|
|
|
2019-01-06 21:17:07 +01:00
|
|
|
class Rom_block : public Block::Driver
|
2013-11-22 23:15:47 +01:00
|
|
|
{
|
|
|
|
private:
|
|
|
|
|
2016-08-12 15:10:50 +02:00
|
|
|
Env &_env;
|
2013-11-22 23:15:47 +01:00
|
|
|
Rom_connection _rom;
|
2016-08-12 15:10:50 +02:00
|
|
|
size_t _blk_sz;
|
|
|
|
Dataspace_capability _file_cap = _rom.dataspace();
|
|
|
|
addr_t _file_addr = _env.rm().attach(_file_cap);
|
|
|
|
size_t _file_sz = Dataspace_client(_file_cap).size();
|
|
|
|
size_t _blk_cnt = _file_sz / _blk_sz;
|
2013-11-22 23:15:47 +01:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
2016-08-12 15:10:50 +02:00
|
|
|
using String = Genode::String<64UL>;
|
|
|
|
|
2019-01-21 10:48:39 +01:00
|
|
|
Rom_block(Env &env, String const &name, size_t blk_sz)
|
2017-01-16 15:33:56 +01:00
|
|
|
: Block::Driver(env.ram()), _env(env), _rom(env, name.string()),
|
|
|
|
_blk_sz(blk_sz) {}
|
2013-11-22 23:15:47 +01:00
|
|
|
|
|
|
|
|
|
|
|
/****************************
|
|
|
|
** Block-driver interface **
|
|
|
|
****************************/
|
|
|
|
|
2019-04-03 16:05:10 +02:00
|
|
|
Block::Session::Info info() const override
|
2013-11-22 23:15:47 +01:00
|
|
|
{
|
2019-04-03 16:05:10 +02:00
|
|
|
return { .block_size = _blk_sz,
|
|
|
|
.block_count = _blk_cnt,
|
|
|
|
.writeable = false };
|
2013-11-22 23:15:47 +01:00
|
|
|
}
|
|
|
|
|
2016-08-12 15:10:50 +02:00
|
|
|
void read(Block::sector_t block_number,
|
|
|
|
size_t block_count,
|
|
|
|
char* buffer,
|
2019-02-14 22:39:08 +01:00
|
|
|
Block::Packet_descriptor &packet) override
|
2013-11-22 23:15:47 +01:00
|
|
|
{
|
|
|
|
/* sanity check block number */
|
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
|
|
|
if (block_number + block_count > _file_sz / _blk_sz) {
|
2016-08-12 15:10:50 +02:00
|
|
|
warning("requested blocks ", block_number, "-",
|
|
|
|
block_number + block_count, " out of range!");
|
2013-11-22 23:15:47 +01:00
|
|
|
return;
|
2011-12-22 16:19:25 +01:00
|
|
|
}
|
|
|
|
|
2013-12-04 14:34:53 +01:00
|
|
|
size_t offset = (size_t) block_number * _blk_sz;
|
2013-11-22 23:15:47 +01:00
|
|
|
size_t size = block_count * _blk_sz;
|
2011-12-22 16:19:25 +01:00
|
|
|
|
2013-11-22 23:15:47 +01:00
|
|
|
/* copy file content to packet payload */
|
2013-12-03 15:41:14 +01:00
|
|
|
memcpy((void*)buffer, (void*)(_file_addr + offset), size);
|
2011-12-22 16:19:25 +01:00
|
|
|
|
2014-02-06 15:23:17 +01:00
|
|
|
ack_packet(packet);
|
2013-11-22 23:15:47 +01:00
|
|
|
}
|
|
|
|
};
|
2011-12-22 16:19:25 +01:00
|
|
|
|
2013-11-22 23:15:47 +01:00
|
|
|
|
2014-01-13 13:44:33 +01:00
|
|
|
struct Main
|
2011-12-22 16:19:25 +01:00
|
|
|
{
|
2016-08-12 15:10:50 +02:00
|
|
|
Env &env;
|
|
|
|
Heap heap { env.ram(), env.rm() };
|
2014-01-13 13:44:33 +01:00
|
|
|
|
|
|
|
struct Factory : Block::Driver_factory
|
2013-11-22 23:15:47 +01:00
|
|
|
{
|
2016-08-12 15:10:50 +02:00
|
|
|
Env &env;
|
|
|
|
Heap &heap;
|
|
|
|
|
|
|
|
Factory(Env &env, Heap &heap)
|
|
|
|
: env(env), heap(heap) {}
|
|
|
|
|
2019-02-14 22:39:08 +01:00
|
|
|
Block::Driver *create() override
|
2014-01-13 13:44:33 +01:00
|
|
|
{
|
2019-01-21 10:48:39 +01:00
|
|
|
Attached_rom_dataspace config(env, "config");
|
2013-11-22 23:15:47 +01:00
|
|
|
|
2019-01-21 10:48:39 +01:00
|
|
|
Rom_block::String const file =
|
|
|
|
config.xml().attribute_value("file", Rom_block::String());
|
2013-11-22 23:15:47 +01:00
|
|
|
|
2019-01-21 10:48:39 +01:00
|
|
|
size_t const blk_sz =
|
|
|
|
config.xml().attribute_value("block_size", 512UL);
|
|
|
|
|
|
|
|
log("using file=", file, " as device with block size ", blk_sz, ".");
|
2013-11-22 23:15:47 +01:00
|
|
|
|
2014-01-13 13:44:33 +01:00
|
|
|
try {
|
2019-01-06 21:17:07 +01:00
|
|
|
return new (&heap) Rom_block(env, file, blk_sz);
|
2014-01-13 13:44:33 +01:00
|
|
|
}
|
2019-01-21 10:48:39 +01:00
|
|
|
catch (Rom_connection::Rom_connection_failed) {
|
|
|
|
error("cannot open file ", file); }
|
|
|
|
|
Streamline exception types
This patch reduces the number of exception types by facilitating
globally defined exceptions for common usage patterns shared by most
services. In particular, RPC functions that demand a session-resource
upgrade not longer reflect this condition via a session-specific
exception but via the 'Out_of_ram' or 'Out_of_caps' types.
Furthermore, the 'Parent::Service_denied', 'Parent::Unavailable',
'Root::Invalid_args', 'Root::Unavailable', 'Service::Invalid_args',
'Service::Unavailable', and 'Local_service::Factory::Denied' types have
been replaced by the single 'Service_denied' exception type defined in
'session/session.h'.
This consolidation eases the error handling (there are fewer exceptions
to handle), alleviates the need to convert exceptions along the
session-creation call chain, and avoids possible aliasing problems
(catching the wrong type with the same name but living in a different
scope).
2017-05-07 22:03:22 +02:00
|
|
|
throw Service_denied();
|
2013-11-22 23:15:47 +01:00
|
|
|
}
|
2011-12-22 16:19:25 +01:00
|
|
|
|
2019-02-14 22:39:08 +01:00
|
|
|
void destroy(Block::Driver *driver) override {
|
2016-08-12 15:10:50 +02:00
|
|
|
Genode::destroy(&heap, driver); }
|
|
|
|
} factory { env, heap };
|
2011-12-22 16:19:25 +01:00
|
|
|
|
2017-07-31 19:53:34 +02:00
|
|
|
enum { WRITEABLE = false };
|
|
|
|
|
|
|
|
Block::Root root { env.ep(), heap, env.rm(), factory, WRITEABLE };
|
2011-12-22 16:19:25 +01:00
|
|
|
|
2016-08-12 15:10:50 +02:00
|
|
|
Main(Env &env) : env(env) {
|
|
|
|
env.parent().announce(env.ep().manage(root)); }
|
2014-01-13 13:44:33 +01:00
|
|
|
};
|
2011-12-22 16:19:25 +01:00
|
|
|
|
|
|
|
|
2016-11-28 15:13:14 +01:00
|
|
|
void Component::construct(Genode::Env &env) { static Main server(env); }
|