genode/repos/os/src/server/tar_rom/main.cc

253 lines
6.1 KiB
C++
Raw Normal View History

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
*/
/*
* 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
* 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>
#include <base/heap.h>
#include <base/log.h>
#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
/* 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
*
* \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))
{
if (!_file_ds.valid())
throw Service_denied();
}
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)
{
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,
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