2011-12-22 15:19:25 +00:00
|
|
|
/*
|
|
|
|
* \brief Service that provides files of a TAR archive as ROM sessions
|
|
|
|
* \author Martin stein
|
|
|
|
* \author Norman Feske
|
|
|
|
* \date 2010-04-21
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2017-02-20 12:23:52 +00:00
|
|
|
* Copyright (C) 2010-2017 Genode Labs GmbH
|
2011-12-22 15:19:25 +00:00
|
|
|
*
|
|
|
|
* This file is part of the Genode OS framework, which is distributed
|
2017-02-20 12:23:52 +00:00
|
|
|
* under the terms of the GNU Affero General Public License version 3.
|
2011-12-22 15:19:25 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* Genode includes */
|
2016-11-25 15:54:49 +00:00
|
|
|
#include <base/component.h>
|
|
|
|
#include <base/attached_rom_dataspace.h>
|
base: avoid use of deprecated base/printf.h
Besides adapting the components to the use of base/log.h, the patch
cleans up a few base headers, i.e., it removes unused includes from
root/component.h, specifically base/heap.h and
ram_session/ram_session.h. Hence, components that relied on the implicit
inclusion of those headers have to manually include those headers now.
While adjusting the log messages, I repeatedly stumbled over the problem
that printing char * arguments is ambiguous. It is unclear whether to
print the argument as pointer or null-terminated string. To overcome
this problem, the patch introduces a new type 'Cstring' that allows the
caller to express that the argument should be handled as null-terminated
string. As a nice side effect, with this type in place, the optional len
argument of the 'String' class could be removed. Instead of supplying a
pair of (char const *, size_t), the constructor accepts a 'Cstring'.
This, in turn, clears the way let the 'String' constructor use the new
output mechanism to assemble a string from multiple arguments (and
thereby getting rid of snprintf within Genode in the near future).
To enforce the explicit resolution of the char * ambiguity, the 'char *'
overload of the 'print' function is marked as deleted.
Issue #1987
2016-07-13 17:07:09 +00:00
|
|
|
#include <base/heap.h>
|
|
|
|
#include <base/log.h>
|
2016-05-12 12:58:51 +00:00
|
|
|
#include <base/session_label.h>
|
2016-11-25 15:54:49 +00:00
|
|
|
#include <root/component.h>
|
|
|
|
|
|
|
|
namespace Tar_rom {
|
|
|
|
|
|
|
|
using namespace Genode;
|
|
|
|
class Rom_session_component;
|
|
|
|
class Rom_root;
|
|
|
|
struct Main;
|
|
|
|
}
|
2011-12-22 15:19:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A 'Rom_session_component' exports a single file of the tar archive
|
|
|
|
*/
|
2016-11-25 15:54:49 +00:00
|
|
|
class Tar_rom::Rom_session_component : public Rpc_object<Rom_session>
|
2011-12-22 15:19:25 +00:00
|
|
|
{
|
|
|
|
private:
|
|
|
|
|
2016-11-25 15:54:49 +00:00
|
|
|
Ram_session &_ram;
|
|
|
|
|
|
|
|
char const * const _tar_addr;
|
|
|
|
size_t const _tar_size;
|
|
|
|
|
|
|
|
Ram_dataspace_capability _file_ds;
|
2011-12-22 15:19:25 +00:00
|
|
|
|
|
|
|
enum {
|
|
|
|
/* length of on data block in tar */
|
|
|
|
_BLOCK_LEN = 512,
|
|
|
|
|
|
|
|
/* length of the header field "file-size" in tar */
|
|
|
|
_FIELD_SIZE_LEN = 124
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copy file content into dataspace
|
|
|
|
*
|
|
|
|
* \param dst destination dataspace
|
|
|
|
*/
|
2016-11-25 15:54:49 +00:00
|
|
|
void _copy_content_to_dataspace(Region_map &rm, Dataspace_capability dst,
|
|
|
|
char const *src, size_t len)
|
2011-12-22 15:19:25 +00:00
|
|
|
{
|
2016-11-25 15:54:49 +00:00
|
|
|
/* temporarily map dataspace */
|
|
|
|
Attached_dataspace ds(rm, dst);
|
2011-12-22 15:19:25 +00:00
|
|
|
|
|
|
|
/* copy content */
|
2016-11-25 15:54:49 +00:00
|
|
|
size_t bytes_to_copy = min(len, ds.size());
|
|
|
|
memcpy(ds.local_addr<char>(), src, bytes_to_copy);
|
2011-12-22 15:19:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize dataspace containing the content of the archived file
|
|
|
|
*/
|
2016-11-25 15:54:49 +00:00
|
|
|
Ram_dataspace_capability _init_file_ds(Ram_session &ram, Region_map &rm,
|
|
|
|
Session_label const &name)
|
2011-12-22 15:19:25 +00:00
|
|
|
{
|
|
|
|
/* measure size of archive in blocks */
|
|
|
|
unsigned block_id = 0, block_cnt = _tar_size/_BLOCK_LEN;
|
|
|
|
|
2016-11-25 15:54:49 +00:00
|
|
|
char const *file_content = nullptr;
|
|
|
|
unsigned long file_size = 0;
|
|
|
|
|
2011-12-22 15:19:25 +00:00
|
|
|
/* scan metablocks of archive */
|
|
|
|
while (block_id < block_cnt) {
|
|
|
|
|
2016-11-25 15:54:49 +00:00
|
|
|
ascii_to_unsigned(_tar_addr + block_id*_BLOCK_LEN +
|
|
|
|
_FIELD_SIZE_LEN, file_size, 8);
|
2011-12-22 15:19:25 +00:00
|
|
|
|
2012-08-17 06:29:03 +00:00
|
|
|
/* get name of tar record */
|
|
|
|
char const *record_filename = _tar_addr + block_id*_BLOCK_LEN;
|
|
|
|
|
|
|
|
/* skip leading dot of path if present */
|
|
|
|
if (record_filename[0] == '.' && record_filename[1] == '/')
|
|
|
|
record_filename++;
|
|
|
|
|
2011-12-22 15:19:25 +00:00
|
|
|
/* get infos about current file */
|
2016-11-25 15:54:49 +00:00
|
|
|
if (name == record_filename) {
|
|
|
|
file_content = _tar_addr + (block_id+1) * _BLOCK_LEN;
|
2011-12-22 15:19:25 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* some datablocks */ /* one metablock */
|
|
|
|
block_id = block_id + (file_size / _BLOCK_LEN) + 1;
|
|
|
|
|
|
|
|
/* round up */
|
|
|
|
if (file_size % _BLOCK_LEN != 0) block_id++;
|
|
|
|
|
|
|
|
/* check for end of tar archive */
|
|
|
|
if (block_id*_BLOCK_LEN >= _tar_size)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* lookout for empty eof-blocks */
|
|
|
|
if (*(_tar_addr + (block_id*_BLOCK_LEN)) == 0x00)
|
|
|
|
if (*(_tar_addr + (block_id*_BLOCK_LEN + 1)) == 0x00)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-11-25 15:54:49 +00:00
|
|
|
if (!file_content) {
|
|
|
|
error("couldn't find file '", name, "', empty result");
|
|
|
|
return Ram_dataspace_capability();
|
2011-12-22 15:19:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* try to allocate memory for file */
|
2016-11-25 15:54:49 +00:00
|
|
|
Ram_dataspace_capability file_ds;
|
2011-12-22 15:19:25 +00:00
|
|
|
try {
|
2016-11-25 15:54:49 +00:00
|
|
|
file_ds = ram.alloc(file_size);
|
2011-12-22 15:19:25 +00:00
|
|
|
|
|
|
|
/* get content of file copied into dataspace and return */
|
2016-11-25 15:54:49 +00:00
|
|
|
_copy_content_to_dataspace(rm, file_ds, file_content, file_size);
|
2011-12-22 15:19:25 +00:00
|
|
|
} catch (...) {
|
2016-11-25 15:54:49 +00:00
|
|
|
error("couldn't allocate memory for file, empty result");
|
2011-12-22 15:19:25 +00:00
|
|
|
return file_ds;
|
|
|
|
}
|
|
|
|
|
|
|
|
return file_ds;
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor scans and seeks to file
|
|
|
|
*
|
|
|
|
* \param tar_addr local address to tar archive
|
|
|
|
* \param tar_size size of tar archive in bytes
|
2016-11-25 15:54:49 +00:00
|
|
|
* \param label name of the requested ROM module
|
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 20:03:22 +00:00
|
|
|
*
|
|
|
|
* \throw Service_denied
|
2011-12-22 15:19:25 +00:00
|
|
|
*/
|
2016-11-25 15:54:49 +00:00
|
|
|
Rom_session_component(Ram_session &ram, Region_map &rm,
|
|
|
|
char const *tar_addr, unsigned tar_size,
|
|
|
|
Session_label const &label)
|
2011-12-22 15:19:25 +00:00
|
|
|
:
|
2016-11-25 15:54:49 +00:00
|
|
|
_ram(ram), _tar_addr(tar_addr), _tar_size(tar_size),
|
|
|
|
_file_ds(_init_file_ds(ram, rm, label))
|
2013-01-17 07:23:54 +00:00
|
|
|
{
|
|
|
|
if (!_file_ds.valid())
|
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 20:03:22 +00:00
|
|
|
throw Service_denied();
|
2013-01-17 07:23:54 +00:00
|
|
|
}
|
2011-12-22 15:19:25 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Destructor
|
|
|
|
*/
|
2016-11-25 15:54:49 +00:00
|
|
|
~Rom_session_component() { _ram.free(_file_ds); }
|
2011-12-22 15:19:25 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return dataspace with content of file
|
|
|
|
*/
|
2016-11-25 15:54:49 +00:00
|
|
|
Rom_dataspace_capability dataspace()
|
2011-12-22 15:19:25 +00:00
|
|
|
{
|
2016-11-25 15:54:49 +00:00
|
|
|
Dataspace_capability ds = _file_ds;
|
|
|
|
return static_cap_cast<Rom_dataspace>(ds);
|
2011-12-22 15:19:25 +00:00
|
|
|
}
|
Support for dynamic ROM sessions, fix #170
This patch introduces support for ROM sessions that update their
provided data during the lifetime of the session. The 'Rom_session'
interface had been extended with the new 'release()' and 'sigh()'
functions, which are needed to support the new protocol. All ROM
services have been updated to the new interface.
Furthermore, the patch changes the child policy of init
with regard to the handling of configuration files. The 'Init::Child'
used to always provide the ROM dataspace with the child's config file
via a locally implemented ROM service. However, for dynamic ROM
sessions, we need to establish a session to the real supplier of the ROM
data. This is achieved by using a new 'Child_policy_redirect_rom_file'
policy to handle the 'configfile' rather than handling the 'configfile'
case entirely within 'Child_config'.
To see the new facility in action, the new 'os/run/dynamic_config.run'
script provides a simple scenario. The config file of the test program
is provided by a service, which generates and updates the config data
at regular intervals.
In addition, new support has been added to let slaves use dynamic
reconfiguration. By using the new 'Child_policy_dynamic_rom_file', the
configuration of a slave can be changed dynamically at runtime via the
new 'configure()' function.
The config is provided as plain null-terminated string (instead of a
dataspace capability) because we need to buffer the config data anyway.
So there is no benefit of using a dataspace. For buffering configuration
data, a 'Ram_session' must be supplied. If no 'Ram_session' is specified
at construction time of a 'Slave_policy', no config is supplied to the
slave (which is still a common case).
An example for dynamically reconfiguring a slave is provided by
'os/run/dynamic_config_slave.run'.
2012-04-04 15:07:19 +00:00
|
|
|
|
2016-11-25 15:54:49 +00:00
|
|
|
void sigh(Signal_context_capability) { }
|
2011-12-22 15:19:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-11-25 15:54:49 +00:00
|
|
|
class Tar_rom::Rom_root : public Root_component<Rom_session_component>
|
2011-12-22 15:19:25 +00:00
|
|
|
{
|
|
|
|
private:
|
|
|
|
|
2016-11-25 15:54:49 +00:00
|
|
|
Env &_env;
|
|
|
|
|
|
|
|
char const * const _tar_addr;
|
|
|
|
unsigned const _tar_size;
|
2011-12-22 15:19:25 +00:00
|
|
|
|
|
|
|
Rom_session_component *_create_session(const char *args)
|
|
|
|
{
|
2016-05-12 12:58:51 +00:00
|
|
|
Session_label const label = label_from_args(args);
|
|
|
|
Session_label const module_name = label.last_element();
|
2016-11-25 15:54:49 +00:00
|
|
|
log("connection for module '", module_name, "' requested");
|
2011-12-22 15:19:25 +00:00
|
|
|
|
|
|
|
/* create new session for the requested file */
|
2016-11-25 15:54:49 +00:00
|
|
|
return new (md_alloc()) Rom_session_component(_env.ram(), _env.rm(),
|
|
|
|
_tar_addr, _tar_size,
|
2016-05-12 12:58:51 +00:00
|
|
|
module_name.string());
|
2011-12-22 15:19:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
2016-11-25 15:54:49 +00:00
|
|
|
* \param tar_base local address of tar archive
|
|
|
|
* \param tar_size size of tar archive in bytes
|
2011-12-22 15:19:25 +00:00
|
|
|
*/
|
2016-11-25 15:54:49 +00:00
|
|
|
Rom_root(Env &env, Allocator &md_alloc,
|
|
|
|
char const *tar_addr, size_t tar_size)
|
2011-12-22 15:19:25 +00:00
|
|
|
:
|
2016-11-25 15:54:49 +00:00
|
|
|
Root_component<Rom_session_component>(env.ep(), md_alloc),
|
|
|
|
_env(env), _tar_addr(tar_addr), _tar_size(tar_size)
|
2011-12-22 15:19:25 +00:00
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-11-25 15:54:49 +00:00
|
|
|
struct Tar_rom::Main
|
2011-12-22 15:19:25 +00:00
|
|
|
{
|
2016-11-25 15:54:49 +00:00
|
|
|
Env &_env;
|
|
|
|
|
|
|
|
Attached_rom_dataspace _config { _env, "config" };
|
|
|
|
|
|
|
|
typedef String<64> Name;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read name of tar archive from config
|
|
|
|
*/
|
|
|
|
Name _tar_name()
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
return _config.xml().sub_node("archive").attribute_value("name", Name());
|
|
|
|
} catch (...) {
|
|
|
|
error("could not read archive name argument from config");
|
|
|
|
throw;
|
|
|
|
}
|
2011-12-22 15:19:25 +00:00
|
|
|
}
|
|
|
|
|
2016-11-25 15:54:49 +00:00
|
|
|
Attached_rom_dataspace _tar_ds { _env, _tar_name().string() };
|
2011-12-22 15:19:25 +00:00
|
|
|
|
2016-11-25 15:54:49 +00:00
|
|
|
Sliced_heap _sliced_heap { _env.ram(), _env.rm() };
|
2011-12-22 15:19:25 +00:00
|
|
|
|
2016-11-25 15:54:49 +00:00
|
|
|
Rom_root _root { _env, _sliced_heap, _tar_ds.local_addr<char>(), _tar_ds.size() };
|
2011-12-22 15:19:25 +00:00
|
|
|
|
2016-11-25 15:54:49 +00:00
|
|
|
Main(Env &env) : _env(env)
|
|
|
|
{
|
|
|
|
log("using tar archive '", _tar_name(), "' with size ", _tar_ds.size());
|
2011-12-22 15:19:25 +00:00
|
|
|
|
2016-11-25 15:54:49 +00:00
|
|
|
env.parent().announce(env.ep().manage(_root));
|
|
|
|
}
|
|
|
|
};
|
2011-12-22 15:19:25 +00:00
|
|
|
|
|
|
|
|
2016-11-25 15:54:49 +00:00
|
|
|
void Component::construct(Genode::Env &env) { static Tar_rom::Main main(env); }
|
2011-12-22 15:19:25 +00:00
|
|
|
|