Home | History | Annotate | Line # | Download | only in efiboot
      1 /* $NetBSD: module.c,v 1.2 2022/09/21 14:30:01 riastradh 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 (at) invisible.ca>.
      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 "module.h"
     34 
     35 #include <sys/queue.h>
     36 
     37 int module_enabled = 1;
     38 char module_prefix[128];
     39 
     40 struct boot_module {
     41 	char *module_name;
     42 	TAILQ_ENTRY(boot_module) entries;
     43 };
     44 TAILQ_HEAD(, boot_module) boot_modules = TAILQ_HEAD_INITIALIZER(boot_modules);
     45 
     46 static const char *
     47 module_machine(void)
     48 {
     49 	return EFIBOOT_MODULE_MACHINE;
     50 }
     51 
     52 static void
     53 module_set_prefix(const char *kernel_path)
     54 {
     55 #ifdef KERNEL_DIR
     56 	char *ptr = strrchr(kernel_path, '/');
     57 	if (ptr)
     58 		*ptr = '\0';
     59 	snprintf(module_prefix, sizeof(module_prefix), "%s/modules", kernel_path);
     60 	if (ptr)
     61 		*ptr = '/';
     62 #else
     63 	const u_int vmajor = netbsd_version / 100000000;
     64 	const u_int vminor = netbsd_version / 1000000 % 100;
     65 	const u_int vpatch = netbsd_version / 100 % 10000;
     66 
     67 	if (vminor == 99) {
     68 		snprintf(module_prefix, sizeof(module_prefix),
     69 		    "/stand/%s/%u.%u.%u/modules", module_machine(),
     70 		    vmajor, vminor, vpatch);
     71 	} else if (vmajor != 0) {
     72 		snprintf(module_prefix, sizeof(module_prefix),
     73 		    "/stand/%s/%u.%u/modules", module_machine(),
     74 		    vmajor, vminor);
     75 	}
     76 #endif
     77 }
     78 
     79 void
     80 module_init(const char *kernel_path)
     81 {
     82 	module_set_prefix(kernel_path);
     83 }
     84 
     85 void
     86 module_foreach(void (*fn)(const char *))
     87 {
     88 	struct boot_module *bm;
     89 
     90 	TAILQ_FOREACH(bm, &boot_modules, entries) {
     91 		fn(bm->module_name);
     92 	}
     93 }
     94 
     95 void
     96 module_add(const char *module_name)
     97 {
     98 	struct boot_module *bm;
     99 
    100 	/* Trim leading whitespace */
    101 	while (*module_name == ' ' || *module_name == '\t')
    102 		++module_name;
    103 
    104 	/* Duplicate check */
    105 	TAILQ_FOREACH(bm, &boot_modules, entries) {
    106 		if (strcmp(bm->module_name, module_name) == 0)
    107 			return;
    108 	}
    109 
    110 	/* Add to list of modules */
    111 	bm = alloc(sizeof(*bm));
    112 	const int slen = strlen(module_name) + 1;
    113 	bm->module_name = alloc(slen);
    114 	memcpy(bm->module_name, module_name, slen);
    115 	TAILQ_INSERT_TAIL(&boot_modules, bm, entries);
    116 }
    117 
    118 void
    119 module_remove(const char *module_name)
    120 {
    121 	struct boot_module *bm;
    122 
    123 	/* Trim leading whitespace */
    124 	while (*module_name == ' ' || *module_name == '\t')
    125 		++module_name;
    126 
    127 	TAILQ_FOREACH(bm, &boot_modules, entries) {
    128 		if (strcmp(bm->module_name, module_name) == 0) {
    129 			TAILQ_REMOVE(&boot_modules, bm, entries);
    130 			dealloc(bm->module_name, strlen(bm->module_name) + 1);
    131 			dealloc(bm, sizeof(*bm));
    132 			return;
    133 		}
    134 	}
    135 }
    136 
    137 void
    138 module_remove_all(void)
    139 {
    140 	struct boot_module *bm;
    141 
    142 	while ((bm = TAILQ_FIRST(&boot_modules)) != NULL) {
    143 		TAILQ_REMOVE(&boot_modules, bm, entries);
    144 		dealloc(bm->module_name, strlen(bm->module_name) + 1);
    145 		dealloc(bm, sizeof(*bm));
    146 	}
    147 }
    148 
    149 void
    150 module_enable(int onoff)
    151 {
    152 	module_enabled = onoff;
    153 }
    154