xref: /freebsd/stand/common/modinfo.c (revision 19fae0f66023a97a9b464b3beeeabb2081f575b3)
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 	mm->md_addr = a;			\
101     }						\
102     a += MOD_ALIGN(mm->md_size);		\
103 }
104 
105 #define MOD_END(a, c) {				\
106     COPY32(MODINFO_END, a, c);			\
107     COPY32(0, a, c);				\
108 }
109 
110 #define MOD_ALIGN(l)	roundup(l, align)
111 
112 vm_offset_t
113 md_copymodules(vm_offset_t addr, bool kern64)
114 {
115 	struct preloaded_file	*fp;
116 	struct file_metadata	*md;
117 	uint64_t		scratch64;
118 	uint32_t		scratch32;
119 	int			c;
120 	int			align;
121 
122 	align = kern64 ? sizeof(uint64_t) : sizeof(uint32_t);
123 	c = addr != 0;
124 	/* start with the first module on the list, should be the kernel */
125 	for (fp = file_findfile(NULL, NULL); fp != NULL; fp = fp->f_next) {
126 
127 		MOD_NAME(addr, fp->f_name, c);	/* this field must come first */
128 		MOD_TYPE(addr, fp->f_type, c);
129 		if (fp->f_args)
130 			MOD_ARGS(addr, fp->f_args, c);
131 		if (kern64) {
132 			scratch64 = fp->f_addr;
133 			MOD_ADDR(addr, scratch64, c);
134 			scratch64 = fp->f_size;
135 			MOD_SIZE(addr, scratch64, c);
136 		} else {
137 			scratch32 = fp->f_addr;
138 #ifdef __arm__
139 			scratch32 -= __elfN(relocation_offset);
140 #endif
141 			MOD_ADDR(addr, scratch32, c);
142 			MOD_SIZE(addr, fp->f_size, c);
143 		}
144 		for (md = fp->f_metadata; md != NULL; md = md->md_next) {
145 			if (!(md->md_type & MODINFOMD_NOCOPY)) {
146 				MOD_METADATA(addr, md, c);
147 			}
148 		}
149 	}
150 	MOD_END(addr, c);
151 	return(addr);
152 }
153 
154 /*
155  * Copy the environment into the load area starting at (addr).
156  * Each variable is formatted as <name>=<value>, with a single nul
157  * separating each variable, and a double nul terminating the environment.
158  */
159 vm_offset_t
160 md_copyenv(vm_offset_t start)
161 {
162 	struct env_var *ep;
163 	vm_offset_t addr, last;
164 	size_t len;
165 
166 	addr = last = start;
167 
168 	/* Traverse the environment. */
169 	for (ep = environ; ep != NULL; ep = ep->ev_next) {
170 		len = strlen(ep->ev_name);
171 		if ((size_t)archsw.arch_copyin(ep->ev_name, addr, len) != len)
172 			break;
173 		addr += len;
174 		if (archsw.arch_copyin("=", addr, 1) != 1)
175 			break;
176 		addr++;
177 		if (ep->ev_value != NULL) {
178 			len = strlen(ep->ev_value);
179 			if ((size_t)archsw.arch_copyin(ep->ev_value, addr, len) != len)
180 				break;
181 			addr += len;
182 		}
183 		if (archsw.arch_copyin("", addr, 1) != 1)
184 			break;
185 		last = ++addr;
186 	}
187 
188 	if (archsw.arch_copyin("", last++, 1) != 1)
189 		last = start;
190 	return(last);
191 }
192