^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Functions for working with device tree overlays
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2012 Pantelis Antoniou <panto@antoniou-consulting.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright (C) 2012 Texas Instruments Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #define pr_fmt(fmt) "OF: overlay: " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/of_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/of_fdt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/ctype.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/libfdt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/idr.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include "of_private.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * struct target - info about current target node as recursing through overlay
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * @np: node where current level of overlay will be applied
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * @in_livetree: @np is a node in the live devicetree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) * Used in the algorithm to create the portion of a changeset that describes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * an overlay fragment, which is a devicetree subtree. Initially @np is a node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * in the live devicetree where the overlay subtree is targeted to be grafted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * into. When recursing to the next level of the overlay subtree, the target
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * also recurses to the next level of the live devicetree, as long as overlay
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * subtree node also exists in the live devicetree. When a node in the overlay
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * subtree does not exist at the same level in the live devicetree, target->np
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * points to a newly allocated node, and all subsequent targets in the subtree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) * will be newly allocated nodes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) struct target {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) struct device_node *np;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) bool in_livetree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) * struct fragment - info about fragment nodes in overlay expanded device tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) * @target: target of the overlay operation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) * @overlay: pointer to the __overlay__ node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) struct fragment {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) struct device_node *overlay;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) struct device_node *target;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) * struct overlay_changeset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) * @id: changeset identifier
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) * @ovcs_list: list on which we are located
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) * @fdt: FDT that was unflattened to create @overlay_tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) * @overlay_tree: expanded device tree that contains the fragment nodes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) * @count: count of fragment structures
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) * @fragments: fragment nodes in the overlay expanded device tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) * @symbols_fragment: last element of @fragments[] is the __symbols__ node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) * @cset: changeset to apply fragments to live device tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) struct overlay_changeset {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) int id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) struct list_head ovcs_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) const void *fdt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) struct device_node *overlay_tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) int count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) struct fragment *fragments;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) bool symbols_fragment;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) struct of_changeset cset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) /* flags are sticky - once set, do not reset */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) static int devicetree_state_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) #define DTSF_APPLY_FAIL 0x01
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) #define DTSF_REVERT_FAIL 0x02
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) * If a changeset apply or revert encounters an error, an attempt will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) * be made to undo partial changes, but may fail. If the undo fails
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) * we do not know the state of the devicetree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) static int devicetree_corrupt(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) return devicetree_state_flags &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) (DTSF_APPLY_FAIL | DTSF_REVERT_FAIL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) static int build_changeset_next_level(struct overlay_changeset *ovcs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) struct target *target, const struct device_node *overlay_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) * of_resolve_phandles() finds the largest phandle in the live tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) * of_overlay_apply() may add a larger phandle to the live tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) * Do not allow race between two overlays being applied simultaneously:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) * mutex_lock(&of_overlay_phandle_mutex)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) * of_resolve_phandles()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) * of_overlay_apply()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) * mutex_unlock(&of_overlay_phandle_mutex)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) static DEFINE_MUTEX(of_overlay_phandle_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) void of_overlay_mutex_lock(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) mutex_lock(&of_overlay_phandle_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) void of_overlay_mutex_unlock(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) mutex_unlock(&of_overlay_phandle_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) static LIST_HEAD(ovcs_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) static DEFINE_IDR(ovcs_idr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) static BLOCKING_NOTIFIER_HEAD(overlay_notify_chain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) * of_overlay_notifier_register() - Register notifier for overlay operations
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) * @nb: Notifier block to register
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) * Register for notification on overlay operations on device tree nodes. The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) * reported actions definied by @of_reconfig_change. The notifier callback
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) * furthermore receives a pointer to the affected device tree node.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) * Note that a notifier callback is not supposed to store pointers to a device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) * tree node or its content beyond @OF_OVERLAY_POST_REMOVE corresponding to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) * respective node it received.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) int of_overlay_notifier_register(struct notifier_block *nb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) return blocking_notifier_chain_register(&overlay_notify_chain, nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) EXPORT_SYMBOL_GPL(of_overlay_notifier_register);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) * of_overlay_notifier_register() - Unregister notifier for overlay operations
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) * @nb: Notifier block to unregister
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) int of_overlay_notifier_unregister(struct notifier_block *nb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) return blocking_notifier_chain_unregister(&overlay_notify_chain, nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) EXPORT_SYMBOL_GPL(of_overlay_notifier_unregister);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) static char *of_overlay_action_name[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) "pre-apply",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) "post-apply",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) "pre-remove",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) "post-remove",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) static int overlay_notify(struct overlay_changeset *ovcs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) enum of_overlay_notify_action action)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) struct of_overlay_notify_data nd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) int i, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) for (i = 0; i < ovcs->count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) struct fragment *fragment = &ovcs->fragments[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) nd.target = fragment->target;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) nd.overlay = fragment->overlay;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) ret = blocking_notifier_call_chain(&overlay_notify_chain,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) action, &nd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) if (ret == NOTIFY_OK || ret == NOTIFY_STOP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) ret = notifier_to_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) pr_err("overlay changeset %s notifier error %d, target: %pOF\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) of_overlay_action_name[action], ret, nd.target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) * The values of properties in the "/__symbols__" node are paths in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) * the ovcs->overlay_tree. When duplicating the properties, the paths
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) * need to be adjusted to be the correct path for the live device tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) * The paths refer to a node in the subtree of a fragment node's "__overlay__"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) * node, for example "/fragment@0/__overlay__/symbol_path_tail",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) * where symbol_path_tail can be a single node or it may be a multi-node path.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) * The duplicated property value will be modified by replacing the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) * "/fragment_name/__overlay/" portion of the value with the target
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) * path from the fragment node.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) static struct property *dup_and_fixup_symbol_prop(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) struct overlay_changeset *ovcs, const struct property *prop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) struct fragment *fragment;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) struct property *new_prop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) struct device_node *fragment_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) struct device_node *overlay_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) const char *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) const char *path_tail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) const char *target_path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) int k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) int overlay_name_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) int path_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) int path_tail_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) int target_path_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) if (!prop->value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) if (strnlen(prop->value, prop->length) >= prop->length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) path = prop->value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) path_len = strlen(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) if (path_len < 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) fragment_node = __of_find_node_by_path(ovcs->overlay_tree, path + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) overlay_node = __of_find_node_by_path(fragment_node, "__overlay__/");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) of_node_put(fragment_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) of_node_put(overlay_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) for (k = 0; k < ovcs->count; k++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) fragment = &ovcs->fragments[k];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) if (fragment->overlay == overlay_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) if (k >= ovcs->count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) overlay_name_len = snprintf(NULL, 0, "%pOF", fragment->overlay);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) if (overlay_name_len > path_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) path_tail = path + overlay_name_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) path_tail_len = strlen(path_tail);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) target_path = kasprintf(GFP_KERNEL, "%pOF", fragment->target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) if (!target_path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) target_path_len = strlen(target_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) new_prop = kzalloc(sizeof(*new_prop), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) if (!new_prop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) goto err_free_target_path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) new_prop->name = kstrdup(prop->name, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) new_prop->length = target_path_len + path_tail_len + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) new_prop->value = kzalloc(new_prop->length, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) if (!new_prop->name || !new_prop->value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) goto err_free_new_prop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) strcpy(new_prop->value, target_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) strcpy(new_prop->value + target_path_len, path_tail);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) of_property_set_flag(new_prop, OF_DYNAMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) kfree(target_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) return new_prop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) err_free_new_prop:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) kfree(new_prop->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) kfree(new_prop->value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) kfree(new_prop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) err_free_target_path:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) kfree(target_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) * add_changeset_property() - add @overlay_prop to overlay changeset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) * @ovcs: overlay changeset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) * @target: where @overlay_prop will be placed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) * @overlay_prop: property to add or update, from overlay tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) * @is_symbols_prop: 1 if @overlay_prop is from node "/__symbols__"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) * If @overlay_prop does not already exist in live devicetree, add changeset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) * entry to add @overlay_prop in @target, else add changeset entry to update
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) * value of @overlay_prop.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) * @target may be either in the live devicetree or in a new subtree that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) * is contained in the changeset.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) * Some special properties are not added or updated (no error returned):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) * "name", "phandle", "linux,phandle".
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) * Properties "#address-cells" and "#size-cells" are not updated if they
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) * are already in the live tree, but if present in the live tree, the values
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) * in the overlay must match the values in the live tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) * Update of property in symbols node is not allowed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) * Returns 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) * invalid @overlay.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) static int add_changeset_property(struct overlay_changeset *ovcs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) struct target *target, struct property *overlay_prop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) bool is_symbols_prop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) struct property *new_prop = NULL, *prop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) if (target->in_livetree)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) if (!of_prop_cmp(overlay_prop->name, "name") ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) !of_prop_cmp(overlay_prop->name, "phandle") ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) !of_prop_cmp(overlay_prop->name, "linux,phandle"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) if (target->in_livetree)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) prop = of_find_property(target->np, overlay_prop->name, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) prop = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) if (prop) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) if (!of_prop_cmp(prop->name, "#address-cells")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) if (!of_prop_val_eq(prop, overlay_prop)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) pr_err("ERROR: changing value of #address-cells is not allowed in %pOF\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) target->np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) } else if (!of_prop_cmp(prop->name, "#size-cells")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) if (!of_prop_val_eq(prop, overlay_prop)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) pr_err("ERROR: changing value of #size-cells is not allowed in %pOF\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) target->np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) if (is_symbols_prop) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) if (prop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) new_prop = dup_and_fixup_symbol_prop(ovcs, overlay_prop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) new_prop = __of_prop_dup(overlay_prop, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) if (!new_prop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) if (!prop) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) if (!target->in_livetree) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) new_prop->next = target->np->deadprops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) target->np->deadprops = new_prop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) ret = of_changeset_add_property(&ovcs->cset, target->np,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) new_prop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) ret = of_changeset_update_property(&ovcs->cset, target->np,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) new_prop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) if (!of_node_check_flag(target->np, OF_OVERLAY))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) pr_err("WARNING: memory leak will occur if overlay removed, property: %pOF/%s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) target->np, new_prop->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) kfree(new_prop->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) kfree(new_prop->value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) kfree(new_prop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) * add_changeset_node() - add @node (and children) to overlay changeset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) * @ovcs: overlay changeset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) * @target: where @node will be placed in live tree or changeset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) * @node: node from within overlay device tree fragment
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) * If @node does not already exist in @target, add changeset entry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) * to add @node in @target.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) * If @node already exists in @target, and the existing node has
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) * a phandle, the overlay node is not allowed to have a phandle.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) * If @node has child nodes, add the children recursively via
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) * build_changeset_next_level().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) * NOTE_1: A live devicetree created from a flattened device tree (FDT) will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) * not contain the full path in node->full_name. Thus an overlay
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) * created from an FDT also will not contain the full path in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) * node->full_name. However, a live devicetree created from Open
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) * Firmware may have the full path in node->full_name.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) * add_changeset_node() follows the FDT convention and does not include
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) * the full path in node->full_name. Even though it expects the overlay
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) * to not contain the full path, it uses kbasename() to remove the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) * full path should it exist. It also uses kbasename() in comparisons
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) * to nodes in the live devicetree so that it can apply an overlay to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) * a live devicetree created from Open Firmware.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) * NOTE_2: Multiple mods of created nodes not supported.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) * Returns 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) * invalid @overlay.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) static int add_changeset_node(struct overlay_changeset *ovcs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) struct target *target, struct device_node *node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) const char *node_kbasename;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) const __be32 *phandle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) struct device_node *tchild;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) struct target target_child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) int ret = 0, size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) node_kbasename = kbasename(node->full_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) for_each_child_of_node(target->np, tchild)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) if (!of_node_cmp(node_kbasename, kbasename(tchild->full_name)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) if (!tchild) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) tchild = __of_node_dup(NULL, node_kbasename);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) if (!tchild)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) tchild->parent = target->np;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) tchild->name = __of_get_property(node, "name", NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) if (!tchild->name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) tchild->name = "<NULL>";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) /* ignore obsolete "linux,phandle" */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) phandle = __of_get_property(node, "phandle", &size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) if (phandle && (size == 4))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) tchild->phandle = be32_to_cpup(phandle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) of_node_set_flag(tchild, OF_OVERLAY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) ret = of_changeset_attach_node(&ovcs->cset, tchild);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) target_child.np = tchild;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) target_child.in_livetree = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) ret = build_changeset_next_level(ovcs, &target_child, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) of_node_put(tchild);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) if (node->phandle && tchild->phandle) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) target_child.np = tchild;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) target_child.in_livetree = target->in_livetree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) ret = build_changeset_next_level(ovcs, &target_child, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) of_node_put(tchild);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) * build_changeset_next_level() - add level of overlay changeset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) * @ovcs: overlay changeset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) * @target: where to place @overlay_node in live tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) * @overlay_node: node from within an overlay device tree fragment
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) * Add the properties (if any) and nodes (if any) from @overlay_node to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) * @ovcs->cset changeset. If an added node has child nodes, they will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) * be added recursively.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) * Do not allow symbols node to have any children.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) * Returns 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) * invalid @overlay_node.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) static int build_changeset_next_level(struct overlay_changeset *ovcs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) struct target *target, const struct device_node *overlay_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) struct device_node *child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) struct property *prop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) for_each_property_of_node(overlay_node, prop) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) ret = add_changeset_property(ovcs, target, prop, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) pr_debug("Failed to apply prop @%pOF/%s, err=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) target->np, prop->name, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) for_each_child_of_node(overlay_node, child) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) ret = add_changeset_node(ovcs, target, child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) pr_debug("Failed to apply node @%pOF/%pOFn, err=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) target->np, child, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) of_node_put(child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) * Add the properties from __overlay__ node to the @ovcs->cset changeset.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) static int build_changeset_symbols_node(struct overlay_changeset *ovcs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) struct target *target,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) const struct device_node *overlay_symbols_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) struct property *prop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) for_each_property_of_node(overlay_symbols_node, prop) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) ret = add_changeset_property(ovcs, target, prop, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) pr_debug("Failed to apply symbols prop @%pOF/%s, err=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) target->np, prop->name, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) static int find_dup_cset_node_entry(struct overlay_changeset *ovcs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) struct of_changeset_entry *ce_1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) struct of_changeset_entry *ce_2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) char *fn_1, *fn_2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) int node_path_match;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) if (ce_1->action != OF_RECONFIG_ATTACH_NODE &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) ce_1->action != OF_RECONFIG_DETACH_NODE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) ce_2 = ce_1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) list_for_each_entry_continue(ce_2, &ovcs->cset.entries, node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) if ((ce_2->action != OF_RECONFIG_ATTACH_NODE &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) ce_2->action != OF_RECONFIG_DETACH_NODE) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) of_node_cmp(ce_1->np->full_name, ce_2->np->full_name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) fn_1 = kasprintf(GFP_KERNEL, "%pOF", ce_1->np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) fn_2 = kasprintf(GFP_KERNEL, "%pOF", ce_2->np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) node_path_match = !strcmp(fn_1, fn_2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) kfree(fn_1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) kfree(fn_2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) if (node_path_match) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) pr_err("ERROR: multiple fragments add and/or delete node %pOF\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) ce_1->np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) static int find_dup_cset_prop(struct overlay_changeset *ovcs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) struct of_changeset_entry *ce_1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) struct of_changeset_entry *ce_2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) char *fn_1, *fn_2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) int node_path_match;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) if (ce_1->action != OF_RECONFIG_ADD_PROPERTY &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) ce_1->action != OF_RECONFIG_REMOVE_PROPERTY &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) ce_1->action != OF_RECONFIG_UPDATE_PROPERTY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) ce_2 = ce_1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) list_for_each_entry_continue(ce_2, &ovcs->cset.entries, node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) if ((ce_2->action != OF_RECONFIG_ADD_PROPERTY &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) ce_2->action != OF_RECONFIG_REMOVE_PROPERTY &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) ce_2->action != OF_RECONFIG_UPDATE_PROPERTY) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) of_node_cmp(ce_1->np->full_name, ce_2->np->full_name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) fn_1 = kasprintf(GFP_KERNEL, "%pOF", ce_1->np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) fn_2 = kasprintf(GFP_KERNEL, "%pOF", ce_2->np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) node_path_match = !strcmp(fn_1, fn_2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) kfree(fn_1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) kfree(fn_2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) if (node_path_match &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) !of_prop_cmp(ce_1->prop->name, ce_2->prop->name)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) pr_err("ERROR: multiple fragments add, update, and/or delete property %pOF/%s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) ce_1->np, ce_1->prop->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) * changeset_dup_entry_check() - check for duplicate entries
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) * @ovcs: Overlay changeset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) * Check changeset @ovcs->cset for multiple {add or delete} node entries for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) * the same node or duplicate {add, delete, or update} properties entries
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) * for the same property.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) * Returns 0 on success, or -EINVAL if duplicate changeset entry found.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) static int changeset_dup_entry_check(struct overlay_changeset *ovcs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) struct of_changeset_entry *ce_1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) int dup_entry = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) list_for_each_entry(ce_1, &ovcs->cset.entries, node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) dup_entry |= find_dup_cset_node_entry(ovcs, ce_1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) dup_entry |= find_dup_cset_prop(ovcs, ce_1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) return dup_entry ? -EINVAL : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) * build_changeset() - populate overlay changeset in @ovcs from @ovcs->fragments
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) * @ovcs: Overlay changeset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) * Create changeset @ovcs->cset to contain the nodes and properties of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) * overlay device tree fragments in @ovcs->fragments[]. If an error occurs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) * any portions of the changeset that were successfully created will remain
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) * in @ovcs->cset.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) * Returns 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) * invalid overlay in @ovcs->fragments[].
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) static int build_changeset(struct overlay_changeset *ovcs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) struct fragment *fragment;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) struct target target;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) int fragments_count, i, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) * if there is a symbols fragment in ovcs->fragments[i] it is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) * the final element in the array
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) if (ovcs->symbols_fragment)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) fragments_count = ovcs->count - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) fragments_count = ovcs->count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) for (i = 0; i < fragments_count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) fragment = &ovcs->fragments[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) target.np = fragment->target;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) target.in_livetree = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) ret = build_changeset_next_level(ovcs, &target,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) fragment->overlay);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) pr_debug("fragment apply failed '%pOF'\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) fragment->target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) if (ovcs->symbols_fragment) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) fragment = &ovcs->fragments[ovcs->count - 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) target.np = fragment->target;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) target.in_livetree = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) ret = build_changeset_symbols_node(ovcs, &target,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) fragment->overlay);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) pr_debug("symbols fragment apply failed '%pOF'\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) fragment->target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) return changeset_dup_entry_check(ovcs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) * Find the target node using a number of different strategies
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) * in order of preference:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) * 1) "target" property containing the phandle of the target
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) * 2) "target-path" property containing the path of the target
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) static struct device_node *find_target(struct device_node *info_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) struct device_node *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) const char *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) u32 val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) ret = of_property_read_u32(info_node, "target", &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) node = of_find_node_by_phandle(val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) if (!node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) pr_err("find target, node: %pOF, phandle 0x%x not found\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) info_node, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) return node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) ret = of_property_read_string(info_node, "target-path", &path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) node = of_find_node_by_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) if (!node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) pr_err("find target, node: %pOF, path '%s' not found\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) info_node, path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) return node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) pr_err("find target, node: %pOF, no target property\n", info_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) * init_overlay_changeset() - initialize overlay changeset from overlay tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) * @ovcs: Overlay changeset to build
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) * @fdt: the FDT that was unflattened to create @tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) * @tree: Contains all the overlay fragments and overlay fixup nodes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) * Initialize @ovcs. Populate @ovcs->fragments with node information from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) * the top level of @tree. The relevant top level nodes are the fragment
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) * nodes and the __symbols__ node. Any other top level node will be ignored.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) * Returns 0 on success, -ENOMEM if memory allocation failure, -EINVAL if error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) * detected in @tree, or -ENOSPC if idr_alloc() error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) static int init_overlay_changeset(struct overlay_changeset *ovcs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) const void *fdt, struct device_node *tree)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) struct device_node *node, *overlay_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) struct fragment *fragment;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) struct fragment *fragments;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) int cnt, id, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) * Warn for some issues. Can not return -EINVAL for these until
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) * of_unittest_apply_overlay() is fixed to pass these checks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) if (!of_node_check_flag(tree, OF_DYNAMIC))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) pr_debug("%s() tree is not dynamic\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) if (!of_node_check_flag(tree, OF_DETACHED))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) pr_debug("%s() tree is not detached\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) if (!of_node_is_root(tree))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) pr_debug("%s() tree is not root\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) ovcs->overlay_tree = tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) ovcs->fdt = fdt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) INIT_LIST_HEAD(&ovcs->ovcs_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) of_changeset_init(&ovcs->cset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) id = idr_alloc(&ovcs_idr, ovcs, 1, 0, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) if (id <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) return id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) cnt = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) /* fragment nodes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) for_each_child_of_node(tree, node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) overlay_node = of_get_child_by_name(node, "__overlay__");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) if (overlay_node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) cnt++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) of_node_put(overlay_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) node = of_get_child_by_name(tree, "__symbols__");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) if (node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) cnt++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) of_node_put(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) fragments = kcalloc(cnt, sizeof(*fragments), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) if (!fragments) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) goto err_free_idr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) cnt = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) for_each_child_of_node(tree, node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) overlay_node = of_get_child_by_name(node, "__overlay__");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) if (!overlay_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) fragment = &fragments[cnt];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) fragment->overlay = overlay_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) fragment->target = find_target(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) if (!fragment->target) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) of_node_put(fragment->overlay);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) of_node_put(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) goto err_free_fragments;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) cnt++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) * if there is a symbols fragment in ovcs->fragments[i] it is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) * the final element in the array
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) node = of_get_child_by_name(tree, "__symbols__");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) if (node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) ovcs->symbols_fragment = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) fragment = &fragments[cnt];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) fragment->overlay = node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) fragment->target = of_find_node_by_path("/__symbols__");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) if (!fragment->target) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) pr_err("symbols in overlay, but not in live tree\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) goto err_free_fragments;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) cnt++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) if (!cnt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) pr_err("no fragments or symbols in overlay\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) goto err_free_fragments;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) ovcs->id = id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) ovcs->count = cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) ovcs->fragments = fragments;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) err_free_fragments:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) kfree(fragments);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) err_free_idr:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) idr_remove(&ovcs_idr, id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) pr_err("%s() failed, ret = %d\n", __func__, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) static void free_overlay_changeset(struct overlay_changeset *ovcs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) if (ovcs->cset.entries.next)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) of_changeset_destroy(&ovcs->cset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) if (ovcs->id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) idr_remove(&ovcs_idr, ovcs->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) for (i = 0; i < ovcs->count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) of_node_put(ovcs->fragments[i].target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) of_node_put(ovcs->fragments[i].overlay);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) kfree(ovcs->fragments);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) * There should be no live pointers into ovcs->overlay_tree and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) * ovcs->fdt due to the policy that overlay notifiers are not allowed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) * to retain pointers into the overlay devicetree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) kfree(ovcs->overlay_tree);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) kfree(ovcs->fdt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) kfree(ovcs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) * internal documentation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) * of_overlay_apply() - Create and apply an overlay changeset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) * @fdt: the FDT that was unflattened to create @tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) * @tree: Expanded overlay device tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) * @ovcs_id: Pointer to overlay changeset id
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) * Creates and applies an overlay changeset.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) * If an error occurs in a pre-apply notifier, then no changes are made
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) * to the device tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) * A non-zero return value will not have created the changeset if error is from:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) * - parameter checks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) * - building the changeset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) * - overlay changeset pre-apply notifier
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) * If an error is returned by an overlay changeset pre-apply notifier
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) * then no further overlay changeset pre-apply notifier will be called.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) * A non-zero return value will have created the changeset if error is from:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) * - overlay changeset entry notifier
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) * - overlay changeset post-apply notifier
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) * If an error is returned by an overlay changeset post-apply notifier
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) * then no further overlay changeset post-apply notifier will be called.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) * If more than one notifier returns an error, then the last notifier
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) * error to occur is returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) * If an error occurred while applying the overlay changeset, then an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) * attempt is made to revert any changes that were made to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) * device tree. If there were any errors during the revert attempt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) * then the state of the device tree can not be determined, and any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) * following attempt to apply or remove an overlay changeset will be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) * refused.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) * Returns 0 on success, or a negative error number. Overlay changeset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) * id is returned to *ovcs_id.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) static int of_overlay_apply(const void *fdt, struct device_node *tree,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) int *ovcs_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) struct overlay_changeset *ovcs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) int ret = 0, ret_revert, ret_tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) * As of this point, fdt and tree belong to the overlay changeset.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) * overlay changeset code is responsible for freeing them.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) if (devicetree_corrupt()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) pr_err("devicetree state suspect, refuse to apply overlay\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) kfree(fdt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) kfree(tree);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) ret = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) ovcs = kzalloc(sizeof(*ovcs), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) if (!ovcs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) kfree(fdt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) kfree(tree);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) of_overlay_mutex_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) mutex_lock(&of_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) ret = of_resolve_phandles(tree);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) goto err_free_tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) ret = init_overlay_changeset(ovcs, fdt, tree);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) goto err_free_tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) * after overlay_notify(), ovcs->overlay_tree related pointers may have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) * leaked to drivers, so can not kfree() tree, aka ovcs->overlay_tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) * and can not free fdt, aka ovcs->fdt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) ret = overlay_notify(ovcs, OF_OVERLAY_PRE_APPLY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) pr_err("overlay changeset pre-apply notify error %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) goto err_free_overlay_changeset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965) ret = build_changeset(ovcs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) goto err_free_overlay_changeset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) ret_revert = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) ret = __of_changeset_apply_entries(&ovcs->cset, &ret_revert);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) if (ret_revert) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) pr_debug("overlay changeset revert error %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) ret_revert);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) devicetree_state_flags |= DTSF_APPLY_FAIL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977) goto err_free_overlay_changeset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) ret = __of_changeset_apply_notify(&ovcs->cset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982) pr_err("overlay apply changeset entry notify error %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) /* notify failure is not fatal, continue */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985) list_add_tail(&ovcs->ovcs_list, &ovcs_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) *ovcs_id = ovcs->id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) ret_tmp = overlay_notify(ovcs, OF_OVERLAY_POST_APPLY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) if (ret_tmp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) pr_err("overlay changeset post-apply notify error %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991) ret_tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) ret = ret_tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998) err_free_tree:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999) kfree(fdt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) kfree(tree);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) err_free_overlay_changeset:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) free_overlay_changeset(ovcs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) mutex_unlock(&of_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) of_overlay_mutex_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) pr_debug("%s() err=%d\n", __func__, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) int of_overlay_fdt_apply(const void *overlay_fdt, u32 overlay_fdt_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) int *ovcs_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) const void *new_fdt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) u32 size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) struct device_node *overlay_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) *ovcs_id = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) if (overlay_fdt_size < sizeof(struct fdt_header) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) fdt_check_header(overlay_fdt)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) pr_err("Invalid overlay_fdt header\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) size = fdt_totalsize(overlay_fdt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) if (overlay_fdt_size < size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) * Must create permanent copy of FDT because of_fdt_unflatten_tree()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) * will create pointers to the passed in FDT in the unflattened tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) new_fdt = kmemdup(overlay_fdt, size, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) if (!new_fdt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) of_fdt_unflatten_tree(new_fdt, NULL, &overlay_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) if (!overlay_root) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) pr_err("unable to unflatten overlay_fdt\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) goto out_free_new_fdt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) ret = of_overlay_apply(new_fdt, overlay_root, ovcs_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) * new_fdt and overlay_root now belong to the overlay
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) * changeset.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) * overlay changeset code is responsible for freeing them.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) out_free_new_fdt:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) kfree(new_fdt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) EXPORT_SYMBOL_GPL(of_overlay_fdt_apply);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) * Find @np in @tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) * Returns 1 if @np is @tree or is contained in @tree, else 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) static int find_node(struct device_node *tree, struct device_node *np)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) struct device_node *child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) if (tree == np)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) for_each_child_of_node(tree, child) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) if (find_node(child, np)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) of_node_put(child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) * Is @remove_ce_node a child of, a parent of, or the same as any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) * node in an overlay changeset more topmost than @remove_ovcs?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) * Returns 1 if found, else 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) static int node_overlaps_later_cs(struct overlay_changeset *remove_ovcs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) struct device_node *remove_ce_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) struct overlay_changeset *ovcs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) struct of_changeset_entry *ce;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) list_for_each_entry_reverse(ovcs, &ovcs_list, ovcs_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) if (ovcs == remove_ovcs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) list_for_each_entry(ce, &ovcs->cset.entries, node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) if (find_node(ce->np, remove_ce_node)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) pr_err("%s: #%d overlaps with #%d @%pOF\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) __func__, remove_ovcs->id, ovcs->id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) remove_ce_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) if (find_node(remove_ce_node, ce->np)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) pr_err("%s: #%d overlaps with #%d @%pOF\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) __func__, remove_ovcs->id, ovcs->id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) remove_ce_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) * We can safely remove the overlay only if it's the top-most one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) * Newly applied overlays are inserted at the tail of the overlay list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) * so a top most overlay is the one that is closest to the tail.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) * The topmost check is done by exploiting this property. For each
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) * affected device node in the log list we check if this overlay is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) * the one closest to the tail. If another overlay has affected this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) * device node and is closest to the tail, then removal is not permited.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) static int overlay_removal_is_ok(struct overlay_changeset *remove_ovcs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) struct of_changeset_entry *remove_ce;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) list_for_each_entry(remove_ce, &remove_ovcs->cset.entries, node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) if (node_overlaps_later_cs(remove_ovcs, remove_ce->np)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) pr_err("overlay #%d is not topmost\n", remove_ovcs->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) * of_overlay_remove() - Revert and free an overlay changeset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) * @ovcs_id: Pointer to overlay changeset id
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) * Removes an overlay if it is permissible. @ovcs_id was previously returned
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) * by of_overlay_fdt_apply().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) * If an error occurred while attempting to revert the overlay changeset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) * then an attempt is made to re-apply any changeset entry that was
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) * reverted. If an error occurs on re-apply then the state of the device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) * tree can not be determined, and any following attempt to apply or remove
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) * an overlay changeset will be refused.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) * A non-zero return value will not revert the changeset if error is from:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) * - parameter checks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) * - overlay changeset pre-remove notifier
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) * - overlay changeset entry revert
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) * If an error is returned by an overlay changeset pre-remove notifier
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) * then no further overlay changeset pre-remove notifier will be called.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) * If more than one notifier returns an error, then the last notifier
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) * error to occur is returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) * A non-zero return value will revert the changeset if error is from:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) * - overlay changeset entry notifier
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) * - overlay changeset post-remove notifier
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) * If an error is returned by an overlay changeset post-remove notifier
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) * then no further overlay changeset post-remove notifier will be called.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) * Returns 0 on success, or a negative error number. *ovcs_id is set to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) * zero after reverting the changeset, even if a subsequent error occurs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) int of_overlay_remove(int *ovcs_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) struct overlay_changeset *ovcs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) int ret, ret_apply, ret_tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) if (devicetree_corrupt()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) pr_err("suspect devicetree state, refuse to remove overlay\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) ret = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) mutex_lock(&of_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) ovcs = idr_find(&ovcs_idr, *ovcs_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) if (!ovcs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) ret = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) pr_err("remove: Could not find overlay #%d\n", *ovcs_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) if (!overlay_removal_is_ok(ovcs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) ret = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) ret = overlay_notify(ovcs, OF_OVERLAY_PRE_REMOVE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) pr_err("overlay changeset pre-remove notify error %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) list_del(&ovcs->ovcs_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) ret_apply = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) ret = __of_changeset_revert_entries(&ovcs->cset, &ret_apply);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) if (ret_apply)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) devicetree_state_flags |= DTSF_REVERT_FAIL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) ret = __of_changeset_revert_notify(&ovcs->cset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) pr_err("overlay remove changeset entry notify error %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) /* notify failure is not fatal, continue */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) *ovcs_id = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) ret_tmp = overlay_notify(ovcs, OF_OVERLAY_POST_REMOVE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) if (ret_tmp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) pr_err("overlay changeset post-remove notify error %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) ret_tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) ret = ret_tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) free_overlay_changeset(ovcs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) mutex_unlock(&of_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) pr_debug("%s() err=%d\n", __func__, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) EXPORT_SYMBOL_GPL(of_overlay_remove);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) * of_overlay_remove_all() - Reverts and frees all overlay changesets
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) * Removes all overlays from the system in the correct order.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) * Returns 0 on success, or a negative error number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) int of_overlay_remove_all(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) struct overlay_changeset *ovcs, *ovcs_n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) /* the tail of list is guaranteed to be safe to remove */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) list_for_each_entry_safe_reverse(ovcs, ovcs_n, &ovcs_list, ovcs_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) ret = of_overlay_remove(&ovcs->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) EXPORT_SYMBOL_GPL(of_overlay_remove_all);