2014-10-29 04:35:58 +08:00
|
|
|
/*
|
|
|
|
* Functions for working with device tree overlays
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012 Pantelis Antoniou <panto@antoniou-consulting.com>
|
|
|
|
* Copyright (C) 2012 Texas Instruments Inc.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* version 2 as published by the Free Software Foundation.
|
|
|
|
*/
|
2016-06-15 21:32:18 +08:00
|
|
|
|
|
|
|
#define pr_fmt(fmt) "OF: overlay: " fmt
|
|
|
|
|
2014-10-29 04:35:58 +08:00
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/of.h>
|
|
|
|
#include <linux/of_device.h>
|
|
|
|
#include <linux/string.h>
|
|
|
|
#include <linux/ctype.h>
|
|
|
|
#include <linux/errno.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/err.h>
|
2015-02-17 10:36:58 +08:00
|
|
|
#include <linux/idr.h>
|
2014-10-29 04:35:58 +08:00
|
|
|
|
|
|
|
#include "of_private.h"
|
|
|
|
|
|
|
|
/**
|
2017-10-18 07:36:23 +08:00
|
|
|
* struct fragment - info about fragment nodes in overlay expanded device tree
|
2014-10-29 04:35:58 +08:00
|
|
|
* @target: target of the overlay operation
|
2017-10-18 07:36:23 +08:00
|
|
|
* @overlay: pointer to the __overlay__ node
|
2014-10-29 04:35:58 +08:00
|
|
|
*/
|
2017-10-18 07:36:23 +08:00
|
|
|
struct fragment {
|
2014-10-29 04:35:58 +08:00
|
|
|
struct device_node *target;
|
|
|
|
struct device_node *overlay;
|
2017-07-20 00:25:22 +08:00
|
|
|
bool is_symbols_node;
|
2014-10-29 04:35:58 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2017-10-18 07:36:23 +08:00
|
|
|
* struct overlay_changeset
|
|
|
|
* @ovcs_list: list on which we are located
|
|
|
|
* @count: count of @fragments structures
|
|
|
|
* @fragments: info about fragment nodes in overlay expanded device tree
|
|
|
|
* @cset: changeset to apply fragments to live device tree
|
2014-10-29 04:35:58 +08:00
|
|
|
*/
|
2017-10-18 07:36:23 +08:00
|
|
|
struct overlay_changeset {
|
2014-10-29 04:35:58 +08:00
|
|
|
int id;
|
2017-10-18 07:36:23 +08:00
|
|
|
struct list_head ovcs_list;
|
2014-10-29 04:35:58 +08:00
|
|
|
int count;
|
2017-10-18 07:36:23 +08:00
|
|
|
struct fragment *fragments;
|
2014-10-29 04:35:58 +08:00
|
|
|
struct of_changeset cset;
|
|
|
|
};
|
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
static int build_changeset_next_level(struct overlay_changeset *ovcs,
|
|
|
|
struct device_node *target_node,
|
|
|
|
const struct device_node *overlay_node,
|
2017-07-20 00:25:22 +08:00
|
|
|
bool is_symbols_node);
|
2014-10-29 04:35:58 +08:00
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
static BLOCKING_NOTIFIER_HEAD(overlay_notify_chain);
|
2016-11-02 03:14:22 +08:00
|
|
|
|
|
|
|
int of_overlay_notifier_register(struct notifier_block *nb)
|
|
|
|
{
|
2017-10-18 07:36:23 +08:00
|
|
|
return blocking_notifier_chain_register(&overlay_notify_chain, nb);
|
2016-11-02 03:14:22 +08:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(of_overlay_notifier_register);
|
|
|
|
|
|
|
|
int of_overlay_notifier_unregister(struct notifier_block *nb)
|
|
|
|
{
|
2017-10-18 07:36:23 +08:00
|
|
|
return blocking_notifier_chain_unregister(&overlay_notify_chain, nb);
|
2016-11-02 03:14:22 +08:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(of_overlay_notifier_unregister);
|
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
static int overlay_notify(struct overlay_changeset *ovcs,
|
|
|
|
enum of_overlay_notify_action action)
|
2016-11-02 03:14:22 +08:00
|
|
|
{
|
|
|
|
struct of_overlay_notify_data nd;
|
|
|
|
int i, ret;
|
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
for (i = 0; i < ovcs->count; i++) {
|
|
|
|
struct fragment *fragment = &ovcs->fragments[i];
|
2016-11-02 03:14:22 +08:00
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
nd.target = fragment->target;
|
|
|
|
nd.overlay = fragment->overlay;
|
2016-11-02 03:14:22 +08:00
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
ret = blocking_notifier_call_chain(&overlay_notify_chain,
|
2016-11-02 03:14:22 +08:00
|
|
|
action, &nd);
|
|
|
|
if (ret)
|
|
|
|
return notifier_to_errno(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-10-18 07:36:24 +08:00
|
|
|
/*
|
|
|
|
* The properties in the "/__symbols__" node are "symbols".
|
|
|
|
*
|
|
|
|
* The value of properties in the "/__symbols__" node is the path of a
|
|
|
|
* node in the subtree of a fragment node's "__overlay__" node, for
|
|
|
|
* example "/fragment@0/__overlay__/symbol_path_tail". Symbol_path_tail
|
|
|
|
* can be a single node or it may be a multi-node path.
|
|
|
|
*
|
|
|
|
* The duplicated property value will be modified by replacing the
|
|
|
|
* "/fragment_name/__overlay/" portion of the value with the target
|
|
|
|
* path from the fragment node.
|
|
|
|
*/
|
2017-10-18 07:36:23 +08:00
|
|
|
static struct property *dup_and_fixup_symbol_prop(
|
|
|
|
struct overlay_changeset *ovcs, const struct property *prop)
|
2017-07-20 00:25:22 +08:00
|
|
|
{
|
2017-10-18 07:36:23 +08:00
|
|
|
struct fragment *fragment;
|
2017-07-20 00:25:22 +08:00
|
|
|
struct property *new;
|
|
|
|
const char *overlay_name;
|
2017-10-18 07:36:24 +08:00
|
|
|
char *symbol_path_tail;
|
2017-07-20 00:25:22 +08:00
|
|
|
char *symbol_path;
|
|
|
|
const char *target_path;
|
|
|
|
int k;
|
2017-10-18 07:36:24 +08:00
|
|
|
int symbol_path_tail_len;
|
2017-07-20 00:25:22 +08:00
|
|
|
int overlay_name_len;
|
|
|
|
int target_path_len;
|
|
|
|
|
|
|
|
if (!prop->value)
|
|
|
|
return NULL;
|
|
|
|
symbol_path = prop->value;
|
|
|
|
|
|
|
|
new = kzalloc(sizeof(*new), GFP_KERNEL);
|
|
|
|
if (!new)
|
|
|
|
return NULL;
|
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
for (k = 0; k < ovcs->count; k++) {
|
|
|
|
fragment = &ovcs->fragments[k];
|
|
|
|
overlay_name = fragment->overlay->full_name;
|
2017-07-20 00:25:22 +08:00
|
|
|
overlay_name_len = strlen(overlay_name);
|
|
|
|
if (!strncasecmp(symbol_path, overlay_name, overlay_name_len))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
if (k >= ovcs->count)
|
2017-07-20 00:25:22 +08:00
|
|
|
goto err_free;
|
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
target_path = fragment->target->full_name;
|
2017-07-20 00:25:22 +08:00
|
|
|
target_path_len = strlen(target_path);
|
|
|
|
|
2017-10-18 07:36:24 +08:00
|
|
|
symbol_path_tail = symbol_path + overlay_name_len;
|
|
|
|
symbol_path_tail_len = strlen(symbol_path_tail);
|
2017-07-20 00:25:22 +08:00
|
|
|
|
|
|
|
new->name = kstrdup(prop->name, GFP_KERNEL);
|
2017-10-18 07:36:24 +08:00
|
|
|
new->length = target_path_len + symbol_path_tail_len + 1;
|
2017-07-20 00:25:22 +08:00
|
|
|
new->value = kzalloc(new->length, GFP_KERNEL);
|
|
|
|
|
|
|
|
if (!new->name || !new->value)
|
|
|
|
goto err_free;
|
|
|
|
|
|
|
|
strcpy(new->value, target_path);
|
2017-10-18 07:36:24 +08:00
|
|
|
strcpy(new->value + target_path_len, symbol_path_tail);
|
2017-07-20 00:25:22 +08:00
|
|
|
|
|
|
|
of_property_set_flag(new, OF_DYNAMIC);
|
|
|
|
|
|
|
|
return new;
|
|
|
|
|
|
|
|
err_free:
|
|
|
|
kfree(new->name);
|
|
|
|
kfree(new->value);
|
|
|
|
kfree(new);
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
/**
|
|
|
|
* add_changeset_property() - add @overlay_prop to overlay changeset
|
|
|
|
* @ovcs: overlay changeset
|
|
|
|
* @target_node: where to place @overlay_prop in live tree
|
|
|
|
* @overlay_prop: property to add or update, from overlay tree
|
|
|
|
* is_symbols_node: 1 if @target_node is "/__symbols__"
|
|
|
|
*
|
|
|
|
* If @overlay_prop does not already exist in @target_node, add changeset entry
|
|
|
|
* to add @overlay_prop in @target_node, else add changeset entry to update
|
|
|
|
* value of @overlay_prop.
|
|
|
|
*
|
2017-10-18 07:36:21 +08:00
|
|
|
* Some special properties are not updated (no error returned).
|
2017-10-18 07:36:23 +08:00
|
|
|
*
|
2017-10-18 07:36:21 +08:00
|
|
|
* Update of property in symbols node is not allowed.
|
2017-10-18 07:36:23 +08:00
|
|
|
*
|
|
|
|
* Returns 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if
|
|
|
|
* invalid @overlay.
|
2017-10-18 07:36:21 +08:00
|
|
|
*/
|
2017-10-18 07:36:23 +08:00
|
|
|
static int add_changeset_property(struct overlay_changeset *ovcs,
|
|
|
|
struct device_node *target_node,
|
|
|
|
struct property *overlay_prop,
|
2017-07-20 00:25:22 +08:00
|
|
|
bool is_symbols_node)
|
2014-10-29 04:35:58 +08:00
|
|
|
{
|
2017-10-18 07:36:23 +08:00
|
|
|
struct property *new_prop = NULL, *prop;
|
2017-10-16 17:54:32 +08:00
|
|
|
int ret = 0;
|
2014-10-29 04:35:58 +08:00
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
prop = of_find_property(target_node, overlay_prop->name, NULL);
|
2014-10-29 04:35:58 +08:00
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
if (!of_prop_cmp(overlay_prop->name, "name") ||
|
|
|
|
!of_prop_cmp(overlay_prop->name, "phandle") ||
|
|
|
|
!of_prop_cmp(overlay_prop->name, "linux,phandle"))
|
2014-10-29 04:35:58 +08:00
|
|
|
return 0;
|
|
|
|
|
2017-07-20 00:25:22 +08:00
|
|
|
if (is_symbols_node) {
|
2017-10-18 07:36:23 +08:00
|
|
|
if (prop)
|
2017-07-20 00:25:22 +08:00
|
|
|
return -EINVAL;
|
2017-10-18 07:36:23 +08:00
|
|
|
new_prop = dup_and_fixup_symbol_prop(ovcs, overlay_prop);
|
2017-07-20 00:25:22 +08:00
|
|
|
} else {
|
2017-10-18 07:36:23 +08:00
|
|
|
new_prop = __of_prop_dup(overlay_prop, GFP_KERNEL);
|
2017-07-20 00:25:22 +08:00
|
|
|
}
|
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
if (!new_prop)
|
2014-10-29 04:35:58 +08:00
|
|
|
return -ENOMEM;
|
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
if (!prop)
|
|
|
|
ret = of_changeset_add_property(&ovcs->cset, target_node,
|
|
|
|
new_prop);
|
2017-10-18 07:36:21 +08:00
|
|
|
else
|
2017-10-18 07:36:23 +08:00
|
|
|
ret = of_changeset_update_property(&ovcs->cset, target_node,
|
|
|
|
new_prop);
|
2017-10-16 17:54:32 +08:00
|
|
|
|
|
|
|
if (ret) {
|
2017-10-18 07:36:23 +08:00
|
|
|
kfree(new_prop->name);
|
|
|
|
kfree(new_prop->value);
|
|
|
|
kfree(new_prop);
|
2017-10-16 17:54:32 +08:00
|
|
|
}
|
|
|
|
return ret;
|
2014-10-29 04:35:58 +08:00
|
|
|
}
|
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
/**
|
|
|
|
* add_changeset_node() - add @node (and children) to overlay changeset
|
|
|
|
* @ovcs: overlay changeset
|
|
|
|
* @target_node: where to place @node in live tree
|
|
|
|
* @node: node from within overlay device tree fragment
|
|
|
|
*
|
|
|
|
* If @node does not already exist in @target_node, add changeset entry
|
|
|
|
* to add @node in @target_node.
|
|
|
|
*
|
|
|
|
* If @node already exists in @target_node, and the existing node has
|
|
|
|
* a phandle, the overlay node is not allowed to have a phandle.
|
|
|
|
*
|
|
|
|
* If @node has child nodes, add the children recursively via
|
|
|
|
* build_changeset_next_level().
|
|
|
|
*
|
|
|
|
* NOTE: Multiple mods of created nodes not supported.
|
|
|
|
*
|
|
|
|
* Returns 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if
|
|
|
|
* invalid @overlay.
|
|
|
|
*/
|
|
|
|
static int add_changeset_node(struct overlay_changeset *ovcs,
|
|
|
|
struct device_node *target_node, struct device_node *node)
|
2014-10-29 04:35:58 +08:00
|
|
|
{
|
2017-10-18 07:36:23 +08:00
|
|
|
const char *node_kbasename;
|
2015-03-03 21:04:45 +08:00
|
|
|
struct device_node *tchild;
|
2014-10-29 04:35:58 +08:00
|
|
|
int ret = 0;
|
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
node_kbasename = kbasename(node->full_name);
|
|
|
|
if (!node_kbasename)
|
2014-10-29 04:35:58 +08:00
|
|
|
return -ENOMEM;
|
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
for_each_child_of_node(target_node, tchild)
|
|
|
|
if (!of_node_cmp(node_kbasename, kbasename(tchild->full_name)))
|
2017-07-20 00:25:21 +08:00
|
|
|
break;
|
|
|
|
|
2017-10-18 07:36:22 +08:00
|
|
|
if (tchild) {
|
2017-10-18 07:36:23 +08:00
|
|
|
if (node->phandle)
|
2017-06-22 03:21:56 +08:00
|
|
|
return -EINVAL;
|
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
ret = build_changeset_next_level(ovcs, tchild, node, 0);
|
2014-10-29 04:35:58 +08:00
|
|
|
of_node_put(tchild);
|
|
|
|
} else {
|
2017-10-18 07:36:23 +08:00
|
|
|
tchild = __of_node_dup(node, "%pOF/%s",
|
|
|
|
target_node, node_kbasename);
|
2014-10-29 04:35:58 +08:00
|
|
|
if (!tchild)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
tchild->parent = target_node;
|
2014-10-29 04:35:58 +08:00
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
ret = of_changeset_attach_node(&ovcs->cset, tchild);
|
2014-10-29 04:35:58 +08:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
ret = build_changeset_next_level(ovcs, tchild, node, 0);
|
2014-10-29 04:35:58 +08:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
/**
|
|
|
|
* build_changeset_next_level() - add level of overlay changeset
|
|
|
|
* @ovcs: overlay changeset
|
|
|
|
* @target_node: where to place @overlay_node in live tree
|
|
|
|
* @overlay_node: node from within an overlay device tree fragment
|
|
|
|
* @is_symbols_node: @overlay_node is node "/__symbols__"
|
2014-10-29 04:35:58 +08:00
|
|
|
*
|
2017-10-18 07:36:23 +08:00
|
|
|
* Add the properties (if any) and nodes (if any) from @overlay_node to the
|
|
|
|
* @ovcs->cset changeset. If an added node has child nodes, they will
|
|
|
|
* be added recursively.
|
2017-10-18 07:36:21 +08:00
|
|
|
*
|
|
|
|
* Do not allow symbols node to have any children.
|
2017-10-18 07:36:23 +08:00
|
|
|
*
|
|
|
|
* Returns 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if
|
|
|
|
* invalid @overlay_node.
|
2014-10-29 04:35:58 +08:00
|
|
|
*/
|
2017-10-18 07:36:23 +08:00
|
|
|
static int build_changeset_next_level(struct overlay_changeset *ovcs,
|
|
|
|
struct device_node *target_node,
|
|
|
|
const struct device_node *overlay_node,
|
2017-07-20 00:25:22 +08:00
|
|
|
bool is_symbols_node)
|
2014-10-29 04:35:58 +08:00
|
|
|
{
|
|
|
|
struct device_node *child;
|
|
|
|
struct property *prop;
|
|
|
|
int ret;
|
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
for_each_property_of_node(overlay_node, prop) {
|
|
|
|
ret = add_changeset_property(ovcs, target_node, prop,
|
|
|
|
is_symbols_node);
|
2014-10-29 04:35:58 +08:00
|
|
|
if (ret) {
|
2017-06-02 04:50:55 +08:00
|
|
|
pr_err("Failed to apply prop @%pOF/%s\n",
|
2017-10-18 07:36:23 +08:00
|
|
|
target_node, prop->name);
|
2014-10-29 04:35:58 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-20 00:25:22 +08:00
|
|
|
if (is_symbols_node)
|
|
|
|
return 0;
|
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
for_each_child_of_node(overlay_node, child) {
|
|
|
|
ret = add_changeset_node(ovcs, target_node, child);
|
2017-10-18 07:36:22 +08:00
|
|
|
if (ret) {
|
2017-10-18 07:36:23 +08:00
|
|
|
pr_err("Failed to apply node @%pOF/%s\n",
|
|
|
|
target_node, child->name);
|
2015-10-22 17:02:48 +08:00
|
|
|
of_node_put(child);
|
2014-10-29 04:35:58 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-18 07:36:23 +08:00
|
|
|
* build_changeset() - populate overlay changeset in @ovcs from @ovcs->fragments
|
|
|
|
* @ovcs: Overlay changeset
|
2014-10-29 04:35:58 +08:00
|
|
|
*
|
2017-10-18 07:36:23 +08:00
|
|
|
* Create changeset @ovcs->cset to contain the nodes and properties of the
|
|
|
|
* overlay device tree fragments in @ovcs->fragments[]. If an error occurs,
|
|
|
|
* any portions of the changeset that were successfully created will remain
|
|
|
|
* in @ovcs->cset.
|
|
|
|
*
|
|
|
|
* Returns 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if
|
|
|
|
* invalid overlay in @ovcs->fragments[].
|
2014-10-29 04:35:58 +08:00
|
|
|
*/
|
2017-10-18 07:36:23 +08:00
|
|
|
static int build_changeset(struct overlay_changeset *ovcs)
|
2014-10-29 04:35:58 +08:00
|
|
|
{
|
2017-10-18 07:36:23 +08:00
|
|
|
int i, ret;
|
2014-10-29 04:35:58 +08:00
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
for (i = 0; i < ovcs->count; i++) {
|
|
|
|
struct fragment *fragment = &ovcs->fragments[i];
|
2014-10-29 04:35:58 +08:00
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
ret = build_changeset_next_level(ovcs, fragment->target,
|
|
|
|
fragment->overlay,
|
|
|
|
fragment->is_symbols_node);
|
|
|
|
if (ret) {
|
|
|
|
pr_err("apply failed '%pOF'\n", fragment->target);
|
|
|
|
return ret;
|
2014-10-29 04:35:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find the target node using a number of different strategies
|
2017-10-18 07:36:21 +08:00
|
|
|
* in order of preference:
|
2014-10-29 04:35:58 +08:00
|
|
|
*
|
2017-10-18 07:36:21 +08:00
|
|
|
* 1) "target" property containing the phandle of the target
|
|
|
|
* 2) "target-path" property containing the path of the target
|
2014-10-29 04:35:58 +08:00
|
|
|
*/
|
|
|
|
static struct device_node *find_target_node(struct device_node *info_node)
|
|
|
|
{
|
|
|
|
const char *path;
|
|
|
|
u32 val;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = of_property_read_u32(info_node, "target", &val);
|
2017-10-18 07:36:22 +08:00
|
|
|
if (!ret)
|
2014-10-29 04:35:58 +08:00
|
|
|
return of_find_node_by_phandle(val);
|
|
|
|
|
|
|
|
ret = of_property_read_string(info_node, "target-path", &path);
|
2017-10-18 07:36:22 +08:00
|
|
|
if (!ret)
|
2014-10-29 04:35:58 +08:00
|
|
|
return of_find_node_by_path(path);
|
|
|
|
|
2016-06-15 21:32:18 +08:00
|
|
|
pr_err("Failed to find target for node %p (%s)\n",
|
2014-10-29 04:35:58 +08:00
|
|
|
info_node, info_node->name);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* of_fill_overlay_info() - Fill an overlay info structure
|
|
|
|
* @ov Overlay to fill
|
|
|
|
* @info_node: Device node containing the overlay
|
|
|
|
* @ovinfo: Pointer to the overlay info structure to fill
|
|
|
|
*
|
|
|
|
* Fills an overlay info structure with the overlay information
|
|
|
|
* from a device node. This device node must have a target property
|
|
|
|
* which contains a phandle of the overlay target node, and an
|
|
|
|
* __overlay__ child node which has the overlay contents.
|
|
|
|
* Both ovinfo->target & ovinfo->overlay have their references taken.
|
|
|
|
*
|
|
|
|
* Returns 0 on success, or a negative error value.
|
|
|
|
*/
|
2017-10-18 07:36:23 +08:00
|
|
|
static int of_fill_overlay_info(struct overlay_changeset *ovcset,
|
|
|
|
struct device_node *info_node, struct fragment *fragment)
|
2014-10-29 04:35:58 +08:00
|
|
|
{
|
2017-10-18 07:36:23 +08:00
|
|
|
fragment->overlay = of_get_child_by_name(info_node, "__overlay__");
|
|
|
|
if (!fragment->overlay)
|
2014-10-29 04:35:58 +08:00
|
|
|
goto err_fail;
|
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
fragment->target = find_target_node(info_node);
|
|
|
|
if (!fragment->target)
|
2014-10-29 04:35:58 +08:00
|
|
|
goto err_fail;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
err_fail:
|
2017-10-18 07:36:23 +08:00
|
|
|
of_node_put(fragment->target);
|
|
|
|
of_node_put(fragment->overlay);
|
2014-10-29 04:35:58 +08:00
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
memset(fragment, 0, sizeof(*fragment));
|
2014-10-29 04:35:58 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-18 07:36:23 +08:00
|
|
|
* init_overlay_changeset() - initialize overlay changeset from overlay tree
|
|
|
|
* @ovcs Overlay changeset to build
|
|
|
|
* @tree: Contains all the overlay fragments and overlay fixup nodes
|
2014-10-29 04:35:58 +08:00
|
|
|
*
|
2017-10-18 07:36:23 +08:00
|
|
|
* Initialize @ovcs. Populate @ovcs->fragments with node information from
|
|
|
|
* the top level of @tree. The relevant top level nodes are the fragment
|
|
|
|
* nodes and the __symbols__ node. Any other top level node will be ignored.
|
2014-10-29 04:35:58 +08:00
|
|
|
*
|
2017-10-18 07:36:23 +08:00
|
|
|
* Returns 0 on success, -ENOMEM if memory allocation failure, -EINVAL if error
|
|
|
|
* detected in @tree, or -ENODEV if no valid nodes found.
|
2014-10-29 04:35:58 +08:00
|
|
|
*/
|
2017-10-18 07:36:23 +08:00
|
|
|
static int init_overlay_changeset(struct overlay_changeset *ovcs,
|
2014-10-29 04:35:58 +08:00
|
|
|
struct device_node *tree)
|
|
|
|
{
|
|
|
|
struct device_node *node;
|
2017-10-18 07:36:23 +08:00
|
|
|
struct fragment *fragment;
|
|
|
|
struct fragment *fragments;
|
|
|
|
int cnt, ret;
|
2014-10-29 04:35:58 +08:00
|
|
|
|
|
|
|
cnt = 0;
|
|
|
|
for_each_child_of_node(tree, node)
|
|
|
|
cnt++;
|
|
|
|
|
2017-07-20 00:25:22 +08:00
|
|
|
if (of_get_child_by_name(tree, "__symbols__"))
|
|
|
|
cnt++;
|
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
fragments = kcalloc(cnt, sizeof(*fragments), GFP_KERNEL);
|
|
|
|
if (!fragments)
|
2014-10-29 04:35:58 +08:00
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
cnt = 0;
|
|
|
|
for_each_child_of_node(tree, node) {
|
2017-10-18 07:36:23 +08:00
|
|
|
ret = of_fill_overlay_info(ovcs, node, &fragments[cnt]);
|
|
|
|
if (!ret)
|
2014-10-29 04:35:58 +08:00
|
|
|
cnt++;
|
|
|
|
}
|
|
|
|
|
2017-07-20 00:25:22 +08:00
|
|
|
node = of_get_child_by_name(tree, "__symbols__");
|
|
|
|
if (node) {
|
2017-10-18 07:36:23 +08:00
|
|
|
fragment = &fragments[cnt];
|
|
|
|
fragment->overlay = node;
|
|
|
|
fragment->target = of_find_node_by_path("/__symbols__");
|
|
|
|
fragment->is_symbols_node = 1;
|
2017-07-20 00:25:22 +08:00
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
if (!fragment->target) {
|
2017-07-20 00:25:22 +08:00
|
|
|
pr_err("no symbols in root of device tree.\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
cnt++;
|
|
|
|
}
|
|
|
|
|
2017-10-18 07:36:22 +08:00
|
|
|
if (!cnt) {
|
2017-10-18 07:36:23 +08:00
|
|
|
kfree(fragments);
|
2014-10-29 04:35:58 +08:00
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
ovcs->count = cnt;
|
|
|
|
ovcs->fragments = fragments;
|
2014-10-29 04:35:58 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-18 07:36:23 +08:00
|
|
|
* free_overlay_fragments() - Free a fragments array
|
|
|
|
* @ovcs Overlay to free the overlay info from
|
2014-10-29 04:35:58 +08:00
|
|
|
*
|
2017-10-18 07:36:23 +08:00
|
|
|
* Frees the memory of an ovcs->fragments[] array.
|
2014-10-29 04:35:58 +08:00
|
|
|
*/
|
2017-10-18 07:36:23 +08:00
|
|
|
static void free_overlay_fragments(struct overlay_changeset *ovcs)
|
2014-10-29 04:35:58 +08:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* do it in reverse */
|
2017-10-18 07:36:23 +08:00
|
|
|
for (i = ovcs->count - 1; i >= 0; i--) {
|
|
|
|
of_node_put(ovcs->fragments[i].target);
|
|
|
|
of_node_put(ovcs->fragments[i].overlay);
|
2014-10-29 04:35:58 +08:00
|
|
|
}
|
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
kfree(ovcs->fragments);
|
2014-10-29 04:35:58 +08:00
|
|
|
}
|
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
static LIST_HEAD(ovcs_list);
|
|
|
|
static DEFINE_IDR(ovcs_idr);
|
2014-10-29 04:35:58 +08:00
|
|
|
|
|
|
|
/**
|
2017-10-18 07:36:23 +08:00
|
|
|
* of_overlay_apply() - Create and apply an overlay changeset
|
|
|
|
* @tree: Expanded overlay device tree
|
2014-10-29 04:35:58 +08:00
|
|
|
*
|
2017-10-18 07:36:23 +08:00
|
|
|
* Creates and applies an overlay changeset. If successful, the overlay
|
|
|
|
* changeset is added to the overlay changeset list.
|
2014-10-29 04:35:58 +08:00
|
|
|
*
|
2017-10-18 07:36:23 +08:00
|
|
|
* Returns the id of the created overlay changeset, or a negative error number
|
2014-10-29 04:35:58 +08:00
|
|
|
*/
|
2017-10-18 07:36:23 +08:00
|
|
|
int of_overlay_apply(struct device_node *tree)
|
2014-10-29 04:35:58 +08:00
|
|
|
{
|
2017-10-18 07:36:23 +08:00
|
|
|
struct overlay_changeset *ovcs;
|
|
|
|
int id, ret;
|
2014-10-29 04:35:58 +08:00
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
ovcs = kzalloc(sizeof(*ovcs), GFP_KERNEL);
|
|
|
|
if (!ovcs)
|
2014-10-29 04:35:58 +08:00
|
|
|
return -ENOMEM;
|
2017-10-18 07:36:23 +08:00
|
|
|
ovcs->id = -1;
|
2014-10-29 04:35:58 +08:00
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
INIT_LIST_HEAD(&ovcs->ovcs_list);
|
2014-10-29 04:35:58 +08:00
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
of_changeset_init(&ovcs->cset);
|
2014-10-29 04:35:58 +08:00
|
|
|
|
|
|
|
mutex_lock(&of_mutex);
|
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
id = idr_alloc(&ovcs_idr, ovcs, 0, 0, GFP_KERNEL);
|
2014-10-29 04:35:58 +08:00
|
|
|
if (id < 0) {
|
2017-10-18 07:36:23 +08:00
|
|
|
ret = id;
|
2014-10-29 04:35:58 +08:00
|
|
|
goto err_destroy_trans;
|
|
|
|
}
|
2017-10-18 07:36:23 +08:00
|
|
|
ovcs->id = id;
|
2014-10-29 04:35:58 +08:00
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
ret = init_overlay_changeset(ovcs, tree);
|
|
|
|
if (ret) {
|
|
|
|
pr_err("init_overlay_changeset() failed for tree@%pOF\n",
|
2017-06-02 04:50:55 +08:00
|
|
|
tree);
|
2014-10-29 04:35:58 +08:00
|
|
|
goto err_free_idr;
|
|
|
|
}
|
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
ret = overlay_notify(ovcs, OF_OVERLAY_PRE_APPLY);
|
|
|
|
if (ret < 0) {
|
|
|
|
pr_err("%s: Pre-apply notifier failed (ret=%d)\n",
|
|
|
|
__func__, ret);
|
|
|
|
goto err_free_overlay_fragments;
|
2016-11-02 03:14:22 +08:00
|
|
|
}
|
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
ret = build_changeset(ovcs);
|
|
|
|
if (ret)
|
|
|
|
goto err_free_overlay_fragments;
|
2016-06-15 21:32:18 +08:00
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
ret = __of_changeset_apply(&ovcs->cset);
|
|
|
|
if (ret)
|
|
|
|
goto err_free_overlay_fragments;
|
2014-10-29 04:35:58 +08:00
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
list_add_tail(&ovcs->ovcs_list, &ovcs_list);
|
2014-10-29 04:35:58 +08:00
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
overlay_notify(ovcs, OF_OVERLAY_POST_APPLY);
|
2016-11-02 03:14:22 +08:00
|
|
|
|
2014-10-29 04:35:58 +08:00
|
|
|
mutex_unlock(&of_mutex);
|
|
|
|
|
|
|
|
return id;
|
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
err_free_overlay_fragments:
|
|
|
|
free_overlay_fragments(ovcs);
|
2014-10-29 04:35:58 +08:00
|
|
|
err_free_idr:
|
2017-10-18 07:36:23 +08:00
|
|
|
idr_remove(&ovcs_idr, ovcs->id);
|
2014-10-29 04:35:58 +08:00
|
|
|
err_destroy_trans:
|
2017-10-18 07:36:23 +08:00
|
|
|
of_changeset_destroy(&ovcs->cset);
|
|
|
|
kfree(ovcs);
|
2014-10-29 04:35:58 +08:00
|
|
|
mutex_unlock(&of_mutex);
|
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
return ret;
|
2014-10-29 04:35:58 +08:00
|
|
|
}
|
2017-10-18 07:36:23 +08:00
|
|
|
EXPORT_SYMBOL_GPL(of_overlay_apply);
|
2014-10-29 04:35:58 +08:00
|
|
|
|
2017-10-18 07:36:21 +08:00
|
|
|
/*
|
2017-10-18 07:36:23 +08:00
|
|
|
* Find @np in @tree.
|
|
|
|
*
|
|
|
|
* Returns 1 if @np is @tree or is contained in @tree, else 0
|
2017-10-18 07:36:21 +08:00
|
|
|
*/
|
2017-10-18 07:36:23 +08:00
|
|
|
static int find_node(struct device_node *tree, struct device_node *np)
|
2014-10-29 04:35:58 +08:00
|
|
|
{
|
|
|
|
struct device_node *child;
|
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
if (tree == np)
|
2014-10-29 04:35:58 +08:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
for_each_child_of_node(tree, child) {
|
2017-10-18 07:36:23 +08:00
|
|
|
if (find_node(child, np)) {
|
2015-10-22 17:02:48 +08:00
|
|
|
of_node_put(child);
|
2014-10-29 04:35:58 +08:00
|
|
|
return 1;
|
2015-10-22 17:02:48 +08:00
|
|
|
}
|
2014-10-29 04:35:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-10-18 07:36:21 +08:00
|
|
|
/*
|
2017-10-18 07:36:23 +08:00
|
|
|
* Is @remove_ce_np a child of or the same as any
|
|
|
|
* node in an overlay changeset more topmost than @remove_ovcs?
|
|
|
|
*
|
|
|
|
* Returns 1 if found, else 0
|
2017-10-18 07:36:21 +08:00
|
|
|
*/
|
2017-10-18 07:36:23 +08:00
|
|
|
static int node_in_later_cs(struct overlay_changeset *remove_ovcs,
|
|
|
|
struct device_node *remove_ce_np)
|
2014-10-29 04:35:58 +08:00
|
|
|
{
|
2017-10-18 07:36:23 +08:00
|
|
|
struct overlay_changeset *ovcs;
|
2014-10-29 04:35:58 +08:00
|
|
|
struct of_changeset_entry *ce;
|
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
list_for_each_entry_reverse(ovcs, &ovcs_list, ovcs_list) {
|
|
|
|
if (ovcs == remove_ovcs)
|
2014-10-29 04:35:58 +08:00
|
|
|
break;
|
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
list_for_each_entry(ce, &ovcs->cset.entries, node) {
|
|
|
|
if (find_node(ce->np, remove_ce_np)) {
|
2017-06-02 04:50:55 +08:00
|
|
|
pr_err("%s: #%d clashes #%d @%pOF\n",
|
2017-10-18 07:36:23 +08:00
|
|
|
__func__, remove_ovcs->id, ovcs->id,
|
|
|
|
remove_ce_np);
|
|
|
|
return 1;
|
2014-10-29 04:35:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
return 0;
|
2014-10-29 04:35:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We can safely remove the overlay only if it's the top-most one.
|
|
|
|
* Newly applied overlays are inserted at the tail of the overlay list,
|
|
|
|
* so a top most overlay is the one that is closest to the tail.
|
|
|
|
*
|
|
|
|
* The topmost check is done by exploiting this property. For each
|
|
|
|
* affected device node in the log list we check if this overlay is
|
|
|
|
* the one closest to the tail. If another overlay has affected this
|
|
|
|
* device node and is closest to the tail, then removal is not permited.
|
|
|
|
*/
|
2017-10-18 07:36:23 +08:00
|
|
|
static int overlay_removal_is_ok(struct overlay_changeset *remove_ovcs)
|
2014-10-29 04:35:58 +08:00
|
|
|
{
|
2017-10-18 07:36:23 +08:00
|
|
|
struct of_changeset_entry *remove_ce;
|
2014-10-29 04:35:58 +08:00
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
list_for_each_entry(remove_ce, &remove_ovcs->cset.entries, node) {
|
|
|
|
if (node_in_later_cs(remove_ovcs, remove_ce->np)) {
|
|
|
|
pr_err("overlay #%d is not topmost\n", remove_ovcs->id);
|
2014-10-29 04:35:58 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-18 07:36:23 +08:00
|
|
|
* of_overlay_remove() - Revert and free an overlay changeset
|
|
|
|
* @ovcs_id: Overlay changeset id number
|
2014-10-29 04:35:58 +08:00
|
|
|
*
|
2017-10-18 07:36:23 +08:00
|
|
|
* Removes an overlay if it is permissible. ovcs_id was previously returned
|
|
|
|
* by of_overlay_apply().
|
2014-10-29 04:35:58 +08:00
|
|
|
*
|
2015-05-21 20:10:26 +08:00
|
|
|
* Returns 0 on success, or a negative error number
|
2014-10-29 04:35:58 +08:00
|
|
|
*/
|
2017-10-18 07:36:23 +08:00
|
|
|
int of_overlay_remove(int ovcs_id)
|
2014-10-29 04:35:58 +08:00
|
|
|
{
|
2017-10-18 07:36:23 +08:00
|
|
|
struct overlay_changeset *ovcs;
|
|
|
|
int ret = 0;
|
2014-10-29 04:35:58 +08:00
|
|
|
|
|
|
|
mutex_lock(&of_mutex);
|
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
ovcs = idr_find(&ovcs_idr, ovcs_id);
|
|
|
|
if (!ovcs) {
|
|
|
|
ret = -ENODEV;
|
|
|
|
pr_err("remove: Could not find overlay #%d\n", ovcs_id);
|
2014-10-29 04:35:58 +08:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
if (!overlay_removal_is_ok(ovcs)) {
|
|
|
|
ret = -EBUSY;
|
2014-10-29 04:35:58 +08:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
overlay_notify(ovcs, OF_OVERLAY_PRE_REMOVE);
|
|
|
|
list_del(&ovcs->ovcs_list);
|
|
|
|
__of_changeset_revert(&ovcs->cset);
|
|
|
|
overlay_notify(ovcs, OF_OVERLAY_POST_REMOVE);
|
|
|
|
free_overlay_fragments(ovcs);
|
|
|
|
idr_remove(&ovcs_idr, ovcs_id);
|
|
|
|
of_changeset_destroy(&ovcs->cset);
|
|
|
|
kfree(ovcs);
|
2014-10-29 04:35:58 +08:00
|
|
|
|
|
|
|
out:
|
|
|
|
mutex_unlock(&of_mutex);
|
|
|
|
|
2017-10-18 07:36:23 +08:00
|
|
|
return ret;
|
2014-10-29 04:35:58 +08:00
|
|
|
}
|
2017-10-18 07:36:23 +08:00
|
|
|
EXPORT_SYMBOL_GPL(of_overlay_remove);
|
2014-10-29 04:35:58 +08:00
|
|
|
|
|
|
|
/**
|
2017-10-18 07:36:23 +08:00
|
|
|
* of_overlay_remove_all() - Reverts and frees all overlay changesets
|
2014-10-29 04:35:58 +08:00
|
|
|
*
|
|
|
|
* Removes all overlays from the system in the correct order.
|
|
|
|
*
|
2015-05-21 20:10:26 +08:00
|
|
|
* Returns 0 on success, or a negative error number
|
2014-10-29 04:35:58 +08:00
|
|
|
*/
|
2017-10-18 07:36:23 +08:00
|
|
|
int of_overlay_remove_all(void)
|
2014-10-29 04:35:58 +08:00
|
|
|
{
|
2017-10-18 07:36:23 +08:00
|
|
|
struct overlay_changeset *ovcs, *ovcs_n;
|
2014-10-29 04:35:58 +08:00
|
|
|
|
|
|
|
mutex_lock(&of_mutex);
|
|
|
|
|
|
|
|
/* the tail of list is guaranteed to be safe to remove */
|
2017-10-18 07:36:23 +08:00
|
|
|
list_for_each_entry_safe_reverse(ovcs, ovcs_n, &ovcs_list, ovcs_list) {
|
|
|
|
list_del(&ovcs->ovcs_list);
|
|
|
|
__of_changeset_revert(&ovcs->cset);
|
|
|
|
free_overlay_fragments(ovcs);
|
|
|
|
idr_remove(&ovcs_idr, ovcs->id);
|
|
|
|
kfree(ovcs);
|
2014-10-29 04:35:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
mutex_unlock(&of_mutex);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2017-10-18 07:36:23 +08:00
|
|
|
EXPORT_SYMBOL_GPL(of_overlay_remove_all);
|