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