Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/stand/common/modinfo.c
34677 views
1
/*-
2
* Copyright (c) 1998 Michael Smith <[email protected]>
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
const char md_modtype[] = MODTYPE;
113
const char md_kerntype[] = KERNTYPE;
114
const char md_modtype_obj[] = MODTYPE_OBJ;
115
const char md_kerntype_mb[] = KERNTYPE_MB;
116
117
vm_offset_t
118
md_copymodules(vm_offset_t addr, bool kern64)
119
{
120
struct preloaded_file *fp;
121
struct file_metadata *md;
122
uint64_t scratch64;
123
uint32_t scratch32;
124
int c;
125
int align;
126
127
align = kern64 ? sizeof(uint64_t) : sizeof(uint32_t);
128
c = addr != 0;
129
/* start with the first module on the list, should be the kernel */
130
for (fp = file_findfile(NULL, NULL); fp != NULL; fp = fp->f_next) {
131
132
MOD_NAME(addr, fp->f_name, c); /* this field must come first */
133
MOD_TYPE(addr, fp->f_type, c);
134
if (fp->f_args)
135
MOD_ARGS(addr, fp->f_args, c);
136
if (kern64) {
137
scratch64 = fp->f_addr;
138
MOD_ADDR(addr, scratch64, c);
139
scratch64 = fp->f_size;
140
MOD_SIZE(addr, scratch64, c);
141
} else {
142
scratch32 = fp->f_addr;
143
#ifdef __arm__
144
scratch32 -= __elfN(relocation_offset);
145
#endif
146
MOD_ADDR(addr, scratch32, c);
147
MOD_SIZE(addr, fp->f_size, c);
148
}
149
for (md = fp->f_metadata; md != NULL; md = md->md_next) {
150
if (!(md->md_type & MODINFOMD_NOCOPY)) {
151
MOD_METADATA(addr, md, c);
152
}
153
}
154
}
155
MOD_END(addr, c);
156
return(addr);
157
}
158
159
/*
160
* Copy the environment into the load area starting at (addr).
161
* Each variable is formatted as <name>=<value>, with a single nul
162
* separating each variable, and a double nul terminating the environment.
163
*/
164
vm_offset_t
165
md_copyenv(vm_offset_t start)
166
{
167
struct env_var *ep;
168
vm_offset_t addr, last;
169
size_t len;
170
171
addr = last = start;
172
173
/* Traverse the environment. */
174
for (ep = environ; ep != NULL; ep = ep->ev_next) {
175
if ((ep->ev_flags & EV_NOKENV) != 0)
176
continue;
177
len = strlen(ep->ev_name);
178
if ((size_t)archsw.arch_copyin(ep->ev_name, addr, len) != len)
179
break;
180
addr += len;
181
if (archsw.arch_copyin("=", addr, 1) != 1)
182
break;
183
addr++;
184
if (ep->ev_value != NULL) {
185
len = strlen(ep->ev_value);
186
if ((size_t)archsw.arch_copyin(ep->ev_value, addr, len) != len)
187
break;
188
addr += len;
189
}
190
if (archsw.arch_copyin("", addr, 1) != 1)
191
break;
192
last = ++addr;
193
}
194
195
if (archsw.arch_copyin("", last++, 1) != 1)
196
last = start;
197
return(last);
198
}
199
200
/*
201
* Take the ending address and round it up to the currently required
202
* alignment. This typically is the page size, but is the larger of the compiled
203
* kernel page size, the loader page size, and the typical page size on the
204
* platform.
205
*
206
* XXX For the moment, it's just PAGE_SIZE to make the refactoring go faster,
207
* but needs to hook-in the replacement of arch_loadaddr.
208
*
209
* Also, we may need other logical things when dealing with different types of
210
* page sizes and/or masking or sizes. This works well for addr and sizes, but
211
* not for masks.
212
*
213
* Also, this is different than the MOD_ALIGN macro above, which is used for
214
* aligning elements in the metadata lists, not for whare modules can begin.
215
*/
216
vm_offset_t
217
md_align(vm_offset_t addr)
218
{
219
return (roundup(addr, PAGE_SIZE));
220
}
221
222