1/* $NetBSD: overlay.c,v 1.1 2020/06/26 03:23:04 thorpej Exp $ */
2
3/*-
4 * Copyright (c) 2020 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jared McNeill <jmcneill@invisible.ca> and Jason R. Thorpe.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#include "efiboot.h"
33#include "overlay.h"
34
35#include <sys/queue.h>
36
37int	dtoverlay_enabled = 1;
38
39struct dtoverlay_entry {
40	TAILQ_ENTRY(dtoverlay_entry) entries;
41	char overlay_path[];
42};
43TAILQ_HEAD(, dtoverlay_entry) dtoverlay_entries =
44    TAILQ_HEAD_INITIALIZER(dtoverlay_entries);
45
46#define	DTOVERLAY_ENTRY_SIZE(p)	(sizeof(struct dtoverlay_entry) + strlen(p) + 1)
47
48void
49dtoverlay_foreach(void (*fn)(const char *))
50{
51	struct dtoverlay_entry *d;
52
53	TAILQ_FOREACH(d, &dtoverlay_entries, entries) {
54		fn(d->overlay_path);
55	}
56}
57
58void
59dtoverlay_add(const char *overlay_path)
60{
61	struct dtoverlay_entry *d;
62
63	/* Trim leading whitespace */
64	while (*overlay_path == ' ' || *overlay_path == '\t')
65		++overlay_path;
66
67	/* Duplicate check */
68	TAILQ_FOREACH(d, &dtoverlay_entries, entries) {
69		if (strcmp(d->overlay_path, overlay_path) == 0)
70			return;
71	}
72
73	/* Add to list of overlays. */
74	d = alloc(DTOVERLAY_ENTRY_SIZE(overlay_path));
75	strcpy(d->overlay_path, overlay_path);
76	TAILQ_INSERT_TAIL(&dtoverlay_entries, d, entries);
77}
78
79#if 0
80void
81dtoverlay_remove(const char *overlay_path)
82{
83	struct dtoverlay_entry *d;
84
85	/* Trim leading whitespace */
86	while (*overlay_path == ' ' || *overlay_path == '\t')
87		++overlay_path;
88
89	TAILQ_FOREACH(d, &dtoverlay_entries, entries) {
90		if (strcmp(d->overlay_path, overlay_path) == 0) {
91			TAILQ_REMOVE(&dtoverlay_entries, d, entries);
92			dealloc(d, DTOVERLAY_ENTRY_SIZE(d->overlay_path));
93			return;
94		}
95	}
96}
97#endif
98
99void
100dtoverlay_remove_all(void)
101{
102	struct dtoverlay_entry *d;
103
104	while ((d = TAILQ_FIRST(&dtoverlay_entries)) != NULL) {
105		TAILQ_REMOVE(&dtoverlay_entries, d, entries);
106		dealloc(d, DTOVERLAY_ENTRY_SIZE(d->overlay_path));
107	}
108}
109
110void
111dtoverlay_enable(int onoff)
112{
113	dtoverlay_enabled = onoff;
114}
115