xref: /freebsd/stand/common/modinfo.c (revision 84823cc70824c8d842f503d8c2e6d7b0c2d95b61)
1 /*-
2  * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  *	from: FreeBSD: src/sys/boot/sparc64/loader/metadata.c,v 1.6
27  */
28 #include <stand.h>
29 #include <sys/param.h>
30 #include <sys/linker.h>
31 #include <sys/boot.h>
32 #include <sys/reboot.h>
33 #if defined(LOADER_FDT_SUPPORT)
34 #include <fdt_platform.h>
35 #endif
36 
37 #ifdef __arm__
38 #include <machine/elf.h>
39 #endif
40 #include <machine/metadata.h>
41 
42 #include "bootstrap.h"
43 #include "modinfo.h"
44 
45 /*
46  * Copy module-related data into the load area, where it can be
47  * used as a directory for loaded modules.
48  *
49  * Module data is presented in a self-describing format.  Each datum
50  * is preceded by a 32-bit identifier and a 32-bit size field.
51  *
52  * Currently, the following data are saved:
53  *
54  * MOD_NAME	(variable)		module name (string)
55  * MOD_TYPE	(variable)		module type (string)
56  * MOD_ARGS	(variable)		module parameters (string)
57  * MOD_ADDR	sizeof(vm_offset_t)	module load address
58  * MOD_SIZE	sizeof(size_t)		module size
59  * MOD_METADATA	(variable)		type-specific metadata
60  *
61  * Clients are required to define a MOD_ALIGN(l) macro which rounds the passed
62  * in length to the required alignment for the kernel being booted.
63  */
64 
65 #define COPY32(v, a, c) {			\
66     uint32_t	x = (v);			\
67     if (c)					\
68         archsw.arch_copyin(&x, a, sizeof(x));	\
69     a += sizeof(x);				\
70 }
71 
72 #define MOD_STR(t, a, s, c) {			\
73     COPY32(t, a, c);				\
74     COPY32(strlen(s) + 1, a, c)			\
75     if (c)					\
76         archsw.arch_copyin(s, a, strlen(s) + 1);\
77     a += MOD_ALIGN(strlen(s) + 1);		\
78 }
79 
80 #define MOD_NAME(a, s, c)	MOD_STR(MODINFO_NAME, a, s, c)
81 #define MOD_TYPE(a, s, c)	MOD_STR(MODINFO_TYPE, a, s, c)
82 #define MOD_ARGS(a, s, c)	MOD_STR(MODINFO_ARGS, a, s, c)
83 
84 #define MOD_VAR(t, a, s, c) {			\
85     COPY32(t, a, c);				\
86     COPY32(sizeof(s), a, c);			\
87     if (c)					\
88         archsw.arch_copyin(&s, a, sizeof(s));	\
89     a += MOD_ALIGN(sizeof(s));			\
90 }
91 
92 #define MOD_ADDR(a, s, c)	MOD_VAR(MODINFO_ADDR, a, s, c)
93 #define MOD_SIZE(a, s, c)	MOD_VAR(MODINFO_SIZE, a, s, c)
94 
95 #define MOD_METADATA(a, mm, c) {		\
96     COPY32(MODINFO_METADATA | mm->md_type, a, c);\
97     COPY32(mm->md_size, a, c);			\
98     if (c)					\
99         archsw.arch_copyin(mm->md_data, a, mm->md_size);\
100     a += MOD_ALIGN(mm->md_size);		\
101 }
102 
103 #define MOD_END(a, c) {				\
104     COPY32(MODINFO_END, a, c);			\
105     COPY32(0, a, c);				\
106 }
107 
108 #define MOD_ALIGN(l)	roundup(l, align)
109 
110 vm_offset_t
111 md_copymodules(vm_offset_t addr, bool kern64)
112 {
113 	struct preloaded_file	*fp;
114 	struct file_metadata	*md;
115 	uint64_t		scratch64;
116 	uint32_t		scratch32;
117 	int			c;
118 	int			align;
119 
120 	align = kern64 ? sizeof(uint64_t) : sizeof(uint32_t);
121 	c = addr != 0;
122 	/* start with the first module on the list, should be the kernel */
123 	for (fp = file_findfile(NULL, NULL); fp != NULL; fp = fp->f_next) {
124 
125 		MOD_NAME(addr, fp->f_name, c);	/* this field must come first */
126 		MOD_TYPE(addr, fp->f_type, c);
127 		if (fp->f_args)
128 			MOD_ARGS(addr, fp->f_args, c);
129 		if (kern64) {
130 			scratch64 = fp->f_addr;
131 			MOD_ADDR(addr, scratch64, c);
132 			scratch64 = fp->f_size;
133 			MOD_SIZE(addr, scratch64, c);
134 		} else {
135 			scratch32 = fp->f_addr;
136 #ifdef __arm__
137 			scratch32 -= __elfN(relocation_offset);
138 #endif
139 			MOD_ADDR(addr, scratch32, c);
140 			MOD_SIZE(addr, fp->f_size, c);
141 		}
142 		for (md = fp->f_metadata; md != NULL; md = md->md_next) {
143 			if (!(md->md_type & MODINFOMD_NOCOPY)) {
144 				MOD_METADATA(addr, md, c);
145 			}
146 		}
147 	}
148 	MOD_END(addr, c);
149 	return(addr);
150 }
151 
152 /*
153  * Copy the environment into the load area starting at (addr).
154  * Each variable is formatted as <name>=<value>, with a single nul
155  * separating each variable, and a double nul terminating the environment.
156  */
157 vm_offset_t
158 md_copyenv(vm_offset_t start)
159 {
160 	struct env_var *ep;
161 	vm_offset_t addr, last;
162 	size_t len;
163 
164 	addr = last = start;
165 
166 	/* Traverse the environment. */
167 	for (ep = environ; ep != NULL; ep = ep->ev_next) {
168 		len = strlen(ep->ev_name);
169 		if ((size_t)archsw.arch_copyin(ep->ev_name, addr, len) != len)
170 			break;
171 		addr += len;
172 		if (archsw.arch_copyin("=", addr, 1) != 1)
173 			break;
174 		addr++;
175 		if (ep->ev_value != NULL) {
176 			len = strlen(ep->ev_value);
177 			if ((size_t)archsw.arch_copyin(ep->ev_value, addr, len) != len)
178 				break;
179 			addr += len;
180 		}
181 		if (archsw.arch_copyin("", addr, 1) != 1)
182 			break;
183 		last = ++addr;
184 	}
185 
186 	if (archsw.arch_copyin("", last++, 1) != 1)
187 		last = start;
188 	return(last);
189 }
190