2011-12-22 15:19:25 +00:00
|
|
|
/*
|
|
|
|
* \brief Utility for handling child configuration
|
|
|
|
* \author Norman Feske
|
|
|
|
* \date 2008-03-22
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2013-01-10 20:44:47 +00:00
|
|
|
* Copyright (C) 2008-2013 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 General Public License version 2.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _INCLUDE__INIT__CHILD_CONFIG_H_
|
|
|
|
#define _INCLUDE__INIT__CHILD_CONFIG_H_
|
|
|
|
|
|
|
|
#include <util/xml_node.h>
|
2016-11-06 13:26:34 +00:00
|
|
|
#include <base/attached_dataspace.h>
|
|
|
|
#include <ram_session/ram_session.h>
|
2011-12-22 15:19:25 +00:00
|
|
|
|
2015-03-04 20:12:14 +00:00
|
|
|
namespace Init { class Child_config; }
|
|
|
|
|
|
|
|
|
|
|
|
class Init::Child_config
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
|
2016-11-06 13:26:34 +00:00
|
|
|
Genode::Ram_session &_ram;
|
2015-03-04 20:12:14 +00:00
|
|
|
|
2016-11-06 13:26:34 +00:00
|
|
|
typedef Genode::String<64> Rom_name;
|
|
|
|
Rom_name const _rom_name;
|
2015-03-04 20:12:14 +00:00
|
|
|
|
2016-11-06 13:26:34 +00:00
|
|
|
Genode::Ram_dataspace_capability const _ram_ds;
|
|
|
|
|
|
|
|
Rom_name _rom_name_from_start_node(Genode::Xml_node start)
|
|
|
|
{
|
|
|
|
if (!start.has_sub_node("configfile"))
|
|
|
|
return Rom_name();
|
|
|
|
|
|
|
|
return start.sub_node("configfile").attribute_value("name", Rom_name());
|
|
|
|
}
|
2015-03-04 20:12:14 +00:00
|
|
|
|
|
|
|
/**
|
2016-11-06 13:26:34 +00:00
|
|
|
* Buffer '<config>' sub node in a dedicated RAM dataspace
|
2015-03-04 20:12:14 +00:00
|
|
|
*
|
2016-11-06 13:26:34 +00:00
|
|
|
* \throw Ram_session::Alloc_failed
|
|
|
|
* \throw Rm_session::Attach_failed
|
2015-03-04 20:12:14 +00:00
|
|
|
*/
|
2016-11-06 13:26:34 +00:00
|
|
|
Genode::Ram_dataspace_capability
|
|
|
|
_ram_ds_from_start_node(Genode::Xml_node start,
|
|
|
|
Genode::Ram_session &ram, Genode::Region_map &rm)
|
2015-03-04 20:12:14 +00:00
|
|
|
{
|
|
|
|
/*
|
2016-11-06 13:26:34 +00:00
|
|
|
* If the start node contains a 'config' entry, we copy this entry
|
|
|
|
* into a fresh dataspace to be provided to our child.
|
2011-12-22 15:19:25 +00:00
|
|
|
*/
|
2016-11-06 13:26:34 +00:00
|
|
|
Genode::Xml_node const config = start.has_sub_node("config")
|
|
|
|
? start.sub_node("config")
|
|
|
|
: Genode::Xml_node("<config/>");
|
2015-03-04 20:12:14 +00:00
|
|
|
|
2016-11-06 13:26:34 +00:00
|
|
|
Genode::Ram_dataspace_capability ram_ds;
|
2015-03-04 20:12:14 +00:00
|
|
|
try {
|
2011-12-22 15:19:25 +00:00
|
|
|
/*
|
2016-11-06 13:26:34 +00:00
|
|
|
* Allocate RAM dataspace that is big enough to hold the
|
|
|
|
* configuration and the null termination.
|
2011-12-22 15:19:25 +00:00
|
|
|
*/
|
2016-11-06 13:26:34 +00:00
|
|
|
ram_ds = ram.alloc(config.size() + 1);
|
2011-12-22 15:19:25 +00:00
|
|
|
|
|
|
|
/*
|
2016-11-06 13:26:34 +00:00
|
|
|
* Make dataspace locally accessible, copy configuration into the
|
|
|
|
* dataspace, and append a string-terminating zero.
|
2011-12-22 15:19:25 +00:00
|
|
|
*/
|
2016-11-06 13:26:34 +00:00
|
|
|
Genode::Attached_dataspace attached(rm, ram_ds);
|
|
|
|
|
|
|
|
Genode::memcpy(attached.local_addr<char>(),
|
|
|
|
config.addr(), config.size());
|
|
|
|
|
|
|
|
attached.local_addr<char>()[config.size()] = 0;
|
|
|
|
|
|
|
|
return ram_ds;
|
|
|
|
}
|
|
|
|
catch (Genode::Region_map::Attach_failed) { ram.free(ram_ds); throw; }
|
2015-03-04 20:12:14 +00:00
|
|
|
}
|
|
|
|
|
2016-11-06 13:26:34 +00:00
|
|
|
public:
|
|
|
|
|
2015-03-04 20:12:14 +00:00
|
|
|
/**
|
2016-11-06 13:26:34 +00:00
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
* The provided RAM session is used to obtain a dataspace for
|
|
|
|
* holding the copy of the child's configuration data unless the
|
|
|
|
* configuration is supplied via a config ROM module.
|
|
|
|
*
|
|
|
|
* \throw Ram_session::Alloc_failed failed to allocate the backing
|
|
|
|
* store for holding config data
|
|
|
|
*
|
|
|
|
* \throw Region_map::Attach_failed failed to temporarily attach the
|
|
|
|
* config dataspace to the local
|
|
|
|
* address space
|
|
|
|
*
|
|
|
|
* If the start node contains a 'filename' entry, we only keep the
|
|
|
|
* information about the ROM module name.
|
2015-03-04 20:12:14 +00:00
|
|
|
*/
|
2016-11-06 13:26:34 +00:00
|
|
|
Child_config(Genode::Ram_session &ram, Genode::Region_map &local_rm,
|
|
|
|
Genode::Xml_node start)
|
|
|
|
:
|
|
|
|
_ram(ram),
|
|
|
|
_rom_name(_rom_name_from_start_node(start)),
|
|
|
|
_ram_ds(_rom_name.valid() ? Genode::Ram_dataspace_capability()
|
|
|
|
: _ram_ds_from_start_node(start, ram, local_rm))
|
|
|
|
{ }
|
2015-03-04 20:12:14 +00:00
|
|
|
|
2016-11-06 13:26:34 +00:00
|
|
|
/**
|
|
|
|
* Destructor
|
|
|
|
*/
|
|
|
|
~Child_config() { if (_ram_ds.valid()) _ram.free(_ram_ds); }
|
2015-03-04 20:12:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return file name if configuration comes from a file
|
|
|
|
*
|
2015-03-20 16:50:41 +00:00
|
|
|
* If the configuration is provided inline, the method returns 0.
|
2015-03-04 20:12:14 +00:00
|
|
|
*/
|
|
|
|
char const *filename() const {
|
2016-11-06 13:26:34 +00:00
|
|
|
return _rom_name.valid() ? _rom_name.string() : nullptr; }
|
2015-03-04 20:12:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Request dataspace holding the start node's configuration data
|
|
|
|
*
|
2015-03-20 16:50:41 +00:00
|
|
|
* This method returns a valid dataspace only when using an
|
2015-03-04 20:12:14 +00:00
|
|
|
* inline configuration (if 'filename()' returns 0).
|
|
|
|
*/
|
|
|
|
Genode::Dataspace_capability dataspace() {
|
2016-11-06 13:26:34 +00:00
|
|
|
return Genode::Dataspace_capability(_ram_ds); }
|
2015-03-04 20:12:14 +00:00
|
|
|
};
|
2011-12-22 15:19:25 +00:00
|
|
|
|
|
|
|
#endif /* _INCLUDE__INIT__CHILD_CONFIG_H_ */
|